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");
26 // static js files path
27 $js_path = $path_to_root.'/js/';
28 // standard external js scripts included in all files
29 $js_static = array('JsHttpRequest.js', 'behaviour.js', 'utils.js', 'inserts.js');
30 // additional js source included in header
31 $js_lib = $js_userlib = array();
33 function page($title, $no_menu=false, $is_index=false, $onload="", $js="", $script_only=false, $css='')
36 global $path_to_root, $page_security, $page_nested;
38 if (++$page_nested) return;
40 $hide_menu = $no_menu;
42 include_once($path_to_root . "/includes/page/header.inc");
44 page_header($title, $no_menu, $is_index, $onload, $js, $css);
45 check_page_security($page_security);
49 echo display_heading(_('This page is usable only with javascript enabled browsers.'));
51 div_start('_page_body', null, true);
53 div_start('_page_body'); // whole page content for ajax reloading
57 function end_page($no_menu=false, $is_index=false, $final_screen=false, $type_no=0, $trans_no=0)
59 global $path_to_root, $page_nested;
61 if ($page_nested-- > 0) return;
63 if (!$is_index && function_exists('hyperlink_back'))
64 hyperlink_back(true, $no_menu, $type_no, $trans_no, $final_screen);
65 div_end(); // end of _page_body section
67 include_once($path_to_root . "/includes/page/footer.inc");
68 page_footer($no_menu, $is_index);
71 function cache_js_file($fpath, $text)
75 if (!$SysPrefs->go_debug) $text = js_compress($text);
77 $file = force_open($fpath);
78 if (!$file) return false;
79 if (!fwrite($file, $text)) return false;
85 Open file for writing with creration of subfolders if needed.
87 function force_open($fname)
89 $file = pathinfo($fname);
91 $path = $fname[0] == '/' ? '/' : '';
92 $tree = explode('/', $file['dirname']);
93 foreach($tree as $level) {
95 if (!file_exists($path)) {
102 return fopen($fname, 'w');
105 function add_js_file($filename)
109 $search = array_search($filename, $js_static);
110 if ($search === false || $search === null) // php>4.2.0 returns null
111 $js_static[] = $filename;
114 function add_js_ufile($filename)
118 $search = array_search($filename, $js_userlib);
119 if ($search === false || $search === null) // php>4.2.0 returns null
120 $js_userlib[] = $filename;
123 function add_js_source($text)
127 $search = array_search($text, $js_lib);
128 if ($search === false || $search === null) // php>4.2.0 returns null
133 * Compresses the Javascript code for more efficient delivery.
134 * copyright (c) 2005 by Jared White & J. Max Wilson
135 * http://www.xajaxproject.org
136 * Added removing comments from output.
137 * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
139 function js_compress($sJS)
141 //remove windows cariage returns
142 $sJS = str_replace("\r","",$sJS);
144 //array to store replaced literal strings
145 $literal_strings = array();
147 //explode the string into lines
148 $lines = explode("\n",$sJS);
149 //loop through all the lines, building a new string at the same time as removing literal strings
157 for($i=0;$i<count($lines);$i++)
160 $inNormalComment = false;
162 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
163 $len = strlen($line);
164 for($j=0;$j<$len;$j++)
166 $c = $line[$j]; // this is _really_ faster than subst
169 //look for start of quote
170 if(!$inQuote && !$inComment)
172 //is this character a quote or a comment
173 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
181 else if($d=="/*" && !$inNormalComment)
190 else if($d=="//") //ignore string markers that are found inside comments
192 $inNormalComment = true;
200 else //allready in a string so find end quote
202 if($c == $quoteChar && !$escaped && !$inComment)
207 //subsitute in a marker for the string
208 $clean .= "___" . count($literal_strings) . "___";
210 //push the string onto our array
211 array_push($literal_strings,$literal);
214 else if($inComment && $d=="*/")
219 //subsitute in a marker for the string
220 $clean .= "___" . count($literal_strings) . "___";
222 //push the string onto our array
223 array_push($literal_strings,$literal);
227 else if($c == "\\" && !$escaped)
235 if($inComment) $literal .= "\n";
238 //explode the clean string into lines again
239 $lines = explode("\n",$clean);
241 //now process each line at a time
242 for($i=0;$i<count($lines);$i++)
247 $line = preg_replace("/\/\/(.*)/","",$line);
249 //strip leading and trailing whitespace
252 //remove all whitespace with a single space
253 $line = preg_replace("/\s+/"," ",$line);
255 //remove any whitespace that occurs after/before an operator
256 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
262 $sJS = implode("\n",$lines);
264 //make sure there is a max of 1 \n after each line
265 $sJS = preg_replace("/[\n]+/","\n",$sJS);
267 //strip out line breaks that immediately follow a semi-colon
268 $sJS = preg_replace("/;\n/",";",$sJS);
270 //curly brackets aren't on their own
271 $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
273 //finally loop through and replace all the literal strings:
274 for($i=0;$i<count($literal_strings);$i++) {
275 if (strpos($literal_strings[$i],"/*")!==false)
276 $literal_strings[$i]= '';
277 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
283 Check if file can be updated, restoring subdirectories
284 if needed. Returns 1 when no confilcts, -1 when file exists and is writable
286 function check_write($path)
288 if ($path == ''//|| $path == '.' || $path == '..'
291 return is_writable($path) ? (is_dir($path) ? 1 : -1)
292 : (is_file($path) ? 0 : ($path == '.' || $path == '..' ? 0 : check_write(dirname($path))));
296 Copies set of files. When $strict is set
297 also removes files from the $to which
298 does not exists in $from directory but arelisted in $flist.
300 function copy_files($flist, $from, $to, $strict=false)
302 foreach ($flist as $file) {
303 if (file_exists($from.'/'.$file)) {
304 if (!copy_file($file, $from, $to))
306 } else if ($strict) {
307 unlink($to.'/'.$file);
314 Copies file from base to target directory, restoring subdirectories
317 function copy_file($file, $from, $to)
320 if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
321 if (!copy_file(dirname($file), null, $to))
325 // error_log( 'dodanie katalogu '.$to.'/'.$file);
326 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
329 // error_log( 'skopiowanie '.$to.'/'.$file);
330 return @copy($from.'/'.$file, $to.'/'.$file);
334 Search for file, looking first for company specific version, then for
335 version provided by any extension module, finally in main FA directory.
336 Also adds include path for any related files, and sets $local_path_to_root
337 to enable local translation domains.
339 Returns found file path or null.
341 function find_custom_file($rep)
343 global $installed_extensions, $path_to_root, $local_path_to_root;
345 // customized per company version
346 $path = company_path();
348 if (file_exists($file)) {
349 // add local include path
350 $local_path_to_root = $path;
351 set_include_path(dirname($file).PATH_SEPARATOR.get_include_path());
354 // file added by active extension modules
355 if (count($installed_extensions) > 0)
357 $extensions = $installed_extensions;
358 foreach ($extensions as $ext)
359 if (($ext['active'] && $ext['type'] == 'extension')) {
360 $path = $path_to_root.'/'.$ext['path'];
362 if (file_exists($file)) {
363 set_include_path($path.PATH_SEPARATOR.get_include_path());
364 $local_path_to_root = $path;
370 $file = $path_to_root.$rep;
371 if (file_exists($file))
378 Protect against directory traversal.
379 Changes all not POSIX compatible chars to underscore.
381 function clean_file_name($filename) {
382 $filename = str_replace(chr(0), '', $filename);
383 return preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);
387 Simple random password generator.
389 function generate_password()
391 if (PHP_VERSION >= '5.3')
392 $bytes = openssl_random_pseudo_bytes(8, $cstrong);
394 $bytes = sprintf("08%x", mt_rand(0,0xffffffff));
396 return base64_encode($bytes);
399 if (!function_exists('array_fill_keys')) // since 5.2
401 function array_fill_keys($keys, $value)
403 return array_combine($keys, array_fill(count($keys), $value));