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