PageRenderTime 36ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/mcmis/reportico/jpgraph/src/jpgraph_errhandler.inc.php

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