Eliminated non-static method calls and some more fixes to avoid log warnings on php4&5
[fa-stable.git] / includes / main.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/includes/db/connect_db.inc");
13
14 include_once($path_to_root . "/includes/errors.inc");
15 include_once($path_to_root . "/includes/types.inc");
16 include_once($path_to_root . "/includes/systypes.inc");
17 include_once($path_to_root . "/includes/references.inc");
18 include_once($path_to_root . "/includes/prefs/sysprefs.inc");
19 include_once($path_to_root . "/includes/db/comments_db.inc");
20 include_once($path_to_root . "/includes/db/sql_functions.inc");
21 include_once($path_to_root . "/includes/db/audit_trail_db.inc");
22 //include_once($path_to_root . "/includes/validation.inc");
23
24 include_once($path_to_root . "/admin/db/users_db.inc");
25 include_once($path_to_root . "/includes/ui/ui_view.inc");
26 include_once($path_to_root . "/includes/ui/ui_controls.inc");
27 include_once($path_to_root . "/installed_extensions.php");
28         
29 function page($title, $no_menu=false, $is_index=false, $onload="", $js="", $script_only=false)
30 {
31
32         global $path_to_root;
33
34         $hide_menu = $no_menu;
35
36         include($path_to_root . "/includes/page/header.inc");
37
38         page_header($title, $no_menu, $is_index, $onload, $js);
39 //      error_box();
40         if($script_only) {              
41                 echo '<noscript>';
42                 echo display_heading(_('This page is usable only with javascript enabled browsers.'));
43                 echo '</noscript>';
44                 div_start('_page_body', null, true);
45         } else {
46                 div_start('_page_body'); // whole page content for ajax reloading
47         }
48 }
49
50 function end_page($no_menu=false, $is_index=false)
51 {
52         global $path_to_root, $Ajax;
53         $hide_menu = $no_menu;
54                 div_end();      // _page_body section
55         include($path_to_root . "/includes/page/footer.inc");
56         page_footer($no_menu, $is_index);
57 }
58
59 function flush_dir($path, $wipe = false) 
60 {
61         $dir = opendir($path);
62         while(false !== ($fname = readdir($dir))) {
63                 if($fname=='.' || $fname=='..' || (!$wipe && $fname=='index.php')) continue;
64                 if(is_dir($path.'/'.$fname)) {
65                     flush_dir($path.'/'.$fname, $wipe);
66                     if ($wipe) @rmdir($path.'/'.$fname);
67                 } else
68                     @unlink($path.'/'.$fname);
69         }
70 }
71
72 function cache_js_file($fpath, $text) 
73 {
74         global $go_debug;
75
76         if(!$go_debug) $text = js_compress($text);
77
78         $file = fopen($fpath, 'w');
79         if (!$file) return false;
80         if (!fwrite($file, $text)) return false;
81         return fclose($file);
82
83 }
84
85 function add_js_file($filename) 
86 {
87           global $js_static;
88
89           $search = array_search($filename, $js_static);
90           if ($search === false || $search === null) // php>4.2.0 returns null
91                 $js_static[] = $filename;       
92 }
93
94 function add_js_ufile($filename) 
95 {
96           global $js_userlib;
97
98           $search = array_search($filename, $js_userlib);
99           if ($search === false || $search === null) // php>4.2.0 returns null
100                 $js_userlib[] = $filename;
101 }
102
103 function add_js_source($text) 
104 {
105           global $js_lib;
106           
107           $search = array_search($text, $js_lib);
108           if ($search === false || $search === null) // php>4.2.0 returns null
109                 $js_lib[] = $text;
110 }
111
112 /**
113  * Compresses the Javascript code for more efficient delivery.
114  * copyright (c) 2005 by Jared White & J. Max Wilson
115  * http://www.xajaxproject.org
116  * Added removing comments from output.
117  * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
118  */
119 function js_compress($sJS)
120 {
121         //remove windows cariage returns
122         $sJS = str_replace("\r","",$sJS);
123         
124         //array to store replaced literal strings
125         $literal_strings = array();
126         
127         //explode the string into lines
128         $lines = explode("\n",$sJS);
129         //loop through all the lines, building a new string at the same time as removing literal strings
130         $clean = "";
131         $inComment = false;
132         $literal = "";
133         $inQuote = false;
134         $escaped = false;
135         $quoteChar = "";
136         
137         for($i=0;$i<count($lines);$i++)
138         {
139                 $line = $lines[$i];
140                 $inNormalComment = false;
141         
142                 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
143                 $len = strlen($line);
144                 for($j=0;$j<$len;$j++)
145                 {
146                         $c = $line[$j];         // this is _really_ faster than subst
147                         $d = $c.$line[$j+1];
148         
149                         //look for start of quote
150                         if(!$inQuote && !$inComment)
151                         {
152                                 //is this character a quote or a comment
153                                 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
154                                 {
155                                         $inQuote = true;
156                                         $inComment = false;
157                                         $escaped = false;
158                                         $quoteChar = $c;
159                                         $literal = $c;
160                                 }
161                                 else if($d=="/*" && !$inNormalComment)
162                                 {
163                                         $inQuote = false;
164                                         $inComment = true;
165                                         $escaped = false;
166                                         $quoteChar = $d;
167                                         $literal = $d;  
168                                         $j++;   
169                                 }
170                                 else if($d=="//") //ignore string markers that are found inside comments
171                                 {
172                                         $inNormalComment = true;
173                                         $clean .= $c;
174                                 }
175                                 else
176                                 {
177                                         $clean .= $c;
178                                 }
179                         }
180                         else //allready in a string so find end quote
181                         {
182                                 if($c == $quoteChar && !$escaped && !$inComment)
183                                 {
184                                         $inQuote = false;
185                                         $literal .= $c;
186         
187                                         //subsitute in a marker for the string
188                                         $clean .= "___" . count($literal_strings) . "___";
189         
190                                         //push the string onto our array
191                                         array_push($literal_strings,$literal);
192         
193                                 }
194                                 else if($inComment && $d=="*/")
195                                 {
196                                         $inComment = false;
197                                         $literal .= $d;
198         
199                                         //subsitute in a marker for the string
200                                         $clean .= "___" . count($literal_strings) . "___";
201         
202                                         //push the string onto our array
203                                         array_push($literal_strings,$literal);
204         
205                                         $j++;
206                                 }
207                                 else if($c == "\\" && !$escaped)
208                                         $escaped = true;
209                                 else
210                                         $escaped = false;
211         
212                                 $literal .= $c;
213                         }
214                 }
215                 if($inComment) $literal .= "\n";
216                 $clean .= "\n";
217         }
218         //explode the clean string into lines again
219         $lines = explode("\n",$clean);
220         
221         //now process each line at a time
222         for($i=0;$i<count($lines);$i++)
223         {
224                 $line = $lines[$i];
225         
226                 //remove comments
227                 $line = preg_replace("/\/\/(.*)/","",$line);
228         
229                 //strip leading and trailing whitespace
230                 $line = trim($line);
231         
232                 //remove all whitespace with a single space
233                 $line = preg_replace("/\s+/"," ",$line);
234         
235                 //remove any whitespace that occurs after/before an operator
236                 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
237         
238                 $lines[$i] = $line;
239         }
240         
241         //implode the lines
242         $sJS = implode("\n",$lines);
243         
244         //make sure there is a max of 1 \n after each line
245         $sJS = preg_replace("/[\n]+/","\n",$sJS);
246         
247         //strip out line breaks that immediately follow a semi-colon
248         $sJS = preg_replace("/;\n/",";",$sJS);
249         
250         //curly brackets aren't on their own
251         $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
252         
253         //finally loop through and replace all the literal strings:
254         for($i=0;$i<count($literal_strings);$i++) {
255             if (strpos($literal_strings[$i],"/*")!==false) 
256                 $literal_strings[$i]= '';
257                 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
258         }
259         return $sJS;
260 }
261 //-----------------------------------------------------------------------------
262 //      Inserts $elements into $array at position $index.
263 //      $elements is list of any objects
264 //
265 function array_insert(&$array, $index, $elements)
266 {
267         if (!is_array($elements)) $elements = array($elements);
268
269         $head  = array_splice($array, 0, $index);
270         $array = array_merge($head, $elements, $array);
271 }
272
273 function array_remove(&$array, $index, $len=1)
274 {
275         array_splice($array, $index, $len);
276 }
277
278 function array_replace(&$array, $index, $len, $elements)
279 {
280         array_splice($array, $index, $len);
281         array_insert($array, $index, $elements);
282 }
283
284 function array_append(&$array, $elements)
285 {
286         foreach($elements as $key => $el) {
287                 if(is_int($key))
288                         $array[] = $el;
289                 else
290                         $array[$key] = $el;
291         }
292 }
293
294 ?>