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