1 <?php
2 /**
3 * @package Joomla.Legacy
4 * @subpackage Exception
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Joomla! Exception object.
14 *
15 * @since 1.5
16 * @deprecated 1.7
17 */
18 class JException extends Exception
19 {
20 /**
21 * Error level.
22 *
23 * @var string
24 * @since 1.5
25 * @deprecated 1.7
26 */
27 protected $level = null;
28
29 /**
30 * Error code.
31 *
32 * @var string
33 * @since 1.5
34 * @deprecated 1.7
35 */
36 protected $code = null;
37
38 /**
39 * Error message.
40 *
41 * @var string
42 * @since 1.5
43 * @deprecated 1.7
44 */
45 protected $message = null;
46
47 /**
48 * Additional info about the error relevant to the developer,
49 * for example, if a database connect fails, the dsn used
50 *
51 * @var string
52 * @since 1.5
53 * @deprecated 1.7
54 */
55 protected $info = '';
56
57 /**
58 * Name of the file the error occurred in [Available if backtrace is enabled]
59 *
60 * @var string
61 * @since 1.5
62 * @deprecated 1.7
63 */
64 protected $file = null;
65
66 /**
67 * Line number the error occurred in [Available if backtrace is enabled]
68 *
69 * @var integer
70 * @since 1.5
71 * @deprecated 1.7
72 */
73 protected $line = 0;
74
75 /**
76 * Name of the method the error occurred in [Available if backtrace is enabled]
77 *
78 * @var string
79 * @since 1.5
80 * @deprecated 1.7
81 */
82 protected $function = null;
83
84 /**
85 * Name of the class the error occurred in [Available if backtrace is enabled]
86 *
87 * @var string
88 * @since 1.5
89 * @deprecated 1.7
90 */
91 protected $class = null;
92
93 /**
94 * @var string Error type.
95 * @since 1.5
96 * @deprecated 1.7
97 */
98 protected $type = null;
99
100 /**
101 * Arguments received by the method the error occurred in [Available if backtrace is enabled]
102 *
103 * @var array
104 * @since 1.5
105 * @deprecated 1.7
106 */
107 protected $args = array();
108
109 /**
110 * Backtrace information.
111 *
112 * @var mixed
113 * @since 1.5
114 * @deprecated 1.7
115 */
116 protected $backtrace = null;
117
118 /**
119 * Container holding the error messages
120 *
121 * @var string[]
122 * @since 1.6
123 * @deprecated 1.7
124 */
125 protected $_errors = array();
126
127 /**
128 * Constructor
129 * - used to set up the error with all needed error details.
130 *
131 * @param string $msg The error message
132 * @param integer $code The error code from the application
133 * @param integer $level The error level (use the PHP constants E_ALL, E_NOTICE etc.).
134 * @param string $info Optional: The additional error information.
135 * @param boolean $backtrace True if backtrace information is to be collected
136 *
137 * @since 1.5
138 * @deprecated 1.7
139 */
140 public function __construct($msg, $code = 0, $level = null, $info = null, $backtrace = false)
141 {
142 JLog::add('JException is deprecated.', JLog::WARNING, 'deprecated');
143
144 $this->level = $level;
145 $this->code = $code;
146 $this->message = $msg;
147
148 if ($info != null)
149 {
150 $this->info = $info;
151 }
152
153 if ($backtrace && function_exists('debug_backtrace'))
154 {
155 $this->backtrace = debug_backtrace();
156
157 for ($i = count($this->backtrace) - 1; $i >= 0; --$i)
158 {
159 ++$i;
160
161 if (isset($this->backtrace[$i]['file']))
162 {
163 $this->file = $this->backtrace[$i]['file'];
164 }
165
166 if (isset($this->backtrace[$i]['line']))
167 {
168 $this->line = $this->backtrace[$i]['line'];
169 }
170
171 if (isset($this->backtrace[$i]['class']))
172 {
173 $this->class = $this->backtrace[$i]['class'];
174 }
175
176 if (isset($this->backtrace[$i]['function']))
177 {
178 $this->function = $this->backtrace[$i]['function'];
179 }
180
181 if (isset($this->backtrace[$i]['type']))
182 {
183 $this->type = $this->backtrace[$i]['type'];
184 }
185
186 $this->args = false;
187
188 if (isset($this->backtrace[$i]['args']))
189 {
190 $this->args = $this->backtrace[$i]['args'];
191 }
192
193 break;
194 }
195 }
196
197 // Store exception for debugging purposes!
198 JError::addToStack($this);
199
200 parent::__construct($msg, (int) $code);
201 }
202
203 /**
204 * Returns to error message
205 *
206 * @return string Error message
207 *
208 * @since 1.6
209 * @deprecated 1.7
210 */
211 public function __toString()
212 {
213 JLog::add('JException::__toString is deprecated.', JLog::WARNING, 'deprecated');
214
215 return $this->message;
216 }
217
218 /**
219 * Returns to error message
220 *
221 * @return string Error message
222 *
223 * @since 1.5
224 * @deprecated 1.7
225 */
226 public function toString()
227 {
228 JLog::add('JException::toString is deprecated.', JLog::WARNING, 'deprecated');
229
230 return (string) $this;
231 }
232
233 /**
234 * Returns a property of the object or the default value if the property is not set.
235 *
236 * @param string $property The name of the property
237 * @param mixed $default The default value
238 *
239 * @return mixed The value of the property or null
240 *
241 * @since 1.6
242 * @deprecated 1.7
243 * @see JException::getProperties()
244 */
245 public function get($property, $default = null)
246 {
247 JLog::add('JException::get is deprecated.', JLog::WARNING, 'deprecated');
248
249 if (isset($this->$property))
250 {
251 return $this->$property;
252 }
253
254 return $default;
255 }
256
257 /**
258 * Returns an associative array of object properties
259 *
260 * @param boolean $public If true, returns only the public properties
261 *
262 * @return array Object properties
263 *
264 * @since 1.6
265 * @deprecated 1.7
266 * @see JException::get()
267 */
268 public function getProperties($public = true)
269 {
270 JLog::add('JException::getProperties is deprecated.', JLog::WARNING, 'deprecated');
271
272 $vars = get_object_vars($this);
273
274 if ($public)
275 {
276 foreach ($vars as $key => $value)
277 {
278 if ('_' == substr($key, 0, 1))
279 {
280 unset($vars[$key]);
281 }
282 }
283 }
284
285 return $vars;
286 }
287
288 /**
289 * Get the most recent error message
290 *
291 * @param integer $i Option error index
292 * @param boolean $toString Indicates if JError objects should return their error message
293 *
294 * @return string Error message
295 *
296 * @since 1.6
297 * @deprecated 1.7
298 */
299 public function getError($i = null, $toString = true)
300 {
301 JLog::add('JException::getError is deprecated.', JLog::WARNING, 'deprecated');
302
303 // Find the error
304 if ($i === null)
305 {
306 // Default, return the last message
307 $error = end($this->_errors);
308 }
309 elseif (!array_key_exists($i, $this->_errors))
310 {
311 // If $i has been specified but does not exist, return false
312 return false;
313 }
314 else
315 {
316 $error = $this->_errors[$i];
317 }
318
319 // Check if only the string is requested
320 if ($error instanceof Exception && $toString)
321 {
322 return (string) $error;
323 }
324
325 return $error;
326 }
327
328 /**
329 * Return all errors, if any
330 *
331 * @return array Array of error messages or JErrors
332 *
333 * @since 1.6
334 * @deprecated 1.7
335 */
336 public function getErrors()
337 {
338 JLog::add('JException::getErrors is deprecated.', JLog::WARNING, 'deprecated');
339
340 return $this->_errors;
341 }
342
343 /**
344 * Modifies a property of the object, creating it if it does not already exist.
345 *
346 * @param string $property The name of the property
347 * @param mixed $value The value of the property to set
348 *
349 * @return mixed Previous value of the property
350 *
351 * @since 1.6
352 * @deprecated 1.7
353 * @see JException::setProperties()
354 */
355 public function set($property, $value = null)
356 {
357 JLog::add('JException::set is deprecated.', JLog::WARNING, 'deprecated');
358
359 $previous = isset($this->$property) ? $this->$property : null;
360 $this->$property = $value;
361
362 return $previous;
363 }
364
365 /**
366 * Set the object properties based on a named array/hash
367 *
368 * @param mixed $properties Either and associative array or another object
369 *
370 * @return boolean
371 *
372 * @since 1.6
373 * @deprecated 1.7
374 * @see JException::set()
375 */
376 public function setProperties($properties)
377 {
378 JLog::add('JException::setProperties is deprecated.', JLog::WARNING, 'deprecated');
379
380 // Cast to an array
381 $properties = (array) $properties;
382
383 if (is_array($properties))
384 {
385 foreach ($properties as $k => $v)
386 {
387 $this->$k = $v;
388 }
389
390 return true;
391 }
392
393 return false;
394 }
395
396 /**
397 * Add an error message
398 *
399 * @param string $error Error message
400 *
401 * @return void
402 *
403 * @since 1.6
404 * @deprecated 1.7
405 */
406 public function setError($error)
407 {
408 JLog::add('JException::setErrors is deprecated.', JLog::WARNING, 'deprecated');
409
410 $this->_errors[] = $error;
411 }
412 }
413