| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- # idea: serve (tools) package files on the fly...
- class MyAsset {
- protected $pluginPaths = array();
- protected $url;
- protected $parseData;
- /**
- * MyAsset::js()
- *
- * @param boolean $setHeaders
- * @return string Script tags.
- */
- public function js($setHeaders = true) {
- $this->_init();
- $res = $this->_parse($_SERVER['QUERY_STRING'], 'js');
- if ($setHeaders) {
- $this->_headers('js');
- }
- return $this->_get($res, 'js');
- }
- /**
- * MyAsset::css()
- *
- * @param boolean $setHeaders
- * @return string Style tags.
- */
- public function css($setHeaders = true) {
- $this->_init();
- $res = $this->_parse($_SERVER['QUERY_STRING'], 'css');
- if ($setHeaders) {
- $this->_headers('css');
- }
- return $this->_get($res, 'css');
- }
- /**
- * MyAsset::_headers()
- *
- * @param string $type
- * @return void
- */
- public function _headers($type) {
- if ($type === 'js') {
- $type = 'text/javascript'; //'application/x-javascript';
- } elseif ($type === 'css') {
- $type = 'text/css';
- }
- header("Date: " . date("D, j M Y G:i:s ", time()) . 'GMT');
- header("Content-Type: ".$type.'; charset=utf-8');
- header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
- header("Cache-Control: max-age=".DAY.", must-revalidate"); // HTTP/1.1
- header("Pragma: cache"); // HTTP/1.0
- }
- /**
- * searches, combines, packs, caches and returns scripts
- * 2011-03-23 ms
- */
- public function _get($assets, $type = null) {
- $script = array();
- foreach ($assets as $plugin => $packages) {
- foreach ($packages as $package => $packageFiles) {
- $path = $this->_path($plugin, $package, $type);
- foreach ($packageFiles as $file) {
- $script[] = $this->_read($path . $file);
- }
- }
- }
- $script = implode(PHP_EOL, $script);
- return $script;
- }
- public function _init() {
- if (empty($_SERVER['QUERY_STRING'])) {
- header('HTTP/1.1 404 Not Found');
- exit('File Not Found');
- }
- Configure::write('debug', 0);
- }
- /**
- * get the content of a single file
- * 2011-03-23 ms
- */
- public function _read($path) {
- $path = str_replace('/', DS, $path);
- if (strpos($path, '..') !== false) {
- trigger_error('MyAsset: Invalid file ('.$path.') ['.$this->url.']');
- return '';
- }
- if (!file_exists($path)) {
- trigger_error('MyAsset: File not exists ('.$path.') ['.$this->url.']');
- return '';
- }
- $data = file_get_contents($path);
- //TODO: compress?
- return $data;
- }
- /**
- * @deprecated?
- * 2011-03-23 ms
- */
- public function _makeCleanCss($path, $name, $pack = false) {
- $data = file_get_contents($path);
- if (!$pack) {
- $output = " /* file: $name, uncompressed */ " . $data;
- return $output;
- }
- if (!isset($this->Css)) {
- App::import('Vendor', 'csspp' . DS . 'csspp');
- $this->Css = new csspp();
- }
- $output = $this->Css->compress($data);
- $ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100);
- $output = " /* file: $name, ratio: $ratio% */ " . $output;
- return $output;
- }
- /**
- * @return string $result or bool FALSE on failure
- * 2011-03-23 ms
- */
- public function _readCssCache($path) {
- if (file_exists($path)) {
- return file_get_contents($path);
- }
- return false;
- }
- /**
- * @return boolean $result
- * @deprecated?
- * 2011-03-23 ms
- */
- public function _writeCssCache($path, $content) {
- if (!is_dir(dirname($path))) {
- if (!mkdir(dirname($path), 0755, true)) {
- trigger_error('MyAsset: Cannot create cache folder');
- return false;
- }
- }
- $cache = new File($path);
- return $cache->write($content);
- }
- /**
- * Get correct path of asset file.
- *
- * - PluginName.Asset => webroot/asset/ dir in plugin (new)
- * - App.Webroot => webroot/ dir
- * - App => packages
- * - Root => packages
- * - PluginName => packages in plugin
- *
- * @return string Path or bool false on failure.
- * 2011-03-23 ms
- */
- public function _path($plugin, $package, $type = null) {
- if ($type !== 'js' && $type !== 'css') {
- return false;
- }
- $pluginPathKey = $plugin;
- if ($package === 'Asset') {
- $pluginPathKey .= $package;
- }
- if (isset($this->pluginPaths[$pluginPathKey])) {
- return $this->pluginPaths[$pluginPathKey];
- }
- if ($plugin === 'App' && $package === 'Webroot') {
- $pluginPath = WWW_ROOT . $type . DS;
- } elseif ($plugin === 'App') {
- $pluginPath = APP . 'packages' . DS;
- } elseif ($plugin === 'Root') {
- $pluginPath = ROOT . DS . 'packages' . DS;
- } elseif ($package === 'Asset') {
- $pluginPath = App::pluginPath($plugin) . 'webroot' . DS . 'asset' . DS;
- $plugin = $plugin . 'Asset';
- } else {
- $pluginPath = App::pluginPath($plugin) . 'packages' . DS;
- }
- if (!$pluginPath) {
- return false;
- }
- if ($package === 'Webroot' || $package === 'Asset') {
- $packagePath = '';
- } else {
- $packagePath = strtolower($package) . DS . 'files' . DS;
- }
- $this->pluginPaths[$pluginPathKey] = $pluginPath;
- return $this->pluginPaths[$pluginPathKey] . $packagePath;
- }
- /**
- * url (example): file=x & file=Tools|y & file=Tools.Jquery|jquery/sub/z
- * => x is in webroot/
- * => y is in plugins/tools/webroot/
- * => z is in plugins/tools/packages/jquery/files/jquery/sub/
- * 2011-03-23 ms
- */
- public function _parse($string, $type = null) {
- $parts = explode('&', urldecode($string));
- $res = array();
- foreach ($parts as $part) {
- if (preg_match('|\.\.|', $part)) {
- trigger_error('MyAsset: Invalid piece ('.$part.')');
- continue;
- }
- $plugin = 'App';
- $package = 'Webroot';
- list($key, $content) = explode('=', $part, 2);
- if ($key !== 'file') {
- continue;
- }
- if (strpos($content, '|') !== false) {
- list($plugin, $content) = explode('|', $content, 2);
- if (strpos($plugin, '.') !== false) {
- list($plugin, $package) = explode('.', $plugin, 2);
- }
- }
- if ($type === 'js') {
- if (substr($content, -3) !== '.js') {
- $content .= '.js';
- }
- } elseif ($type === 'css') {
- if (substr($content, -4) !== '.css') {
- $content .= '.css';
- }
- }
- $res[$plugin][$package][] = $content;
- }
- $this->url = $string;
- $this->parseData = $res;
- return $res;
- }
- }
- /*
- if (preg_match('|\.\.|', $url) || !preg_match('|^ccss/(.+)$|i', $url, $regs)) {
- die('Wrong file name.');
- }
- $filename = 'css/' . $regs[1];
- $filepath = CSS . $regs[1];
- $cachepath = CACHE . 'css' . DS . str_replace(array('/','\\'), '-', $regs[1]);
- if (!file_exists($filepath)) {
- die('Wrong file name.');
- }
- if (file_exists($cachepath)) {
- $templateModified = filemtime($filepath);
- $cacheModified = filemtime($cachepath);
- if ($templateModified > $cacheModified) {
- $output = make_clean_css($filepath, $filename);
- write_css_cache($cachepath, $output);
- } else {
- $output = file_get_contents($cachepath);
- }
- } else {
- $output = make_clean_css($filepath, $filename);
- write_css_cache($cachepath, $output);
- $templateModified = time();
- }
- header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT');
- header("Content-Type: text/css");
- header("Expires: " . gmdate("D, d M Y H:i:s", time() + DAY) . " GMT");
- header("Cache-Control: max-age=86400, must-revalidate"); // HTTP/1.1
- header("Pragma: cache"); // HTTP/1.0
- print $output;
- */
|