source: trunk/client/modules/Elezioni/grafici/jpgraph_errhandler.inc.php@ 392

Last change on this file since 392 was 346, checked in by roby, 4 years ago

ulteriori modifiche per adeguamento a php7
Client: aggiornamento jpgraph

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