PageRenderTime 2ms CodeModel.GetById 23ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle1917/configuracionCated/phpmyadmin/libraries/Error.class.php

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