From 388d8405bb10b3b2107629f8cd4133bff3a7693c Mon Sep 17 00:00:00 2001 From: brettp Date: Wed, 13 Apr 2011 14:39:21 +0000 Subject: Consolidated the css and js pagehandlers with elgg_cachable_view_pagehandler() and fixed for URLs with multiple dots after the last / (like /js/calendars/fullcalendar.min.123456789.js) git-svn-id: http://code.elgg.org/elgg/trunk@8985 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/elgglib.php | 76 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 23 deletions(-) (limited to 'engine/lib/elgglib.php') diff --git a/engine/lib/elgglib.php b/engine/lib/elgglib.php index 1aef48ef4..063e25fc4 100644 --- a/engine/lib/elgglib.php +++ b/engine/lib/elgglib.php @@ -1685,21 +1685,7 @@ function _elgg_shutdown_hook() { * @elgg_pagehandler js */ function elgg_js_page_handler($page) { - if (is_array($page) && sizeof($page)) { - $js = implode('/', $page); - $js = substr($js, 0, strpos($js, '.')); - $return = elgg_view('js/' . $js); - - header('Content-type: text/javascript'); - - // @todo should js be cached when simple cache turned off - //header('Expires: ' . date('r', time() + 864000)); - //header("Pragma: public"); - //header("Cache-Control: public"); - //header("Content-Length: " . strlen($return)); - - echo $return; - } + return elgg_cacheable_view_page_handler($page, 'js'); } /** @@ -1749,18 +1735,62 @@ function elgg_css_page_handler($page) { // default css $page[0] = 'elgg'; } + + return elgg_cacheable_view_page_handler($page, 'css'); +} + +/** + * Serves a JS or CSS view with headers for caching. + * + * //name/of/view.. + * + * @param array $page The page array + * @param string $type The type: js or css + * + * @return mixed + */ +function elgg_cacheable_view_page_handler($page, $type) { + + switch ($type) { + case 'js': + $content_type = 'text/javascript'; + break; + + case 'css': + $content_type = 'text/css'; + break; + + default: + return false; + break; + } + + if ($page) { + // the view file names can have multiple dots + // eg: views/default/js/calendars/jquery.fullcalendar.min.php + // translates to the url /js/calendars/jquery.fullcalendar.min..js + // and the view js/calendars/jquery.fullcalendar.min + // we ignore the last two dots for the ts and the ext. + $last_part = array_pop($page); + $last_part_bits = explode('.', $last_part); + $last_part_bits = array_slice($last_part_bits, 0, -2); + $page[] = implode('.', $last_part_bits); - $css = substr($page[0], 0, strpos($page[0], '.')); - $return = elgg_view("css/$css"); + $view = implode('/', $page); + $return = elgg_view("$type/$view"); + + header("Content-type: $content_type"); - header("Content-type: text/css", true); + // @todo should js be cached when simple cache turned off + //header('Expires: ' . date('r', time() + 864000)); + //header("Pragma: public"); + //header("Cache-Control: public"); + //header("Content-Length: " . strlen($return)); - // @todo should css be cached when simple cache is turned off - //header('Expires: ' . date('r', time() + 86400000), true); - //header("Pragma: public", true); - //header("Cache-Control: public", true); + echo $return; + } - echo $return; + return true; } /** -- cgit v1.2.3