Code

Updated php_setup - set_error_handler
[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) && $config->get_cfg_value("displayerrors") == "true"){
48       
49       /* Write to syslog */
50       if(class_exists("log") && !preg_match("/No such object/",$errstr)){
51         new log("view","error","",array(),"PHP error: $errstr ($errfile, line $errline)");
52       }
54       return;
55   }
56   
57   /* Send all errors to logging class, except "Ldap : No such object" messages*/
58   if(class_exists("log") && !preg_match("/No such object/",$errstr)){
59     new log("debug","all",$errfile,array(),"Type:".$errno.", Message:".$errstr.", File:".$errfile.", Line: ".$errline);
60   }
62   /* Create header as needed */
63   if ($error_collector == ""){
65     /* Mailto body header */
66     if(function_exists("prepare4mailbody")){
67       $version= "unknown";
68       if(function_exists("get_gosa_version")){
69               $version= get_gosa_version();
70       }
71       $error_collector_mailto .=prepare4mailbody(
72                                 "Oups. Seems like you've catched some kind of bug inside GOsa/PHP. You may want to help ".
73                                 "us to improve the software stability. If so, please provide some more information below.".
74                                 "\n\n".
75                                 "*** GOsa bug report ***".
76                                 "\nGOsa Version: $version".
77                                 "\n\n".
78                                 "Please describe what you did to produce this error as detailed as possible. Can you ".
79                                 "reproduce this bug using the demo on http://www.gosa-project.org ?".
80                                 "\n\n".
81                                 "*** PHP error information ***\n\n");
82       }
83     
84     if (session::is_set('js') && session::get('js')==FALSE){
85       $error_collector= "<div>";
86     } else {
87       $error_collector= "
88         <table summary=\"\" width=\"100%\" style='background-color:#E0E0E0;border-bottom:1px solid black;z-index:150;'>
89           <tr>
90             <td>
91               <img alt=\"\" align=\"middle\" src='".get_template_path('images/warning.png')."'>&nbsp;
92               <font style='font-size:14px;font-weight:bold'>".
93                 _("Generating this page caused the PHP interpreter to raise some errors!")."
94               </font>
95             </td>
96             <td align=right>
97               <a href=\"mailto:gosa-bugs@oss.gonicus.de?subject=GOsa%20bugreport&amp;body=%BUGBODY%\">
98                 <img border='0' src='images/mailto.png' title='"._("Send bug report to the GOsa Team")."' class='center' alt=''>&nbsp;"._("Send bugreport")."
99               </a>
100             </td>
101             <td align=right>
102               <button onClick='toggle(\"errorbox\")'>".
103                 _("Toggle information")."
104               </button>
105             </td>
106           </tr>
107         </table>
108         <div id='errorbox' style='position:absolute; z-index:150; visibility: hidden'>";
109     }
110   }
111  
112   /* Create error header */
113   $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>";
115   $error_collector_mailto .= prepare4mailbody("=== Error === \n");
116   $error_collector_mailto .= prepare4mailbody("PHP error: $errstr ($errfile, line $errline)\n");
117   $error_collector_mailto .= prepare4mailbody("=== /Error === \n\n");
118   
119   /* Extract traceback data - if available */
120   if (function_exists('debug_backtrace')){
121     $trace= debug_backtrace();
123     $error_collector_mailto .= prepare4mailbody("=== Trace ===");
125     /* Generate trace history */
126     for ($index= 0; $index<count($trace); $index++){
129       $ct= $trace[$index];
130       $loc= "";
131       if (isset($ct['class'])){
132         $loc.= _("class")." ".$ct['class'];
133         if (isset($ct['function'])){
134           $loc.= " / ";
135         }
136       }
137       if (isset($ct['function'])){
138         $loc.= _("function")." ".$ct['function'];
139       }
140       if (isset($ct['type'])){
141         switch ($ct['type']){
142           case "::":
143             $type= _("static");
144           break;
146           case "->":
147             $type= _("method");
148           break;
149         }
150       } else {
151         $type= "-";
152       }
153       $args= "";
154       if (isset($ct['args'])){
155         foreach ($ct['args'] as $arg){
156        
157           /* Avoid convertig object to string errors */ 
158           if(is_object($arg)){
159             $arg = "CLASS:&nbsp;".get_class($arg);
160           }
161   
162           $args.= htmlentities("\"$arg\", ");
163         }
164       }
165       $args= preg_replace("/, $/", "", $args);
166       if ($args == ""){
167         $args= "-";
168       }
169       if(isset($ct['file'])) {
170         $file= $ct['file'];
171       }else{
172         $file="";
173       }
174       if(isset($ct['line'])) {
175         $line= $ct['line'];
176       }else{
177         $line="";
178       }
179       $color= ($index&1)?'#404040':'606060';
180       $error_collector.= "<tr style='background-color:$color'><td style='padding-left:20px' width=\"30%\">"._("Trace")."[$index]: $loc</td>";
181       $error_collector.= "<td>"._("File").": $file ("._('Line')." $line)</td><td width=\"10%\">"._("Type").": $type</td></tr>";
182       $error_collector.= "<tr style='background-color:$color'><td colspan=3 style='padding-left:20px;'>"._("Arguments").": $args</td></tr>";
184       /* Add trace part to mailto body */
185       $error_collector_mailto .= prepare4mailbody(
186                                    "\nTrace[".$index."]:".$loc.
187                                    "\nFile : ".$file.
188                                    "\nLine : ".$line.
189                                    "\nType : ".$type.
190                                    "\n  ".$args.
191                                    "\n");
193     }
194   }
196   $error_collector_mailto .= prepare4mailbody("=== /Trace === \n");
198   /* Close error table */
199   $error_collector.= "</table>";
201   /* Flush in case of fatal errors */
202   if (preg_match('/^fatal/i', $errstr)){
203     echo $error_collector."</div>";
204     flush();
205     exit;
206   }
210 function prepare4mailbody($string)
212   $string = html_entity_decode($string);
214   $from = array(
215                 "/%/",
216                 "/ /",
217                 "/\n/",
218                 "/\r/",
219                 "/!/",
220                 "/#/",
221                 "/\*/",
222                 "/\//",
223                 "/</",
224                 "/>/",
225                 "/\?/",
226                 "/\"/");
228   $to = array(
229                 "%25",
230                 "%20",
231                 "%0A",
232                 "%0D",
233                 "%21",
234                 "%23",
235                 "%2A",
236                 "%2F",
237                 "%3C",
238                 "%3E",
239                 "%3F",
240                 "%22");
242   $string = preg_replace($from,$to,$string);
244   return($string);
248 function dummy_error_handler()
252 /* Bail out for incompatible/old PHP versions */
253 if (!version_compare(phpversion(),"5.2.0",">=")){
254   echo "PHP version needs to be 5.2.0 or above to run GOsa. Aborted.";
255   exit();
258 /* Set timezone */
259 date_default_timezone_set("GMT");
261 /* Get base dir for reference */
262 $BASE_DIR= dirname(dirname(__FILE__));
263 $ROOT_DIR= $BASE_DIR."/html";
264 error_reporting (E_ALL | E_STRICT);
266 /* Register error handler */
267 $error_collector= "";
268 $error_collector_mailto= "";
270 set_error_handler('gosaRaiseError', E_WARNING |  E_NOTICE | E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE | E_STRICT) 
272 $variables_order= "ES";
273 ini_set("register_globals",0);
274 ini_set("track_vars",1);
275 ini_set("display_errors",1);
276 ini_set("report_memleaks",1);
277 ini_set("include_path",".:$BASE_DIR/include:$BASE_DIR/include/utils/excel");
279 /* Do smarty setup */
280 require("smarty/Smarty.class.php");
281 $smarty = new Smarty;
282 $smarty->template_dir = $BASE_DIR.'/ihtml/';
283 $smarty->caching= false;
284 $smarty->php_handling= SMARTY_PHP_REMOVE;
287 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
288 ?>