Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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