PageRenderTime 43ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/phpmyadmin/libraries/Error.class.php

https://github.com/steph/PortFolio
PHP | 422 lines | 198 code | 30 blank | 194 comment | 11 complexity | 7e20b72e1d309d85875d0c2b68b812ac MD5 | raw file
  1<?php
  2/* vim: set expandtab sw=4 ts=4 sts=4: */
  3/**
  4 * Holds class PMA_Error
  5 *
  6 * @package phpMyAdmin
  7 */
  8
  9/**
 10 * base class
 11 */
 12require_once './libraries/Message.class.php';
 13
 14/**
 15 * a single error
 16 *
 17 * @package phpMyAdmin
 18 */
 19class PMA_Error extends PMA_Message
 20{
 21    /**
 22     * Error types
 23     *
 24     * @var array
 25     */
 26    static public $errortype = array (
 27        E_ERROR              => 'Error',
 28        E_WARNING            => 'Warning',
 29        E_PARSE              => 'Parsing Error',
 30        E_NOTICE             => 'Notice',
 31        E_CORE_ERROR         => 'Core Error',
 32        E_CORE_WARNING       => 'Core Warning',
 33        E_COMPILE_ERROR      => 'Compile Error',
 34        E_COMPILE_WARNING    => 'Compile Warning',
 35        E_USER_ERROR         => 'User Error',
 36        E_USER_WARNING       => 'User Warning',
 37        E_USER_NOTICE        => 'User Notice',
 38        E_STRICT             => 'Runtime Notice',
 39        E_DEPRECATED         => 'Deprecation Notice',
 40        E_RECOVERABLE_ERROR  => 'Catchable Fatal Error',
 41    );
 42
 43    /**
 44     * Error levels
 45     *
 46     * @var array
 47     */
 48    static public $errorlevel = array (
 49        E_ERROR              => 'error',
 50        E_WARNING            => 'error',
 51        E_PARSE              => 'error',
 52        E_NOTICE             => 'notice',
 53        E_CORE_ERROR         => 'error',
 54        E_CORE_WARNING       => 'error',
 55        E_COMPILE_ERROR      => 'error',
 56        E_COMPILE_WARNING    => 'error',
 57        E_USER_ERROR         => 'error',
 58        E_USER_WARNING       => 'error',
 59        E_USER_NOTICE        => 'notice',
 60        E_STRICT             => 'notice',
 61        E_DEPRECATED         => 'notice',
 62        E_RECOVERABLE_ERROR  => 'error',
 63    );
 64
 65    /**
 66     * The file in which the error occured
 67     *
 68     * @var string
 69     */
 70    protected $_file = '';
 71
 72    /**
 73     * The line in which the error occured
 74     *
 75     * @var integer
 76     */
 77    protected $_line = 0;
 78
 79    /**
 80     * Holds the backtrace for this error
 81     *
 82     * @var array
 83     */
 84    protected $_backtrace = array();
 85
 86    /**
 87     * Unique id
 88     *
 89     * @var string
 90     */
 91    protected $_hash = null;
 92
 93    /**
 94     * Constructor
 95     *
 96     * @uses    debug_backtrace()
 97     * @uses    PMA_Error::setNumber()
 98     * @uses    PMA_Error::setMessage()
 99     * @uses    PMA_Error::setFile()
100     * @uses    PMA_Error::setLine()
101     * @uses    PMA_Error::setBacktrace()
102     * @param   integer $errno
103     * @param   string  $errstr
104     * @param   string  $errfile
105     * @param   integer $errline
106     */
107    public function __construct($errno, $errstr, $errfile, $errline)
108    {
109        $this->setNumber($errno);
110        $this->setMessage($errstr, false);
111        $this->setFile($errfile);
112        $this->setLine($errline);
113
114        $backtrace = debug_backtrace();
115        // remove last two calls: debug_backtrace() and handleError()
116        unset($backtrace[0]);
117        unset($backtrace[1]);
118
119        $this->setBacktrace($backtrace);
120    }
121
122    /**
123     * sets PMA_Error::$_backtrace
124     *
125     * @uses    PMA_Error::$_backtrace to set it
126     * @param   array $backtrace
127     */
128    public function setBacktrace($backtrace)
129    {
130        $this->_backtrace = $backtrace;
131    }
132
133    /**
134     * sets PMA_Error::$_line
135     *
136     * @uses    PMA_Error::$_line to set it
137     * @param   integer $line
138     */
139    public function setLine($line)
140    {
141        $this->_line = $line;
142    }
143
144    /**
145     * sets PMA_Error::$_file
146     *
147     * @uses    PMA_Error::$_file to set it
148     * @uses    PMA_Error::relPath()
149     * @param   string $file
150     */
151    public function setFile($file)
152    {
153        $this->_file = PMA_Error::relPath($file);
154    }
155
156
157    /**
158     * returns unique PMA_Error::$_hash, if not exists it will be created
159     *
160     * @uses    PMA_Error::$_hash as return value and to set it if required
161     * @uses    PMA_Error::getNumber()
162     * @uses    PMA_Error::getMessage()
163     * @uses    PMA_Error::getFile()
164     * @uses    PMA_Error::getLine()
165     * @uses    PMA_Error::getBacktrace()
166     * @uses    md5()
167     * @param   string $file
168     * @return  string PMA_Error::$_hash
169     */
170    public function getHash()
171    {
172        try {
173            $backtrace = serialize($this->getBacktrace());
174        } catch(Exception $e){
175            $backtrace = '';
176        }
177        if (null === $this->_hash) {
178            $this->_hash = md5(
179                $this->getNumber() .
180                $this->getMessage() .
181                $this->getFile() .
182                $this->getLine() .
183                $backtrace
184            );
185        }
186
187        return $this->_hash;
188    }
189
190    /**
191     * returns PMA_Error::$_backtrace
192     *
193     * @uses    PMA_Error::$_backtrace as return value
194     * @return  array PMA_Error::$_backtrace
195     */
196    public function getBacktrace()
197    {
198        return $this->_backtrace;
199    }
200
201    /**
202     * returns PMA_Error::$_file
203     *
204     * @uses    PMA_Error::$_file as return value
205     * @return  string PMA_Error::$_file
206     */
207    public function getFile()
208    {
209        return $this->_file;
210    }
211
212    /**
213     * returns PMA_Error::$_line
214     *
215     * @uses    PMA_Error::$_line as return value
216     * @return  integer PMA_Error::$_line
217     */
218    public function getLine()
219    {
220        return $this->_line;
221    }
222
223    /**
224     * returns type of error
225     *
226     * @uses    PMA_Error::$errortype
227     * @uses    PMA_Error::getNumber()
228     * @return  string  type of error
229     */
230    public function getType()
231    {
232        return PMA_Error::$errortype[$this->getNumber()];
233    }
234
235    /**
236     * returns level of error
237     *
238     * @uses    PMA_Error::$$errorlevel
239     * @uses    PMA_Error::getNumber()
240     * @return  string  level of error
241     */
242    public function getLevel()
243    {
244        return PMA_Error::$errorlevel[$this->getNumber()];
245    }
246
247    /**
248     * returns title prepared for HTML Title-Tag
249     *
250     * @uses    PMA_Error::getTitle()
251     * @uses    htmlspecialchars()
252     * @uses    substr()
253     * @return  string   HTML escaped and truncated title
254     */
255    public function getHtmlTitle()
256    {
257        return htmlspecialchars(substr($this->getTitle(), 0, 100));
258    }
259
260    /**
261     * returns title for error
262     *
263     * @uses    PMA_Error::getType()
264     * @uses    PMA_Error::getMessage()
265     * @return string
266     */
267    public function getTitle()
268    {
269        return $this->getType() . ': ' . $this->getMessage();
270    }
271
272    /**
273     * Display HTML backtrace
274     *
275     * @uses    PMA_Error::getBacktrace()
276     * @uses    PMA_Error::relPath()
277     * @uses    PMA_Error::displayArg()
278     * @uses    count()
279     */
280    public function displayBacktrace()
281    {
282        foreach ($this->getBacktrace() as $step) {
283            echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
284            if (isset($step['class'])) {
285                echo $step['class'] . $step['type'];
286            }
287            echo $step['function'] . '(';
288            if (isset($step['args']) && (count($step['args']) > 1)) {
289                echo "<br />\n";
290                foreach ($step['args'] as $arg) {
291                    echo "\t";
292                    $this->displayArg($arg, $step['function']);
293                    echo ',' . "<br />\n";
294                }
295            } elseif (isset($step['args']) && (count($step['args']) > 0)) {
296                foreach ($step['args'] as $arg) {
297                    $this->displayArg($arg, $step['function']);
298                }
299            }
300            echo ')' . "<br />\n";
301        }
302    }
303
304    /**
305     * Display a single function argument
306     * if $function is one of include/require the $arg is converted te relative path
307     *
308     * @uses    PMA_Error::relPath()
309     * @uses    in_array()
310     * @uses    gettype()
311     * @param string $arg
312     * @param string $function
313     */
314    protected function displayArg($arg, $function)
315    {
316        $include_functions = array(
317            'include',
318            'include_once',
319            'require',
320            'require_once',
321        );
322
323        if (in_array($function, $include_functions)) {
324            echo PMA_Error::relPath($arg);
325        } elseif (is_scalar($arg)) {
326            echo gettype($arg) . ' ' . htmlspecialchars($arg);
327        } else {
328            echo gettype($arg);
329        }
330    }
331
332    /**
333     * Displays the error in HTML
334     *
335     * @uses    PMA_Error::getLevel()
336     * @uses    PMA_Error::getType()
337     * @uses    PMA_Error::getMessage()
338     * @uses    PMA_Error::displayBacktrace()
339     * @uses    PMA_Error::isDisplayed()
340     */
341    public function display()
342    {
343        echo '<div class="' . $this->getLevel() . '">';
344        if (! $this->isUserError()) {
345            echo '<strong>' . $this->getType() . '</strong>';
346            echo ' in ' . $this->getFile() . '#' . $this->getLine();
347            echo "<br />\n";
348        }
349        echo $this->getMessage();
350        if (! $this->isUserError()) {
351            echo "<br />\n";
352            echo "<br />\n";
353            echo "<strong>Backtrace</strong><br />\n";
354            echo "<br />\n";
355            echo $this->displayBacktrace();
356        }
357        echo '</div>';
358        $this->isDisplayed(true);
359    }
360
361    /**
362     * whether this error is a user error
363     *
364     * @uses    E_USER_WARNING
365     * @uses    E_USER_ERROR
366     * @uses    E_USER_NOTICE
367     * @uses    PMA_Error::getNumber()
368     * @return  boolean
369     */
370    public function isUserError()
371    {
372        return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
373    }
374
375    /**
376     * return short relative path to phpMyAdmin basedir
377     *
378     * prevent path disclusore in error message,
379     * and make users feel save to submit error reports
380     *
381     * @static
382     * @uses    PHP_OS()
383     * @uses    __FILE__()
384     * @uses    realpath()
385     * @uses    substr()
386     * @uses    explode()
387     * @uses    dirname()
388     * @uses    implode()
389     * @uses    count()
390     * @uses    array_pop()
391     * @uses    str_replace()
392     * @param   string $dest  path to be shorten
393     * @return  string shortened path
394     */
395    static function relPath($dest)
396    {
397        $dest = realpath($dest);
398
399        if (substr(PHP_OS, 0, 3) == 'WIN') {
400            $path_separator = '\\';
401        } else {
402            $path_separator = '/';
403        }
404
405        $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
406        $Adest = explode($path_separator, $dest);
407
408        $result = '.';
409        // && count ($Adest)>0 && count($Ahere)>0 )
410        while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
411            if (count($Ahere) > count($Adest)) {
412                array_pop($Ahere);
413                $result .= $path_separator . '..';
414            } else {
415                array_pop($Adest);
416            }
417        }
418        $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
419        return str_replace($path_separator . $path_separator, $path_separator, $path);
420    }
421}
422?>