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 | |
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
-rw-r--r-- | _css/css.php | 37 | ||||
-rw-r--r-- | _css/js.php | 40 | ||||
-rw-r--r-- | engine/handlers/cache_handler.php | 89 | ||||
-rw-r--r-- | engine/lib/views.php | 7 | ||||
-rw-r--r-- | engine/start.php | 8 | ||||
-rw-r--r-- | htaccess_dist | 10 | ||||
-rw-r--r-- | simplecache/view.php | 78 | ||||
-rw-r--r-- | views/default/css/elgg.php | 3 | ||||
-rw-r--r-- | views/default/friends/picker.php | 2 |
9 files changed, 106 insertions, 168 deletions
diff --git a/_css/css.php b/_css/css.php deleted file mode 100644 index 4a5152afb..000000000 --- a/_css/css.php +++ /dev/null @@ -1,37 +0,0 @@ -<?php -/** - * Outputs the main CSS view. - * - * Requests to $url/css.css are rewritten via - * mod_rewrite rules in .htaccess (or similar) to this file. - * - * The main CSS is a view located at 'css'. The location of the - * file used to generate this view changes based upon current viewtype - * and plugins enabled. By default the viewtype is 'default' (HTML) and the - * view file is views/default/css.php. Plugins can override or extend this view. - * - * This view is cached via simplecache. - * - * @see views/default/css.php - * @see simplecache/view.php - * @see elgg_extend_view() - * - * @uses $_GET['viewtype'] The current viewtype. Determines where to look for the - * css.php view. - * @uses $override A global that tells simplecache to ignore caching. - * - * @package Elgg - * @subpackage Core - */ - -global $viewinput, $override; - -$viewinput['view'] = 'css'; -$viewinput['viewtype'] = $_GET['viewtype']; - -header("Content-type: text/css", true); -header('Expires: ' . date('r', time() + 86400000), true); -header("Pragma: public", true); -header("Cache-Control: public", true); - -require_once(dirname(dirname(__FILE__)) . '/simplecache/view.php'); diff --git a/_css/js.php b/_css/js.php deleted file mode 100644 index 41b7901f9..000000000 --- a/_css/js.php +++ /dev/null @@ -1,40 +0,0 @@ -<?php -/** - * Outputs a JS view. - * - * There are 2 main JS views used in elgg: - * js/initialise_elgg - * js/friendsPickerv1 - * - * The location of the files used to generate these view can change based upon - * current viewtype and plugins enabled. By default the viewtype is - * 'default' (HTML) and the view files are in views/default/js/. Plugins can - * override or extend these views. - * - * These 2 main JS views are cached via simplecache. - * - * @see views/default/js/initialise_elgg.php - * @see views/default/js/friendsPickerv1.php - * @see simplecache/view.php - * @see elgg_extend_view() - * - * @uses $_GET['viewtype'] The current viewtype. Determines where to look for the - * JS view files. - * @uses $_GET['view'] The view to output, relative to the view js/ - * @uses $override A global that tells simplecache to ignore caching. - * - * @package Elgg - * @subpackage Core - */ - -global $viewinput, $override; - -$viewinput['view'] = 'js/' . $_GET['js']; -$viewinput['viewtype'] = $_GET['viewtype']; - -header('Content-type: text/javascript'); -header('Expires: ' . date('r', time() + 864000000)); -header("Pragma: public"); -header("Cache-Control: public"); - -require_once(dirname(dirname(__FILE__)) . '/simplecache/view.php');
\ No newline at end of file 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; diff --git a/engine/lib/views.php b/engine/lib/views.php index bf133f592..6c4cec0f2 100644 --- a/engine/lib/views.php +++ b/engine/lib/views.php @@ -1335,10 +1335,11 @@ function elgg_views_register_core_head_elements() { $lastcache = $CONFIG->lastcache; $viewtype = elgg_get_viewtype(); - $url = "{$base}_css/js.php?lastcache=$lastcache&js=initialise_elgg&viewtype=$viewtype"; + //$url = "{$base}_css/js.php?lastcache=$lastcache&js=initialise_elgg&viewtype=$viewtype"; + $url = "{$base}cache/js/initialise_elgg/$viewtype/initialise_elgg.$lastcache.js"; elgg_register_js($url, 'initialise_elgg'); - $url = "{$base}_css/css.css?lastcache=$lastcache&viewtype=$viewtype"; + $url = "{$base}cache/css/elgg/$viewtype/elgg.$lastcache.css"; elgg_register_css($url, 'elgg'); } @@ -1353,7 +1354,7 @@ function elgg_views_register_core_head_elements() { function elgg_views_boot() { global $CONFIG; - elgg_view_register_simplecache('css'); + elgg_view_register_simplecache('css/elgg'); elgg_view_register_simplecache('js/friendsPickerv1'); elgg_view_register_simplecache('js/initialise_elgg'); diff --git a/engine/start.php b/engine/start.php index 7a3dee31e..646ddbf72 100644 --- a/engine/start.php +++ b/engine/start.php @@ -133,11 +133,11 @@ elgg_trigger_event('init', 'system'); // Don't do it on upgrade because upgrade does it itself. // @todo - move into function and perhaps run off init system event if (!defined('UPGRADING')) { - $view = get_input('view', 'default'); - $lastupdate = datalist_get("simplecache_lastupdate_$view"); - $lastcached = datalist_get("simplecache_lastcached_$view"); + $viewtype = get_input('view', 'default'); + $lastupdate = datalist_get("simplecache_lastupdate_$viewtype"); + $lastcached = datalist_get("simplecache_lastcached_$viewtype"); if ($lastupdate == 0 || $lastcached < $lastupdate) { - elgg_view_regenerate_simplecache($view); + elgg_view_regenerate_simplecache($viewtype); } // needs to be set for links in html head $CONFIG->lastcache = $lastcached; diff --git a/htaccess_dist b/htaccess_dist index dcc829fba..3295ed87a 100644 --- a/htaccess_dist +++ b/htaccess_dist @@ -99,17 +99,17 @@ RewriteEngine on RewriteRule ^action\/([A-Za-z0-9\_\-\/]+)$ engine/handlers/action_handler.php?action=$1&%{QUERY_STRING} +RewriteRule ^pg\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/page_handler.php?handler=$1&page=$2&%{QUERY_STRING} +RewriteRule ^pg\/([A-Za-z0-9\_\-]+)$ engine/handlers/page_handler.php?handler=$1&%{QUERY_STRING} + +RewriteRule ^cache\/(.*)$ engine/handlers/cache_handler.php?request=$1&%{QUERY_STRING} + RewriteRule ^services\/api\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/service_handler.php?handler=$1&request=$2&%{QUERY_STRING} RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)$ engine/handlers/export_handler.php?view=$1&guid=$2 RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/$ engine/handlers/export_handler.php?view=$1&guid=$2 RewriteRule ^export\/([A-Za-z]+)\/([0-9]+)\/([A-Za-z]+)\/([A-Za-z0-9\_]+)\/$ engine/handlers/export_handler.php?view=$1&guid=$2&type=$3&idname=$4 -RewriteRule ^\_css\/css\.css$ _css/css.php - -RewriteRule ^pg\/([A-Za-z0-9\_\-]+)\/(.*)$ engine/handlers/page_handler.php?handler=$1&page=$2&%{QUERY_STRING} -RewriteRule ^pg\/([A-Za-z0-9\_\-]+)$ engine/handlers/page_handler.php?handler=$1&%{QUERY_STRING} - RewriteRule xml-rpc.php engine/handlers/xml-rpc_handler.php RewriteRule mt/mt-xmlrpc.cgi engine/handlers/xml-rpc_handler.php diff --git a/simplecache/view.php b/simplecache/view.php deleted file mode 100644 index bdfec45a7..000000000 --- a/simplecache/view.php +++ /dev/null @@ -1,78 +0,0 @@ -<?php -/** - * Simple cache viewer - * Bypasses the engine to view simple cached CSS views. - * - * @package Elgg - * @subpackage Core - */ - -// Get DB settings, connect -require_once(dirname(dirname(__FILE__)) . '/engine/settings.php'); - -global $CONFIG, $viewinput, $override; -if (!isset($override)) { - $override = FALSE; -} - -$contents = ''; - -if (!isset($viewinput)) { - $viewinput = $_GET; -} - -if ($mysql_dblink = @mysql_connect($CONFIG->dbhost, $CONFIG->dbuser, $CONFIG->dbpass, true)) { - $view_name = $viewinput['view']; - $viewtype = $viewinput['viewtype']; - if (empty($viewtype)) { - $viewtype = 'default'; - } - - if (@mysql_select_db($CONFIG->dbname, $mysql_dblink)) { - // get dataroot and simplecache_enabled in one select for efficiency - $simplecache_enabled = true; - if (!isset($dataroot)) { - $query = "select name, value from {$CONFIG->dbprefix}datalists - where name in ('dataroot', 'simplecache_enabled')"; - if ($result = mysql_query($query, $mysql_dblink)) { - $row = mysql_fetch_object($result); - - while ($row) { - if ($row->name == 'dataroot') { - $dataroot = $row->value; - } else if ($row->name == 'simplecache_enabled') { - $simplecache_enabled = $row->value; - } - $row = mysql_fetch_object($result); - } - } - } - - if ($simplecache_enabled || $override) { - $filename = $dataroot . 'views_simplecache/' . md5($viewtype . $view_name); - if (file_exists($filename)) { - $contents = file_get_contents($filename); - } else { - $query = "INSERT into {$CONFIG->dbprefix}datalists - set name = 'simplecache_lastupdate_$viewtype', - value = '0' ON DUPLICATE KEY UPDATE value='0'"; - mysql_query($query); - } - } - } -} - -// load full engine if simplecache is disabled, overriden, or invalid -if (!$contents) { - mysql_close($mysql_dblink); - require_once(dirname(dirname(__FILE__)) . "/engine/start.php"); - $contents = elgg_view($view_name); -} - -header("Content-Length: " . strlen($contents)); - -$split_output = str_split($contents, 1024); - -foreach ($split_output as $chunk) { - echo $chunk; -} diff --git a/views/default/css/elgg.php b/views/default/css/elgg.php new file mode 100644 index 000000000..57b118c2b --- /dev/null +++ b/views/default/css/elgg.php @@ -0,0 +1,3 @@ +<?php + +echo elgg_view('css'); diff --git a/views/default/friends/picker.php b/views/default/friends/picker.php index 78c10e394..b8cc1560c 100644 --- a/views/default/friends/picker.php +++ b/views/default/friends/picker.php @@ -12,7 +12,7 @@ $base = elgg_get_site_url(); $viewtype = elgg_get_viewtype(); elgg_register_js("{$base}vendors/jquery/jquery.easing.1.3.packed.js", 'jquery.easing'); -elgg_register_js("{$base}_css/js.php?lastcache={$vars['config']->lastcache}&js=friendsPickerv1&viewtype=$viewtype", 'friendsPicker'); +elgg_register_js("{$base}cache/js/friendsPickerv1/default/friendsPickerv1.{$vars['config']->lastcache}.js", 'friendsPicker'); $chararray = elgg_echo('friendspicker:chararray'); |