Merged latest changes from stable branch.
[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/types.inc");
15 include_once($path_to_root . "/includes/systypes.inc");
16 include_once($path_to_root . "/includes/references.inc");
17 include_once($path_to_root . "/includes/db/comments_db.inc");
18 include_once($path_to_root . "/includes/db/sql_functions.inc");
19 include_once($path_to_root . "/includes/db/audit_trail_db.inc");
20 //include_once($path_to_root . "/includes/validation.inc");
21
22 include_once($path_to_root . "/admin/db/users_db.inc");
23 include_once($path_to_root . "/includes/ui/ui_view.inc");
24 include_once($path_to_root . "/includes/ui/ui_controls.inc");
25
26 $page_nested = -1;
27
28 function page($title, $no_menu=false, $is_index=false, $onload="", $js="", $script_only=false, $css='')
29 {
30
31         global $path_to_root, $page_security, $page_nested;
32
33         if (++$page_nested) return;
34
35         $hide_menu = $no_menu;
36
37         include_once($path_to_root . "/includes/page/header.inc");
38
39         page_header($title, $no_menu, $is_index, $onload, $js, $css);
40         check_page_security($page_security);
41 //      error_box();
42         if($script_only) {
43                 echo '<noscript>';
44                 echo display_heading(_('This page is usable only with javascript enabled browsers.'));
45                 echo '</noscript>';
46                 div_start('_page_body', null, true);
47         } else {
48                 div_start('_page_body'); // whole page content for ajax reloading
49         }
50 }
51
52 function end_page($no_menu=false, $is_index=false, $final_screen=false, $type_no=0, $trans_no=0)
53 {
54         global $path_to_root, $page_nested;
55
56         if ($page_nested-- > 0) return;
57
58         if (!$is_index && function_exists('hyperlink_back'))
59                 hyperlink_back(true, $no_menu, $type_no, $trans_no, $final_screen);
60         div_end();      // end of _page_body section
61
62         include_once($path_to_root . "/includes/page/footer.inc");
63         page_footer($no_menu, $is_index);
64 }
65
66 function cache_js_file($fpath, $text) 
67 {
68         global $go_debug;
69
70         if(!$go_debug) $text = js_compress($text);
71
72         $file = fopen($fpath, 'w');
73         if (!$file) return false;
74         if (!fwrite($file, $text)) return false;
75         return fclose($file);
76
77 }
78
79 function add_js_file($filename) 
80 {
81           global $js_static;
82
83           $search = array_search($filename, $js_static);
84           if ($search === false || $search === null) // php>4.2.0 returns null
85                 $js_static[] = $filename;       
86 }
87
88 function add_js_ufile($filename) 
89 {
90           global $js_userlib;
91
92           $search = array_search($filename, $js_userlib);
93           if ($search === false || $search === null) // php>4.2.0 returns null
94                 $js_userlib[] = $filename;
95 }
96
97 function add_js_source($text) 
98 {
99           global $js_lib;
100           
101           $search = array_search($text, $js_lib);
102           if ($search === false || $search === null) // php>4.2.0 returns null
103                 $js_lib[] = $text;
104 }
105
106 /**
107  * Compresses the Javascript code for more efficient delivery.
108  * copyright (c) 2005 by Jared White & J. Max Wilson
109  * http://www.xajaxproject.org
110  * Added removing comments from output.
111  * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
112  */
113 function js_compress($sJS)
114 {
115         //remove windows cariage returns
116         $sJS = str_replace("\r","",$sJS);
117         
118         //array to store replaced literal strings
119         $literal_strings = array();
120         
121         //explode the string into lines
122         $lines = explode("\n",$sJS);
123         //loop through all the lines, building a new string at the same time as removing literal strings
124         $clean = "";
125         $inComment = false;
126         $literal = "";
127         $inQuote = false;
128         $escaped = false;
129         $quoteChar = "";
130         
131         for($i=0;$i<count($lines);$i++)
132         {
133                 $line = $lines[$i];
134                 $inNormalComment = false;
135         
136                 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
137                 $len = strlen($line);
138                 for($j=0;$j<$len;$j++)
139                 {
140                         $c = $line[$j];         // this is _really_ faster than subst
141                         $d = $c.$line[$j+1];
142         
143                         //look for start of quote
144                         if(!$inQuote && !$inComment)
145                         {
146                                 //is this character a quote or a comment
147                                 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
148                                 {
149                                         $inQuote = true;
150                                         $inComment = false;
151                                         $escaped = false;
152                                         $quoteChar = $c;
153                                         $literal = $c;
154                                 }
155                                 else if($d=="/*" && !$inNormalComment)
156                                 {
157                                         $inQuote = false;
158                                         $inComment = true;
159                                         $escaped = false;
160                                         $quoteChar = $d;
161                                         $literal = $d;  
162                                         $j++;   
163                                 }
164                                 else if($d=="//") //ignore string markers that are found inside comments
165                                 {
166                                         $inNormalComment = true;
167                                         $clean .= $c;
168                                 }
169                                 else
170                                 {
171                                         $clean .= $c;
172                                 }
173                         }
174                         else //allready in a string so find end quote
175                         {
176                                 if($c == $quoteChar && !$escaped && !$inComment)
177                                 {
178                                         $inQuote = false;
179                                         $literal .= $c;
180         
181                                         //subsitute in a marker for the string
182                                         $clean .= "___" . count($literal_strings) . "___";
183         
184                                         //push the string onto our array
185                                         array_push($literal_strings,$literal);
186         
187                                 }
188                                 else if($inComment && $d=="*/")
189                                 {
190                                         $inComment = false;
191                                         $literal .= $d;
192         
193                                         //subsitute in a marker for the string
194                                         $clean .= "___" . count($literal_strings) . "___";
195         
196                                         //push the string onto our array
197                                         array_push($literal_strings,$literal);
198         
199                                         $j++;
200                                 }
201                                 else if($c == "\\" && !$escaped)
202                                         $escaped = true;
203                                 else
204                                         $escaped = false;
205         
206                                 $literal .= $c;
207                         }
208                 }
209                 if($inComment) $literal .= "\n";
210                 $clean .= "\n";
211         }
212         //explode the clean string into lines again
213         $lines = explode("\n",$clean);
214         
215         //now process each line at a time
216         for($i=0;$i<count($lines);$i++)
217         {
218                 $line = $lines[$i];
219         
220                 //remove comments
221                 $line = preg_replace("/\/\/(.*)/","",$line);
222         
223                 //strip leading and trailing whitespace
224                 $line = trim($line);
225         
226                 //remove all whitespace with a single space
227                 $line = preg_replace("/\s+/"," ",$line);
228         
229                 //remove any whitespace that occurs after/before an operator
230                 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
231         
232                 $lines[$i] = $line;
233         }
234         
235         //implode the lines
236         $sJS = implode("\n",$lines);
237         
238         //make sure there is a max of 1 \n after each line
239         $sJS = preg_replace("/[\n]+/","\n",$sJS);
240         
241         //strip out line breaks that immediately follow a semi-colon
242         $sJS = preg_replace("/;\n/",";",$sJS);
243         
244         //curly brackets aren't on their own
245         $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
246         
247         //finally loop through and replace all the literal strings:
248         for($i=0;$i<count($literal_strings);$i++) {
249             if (strpos($literal_strings[$i],"/*")!==false) 
250                 $literal_strings[$i]= '';
251                 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
252         }
253         return $sJS;
254 }
255
256 /*
257         Check if file can be updated, restoring subdirectories 
258         if needed. Returns 1 when no confilcts, -1 when file exists and is writable
259 */
260 function check_write($path)
261 {
262         if ($path == ''//|| $path == '.' || $path == '..'
263         ) return 0;
264         
265         return is_writable($path) ? (is_dir($path) ? 1 : -1) 
266                 : (is_file($path) ? 0 : ($path == '.' ? 0 : check_write(dirname($path))));
267 }
268
269 /*
270         Copies set of files. When $strict is set
271         also removes files from the $to which 
272         does not exists in $from directory but arelisted in $flist.
273 */
274 function copy_files($flist, $from, $to, $strict=false)
275 {
276         foreach ($flist as $file) {
277                 if (file_exists($from.'/'.$file)) {
278                         if (!copy_file($file, $from, $to))
279                                 return false;
280                 } else if ($strict) {
281                                 unlink($to.'/'.$file);
282                 }
283         }
284         return true;
285 }
286
287 /*
288         Copies file from base to target directory, restoring subdirectories 
289         if needed.
290 */
291 function copy_file($file, $from, $to)
292 {
293
294         if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
295                 if (!copy_file(dirname($file), null, $to))
296                         return false;
297         }
298         if (!$from) {
299         //              error_log( 'dodanie katalogu '.$to.'/'.$file);
300                 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
301         }
302         else {
303         //              error_log( 'skopiowanie '.$to.'/'.$file);
304                 return @copy($from.'/'.$file, $to.'/'.$file);
305         }
306 }
307 /*
308         Search for file, looking first for company specific version, then for 
309         version provided by any extension module, finally in main FA directory.
310         Also adds include path for any related files, and sets $local_path_to_root 
311         to enable local translation domains.
312         
313         Returns found file path or null.
314 */
315 function find_custom_file($rep)
316 {
317         global $installed_extensions, $path_to_root, $local_path_to_root;
318
319         // customized per company version
320         $path = company_path();
321         $file = $path.$rep;
322         if (file_exists($file)) {
323                 // add local include path
324                 $local_path_to_root = $path;
325                 set_include_path(dirname($file).PATH_SEPARATOR.get_include_path());
326                 return $file;
327         }
328         // file added by active extension modules
329         if (count($installed_extensions) > 0)
330         {
331                 $extensions = $installed_extensions;
332                 foreach ($extensions as $ext)
333                         if (($ext['active'] && $ext['type'] == 'extension')) {
334                                 $path = $path_to_root.'/'.$ext['path'];
335                                 $file = $path.$rep;
336                                 if (file_exists($file)) {
337                                         set_include_path($path.PATH_SEPARATOR.get_include_path());
338                                         $local_path_to_root = $path;
339                                         return $file;
340                                 }
341                         }
342         }
343         // standard location
344         $file = $path_to_root.$rep;
345         if (file_exists($file))
346                 return $file;
347
348         return null;
349 }
350 /*
351         
352         Protect against directory traversal.
353         Changes all not POSIX compatible chars to underscore.
354 */
355 function clean_file_name($filename) {
356     return preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);
357 }
358
359 ?>