source: temp/test-xoops.ec-cube.net/html/class/errorhandler.php @ 405

Revision 405, 7.1 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: errorhandler.php,v 1.5 2006/05/01 02:37:24 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
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//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27
28/**
29 * Error handler class
30 *
31 * @author Michael van Dam
32 */
33class XoopsErrorHandler
34{
35    /**
36     * List of errors
37     *
38     * @var array
39     * @access private
40     */
41    var $_errors = array();
42
43    /**
44     * Show error messages?
45     *
46     * @var boolean
47     * @access private
48     */
49    var $_showErrors = false;
50
51    /**
52     * Was there a fatal error (E_USER_ERROR)
53     *
54     * @var boolean
55     * @access private
56     */
57    var $_isFatal = false;
58
59    /**
60     * Constructor
61     *
62     * Registers the error handler and shutdown functions.  NOTE: when
63     * registering an error handler, the setting or 'error_reporting' is
64     * ignored and *everything* is trapped.
65     */
66    function XoopsErrorHandler()
67    {
68        set_error_handler('XoopsErrorHandler_HandleError');
69        register_shutdown_function('XoopsErrorHandler_Shutdown');
70    }
71
72    /**
73     * Get the (singleton) instance of the error handler
74     *
75     * @access public
76     */
77    function &getInstance()
78    {
79        static $instance = null;
80        if (empty($instance)) {
81            $instance = new XoopsErrorHandler;
82        }
83        return $instance;
84    }
85
86    /**
87     * Activate the error handler
88     *
89     * @access public
90     * @param boolean $showErrors True if debug mode is on
91     * @return void
92     */
93    function activate($showErrors=false)
94    {
95        $this->_showErrors = $showErrors;
96    }
97
98    /**
99     * Handle an error
100     *
101     * @param array $error Associative array containing error info
102     * @access public
103     * @return void
104     */
105    function handleError($error)
106    {
107        if (($error['errno'] & error_reporting()) != $error['errno']) {
108            return;
109        }
110        $this->_errors[] = $error;
111        if ($error['errno'] == E_USER_ERROR) {
112            $this->_isFatal = true;
113            exit();
114        }
115    }
116
117    /**
118     * Render the list of errors
119     *
120     * NOTE: Unfortunately PHP 'fatal' and 'parse' errors are not trappable.
121     * If the server has 'display_errors Off', then the result will be a
122     * blank page.  It would be nice to print a message 'This page cannot
123     * be displayed', but there seems to be no way to print this only when
124     * exiting due to a fatal error rather than normal end of page.
125     *
126     * Thus, 'trigger_error' should be used to trap problems early and
127     * display a meaningful message before a PHP fatal or parse error can
128     * occur.
129     *
130     * @TODO Use CSS
131     * @TODO Use language? or allow customized message?
132     *
133     * @access public
134     * @return void
135     */
136    function renderErrors()
137    {
138        //
139        // TODO We should plan new style about the following lines.
140        //
141        $output = '';
142        if ($this->_isFatal) {
143            $output .= 'This page cannot be displayed due to an internal error.<br/><br/>';
144            $output .= 'If you are the administrator of this site, please visit the <a href="http://xoopscube.org/">XOOPS Cube official site</a> for assistance.<br/><br/>';
145        }
146        if (!$this->_showErrors || empty($this->_errors)) {
147            return $output;
148        }
149
150        foreach( $this->_errors as $error )
151        {
152            switch ( $error['errno'] )
153            {
154                case E_USER_NOTICE:
155                    $output .= "Notice [Xoops]: ";
156                    break;
157                case E_USER_WARNING:
158                    $output .= "Warning [Xoops]: ";
159                    break;
160                case E_USER_ERROR:
161                    $output .= "Error [Xoops]: ";
162                    break;
163                case E_NOTICE:
164                    $output .= "Notice [PHP]: ";
165                    break;
166                case E_WARNING:
167                    $output .= "Warning [PHP]: ";
168                    break;
169                default:
170                    $output .= "Unknown Condition [" . $error['errno'] . "]: ";
171            }
172            $output .= sprintf( "%s in file %s line %s<br />\n", $error['errstr'], $error['errfile'], $error['errline'] );
173        }
174        return $output;
175    }
176
177}
178
179/**
180 * User-defined error handler (called from 'trigger_error')
181 *
182 * NOTE: Some recent versions of PHP have a 5th parameter, &$p_ErrContext
183 * which is an associative array of all variables defined in scope in which
184 * error occurred.  We cannot support this, for compatibility with older PHP.
185 *
186 * @access public
187 * @param int $errNo Type of error
188 * @param string $errStr Error message
189 * @param string $errFile File in which error occurred
190 * @param int $errLine Line number on which error occurred
191 * @return void
192 */
193function XoopsErrorHandler_HandleError($errNo, $errStr, $errFile, $errLine)
194{
195    // NOTE: we only store relative pathnames
196    $new_error = array(
197        'errno' => $errNo,
198        'errstr' => $errStr,
199        'errfile' => preg_replace("|^" . XOOPS_ROOT_PATH . "/|", '', $errFile),
200        'errline' => $errLine
201        );
202    $error_handler =& XoopsErrorHandler::getInstance();
203    $error_handler->handleError($new_error);
204}
205
206/**
207 * User-defined shutdown function (called from 'exit')
208 *
209 * @access public
210 * @return void
211 */
212function XoopsErrorHandler_Shutdown()
213{
214    $error_handler =& XoopsErrorHandler::getInstance();
215    echo $error_handler->renderErrors();
216}
217
218?>
Note: See TracBrowser for help on using the repository browser.