Files manipulation helpers added.
[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)
51 {
52         global $path_to_root;
53
54         if (!$is_index && !$hide_back_link && function_exists('hyperlink_back'))
55                 hyperlink_back(true, $no_menu);
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 flush_dir($path, $wipe = false) 
63 {
64         $dir = opendir($path);
65         while(false !== ($fname = readdir($dir))) {
66                 if($fname=='.' || $fname=='..' || $fname=='CVS' || (!$wipe && $fname=='index.php')) continue;
67                 if(is_dir($path.'/'.$fname)) {
68                     flush_dir($path.'/'.$fname, $wipe);
69                     if ($wipe) @rmdir($path.'/'.$fname);
70                 } else
71                     @unlink($path.'/'.$fname);
72         }
73 }
74
75 function cache_js_file($fpath, $text) 
76 {
77         global $go_debug;
78
79         if(!$go_debug) $text = js_compress($text);
80
81         $file = fopen($fpath, 'w');
82         if (!$file) return false;
83         if (!fwrite($file, $text)) return false;
84         return fclose($file);
85
86 }
87
88 function add_js_file($filename) 
89 {
90           global $js_static;
91
92           $search = array_search($filename, $js_static);
93           if ($search === false || $search === null) // php>4.2.0 returns null
94                 $js_static[] = $filename;       
95 }
96
97 function add_js_ufile($filename) 
98 {
99           global $js_userlib;
100
101           $search = array_search($filename, $js_userlib);
102           if ($search === false || $search === null) // php>4.2.0 returns null
103                 $js_userlib[] = $filename;
104 }
105
106 function add_js_source($text) 
107 {
108           global $js_lib;
109           
110           $search = array_search($text, $js_lib);
111           if ($search === false || $search === null) // php>4.2.0 returns null
112                 $js_lib[] = $text;
113 }
114
115 /**
116  * Compresses the Javascript code for more efficient delivery.
117  * copyright (c) 2005 by Jared White & J. Max Wilson
118  * http://www.xajaxproject.org
119  * Added removing comments from output.
120  * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
121  */
122 function js_compress($sJS)
123 {
124         //remove windows cariage returns
125         $sJS = str_replace("\r","",$sJS);
126         
127         //array to store replaced literal strings
128         $literal_strings = array();
129         
130         //explode the string into lines
131         $lines = explode("\n",$sJS);
132         //loop through all the lines, building a new string at the same time as removing literal strings
133         $clean = "";
134         $inComment = false;
135         $literal = "";
136         $inQuote = false;
137         $escaped = false;
138         $quoteChar = "";
139         
140         for($i=0;$i<count($lines);$i++)
141         {
142                 $line = $lines[$i];
143                 $inNormalComment = false;
144         
145                 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
146                 $len = strlen($line);
147                 for($j=0;$j<$len;$j++)
148                 {
149                         $c = $line[$j];         // this is _really_ faster than subst
150                         $d = $c.$line[$j+1];
151         
152                         //look for start of quote
153                         if(!$inQuote && !$inComment)
154                         {
155                                 //is this character a quote or a comment
156                                 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
157                                 {
158                                         $inQuote = true;
159                                         $inComment = false;
160                                         $escaped = false;
161                                         $quoteChar = $c;
162                                         $literal = $c;
163                                 }
164                                 else if($d=="/*" && !$inNormalComment)
165                                 {
166                                         $inQuote = false;
167                                         $inComment = true;
168                                         $escaped = false;
169                                         $quoteChar = $d;
170                                         $literal = $d;  
171                                         $j++;   
172                                 }
173                                 else if($d=="//") //ignore string markers that are found inside comments
174                                 {
175                                         $inNormalComment = true;
176                                         $clean .= $c;
177                                 }
178                                 else
179                                 {
180                                         $clean .= $c;
181                                 }
182                         }
183                         else //allready in a string so find end quote
184                         {
185                                 if($c == $quoteChar && !$escaped && !$inComment)
186                                 {
187                                         $inQuote = false;
188                                         $literal .= $c;
189         
190                                         //subsitute in a marker for the string
191                                         $clean .= "___" . count($literal_strings) . "___";
192         
193                                         //push the string onto our array
194                                         array_push($literal_strings,$literal);
195         
196                                 }
197                                 else if($inComment && $d=="*/")
198                                 {
199                                         $inComment = false;
200                                         $literal .= $d;
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                                         $j++;
209                                 }
210                                 else if($c == "\\" && !$escaped)
211                                         $escaped = true;
212                                 else
213                                         $escaped = false;
214         
215                                 $literal .= $c;
216                         }
217                 }
218                 if($inComment) $literal .= "\n";
219                 $clean .= "\n";
220         }
221         //explode the clean string into lines again
222         $lines = explode("\n",$clean);
223         
224         //now process each line at a time
225         for($i=0;$i<count($lines);$i++)
226         {
227                 $line = $lines[$i];
228         
229                 //remove comments
230                 $line = preg_replace("/\/\/(.*)/","",$line);
231         
232                 //strip leading and trailing whitespace
233                 $line = trim($line);
234         
235                 //remove all whitespace with a single space
236                 $line = preg_replace("/\s+/"," ",$line);
237         
238                 //remove any whitespace that occurs after/before an operator
239                 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
240         
241                 $lines[$i] = $line;
242         }
243         
244         //implode the lines
245         $sJS = implode("\n",$lines);
246         
247         //make sure there is a max of 1 \n after each line
248         $sJS = preg_replace("/[\n]+/","\n",$sJS);
249         
250         //strip out line breaks that immediately follow a semi-colon
251         $sJS = preg_replace("/;\n/",";",$sJS);
252         
253         //curly brackets aren't on their own
254         $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
255         
256         //finally loop through and replace all the literal strings:
257         for($i=0;$i<count($literal_strings);$i++) {
258             if (strpos($literal_strings[$i],"/*")!==false) 
259                 $literal_strings[$i]= '';
260                 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
261         }
262         return $sJS;
263 }
264
265 /*
266         Check if file can be updated, restoring subdirectories 
267         if needed. Returns 1 when no confilcts, -1 when file exists and is writable
268 */
269 function check_write($path)
270 {
271         if ($path == ''//|| $path == '.' || $path == '..'
272         ) return 0;
273         
274         return is_writable($path) ? (is_dir($path) ? 1 : -1) 
275                 : ($path =='.' ? 0 : check_write(dirname($path)));
276 }
277
278 /*
279         Copies set of files. When $strict is set
280         also removes files from the $to which 
281         does not exists in $from directory but arelisted in $flist.
282 */
283 function copy_files($flist, $from, $to, $strict=false)
284 {
285         foreach ($flist as $file) {
286                 if (file_exists($from.'/'.$file))
287                         if (!copy_file($file, $from, $to))
288                                 return false;
289                 if ($strict && !is_file($from.'/'.$file)) // if 
290                                 unlink($to.'/'.$file);
291         }
292         return true;
293 }
294
295 /*
296         Copies file from base to target directory, restoring subdirectories 
297         if needed.
298 */
299 function copy_file($file, $from, $to)
300 {
301
302         if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
303                 if (!copy_file(dirname($file), null, $to))
304                         return false;
305         }
306         if (!$from) {
307         //              error_log( 'dodanie katalogu '.$to.'/'.$file);
308                 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
309         }
310         else {
311         //              error_log( 'skopiowanie '.$to.'/'.$file);
312                 return @copy($from.'/'.$file, $to.'/'.$file);
313         }
314 }
315
316 ?>