PageRenderTime 74ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/gantry/core/gantrygzipper.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 253 lines | 192 code | 37 blank | 24 comment | 29 complexity | 253ea0681ac2df931c55355a941aada1 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * @package gantry
  4. * @subpackage core
  5. * @version 3.2.22 August 3, 2012
  6. * @author RocketTheme http://www.rockettheme.com
  7. * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
  8. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  9. *
  10. * Gantry uses the Joomla Framework (http://www.joomla.org), a GNU/GPLv2 content management system
  11. *
  12. */
  13. defined('GANTRY_VERSION') or die();
  14. gantry_import('core.gantrysingleton');
  15. /**
  16. * @package gantry
  17. * @subpackage core
  18. */
  19. class GantryGZipper {
  20. function process() {
  21. }
  22. function processCSSFiles() {
  23. global $gantry;
  24. $cache_time = $gantry->get("gzipper-time");
  25. $expires_time = $gantry->get("gzipper-expirestime", 1440);
  26. $strip_css = $gantry->get("gzipper-stripwhitespace", 1);
  27. $grouped_priories = array();
  28. $output = array();
  29. foreach ($gantry->_styles as $priorities) {
  30. foreach ($priorities as $links) {
  31. $css_links[$links->path] = $links->url;
  32. }
  33. }
  34. // $css_links = $gantry->_styles;
  35. ksort($gantry->_styles);
  36. foreach ($gantry->_styles as $style_priority => $styles) {
  37. $order_keeper = 0;
  38. $bump_ok = false;
  39. foreach ($styles as $style_entry) {
  40. if ($style_entry->type == 'url') {
  41. $directory = 'REMOTE_URL';
  42. $filename = $style_entry->url;
  43. $bump_ok = true;
  44. }
  45. else {
  46. $directory = dirname($style_entry->path);
  47. $filename = basename($style_entry->path);
  48. }
  49. $grouped_priories[$style_priority][$order_keeper][$directory][$filename] = $style_entry;
  50. if ($bump_ok) {
  51. $order_keeper++;
  52. $bump_ok = false;
  53. }
  54. }
  55. }
  56. foreach ($grouped_priories as $priority => $order_kept_entries) {
  57. foreach ($order_kept_entries as $ordered_files) {
  58. foreach ($ordered_files as $dir => $files) {
  59. // Process full urls
  60. if ($dir == 'REMOTE_URL') {
  61. foreach ($files as $file => $link) {
  62. $gantry->document->addStyleSheet($link->url);
  63. }
  64. continue;
  65. }
  66. // Process
  67. else {
  68. if (!is_writable($dir)) {
  69. foreach ($files as $css_file) {
  70. $gantry->document->addStyleSheet($css_file->url);
  71. }
  72. continue;
  73. }
  74. $md5sum = "";
  75. $path = "";
  76. jimport('joomla.filesystem.file');
  77. //first trip through to build filename
  78. foreach ($files as $file => $details) {
  79. $md5sum .= md5($details->url);
  80. $detailspath = $dir . DS . $file;
  81. if (JFile::exists($detailspath)) {
  82. $path = dirname($details->url);
  83. }
  84. }
  85. $cache_filename = "css-" . md5($md5sum) . ".php";
  86. $cache_fullpath = $dir . DS . $cache_filename;
  87. //see if file is stale
  88. if (JFile::exists($cache_fullpath)) {
  89. $diff = (time() - filectime($cache_fullpath));
  90. } else {
  91. $diff = $cache_time + 1;
  92. }
  93. if ($diff > $cache_time) {
  94. $outfile = GantryGZipper::_getOutHeader("css", $expires_time);
  95. foreach ($files as $file => $details) {
  96. $detailspath = $dir . DS . $file;
  97. if (JFile::exists($detailspath)) {
  98. $css_content = JFile::read($detailspath);
  99. if ($strip_css) {
  100. $css_content = GantryGZipper::_stripCSSWhiteSpace($css_content);
  101. }
  102. $outfile .= "\n\n/*** " . $file . " ***/\n\n" . $css_content;
  103. }
  104. }
  105. JFile::write($cache_fullpath, $outfile);
  106. }
  107. $cache_file_name = $path . "/" . $cache_filename;
  108. $gantry->document->addStyleSheet($cache_file_name);
  109. }
  110. }
  111. }
  112. }
  113. }
  114. function processJsFiles() {
  115. global $gantry;
  116. $path = $gantry->basePath;
  117. $cache_time = $gantry->get("gzipper-time");
  118. $expires_time = $gantry->get("gzipper-expirestime", 1440);
  119. $ordered_files = array();
  120. $output = array();
  121. $md5sum = "";
  122. $script_tags = $gantry->_scripts;
  123. foreach ($script_tags as $filepath => $file) {
  124. $md5sum .= md5($filepath);
  125. $ordered_files[] = array(dirname($filepath), basename($filepath), $file);
  126. }
  127. if (!is_writable(JPATH_CACHE)) {
  128. foreach ($this->_scripts as $js_file) {
  129. $gantry->document->addScript($js_file);
  130. }
  131. return;
  132. }
  133. if (count($ordered_files) > 0) {
  134. $cache_filename = "js-" . md5($md5sum) . ".php";
  135. $cache_fullpath = JPATH_CACHE . DS . $cache_filename;
  136. //see if file is stale
  137. if (JFile::exists($cache_fullpath)) {
  138. $diff = (time() - filectime($cache_fullpath));
  139. } else {
  140. $diff = $cache_time + 1;
  141. }
  142. if ($diff > $cache_time) {
  143. $outfile = GantryGZipper::_getOutHeader("js", $expires_time);
  144. foreach ($ordered_files as $files) {
  145. $dir = $files[0];
  146. $filename = $files[1];
  147. $details = $files[2];
  148. $detailspath = $dir . DS . $filename;
  149. if (JFile::exists($detailspath)) {
  150. $jsfile = JFile::read($detailspath);
  151. // fix for stupid joolma code
  152. if (strpos($filename, 'joomla.javascript.js')!==false or strpos($filename, 'mambojavascript.js')!==false) {
  153. $jsfile = str_replace("// <?php !!", "// ", $jsfile);
  154. }
  155. $jsfile = self::cleanEndLines($jsfile);
  156. $outfile .= "\n\n/*** " . $filename . " ***/\n\n" . $jsfile;
  157. }
  158. }
  159. JFile::write($cache_fullpath, $outfile);
  160. }
  161. $cache_file_name = $path . "/cache/" . $cache_filename;
  162. $cache_url_name = $gantry->baseUrl . "cache/" . $cache_filename;
  163. $gantry->document->addScript($cache_url_name);
  164. }
  165. }
  166. function cleanEndLines($data){
  167. $file_lines = explode("\n",$data);
  168. while (($line = array_pop($file_lines)) != null){
  169. $clean_line = rtrim($line);
  170. if (strlen($clean_line) > 0){
  171. $end_char = substr($line, strlen($clean_line), 1);
  172. array_push($file_lines,$line);
  173. if ($end_char != ';'){
  174. array_push($file_lines, ";");
  175. }
  176. break;
  177. }
  178. }
  179. return implode($file_lines,"\n");
  180. }
  181. function _getOutHeader($type = "css", $expires_time = 1440) {
  182. if ($type == "css") {
  183. $header = '<?php
  184. ob_start ("ob_gzhandler");
  185. header("Content-type: text/css; charset: UTF-8");
  186. header("Cache-Control: must-revalidate");
  187. $expires_time = ' . $expires_time . ';
  188. $offset = 60 * $expires_time ;
  189. $ExpStr = "Expires: " .
  190. gmdate("D, d M Y H:i:s",
  191. time() + $offset) . " GMT";
  192. header($ExpStr);
  193. ?>';
  194. } else {
  195. $header = '<?php
  196. ob_start ("ob_gzhandler");
  197. header("Content-type: application/x-javascript; charset: UTF-8");
  198. header("Cache-Control: must-revalidate");
  199. $expires_time = ' . $expires_time . ';
  200. $offset = 60 * $expires_time ;
  201. $ExpStr = "Expires: " .
  202. gmdate("D, d M Y H:i:s",
  203. time() + $offset) . " GMT";
  204. header($ExpStr);
  205. ?>';
  206. }
  207. return $header;
  208. }
  209. function _stripCSSWhiteSpace($css_content) {
  210. // remove comments
  211. $css_content = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css_content);
  212. // remove tabs, spaces, newlines, etc.
  213. $css_content = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $css_content);
  214. return $css_content;
  215. }
  216. }