Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright	  Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link		  http://cakephp.org CakePHP(tm) Project
12
 * @package		  Cake.Routing
13
 * @since		  CakePHP(tm) v 2.2
14
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
 
17
App::uses('DispatcherFilter', 'Routing');
18
 
19
/**
20
 * Filters a request and tests whether it is a file in the webroot folder or not and
21
 * serves the file to the client if appropriate.
22
 *
23
 * @package Cake.Routing.Filter
24
 */
25
class AssetDispatcher extends DispatcherFilter {
26
 
27
/**
28
 * Default priority for all methods in this filter
29
 * This filter should run before the request gets parsed by router
30
 *
31
 * @var int
32
 */
33
	public $priority = 9;
34
 
35
/**
36
 * Checks if a requested asset exists and sends it to the browser
37
 *
38
 * @param CakeEvent $event containing the request and response object
39
 * @return mixed The resulting response.
40
 * @throws NotFoundException When asset not found
41
 */
42
	public function beforeDispatch(CakeEvent $event) {
43
		$url = urldecode($event->data['request']->url);
44
		if (strpos($url, '..') !== false || strpos($url, '.') === false) {
45
			return;
46
		}
47
 
48
		if ($result = $this->_filterAsset($event)) {
49
			$event->stopPropagation();
50
			return $result;
51
		}
52
 
53
		$assetFile = $this->_getAssetFile($url);
54
		if ($assetFile === null || !file_exists($assetFile)) {
55
			return null;
56
		}
57
		$response = $event->data['response'];
58
		$event->stopPropagation();
59
 
60
		$response->modified(filemtime($assetFile));
61
		if ($response->checkNotModified($event->data['request'])) {
62
			return $response;
63
		}
64
 
65
		$pathSegments = explode('.', $url);
66
		$ext = array_pop($pathSegments);
67
 
68
		$this->_deliverAsset($response, $assetFile, $ext);
69
		return $response;
70
	}
71
 
72
/**
73
 * Checks if the client is requesting a filtered asset and runs the corresponding
74
 * filter if any is configured
75
 *
76
 * @param CakeEvent $event containing the request and response object
77
 * @return CakeResponse if the client is requesting a recognized asset, null otherwise
78
 */
79
	protected function _filterAsset(CakeEvent $event) {
80
		$url = $event->data['request']->url;
81
		$response = $event->data['response'];
82
		$filters = Configure::read('Asset.filter');
83
		$isCss = (
84
			strpos($url, 'ccss/') === 0 ||
85
			preg_match('#^(theme/([^/]+)/ccss/)|(([^/]+)(?<!css)/ccss)/#i', $url)
86
		);
87
		$isJs = (
88
			strpos($url, 'cjs/') === 0 ||
89
			preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url)
90
		);
91
 
92
		if (($isCss && empty($filters['css'])) || ($isJs && empty($filters['js']))) {
93
			$response->statusCode(404);
94
			return $response;
95
		}
96
 
97
		if ($isCss) {
98
			include WWW_ROOT . DS . $filters['css'];
99
			return $response;
100
		}
101
 
102
		if ($isJs) {
103
			include WWW_ROOT . DS . $filters['js'];
104
			return $response;
105
		}
106
	}
107
 
108
/**
109
 * Builds asset file path based off url
110
 *
111
 * @param string $url URL
112
 * @return string Absolute path for asset file
113
 */
114
	protected function _getAssetFile($url) {
115
		$parts = explode('/', $url);
116
		if ($parts[0] === 'theme') {
117
			$themeName = $parts[1];
118
			unset($parts[0], $parts[1]);
119
			$fileFragment = implode(DS, $parts);
120
			$path = App::themePath($themeName) . 'webroot' . DS;
121
			return $path . $fileFragment;
122
		}
123
 
124
		$plugin = Inflector::camelize($parts[0]);
125
		if ($plugin && CakePlugin::loaded($plugin)) {
126
			unset($parts[0]);
127
			$fileFragment = implode(DS, $parts);
128
			$pluginWebroot = CakePlugin::path($plugin) . 'webroot' . DS;
129
			return $pluginWebroot . $fileFragment;
130
		}
131
	}
132
 
133
/**
134
 * Sends an asset file to the client
135
 *
136
 * @param CakeResponse $response The response object to use.
137
 * @param string $assetFile Path to the asset file in the file system
138
 * @param string $ext The extension of the file to determine its mime type
139
 * @return void
140
 */
141
	protected function _deliverAsset(CakeResponse $response, $assetFile, $ext) {
142
		ob_start();
143
		$compressionEnabled = Configure::read('Asset.compress') && $response->compress();
144
		if ($response->type($ext) === $ext) {
145
			$contentType = 'application/octet-stream';
146
			$agent = env('HTTP_USER_AGENT');
147
			if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
148
				$contentType = 'application/octetstream';
149
			}
150
			$response->type($contentType);
151
		}
152
		$response->length(false);
153
		$response->cache(filemtime($assetFile));
154
		$response->send();
155
		ob_clean();
156
 
157
		if ($ext === 'css' || $ext === 'js') {
158
			include $assetFile;
159
		} else {
160
			readfile($assetFile);
161
		}
162
 
163
		if ($compressionEnabled) {
164
			ob_end_flush();
165
		}
166
	}
167
 
168
}