1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| <?php
namespace Snc\RedisBundle\Session\Storage\Handler;
/**
* Sessionhandler to serialize PHP sessions into redis using json as
* format.
*
* This way we can read the session in node.js websockets.
*
* @author "Robert Gruendler <r.gruendler@gmail.com>"
*/
class JsonSessionHandler extends RedisSessionHandler
{
/*
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$raw = $this->unserializeSession($data);
$session = json_encode($raw);
parent::write($sessionId, $session);
}
/*
* {@inheritdoc}
*/
public function read($sessionId)
{
$raw = parent::read($sessionId);
if (strlen($raw) == 0) {
return $raw;
}
return $this->serializeSession(json_decode($raw, true));
}
/**
* @see http://php.net/manual/en/function.session-decode.php#108037
* @param array $session_data
* @throws Exception
* @return multitype:mixed
*/
private function unserializeSession($session_data)
{
$return_data = array();
$offset = 0;
while ($offset < strlen($session_data)) {
if (!strstr(substr($session_data, $offset), "|")) {
throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
}
$pos = strpos($session_data, "|", $offset);
$num = $pos - $offset;
$varname = substr($session_data, $offset, $num);
$offset += $num + 1;
$data = unserialize(substr($session_data, $offset));
$return_data[$varname] = $data;
$offset += strlen(serialize($data));
}
return $return_data;
}
/**
* @see http://php.net/manual/en/function.session-encode.php#76425
* @param array $array
* @return string
*/
private function serializeSession(array $array)
{
$raw = '';
$line = 0;
$keys = array_keys($array);
foreach ($keys as $key) {
$value = $array[$key];
$line++;
$raw .= $key . '|';
if (is_array($value) && isset($value['huge_recursion_blocker_we_hope'])) {
$raw .= 'R:' . $value['huge_recursion_blocker_we_hope'] . ';';
} else {
$raw .= serialize($value);
}
$array[$key] = Array('huge_recursion_blocker_we_hope' => $line);
}
return $raw;
}
} |
Partager