Code

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