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