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