diff options
author | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-28 21:25:22 +0000 |
---|---|---|
committer | cash <cash@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-28 21:25:22 +0000 |
commit | a1b7808dea79b3a547aa805b86d829fa1f5f19db (patch) | |
tree | ea1c8b172058b9bb07ed2741539072ddeec26b5c /engine/handlers/cache_handler.php | |
parent | d892f121e1b9a34edfa49288a20b99d4d4b65f6b (diff) | |
download | elgg-a1b7808dea79b3a547aa805b86d829fa1f5f19db.tar.gz elgg-a1b7808dea79b3a547aa805b86d829fa1f5f19db.tar.bz2 |
Fixes #2160 #2678 introducing a more flexible simplecache handler (this requires an htaccess update)
git-svn-id: http://code.elgg.org/elgg/trunk@7464 36083f99-b078-4883-b0ff-0f9b5a30f544
Diffstat (limited to 'engine/handlers/cache_handler.php')
-rw-r--r-- | engine/handlers/cache_handler.php | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/engine/handlers/cache_handler.php b/engine/handlers/cache_handler.php new file mode 100644 index 000000000..d08863a00 --- /dev/null +++ b/engine/handlers/cache_handler.php @@ -0,0 +1,89 @@ +<?php +/** + * Cache handler. + * + * External access to cached CSS and JavaScript views. The cached file URLS + * should be of the form: cache/<type>/<view>/<viewtype>/<unique_id> where + * type is either css or js, view is the name of the cached view, and + * unique_id is an identifier that is updated every time the cache is flushed. + * The simplest way to maintain a unique identifier is to use the lastcache + * variable in Elgg's config object. + * + * @see elgg_view_register_simplecache() + * + * @package Elgg.Core + * @subpackage Cache + */ + +// Get dataroot +require_once(dirname(dirname(__FILE__)) . '/settings.php'); +$mysql_dblink = mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, true); +if (!$mysql_dblink) { + echo 'Cache error: unable to connect to database server'; + exit; +} + +if (!mysql_select_db($CONFIG->dbname, $mysql_dblink)) { + echo 'Cache error: unable to connect to Elgg database'; + exit; +} + +$query = "select name, value from {$CONFIG->dbprefix}datalists where name = 'dataroot'"; +$result = mysql_query($query, $mysql_dblink); +if (!$result) { + echo 'Cache error: unable to get the data root'; + exit; +} +$row = mysql_fetch_object($result); +$dataroot = $row->value; + + + +$dirty_request = $_GET['request']; +// only alphanumeric characters plus / and . and no '..' +$filter = array("options" => array("regexp" => "/^(\.?[a-zA-Z0-9\/]+)+$/")); +$request = filter_var($dirty_request, FILTER_VALIDATE_REGEXP, $filter); +if (!$request) { + echo 'Cache error: bad request'; + exit; +} +$request = explode('/', $request); + + +//cache/<type>/<view>/<viewtype>/ +$type = $request[0]; +$view = $request[1]; +$viewtype = $request[2]; + +switch ($type) { + case 'css': + header("Content-type: text/css", true); + header('Expires: ' . date('r', time() + 86400000), true); + header("Pragma: public", true); + header("Cache-Control: public", true); + + $view = "css/$view"; + break; + case 'js': + header('Content-type: text/javascript', true); + header('Expires: ' . date('r', time() + 864000000), true); + header("Pragma: public", true); + header("Cache-Control: public", true); + + $view = "js/$view"; + break; +} + +$filename = $dataroot . 'views_simplecache/' . md5($viewtype . $view); + +if (file_exists($filename)) { + $contents = file_get_contents($filename); +} else { + // someone trying to access a non-cached file or a race condition with cache flushing + mysql_close($mysql_dblink); + require_once(dirname(dirname(__FILE__)) . "/start.php"); + elgg_set_viewtype($viewtype); + $contents = elgg_view($view); +} + +echo $contents; |