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