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