|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The controller class. |
|
5
|
|
|
* |
|
6
|
|
|
* The base controller for all other controllers. |
|
7
|
|
|
* Extend this for each created controller |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
10
|
|
|
* @author Omar El Gabry <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
class Controller { |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* view |
|
17
|
|
|
* |
|
18
|
|
|
* @var View |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $view; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* request |
|
24
|
|
|
* |
|
25
|
|
|
* @var Request |
|
26
|
|
|
*/ |
|
27
|
|
|
public $request; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* response |
|
31
|
|
|
* |
|
32
|
|
|
* @var Response |
|
33
|
|
|
*/ |
|
34
|
|
|
public $response; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* loaded components |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
public $components = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Constructor |
|
45
|
|
|
* |
|
46
|
|
|
* @param Request $request |
|
47
|
|
|
* @param Response $response |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(Request $request = null, Response $response = null){ |
|
50
|
|
|
|
|
51
|
|
|
$this->request = $request !== null ? $request : new Request(); |
|
52
|
|
|
$this->response = $response !== null ? $response : new Response(); |
|
53
|
|
|
$this->view = new View($this); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Perform the startup process for this controller. |
|
58
|
|
|
* Events that that will be triggered for each controller: |
|
59
|
|
|
* 1. load components |
|
60
|
|
|
* 2. perform any logic before calling controller's action(method) |
|
61
|
|
|
* 3. trigger startup method of loaded components |
|
62
|
|
|
* |
|
63
|
|
|
* @return void|Response |
|
64
|
|
|
*/ |
|
65
|
|
|
public function startupProcess(){ |
|
66
|
|
|
|
|
67
|
|
|
$this->initialize(); |
|
68
|
|
|
|
|
69
|
|
|
$this->beforeAction(); |
|
70
|
|
|
|
|
71
|
|
|
$result = $this->triggerComponents(); |
|
72
|
|
|
if($result instanceof Response){ |
|
73
|
|
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Initialization method. |
|
79
|
|
|
* initialize components and optionally, assign configuration data |
|
80
|
|
|
* |
|
81
|
|
|
*/ |
|
82
|
|
|
public function initialize(){ |
|
83
|
|
|
|
|
84
|
|
|
$this->loadComponents([ |
|
85
|
|
|
'Auth' => [ |
|
86
|
|
|
'authenticate' => ['User'], |
|
87
|
|
|
'authorize' => ['Controller'] |
|
88
|
|
|
], |
|
89
|
|
|
'Security' |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* load the components by setting the component's name to a controller's property. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $components |
|
97
|
|
|
*/ |
|
98
|
|
|
public function loadComponents(array $components){ |
|
99
|
|
|
|
|
100
|
|
|
if(!empty($components)){ |
|
101
|
|
|
|
|
102
|
|
|
$components = Utility::normalize($components); |
|
103
|
|
|
|
|
104
|
|
|
foreach($components as $component => $config){ |
|
105
|
|
|
|
|
106
|
|
|
if(!in_array($component, $this->components, true)){ |
|
107
|
|
|
$this->components[] = $component; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$class = $component . "Component"; |
|
111
|
|
|
$this->{$component} = empty($config)? new $class($this): new $class($this, $config); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* triggers component startup methods. |
|
118
|
|
|
* But, for auth, we are calling authentication and authorization separately |
|
119
|
|
|
* |
|
120
|
|
|
* You need to Fire the Components and Controller callbacks in the correct order, |
|
121
|
|
|
* For example, Authorization depends on form element, so you need to trigger Security first. |
|
122
|
|
|
* |
|
123
|
|
|
*/ |
|
124
|
|
|
private function triggerComponents(){ |
|
125
|
|
|
|
|
126
|
|
|
// You need to Fire the Components and Controller callbacks in the correct orde |
|
127
|
|
|
// For example, Authorization depends on form element, so you need to trigger Security first. |
|
128
|
|
|
|
|
129
|
|
|
// We supposed to execute startup() method of each component, |
|
130
|
|
|
// but since we need to call Auth -> authenticate, then Security, Auth -> authorize separately |
|
131
|
|
|
|
|
132
|
|
|
// re-construct components in right order |
|
133
|
|
|
$components = ['Auth', 'Security']; |
|
134
|
|
|
foreach($components as $key => $component){ |
|
135
|
|
|
if(!in_array($component, $this->components)){ |
|
136
|
|
|
unset($components[$key]); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$result = null; |
|
141
|
|
|
foreach($components as $component){ |
|
142
|
|
|
|
|
143
|
|
|
if($component === "Auth"){ |
|
144
|
|
|
|
|
145
|
|
|
$authenticate = $this->Auth->config("authenticate"); |
|
|
|
|
|
|
146
|
|
|
if(!empty($authenticate)){ |
|
147
|
|
|
if(!$this->Auth->authenticate()){ |
|
|
|
|
|
|
148
|
|
|
$result = $this->Auth->unauthenticated(); |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
// delay checking authorize till after the loop |
|
153
|
|
|
$authorize = $this->Auth->config("authorize"); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
}else{ |
|
156
|
|
|
$result = $this->{$component}->startup(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if($result instanceof Response){ return $result; } |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// authorize |
|
163
|
|
|
if(!empty($authorize)){ |
|
164
|
|
|
if(!$this->Auth->authorize()){ |
|
|
|
|
|
|
165
|
|
|
$result = $this->Auth->unauthorized(); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if($result instanceof Response){ return $result; } |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* show error page |
|
174
|
|
|
* |
|
175
|
|
|
* call error action method and set response status code |
|
176
|
|
|
* This will work as well for ajax call, see how ajax calls are handled in main.js |
|
177
|
|
|
* |
|
178
|
|
|
* @param int|string $code |
|
179
|
|
|
* |
|
180
|
|
|
*/ |
|
181
|
|
|
public function error($code){ |
|
182
|
|
|
|
|
183
|
|
|
$errors = [ |
|
184
|
|
|
404 => "notfound", |
|
185
|
|
|
401 => "unauthenticated", |
|
186
|
|
|
403 => "unauthorized", |
|
187
|
|
|
400 => "badrequest", |
|
188
|
|
|
500 => "system" |
|
189
|
|
|
]; |
|
190
|
|
|
|
|
191
|
|
|
if(!isset($errors[$code]) || !method_exists("ErrorsController", $errors[$code])){ |
|
192
|
|
|
$code = 500; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
$action = isset($errors[$code])? $errors[$code]: "System"; |
|
196
|
|
|
$this->response->setStatusCode($code); |
|
197
|
|
|
|
|
198
|
|
|
// clear, get page, then send headers |
|
199
|
|
|
$this->response->clearBuffer(); |
|
200
|
|
|
(new ErrorsController($this->request, $this->response))->{$action}(); |
|
201
|
|
|
|
|
202
|
|
|
return $this->response; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Called before the controller action. |
|
207
|
|
|
* Used to perform logic that needs to happen before each controller action. |
|
208
|
|
|
* |
|
209
|
|
|
*/ |
|
210
|
|
|
public function beforeAction(){} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Magic accessor for model autoloading. |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $name Property name |
|
216
|
|
|
* @return object The model instance |
|
217
|
|
|
*/ |
|
218
|
|
|
public function __get($name) { |
|
219
|
|
|
return $this->loadModel($name); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* load model |
|
224
|
|
|
* It assumes the model's constructor doesn't need parameters for constructor |
|
225
|
|
|
* |
|
226
|
|
|
* @param string $model class name |
|
227
|
|
|
*/ |
|
228
|
|
|
public function loadModel($model){ |
|
229
|
|
|
return $this->{$model} = new $model(); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* forces SSL request |
|
234
|
|
|
* |
|
235
|
|
|
* @see core/components/SecurityComponent::secureRequired() |
|
236
|
|
|
*/ |
|
237
|
|
|
public function forceSSL(){ |
|
238
|
|
|
$secured = "https://" . $this->request->currentUrl(); |
|
239
|
|
|
Redirector::to($secured); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.