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