1 | <?php
|
---|
2 | //=======================================================================
|
---|
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 $
|
---|
9 | //
|
---|
10 | // Copyright 2006 (c) Aditus Consulting. All rights reserved.
|
---|
11 | //========================================================================
|
---|
12 |
|
---|
13 |
|
---|
14 | GLOBAL $__jpg_err_locale ;
|
---|
15 | $__jpg_err_locale = DEFAULT_ERR_LOCALE;
|
---|
16 |
|
---|
17 | class ErrMsgText {
|
---|
18 | 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;
|
---|
34 | }
|
---|
35 |
|
---|
36 | 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 |
|
---|
89 | //
|
---|
90 | // A wrapper class that is used to access the specified error object
|
---|
91 | // (to hide the global error parameter and avoid having a GLOBAL directive
|
---|
92 | // in all methods.
|
---|
93 | //
|
---|
94 | class JpGraphError {
|
---|
95 | private static $__jpg_err;
|
---|
96 | public static function Install($aErrObject) {
|
---|
97 | self::$__jpg_err = new $aErrObject;
|
---|
98 | }
|
---|
99 | public static function Raise($aMsg,$aHalt=true){
|
---|
100 | self::$__jpg_err->Raise($aMsg,$aHalt);
|
---|
101 | }
|
---|
102 | public static function SetErrLocale($aLoc) {
|
---|
103 | GLOBAL $__jpg_err_locale ;
|
---|
104 | $__jpg_err_locale = $aLoc;
|
---|
105 | }
|
---|
106 | 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 | }
|
---|
112 |
|
---|
113 | //
|
---|
114 | // First of all set up a default error handler
|
---|
115 | //
|
---|
116 |
|
---|
117 | //=============================================================
|
---|
118 | // The default trivial text error handler.
|
---|
119 | //=============================================================
|
---|
120 | class JpGraphErrObject {
|
---|
121 |
|
---|
122 | protected $iTitle = "JpGraph Error";
|
---|
123 | protected $iDest = false;
|
---|
124 |
|
---|
125 |
|
---|
126 | function JpGraphErrObject() {
|
---|
127 | // Empty. Reserved for future use
|
---|
128 | }
|
---|
129 |
|
---|
130 | function SetTitle($aTitle) {
|
---|
131 | $this->iTitle = $aTitle;
|
---|
132 | }
|
---|
133 |
|
---|
134 | function SetStrokeDest($aDest) {
|
---|
135 | $this->iDest = $aDest;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // 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();
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | //==============================================================
|
---|
157 | // An image based error handler
|
---|
158 | //==============================================================
|
---|
159 | class JpGraphErrObjectImg extends JpGraphErrObject {
|
---|
160 |
|
---|
161 | 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 |
|
---|
278 | ?>
|
---|