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