1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the tiqr project. |
4
|
|
|
* |
5
|
|
|
* The tiqr project aims to provide an open implementation for |
6
|
|
|
* authentication using mobile devices. It was initiated by |
7
|
|
|
* SURFnet and developed by Egeniq. |
8
|
|
|
* |
9
|
|
|
* More information: http://www.tiqr.org |
10
|
|
|
* |
11
|
|
|
* @author Ivo Jansch <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @package tiqr |
14
|
|
|
* |
15
|
|
|
* @license New BSD License - See LICENSE file for details. |
16
|
|
|
* |
17
|
|
|
* @copyright (C) 2010-2011 SURFnet BV |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* A StateStorage implementation using memcache to store state information. |
23
|
|
|
* |
24
|
|
|
* When passing $options to the StateStorage::getStorage method, you can use |
25
|
|
|
* the following settings: |
26
|
|
|
* - "servers": An array of servers with "host" and "port" keys. If "port" is |
27
|
|
|
* ommited the default port is used. |
28
|
|
|
* This option is optional; if servers is not specified, we will |
29
|
|
|
* assume a memcache on localhost on the default memcache port. |
30
|
|
|
* - "prefix": If the memcache server is shared with other applications, a prefix |
31
|
|
|
* can be used so that there are no key collisions with other |
32
|
|
|
* applications. |
33
|
|
|
* |
34
|
|
|
* @author ivo |
35
|
|
|
*/ |
36
|
|
|
class Tiqr_StateStorage_Memcache extends Tiqr_StateStorage_Abstract |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* The memcache instance. |
40
|
|
|
* @var Memcache |
41
|
|
|
*/ |
42
|
|
|
protected $_memcache = NULL; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The flavor of memcache PHP extension we are using. |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private static $extension = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The default configuration |
52
|
|
|
*/ |
53
|
|
|
const DEFAULT_HOST = '127.0.0.1'; |
54
|
|
|
const DEFAULT_PORT = 11211; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the prefix to use for all keys in memcache. |
58
|
|
|
* @return String the prefix |
59
|
|
|
*/ |
60
|
|
|
protected function _getKeyPrefix() |
61
|
|
|
{ |
62
|
|
|
if (isset($this->_options["prefix"])) { |
63
|
|
|
return $this->_options["prefix"]; |
64
|
|
|
} |
65
|
|
|
return ""; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialize the statestorage by setting up the memcache instance. |
70
|
|
|
* It's not necessary to call this function, the Tiqr_StateStorage factory |
71
|
|
|
* will take care of that. |
72
|
|
|
*/ |
73
|
|
|
public function init() |
74
|
|
|
{ |
75
|
|
|
parent::init(); |
76
|
|
|
|
77
|
|
|
$class = class_exists('\Memcache') ? '\Memcache' : (class_exists('\Memcached') ? '\Memcached' : false); |
78
|
|
|
self::$extension = strtolower($class); |
|
|
|
|
79
|
|
|
|
80
|
|
|
$this->_memcache = new $class(); |
81
|
|
|
|
82
|
|
|
if (!isset($this->_options["servers"])) { |
83
|
|
|
$this->_memcache->addServer(self::DEFAULT_HOST, self::DEFAULT_PORT); |
84
|
|
|
} else { |
85
|
|
|
foreach ($this->_options['servers'] as $server) { |
86
|
|
|
if (!array_key_exists('port', $server)) { |
87
|
|
|
$server['port'] = self::DEFAULT_PORT; |
88
|
|
|
} |
89
|
|
|
if (!array_key_exists('host', $server)) { |
90
|
|
|
$server['host'] = self::DEFAULT_HOST; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->_memcache->addServer($server['host'], $server['port']); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* (non-PHPdoc) |
100
|
|
|
* @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue() |
101
|
|
|
*/ |
102
|
|
|
public function setValue($key, $value, $expire=0) |
103
|
|
|
{ |
104
|
|
|
$key = $this->_getKeyPrefix().$key; |
105
|
|
|
|
106
|
|
|
if (self::$extension === '\memcached') { |
107
|
|
|
$this->_memcache->set($key, $value, $expire); |
108
|
|
|
} else { |
109
|
|
|
$this->_memcache->set($key, $value, 0, $expire); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* (non-PHPdoc) |
115
|
|
|
* @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue() |
116
|
|
|
*/ |
117
|
|
|
public function unsetValue($key) |
118
|
|
|
{ |
119
|
|
|
$key = $this->_getKeyPrefix().$key; |
120
|
|
|
|
121
|
|
|
return $this->_memcache->delete($key); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* (non-PHPdoc) |
126
|
|
|
* @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue() |
127
|
|
|
*/ |
128
|
|
|
public function getValue($key) |
129
|
|
|
{ |
130
|
|
|
$key = $this->_getKeyPrefix().$key; |
131
|
|
|
|
132
|
|
|
$result = $this->_memcache->get($key); |
133
|
|
|
if( $result === false ) |
134
|
|
|
return null; |
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|