forked from elkarte/Elkarte
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Errors.class.php
453 lines (393 loc) · 10.2 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
<?php
/**
* This does the job of handling user errors in their many forms
*
* @name ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause
*
* @version 1.0 Beta 2
*
*/
if (!defined('ELK'))
die('No access...');
/**
* 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();
/**
* Multiton. 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 $id the error identifier
* @param int|null $default_severity 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[]|string $error error code
* @param string|int|null $severity error severity
* @param string|null $lang_file 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) && !isset($this->_language_files[$lang_file]))
$this->_language_files[$lang_file] = false;
}
/**
* Remove an error from the list
*
* @param string $error error code
*/
public function removeError($error)
{
if (!empty($error))
{
if (is_array($error))
$error = $error[0];
foreach ($this->_errors as $severity => $errors)
{
if (array_key_exists($error, $errors))
unset($this->_errors[$severity][$error]);
if (empty($this->_errors[$severity]))
unset($this->_errors[$severity]);
}
}
}
/**
* Return an array of errors of a certain severity.
* @todo is it needed at all?
*
* @param string|int|null $severity 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|null $severity 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 $errors the error
*/
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
*/
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|null $severity the severity level wanted
*/
public function prepareErrors($severity = null)
{
global $txt;
if (empty($this->_errors))
return array();
$this->_loadLang();
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]] : (isset($txt[$error_val[0]]) ? $txt[$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;
}
/**
* Load the default error language and any other language file needed
*/
private function _loadLang()
{
// Errors is always needed
loadLanguage('Errors');
// Any custom one?
if (!empty($this->_language_files))
foreach ($this->_language_files as $language => $loaded)
if (!$loaded)
{
loadLanguage($language);
// Remember this file has been loaded already
$this->_language_files[$language] = true;
}
}
/**
* 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|null $default_severity
* @return Error_Context
*/
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];
}
}
/**
* Class Error context for attachments
*/
class attachment_error_context
{
/**
* Holds our static instance of the class
* @var object
*/
private static $_context = null;
/**
* Holds all of the attachment ids
* @var array
*/
private $_attachs = null;
/**
* Holds any errors found
* @var array
*/
private $_generic_error = null;
/**
* Holds if the error is generic of specific to an attachment
* @var string
*/
private $_active_attach = null;
/**
* Add attachment
* Automatically activate the attachments added
*
* @param string $id
* @param string $name
*/
public function addAttach($id, $name)
{
if (empty($id) || empty($name))
{
$this->activate();
return false;
}
if (!isset($this->_attachs[$id]))
$this->_attachs[$id] = array(
'name' => $name,
'error' => Error_Context::context($id, 1),
);
$this->activate($id);
return true;
}
/**
* Sets the active attach (errors are "attached" to that)
*
* @param int $id A valid attachment, if invalid it defaults to 'generic'
*/
public function activate($id = null)
{
if (empty($id) || isset($this->_attachs[$id]))
$this->_active_attach = 'generic';
else
$this->_active_attach = $id;
return $this;
}
/**
* Add an error
*
* @param string $error error code
* @param string $lang_file = null
*/
public function addError($error, $lang_file = null)
{
if (empty($error))
return;
if ($this->_active_attach == 'generic')
{
if (!isset($this->_attachs[$this->_active_attach]))
$this->_generic_error = Error_Context::context('attach_generic_error', 1);
$this->_generic_error->addError($error, $lang_file);
return;
}
$this->_attachs[$this->_active_attach]['error']->addError($error, $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 $severity = null the severity level wanted
*/
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
*/
public function getErrorType()
{
return 1;
}
/**
* Find and return attachment_error_context instance if it exists,
* or create it if it doesn't exist
*/
public static function context()
{
if (self::$_context === null)
self::$_context = new attachment_error_context();
return self::$_context;
}
}