MyAsset.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. # idea: serve (tools) package files on the fly...
  3. class MyAsset {
  4. protected $pluginPaths = array();
  5. protected $url;
  6. protected $parseData;
  7. /**
  8. * MyAsset::js()
  9. *
  10. * @param boolean $setHeaders
  11. * @return string Script tags.
  12. */
  13. public function js($setHeaders = true) {
  14. $this->_init();
  15. $res = $this->_parse($_SERVER['QUERY_STRING'], 'js');
  16. if ($setHeaders) {
  17. $this->_headers('js');
  18. }
  19. return $this->_get($res, 'js');
  20. }
  21. /**
  22. * MyAsset::css()
  23. *
  24. * @param boolean $setHeaders
  25. * @return string Style tags.
  26. */
  27. public function css($setHeaders = true) {
  28. $this->_init();
  29. $res = $this->_parse($_SERVER['QUERY_STRING'], 'css');
  30. if ($setHeaders) {
  31. $this->_headers('css');
  32. }
  33. return $this->_get($res, 'css');
  34. }
  35. /**
  36. * MyAsset::_headers()
  37. *
  38. * @param string $type
  39. * @return void
  40. */
  41. public function _headers($type) {
  42. if ($type === 'js') {
  43. $type = 'text/javascript'; //'application/x-javascript';
  44. } elseif ($type === 'css') {
  45. $type = 'text/css';
  46. }
  47. header("Date: " . date("D, j M Y G:i:s ", time()) . 'GMT');
  48. header("Content-Type: ".$type.'; charset=utf-8');
  49. header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT");
  50. header("Cache-Control: max-age=".DAY.", must-revalidate"); // HTTP/1.1
  51. header("Pragma: cache"); // HTTP/1.0
  52. }
  53. /**
  54. * searches, combines, packs, caches and returns scripts
  55. * 2011-03-23 ms
  56. */
  57. public function _get($assets, $type = null) {
  58. $script = array();
  59. foreach ($assets as $plugin => $packages) {
  60. foreach ($packages as $package => $packageFiles) {
  61. $path = $this->_path($plugin, $package, $type);
  62. foreach ($packageFiles as $file) {
  63. $script[] = $this->_read($path . $file);
  64. }
  65. }
  66. }
  67. $script = implode(PHP_EOL, $script);
  68. return $script;
  69. }
  70. public function _init() {
  71. if (empty($_SERVER['QUERY_STRING'])) {
  72. header('HTTP/1.1 404 Not Found');
  73. exit('File Not Found');
  74. }
  75. Configure::write('debug', 0);
  76. }
  77. /**
  78. * get the content of a single file
  79. * 2011-03-23 ms
  80. */
  81. public function _read($path) {
  82. $path = str_replace('/', DS, $path);
  83. if (strpos($path, '..') !== false) {
  84. trigger_error('MyAsset: Invalid file ('.$path.') ['.$this->url.']');
  85. return '';
  86. }
  87. if (!file_exists($path)) {
  88. trigger_error('MyAsset: File not exists ('.$path.') ['.$this->url.']');
  89. return '';
  90. }
  91. $data = file_get_contents($path);
  92. //TODO: compress?
  93. return $data;
  94. }
  95. /**
  96. * @deprecated?
  97. * 2011-03-23 ms
  98. */
  99. public function _makeCleanCss($path, $name, $pack = false) {
  100. $data = file_get_contents($path);
  101. if (!$pack) {
  102. $output = " /* file: $name, uncompressed */ " . $data;
  103. return $output;
  104. }
  105. if (!isset($this->Css)) {
  106. App::import('Vendor', 'csspp' . DS . 'csspp');
  107. $this->Css = new csspp();
  108. }
  109. $output = $this->Css->compress($data);
  110. $ratio = 100 - (round(strlen($output) / strlen($data), 3) * 100);
  111. $output = " /* file: $name, ratio: $ratio% */ " . $output;
  112. return $output;
  113. }
  114. /**
  115. * @return string $result or bool FALSE on failure
  116. * 2011-03-23 ms
  117. */
  118. public function _readCssCache($path) {
  119. if (file_exists($path)) {
  120. return file_get_contents($path);
  121. }
  122. return false;
  123. }
  124. /**
  125. * @return boolean $result
  126. * @deprecated?
  127. * 2011-03-23 ms
  128. */
  129. public function _writeCssCache($path, $content) {
  130. if (!is_dir(dirname($path))) {
  131. if (!mkdir(dirname($path), 0755, true)) {
  132. trigger_error('MyAsset: Cannot create cache folder');
  133. return false;
  134. }
  135. }
  136. $cache = new File($path);
  137. return $cache->write($content);
  138. }
  139. /**
  140. * Get correct path of asset file.
  141. *
  142. * - PluginName.Asset => webroot/asset/ dir in plugin (new)
  143. * - App.Webroot => webroot/ dir
  144. * - App => packages
  145. * - Root => packages
  146. * - PluginName => packages in plugin
  147. *
  148. * @return string Path or bool false on failure.
  149. * 2011-03-23 ms
  150. */
  151. public function _path($plugin, $package, $type = null) {
  152. if ($type !== 'js' && $type !== 'css') {
  153. return false;
  154. }
  155. $pluginPathKey = $plugin;
  156. if ($package === 'Asset') {
  157. $pluginPathKey .= $package;
  158. }
  159. if (isset($this->pluginPaths[$pluginPathKey])) {
  160. return $this->pluginPaths[$pluginPathKey];
  161. }
  162. if ($plugin === 'App' && $package === 'Webroot') {
  163. $pluginPath = WWW_ROOT . $type . DS;
  164. } elseif ($plugin === 'App') {
  165. $pluginPath = APP . 'packages' . DS;
  166. } elseif ($plugin === 'Root') {
  167. $pluginPath = ROOT . DS . 'packages' . DS;
  168. } elseif ($package === 'Asset') {
  169. $pluginPath = App::pluginPath($plugin) . 'webroot' . DS . 'asset' . DS;
  170. $plugin = $plugin . 'Asset';
  171. } else {
  172. $pluginPath = App::pluginPath($plugin) . 'packages' . DS;
  173. }
  174. if (!$pluginPath) {
  175. return false;
  176. }
  177. if ($package === 'Webroot' || $package === 'Asset') {
  178. $packagePath = '';
  179. } else {
  180. $packagePath = strtolower($package) . DS . 'files' . DS;
  181. }
  182. $this->pluginPaths[$pluginPathKey] = $pluginPath;
  183. return $this->pluginPaths[$pluginPathKey] . $packagePath;
  184. }
  185. /**
  186. * url (example): file=x & file=Tools|y & file=Tools.Jquery|jquery/sub/z
  187. * => x is in webroot/
  188. * => y is in plugins/tools/webroot/
  189. * => z is in plugins/tools/packages/jquery/files/jquery/sub/
  190. * 2011-03-23 ms
  191. */
  192. public function _parse($string, $type = null) {
  193. $parts = explode('&', urldecode($string));
  194. $res = array();
  195. foreach ($parts as $part) {
  196. if (preg_match('|\.\.|', $part)) {
  197. trigger_error('MyAsset: Invalid piece ('.$part.')');
  198. continue;
  199. }
  200. $plugin = 'App';
  201. $package = 'Webroot';
  202. list($key, $content) = explode('=', $part, 2);
  203. if ($key !== 'file') {
  204. continue;
  205. }
  206. if (strpos($content, '|') !== false) {
  207. list($plugin, $content) = explode('|', $content, 2);
  208. if (strpos($plugin, '.') !== false) {
  209. list($plugin, $package) = explode('.', $plugin, 2);
  210. }
  211. }
  212. if ($type === 'js') {
  213. if (substr($content, -3) !== '.js') {
  214. $content .= '.js';
  215. }
  216. } elseif ($type === 'css') {
  217. if (substr($content, -4) !== '.css') {
  218. $content .= '.css';
  219. }
  220. }
  221. $res[$plugin][$package][] = $content;
  222. }
  223. $this->url = $string;
  224. $this->parseData = $res;
  225. return $res;
  226. }
  227. }
  228. /*
  229. if (preg_match('|\.\.|', $url) || !preg_match('|^ccss/(.+)$|i', $url, $regs)) {
  230. die('Wrong file name.');
  231. }
  232. $filename = 'css/' . $regs[1];
  233. $filepath = CSS . $regs[1];
  234. $cachepath = CACHE . 'css' . DS . str_replace(array('/','\\'), '-', $regs[1]);
  235. if (!file_exists($filepath)) {
  236. die('Wrong file name.');
  237. }
  238. if (file_exists($cachepath)) {
  239. $templateModified = filemtime($filepath);
  240. $cacheModified = filemtime($cachepath);
  241. if ($templateModified > $cacheModified) {
  242. $output = make_clean_css($filepath, $filename);
  243. write_css_cache($cachepath, $output);
  244. } else {
  245. $output = file_get_contents($cachepath);
  246. }
  247. } else {
  248. $output = make_clean_css($filepath, $filename);
  249. write_css_cache($cachepath, $output);
  250. $templateModified = time();
  251. }
  252. header("Date: " . date("D, j M Y G:i:s ", $templateModified) . 'GMT');
  253. header("Content-Type: text/css");
  254. header("Expires: " . gmdate("D, d M Y H:i:s", time() + DAY) . " GMT");
  255. header("Cache-Control: max-age=86400, must-revalidate"); // HTTP/1.1
  256. header("Pragma: cache"); // HTTP/1.0
  257. print $output;
  258. */