1
|
<?php
|
2
|
/* SVN FILE: $Id: dispatcher.php 4409 2007-02-02 13:20:59Z phpnut $ */
|
3
|
/**
|
4
|
* Dispatcher takes the URL information, parses it for paramters and
|
5
|
* tells the involved controllers what to do.
|
6
|
*
|
7
|
* This is the heart of Cake's operation.
|
8
|
*
|
9
|
* PHP versions 4 and 5
|
10
|
*
|
11
|
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
|
12
|
* Copyright 2005-2007, Cake Software Foundation, Inc.
|
13
|
* 1785 E. Sahara Avenue, Suite 490-204
|
14
|
* Las Vegas, Nevada 89104
|
15
|
*
|
16
|
* Licensed under The MIT License
|
17
|
* Redistributions of files must retain the above copyright notice.
|
18
|
*
|
19
|
* @filesource
|
20
|
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
|
21
|
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
22
|
* @package cake
|
23
|
* @subpackage cake.cake
|
24
|
* @since CakePHP(tm) v 0.2.9
|
25
|
* @version $Revision: 4409 $
|
26
|
* @modifiedby $LastChangedBy: phpnut $
|
27
|
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
|
28
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
29
|
*/
|
30
|
/**
|
31
|
* List of helpers to include
|
32
|
*/
|
33
|
uses('router', DS.'controller'.DS.'controller');
|
34
|
/**
|
35
|
* Dispatcher translates URLs to controller-action-paramter triads.
|
36
|
*
|
37
|
* Dispatches the request, creating appropriate models and controllers.
|
38
|
*
|
39
|
* @package cake
|
40
|
* @subpackage cake.cake
|
41
|
*/
|
42
|
class Dispatcher extends Object {
|
43
|
/**
|
44
|
* Base URL
|
45
|
* @var string
|
46
|
*/
|
47
|
var $base = false;
|
48
|
/**
|
49
|
* @var string
|
50
|
*/
|
51
|
var $admin = false;
|
52
|
/**
|
53
|
* @var string
|
54
|
*/
|
55
|
var $webservices = null;
|
56
|
/**
|
57
|
* @var string
|
58
|
*/
|
59
|
var $plugin = null;
|
60
|
/**
|
61
|
* Constructor.
|
62
|
*/
|
63
|
function __construct() {
|
64
|
parent::__construct();
|
65
|
}
|
66
|
/**
|
67
|
* Dispatches and invokes given URL, handing over control to the involved controllers, and then renders the results (if autoRender is set).
|
68
|
*
|
69
|
* If no controller of given name can be found, invoke() shows error messages in
|
70
|
* the form of Missing Controllers information. It does the same with Actions (methods of Controllers are called
|
71
|
* Actions).
|
72
|
*
|
73
|
* @param string $url URL information to work on.
|
74
|
* @param array $additionalParams Settings array ("bare", "return"),
|
75
|
* which is melded with the GET and POST params.
|
76
|
* @return boolean Success
|
77
|
*/
|
78
|
function dispatch($url, $additionalParams=array()) {
|
79
|
$params = array_merge($this->parseParams($url), $additionalParams);
|
80
|
$missingController = false;
|
81
|
$missingAction = false;
|
82
|
$missingView = false;
|
83
|
$privateAction = false;
|
84
|
$this->base = $this->baseUrl();
|
85
|
|
86
|
if (empty($params['controller'])) {
|
87
|
$missingController = true;
|
88
|
} else {
|
89
|
$ctrlName = Inflector::camelize($params['controller']);
|
90
|
$ctrlClass = $ctrlName.'Controller';
|
91
|
|
92
|
if (!loadController($ctrlName)) {
|
93
|
$pluginName = Inflector::camelize($params['action']);
|
94
|
if (!loadPluginController(Inflector::underscore($ctrlName), $pluginName)) {
|
95
|
if(preg_match('/([\\.]+)/', $ctrlName)) {
|
96
|
return $this->cakeError('error404', array(
|
97
|
array('url' => strtolower($ctrlName),
|
98
|
'message' => 'Was not found on this server',
|
99
|
'base' => $this->base)));
|
100
|
} elseif(!class_exists($ctrlClass)) {
|
101
|
$missingController = true;
|
102
|
} else {
|
103
|
$params['plugin'] = null;
|
104
|
$this->plugin = null;
|
105
|
}
|
106
|
} else {
|
107
|
$params['plugin'] = Inflector::underscore($ctrlName);
|
108
|
}
|
109
|
} else {
|
110
|
$params['plugin'] = null;
|
111
|
$this->plugin = null;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
if(isset($params['plugin'])){
|
116
|
$plugin = $params['plugin'];
|
117
|
$pluginName = Inflector::camelize($params['action']);
|
118
|
$pluginClass = $pluginName.'Controller';
|
119
|
$ctrlClass = $pluginClass;
|
120
|
$oldAction = $params['action'];
|
121
|
$params = $this->_restructureParams($params);
|
122
|
$this->plugin = $plugin;
|
123
|
loadPluginModels($plugin);
|
124
|
$this->base = $this->base.'/'.Inflector::underscore($ctrlName);
|
125
|
|
126
|
if(empty($params['controller']) || !class_exists($pluginClass)) {
|
127
|
$params['controller'] = Inflector::underscore($ctrlName);
|
128
|
$ctrlClass = $ctrlName.'Controller';
|
129
|
if (!is_null($params['action'])) {
|
130
|
array_unshift($params['pass'], $params['action']);
|
131
|
}
|
132
|
$params['action'] = $oldAction;
|
133
|
}
|
134
|
}
|
135
|
|
136
|
if(defined('CAKE_ADMIN')) {
|
137
|
if(isset($params[CAKE_ADMIN])) {
|
138
|
$this->admin = '/'.CAKE_ADMIN ;
|
139
|
$url = preg_replace('/'.CAKE_ADMIN.'\//', '', $url);
|
140
|
|
141
|
if (empty($params['action'])) {
|
142
|
$params['action'] = CAKE_ADMIN.'_'.'index';
|
143
|
} else {
|
144
|
$params['action'] = CAKE_ADMIN.'_'.$params['action'];
|
145
|
}
|
146
|
} elseif (strpos($params['action'], CAKE_ADMIN) === 0) {
|
147
|
$privateAction = true;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
if ($missingController) {
|
152
|
return $this->cakeError('missingController', array(
|
153
|
array('className' => Inflector::camelize($params['controller']."Controller"),
|
154
|
'webroot' => $this->webroot,
|
155
|
'url' => $url,
|
156
|
'base' => $this->base)));
|
157
|
} else {
|
158
|
$controller =& new $ctrlClass();
|
159
|
}
|
160
|
|
161
|
$classMethods = get_class_methods($controller);
|
162
|
$classVars = get_object_vars($controller);
|
163
|
|
164
|
if (empty($params['action'])) {
|
165
|
$params['action'] = 'index';
|
166
|
}
|
167
|
|
168
|
if((in_array($params['action'], $classMethods) || in_array(strtolower($params['action']), $classMethods)) && strpos($params['action'], '_', 0) === 0) {
|
169
|
$privateAction = true;
|
170
|
}
|
171
|
|
172
|
if(!in_array($params['action'], $classMethods) && !in_array(strtolower($params['action']), $classMethods)) {
|
173
|
$missingAction = true;
|
174
|
}
|
175
|
|
176
|
if (in_array(strtolower($params['action']), array('tostring', 'requestaction', 'log',
|
177
|
'cakeerror', 'constructclasses', 'redirect',
|
178
|
'set', 'setaction', 'validate', 'validateerrors',
|
179
|
'render', 'referer', 'flash', 'flashout',
|
180
|
'generatefieldnames', 'postconditions', 'cleanupfields',
|
181
|
'beforefilter', 'beforerender', 'afterfilter'))) {
|
182
|
$missingAction = true;
|
183
|
}
|
184
|
|
185
|
if(in_array('return', array_keys($params)) && $params['return'] == 1) {
|
186
|
$controller->autoRender = false;
|
187
|
}
|
188
|
|
189
|
$controller->base = $this->base;
|
190
|
$base = strip_plugin($this->base, $this->plugin);
|
191
|
if(defined("BASE_URL")){
|
192
|
$controller->here = $base . $this->admin . $url;
|
193
|
} else {
|
194
|
$controller->here = $base . $this->admin . '/' . $url;
|
195
|
}
|
196
|
$controller->webroot = $this->webroot;
|
197
|
$controller->params = $params;
|
198
|
$controller->action = $params['action'];
|
199
|
|
200
|
if (!empty($controller->params['data'])) {
|
201
|
$controller->data =& $controller->params['data'];
|
202
|
} else {
|
203
|
$controller->data = null;
|
204
|
}
|
205
|
|
206
|
if (!empty($controller->params['pass'])) {
|
207
|
$controller->passed_args =& $controller->params['pass'];
|
208
|
$controller->passedArgs =& $controller->params['pass'];
|
209
|
} else {
|
210
|
$controller->passed_args = null;
|
211
|
$controller->passedArgs = null;
|
212
|
}
|
213
|
|
214
|
if (!empty($params['bare'])) {
|
215
|
$controller->autoLayout = !$params['bare'];
|
216
|
} else {
|
217
|
$controller->autoLayout = $controller->autoLayout;
|
218
|
}
|
219
|
|
220
|
$controller->webservices = $params['webservices'];
|
221
|
$controller->plugin = $this->plugin;
|
222
|
|
223
|
if(!is_null($controller->webservices)) {
|
224
|
array_push($controller->components, $controller->webservices);
|
225
|
array_push($controller->helpers, $controller->webservices);
|
226
|
$component =& new Component($controller);
|
227
|
}
|
228
|
$controller->_initComponents();
|
229
|
$controller->constructClasses();
|
230
|
|
231
|
if ($missingAction && !in_array('scaffold', array_keys($classVars))) {
|
232
|
$this->start($controller);
|
233
|
return $this->cakeError('missingAction', array(
|
234
|
array('className' => Inflector::camelize($params['controller']."Controller"),
|
235
|
'action' => $params['action'],
|
236
|
'webroot' => $this->webroot,
|
237
|
'url' => $url,
|
238
|
'base' => $this->base)));
|
239
|
}
|
240
|
|
241
|
if ($privateAction) {
|
242
|
$this->start($controller);
|
243
|
return $this->cakeError('privateAction', array(
|
244
|
array('className' => Inflector::camelize($params['controller']."Controller"),
|
245
|
'action' => $params['action'],
|
246
|
'webroot' => $this->webroot,
|
247
|
'url' => $url,
|
248
|
'base' => $this->base)));
|
249
|
}
|
250
|
return $this->_invoke($controller, $params, $missingAction);
|
251
|
}
|
252
|
/**
|
253
|
* Invokes given controller's render action if autoRender option is set. Otherwise the contents of the operation are returned as a string.
|
254
|
*
|
255
|
* @param object $controller
|
256
|
* @param array $params
|
257
|
* @param boolean $missingAction
|
258
|
* @return string
|
259
|
*/
|
260
|
function _invoke (&$controller, $params, $missingAction = false) {
|
261
|
$this->start($controller);
|
262
|
$classVars = get_object_vars($controller);
|
263
|
|
264
|
if ($missingAction && in_array('scaffold', array_keys($classVars))) {
|
265
|
uses(DS.'controller'.DS.'scaffold');
|
266
|
return new Scaffold($controller, $params);
|
267
|
} else {
|
268
|
$output = call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? null: $params['pass']);
|
269
|
}
|
270
|
if ($controller->autoRender) {
|
271
|
$output = $controller->render();
|
272
|
}
|
273
|
$controller->output =& $output;
|
274
|
$controller->afterFilter();
|
275
|
return $controller->output;
|
276
|
}
|
277
|
/**
|
278
|
* Starts up a controller
|
279
|
*
|
280
|
* @param object $controller
|
281
|
*/
|
282
|
function start(&$controller) {
|
283
|
if (!empty($controller->beforeFilter)) {
|
284
|
if(is_array($controller->beforeFilter)) {
|
285
|
|
286
|
foreach($controller->beforeFilter as $filter) {
|
287
|
if(is_callable(array($controller,$filter)) && $filter != 'beforeFilter') {
|
288
|
$controller->$filter();
|
289
|
}
|
290
|
}
|
291
|
} else {
|
292
|
if(is_callable(array($controller, $controller->beforeFilter)) && $controller->beforeFilter != 'beforeFilter') {
|
293
|
$controller->{$controller->beforeFilter}();
|
294
|
}
|
295
|
}
|
296
|
}
|
297
|
$controller->beforeFilter();
|
298
|
|
299
|
foreach($controller->components as $c) {
|
300
|
if (isset($controller->{$c}) && is_object($controller->{$c}) && is_callable(array($controller->{$c}, 'startup'))) {
|
301
|
$controller->{$c}->startup($controller);
|
302
|
}
|
303
|
}
|
304
|
}
|
305
|
|
306
|
/**
|
307
|
* Returns array of GET and POST parameters. GET parameters are taken from given URL.
|
308
|
*
|
309
|
* @param string $from_url URL to mine for parameter information.
|
310
|
* @return array Parameters found in POST and GET.
|
311
|
*/
|
312
|
function parseParams($from_url) {
|
313
|
$Route = new Router();
|
314
|
include CONFIGS.'routes.php';
|
315
|
$params = $Route->parse ($from_url);
|
316
|
|
317
|
if (ini_get('magic_quotes_gpc') == 1) {
|
318
|
if(!empty($_POST)) {
|
319
|
$params['form'] = stripslashes_deep($_POST);
|
320
|
}
|
321
|
} else {
|
322
|
$params['form'] = $_POST;
|
323
|
}
|
324
|
|
325
|
if (isset($params['form']['data'])) {
|
326
|
$params['data'] = $params['form']['data'];
|
327
|
}
|
328
|
|
329
|
if (isset($_GET)) {
|
330
|
if (ini_get('magic_quotes_gpc') == 1) {
|
331
|
$params['url'] = stripslashes_deep($_GET);
|
332
|
} else {
|
333
|
$params['url'] = $_GET;
|
334
|
}
|
335
|
}
|
336
|
|
337
|
foreach ($_FILES as $name => $data) {
|
338
|
if ($name != 'data') {
|
339
|
$params['form'][$name] = $data;
|
340
|
}
|
341
|
}
|
342
|
|
343
|
if (isset($_FILES['data'])) {
|
344
|
foreach ($_FILES['data'] as $key => $data) {
|
345
|
|
346
|
foreach ($data as $model => $fields) {
|
347
|
|
348
|
foreach ($fields as $field => $value) {
|
349
|
$params['data'][$model][$field][$key] = $value;
|
350
|
}
|
351
|
}
|
352
|
}
|
353
|
}
|
354
|
$params['bare'] = empty($params['ajax'])? (empty($params['bare'])? 0: 1): 1;
|
355
|
$params['webservices'] = empty($params['webservices']) ? null : $params['webservices'];
|
356
|
return $params;
|
357
|
}
|
358
|
/**
|
359
|
* Returns a base URL.
|
360
|
*
|
361
|
* @return string Base URL
|
362
|
*/
|
363
|
function baseUrl() {
|
364
|
$htaccess = null;
|
365
|
$base = $this->admin;
|
366
|
$this->webroot = '';
|
367
|
|
368
|
if (defined('BASE_URL')) {
|
369
|
$base = BASE_URL.$this->admin;
|
370
|
}
|
371
|
|
372
|
$docRoot = env('DOCUMENT_ROOT');
|
373
|
$scriptName = env('PHP_SELF');
|
374
|
$r = null;
|
375
|
$appDirName = str_replace('/', '\/', preg_quote(APP_DIR));
|
376
|
$webrootDirName = str_replace('/', '\/', preg_quote(WEBROOT_DIR));
|
377
|
|
378
|
if (preg_match('/'.$appDirName.'\\'.DS.$webrootDirName.'/', $docRoot)) {
|
379
|
$this->webroot = '/';
|
380
|
|
381
|
if (preg_match('/^(.*)\/index\.php$/', $scriptName, $r)) {
|
382
|
|
383
|
if(!empty($r[1])) {
|
384
|
return $base.$r[1];
|
385
|
}
|
386
|
}
|
387
|
} else {
|
388
|
if (defined('BASE_URL')) {
|
389
|
$webroot = setUri();
|
390
|
$htaccess = preg_replace('/(?:'.APP_DIR.'(.*)|index\\.php(.*))/i', '', $webroot).APP_DIR.'/'.$webrootDirName.'/';
|
391
|
}
|
392
|
|
393
|
if (preg_match('/^(.*)\\/'.$appDirName.'\\/'.$webrootDirName.'\\/index\\.php$/', $scriptName, $regs)) {
|
394
|
|
395
|
if(APP_DIR === 'app') {
|
396
|
$appDir = null;
|
397
|
} else {
|
398
|
$appDir = '/'.APP_DIR;
|
399
|
}
|
400
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].$appDir.'/';
|
401
|
return $base.$regs[1].$appDir;
|
402
|
|
403
|
} elseif (preg_match('/^(.*)\\/'.$webrootDirName.'([^\/i]*)|index\\\.php$/', $scriptName, $regs)) {
|
404
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[0].'/';
|
405
|
return $base.$regs[0];
|
406
|
|
407
|
} else {
|
408
|
!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
|
409
|
return $base;
|
410
|
}
|
411
|
}
|
412
|
return $base;
|
413
|
}
|
414
|
/**
|
415
|
* Enter description here...
|
416
|
*
|
417
|
* @param unknown_type $params
|
418
|
* @return unknown
|
419
|
*/
|
420
|
function _restructureParams($params) {
|
421
|
$params['controller'] = $params['action'];
|
422
|
|
423
|
if(isset($params['pass'][0])) {
|
424
|
$params['action'] = $params['pass'][0];
|
425
|
array_shift($params['pass']);
|
426
|
} else {
|
427
|
$params['action'] = null;
|
428
|
}
|
429
|
return $params;
|
430
|
}
|
431
|
}
|
432
|
?>
|