Fixed continuation after authorization timeout.
[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/prefs/sysprefs.inc");
19 include_once($path_to_root . "/includes/db/comments_db.inc");
20 include_once($path_to_root . "/includes/db/sql_functions.inc");
21 include_once($path_to_root . "/includes/db/audit_trail_db.inc");
22 //include_once($path_to_root . "/includes/validation.inc");
23
24 include_once($path_to_root . "/admin/db/users_db.inc");
25 include_once($path_to_root . "/includes/ui/ui_view.inc");
26 include_once($path_to_root . "/includes/ui/ui_controls.inc");
27         
28 function page($title, $no_menu=false, $is_index=false, $onload="", $js="", $script_only=false)
29 {
30
31         global $path_to_root, $page_security;
32
33
34         $hide_menu = $no_menu;
35
36         include($path_to_root . "/includes/page/header.inc");
37
38         page_header($title, $no_menu, $is_index, $onload, $js);
39         check_page_security($page_security);
40 //      error_box();
41         if($script_only) {              
42                 echo '<noscript>';
43                 echo display_heading(_('This page is usable only with javascript enabled browsers.'));
44                 echo '</noscript>';
45                 div_start('_page_body', null, true);
46         } else {
47                 div_start('_page_body'); // whole page content for ajax reloading
48         }
49 }
50
51 function end_page($no_menu=false, $is_index=false, $hide_back_link=false)
52 {
53         global $path_to_root;
54
55         if (!$is_index && !$hide_back_link && function_exists('hyperlink_back'))
56                 hyperlink_back();
57         div_end();      // end of _page_body section
58
59         include($path_to_root . "/includes/page/footer.inc");
60         page_footer($no_menu, $is_index, $hide_back_link);
61 }
62
63 function flush_dir($path, $wipe = false) 
64 {
65         $dir = opendir($path);
66         while(false !== ($fname = readdir($dir))) {
67                 if($fname=='.' || $fname=='..' || $fname=='CVS' || (!$wipe && $fname=='index.php')) continue;
68                 if(is_dir($path.'/'.$fname)) {
69                     flush_dir($path.'/'.$fname, $wipe);
70                     if ($wipe) @rmdir($path.'/'.$fname);
71                 } else
72                     @unlink($path.'/'.$fname);
73         }
74 }
75
76 function cache_js_file($fpath, $text) 
77 {
78         global $go_debug;
79
80         if(!$go_debug) $text = js_compress($text);
81
82         $file = fopen($fpath, 'w');
83         if (!$file) return false;
84         if (!fwrite($file, $text)) return false;
85         return fclose($file);
86
87 }
88
89 function add_js_file($filename) 
90 {
91           global $js_static;
92
93           $search = array_search($filename, $js_static);
94           if ($search === false || $search === null) // php>4.2.0 returns null
95                 $js_static[] = $filename;       
96 }
97
98 function add_js_ufile($filename) 
99 {
100           global $js_userlib;
101
102           $search = array_search($filename, $js_userlib);
103           if ($search === false || $search === null) // php>4.2.0 returns null
104                 $js_userlib[] = $filename;
105 }
106
107 function add_js_source($text) 
108 {
109           global $js_lib;
110           
111           $search = array_search($text, $js_lib);
112           if ($search === false || $search === null) // php>4.2.0 returns null
113                 $js_lib[] = $text;
114 }
115
116 /**
117  * Compresses the Javascript code for more efficient delivery.
118  * copyright (c) 2005 by Jared White & J. Max Wilson
119  * http://www.xajaxproject.org
120  * Added removing comments from output.
121  * Warning: Fails on RegExp with quotes - use new RegExp() in this case.
122  */
123 function js_compress($sJS)
124 {
125         //remove windows cariage returns
126         $sJS = str_replace("\r","",$sJS);
127         
128         //array to store replaced literal strings
129         $literal_strings = array();
130         
131         //explode the string into lines
132         $lines = explode("\n",$sJS);
133         //loop through all the lines, building a new string at the same time as removing literal strings
134         $clean = "";
135         $inComment = false;
136         $literal = "";
137         $inQuote = false;
138         $escaped = false;
139         $quoteChar = "";
140         
141         for($i=0;$i<count($lines);$i++)
142         {
143                 $line = $lines[$i];
144                 $inNormalComment = false;
145         
146                 //loop through line's characters and take out any literal strings, replace them with ___i___ where i is the index of this string
147                 $len = strlen($line);
148                 for($j=0;$j<$len;$j++)
149                 {
150                         $c = $line[$j];         // this is _really_ faster than subst
151                         $d = $c.$line[$j+1];
152         
153                         //look for start of quote
154                         if(!$inQuote && !$inComment)
155                         {
156                                 //is this character a quote or a comment
157                                 if(($c=="\"" || $c=="'") && !$inComment && !$inNormalComment)
158                                 {
159                                         $inQuote = true;
160                                         $inComment = false;
161                                         $escaped = false;
162                                         $quoteChar = $c;
163                                         $literal = $c;
164                                 }
165                                 else if($d=="/*" && !$inNormalComment)
166                                 {
167                                         $inQuote = false;
168                                         $inComment = true;
169                                         $escaped = false;
170                                         $quoteChar = $d;
171                                         $literal = $d;  
172                                         $j++;   
173                                 }
174                                 else if($d=="//") //ignore string markers that are found inside comments
175                                 {
176                                         $inNormalComment = true;
177                                         $clean .= $c;
178                                 }
179                                 else
180                                 {
181                                         $clean .= $c;
182                                 }
183                         }
184                         else //allready in a string so find end quote
185                         {
186                                 if($c == $quoteChar && !$escaped && !$inComment)
187                                 {
188                                         $inQuote = false;
189                                         $literal .= $c;
190         
191                                         //subsitute in a marker for the string
192                                         $clean .= "___" . count($literal_strings) . "___";
193         
194                                         //push the string onto our array
195                                         array_push($literal_strings,$literal);
196         
197                                 }
198                                 else if($inComment && $d=="*/")
199                                 {
200                                         $inComment = false;
201                                         $literal .= $d;
202         
203                                         //subsitute in a marker for the string
204                                         $clean .= "___" . count($literal_strings) . "___";
205         
206                                         //push the string onto our array
207                                         array_push($literal_strings,$literal);
208         
209                                         $j++;
210                                 }
211                                 else if($c == "\\" && !$escaped)
212                                         $escaped = true;
213                                 else
214                                         $escaped = false;
215         
216                                 $literal .= $c;
217                         }
218                 }
219                 if($inComment) $literal .= "\n";
220                 $clean .= "\n";
221         }
222         //explode the clean string into lines again
223         $lines = explode("\n",$clean);
224         
225         //now process each line at a time
226         for($i=0;$i<count($lines);$i++)
227         {
228                 $line = $lines[$i];
229         
230                 //remove comments
231                 $line = preg_replace("/\/\/(.*)/","",$line);
232         
233                 //strip leading and trailing whitespace
234                 $line = trim($line);
235         
236                 //remove all whitespace with a single space
237                 $line = preg_replace("/\s+/"," ",$line);
238         
239                 //remove any whitespace that occurs after/before an operator
240                 $line = preg_replace("/\s*([!\}\{;,&=\|\-\+\*\/\)\(:])\s*/","\\1",$line);
241         
242                 $lines[$i] = $line;
243         }
244         
245         //implode the lines
246         $sJS = implode("\n",$lines);
247         
248         //make sure there is a max of 1 \n after each line
249         $sJS = preg_replace("/[\n]+/","\n",$sJS);
250         
251         //strip out line breaks that immediately follow a semi-colon
252         $sJS = preg_replace("/;\n/",";",$sJS);
253         
254         //curly brackets aren't on their own
255         $sJS = preg_replace("/[\n]*\{[\n]*/","{",$sJS);
256         
257         //finally loop through and replace all the literal strings:
258         for($i=0;$i<count($literal_strings);$i++) {
259             if (strpos($literal_strings[$i],"/*")!==false) 
260                 $literal_strings[$i]= '';
261                 $sJS = str_replace("___".$i."___",$literal_strings[$i],$sJS);
262         }
263         return $sJS;
264 }
265
266 ?>