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 = fopen($fpath, 'w');
73 if (!$file) return false;
74 if (!fwrite($file, $text)) return false;
79 function add_js_file($filename)
83 $search = array_search($filename, $js_static);
84 if ($search === false || $search === null) // php>4.2.0 returns null
85 $js_static[] = $filename;
88 function add_js_ufile($filename)
92 $search = array_search($filename, $js_userlib);
93 if ($search === false || $search === null) // php>4.2.0 returns null
94 $js_userlib[] = $filename;
97 function add_js_source($text)
101 $search = array_search($text, $js_lib);
102 if ($search === false || $search === null) // php>4.2.0 returns null
107 * Compresses the Javascript code for more efficient delivery.
108 * copyright (c) 2005 by Jared White & J. Max Wilson
109 * http://www.xajaxproject.org
110 * Added removing comments from output.
111 * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
113 function js_compress($sJS)
115 //remove windows cariage returns
116 $sJS = str_replace("\r","",$sJS);
118 //array to store replaced literal strings
119 $literal_strings = array();
121 //explode the string into lines
122 $lines = explode("\n",$sJS);
123 //loop through all the lines, building a new string at the same time as removing literal strings
131 for($i=0;$i<count($lines);$i++)
134 $inNormalComment = false;
136 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
137 $len = strlen($line);
138 for($j=0;$j<$len;$j++)
140 $c = $line[$j]; // this is _really_ faster than subst
143 //look for start of quote
144 if(!$inQuote && !$inComment)
146 //is this character a quote or a comment
147 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
155 else if($d=="/*" && !$inNormalComment)
164 else if($d=="//") //ignore string markers that are found inside comments
166 $inNormalComment = true;
174 else //allready in a string so find end quote
176 if($c == $quoteChar && !$escaped && !$inComment)
181 //subsitute in a marker for the string
182 $clean .= "___" . count($literal_strings) . "___";
184 //push the string onto our array
185 array_push($literal_strings,$literal);
188 else if($inComment && $d=="*/")
193 //subsitute in a marker for the string
194 $clean .= "___" . count($literal_strings) . "___";
196 //push the string onto our array
197 array_push($literal_strings,$literal);
201 else if($c == "\\" && !$escaped)
209 if($inComment) $literal .= "\n";
212 //explode the clean string into lines again
213 $lines = explode("\n",$clean);
215 //now process each line at a time
216 for($i=0;$i<count($lines);$i++)
221 $line = preg_replace("/\/\/(.*)/","",$line);
223 //strip leading and trailing whitespace
226 //remove all whitespace with a single space
227 $line = preg_replace("/\s+/"," ",$line);
229 //remove any whitespace that occurs after/before an operator
230 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
236 $sJS = implode("\n",$lines);
238 //make sure there is a max of 1 \n after each line
239 $sJS = preg_replace("/[\n]+/","\n",$sJS);
241 //strip out line breaks that immediately follow a semi-colon
242 $sJS = preg_replace("/;\n/",";",$sJS);
244 //curly brackets aren't on their own
245 $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
247 //finally loop through and replace all the literal strings:
248 for($i=0;$i<count($literal_strings);$i++) {
249 if (strpos($literal_strings[$i],"/*")!==false)
250 $literal_strings[$i]= '';
251 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
257 Check if file can be updated, restoring subdirectories
258 if needed. Returns 1 when no confilcts, -1 when file exists and is writable
260 function check_write($path)
262 if ($path == ''//|| $path == '.' || $path == '..'
265 return is_writable($path) ? (is_dir($path) ? 1 : -1)
266 : (is_file($path) ? 0 : ($path == '.' || $path == '..' ? 0 : check_write(dirname($path))));
270 Copies set of files. When $strict is set
271 also removes files from the $to which
272 does not exists in $from directory but arelisted in $flist.
274 function copy_files($flist, $from, $to, $strict=false)
276 foreach ($flist as $file) {
277 if (file_exists($from.'/'.$file)) {
278 if (!copy_file($file, $from, $to))
280 } else if ($strict) {
281 unlink($to.'/'.$file);
288 Copies file from base to target directory, restoring subdirectories
291 function copy_file($file, $from, $to)
294 if (!is_dir(dirname($file=='.' ? $to : ($to.'/'.$file)))) {
295 if (!copy_file(dirname($file), null, $to))
299 // error_log( 'dodanie katalogu '.$to.'/'.$file);
300 return @mkdir($file=='.' ? $to : ($to.'/'.$file));
303 // error_log( 'skopiowanie '.$to.'/'.$file);
304 return @copy($from.'/'.$file, $to.'/'.$file);
308 Search for file, looking first for company specific version, then for
309 version provided by any extension module, finally in main FA directory.
310 Also adds include path for any related files, and sets $local_path_to_root
311 to enable local translation domains.
313 Returns found file path or null.
315 function find_custom_file($rep)
317 global $installed_extensions, $path_to_root, $local_path_to_root;
319 // customized per company version
320 $path = company_path();
322 if (file_exists($file)) {
323 // add local include path
324 $local_path_to_root = $path;
325 set_include_path(dirname($file).PATH_SEPARATOR.get_include_path());
328 // file added by active extension modules
329 if (count($installed_extensions) > 0)
331 $extensions = $installed_extensions;
332 foreach ($extensions as $ext)
333 if (($ext['active'] && $ext['type'] == 'extension')) {
334 $path = $path_to_root.'/'.$ext['path'];
336 if (file_exists($file)) {
337 set_include_path($path.PATH_SEPARATOR.get_include_path());
338 $local_path_to_root = $path;
344 $file = $path_to_root.$rep;
345 if (file_exists($file))
352 Protect against directory traversal.
353 Changes all not POSIX compatible chars to underscore.
355 function clean_file_name($filename) {
356 return preg_replace('/[^a-zA-Z0-9.\-_]/', '_', $filename);