home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / Progress2 / Error.php next >
Encoding:
PHP Script  |  2008-07-02  |  14.1 KB  |  413 lines

  1. <?php
  2. /**
  3.  * Copyright (c) 2005-2008, Laurent Laville <pear@laurent-laville.org>
  4.  *
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  *     * Redistributions of source code must retain the above copyright
  12.  *       notice, this list of conditions and the following disclaimer.
  13.  *     * Redistributions in binary form must reproduce the above copyright
  14.  *       notice, this list of conditions and the following disclaimer in the
  15.  *       documentation and/or other materials provided with the distribution.
  16.  *     * Neither the name of the authors nor the names of its contributors
  17.  *       may be used to endorse or promote products derived from this software
  18.  *       without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * PHP versions 4 and 5
  33.  *
  34.  * @category  HTML
  35.  * @package   HTML_Progress2
  36.  * @author    Laurent Laville <pear@laurent-laville.org>
  37.  * @copyright 2005-2008 Laurent Laville
  38.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  39.  * @version   CVS: $Id: Error.php,v 1.11 2008/03/20 21:27:55 farell Exp $
  40.  * @link      http://pear.php.net/package/HTML_Progress2
  41.  * @since     File available since Release 2.0.0RC1
  42.  */
  43.  
  44. require_once 'PEAR.php';
  45.  
  46. /**
  47.  * This class creates a progress error object, extending the PEAR_Error class.
  48.  *
  49.  * @category  HTML
  50.  * @package   HTML_Progress2
  51.  * @author    Laurent Laville <pear@laurent-laville.org>
  52.  * @copyright 2005-2008 Laurent Laville
  53.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  54.  * @version   Release: 2.4.0
  55.  * @link      http://pear.php.net/package/HTML_Progress2
  56.  * @since     Class available since Release 2.0.0RC1
  57.  */
  58.  
  59. class HTML_Progress2_Error extends PEAR_Error
  60. {
  61.  
  62.     /**
  63.      * Constructor (ZE1)
  64.      *
  65.      * @param string $message  (optional) Final error message
  66.      * @param int    $code     (optional) Error code
  67.      * @param int    $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
  68.      *                         PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  69.      *                         PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  70.      * @param mixed  $options  (optional) error level, _OR_ in the case of
  71.      *                         PEAR_ERROR_CALLBACK, the callback function
  72.      * @param string $userinfo (optional) additional user/debug info
  73.      *
  74.      * @since      version 2.0.0 (2005-10-01)
  75.      * @access     public
  76.      */
  77.     function HTML_Progress2_Error($message = null,
  78.                                   $code = null,
  79.                                   $mode = null, $options = null,
  80.                                   $userinfo = null)
  81.     {
  82.         $this->__construct($message, $code, $mode, $options, $userinfo);
  83.     }
  84.  
  85.     /**
  86.      * Constructor (ZE2)
  87.      *
  88.      * @param string $message  (optional) Final error message
  89.      * @param int    $code     (optional) Error code
  90.      * @param int    $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
  91.      *                         PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  92.      *                         PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  93.      * @param mixed  $options  (optional) error level, _OR_ in the case of
  94.      *                         PEAR_ERROR_CALLBACK, the callback function
  95.      * @param string $userinfo (optional) additional user/debug info
  96.      *
  97.      * @since      version 2.0.0 (2005-10-01)
  98.      * @access     public
  99.      */
  100.     function __construct($message = null,
  101.                          $code = null,
  102.                          $mode = null, $options = null,
  103.                          $userinfo = null)
  104.     {
  105.         if ($mode === null) {
  106.             $mode = PEAR_ERROR_RETURN;
  107.         }
  108.         $this->message   = $message;
  109.         $this->code      = $code;
  110.         $this->mode      = $mode;
  111.         $this->userinfo  = $userinfo;
  112.         $this->backtrace = debug_backtrace();
  113.  
  114.         if ($mode & PEAR_ERROR_CALLBACK) {
  115.             $this->level    = E_USER_NOTICE;
  116.             $this->callback = $options;
  117.         } else {
  118.             if ($options === null) {
  119.                 switch ($userinfo['level']) {
  120.                 case 'exception':
  121.                 case 'error':
  122.                     $options = E_USER_ERROR;
  123.                     break;
  124.                 case 'warning':
  125.                     $options = E_USER_WARNING;
  126.                     break;
  127.                 default:
  128.                     $options = E_USER_NOTICE;
  129.                 }
  130.             }
  131.             $this->level    = $options;
  132.             $this->callback = null;
  133.         }
  134.         if ($this->mode & PEAR_ERROR_PRINT) {
  135.             echo $this->_display($userinfo);
  136.         }
  137.         if ($this->mode & PEAR_ERROR_TRIGGER) {
  138.             trigger_error($this->getMessage(), $this->level);
  139.         }
  140.         if ($this->mode & PEAR_ERROR_DIE) {
  141.             $this->log();
  142.             die();
  143.         }
  144.         if ($this->mode & PEAR_ERROR_CALLBACK) {
  145.             if (is_callable($this->callback)) {
  146.                 call_user_func($this->callback, $this);
  147.             } else {
  148.                 $this->log();
  149.             }
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Get error level from an error object
  155.      *
  156.      * @return     int                      error level
  157.      * @since      version 2.0.0 (2005-10-01)
  158.      * @access     public
  159.      */
  160.     function getLevel()
  161.     {
  162.         return $this->level;
  163.     }
  164.  
  165.     /**
  166.      * Default callback function/method from an error object
  167.      *
  168.      * @return     void
  169.      * @since      version 2.0.0 (2005-10-01)
  170.      * @access     public
  171.      */
  172.     function log()
  173.     {
  174.         $userinfo = $this->getUserInfo();
  175.  
  176.         $display_errors = ini_get('display_errors');
  177.         $log_errors     = ini_get('log_errors');
  178.  
  179.         if ($display_errors) {
  180.             echo $this->_display($userinfo);
  181.         }
  182.  
  183.         if ($log_errors) {
  184.             $this->_log($userinfo);
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * Returns the context of execution formatted.
  190.      *
  191.      * @param string $format the context of execution format
  192.      *
  193.      * @return     string
  194.      * @since      version 2.0.0 (2005-10-01)
  195.      * @access     public
  196.      */
  197.     function sprintContextExec($format)
  198.     {
  199.         $userinfo = $this->getUserInfo();
  200.  
  201.         if (isset($userinfo['context'])) {
  202.             $context = $userinfo['context'];
  203.         } else {
  204.             $context = $this->getBacktrace();
  205.             $context = @array_pop($context);
  206.         }
  207.  
  208.         if ($context) {
  209.             $file = $context['file'];
  210.             $line = $context['line'];
  211.  
  212.             if (isset($context['class'])) {
  213.                 $func  = $context['class'];
  214.                 $func .= $context['type'];
  215.                 $func .= $context['function'];
  216.             } elseif (isset($context['function'])) {
  217.                 $func = $context['function'];
  218.             } else {
  219.                 $func = '';
  220.             }
  221.             return sprintf($format, $file, $line, $func);
  222.         }
  223.         return '';
  224.     }
  225.  
  226.     /**
  227.      * Print an error message
  228.      *
  229.      * @param array $userinfo hash of parameters
  230.      *
  231.      * @return     void
  232.      * @since      version 2.0.0 (2005-10-01)
  233.      * @access     private
  234.      */
  235.     function _display($userinfo)
  236.     {
  237.         $displayDefault = array(
  238.             'eol' => "<br/>\n",
  239.             'lineFormat' => '<b>%1$s</b>: %2$s %3$s',
  240.             'contextFormat' => 'in <b>%3$s</b> ' .
  241.                                '(file <b>%1$s</b> on line <b>%2$s</b>)'
  242.         );
  243.  
  244.         $displayConf = $userinfo['display'];
  245.         $display     = array_merge($displayDefault, $displayConf);
  246.  
  247.         $contextExec = $this->sprintContextExec($display['contextFormat']);
  248.  
  249.         return sprintf($display['lineFormat'] . $display['eol'],
  250.                        ucfirst($userinfo['level']),
  251.                        $this->getMessage(), $contextExec);
  252.     }
  253.  
  254.     /**
  255.      * Send an error message somewhere
  256.      *
  257.      * @param array $userinfo hash of parameters
  258.      *
  259.      * @return     void
  260.      * @since      version 2.0.0 (2005-10-01)
  261.      * @access     private
  262.      */
  263.     function _log($userinfo)
  264.     {
  265.         $logDefault = array(
  266.             'eol' => "\n",
  267.             'lineFormat' => '%1$s %2$s [%3$s] %4$s %5$s',
  268.             'contextFormat' => 'in %3$s (file %1$s on line %2$s)',
  269.             'timeFormat' => '%b %d %H:%M:%S',
  270.             'ident' => $_SERVER['REMOTE_ADDR'],
  271.             'message_type' => 3,
  272.             'destination' => get_class($this) . '.log',
  273.             'extra_headers' => ''
  274.         );
  275.  
  276.         $logConf = $userinfo['log'];
  277.         $log     = array_merge($logDefault, $logConf);
  278.  
  279.         $message_type  = $log['message_type'];
  280.         $destination   = '';
  281.         $extra_headers = '';
  282.         $send          = true;
  283.  
  284.         switch ($message_type) {
  285.         case 0:  // LOG_TYPE_SYSTEM:
  286.             break;
  287.         case 1:  // LOG_TYPE_MAIL:
  288.             $destination   = $log['destination'];
  289.             $extra_headers = $log['extra_headers'];
  290.             break;
  291.         case 3:  // LOG_TYPE_FILE:
  292.             $destination = $log['destination'];
  293.             break;
  294.         default:
  295.             $send = false;
  296.         }
  297.  
  298.         if ($send) {
  299.             $time      = explode(' ', microtime());
  300.             $time      = $time[1] + $time[0];
  301.             $timestamp = isset($userinfo['time']) ? $userinfo['time'] : $time;
  302.  
  303.             $contextExec = $this->sprintContextExec($log['contextFormat']);
  304.  
  305.             $message = sprintf($log['lineFormat'] . $log['eol'],
  306.                            strftime($log['timeFormat'], $timestamp),
  307.                            $log['ident'],
  308.                            $userinfo['level'],
  309.                            $this->getMessage(),
  310.                            $contextExec);
  311.  
  312.             error_log(strip_tags($message), $message_type, $destination,
  313.                       $extra_headers);
  314.         }
  315.     }
  316.  
  317.     /**
  318.      * Default internal error handler
  319.      * Dies if the error is an exception (and would have died anyway)
  320.      *
  321.      * @param int    $code  a numeric error code.
  322.      *                      Valid are HTML_PROGRESS_ERROR_* constants
  323.      * @param string $level error level ('exception', 'error', 'warning', ...)
  324.      *
  325.      * @return     mixed
  326.      * @since      version 2.0.0 (2005-10-01)
  327.      * @access     private
  328.      */
  329.     function _handleError($code, $level)
  330.     {
  331.         if ($level == 'exception') {
  332.             return PEAR_ERROR_DIE;
  333.         } else {
  334.             return null;
  335.         }
  336.     }
  337.  
  338.     /**
  339.      * User callback to generate error messages for any instance
  340.      *
  341.      * @param int   $code     a numeric error code.
  342.      *                        Valid are HTML_PROGRESS_ERROR_* constants
  343.      * @param mixed $userinfo if you need to pass along parameters
  344.      *                        for dynamic messages
  345.      *
  346.      * @return     string
  347.      * @since      version 2.0.0 (2005-10-01)
  348.      * @access     private
  349.      */
  350.     function _msgCallback($code, $userinfo)
  351.     {
  352.         $errorMessages = HTML_Progress2_Error::_getErrorMessage();
  353.  
  354.         if (isset($errorMessages[$code])) {
  355.             $mainmsg = $errorMessages[$code];
  356.         } else {
  357.             $mainmsg = $errorMessages[HTML_PROGRESS_ERROR_UNKNOWN];
  358.         }
  359.  
  360.         if (is_array($userinfo)) {
  361.             foreach ($userinfo as $name => $val) {
  362.                 if (is_array($val)) {
  363.                     // @ is needed in case $val is a multi-dimensional array
  364.                     $val = @implode(', ', $val);
  365.                 }
  366.                 if (is_object($val)) {
  367.                     if (method_exists($val, '__toString')) {
  368.                         $val = $val->__toString();
  369.                     } else {
  370.                         continue;
  371.                     }
  372.                 }
  373.                 $mainmsg = str_replace('%' . $name . '%', $val, $mainmsg);
  374.             }
  375.         }
  376.  
  377.         return $mainmsg;
  378.     }
  379.  
  380.     /**
  381.      * Error Message Template array
  382.      *
  383.      * @return     string
  384.      * @since      version 2.0.0 (2005-10-01)
  385.      * @access     private
  386.      */
  387.     function _getErrorMessage()
  388.     {
  389.         $messages = array(
  390.             HTML_PROGRESS2_ERROR_UNKNOWN =>
  391.                 'unknown error',
  392.             HTML_PROGRESS2_ERROR_INVALID_INPUT =>
  393.                 'invalid input, parameter #%paramnum% '
  394.                     . '"%var%" was expecting '
  395.                     . '"%expected%", instead got "%was%"',
  396.             HTML_PROGRESS2_ERROR_INVALID_CALLBACK =>
  397.                 'invalid callback, parameter #%paramnum% '
  398.                     . '"%var%" expecting %element%,'
  399.                     . ' instead got "%was%" does not exists',
  400.             HTML_PROGRESS2_ERROR_DEPRECATED =>
  401.                 'method is deprecated '
  402.                     . 'use %newmethod% instead of %oldmethod%',
  403.             HTML_PROGRESS2_ERROR_INVALID_OPTION =>
  404.                 '%element% option "%prop%" is not allowed',
  405.             HTML_PROGRESS2_ERROR_INVALID_RESOURCE =>
  406.                 'invalid resource, parameter #%paramnum% '
  407.                     . '"%var%" expecting %expected%,'
  408.                     . ' instead got "%resource%" does not exists'
  409.         );
  410.         return $messages;
  411.     }
  412. }
  413. ?>