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