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