Commit cdc11013 authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review

Merge "Bug 785469: Add support for memcached session save handler"

parents e43d15cb bbd333b7
......@@ -86,25 +86,61 @@ class Session {
ini_set('session.use_strict_mode', true);
}
$sessionpath = get_config('sessionpath');
ini_set('session.save_path', '3;' . $sessionpath);
// Attempt to create session directories
if (!is_dir("$sessionpath/0")) {
// Create three levels of directories, named 0-9, a-f
$characters = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
$characters = array_merge($characters, array('g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v'));
}
foreach ($characters as $c1) {
check_dir_exists("$sessionpath/$c1");
foreach ($characters as $c2) {
check_dir_exists("$sessionpath/$c1/$c2");
foreach ($characters as $c3) {
check_dir_exists("$sessionpath/$c1/$c2/$c3");
// Now lets use the correct session handler
// Currently only deals with file and memcached
switch (get_config('sessionhandler')) {
case 'memcache':
throw new ConfigSanityException(get_string('memcacheusememcached', 'error'));
break;
case 'memcached':
$memcacheservers = get_config('memcacheservers');
if (!$memcacheservers) {
throw new ConfigSanityException(get_string('nomemcacheserversdefined', 'error', get_config('sessionhandler')));
}
if (!extension_loaded(get_config('sessionhandler'))) {
throw new ConfigSanityException(get_string('nophpextension', 'error', get_config('sessionhandler')));
}
// Because we only want memcached servers we need to strip off any 'tcp://' if accidentally added
$servers = preg_replace('#tcp://#', '', $memcacheservers);
foreach (explode(',', $servers) as $server) {
list($destination, $port) = explode(':', $server);
$nc = 'nc -z ' . $destination . ' ' . $port;
$status = exec($nc, $out, $fail);
if ($fail === 1) {
// if server has 'nc' command but can't reach the server:port
throw new ConfigSanityException(get_string('nomemcachedserver', 'error', $server));
}
}
}
ini_set('session.save_handler', 'memcached');
ini_set('session.save_path', $servers);
$sess = new MemcachedSession();
session_set_save_handler($sess, true);
break;
case 'file':
$sessionpath = get_config('sessionpath');
ini_set('session.save_path', '3;' . $sessionpath);
// Attempt to create session directories
if (!is_dir("$sessionpath/0")) {
// Create three levels of directories, named 0-9, a-f
$characters = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
$characters = array_merge($characters, array('g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v'));
}
foreach ($characters as $c1) {
check_dir_exists("$sessionpath/$c1");
foreach ($characters as $c2) {
check_dir_exists("$sessionpath/$c1/$c2");
foreach ($characters as $c3) {
check_dir_exists("$sessionpath/$c1/$c2/$c3");
}
}
}
}
break;
default:
throw new ConfigSanityException(get_string('wrongsessionhandle', 'error', get_config('sessionhandler')));
}
}
......@@ -642,3 +678,11 @@ function clear_duplicate_cookies() {
);
}
}
class MemcachedSession extends SessionHandler {
// we need to extend and override the read method to force it to return string value
// in order to comply with PHP 7's more strict type checking
public function read($session_id) {
return (string)parent::read($session_id);
}
}
......@@ -143,3 +143,8 @@ $string['nopasswordsaltset'] = 'No sitewide password salt has been set. Edit you
$string['passwordsaltweak'] = 'Your sitewide password salt is not strong enough. Edit your config.php and set the "passwordsaltmain" parameter to a longer secret phrase.';
$string['urlsecretweak'] = 'The $cfg->urlsecret set for this site has not been changed from the default value. Edit your config.php and set the $cgf->urlsecret parameter to a different string (or null if you do not wish to use a urlsecret).';
$string['notproductionsite'] = 'This site is not in production mode. Some data may not be available and/or may be out of date.';
$string['wrongsessionhandle'] = 'The session save handler "%s" is not supported in Mahara.';
$string['nomemcachedserver'] = 'The memcache server "%s" is not reachable. Please check the $cfg->memcacheservers value to make sure it is correct';
$string['nophpextension'] = 'The PHP extension "%s" is not enabled. Please enable the extension and restart your webserver or choose a different session option.';
$string['nomemcacheserversdefined'] = 'The session save handler "%s" has no related servers defined. Please set the $cfg->memcacheservers value, eg "localhost:11211"';
$string['memcacheusememcached'] = 'The "memcache" session storage is obsolete. Please use "memcached"';
\ No newline at end of file
......@@ -723,6 +723,15 @@ $cfg->openbadgedisplayer_source = '{"backpack":"https://backpack.openbadges.org/
*/
// $cfg->memcacheservers = 'hostname1:port1,hostname2:port2,hostname3:port3';
/**
* @global string $cfg->sessionhandler
* An alternative session handler for Mahara if you do not wish to use files.
* Specify the name of the session handler.
*/
$cfg->sessionhandler = 'file';
//$cfg->sessionhandler = 'memcached';
//$cfg->memcacheservers = 'localhost:11211';
/**
* @global string $cfg->ssphpsessionhandler
* An alternative session handler for SimpleSAMLphp if you do not wish to use memcache.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment