|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Exception; |
|
4
|
|
|
|
|
5
|
|
|
use Ffcms\Core\Helper\Type\Str; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class BanException. Work around throwed exception to display it |
|
9
|
|
|
*/ |
|
10
|
|
|
class BanException extends \Exception |
|
11
|
|
|
{ |
|
12
|
|
|
protected $message; |
|
13
|
|
|
protected $title; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* BanException constructor |
|
17
|
|
|
* @param string|null $message |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct($message = null, $title = null) |
|
20
|
|
|
{ |
|
21
|
|
|
if ($message !== null) { |
|
22
|
|
|
$this->message = $message; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if ($title !== null) { |
|
26
|
|
|
$this->title = $title; |
|
27
|
|
|
} |
|
28
|
|
|
parent::__construct(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Display native exception |
|
33
|
|
|
* @param string $message |
|
34
|
|
|
* @return string|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public function display($message = null) |
|
37
|
|
|
{ |
|
38
|
|
|
// if passed message is null get exception msg |
|
39
|
|
|
if (!$message) { |
|
40
|
|
|
$message = $this->message; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// hide root path from exception |
|
44
|
|
|
$message = Str::replace(root, '$DOCUMENT_ROOT', $message); |
|
|
|
|
|
|
45
|
|
|
$message = strip_tags($message); |
|
46
|
|
|
|
|
47
|
|
|
// generate response based on environment type |
|
48
|
|
|
switch (env_type) { |
|
|
|
|
|
|
49
|
|
|
case 'html': |
|
50
|
|
|
return $this->sendHTML($message, $this->title ?? 'Error'); |
|
51
|
|
|
case 'json': |
|
52
|
|
|
return $this->sendJSON($message, $this->title ?? 'Error'); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $message; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Build html response |
|
60
|
|
|
* @param string|null $message |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function sendHTML($message = null, $title = null) |
|
64
|
|
|
{ |
|
65
|
|
|
//header('HTTP/1.1 404 Not Found'); |
|
|
|
|
|
|
66
|
|
|
return '<!DOCTYPE html><html><head><title>'. $this->title . '</title></head><body><div style="width:60%; margin: auto; background-color: #fcc;border: 1px solid #faa; padding: 0.5em 1em;"><h1 style="font-size: 120%">' . $title . '</h1><p>' . $message . '</p></div></body></html>'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Build json response |
|
71
|
|
|
* @param string|null $message |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function sendJSON($message = null) |
|
75
|
|
|
{ |
|
76
|
|
|
header('Content-Type: application/json'); |
|
77
|
|
|
return json_encode(['status' => 0, 'message' => $message]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|