Ignore:
Timestamp:
Apr 13, 2019, 8:05:15 PM (5 years ago)
Author:
roby
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/client/modules/Elezioni/grafici/jpgraph_errhandler.inc.php

    r2 r265  
    11<?php
    22//=======================================================================
    3 // File:        JPGRAPH_ERRHANDLER.PHP
    4 // Description: Error handler class together with handling of localized
    5 //              error messages. All localized error messages are stored
    6 //              in a separate file under the "lang/" subdirectory.
    7 // Created:     2006-09-24
    8 // Ver:         $Id: jpgraph_errhandler.inc.php 973 2008-03-09 15:29:44Z ljp $
     3// File:        JPGRAPH_ERRHANDLER.PHP
     4// Description: Error handler class together with handling of localized
     5//              error messages. All localized error messages are stored
     6//              in a separate file under the "lang/" subdirectory.
     7// Created:     2006-09-24
     8// Ver:         $Id: jpgraph_errhandler.inc.php 1920 2009-12-08 10:02:26Z ljp $
    99//
    1010// Copyright 2006 (c) Aditus Consulting. All rights reserved.
    1111//========================================================================
    1212
     13if( !defined('DEFAULT_ERR_LOCALE') ) {
     14    define('DEFAULT_ERR_LOCALE','en');
     15}
     16
     17if( !defined('USE_IMAGE_ERROR_HANDLER') ) {
     18    define('USE_IMAGE_ERROR_HANDLER',true);
     19}
    1320
    1421GLOBAL $__jpg_err_locale ;
     
    1724class ErrMsgText {
    1825    private $lt=NULL;
    19     function ErrMsgText() {
    20         GLOBAL $__jpg_err_locale;
    21         $file = 'lang/'.$__jpg_err_locale.'.inc.php';
    22 
    23         // If the chosen locale doesn't exist try english
    24         if( !file_exists(dirname(__FILE__).'/'.$file) ) {
    25             $__jpg_err_locale = 'en';
    26         }
    27 
    28         $file = 'lang/'.$__jpg_err_locale.'.inc.php';
    29         if( !file_exists(dirname(__FILE__).'/'.$file) ) {
    30             die('Chosen locale file ("'.$file.'") for error messages does not exist or is not readable for the PHP process. Please make sure that the file exists and that the file permissions are such that the PHP process is allowed to read this file.');
    31         }
    32         require($file);
    33         $this->lt = $_jpg_messages;
     26    function __construct() {
     27        GLOBAL $__jpg_err_locale;
     28        $file = 'lang/'.$__jpg_err_locale.'.inc.php';
     29
     30        // If the chosen locale doesn't exist try english
     31        if( !file_exists(dirname(__FILE__).'/'.$file) ) {
     32            $__jpg_err_locale = 'en';
     33        }
     34
     35        $file = 'lang/'.$__jpg_err_locale.'.inc.php';
     36        if( !file_exists(dirname(__FILE__).'/'.$file) ) {
     37            die('Chosen locale file ("'.$file.'") for error messages does not exist or is not readable for the PHP process. Please make sure that the file exists and that the file permissions are such that the PHP process is allowed to read this file.');
     38        }
     39        require($file);
     40        $this->lt = $_jpg_messages;
    3441    }
    3542
    3643    function Get($errnbr,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
    37         GLOBAL $__jpg_err_locale;
    38         if( !isset($this->lt[$errnbr]) ) {
    39             return 'Internal error: The specified error message ('.$errnbr.') does not exist in the chosen locale ('.$__jpg_err_locale.')';
    40         }
    41         $ea = $this->lt[$errnbr];
    42         $j=0;
    43         if( $a1 !== null ) {
    44             $argv[$j++] = $a1;
    45             if( $a2 !== null ) {
    46                 $argv[$j++] = $a2;
    47                 if( $a3 !== null ) {
    48                     $argv[$j++] = $a3;
    49                     if( $a4 !== null ) {
    50                         $argv[$j++] = $a4;
    51                         if( $a5 !== null ) {
    52                             $argv[$j++] = $a5;
    53                         }
    54                     }
    55                 }
    56             }
    57         }
    58         $numargs = $j;
    59         if( $ea[1] != $numargs ) {
    60             // Error message argument count do not match.
    61             // Just return the error message without arguments.
    62             return $ea[0];
    63         }
    64         switch( $numargs ) {
    65             case 1:
    66                 $msg = sprintf($ea[0],$argv[0]);
    67                 break;
    68             case 2:
    69                 $msg = sprintf($ea[0],$argv[0],$argv[1]);
    70                 break;
    71             case 3:
    72                 $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2]);
    73                 break;
    74             case 4:
    75                 $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3]);
    76                 break;
    77             case 5:
    78                 $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3],$argv[4]);
    79                 break;
    80             case 0:
    81             default:
    82                 $msg = sprintf($ea[0]);
    83                 break;
    84         }
    85         return $msg;
    86     }
    87 }
    88 
     44        GLOBAL $__jpg_err_locale;
     45        if( !isset($this->lt[$errnbr]) ) {
     46            return 'Internal error: The specified error message ('.$errnbr.') does not exist in the chosen locale ('.$__jpg_err_locale.')';
     47        }
     48        $ea = $this->lt[$errnbr];
     49        $j=0;
     50        if( $a1 !== null ) {
     51            $argv[$j++] = $a1;
     52            if( $a2 !== null ) {
     53                $argv[$j++] = $a2;
     54                if( $a3 !== null ) {
     55                    $argv[$j++] = $a3;
     56                    if( $a4 !== null ) {
     57                        $argv[$j++] = $a4;
     58                        if( $a5 !== null ) {
     59                            $argv[$j++] = $a5;
     60                        }
     61                    }
     62                }
     63            }
     64        }
     65        $numargs = $j;
     66        if( $ea[1] != $numargs ) {
     67            // Error message argument count do not match.
     68            // Just return the error message without arguments.
     69            return $ea[0];
     70        }
     71        switch( $numargs ) {
     72            case 1:
     73                $msg = sprintf($ea[0],$argv[0]);
     74                break;
     75            case 2:
     76                $msg = sprintf($ea[0],$argv[0],$argv[1]);
     77                break;
     78            case 3:
     79                $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2]);
     80                break;
     81            case 4:
     82                $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3]);
     83                break;
     84            case 5:
     85                $msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3],$argv[4]);
     86                break;
     87            case 0:
     88            default:
     89                $msg = sprintf($ea[0]);
     90                break;
     91        }
     92        return $msg;
     93    }
     94}
     95     
    8996//
    9097// A wrapper class that is used to access the specified error object
     
    93100//
    94101class JpGraphError {
    95     private static $__jpg_err;
    96     public static function Install($aErrObject) {
    97         self::$__jpg_err = new $aErrObject;
    98     }
     102    private static $__iImgFlg = true;
     103    private static $__iLogFile = '';
     104    private static $__iTitle = 'JpGraph Error: ';
    99105    public static function Raise($aMsg,$aHalt=true){
    100         self::$__jpg_err->Raise($aMsg,$aHalt);
     106        throw new JpGraphException($aMsg);
    101107    }
    102108    public static function SetErrLocale($aLoc) {
    103         GLOBAL $__jpg_err_locale ;
    104         $__jpg_err_locale = $aLoc;
     109        GLOBAL $__jpg_err_locale ;
     110        $__jpg_err_locale = $aLoc;
    105111    }
    106112    public static function RaiseL($errnbr,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
    107         $t = new ErrMsgText();
    108         $msg = $t->Get($errnbr,$a1,$a2,$a3,$a4,$a5);
    109         self::$__jpg_err->Raise($msg);
    110     }
    111 }
     113        throw new JpGraphExceptionL($errnbr,$a1,$a2,$a3,$a4,$a5);
     114    }
     115    public static function SetImageFlag($aFlg=true) {
     116        self::$__iImgFlg = $aFlg;
     117    }
     118    public static function GetImageFlag() {
     119        return self::$__iImgFlg;
     120    }
     121    public static function SetLogFile($aFile) {
     122        self::$__iLogFile = $aFile;
     123    }
     124    public static function GetLogFile() {
     125        return self::$__iLogFile;
     126    }
     127    public static function SetTitle($aTitle) {
     128        self::$__iTitle = $aTitle;
     129    }
     130    public static function GetTitle() {
     131        return self::$__iTitle;
     132    }   
     133}
     134
     135class JpGraphException extends Exception {
     136    // Redefine the exception so message isn't optional
     137    public function __construct($message, $code = 0) {
     138        // make sure everything is assigned properly
     139        parent::__construct($message, $code);
     140    }
     141    // custom string representation of object
     142    public function _toString() {
     143        return __CLASS__ . ": [{$this->code}]: {$this->message} at " . basename($this->getFile()) . ":" . $this->getLine() . "\n" . $this->getTraceAsString() . "\n";
     144    }
     145    // custom representation of error as an image
     146    public function Stroke() {
     147        if( JpGraphError::GetImageFlag() ) {
     148                $errobj = new JpGraphErrObjectImg();
     149                $errobj->SetTitle(JpGraphError::GetTitle());
     150        }
     151        else {                 
     152                $errobj = new JpGraphErrObject();
     153                $errobj->SetTitle(JpGraphError::GetTitle());                   
     154                $errobj->SetStrokeDest(JpGraphError::GetLogFile());
     155        }
     156        $errobj->Raise($this->getMessage());
     157    }
     158    static public function defaultHandler(Exception $exception) {
     159        global $__jpg_OldHandler;
     160        if( $exception instanceof JpGraphException ) {
     161            $exception->Stroke();
     162        }
     163        else {
     164            // Restore old handler
     165            if( $__jpg_OldHandler !== NULL ) {
     166                set_exception_handler($__jpg_OldHandler);
     167            }
     168            throw $exception;
     169        }
     170    }
     171}
     172
     173class JpGraphExceptionL extends JpGraphException {
     174   // Redefine the exception so message isn't optional
     175    public function __construct($errcode,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
     176        // make sure everything is assigned properly
     177        $errtxt = new ErrMsgText();
     178        JpGraphError::SetTitle('JpGraph Error: '.$errcode);
     179        parent::__construct($errtxt->Get($errcode,$a1,$a2,$a3,$a4,$a5), 0);
     180    }
     181}
     182
     183// Setup the default handler
     184global $__jpg_OldHandler;
     185$__jpg_OldHandler = set_exception_handler(array('JpGraphException','defaultHandler'));
    112186
    113187//
     
    120194class JpGraphErrObject {
    121195
    122     protected $iTitle = "JpGraph Error";
     196    protected $iTitle = "JpGraph error: ";
    123197    protected $iDest = false;
    124198
    125199
    126     function JpGraphErrObject() {
    127         // Empty. Reserved for future use
     200    function __construct() {
     201        // Empty. Reserved for future use
    128202    }
    129203
    130204    function SetTitle($aTitle) {
    131         $this->iTitle = $aTitle;
    132     }
    133 
    134     function SetStrokeDest($aDest) { 
    135         $this->iDest = $aDest;
     205        $this->iTitle = $aTitle;
     206    }
     207
     208    function SetStrokeDest($aDest) {
     209        $this->iDest = $aDest;
    136210    }
    137211
    138212    // If aHalt is true then execution can't continue. Typical used for fatal errors
    139     function Raise($aMsg,$aHalt=true) {
    140         $aMsg = $this->iTitle.' '.$aMsg;
    141         if ($this->iDest) {
    142             $f = @fopen($this->iDest,'a');
    143             if( $f ) {
    144                 @fwrite($f,$aMsg);
    145                 @fclose($f);
    146             }
    147         }
    148         else {
    149             echo $aMsg;
    150         }
    151         if( $aHalt )
    152             die();
     213    function Raise($aMsg,$aHalt=false) {
     214        if( $this->iDest != '' ) {
     215                if( $this->iDest == 'syslog' ) {
     216                        error_log($this->iTitle.$aMsg);
     217                }
     218                else {
     219                        $str = '['.date('r').'] '.$this->iTitle.$aMsg."\n";
     220                        $f = @fopen($this->iDest,'a');
     221                if( $f ) {             
     222                        @fwrite($f,$str);
     223                    @fclose($f);
     224                }
     225                }
     226        }
     227        else {
     228                $aMsg = $this->iTitle.$aMsg;           
     229                // Check SAPI and if we are called from the command line
     230                // send the error to STDERR instead
     231                if( PHP_SAPI == 'cli' ) {
     232                        fwrite(STDERR,$aMsg);
     233                }
     234                else {
     235                echo $aMsg;
     236                }
     237        }
     238        if( $aHalt )
     239                exit(1);
    153240    }
    154241}
     
    158245//==============================================================
    159246class JpGraphErrObjectImg extends JpGraphErrObject {
     247   
     248    function __construct() {
     249        parent::__construct();
     250        // Empty. Reserved for future use
     251    }
    160252
    161253    function Raise($aMsg,$aHalt=true) {
    162         $img_iconerror =
    163             'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAAaV'.
    164             'BMVEX//////2Xy8mLl5V/Z2VvMzFi/v1WyslKlpU+ZmUyMjEh/'.
    165             'f0VyckJlZT9YWDxMTDjAwMDy8sLl5bnY2K/MzKW/v5yyspKlpY'.
    166             'iYmH+MjHY/PzV/f2xycmJlZVlZWU9MTEXY2Ms/PzwyMjLFTjea'.
    167             'AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACx'.
    168             'IAAAsSAdLdfvwAAAAHdElNRQfTBgISOCqusfs5AAABLUlEQVR4'.
    169             '2tWV3XKCMBBGWfkranCIVClKLd/7P2Q3QsgCxjDTq+6FE2cPH+'.
    170             'xJ0Ogn2lQbsT+Wrs+buAZAV4W5T6Bs0YXBBwpKgEuIu+JERAX6'.
    171             'wM2rHjmDdEITmsQEEmWADgZm6rAjhXsoMGY9B/NZBwJzBvn+e3'.
    172             'wHntCAJdGu9SviwIwoZVDxPB9+Rc0TSEbQr0j3SA1gwdSn6Db0'.
    173             '6Tm1KfV6yzWGQO7zdpvyKLKBDmRFjzeB3LYgK7r6A/noDAfjtS'.
    174             'IXaIzbJSv6WgUebTMV4EoRB8a2mQiQjgtF91HdKDKZ1gtFtQjk'.
    175             'YcWaR5OKOhkYt+ZsTFdJRfPAApOpQYJTNHvCRSJR6SJngQadfc'.
    176             'vd69OLMddVOPCGVnmrFD8bVYd3JXfxXPtLR/+mtv59/ALWiiMx'.
    177             'qL72fwAAAABJRU5ErkJggg==' ;
    178 
    179         if( function_exists("imagetypes") )
    180             $supported = imagetypes();
    181         else
    182             $supported = 0;
    183 
    184         if( !function_exists('imagecreatefromstring') )
    185             $supported = 0;
    186 
    187         if( ob_get_length() || headers_sent() || !($supported & IMG_PNG) ) {
    188             // Special case for headers already sent or that the installation doesn't support
    189             // the PNG format (which the error icon is encoded in).
    190             // Dont return an image since it can't be displayed
    191             die($this->iTitle.' '.$aMsg);               
    192         }
    193 
    194         $aMsg = wordwrap($aMsg,55);
    195         $lines = substr_count($aMsg,"\n");
    196 
    197         // Create the error icon GD
    198         $erricon = Image::CreateFromString(base64_decode($img_iconerror));   
    199 
    200         // Create an image that contains the error text.
    201         $w=400;         
    202         $h=100 + 15*max(0,$lines-3);
    203 
    204         $img = new Image($w,$h);
    205 
    206 
    207         // Drop shadow
    208         $img->SetColor("gray");
    209         $img->FilledRectangle(5,5,$w-1,$h-1,10);
    210         $img->SetColor("gray:0.7");
    211         $img->FilledRectangle(5,5,$w-3,$h-3,10);
    212        
    213         // Window background
    214         $img->SetColor("lightblue");
    215         $img->FilledRectangle(1,1,$w-5,$h-5);
    216         $img->CopyCanvasH($img->img,$erricon,5,30,0,0,40,40);
    217 
    218         // Window border
    219         $img->SetColor("black");
    220         $img->Rectangle(1,1,$w-5,$h-5);
    221         $img->Rectangle(0,0,$w-4,$h-4);
    222        
    223         // Window top row
    224         $img->SetColor("darkred");
    225         for($y=3; $y < 18; $y += 2 )
    226             $img->Line(1,$y,$w-6,$y);
    227 
    228         // "White shadow"
    229         $img->SetColor("white");
    230 
    231         // Left window edge
    232         $img->Line(2,2,2,$h-5);
    233         $img->Line(2,2,$w-6,2);
    234 
    235         // "Gray button shadow"
    236         $img->SetColor("darkgray");
    237 
    238         // Gray window shadow
    239         $img->Line(2,$h-6,$w-5,$h-6);
    240         $img->Line(3,$h-7,$w-5,$h-7);
    241 
    242         // Window title
    243         $m = floor($w/2-5);
    244         $l = 100;
    245         $img->SetColor("lightgray:1.3");
    246         $img->FilledRectangle($m-$l,2,$m+$l,16);
    247 
    248         // Stroke text
    249         $img->SetColor("darkred");
    250         $img->SetFont(FF_FONT2,FS_BOLD);
    251         $img->StrokeText($m-50,15,$this->iTitle);
    252         $img->SetColor("black");
    253         $img->SetFont(FF_FONT1,FS_NORMAL);
    254         $txt = new Text($aMsg,52,25);
    255         $txt->Align("left","top");
    256         $txt->Stroke($img);
    257         if ($this->iDest) {
    258            $img->Stream($this->iDest);
    259         } else {
    260             $img->Headers();
    261             $img->Stream();
    262         }
    263         if( $aHalt )
    264             die();
    265     }
    266 }
    267 
    268 
    269 // Install the default error handler
    270 if( USE_IMAGE_ERROR_HANDLER ) {
    271     JpGraphError::Install("JpGraphErrObjectImg");
    272 }
    273 else {
    274     JpGraphError::Install("JpGraphErrObject");
    275 }
    276 
    277 
     254        $img_iconerror =
     255     'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAAaV'.
     256     'BMVEX//////2Xy8mLl5V/Z2VvMzFi/v1WyslKlpU+ZmUyMjEh/'.
     257     'f0VyckJlZT9YWDxMTDjAwMDy8sLl5bnY2K/MzKW/v5yyspKlpY'.
     258     'iYmH+MjHY/PzV/f2xycmJlZVlZWU9MTEXY2Ms/PzwyMjLFTjea'.
     259     'AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACx'.
     260     'IAAAsSAdLdfvwAAAAHdElNRQfTBgISOCqusfs5AAABLUlEQVR4'.
     261     '2tWV3XKCMBBGWfkranCIVClKLd/7P2Q3QsgCxjDTq+6FE2cPH+'.
     262     'xJ0Ogn2lQbsT+Wrs+buAZAV4W5T6Bs0YXBBwpKgEuIu+JERAX6'.
     263     'wM2rHjmDdEITmsQEEmWADgZm6rAjhXsoMGY9B/NZBwJzBvn+e3'.
     264     'wHntCAJdGu9SviwIwoZVDxPB9+Rc0TSEbQr0j3SA1gwdSn6Db0'.
     265     '6Tm1KfV6yzWGQO7zdpvyKLKBDmRFjzeB3LYgK7r6A/noDAfjtS'.
     266     'IXaIzbJSv6WgUebTMV4EoRB8a2mQiQjgtF91HdKDKZ1gtFtQjk'.
     267     'YcWaR5OKOhkYt+ZsTFdJRfPAApOpQYJTNHvCRSJR6SJngQadfc'.
     268     'vd69OLMddVOPCGVnmrFD8bVYd3JXfxXPtLR/+mtv59/ALWiiMx'.
     269     'qL72fwAAAABJRU5ErkJggg==' ;
     270
     271       
     272        if( function_exists("imagetypes") ) {
     273            $supported = imagetypes();
     274        } else {
     275            $supported = 0;
     276        }
     277
     278        if( !function_exists('imagecreatefromstring') ) {
     279            $supported = 0;
     280        }
     281       
     282        if( ob_get_length() || headers_sent() || !($supported & IMG_PNG) ) {
     283            // Special case for headers already sent or that the installation doesn't support
     284            // the PNG format (which the error icon is encoded in).
     285            // Dont return an image since it can't be displayed
     286            die($this->iTitle.' '.$aMsg);
     287        }
     288
     289        $aMsg = wordwrap($aMsg,55);
     290        $lines = substr_count($aMsg,"\n");
     291
     292        // Create the error icon GD
     293        $erricon = Image::CreateFromString(base64_decode($img_iconerror));
     294
     295        // Create an image that contains the error text.
     296        $w=400;
     297        $h=100 + 15*max(0,$lines-3);
     298
     299        $img = new Image($w,$h);
     300
     301
     302        // Drop shadow
     303        $img->SetColor("gray");
     304        $img->FilledRectangle(5,5,$w-1,$h-1,10);
     305        $img->SetColor("gray:0.7");
     306        $img->FilledRectangle(5,5,$w-3,$h-3,10);
     307
     308        // Window background
     309        $img->SetColor("lightblue");
     310        $img->FilledRectangle(1,1,$w-5,$h-5);
     311        $img->CopyCanvasH($img->img,$erricon,5,30,0,0,40,40);
     312
     313        // Window border
     314        $img->SetColor("black");
     315        $img->Rectangle(1,1,$w-5,$h-5);
     316        $img->Rectangle(0,0,$w-4,$h-4);
     317
     318        // Window top row
     319        $img->SetColor("darkred");
     320        for($y=3; $y < 18; $y += 2 )
     321        $img->Line(1,$y,$w-6,$y);
     322
     323        // "White shadow"
     324        $img->SetColor("white");
     325
     326        // Left window edge
     327        $img->Line(2,2,2,$h-5);
     328        $img->Line(2,2,$w-6,2);
     329
     330        // "Gray button shadow"
     331        $img->SetColor("darkgray");
     332
     333        // Gray window shadow
     334        $img->Line(2,$h-6,$w-5,$h-6);
     335        $img->Line(3,$h-7,$w-5,$h-7);
     336
     337        // Window title
     338        $m = floor($w/2-5);
     339        $l = 110;
     340        $img->SetColor("lightgray:1.3");
     341        $img->FilledRectangle($m-$l,2,$m+$l,16);
     342
     343        // Stroke text
     344        $img->SetColor("darkred");
     345        $img->SetFont(FF_FONT2,FS_BOLD);
     346        $img->StrokeText($m-90,15,$this->iTitle);
     347        $img->SetColor("black");
     348        $img->SetFont(FF_FONT1,FS_NORMAL);
     349        $txt = new Text($aMsg,52,25);
     350        $txt->SetFont(FF_FONT1);
     351        $txt->Align("left","top");
     352        $txt->Stroke($img);
     353        if ($this->iDest) {
     354            $img->Stream($this->iDest);
     355        } else {
     356            $img->Headers();
     357            $img->Stream();
     358        }
     359        if( $aHalt )
     360            die();
     361    }
     362}
     363
     364
     365
     366if( ! USE_IMAGE_ERROR_HANDLER ) {
     367        JpGraphError::SetImageFlag(false);
     368}
    278369?>
Note: See TracChangeset for help on using the changeset viewer.