Code

Fixed one possible bailout
[gosa.git] / gosa-core / include / php_setup.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 define ("LOCALE_DIR", dirname(dirname(__FILE__))."/locale/compiled");
25 function gosaRaiseError($errno, $errstr, $errfile, $errline)
26 {
27   global $error_collector,$config, $error_collector_mailto;
29   /* Return if error reporting is set to zero */
30   if (error_reporting() == 0){
31     return;
32   }
34   /* Workaround for buggy imap_open error outputs */
35   if (preg_match('/imap_open/', $errstr)){
36     return;
37   }
39   /* Hide ldap size limit messages */
40   if (preg_match('/ldap_error/', $errstr)){
41     if (preg_match('/sizelimit/', $errstr)){
42       return;
43     }
44   }
46   /* Error messages are hidden in GOsa, so we only send them to the logging class and abort here */
47   if((isset($config->data))){ 
48     if((isset($config->data['MAIN']['DISPLAYERRORS']))&&(!preg_match("/^true$/i",$config->data['MAIN']['DISPLAYERRORS']))){
50       
51       /* Write to syslog */
52       if(class_exists("log") && !preg_match("/No such object/",$errstr)){
53         new log("view","error","",array(),"PHP error: $errstr ($errfile, line $errline)");
54       }
55       return;
56     }
57   }
58   
59   /* Send all errors to logging class, except "Ldap : No such object" messages*/
60   if(class_exists("log") && !preg_match("/No such object/",$errstr)){
61     new log("debug","all",$errfile,array(),"Type:".$errno.", Message:".$errstr.", File:".$errfile.", Line: ".$errline);
62   }
64   /* Create header as needed */
65   if ($error_collector == ""){
67     /* Mailto body header */
68     if(function_exists("prepare4mailbody")){
69       $version= "unknown";
70       if(function_exists("get_gosa_version")){
71               $version= get_gosa-version();
72       }
73       $error_collector_mailto .=prepare4mailbody(
74                                 "Oups. Seems like you've catched some kind of bug inside GOsa/PHP. You may want to help ".
75                                 "us to improve the software stability. If so, please provide some more information below.".
76                                 "\n\n".
77                                 "*** GOsa bug report ***".
78                                 "\nGOsa Version: $version".
79                                 "\n\n".
80                                 "Please describe what you did to produce this error as detailed as possible. Can you ".
81                                 "reproduce this bug using the demo on http://www.gosa-project.org ?".
82                                 "\n\n".
83                                 "*** PHP error information ***\n\n");
84       }
85     
86     if (session::is_set('js') && session::get('js')==FALSE){
87       $error_collector= "<div>";
88     } else {
89       $error_collector= "
90         <table summary=\"\" width=\"100%\" style='background-color:#E0E0E0;border-bottom:1px solid black;z-index:150;'>
91           <tr>
92             <td>
93               <img alt=\"\" align=\"middle\" src='".get_template_path('images/warning.png')."'>&nbsp;
94               <font style='font-size:14px;font-weight:bold'>".
95                 _("Generating this page caused the PHP interpreter to raise some errors!")."
96               </font>
97             </td>
98             <td align=right>
99               <a href=\"mailto:gosa-bugs@oss.gonicus.de?subject=GOsa%20bugreport&amp;body=%BUGBODY%\">
100                 <img border='0' src='images/mailto.png' title='"._("Send bug report to the GOsa Team")."' class='center' alt=''>&nbsp;"._("Send bugreport")."
101               </a>
102             </td>
103             <td align=right>
104               <button onClick='toggle(\"errorbox\")'>".
105                 _("Toggle information")."
106               </button>
107             </td>
108           </tr>
109         </table>
110         <div id='errorbox' style='position:absolute; z-index:150; visibility: hidden'>";
111     }
112   }
113  
114   /* Create error header */
115   $error_collector.= "<table summary=\"\" width=\"100%\" cellspacing=0 style='background-color:#402005;color:white;border:2px solid red'><tr><td colspan=3><h1 style='color:white'>"._("PHP error")." \"$errstr\"</h1></td></tr>";
117   $error_collector_mailto .= prepare4mailbody("=== Error === \n");
118   $error_collector_mailto .= prepare4mailbody("PHP error: $errstr ($errfile, line $errline)\n");
119   $error_collector_mailto .= prepare4mailbody("=== /Error === \n\n");
120   
121   /* Extract traceback data - if available */
122   if (function_exists('debug_backtrace')){
123     $trace= debug_backtrace();
125     $error_collector_mailto .= prepare4mailbody("=== Trace ===");
127     /* Generate trace history */
128     for ($index= 0; $index<count($trace); $index++){
131       $ct= $trace[$index];
132       $loc= "";
133       if (isset($ct['class'])){
134         $loc.= _("class")." ".$ct['class'];
135         if (isset($ct['function'])){
136           $loc.= " / ";
137         }
138       }
139       if (isset($ct['function'])){
140         $loc.= _("function")." ".$ct['function'];
141       }
142       if (isset($ct['type'])){
143         switch ($ct['type']){
144           case "::":
145             $type= _("static");
146           break;
148           case "->":
149             $type= _("method");
150           break;
151         }
152       } else {
153         $type= "-";
154       }
155       $args= "";
156       if (isset($ct['args'])){
157         foreach ($ct['args'] as $arg){
158        
159           /* Avoid convertig object to string errors */ 
160           if(is_object($arg)){
161             $arg = "CLASS:&nbsp;".get_class($arg);
162           }
163   
164           $args.= htmlentities("\"$arg\", ");
165         }
166       }
167       $args= preg_replace("/, $/", "", $args);
168       if ($args == ""){
169         $args= "-";
170       }
171       if(isset($ct['file'])) {
172         $file= $ct['file'];
173       }else{
174         $file="";
175       }
176       if(isset($ct['line'])) {
177         $line= $ct['line'];
178       }else{
179         $line="";
180       }
181       $color= ($index&1)?'#404040':'606060';
182       $error_collector.= "<tr style='background-color:$color'><td style='padding-left:20px' width=\"30%\">"._("Trace")."[$index]: $loc</td>";
183       $error_collector.= "<td>"._("File").": $file ("._('Line')." $line)</td><td width=\"10%\">"._("Type").": $type</td></tr>";
184       $error_collector.= "<tr style='background-color:$color'><td colspan=3 style='padding-left:20px;'>"._("Arguments").": $args</td></tr>";
186       /* Add trace part to mailto body */
187       $error_collector_mailto .= prepare4mailbody(
188                                    "\nTrace[".$index."]:".$loc.
189                                    "\nFile : ".$file.
190                                    "\nLine : ".$line.
191                                    "\nType : ".$type.
192                                    "\n  ".$args.
193                                    "\n");
195     }
196   }
198   $error_collector_mailto .= prepare4mailbody("=== /Trace === \n");
200   /* Close error table */
201   $error_collector.= "</table>";
203   /* Flush in case of fatal errors */
204   if (preg_match('/^fatal/i', $errstr)){
205     echo $error_collector."</div>";
206     flush();
207     exit;
208   }
212 function prepare4mailbody($string)
214   $string = html_entity_decode($string);
216   $from = array(
217                 "/%/",
218                 "/ /",
219                 "/\n/",
220                 "/\r/",
221                 "/!/",
222                 "/#/",
223                 "/\*/",
224                 "/\//",
225                 "/</",
226                 "/>/",
227                 "/\?/",
228                 "/\"/");
230   $to = array(
231                 "%25",
232                 "%20",
233                 "%0A",
234                 "%0D",
235                 "%21",
236                 "%23",
237                 "%2A",
238                 "%2F",
239                 "%3C",
240                 "%3E",
241                 "%3F",
242                 "%22");
244   $string = preg_replace($from,$to,$string);
246   return($string);
250 function dummy_error_handler()
254 /* Bail out for incompatible/old PHP versions */
255 if (!version_compare(phpversion(),"5.2.0",">=")){
256   echo "PHP version needs to be 5.2.0 or above to run GOsa. Aborted.";
257   exit();
260 /* Set timezone */
261 date_default_timezone_set("GMT");
263 /* Get base dir for reference */
264 $BASE_DIR= dirname(dirname(__FILE__));
265 $ROOT_DIR= $BASE_DIR."/html";
266 error_reporting (E_ALL | E_STRICT);
268 /* Register error handler */
269 $error_collector= "";
270 $error_collector_mailto= "";
271 set_error_handler('gosaRaiseError');
273 $variables_order= "ES";
274 ini_set("register_globals",0);
275 ini_set("track_vars",1);
276 ini_set("display_errors",1);
277 ini_set("report_memleaks",1);
278 ini_set("include_path",".:$BASE_DIR/include:$BASE_DIR/include/utils/excel");
280 /* Do smarty setup */
281 require("smarty/Smarty.class.php");
282 $smarty = new Smarty;
283 $smarty->template_dir = $BASE_DIR.'/ihtml/';
284 $smarty->caching= false;
285 $smarty->php_handling= SMARTY_PHP_REMOVE;
288 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
289 ?>