Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 aurelien 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 974 2008-03-09 15:30:01Z ljp $
9
//
10
// Copyright 2006 (c) Aditus Consulting. All rights reserved.
11
//========================================================================
12
 
13
 
14
// Since the error handler needs to be accessed from anywhere this must be
15
// made a global variable.
16
GLOBAL $__jpg_err;
17
GLOBAL $__jpg_err_locale ;
18
$__jpg_err_locale = DEFAULT_ERR_LOCALE;
19
 
20
// Handles the retrieval of localized error messages
21
class ErrMsgText {
22
    var $lt=NULL;
23
    function ErrMsgText() {
24
	GLOBAL $__jpg_err_locale;
25
	$file = 'lang/'.$__jpg_err_locale.'.inc.php';
26
 
27
	// If the chosen locale doesn't exist try english
28
	if( !file_exists(dirname(__FILE__).'/'.$file) ) {
29
	    $__jpg_err_locale = 'en';
30
	}
31
 
32
	$file = 'lang/'.$__jpg_err_locale.'.inc.php';
33
	if( !file_exists(dirname(__FILE__).'/'.$file) ) {
34
	    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.');
35
	}
36
	require($file);
37
	$this->lt = $_jpg_messages;
38
    }
39
 
40
    function Get($errnbr,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
41
	GLOBAL $__jpg_err_locale;
42
	if( !isset($this->lt[$errnbr]) ) {
43
	    return 'Internal error: The specified error message ('.$errnbr.') does not exist in the chosen locale ('.$__jpg_err_locale.')';
44
	}
45
	$ea = $this->lt[$errnbr];
46
	$j=0;
47
	if( $a1 !== null ) {
48
	    $argv[$j++] = $a1;
49
	    if( $a2 !== null ) {
50
		$argv[$j++] = $a2;
51
		if( $a3 !== null ) {
52
		    $argv[$j++] = $a3;
53
		    if( $a4 !== null ) {
54
			$argv[$j++] = $a4;
55
			if( $a5 !== null ) {
56
			    $argv[$j++] = $a5;
57
			}
58
		    }
59
		}
60
	    }
61
	}
62
	$numargs = $j;
63
	if( $ea[1] != $numargs ) {
64
	    // Error message argument count do not match.
65
	    // Just return the error message without arguments.
66
	    return $ea[0];
67
	}
68
	switch( $numargs ) {
69
	    case 1:
70
		$msg = sprintf($ea[0],$argv[0]);
71
		break;
72
	    case 2:
73
		$msg = sprintf($ea[0],$argv[0],$argv[1]);
74
		break;
75
	    case 3:
76
		$msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2]);
77
		break;
78
	    case 4:
79
		$msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3]);
80
		break;
81
	    case 5:
82
		$msg = sprintf($ea[0],$argv[0],$argv[1],$argv[2],$argv[3],$argv[4]);
83
		break;
84
	    case 0:
85
	    default:
86
		$msg = sprintf($ea[0]);
87
		break;
88
	}
89
	return $msg;
90
    }
91
}
92
 
93
//
94
// A wrapper class that is used to access the specified error object
95
// (to hide the global error parameter and avoid having a GLOBAL directive
96
// in all methods.
97
//
98
class JpGraphError {
99
    function Install($aErrObject) {
100
	GLOBAL $__jpg_err;
101
	$__jpg_err = $aErrObject;
102
    }
103
    function SetErrLocale($aLoc) {
104
	GLOBAL $__jpg_err_locale ;
105
	$__jpg_err_locale = $aLoc;
106
    }
107
    function Raise($aMsg,$aHalt=true){
108
	GLOBAL $__jpg_err;
109
	$tmp = new $__jpg_err;
110
	$tmp->Raise($aMsg,$aHalt);
111
    }
112
    function RaiseL($errnbr,$a1=null,$a2=null,$a3=null,$a4=null,$a5=null) {
113
	GLOBAL $__jpg_err;
114
	$t = new ErrMsgText();
115
	$msg = $t->Get($errnbr,$a1,$a2,$a3,$a4,$a5);
116
	$tmp = new $__jpg_err;
117
	$tmp->Raise($msg);
118
    }
119
}
120
 
121
// The default trivial text error handler.
122
class JpGraphErrObject {
123
 
124
    var $iTitle = "JpGraph Error";
125
    var $iDest = false;
126
 
127
    function JpGraphErrObject() {
128
	// Empty. Reserved for future use
129
    }
130
 
131
    function SetTitle($aTitle) {
132
	$this->iTitle = $aTitle;
133
    }
134
 
135
    function SetStrokeDest($aDest) {
136
	$this->iDest = $aDest;
137
    }
138
 
139
    // If aHalt is true then execution can't continue. Typical used for fatal errors.
140
    function Raise($aMsg,$aHalt=true) {
141
	$aMsg = $this->iTitle.' '.$aMsg."\n";
142
	if ($this->iDest) {
143
	    $f = @fopen($this->iDest,'a');
144
	    if( $f ) {
145
		@fwrite($f,$aMsg);
146
		@fclose($f);
147
	    }
148
	}
149
	else {
150
	    echo $aMsg;
151
	}
152
	if( $aHalt )
153
	    die();
154
    }
155
}
156
 
157
//==============================================================
158
// An image based error handler
159
//==============================================================
160
class JpGraphErrObjectImg extends JpGraphErrObject {
161
 
162
    function Raise($aMsg,$aHalt=true) {
163
	$img_iconerror =
164
	    'iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAMAAAC7IEhfAAAAaV'.
165
	    'BMVEX//////2Xy8mLl5V/Z2VvMzFi/v1WyslKlpU+ZmUyMjEh/'.
166
	    'f0VyckJlZT9YWDxMTDjAwMDy8sLl5bnY2K/MzKW/v5yyspKlpY'.
167
	    'iYmH+MjHY/PzV/f2xycmJlZVlZWU9MTEXY2Ms/PzwyMjLFTjea'.
168
	    'AAAAAXRSTlMAQObYZgAAAAFiS0dEAIgFHUgAAAAJcEhZcwAACx'.
169
	    'IAAAsSAdLdfvwAAAAHdElNRQfTBgISOCqusfs5AAABLUlEQVR4'.
170
	    '2tWV3XKCMBBGWfkranCIVClKLd/7P2Q3QsgCxjDTq+6FE2cPH+'.
171
	    'xJ0Ogn2lQbsT+Wrs+buAZAV4W5T6Bs0YXBBwpKgEuIu+JERAX6'.
172
	    'wM2rHjmDdEITmsQEEmWADgZm6rAjhXsoMGY9B/NZBwJzBvn+e3'.
173
	    'wHntCAJdGu9SviwIwoZVDxPB9+Rc0TSEbQr0j3SA1gwdSn6Db0'.
174
	    '6Tm1KfV6yzWGQO7zdpvyKLKBDmRFjzeB3LYgK7r6A/noDAfjtS'.
175
	    'IXaIzbJSv6WgUebTMV4EoRB8a2mQiQjgtF91HdKDKZ1gtFtQjk'.
176
	    'YcWaR5OKOhkYt+ZsTFdJRfPAApOpQYJTNHvCRSJR6SJngQadfc'.
177
	    'vd69OLMddVOPCGVnmrFD8bVYd3JXfxXPtLR/+mtv59/ALWiiMx'.
178
	    'qL72fwAAAABJRU5ErkJggg==' ;
179
 
180
	if( function_exists("imagetypes") )
181
	    $supported = imagetypes();
182
	else
183
	    $supported = 0;
184
 
185
	if( !function_exists('imagecreatefromstring') )
186
	    $supported = 0;
187
 
188
	if( ob_get_length() || headers_sent() || !($supported & IMG_PNG) ) {
189
	    // Special case for headers already sent or that the installation doesn't support
190
	    // the PNG format (which the error icon is encoded in).
191
	    // Dont return an image since it can't be displayed
192
	    die($this->iTitle.' '.$aMsg);
193
	}
194
 
195
	$aMsg = wordwrap($aMsg,55);
196
	$lines = substr_count($aMsg,"\n");
197
 
198
	// Create the error icon GD
199
	$erricon = Image::CreateFromString(base64_decode($img_iconerror));
200
 
201
	// Create an image that contains the error text.
202
	$w=400;
203
	$h=100 + 15*max(0,$lines-3);
204
 
205
	$img = new Image($w,$h);
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
// Install the default error handler in a global accessible variable
269
if( USE_IMAGE_ERROR_HANDLER ) {
270
    $__jpg_err = "JpGraphErrObjectImg";
271
}
272
else {
273
    $__jpg_err = "JpGraphErrObject";
274
}
275
 
276
 
277
?>