CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

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

Generated using CakePHP API Docs