Projekt

Obecné

Profil

Stáhnout (12.6 KB) Statistiky
| Větev: | Tag: | Revize:
1
<?php
2
/* SVN FILE: $Id: dispatcher.php 5144 2007-05-21 04:46:30Z 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: 5144 $
26
 * @modifiedby		$LastChangedBy: phpnut $
27
 * @lastmodified	$Date: 2007-05-20 23:46:30 -0500 (Sun, 20 May 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 (empty($params['action'])) {
137
			$params['action'] = 'index';
138
		}
139

    
140
		if(defined('CAKE_ADMIN')) {
141
			if(isset($params[CAKE_ADMIN])) {
142
				$this->admin = '/'.CAKE_ADMIN ;
143
				$url = preg_replace('/'.CAKE_ADMIN.'\//', '', $url);
144
				$params['action'] = CAKE_ADMIN.'_'.$params['action'];
145
			} elseif (strpos($params['action'], CAKE_ADMIN) === 0) {
146
					$privateAction = true;
147
			}
148
		}
149

    
150
		if ($missingController) {
151
			return $this->cakeError('missingController', array(
152
											array('className' => Inflector::camelize($params['controller']."Controller"),
153
													'webroot' => $this->webroot,
154
													'url' => $url,
155
													'base' => $this->base)));
156
		} else {
157
			$controller =& new $ctrlClass();
158
		}
159

    
160
		$classMethods = get_class_methods($controller);
161
		$classVars = get_object_vars($controller);
162

    
163
		if((in_array($params['action'], $classMethods) || in_array(strtolower($params['action']), $classMethods)) && strpos($params['action'], '_', 0) === 0) {
164
			$privateAction = true;
165
		}
166

    
167
		if(!in_array($params['action'], $classMethods) && !in_array(strtolower($params['action']), $classMethods)) {
168
			$missingAction = true;
169
		}
170

    
171
		if (in_array(strtolower($params['action']), array('tostring', 'requestaction', 'log',
172
																			'cakeerror', 'constructclasses', 'redirect',
173
																			'set', 'setaction', 'validate', 'validateerrors',
174
																			'render', 'referer', 'flash', 'flashout',
175
																			'generatefieldnames', 'postconditions', 'cleanupfields',
176
																			'beforefilter', 'beforerender', 'afterfilter'))) {
177
			$missingAction = true;
178
		}
179

    
180
		if(in_array('return', array_keys($params)) && $params['return'] == 1) {
181
			$controller->autoRender = false;
182
		}
183

    
184
		$controller->base = $this->base;
185
		$base = strip_plugin($this->base, $this->plugin);
186
		if(defined("BASE_URL")){
187
			$controller->here = $base . $this->admin . $url;
188
		} else {
189
			$controller->here = $base . $this->admin . '/' . $url;
190
		}
191
		$controller->webroot = $this->webroot;
192
		$controller->params = $params;
193
		$controller->action = $params['action'];
194

    
195
		if (!empty($controller->params['data'])) {
196
			$controller->data =& $controller->params['data'];
197
		} else {
198
			$controller->data = null;
199
		}
200

    
201
		if (!empty($controller->params['pass'])) {
202
			$controller->passed_args =& $controller->params['pass'];
203
			$controller->passedArgs =&  $controller->params['pass'];
204
		} else {
205
			$controller->passed_args = null;
206
			$controller->passedArgs = null;
207
		}
208

    
209
		if (!empty($params['bare'])) {
210
			$controller->autoLayout = !$params['bare'];
211
		} else {
212
			$controller->autoLayout = $controller->autoLayout;
213
		}
214

    
215
		$controller->webservices = $params['webservices'];
216
		$controller->plugin = $this->plugin;
217

    
218
		if(!is_null($controller->webservices)) {
219
			array_push($controller->components, $controller->webservices);
220
			array_push($controller->helpers, $controller->webservices);
221
			$component =& new Component($controller);
222
		}
223
		$controller->_initComponents();
224
		$controller->constructClasses();
225

    
226
		if ($missingAction && !in_array('scaffold', array_keys($classVars))) {
227
			$this->start($controller);
228
			return $this->cakeError('missingAction', array(
229
											array('className' => Inflector::camelize($params['controller']."Controller"),
230
													'action' => $params['action'],
231
													'webroot' => $this->webroot,
232
													'url' => $url,
233
													'base' => $this->base)));
234
		}
235

    
236
		if ($privateAction) {
237
			$this->start($controller);
238
			return $this->cakeError('privateAction', array(
239
											array('className' => Inflector::camelize($params['controller']."Controller"),
240
													'action' => $params['action'],
241
													'webroot' => $this->webroot,
242
													'url' => $url,
243
													'base' => $this->base)));
244
		}
245
		return $this->_invoke($controller, $params, $missingAction);
246
	}
247
/**
248
 * Invokes given controller's render action if autoRender option is set. Otherwise the contents of the operation are returned as a string.
249
 *
250
 * @param object $controller
251
 * @param array $params
252
 * @param boolean $missingAction
253
 * @return string
254
 */
255
	function _invoke (&$controller, $params, $missingAction = false) {
256
		$this->start($controller);
257
		$classVars = get_object_vars($controller);
258

    
259
		if ($missingAction && in_array('scaffold', array_keys($classVars))) {
260
			uses(DS.'controller'.DS.'scaffold');
261
			return new Scaffold($controller, $params);
262
		} else {
263
			$output = call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? null: $params['pass']);
264
		}
265
		if ($controller->autoRender) {
266
			$output = $controller->render();
267
		}
268
		$controller->output =& $output;
269
		$controller->afterFilter();
270
		return $controller->output;
271
	}
272
/**
273
 * Starts up a controller
274
 *
275
 * @param object $controller
276
 */
277
	function start(&$controller) {
278
		if (!empty($controller->beforeFilter)) {
279
			if(is_array($controller->beforeFilter)) {
280

    
281
				foreach($controller->beforeFilter as $filter) {
282
					if(is_callable(array($controller,$filter)) && $filter != 'beforeFilter') {
283
						$controller->$filter();
284
					}
285
				}
286
			} else {
287
				if(is_callable(array($controller, $controller->beforeFilter)) && $controller->beforeFilter != 'beforeFilter') {
288
					$controller->{$controller->beforeFilter}();
289
				}
290
			}
291
		}
292
		$controller->beforeFilter();
293

    
294
		foreach($controller->components as $c) {
295
			$path = preg_split('/\/|\./', $c);
296
			$c = $path[count($path) - 1];
297
			if (isset($controller->{$c}) && is_object($controller->{$c}) && is_callable(array($controller->{$c}, 'startup'))) {
298
				$controller->{$c}->startup($controller);
299
			}
300
		}
301
	}
302

    
303
/**
304
 * Returns array of GET and POST parameters. GET parameters are taken from given URL.
305
 *
306
 * @param string $from_url	URL to mine for parameter information.
307
 * @return array Parameters found in POST and GET.
308
 */
309
	function parseParams($from_url) {
310
		$Route = new Router();
311
		include CONFIGS.'routes.php';
312
		$params = $Route->parse ($from_url);
313

    
314
		if (ini_get('magic_quotes_gpc') == 1) {
315
			if(!empty($_POST)) {
316
				$params['form'] = stripslashes_deep($_POST);
317
			}
318
		} else {
319
			$params['form'] = $_POST;
320
		}
321

    
322
		if (isset($params['form']['data'])) {
323
			$params['data'] = $Route->stripEscape($params['form']['data']);
324
		}
325

    
326
		if (isset($_GET)) {
327
			if (ini_get('magic_quotes_gpc') == 1) {
328
				$params['url'] = stripslashes_deep($_GET);
329
			} else {
330
				$params['url'] = $_GET;
331
			}
332
		}
333

    
334
		foreach ($_FILES as $name => $data) {
335
			if ($name != 'data') {
336
				$params['form'][$name] = $data;
337
			}
338
		}
339

    
340
		if (isset($_FILES['data'])) {
341
			foreach ($_FILES['data'] as $key => $data) {
342

    
343
				foreach ($data as $model => $fields) {
344

    
345
					foreach ($fields as $field => $value) {
346
						$params['data'][$model][$field][$key] = $value;
347
					}
348
				}
349
			}
350
		}
351
		$params['bare'] = empty($params['ajax'])? (empty($params['bare'])? 0: 1): 1;
352
		$params['webservices'] = empty($params['webservices']) ? null : $params['webservices'];
353
		return $params;
354
	}
355
/**
356
 * Returns a base URL.
357
 *
358
 * @return string	Base URL
359
 */
360
	function baseUrl() {
361
		$htaccess = null;
362
		$base = $this->admin;
363
		$this->webroot = '';
364

    
365
		if (defined('BASE_URL')) {
366
			$base = BASE_URL.$this->admin;
367
		}
368

    
369
		$docRoot = env('DOCUMENT_ROOT');
370
		$scriptName = env('PHP_SELF');
371
		$r = null;
372
		$appDirName = str_replace('/', '\/', preg_quote(APP_DIR));
373
		$webrootDirName = str_replace('/', '\/', preg_quote(WEBROOT_DIR));
374

    
375
		if (preg_match('/'.$appDirName.'\\'.DS.$webrootDirName.'/', $docRoot)) {
376
			$this->webroot = '/';
377

    
378
			if (preg_match('/^(.*)\/index\.php$/', $scriptName, $r)) {
379

    
380
				if(!empty($r[1])) {
381
					return  $base.$r[1];
382
				}
383
			}
384
		} else {
385
			if (defined('BASE_URL')) {
386
				$webroot = setUri();
387
				$htaccess = preg_replace('/(?:'.APP_DIR.'\\/(.*)|index\\.php(.*))/i', '', $webroot).APP_DIR.'/'.$webrootDirName.'/';
388
			}
389

    
390
			if (preg_match('/^(.*)\\/'.$appDirName.'\\/'.$webrootDirName.'\\/index\\.php$/', $scriptName, $regs)) {
391

    
392
				if(APP_DIR === 'app') {
393
					$appDir = null;
394
				} else {
395
					$appDir = '/'.APP_DIR;
396
				}
397
				!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[1].$appDir.'/';
398
				return  $base.$regs[1].$appDir;
399

    
400
			} elseif (preg_match('/^(.*)\\/'.$webrootDirName.'([^\/i]*)|index\\\.php$/', $scriptName, $regs)) {
401
				!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = $regs[0].'/';
402
				return  $base.$regs[0];
403

    
404
			} else {
405
				!empty($htaccess)? $this->webroot = $htaccess : $this->webroot = '/';
406
				return $base;
407
			}
408
		}
409
		return $base;
410
	}
411
/**
412
 * Enter description here...
413
 *
414
 * @param unknown_type $params
415
 * @return unknown
416
 */
417
	function _restructureParams($params) {
418
		$params['controller'] = $params['action'];
419

    
420
		if(isset($params['pass'][0])) {
421
			$params['action'] = $params['pass'][0];
422
			array_shift($params['pass']);
423
		} else {
424
			$params['action'] = null;
425
		}
426
		return $params;
427
	}
428
}
429
?>
(7-7/7)