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");
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");
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");
28 function page($title, $no_menu=false, $is_index=false, $onload="", $js="", $script_only=false, $css='')
31 global $path_to_root, $page_security, $page_nested;
33 if (++$page_nested) return;
35 $hide_menu = $no_menu;
37 include_once($path_to_root . "/includes/page/header.inc");
39 page_header($title, $no_menu, $is_index, $onload, $js, $css);
40 check_page_security($page_security);
44 echo display_heading(_('This page is usable only with javascript enabled browsers.'));
46 div_start('_page_body', null, true);
48 div_start('_page_body'); // whole page content for ajax reloading
52 function end_page($no_menu=false, $is_index=false, $final_screen=false, $type_no=0, $trans_no=0)
54 global $path_to_root, $page_nested;
56 if ($page_nested-- > 0) return;
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
62 include_once($path_to_root . "/includes/page/footer.inc");
63 page_footer($no_menu, $is_index);
66 function cache_js_file($fpath, $text)
70 if(!$go_debug) $text = js_compress($text);
72 $file = force_open($fpath);
73 if (!$file) return false;
74 if (!fwrite($file, $text)) return false;
80 Open file for writing with creration of subfolders if needed.
82 function force_open($fname)
84 $file = pathinfo($fname);
86 $path = $fname[0] == '/' ? '/' : '';
87 $tree = explode('/', $file['dirname']);
88 foreach($tree as $level) {
90 if (!file_exists($path)) {
97 return fopen($fname, 'w');
100 function add_js_file($filename)
104 $search = array_search($filename, $js_static);
105 if ($search === false || $search === null) // php>4.2.0 returns null
106 $js_static[] = $filename;
109 function add_js_ufile($filename)
113 $search = array_search($filename, $js_userlib);
114 if ($search === false || $search === null) // php>4.2.0 returns null
115 $js_userlib[] = $filename;
118 function add_js_source($text)
122 $search = array_search($text, $js_lib);
123 if ($search === false || $search === null) // php>4.2.0 returns null
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.
134 function js_compress($sJS)
136 //remove windows cariage returns
137 $sJS = str_replace("\r","",$sJS);
139 //array to store replaced literal strings
140 $literal_strings = array();
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
152 for($i=0;$i<count($lines);$i++)
155 $inNormalComment = false;
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++)
161 $c = $line[$j]; // this is _really_ faster than subst
164 //look for start of quote
165 if(!$inQuote && !$inComment)
167 //is this character a quote or a comment
168 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
176 else if($d=="/*" && !$inNormalComment)
185 else if($d=="//") //ignore string markers that are found inside comments
187 $inNormalComment = true;
195 else //allready in a string so find end quote
197 if($c == $quoteChar && !$escaped && !$inComment)
202 //subsitute in a marker for the string
203 $clean .= "___" . count($literal_strings) . "___";
205 //push the string onto our array
206 array_push($literal_strings,$literal);
209 else if($inComment && $d=="*/")
214 //subsitute in a marker for the string
215 $clean .= "___" . count($literal_strings) . "___";
217 //push the string onto our array
218 array_push($literal_strings,$literal);
222 else if($c == "\\" && !$escaped)
230 if($inComment) $literal .= "\n";
233 //explode the clean string into lines again
234 $lines = explode("\n",$clean);
236 //now process each line at a time
237 for($i=0;$i<count($lines);$i++)
242 $line = preg_replace("/\/\/(.*)/","",$line);
244 //strip leading and trailing whitespace
247 //remove all whitespace with a single space
248 $line = preg_replace("/\s+/"," ",$line);
250 //remove any whitespace that occurs after/before an operator
251 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
257 $sJS = implode("\n",$lines);
259 //make sure there is a max of 1 \n after each line
260 $sJS = preg_replace("/[\n]+/","\n",$sJS);
262 //strip out line breaks that immediately follow a semi-colon
263 $sJS = preg_replace("/;\n/",";",$sJS);
265 //curly brackets aren't on their own
266 $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
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);
278 Check if file can be updated, restoring subdirectories
279 if needed. Returns 1 when no confilcts, -1 when file exists and is writable
281 function check_write($path)
283 if ($path == ''//|| $path == '.' || $path == '..'
286 return is_writable($path) ? (is_dir($path) ? 1 : -1)
287 : (is_file($path) ? 0 : ($path == '.' || $path == '..' ? 0 : check_write(dirname($path))));
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.
295 function copy_files($flist, $from, $to, $strict=false)
297 foreach ($flist as $file) {
298 if (file_exists($from.'/'.$file)) {
299 if (!copy_file($file, $from, $to))
301 } else if ($strict) {
302 unlink($to.'/'.$file);
309 Copies file from base to target directory, restoring subdirectories
312 function copy_file($file, $from, $to)
315 if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
316 if (!copy_file(dirname($file), null, $to))
320 // error_log( 'dodanie katalogu '.$to.'/'.$file);
321 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
324 // error_log( 'skopiowanie '.$to.'/'.$file);
325 return @copy($from.'/'.$file, $to.'/'.$file);
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.
334 Returns found file path or null.
336 function find_custom_file($rep)
338 global $installed_extensions, $path_to_root, $local_path_to_root;
340 // customized per company version
341 $path = company_path();
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());
349 // file added by active extension modules
350 if (count($installed_extensions) > 0)
352 $extensions = $installed_extensions;
353 foreach ($extensions as $ext)
354 if (($ext['active'] && $ext['type'] == 'extension')) {
355 $path = $path_to_root.'/'.$ext['path'];
357 if (file_exists($file)) {
358 set_include_path($path.PATH_SEPARATOR.get_include_path());
359 $local_path_to_root = $path;
365 $file = $path_to_root.$rep;
366 if (file_exists($file))
373 Protect against directory traversal.
374 Changes all not POSIX compatible chars to underscore.
376 function clean_file_name($filename) {
377 $filename = str_replace(chr(0), '', $filename);
378 return preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);