Code

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