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 . "/admin/db/transactions_db.inc");
15 include_once($path_to_root . "/includes/types.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");
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");
27 function page($title, $no_menu=false, $is_index=false, $onload="", $js="", $script_only=false, $css='')
30 global $path_to_root, $page_security, $page_nested;
32 if (++$page_nested) return;
34 $hide_menu = $no_menu;
36 include_once($path_to_root . "/includes/page/header.inc");
38 page_header($title, $no_menu, $is_index, $onload, $js, $css);
39 check_page_security($page_security);
43 echo display_heading(_('This page is usable only with javascript enabled browsers.'));
45 div_start('_page_body', null, true);
47 div_start('_page_body'); // whole page content for ajax reloading
51 function end_page($no_menu=false, $is_index=false, $final_screen=false, $type_no=0, $trans_no=0)
53 global $path_to_root, $page_nested;
55 if ($page_nested-- > 0) return;
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
61 include_once($path_to_root . "/includes/page/footer.inc");
62 page_footer($no_menu, $is_index);
65 function cache_js_file($fpath, $text)
69 if(!$go_debug) $text = js_compress($text);
71 $file = force_open($fpath);
72 if (!$file) return false;
73 if (!fwrite($file, $text)) return false;
79 Open file for writing with creration of subfolders if needed.
81 function force_open($fname)
83 $file = pathinfo($fname);
85 $path = $fname[0] == '/' ? '/' : '';
86 $tree = explode('/', $file['dirname']);
87 foreach($tree as $level) {
89 if (!file_exists($path)) {
96 return fopen($fname, 'w');
99 function add_js_file($filename)
103 $search = array_search($filename, $js_static);
104 if ($search === false || $search === null) // php>4.2.0 returns null
105 $js_static[] = $filename;
108 function add_js_ufile($filename)
112 $search = array_search($filename, $js_userlib);
113 if ($search === false || $search === null) // php>4.2.0 returns null
114 $js_userlib[] = $filename;
117 function add_js_source($text)
121 $search = array_search($text, $js_lib);
122 if ($search === false || $search === null) // php>4.2.0 returns null
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.
133 function js_compress($sJS)
135 //remove windows cariage returns
136 $sJS = str_replace("\r","",$sJS);
138 //array to store replaced literal strings
139 $literal_strings = array();
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
151 for($i=0;$i<count($lines);$i++)
154 $inNormalComment = false;
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++)
160 $c = $line[$j]; // this is _really_ faster than subst
163 //look for start of quote
164 if(!$inQuote && !$inComment)
166 //is this character a quote or a comment
167 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
175 else if($d=="/*" && !$inNormalComment)
184 else if($d=="//") //ignore string markers that are found inside comments
186 $inNormalComment = true;
194 else //allready in a string so find end quote
196 if($c == $quoteChar && !$escaped && !$inComment)
201 //subsitute in a marker for the string
202 $clean .= "___" . count($literal_strings) . "___";
204 //push the string onto our array
205 array_push($literal_strings,$literal);
208 else if($inComment && $d=="*/")
213 //subsitute in a marker for the string
214 $clean .= "___" . count($literal_strings) . "___";
216 //push the string onto our array
217 array_push($literal_strings,$literal);
221 else if($c == "\\" && !$escaped)
229 if($inComment) $literal .= "\n";
232 //explode the clean string into lines again
233 $lines = explode("\n",$clean);
235 //now process each line at a time
236 for($i=0;$i<count($lines);$i++)
241 $line = preg_replace("/\/\/(.*)/","",$line);
243 //strip leading and trailing whitespace
246 //remove all whitespace with a single space
247 $line = preg_replace("/\s+/"," ",$line);
249 //remove any whitespace that occurs after/before an operator
250 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
256 $sJS = implode("\n",$lines);
258 //make sure there is a max of 1 \n after each line
259 $sJS = preg_replace("/[\n]+/","\n",$sJS);
261 //strip out line breaks that immediately follow a semi-colon
262 $sJS = preg_replace("/;\n/",";",$sJS);
264 //curly brackets aren't on their own
265 $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
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);
277 Check if file can be updated, restoring subdirectories
278 if needed. Returns 1 when no confilcts, -1 when file exists and is writable
280 function check_write($path)
282 if ($path == ''//|| $path == '.' || $path == '..'
285 return is_writable($path) ? (is_dir($path) ? 1 : -1)
286 : (is_file($path) ? 0 : ($path == '.' || $path == '..' ? 0 : check_write(dirname($path))));
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.
294 function copy_files($flist, $from, $to, $strict=false)
296 foreach ($flist as $file) {
297 if (file_exists($from.'/'.$file)) {
298 if (!copy_file($file, $from, $to))
300 } else if ($strict) {
301 unlink($to.'/'.$file);
308 Copies file from base to target directory, restoring subdirectories
311 function copy_file($file, $from, $to)
314 if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
315 if (!copy_file(dirname($file), null, $to))
319 // error_log( 'dodanie katalogu '.$to.'/'.$file);
320 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
323 // error_log( 'skopiowanie '.$to.'/'.$file);
324 return @copy($from.'/'.$file, $to.'/'.$file);
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.
333 Returns found file path or null.
335 function find_custom_file($rep)
337 global $installed_extensions, $path_to_root, $local_path_to_root;
339 // customized per company version
340 $path = company_path();
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());
348 // file added by active extension modules
349 if (count($installed_extensions) > 0)
351 $extensions = $installed_extensions;
352 foreach ($extensions as $ext)
353 if (($ext['active'] && $ext['type'] == 'extension')) {
354 $path = $path_to_root.'/'.$ext['path'];
356 if (file_exists($file)) {
357 set_include_path($path.PATH_SEPARATOR.get_include_path());
358 $local_path_to_root = $path;
364 $file = $path_to_root.$rep;
365 if (file_exists($file))
372 Protect against directory traversal.
373 Changes all not POSIX compatible chars to underscore.
375 function clean_file_name($filename) {
376 $filename = str_replace(chr(0), '', $filename);
377 return preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);