|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Core\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Core\Services\Globals\Globals; |
|
6
|
|
|
|
|
7
|
|
|
class Controller |
|
8
|
|
|
{ |
|
9
|
|
|
protected string $viewPath; |
|
10
|
|
|
protected $template; |
|
11
|
|
|
|
|
12
|
|
|
public function __construct() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->GET = $this->accessGlobal('GET'); |
|
|
|
|
|
|
15
|
|
|
$this->POST = $this->accessGlobal('POST'); |
|
|
|
|
|
|
16
|
|
|
$this->SERVER = $this->accessGlobal('SERVER'); |
|
|
|
|
|
|
17
|
|
|
$this->SESSION = $this->accessGlobal('SESSION'); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Sends to the targeted View and sends the $variables |
|
22
|
|
|
* @param string $view ex: table.methode.attributs - 'post.show.1' |
|
23
|
|
|
* @param array $variables |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
|
protected function render(string $view, array $variables = []) |
|
27
|
|
|
{ |
|
28
|
|
|
\ob_start(); |
|
29
|
|
|
\extract($variables); |
|
30
|
|
|
require($this->viewPath . \str_replace('.', '/', $view) . '.php'); |
|
31
|
|
|
$content = \ob_get_clean(); |
|
32
|
|
|
require($this->viewPath . 'templates/' . $this->template . '.php'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the name of the table associated with the controller |
|
37
|
|
|
* @return string ex : 'Post' |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function tableName(): string |
|
40
|
|
|
{ |
|
41
|
|
|
\preg_match('/[\w\-\_]+(?=Controller)/im', \get_called_class(), $tableName); |
|
42
|
|
|
return $tableName[0]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Return to the previous page |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function previousPage() |
|
50
|
|
|
{ |
|
51
|
|
|
if (isset($this->GET['return'])) { |
|
52
|
|
|
return \header('Location: index.php?p=' . $this->GET['return']); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
return \header('Location: index.php'); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Format a date at the Local stardard |
|
59
|
|
|
* @param string $date |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function formatDate(string $date): string |
|
63
|
|
|
{ |
|
64
|
|
|
setlocale(LC_TIME, 'fr', 'fr_FR', 'fr_FR.ISO8859-1'); |
|
65
|
|
|
|
|
66
|
|
|
$date = \strtotime($date); |
|
67
|
|
|
$day = strftime("%d ", $date); |
|
68
|
|
|
$day .= ucfirst(strftime("%B ", $date)); |
|
69
|
|
|
return $day .= strftime("%G", $date); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Management in the event of an error HTTP 403 |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function forbidden() |
|
77
|
|
|
{ |
|
78
|
|
|
header("HTTP/1.0 403 Forbidden"); |
|
79
|
|
|
return \header('Location: index.php?p=user.login'); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Management in the event of an error 404 |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function notFound() |
|
87
|
|
|
{ |
|
88
|
|
|
header("HTTP/1.0 404 Not Found"); |
|
89
|
|
|
return \header('Location: index.php'); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add an XSS filter on the displayed datas |
|
94
|
|
|
* @param string $input |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function antiXss(string $input): string |
|
98
|
|
|
{ |
|
99
|
|
|
return \nl2br(\htmlspecialchars($input), ENT_QUOTES); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Secure get access to superglobals |
|
104
|
|
|
* @param string $global name of the superglobal targeted |
|
105
|
|
|
* @return mixed |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function accessGlobal(string $global) |
|
108
|
|
|
{ |
|
109
|
|
|
$globals = new Globals; |
|
110
|
|
|
$global = 'get' . $global; |
|
111
|
|
|
return $globals->$global(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|