-
Notifications
You must be signed in to change notification settings - Fork 58
/
Errors.class.php
395 lines (347 loc) · 9.05 KB
/
Errors.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* @version 1.0 Alpha
*
*/
if (!defined('ELK'))
die('Hacking attempt...');
/**
* This class is an experiment for the job of handling errors.
*/
class error_context
{
/**
* Holds the unique identifier of the error (a name).
*
* @var string
*/
private $_name = null;
/**
* An array that holds all the errors occurred separated by severity.
*
* @var array
*/
private $_errors = null;
/**
* The default severity code.
*
* @var mixed
*/
private $_default_severity = 0;
/**
* A list of all severity code from the less important to the most serious.
*
* @var array/mixed
*/
private $_severity_levels = array(0);
/**
* Certain errors may need some specific language file...
*
* @var array
*/
private $_language_files = array();
/**
* Multipleton. This is an array of instances of error_context.
* All callers use an error context ('post', 'attach', or 'default' if none chosen).
*
* @var array of error_context
*/
private static $_contexts = null;
const MINOR = 0;
const SERIOUS = 1;
/**
* Create and initialize an instance of the class
*
* @param string error identifier
* @param int the default error severity level
*/
private function __construct ($id = 'default', $default_severity = null)
{
if (!empty($id))
$this->_name = $id;
// initialize severity levels... waiting for details!
$this->_severity_levels = array(error_context::MINOR, error_context::SERIOUS);
// initialize default severity (not sure this is needed)
if ($default_severity === null || !in_array($default_severity, $this->_severity_levels))
$this->_default_severity = error_context::MINOR;
else
$this->_default_severity = $default_severity;
$this->_errors = array();
}
/**
* Add an error to the list
*
* @param string error code
* @param mixed error severity
* @param string lang_file
*/
public function addError($error, $severity = null, $lang_file = null)
{
$severity = $severity !== null && in_array($severity, $this->_severity_levels) ? $severity : $this->_default_severity;
if (!empty($error))
{
if (is_array($error))
$this->_errors[$severity][$error[0]] = $error;
else
$this->_errors[$severity][$error] = $error;
}
if (!empty($lang_file))
$this->_language_files[] = $lang_file;
}
/**
* Remove an error from the list
*
* @param string error code
*/
public function removeError($error)
{
if (!empty($error))
{
if (is_array($error))
$error = $error[0];
foreach ($this->_errors as $severity => $errors)
if (in_array($error, $errors))
unset($this->_errors[$severity][$error]);
}
}
/**
* Return an array of errors of a certain severity.
* @todo is it needed at all?
*
* @param string the severity level wanted. If null returns all the errors
*/
public function getErrors($severity = null)
{
if ($severity !== null && in_array($severity, $this->_severity_levels) && !empty($this->_errors[$severity]))
return $this->_errors[$severity];
elseif ($severity === null && !empty($this->_errors))
return $this->_errors;
else
return false;
}
/**
* Returns if there are errors or not.
*
* @param string the severity level wanted. If null returns all the errors
* @return bool
*/
public function hasErrors($severity = null)
{
if ($severity !== null && in_array($severity, $this->_severity_levels))
return !empty($this->_errors[$severity]);
elseif ($severity === null)
return !empty($this->_errors);
else
return false;
}
/**
* Check if a particular error exists.
*
* @param string the error
* @return bool
*/
public function hasError($errors)
{
if (empty($errors))
return false;
else
{
$errors = is_array($errors) ? $errors : array($errors);
foreach ($errors as $error)
foreach ($this->_errors as $current_errors)
if (isset($current_errors[$error]))
return true;
}
return false;
}
/**
* Return the code of the highest error level encountered
*
* @return int
*/
public function getErrorType()
{
$levels = array_reverse($this->_severity_levels);
foreach ($levels as $level)
if (!empty($this->_errors[$level]))
return $level;
return $level;
}
/**
* Return an array containing the error strings
* If severity is null the function returns all the errors
*
* @param string the severity level wanted
*/
public function prepareErrors($severity = null)
{
global $txt;
// Load the default error language and any other language file needed
// @todo: we could load these languages only if really necessary...it just needs a coupld of changes
loadLanguage('Errors');
if (!empty($this->_language_files))
foreach ($this->_language_files as $language)
loadLanguage($language);
if (empty($this->_errors))
return array();
call_integration_hook('integrate_' . $this->_name . '_errors', array(&$this->_errors, &$this->_severity_levels));
$errors = array();
$returns = array();
if ($severity === null)
{
foreach ($this->_errors as $err)
$errors = array_merge($errors, $err);
}
elseif (in_array($severity, $this->_severity_levels) && !empty($this->_errors[$severity]))
$errors = $this->_errors[$severity];
foreach ($errors as $error_val)
{
if (is_array($error_val))
$returns[$error_val[0]] = vsprintf(isset($txt['error_' . $error_val[0]]) ? $txt['error_' . $error_val[0]] : $error_val[0], $error_val[1]);
else
$returns[$error_val] = isset($txt['error_' . $error_val]) ? $txt['error_' . $error_val] : (isset($txt[$error_val]) ? $txt[$error_val] : $error_val);
}
return $returns;
}
/**
* Find and return error_context instance if it exists,
* or create a new instance for $id if it didn't already exist.
*
* @param string $id
* @param int $default_severity
*/
public static function context($id = 'default', $default_severity = null)
{
if (self::$_contexts === null)
self::$_contexts = array();
if (!array_key_exists($id, self::$_contexts))
self::$_contexts[$id] = new error_context($id, $default_severity);
return self::$_contexts[$id];
}
}
/**
* Error context for attachments
*
*/
class attachment_error_context
{
private static $_context = null;
private $_attachs = null;
private $_generic_error = null;
/**
* Add attachment
*
* @param string $id
* @param string $name
*/
public function addAttach($id, $name)
{
if (empty($id) || empty($name))
return false;
if (!isset($this->_attachs[$id]))
$this->_attachs[$id] = array(
'name' => $name,
'error' => error_context::context($id, 1),
);
return true;
}
/**
* Add an error
*
* @param string $error error code
* @param string $attachID = 'generic'
* @param string $lang_file = null
*/
public function addError($error, $attachID = 'generic', $lang_file = null)
{
if (empty($error))
return;
if ($attachID == 'generic')
{
if (!isset($this->_attachs[$attachID]))
$this->_generic_error = error_context::context('attach_generic_error', 1);
$this->_generic_error->addError($error, null, $lang_file);
return;
}
$this->_attachs[$attachID]['error']->addError($error, null, $lang_file);
}
/**
* If this error context has errors stored.
*
* @param string $attachID
* @param int $severity the severity level
*/
public function hasErrors($attachID = null, $severity = null)
{
if ($this->_generic_error !== null)
if ($this->_generic_error->hasErrors($severity))
return true;
if (!empty($this->_attachs))
{
if ($attachID !== null)
{
if (isset($this->_attachs[$attachID]))
return $this->_attachs[$attachID]['error']->hasErrors($severity);
}
else
{
foreach ($this->_attachs as $attach)
if ($attach['error']->hasErrors($severity))
return true;
}
}
return false;
}
/**
* Prepare the errors for display.
* Return an array containing the error strings
* If severity is null the function returns all the errors
*
* @param int = null the severity level wanted
* @return array
*/
public function prepareErrors($severity = null)
{
global $txt;
$returns = array();
if ($this->_generic_error !== null)
$returns['attach_generic'] = array(
'errors' => $this->_generic_error->prepareErrors($severity),
'type' => $this->getErrorType(),
'title' => $txt['attach_error_title'],
);
if (!empty($this->_attachs))
foreach ($this->_attachs as $attachID => $error)
$returns[$attachID] = array(
'errors' => $error['error']->prepareErrors($severity),
'type' => $this->getErrorType(),
'title' => sprintf($txt['attach_warning'], $error['name']),
);
return $returns;
}
/**
* Return the type of the error
*
* @return int
*/
public function getErrorType()
{
return 1;
}
/**
* Find and return attachment_error_context instance if it exists,
* or create it if it doesn't exist
*
* @return attachment_error_context
*/
public static function context()
{
if (self::$_context === null)
self::$_context = new attachment_error_context();
return self::$_context;
}
}