commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / PEAR / Exception.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3 /**
4 * PEAR_Exception
5 *
6 * PHP versions 4 and 5
7 *
8 * @category pear
9 * @package PEAR
10 * @author Tomas V. V. Cox <cox@idecnet.com>
11 * @author Hans Lellelid <hans@velum.net>
12 * @author Bertrand Mansion <bmansion@mamasam.com>
13 * @author Greg Beaver <cellog@php.net>
14 * @copyright 1997-2009 The Authors
15 * @license http://opensource.org/licenses/bsd-license.php New BSD License
16 * @version CVS: $Id: Exception.php 276383 2009-02-24 23:39:37Z dufuz $
17 * @link http://pear.php.net/package/PEAR
18 * @since File available since Release 1.3.3
19 */
20
21
22 /**
23 * Base PEAR_Exception Class
24 *
25 * 1) Features:
26 *
27 * - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception))
28 * - Definable triggers, shot when exceptions occur
29 * - Pretty and informative error messages
30 * - Added more context info available (like class, method or cause)
31 * - cause can be a PEAR_Exception or an array of mixed
32 * PEAR_Exceptions/PEAR_ErrorStack warnings
33 * - callbacks for specific exception classes and their children
34 *
35 * 2) Ideas:
36 *
37 * - Maybe a way to define a 'template' for the output
38 *
39 * 3) Inherited properties from PHP Exception Class:
40 *
41 * protected $message
42 * protected $code
43 * protected $line
44 * protected $file
45 * private $trace
46 *
47 * 4) Inherited methods from PHP Exception Class:
48 *
49 * __clone
50 * __construct
51 * getMessage
52 * getCode
53 * getFile
54 * getLine
55 * getTraceSafe
56 * getTraceSafeAsString
57 * __toString
58 *
59 * 5) Usage example
60 *
61 * <code>
62 * require_once 'PEAR/Exception.php';
63 *
64 * class Test {
65 * function foo() {
66 * throw new PEAR_Exception('Error Message', ERROR_CODE);
67 * }
68 * }
69 *
70 * function myLogger($pear_exception) {
71 * echo $pear_exception->getMessage();
72 * }
73 * // each time a exception is thrown the 'myLogger' will be called
74 * // (its use is completely optional)
75 * PEAR_Exception::addObserver('myLogger');
76 * $test = new Test;
77 * try {
78 * $test->foo();
79 * } catch (PEAR_Exception $e) {
80 * print $e;
81 * }
82 * </code>
83 *
84 * @category pear
85 * @package PEAR
86 * @author Tomas V.V.Cox <cox@idecnet.com>
87 * @author Hans Lellelid <hans@velum.net>
88 * @author Bertrand Mansion <bmansion@mamasam.com>
89 * @author Greg Beaver <cellog@php.net>
90 * @copyright 1997-2009 The Authors
91 * @license http://opensource.org/licenses/bsd-license.php New BSD License
92 * @version Release: 1.9.0
93 * @link http://pear.php.net/package/PEAR
94 * @since Class available since Release 1.3.3
95 *
96 */
97
98 // CRM-8921
99 if ( ! class_exists( 'PEAR_Exception' ) ) {
100
101 class PEAR_Exception extends Exception
102 {
103 const OBSERVER_PRINT = -2;
104 const OBSERVER_TRIGGER = -4;
105 const OBSERVER_DIE = -8;
106 protected $cause;
107 private static $_observers = array();
108 private static $_uniqueid = 0;
109 private $_trace;
110
111 /**
112 * Supported signatures:
113 * - PEAR_Exception(string $message);
114 * - PEAR_Exception(string $message, int $code);
115 * - PEAR_Exception(string $message, Exception $cause);
116 * - PEAR_Exception(string $message, Exception $cause, int $code);
117 * - PEAR_Exception(string $message, PEAR_Error $cause);
118 * - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
119 * - PEAR_Exception(string $message, array $causes);
120 * - PEAR_Exception(string $message, array $causes, int $code);
121 * @param string exception message
122 * @param int|Exception|PEAR_Error|array|null exception cause
123 * @param int|null exception code or null
124 */
125 public function __construct($message, $p2 = null, $p3 = null)
126 {
127 if (is_int($p2)) {
128 $code = $p2;
129 $this->cause = null;
130 } elseif (is_object($p2) || is_array($p2)) {
131 // using is_object allows both Exception and PEAR_Error
132 if (is_object($p2) && !($p2 instanceof Exception)) {
133 if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
134 throw new PEAR_Exception('exception cause must be Exception, ' .
135 'array, or PEAR_Error');
136 }
137 }
138 $code = $p3;
139 if (is_array($p2) && isset($p2['message'])) {
140 // fix potential problem of passing in a single warning
141 $p2 = array($p2);
142 }
143 $this->cause = $p2;
144 } else {
145 $code = null;
146 $this->cause = null;
147 }
148 parent::__construct($message, $code);
149 $this->signal();
150 }
151
152 /**
153 * @param mixed $callback - A valid php callback, see php func is_callable()
154 * - A PEAR_Exception::OBSERVER_* constant
155 * - An array(const PEAR_Exception::OBSERVER_*,
156 * mixed $options)
157 * @param string $label The name of the observer. Use this if you want
158 * to remove it later with removeObserver()
159 */
160 public static function addObserver($callback, $label = 'default')
161 {
162 self::$_observers[$label] = $callback;
163 }
164
165 public static function removeObserver($label = 'default')
166 {
167 unset(self::$_observers[$label]);
168 }
169
170 /**
171 * @return int unique identifier for an observer
172 */
173 public static function getUniqueId()
174 {
175 return self::$_uniqueid++;
176 }
177
178 private function signal()
179 {
180 foreach (self::$_observers as $func) {
181 if (is_callable($func)) {
182 call_user_func($func, $this);
183 continue;
184 }
185 settype($func, 'array');
186 switch ($func[0]) {
187 case self::OBSERVER_PRINT :
188 $f = (isset($func[1])) ? $func[1] : '%s';
189 printf($f, $this->getMessage());
190 break;
191 case self::OBSERVER_TRIGGER :
192 $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
193 trigger_error($this->getMessage(), $f);
194 break;
195 case self::OBSERVER_DIE :
196 $f = (isset($func[1])) ? $func[1] : '%s';
197 die(printf($f, $this->getMessage()));
198 break;
199 default:
200 trigger_error('invalid observer type', E_USER_WARNING);
201 }
202 }
203 }
204
205 /**
206 * Return specific error information that can be used for more detailed
207 * error messages or translation.
208 *
209 * This method may be overridden in child exception classes in order
210 * to add functionality not present in PEAR_Exception and is a placeholder
211 * to define API
212 *
213 * The returned array must be an associative array of parameter => value like so:
214 * <pre>
215 * array('name' => $name, 'context' => array(...))
216 * </pre>
217 * @return array
218 */
219 public function getErrorData()
220 {
221 return array();
222 }
223
224 /**
225 * Returns the exception that caused this exception to be thrown
226 * @access public
227 * @return Exception|array The context of the exception
228 */
229 public function getCause()
230 {
231 return $this->cause;
232 }
233
234 /**
235 * Function must be public to call on caused exceptions
236 * @param array
237 */
238 public function getCauseMessage(&$causes)
239 {
240 $trace = $this->getTraceSafe();
241 $cause = array('class' => get_class($this),
242 'message' => $this->message,
243 'file' => 'unknown',
244 'line' => 'unknown');
245 if (isset($trace[0])) {
246 if (isset($trace[0]['file'])) {
247 $cause['file'] = $trace[0]['file'];
248 $cause['line'] = $trace[0]['line'];
249 }
250 }
251 $causes[] = $cause;
252 if ($this->cause instanceof PEAR_Exception) {
253 $this->cause->getCauseMessage($causes);
254 } elseif ($this->cause instanceof Exception) {
255 $causes[] = array('class' => get_class($this->cause),
256 'message' => $this->cause->getMessage(),
257 'file' => $this->cause->getFile(),
258 'line' => $this->cause->getLine());
259 } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
260 $causes[] = array('class' => get_class($this->cause),
261 'message' => $this->cause->getMessage(),
262 'file' => 'unknown',
263 'line' => 'unknown');
264 } elseif (is_array($this->cause)) {
265 foreach ($this->cause as $cause) {
266 if ($cause instanceof PEAR_Exception) {
267 $cause->getCauseMessage($causes);
268 } elseif ($cause instanceof Exception) {
269 $causes[] = array('class' => get_class($cause),
270 'message' => $cause->getMessage(),
271 'file' => $cause->getFile(),
272 'line' => $cause->getLine());
273 } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) {
274 $causes[] = array('class' => get_class($cause),
275 'message' => $cause->getMessage(),
276 'file' => 'unknown',
277 'line' => 'unknown');
278 } elseif (is_array($cause) && isset($cause['message'])) {
279 // PEAR_ErrorStack warning
280 $causes[] = array(
281 'class' => $cause['package'],
282 'message' => $cause['message'],
283 'file' => isset($cause['context']['file']) ?
284 $cause['context']['file'] :
285 'unknown',
286 'line' => isset($cause['context']['line']) ?
287 $cause['context']['line'] :
288 'unknown',
289 );
290 }
291 }
292 }
293 }
294
295 public function getTraceSafe()
296 {
297 if (!isset($this->_trace)) {
298 $this->_trace = $this->getTrace();
299 if (empty($this->_trace)) {
300 $backtrace = debug_backtrace();
301 $this->_trace = array($backtrace[count($backtrace)-1]);
302 }
303 }
304 return $this->_trace;
305 }
306
307 public function getErrorClass()
308 {
309 $trace = $this->getTraceSafe();
310 return $trace[0]['class'];
311 }
312
313 public function getErrorMethod()
314 {
315 $trace = $this->getTraceSafe();
316 return $trace[0]['function'];
317 }
318
319 public function __toString()
320 {
321 if (isset($_SERVER['REQUEST_URI'])) {
322 return $this->toHtml();
323 }
324 return $this->toText();
325 }
326
327 public function toHtml()
328 {
329 $trace = $this->getTraceSafe();
330 $causes = array();
331 $this->getCauseMessage($causes);
332 $html = '<table border="1" cellspacing="0">' . "\n";
333 foreach ($causes as $i => $cause) {
334 $html .= '<tr><td colspan="3" bgcolor="#ff9999">'
335 . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
336 . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> '
337 . 'on line <b>' . $cause['line'] . '</b>'
338 . "</td></tr>\n";
339 }
340 $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n"
341 . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>'
342 . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>'
343 . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
344
345 foreach ($trace as $k => $v) {
346 $html .= '<tr><td align="center">' . $k . '</td>'
347 . '<td>';
348 if (!empty($v['class'])) {
349 $html .= $v['class'] . $v['type'];
350 }
351 $html .= $v['function'];
352 $args = array();
353 if (!empty($v['args'])) {
354 foreach ($v['args'] as $arg) {
355 if (is_null($arg)) $args[] = 'null';
356 elseif (is_array($arg)) $args[] = 'Array';
357 elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')';
358 elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
359 elseif (is_int($arg) || is_double($arg)) $args[] = $arg;
360 else {
361 $arg = (string)$arg;
362 $str = htmlspecialchars(substr($arg, 0, 16));
363 if (strlen($arg) > 16) $str .= '&hellip;';
364 $args[] = "'" . $str . "'";
365 }
366 }
367 }
368 $html .= '(' . implode(', ',$args) . ')'
369 . '</td>'
370 . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
371 . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
372 . '</td></tr>' . "\n";
373 }
374 $html .= '<tr><td align="center">' . ($k+1) . '</td>'
375 . '<td>{main}</td>'
376 . '<td>&nbsp;</td></tr>' . "\n"
377 . '</table>';
378 return $html;
379 }
380
381 public function toText()
382 {
383 $causes = array();
384 $this->getCauseMessage($causes);
385 $causeMsg = '';
386 foreach ($causes as $i => $cause) {
387 $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
388 . $cause['message'] . ' in ' . $cause['file']
389 . ' on line ' . $cause['line'] . "\n";
390 }
391 return $causeMsg . $this->getTraceAsString();
392 }
393 }
394
395 }
396
397 ?>