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