Earlier error handler binding.
[fa-stable.git] / includes / main.inc
1 <?php
2
3     include_once($path_to_root . "/includes/db/connect_db.inc");
4
5     include_once($path_to_root . "/includes/reserved.inc");
6     include_once($path_to_root . "/includes/errors.inc");
7     include_once($path_to_root . "/includes/types.inc");
8     include_once($path_to_root . "/includes/systypes.inc");
9     include_once($path_to_root . "/includes/references.inc");
10     include_once($path_to_root . "/includes/prefs/sysprefs.inc");
11     include_once($path_to_root . "/includes/db/comments_db.inc");
12     include_once($path_to_root . "/includes/db/sql_functions.inc");
13
14     include_once($path_to_root . "/admin/db/users_db.inc");
15     include_once($path_to_root . "/includes/ui/ui_view.inc");
16         
17     function page($title, $no_menu=false, $is_index=false, $onload="", $js="")
18     {
19
20         global $path_to_root;
21
22         $hide_menu = $no_menu;
23
24         include($path_to_root . "/includes/page/header.inc");
25
26         page_header($title, $no_menu, $is_index, $onload, $js);
27                 error_box();
28     }
29
30     function end_page($no_menu=false, $is_index=false)
31     {
32         global $path_to_root, $Ajax;
33
34         $hide_menu = $no_menu;
35
36         include($path_to_root . "/includes/page/footer.inc");
37
38                 $Ajax->run();
39         page_footer($no_menu, $is_index);
40     }
41
42     function flush_dir($path) {
43         $dir = opendir($path);
44         while(false !== ($fname = readdir($dir))) {
45                 if($fname=='.' || $fname=='..') continue;
46                 if(is_dir($path.'/'.$fname)) {
47                     flush_dir($path.'/'.$fname);
48                     @rmdir($path.'/'.$fname);
49                 } else
50                     @unlink($path.'/'.$fname);
51         }
52     }
53
54     function cache_js_file($fpath, $text) 
55     {
56         global $go_debug;
57
58         if(!$go_debug) $text = js_compress($text);
59
60         $file = fopen($fpath, 'w');
61         if (!$file) return false;
62         if (!fwrite($file, $text)) return false;
63         return fclose($file);
64
65     }
66
67     function add_js_file($filename) 
68     {
69           global $js_static;
70
71           $search = array_search($filename, $js_static);
72           if ($search === false || $search === null) // php>4.2.0 returns null
73                 $js_static[] = $filename;       
74     }
75
76     function add_js_ufile($filename) 
77     {
78           global $js_userlib;
79
80           $search = array_search($filename, $js_userlib);
81           if ($search === false || $search === null) // php>4.2.0 returns null
82                 $js_userlib[] = $filename;
83     }
84
85     function add_js_source($text) 
86     {
87           global $js_lib;
88           
89           $search = array_search($text, $js_lib);
90           if ($search === false || $search === null) // php>4.2.0 returns null
91                 $js_lib[] = $text;
92     }
93
94 /**
95  * Compresses the Javascript code for more efficient delivery.
96  * copyright (c) 2005 by Jared White & J. Max Wilson
97  * http://www.xajaxproject.org
98  * Added removing comments from output.
99  * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
100  */
101 function js_compress($sJS)
102 {
103         //remove windows cariage returns
104         $sJS = str_replace("\r","",$sJS);
105         
106         //array to store replaced literal strings
107         $literal_strings = array();
108         
109         //explode the string into lines
110         $lines = explode("\n",$sJS);
111         //loop through all the lines, building a new string at the same time as removing literal strings
112         $clean = "";
113         $inComment = false;
114         $literal = "";
115         $inQuote = false;
116         $escaped = false;
117         $quoteChar = "";
118         
119         for($i=0;$i<count($lines);$i++)
120         {
121                 $line = $lines[$i];
122                 $inNormalComment = false;
123         
124                 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
125                 for($j=0;$j<strlen($line);$j++)
126                 {
127                         $c = substr($line,$j,1);
128                         $d = substr($line,$j,2);
129         
130                         //look for start of quote
131                         if(!$inQuote && !$inComment)
132                         {
133                                 //is this character a quote or a comment
134                                 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
135                                 {
136                                         $inQuote = true;
137                                         $inComment = false;
138                                         $escaped = false;
139                                         $quoteChar = $c;
140                                         $literal = $c;
141                                 }
142                                 else if($d=="/*" && !$inNormalComment)
143                                 {
144                                         $inQuote = false;
145                                         $inComment = true;
146                                         $escaped = false;
147                                         $quoteChar = $d;
148                                         $literal = $d;  
149                                         $j++;   
150                                 }
151                                 else if($d=="//") //ignore string markers that are found inside comments
152                                 {
153                                         $inNormalComment = true;
154                                         $clean .= $c;
155                                 }
156                                 else
157                                 {
158                                         $clean .= $c;
159                                 }
160                         }
161                         else //allready in a string so find end quote
162                         {
163                                 if($c == $quoteChar && !$escaped && !$inComment)
164                                 {
165                                         $inQuote = false;
166                                         $literal .= $c;
167         
168                                         //subsitute in a marker for the string
169                                         $clean .= "___" . count($literal_strings) . "___";
170         
171                                         //push the string onto our array
172                                         array_push($literal_strings,$literal);
173         
174                                 }
175                                 else if($inComment && $d=="*/")
176                                 {
177                                         $inComment = false;
178                                         $literal .= $d;
179         
180                                         //subsitute in a marker for the string
181                                         $clean .= "___" . count($literal_strings) . "___";
182         
183                                         //push the string onto our array
184                                         array_push($literal_strings,$literal);
185         
186                                         $j++;
187                                 }
188                                 else if($c == "\\" && !$escaped)
189                                         $escaped = true;
190                                 else
191                                         $escaped = false;
192         
193                                 $literal .= $c;
194                         }
195                 }
196                 if($inComment) $literal .= "\n";
197                 $clean .= "\n";
198         }
199         //explode the clean string into lines again
200         $lines = explode("\n",$clean);
201         
202         //now process each line at a time
203         for($i=0;$i<count($lines);$i++)
204         {
205                 $line = $lines[$i];
206         
207                 //remove comments
208                 $line = preg_replace("/\/\/(.*)/","",$line);
209         
210                 //strip leading and trailing whitespace
211                 $line = trim($line);
212         
213                 //remove all whitespace with a single space
214                 $line = preg_replace("/\s+/"," ",$line);
215         
216                 //remove any whitespace that occurs after/before an operator
217                 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
218         
219                 $lines[$i] = $line;
220         }
221         
222         //implode the lines
223         $sJS = implode("\n",$lines);
224         
225         //make sure there is a max of 1 \n after each line
226         $sJS = preg_replace("/[\n]+/","\n",$sJS);
227         
228         //strip out line breaks that immediately follow a semi-colon
229         $sJS = preg_replace("/;\n/",";",$sJS);
230         
231         //curly brackets aren't on their own
232         $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
233         
234         //finally loop through and replace all the literal strings:
235         for($i=0;$i<count($literal_strings);$i++) {
236             if (strpos($literal_strings[$i],"/*")!==false) 
237                 $literal_strings[$i]= '';
238                 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
239         }
240         return $sJS;
241 }
242 ?>