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