Fixed display issues when different settings were used for various app users (javascr...
[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 = force_open($fpath);
73         if (!$file) return false;
74         if (!fwrite($file, $text)) return false;
75         return fclose($file);
76
77 }
78
79 /*
80         Open file for writing with creration of subfolders if needed.
81 */
82 function force_open($fname)
83 {
84         $file = pathinfo($fname);
85
86         $path = $fname[0] == '/' ? '/' : '';
87         $tree = explode('/', $file['dirname']);
88         foreach($tree as $level) {
89                 $path .= $level;
90                 if (!file_exists($path)) {
91                         if (!mkdir($path)) {
92                                 return null;
93                         }
94                 }
95                 $path .= '/';
96         }
97         return fopen($fname, 'w');
98 }
99
100 function add_js_file($filename) 
101 {
102           global $js_static;
103
104           $search = array_search($filename, $js_static);
105           if ($search === false || $search === null) // php>4.2.0 returns null
106                 $js_static[] = $filename;       
107 }
108
109 function add_js_ufile($filename) 
110 {
111           global $js_userlib;
112
113           $search = array_search($filename, $js_userlib);
114           if ($search === false || $search === null) // php>4.2.0 returns null
115                 $js_userlib[] = $filename;
116 }
117
118 function add_js_source($text) 
119 {
120           global $js_lib;
121           
122           $search = array_search($text, $js_lib);
123           if ($search === false || $search === null) // php>4.2.0 returns null
124                 $js_lib[] = $text;
125 }
126
127 /**
128  * Compresses the Javascript code for more efficient delivery.
129  * copyright (c) 2005 by Jared White & J. Max Wilson
130  * http://www.xajaxproject.org
131  * Added removing comments from output.
132  * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
133  */
134 function js_compress($sJS)
135 {
136         //remove windows cariage returns
137         $sJS = str_replace("\r","",$sJS);
138         
139         //array to store replaced literal strings
140         $literal_strings = array();
141         
142         //explode the string into lines
143         $lines = explode("\n",$sJS);
144         //loop through all the lines, building a new string at the same time as removing literal strings
145         $clean = "";
146         $inComment = false;
147         $literal = "";
148         $inQuote = false;
149         $escaped = false;
150         $quoteChar = "";
151         
152         for($i=0;$i<count($lines);$i++)
153         {
154                 $line = $lines[$i];
155                 $inNormalComment = false;
156         
157                 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
158                 $len = strlen($line);
159                 for($j=0;$j<$len;$j++)
160                 {
161                         $c = $line[$j];         // this is _really_ faster than subst
162                         $d = $c.$line[$j+1];
163         
164                         //look for start of quote
165                         if(!$inQuote && !$inComment)
166                         {
167                                 //is this character a quote or a comment
168                                 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
169                                 {
170                                         $inQuote = true;
171                                         $inComment = false;
172                                         $escaped = false;
173                                         $quoteChar = $c;
174                                         $literal = $c;
175                                 }
176                                 else if($d=="/*" && !$inNormalComment)
177                                 {
178                                         $inQuote = false;
179                                         $inComment = true;
180                                         $escaped = false;
181                                         $quoteChar = $d;
182                                         $literal = $d;  
183                                         $j++;   
184                                 }
185                                 else if($d=="//") //ignore string markers that are found inside comments
186                                 {
187                                         $inNormalComment = true;
188                                         $clean .= $c;
189                                 }
190                                 else
191                                 {
192                                         $clean .= $c;
193                                 }
194                         }
195                         else //allready in a string so find end quote
196                         {
197                                 if($c == $quoteChar && !$escaped && !$inComment)
198                                 {
199                                         $inQuote = false;
200                                         $literal .= $c;
201         
202                                         //subsitute in a marker for the string
203                                         $clean .= "___" . count($literal_strings) . "___";
204         
205                                         //push the string onto our array
206                                         array_push($literal_strings,$literal);
207         
208                                 }
209                                 else if($inComment && $d=="*/")
210                                 {
211                                         $inComment = false;
212                                         $literal .= $d;
213         
214                                         //subsitute in a marker for the string
215                                         $clean .= "___" . count($literal_strings) . "___";
216         
217                                         //push the string onto our array
218                                         array_push($literal_strings,$literal);
219         
220                                         $j++;
221                                 }
222                                 else if($c == "\\" && !$escaped)
223                                         $escaped = true;
224                                 else
225                                         $escaped = false;
226         
227                                 $literal .= $c;
228                         }
229                 }
230                 if($inComment) $literal .= "\n";
231                 $clean .= "\n";
232         }
233         //explode the clean string into lines again
234         $lines = explode("\n",$clean);
235         
236         //now process each line at a time
237         for($i=0;$i<count($lines);$i++)
238         {
239                 $line = $lines[$i];
240         
241                 //remove comments
242                 $line = preg_replace("/\/\/(.*)/","",$line);
243         
244                 //strip leading and trailing whitespace
245                 $line = trim($line);
246         
247                 //remove all whitespace with a single space
248                 $line = preg_replace("/\s+/"," ",$line);
249         
250                 //remove any whitespace that occurs after/before an operator
251                 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
252         
253                 $lines[$i] = $line;
254         }
255         
256         //implode the lines
257         $sJS = implode("\n",$lines);
258         
259         //make sure there is a max of 1 \n after each line
260         $sJS = preg_replace("/[\n]+/","\n",$sJS);
261         
262         //strip out line breaks that immediately follow a semi-colon
263         $sJS = preg_replace("/;\n/",";",$sJS);
264         
265         //curly brackets aren't on their own
266         $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
267         
268         //finally loop through and replace all the literal strings:
269         for($i=0;$i<count($literal_strings);$i++) {
270             if (strpos($literal_strings[$i],"/*")!==false) 
271                 $literal_strings[$i]= '';
272                 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
273         }
274         return $sJS;
275 }
276
277 /*
278         Check if file can be updated, restoring subdirectories 
279         if needed. Returns 1 when no confilcts, -1 when file exists and is writable
280 */
281 function check_write($path)
282 {
283         if ($path == ''//|| $path == '.' || $path == '..'
284         ) return 0;
285
286         return is_writable($path) ? (is_dir($path) ? 1 : -1) 
287                 : (is_file($path) ? 0 : ($path == '.' || $path == '..' ? 0 : check_write(dirname($path))));
288 }
289
290 /*
291         Copies set of files. When $strict is set
292         also removes files from the $to which 
293         does not exists in $from directory but arelisted in $flist.
294 */
295 function copy_files($flist, $from, $to, $strict=false)
296 {
297         foreach ($flist as $file) {
298                 if (file_exists($from.'/'.$file)) {
299                         if (!copy_file($file, $from, $to))
300                                 return false;
301                 } else if ($strict) {
302                                 unlink($to.'/'.$file);
303                 }
304         }
305         return true;
306 }
307
308 /*
309         Copies file from base to target directory, restoring subdirectories 
310         if needed.
311 */
312 function copy_file($file, $from, $to)
313 {
314
315         if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
316                 if (!copy_file(dirname($file), null, $to))
317                         return false;
318         }
319         if (!$from) {
320         //              error_log( 'dodanie katalogu '.$to.'/'.$file);
321                 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
322         }
323         else {
324         //              error_log( 'skopiowanie '.$to.'/'.$file);
325                 return @copy($from.'/'.$file, $to.'/'.$file);
326         }
327 }
328 /*
329         Search for file, looking first for company specific version, then for 
330         version provided by any extension module, finally in main FA directory.
331         Also adds include path for any related files, and sets $local_path_to_root 
332         to enable local translation domains.
333         
334         Returns found file path or null.
335 */
336 function find_custom_file($rep)
337 {
338         global $installed_extensions, $path_to_root, $local_path_to_root;
339
340         // customized per company version
341         $path = company_path();
342         $file = $path.$rep;
343         if (file_exists($file)) {
344                 // add local include path
345                 $local_path_to_root = $path;
346                 set_include_path(dirname($file).PATH_SEPARATOR.get_include_path());
347                 return $file;
348         }
349         // file added by active extension modules
350         if (count($installed_extensions) > 0)
351         {
352                 $extensions = $installed_extensions;
353                 foreach ($extensions as $ext)
354                         if (($ext['active'] && $ext['type'] == 'extension')) {
355                                 $path = $path_to_root.'/'.$ext['path'];
356                                 $file = $path.$rep;
357                                 if (file_exists($file)) {
358                                         set_include_path($path.PATH_SEPARATOR.get_include_path());
359                                         $local_path_to_root = $path;
360                                         return $file;
361                                 }
362                         }
363         }
364         // standard location
365         $file = $path_to_root.$rep;
366         if (file_exists($file))
367                 return $file;
368
369         return null;
370 }
371 /*
372         
373         Protect against directory traversal.
374         Changes all not POSIX compatible chars to underscore.
375 */
376 function clean_file_name($filename) {
377     $filename = str_replace(chr(0), '', $filename);
378     return preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);
379 }
380
381 ?>