Skip to content

Commit af3a58b

Browse files
committed
building basic structure
1 parent 415a56d commit af3a58b

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/nbproject/

Controller/Session.php

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Idrinth\PhpMemcachedSession\Controller;
4+
5+
class Session implements \SessionHandlerInterface {
6+
/**
7+
*
8+
* @return boolean
9+
*/
10+
public function close() {
11+
return true;
12+
}
13+
/**
14+
*
15+
* @return string
16+
*/
17+
public function create_sid() {
18+
return sha1(mt_rand() . microtime() . getmypid() . ini_get('idrinth_session.sid_pepper'));
19+
}
20+
/**
21+
*
22+
* @param string $session_id
23+
*/
24+
public function destroy($session_id) {
25+
return \Idrinth\PhpMemcachedSession\Model\Session::get($session_id)->delete();
26+
}
27+
/**
28+
*
29+
* @param int $maxlifetime
30+
* @return boolean
31+
*/
32+
public function gc($maxlifetime) {
33+
return true;
34+
}
35+
/**
36+
*
37+
* @param string $save_path
38+
* @param string $name
39+
* @return boolean
40+
*/
41+
public function open($save_path,$name) {
42+
ini_set('session.serialize_handler','php_serialize');
43+
return true;
44+
}
45+
/**
46+
*
47+
* @param string $session_id
48+
* @return string
49+
*/
50+
public function read($session_id) {
51+
return \Idrinth\PhpMemcachedSession\Model\Session::get($session_id)->load();
52+
}
53+
/**
54+
*
55+
* @param string $session_id
56+
* @param string $session_data
57+
* @return boolean
58+
*/
59+
public function write($session_id,$session_data) {
60+
return \Idrinth\PhpMemcachedSession\Model\Session::get($session_id)->save($session_data);
61+
}
62+
}

Model/Session.php

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Idrinth\PhpMemcachedSession\Model;
4+
5+
class Session {
6+
/**
7+
*
8+
* @var string
9+
*/
10+
protected $agent;
11+
/**
12+
*
13+
* @var \Idrinth\PhpMemcachedSession\Repository\MemCache
14+
*/
15+
protected $repository;
16+
/**
17+
*
18+
* @var string
19+
*/
20+
protected $original = '';
21+
/**
22+
*
23+
* @var string
24+
*/
25+
protected $id;
26+
/**
27+
*
28+
* @var \Idrinth\PhpMemcachedSession\Model\Session
29+
*/
30+
protected static $instance;
31+
/**
32+
*
33+
* @param string $id
34+
*/
35+
protected function __construct($id) {
36+
$this->id = $id;
37+
$this->agent = $this->getUserAgent();
38+
$this->repository = new \Idrinth\PhpMemcachedSession\Repository\MemCache();
39+
}
40+
/**
41+
*
42+
* @return string a serialized string
43+
*/
44+
public function load() {
45+
$this->original = $this->getByKey([$this->agent,$this->id]) . '';
46+
return $this->original;
47+
}
48+
/**
49+
*
50+
* @param string $data
51+
* @return boolean was it saved?
52+
*/
53+
public function save($data) {
54+
if($data === $this->original) {
55+
return true;//nothing to change
56+
}
57+
return $this->repository->updateByKey([$this->agent,$this->id],$data);
58+
}
59+
/**
60+
* deletes the current data and instance
61+
*/
62+
public function delete() {
63+
$this->repository->removeByKey([$this->agent,$this->id]);
64+
self::$instance = null;
65+
}
66+
/**
67+
*
68+
* @return string
69+
*/
70+
private function getUserAgent() {
71+
$agent = $_SERVER['HTTP_USER_AGENT'];
72+
if(strpos($agent,'Opera') || strpos($agent,'OPR/')) {
73+
return 'opera';
74+
}
75+
if(strpos($agent,'Edge')) {
76+
return 'edge';
77+
}
78+
if(strpos($agent,'Chrome')) {
79+
return 'chrome';
80+
}
81+
if(strpos($agent,'Safari')) {
82+
return 'safari';
83+
}
84+
if(strpos($agent,'Firefox')) {
85+
return 'firefox';
86+
}
87+
if(strpos($agent,'MSIE') || strpos($agent,'Trident/7')) {
88+
return 'internet explorer';
89+
}
90+
return trim(preg_replace('/(\/|\(|[0-9]|V[0-9]).*/i','',$agent));
91+
}
92+
/**
93+
*
94+
* @param string $id
95+
* @return \Idrinth\PhpMemcachedSession\Model\Session
96+
*/
97+
public static function get($id) {
98+
if(!self::$instance) {
99+
self::$instance = new self($id);
100+
}
101+
return self::$instance;
102+
}
103+
}

Repository/MemCache.php

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Idrinth\PhpMemcachedSession\Repository;
4+
5+
class MemCache {
6+
/**
7+
*
8+
* @var int
9+
*/
10+
protected $duration = 3600;
11+
/**
12+
*
13+
* @var string[]
14+
*/
15+
protected $prefix = array('idrinth','memcached-session');
16+
/**
17+
*
18+
* @var \Memcached
19+
*/
20+
protected $memcache;
21+
/**
22+
* @return \Idrinth\PhpMemcachedSession\Repository\MemCache
23+
*/
24+
public function __construct() {
25+
$this->memcache = new \Memcached();
26+
$this->duration = ini_get("session.gc_maxlifetime");
27+
if(count($this->memcache->getServerList()) == 0) {
28+
$this->memcache->addServer(ini_get('idrinth_session.memcache_server'),ini_get('idrinth_session.memcache_port'));
29+
}
30+
if(!$this->duration) {
31+
$this->duration = 3600;
32+
}
33+
return $this;
34+
}
35+
/**
36+
*
37+
* @param string[] $params
38+
* @return string
39+
*/
40+
protected function getKey($params = array()) {
41+
return trim(implode('.',$this->prefix) . '.' . implode('.',$params),'.');
42+
}
43+
/**
44+
*
45+
* @param array $params
46+
* @return string
47+
*/
48+
public function getByKey(array $params) {
49+
$value = $this->memcache->get($this->getKey($params));
50+
if($value) {
51+
$this->memcache->touch($this->getKey($params),time() + $this->duration);
52+
}
53+
return $value;
54+
}
55+
/**
56+
*
57+
* @param string[] $params
58+
* @param string $value
59+
* @return boolean
60+
*/
61+
public function setByKey(array $params,$value) {
62+
return $this->memcache->set($this->getKey($params),$value,time() + $this->duration);
63+
}
64+
/**
65+
*
66+
* @param string[] $params
67+
* @return boolean
68+
*/
69+
public function removeByKey(array $params) {
70+
return $this->memcache->set($this->getKey($params),'',1);
71+
}
72+
}

packages.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "idrinth/php-memcached-session",
3+
"description": "Provides a sessionhandler for php and memcached",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": ["session", "php", "memcached"],
7+
"homepage": "https://github.com/Idrinth/PhpMemcachedSession",
8+
"authors": [
9+
{
10+
"name": "Björn Büttner",
11+
"email": "[email protected]",
12+
"homepage": "http://idrinth.de",
13+
"role": "Developer"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"Idrinth\\PhpMemcachedSession\\": ""
19+
}
20+
},
21+
"support": {
22+
"issues": "https://github.com/Idrinth/PhpMemcachedSession/issues",
23+
"source": "https://github.com/Idrinth/PhpMemcachedSession"
24+
},
25+
"require": {
26+
"php": "^5.6.0",
27+
"ext-memcached": "*"
28+
}
29+
}

0 commit comments

Comments
 (0)