From 42c82ef7f081630c15a5d354205896764348a76a Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 29 Jun 2009 06:08:50 -0700 Subject: Temporary checkin to allow merge with trunk... don't integrate --- modules/gallery/libraries/Theme_View.php | 29 ++++++++++++++++++++++++- modules/organize/helpers/organize_theme.php | 3 +-- modules/server_add/helpers/server_add_theme.php | 3 +-- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'modules') diff --git a/modules/gallery/libraries/Theme_View.php b/modules/gallery/libraries/Theme_View.php index 25818821..c5888b4a 100644 --- a/modules/gallery/libraries/Theme_View.php +++ b/modules/gallery/libraries/Theme_View.php @@ -20,6 +20,7 @@ class Theme_View_Core extends View { private $theme_name = null; private $scripts = array(); + private $css = array(); /** * Attempts to load a view and pre-load view data. @@ -172,6 +173,10 @@ class Theme_View_Core extends View { $this->scripts[$file] = 1; } + public function css($file) { + $this->css[$file] = 1; + } + /** * Combine a series of Javascript files into a single one and cache it in the database, then * return a single "; } + /** + * Combine a series of Javascript files into a single one and cache it in the database, then + * return a single "; -- cgit v1.2.3 From 6ec293dfe70665c528d803b78a2bb295633496ec Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 29 Jun 2009 08:24:42 -0700 Subject: *Note* work in progress. Implement the combined css functionality. Local url references and replace with absolute urls instead of relative. --- modules/gallery/controllers/combined.php | 46 +++++++++++++++++++++++++++++ modules/gallery/libraries/Gallery_View.php | 47 ++++++++++++++++++++++++++++-- themes/default/views/page.html.php | 2 +- 3 files changed, 91 insertions(+), 4 deletions(-) (limited to 'modules') diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index 510482db..50fe77c4 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -58,5 +58,51 @@ class Combined_Controller extends Controller { Kohana::close_buffers(false); print $content; } + + public function css($key) { + if (preg_match('/[^0-9a-f]/', $key)) { + // The key can't contain non-hex, so just terminate early + Kohana::show_404(); + } + + // We don't need to save the session for this request + Session::abort_save(); + + // Our data is immutable, so if they already have a copy then it needs no updating. + if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { + header('HTTP/1.0 304 Not Modified'); + return; + } + + $cache = Cache::instance(); + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { + $content = $cache->get("{$key}_gz"); + } + + if (empty($content)) { + $content = $cache->get($key); + } + + if (empty($content)) { + Kohana::show_404(); + } + + if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { + header("Content-Encoding: gzip"); + header("Cache-Control: public"); + } + + header("Content-Type: text/css; charset=UTF-8"); + header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); + header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); + + Kohana::close_buffers(false); + print $content; + } + + public function __call($function, $args) { + array_unshift($args, $function); + print ""; + } } diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 7000d3de..1c1dec38 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -113,12 +113,53 @@ class Gallery_View_Core extends View { protected function combine_css() { $links = array(); $key = ""; + static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; + foreach (array_keys($this->css) as $file) { - $links[] = ""; + $path = DOCROOT . $file; + if (file_exists($path)) { + $stats = stat($path); + $links[] = $path; + // 7 == size, 9 == mtime, see http://php.net/stat + $key = "{$key}$file $stats[7] $stats[9],"; + } else { + Kohana::log("alert", "CSS file missing: " . $file); + } + } + $key = md5($key); + $cache = Cache::instance(); + $contents = $cache->get($key); + $docroot_length = strlen(DOCROOT); + + if (empty($contents)) { + $contents = ""; + foreach ($links as $link) { + $css = file_get_contents($link); + if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { + $search = $replace = array(); + foreach ($matches as $match) { + $relative = substr(realpath(dirname($link) . "/$match[1]"), $docroot_length); + if (!empty($relative)) { + $search[] = $match[1]; + $replace[] = url::abs_file($relative); + } else { + Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ", + $match[1], $link)); + } + } + $css = str_replace($search, $replace, $css); + } + $contents .= $css; + } + $cache->set($key, $contents, array("css"), 30 * 84600); + if (function_exists("gzencode")) { + $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array("css", "gzip"), 30 * 84600); + } } - return implode("\n", $links); + return ""; } } \ No newline at end of file diff --git a/themes/default/views/page.html.php b/themes/default/views/page.html.php index f6984958..2e68f571 100644 --- a/themes/default/views/page.html.php +++ b/themes/default/views/page.html.php @@ -25,8 +25,8 @@ " type="image/x-icon" /> css("lib/yui/reset-fonts-grids.css") ?> - css("lib/themeroller/ui.base.css") ?> css("lib/superfish/css/superfish.css") ?> + css("lib/themeroller/ui.base.css") ?> theme_css("css/screen.css") ?> "; - } } diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index 01496c0d..5e0d5feb 100644 --- a/modules/gallery/libraries/Admin_View.php +++ b/modules/gallery/libraries/Admin_View.php @@ -99,6 +99,7 @@ class Admin_View_Core extends Gallery_View { } if ($function == "admin_head") { + array_unshift($blocks, $this->combine_css()); array_unshift($blocks, $this->combine_script()); } diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 1c1dec38..b4cfde46 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -113,7 +113,6 @@ class Gallery_View_Core extends View { protected function combine_css() { $links = array(); $key = ""; - static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; foreach (array_keys($this->css) as $file) { $path = DOCROOT . $file; @@ -130,27 +129,12 @@ class Gallery_View_Core extends View { $key = md5($key); $cache = Cache::instance(); $contents = $cache->get($key); - $docroot_length = strlen(DOCROOT); + $contents = ""; if (empty($contents)) { $contents = ""; foreach ($links as $link) { - $css = file_get_contents($link); - if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { - $search = $replace = array(); - foreach ($matches as $match) { - $relative = substr(realpath(dirname($link) . "/$match[1]"), $docroot_length); - if (!empty($relative)) { - $search[] = $match[1]; - $replace[] = url::abs_file($relative); - } else { - Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ", - $match[1], $link)); - } - } - $css = str_replace($search, $replace, $css); - } - $contents .= $css; + $contents .= $this->process_css($link); } $cache->set($key, $contents, array("css"), 30 * 84600); if (function_exists("gzencode")) { @@ -162,4 +146,38 @@ class Gallery_View_Core extends View { url::site("combined/css/$key") . "\" />"; } + private function process_css($css_file) { + static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; + $docroot_length = strlen(DOCROOT); + + $css = file_get_contents($css_file); + if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { + $search = $replace = array(); + foreach ($matches as $match) { + $relative = substr(realpath(dirname($css_file) . "/$match[1]"), $docroot_length); + if (!empty($relative)) { + $search[] = $match[0]; + $replace[] = "url('" . url::abs_file($relative) . "')"; + } else { + Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ", + $match[1], $css_file)); + } + } + $css = str_replace($search, $replace, $css); + } + $imports = preg_match_all("#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#", + $css, $matches, PREG_SET_ORDER); + + if ($imports) { + $search = $replace = array(); + foreach ($matches as $match) { + Kohana::log("error", dirname($css_file) . "/$match[1]"); + $search[] = $match[0]; + $replace[] = $this->process_css(dirname($css_file) . "/$match[1]"); + } + $css = str_replace($search, $replace, $css); + } + + return $css; + } } \ No newline at end of file -- cgit v1.2.3 From 006b63030a364677143799c7ce41eabb10c86eee Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 29 Jun 2009 15:38:55 -0700 Subject: Combine the Combined::javascript and Combined::css into a single method implemented by the magic method __call. The first parameter is the content type for text/xxxx and the 2nd parameter is the key of the combined file. --- modules/gallery/controllers/combined.php | 46 ++++---------------------------- 1 file changed, 5 insertions(+), 41 deletions(-) (limited to 'modules') diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index 399a60f4..8a157e6b 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -18,48 +18,11 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Combined_Controller extends Controller { - public function javascript($key) { - if (preg_match('/[^0-9a-f]/', $key)) { - // The key can't contain non-hex, so just terminate early - Kohana::show_404(); - } - - // We don't need to save the session for this request - Session::abort_save(); - - // Our data is immutable, so if they already have a copy then it needs no updating. - if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { - header('HTTP/1.0 304 Not Modified'); - return; - } - - $cache = Cache::instance(); - if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { - $content = $cache->get("{$key}_gz"); - } - - if (empty($content)) { - $content = $cache->get($key); - } - - if (empty($content)) { + public function __call($type, $key) { + if (empty($key)) { Kohana::show_404(); } - - if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false) { - header("Content-Encoding: gzip"); - header("Cache-Control: public"); - } - - header("Content-Type: text/javascript; charset=UTF-8"); - header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); - header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); - - Kohana::close_buffers(false); - print $content; - } - - public function css($key) { + $key = $key[0]; if (preg_match('/[^0-9a-f]/', $key)) { // The key can't contain non-hex, so just terminate early Kohana::show_404(); @@ -92,12 +55,13 @@ class Combined_Controller extends Controller { header("Cache-Control: public"); } - header("Content-Type: text/css; charset=UTF-8"); + header("Content-Type: text/$type; charset=UTF-8"); header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); Kohana::close_buffers(false); print $content; } + } -- cgit v1.2.3 From 3080317d6e5e4ea9e56b1fd5444c4bcf5852c362 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 29 Jun 2009 17:44:02 -0700 Subject: Refactor combined controller a bit 1) Create public javascript() and css() functions and turn __call() into a private function to protect us against having some random type show up in there. Otherwise anything you put in the 2nd argument gets emitted in the header which is a security hole. 2) Fix a bug ("$key = $key[0]") which was breaking functionality. Eliminate the hex check, it's not really necessary in the majority case and doesn't hurt us in edge cases. 3) Convert some empty() calls to !, no need for a function call there. 4) Add phpDoc. --- modules/gallery/controllers/combined.php | 41 ++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 13 deletions(-) (limited to 'modules') diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index 8a157e6b..f6c6d60b 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -18,35 +18,49 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Combined_Controller extends Controller { - public function __call($type, $key) { - if (empty($key)) { - Kohana::show_404(); - } - $key = $key[0]; - if (preg_match('/[^0-9a-f]/', $key)) { - // The key can't contain non-hex, so just terminate early - Kohana::show_404(); - } + /** + * Return the combined Javascript bundle associated with the given key. + */ + public function javascript($key) { + return $this->_emit("javascript", $key); + } - // We don't need to save the session for this request - Session::abort_save(); + /** + * Return the combined CSS bundle associated with the given key. + */ + public function css($key) { + return $this->_emit("css", $key); + } + /** + * Print out a cached entry. + * @param string the combined entry type (either "javascript" or "css") + * @param string the key (typically an md5 sum) + */ + private function _emit($type, $key) { // Our data is immutable, so if they already have a copy then it needs no updating. if (!empty($_SERVER["HTTP_IF_MODIFIED_SINCE"])) { header('HTTP/1.0 304 Not Modified'); return; } + if (empty($key)) { + Kohana::show_404(); + } + + // We don't need to save the session for this request + Session::abort_save(); + $cache = Cache::instance(); if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip") !== false ) { $content = $cache->get("{$key}_gz"); } - if (empty($content)) { + if (!$content) { $content = $cache->get($key); } - if (empty($content)) { + if (!$content) { Kohana::show_404(); } @@ -55,6 +69,7 @@ class Combined_Controller extends Controller { header("Cache-Control: public"); } + // $type is either 'javascript' or 'css' header("Content-Type: text/$type; charset=UTF-8"); header("Expires: Tue, 19 Jan 2038 00:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s T", time())); -- cgit v1.2.3 From fa8ca2f7ad21980f0b45e7acc67be4be296f9f87 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 29 Jun 2009 18:12:53 -0700 Subject: Refactor combine_xxx() functions together into combine_files() and use html functions to generate the resulting elements. Add phpdoc. --- modules/gallery/controllers/combined.php | 2 + modules/gallery/libraries/Admin_View.php | 6 +-- modules/gallery/libraries/Gallery_View.php | 81 ++++++++++-------------------- modules/gallery/libraries/Theme_View.php | 4 +- 4 files changed, 34 insertions(+), 59 deletions(-) (limited to 'modules') diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index f6c6d60b..9df74638 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -22,6 +22,7 @@ class Combined_Controller extends Controller { * Return the combined Javascript bundle associated with the given key. */ public function javascript($key) { + $key = substr($key, 0, strlen($key) - 3); // strip off the trailing .js return $this->_emit("javascript", $key); } @@ -29,6 +30,7 @@ class Combined_Controller extends Controller { * Return the combined CSS bundle associated with the given key. */ public function css($key) { + $key = substr($key, 0, strlen($key) - 4); // strip off the trailing .css return $this->_emit("css", $key); } diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index 5e0d5feb..f7de96bf 100644 --- a/modules/gallery/libraries/Admin_View.php +++ b/modules/gallery/libraries/Admin_View.php @@ -98,9 +98,9 @@ class Admin_View_Core extends Gallery_View { } } - if ($function == "admin_head") { - array_unshift($blocks, $this->combine_css()); - array_unshift($blocks, $this->combine_script()); + if ($function == "head") { + array_unshift($blocks, $this->combine_files($this->css, "css")); + array_unshift($blocks, $this->combine_files($this->css, "javascript")); } if (Session::instance()->get("debug")) { diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 9d4fe5c0..48cf8089 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -49,45 +49,6 @@ class Gallery_View_Core extends View { return $absolute_url ? url::abs_file($arg) : url::file($arg); } - /** - * Combine a series of Javascript files into a single one and cache it in the database, then - * return a single "; - } - /** * Add a css file to the combined css list. * @param $file the relative path to a script from the gallery3 directory @@ -107,12 +68,11 @@ class Gallery_View_Core extends View { } /** - * Combine a series of Javascript files into a single one and cache it in the database, then - * return a single