aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/lib/elgglib.php76
1 files changed, 53 insertions, 23 deletions
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.
+ *
+ * /<css||js>/name/of/view.<last_cache>.<css||js>
+ *
+ * @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.<ts>.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;
}
/**