Sreg.php
8.76 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_OpenId
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Sreg.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_OpenId_Extension
*/
require_once "Zend/OpenId/Extension.php";
/**
* 'Simple Refistration Extension' for Zend_OpenId
*
* @category Zend
* @package Zend_OpenId
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_OpenId_Extension_Sreg extends Zend_OpenId_Extension
{
/**
* SREG 1.1 namespace. All OpenID SREG 1.1 messages MUST contain variable
* openid.ns.sreg with its value.
*/
const NAMESPACE_1_1 = "http://openid.net/extensions/sreg/1.1";
private $_props;
private $_policy_url;
private $_version;
/**
* Creates SREG extension object
*
* @param array $props associative array of SREG variables
* @param string $policy_url SREG policy URL
* @param float $version SREG version
* @return array
*/
public function __construct(array $props=null, $policy_url=null, $version=1.0)
{
$this->_props = $props;
$this->_policy_url = $policy_url;
$this->_version = $version;
}
/**
* Returns associative array of SREG variables
*
* @return array
*/
public function getProperties() {
if (is_array($this->_props)) {
return $this->_props;
} else {
return array();
}
}
/**
* Returns SREG policy URL
*
* @return string
*/
public function getPolicyUrl() {
return $this->_policy_url;
}
/**
* Returns SREG protocol version
*
* @return float
*/
public function getVersion() {
return $this->_version;
}
/**
* Returns array of allowed SREG variable names.
*
* @return array
*/
public static function getSregProperties()
{
return array(
"nickname",
"email",
"fullname",
"dob",
"gender",
"postcode",
"country",
"language",
"timezone"
);
}
/**
* Adds additional SREG data to OpenId 'checkid_immediate' or
* 'checkid_setup' request.
*
* @param array &$params request's var/val pairs
* @return bool
*/
public function prepareRequest(&$params)
{
if (is_array($this->_props) && count($this->_props) > 0) {
foreach ($this->_props as $prop => $req) {
if ($req) {
if (isset($required)) {
$required .= ','.$prop;
} else {
$required = $prop;
}
} else {
if (isset($optional)) {
$optional .= ','.$prop;
} else {
$optional = $prop;
}
}
}
if ($this->_version >= 1.1) {
$params['openid.ns.sreg'] = Zend_OpenId_Extension_Sreg::NAMESPACE_1_1;
}
if (!empty($required)) {
$params['openid.sreg.required'] = $required;
}
if (!empty($optional)) {
$params['openid.sreg.optional'] = $optional;
}
if (!empty($this->_policy_url)) {
$params['openid.sreg.policy_url'] = $this->_policy_url;
}
}
return true;
}
/**
* Parses OpenId 'checkid_immediate' or 'checkid_setup' request,
* extracts SREG variables and sets ovject properties to corresponding
* values.
*
* @param array $params request's var/val pairs
* @return bool
*/
public function parseRequest($params)
{
if (isset($params['openid_ns_sreg']) &&
$params['openid_ns_sreg'] === Zend_OpenId_Extension_Sreg::NAMESPACE_1_1) {
$this->_version= 1.1;
} else {
$this->_version= 1.0;
}
if (!empty($params['openid_sreg_policy_url'])) {
$this->_policy_url = $params['openid_sreg_policy_url'];
} else {
$this->_policy_url = null;
}
$props = array();
if (!empty($params['openid_sreg_optional'])) {
foreach (explode(',', $params['openid_sreg_optional']) as $prop) {
$prop = trim($prop);
$props[$prop] = false;
}
}
if (!empty($params['openid_sreg_required'])) {
foreach (explode(',', $params['openid_sreg_required']) as $prop) {
$prop = trim($prop);
$props[$prop] = true;
}
}
$props2 = array();
foreach (self::getSregProperties() as $prop) {
if (isset($props[$prop])) {
$props2[$prop] = $props[$prop];
}
}
$this->_props = (count($props2) > 0) ? $props2 : null;
return true;
}
/**
* Adds additional SREG data to OpenId 'id_res' response.
*
* @param array &$params response's var/val pairs
* @return bool
*/
public function prepareResponse(&$params)
{
if (is_array($this->_props) && count($this->_props) > 0) {
if ($this->_version >= 1.1) {
$params['openid.ns.sreg'] = Zend_OpenId_Extension_Sreg::NAMESPACE_1_1;
}
foreach (self::getSregProperties() as $prop) {
if (!empty($this->_props[$prop])) {
$params['openid.sreg.' . $prop] = $this->_props[$prop];
}
}
}
return true;
}
/**
* Parses OpenId 'id_res' response and sets object's properties according
* to 'openid.sreg.*' variables in response
*
* @param array $params response's var/val pairs
* @return bool
*/
public function parseResponse($params)
{
if (isset($params['openid_ns_sreg']) &&
$params['openid_ns_sreg'] === Zend_OpenId_Extension_Sreg::NAMESPACE_1_1) {
$this->_version= 1.1;
} else {
$this->_version= 1.0;
}
$props = array();
foreach (self::getSregProperties() as $prop) {
if (!empty($params['openid_sreg_' . $prop])) {
$props[$prop] = $params['openid_sreg_' . $prop];
}
}
if (isset($this->_props) && is_array($this->_props)) {
foreach (self::getSregProperties() as $prop) {
if (isset($this->_props[$prop]) &&
$this->_props[$prop] &&
!isset($props[$prop])) {
return false;
}
}
}
$this->_props = (count($props) > 0) ? $props : null;
return true;
}
/**
* Addes SREG properties that are allowed to be send to consumer to
* the given $data argument.
*
* @param array &$data data to be stored in tusted servers database
* @return bool
*/
public function getTrustData(&$data)
{
$data[get_class()] = $this->getProperties();
return true;
}
/**
* Check if given $data contains necessury SREG properties to sutisfy
* OpenId request. On success sets SREG response properties from given
* $data and returns true, on failure returns false.
*
* @param array $data data from tusted servers database
* @return bool
*/
public function checkTrustData($data)
{
if (is_array($this->_props) && count($this->_props) > 0) {
$props = array();
$name = get_class();
if (isset($data[$name])) {
$props = $data[$name];
} else {
$props = array();
}
$props2 = array();
foreach ($this->_props as $prop => $req) {
if (empty($props[$prop])) {
if ($req) {
return false;
}
} else {
$props2[$prop] = $props[$prop];
}
}
$this->_props = (count($props2) > 0) ? $props2 : null;
}
return true;
}
}