ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
SReg.php
Go to the documentation of this file.
1 <?php
2 
45 require_once 'Auth/OpenID/Message.php';
46 require_once 'Auth/OpenID/Extension.php';
47 
48 // The data fields that are listed in the sreg spec
51  'fullname' => 'Full Name',
52  'nickname' => 'Nickname',
53  'dob' => 'Date of Birth',
54  'email' => 'E-mail Address',
55  'gender' => 'Gender',
56  'postcode' => 'Postal Code',
57  'country' => 'Country',
58  'language' => 'Language',
59  'timezone' => 'Time Zone');
60 
65 function Auth_OpenID_checkFieldName($field_name)
66 {
68 
69  if (!in_array($field_name, array_keys($Auth_OpenID_sreg_data_fields))) {
70  return false;
71  }
72  return true;
73 }
74 
75 // URI used in the wild for Yadis documents advertising simple
76 // registration support
77 define('Auth_OpenID_SREG_NS_URI_1_0', 'http://openid.net/sreg/1.0');
78 
79 // URI in the draft specification for simple registration 1.1
80 // <http://openid.net/specs/openid-simple-registration-extension-1_1-01.html>
81 define('Auth_OpenID_SREG_NS_URI_1_1', 'http://openid.net/extensions/sreg/1.1');
82 
83 // This attribute will always hold the preferred URI to use when
84 // adding sreg support to an XRDS file or in an OpenID namespace
85 // declaration.
86 define('Auth_OpenID_SREG_NS_URI', Auth_OpenID_SREG_NS_URI_1_1);
87 
89 
97 function Auth_OpenID_supportsSReg($endpoint)
98 {
99  return ($endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_1) ||
100  $endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_0));
101 }
102 
125  static function _getSRegNS($message)
126  {
127  $alias = null;
128  $found_ns_uri = null;
129 
130  // See if there exists an alias for one of the two defined
131  // simple registration types.
132  foreach (array(Auth_OpenID_SREG_NS_URI_1_1,
133  Auth_OpenID_SREG_NS_URI_1_0) as $sreg_ns_uri) {
134  $alias = $message->namespaces->getAlias($sreg_ns_uri);
135  if ($alias !== null) {
136  $found_ns_uri = $sreg_ns_uri;
137  break;
138  }
139  }
140 
141  if ($alias === null) {
142  // There is no alias for either of the types, so try to
143  // add one. We default to using the modern value (1.1)
144  $found_ns_uri = Auth_OpenID_SREG_NS_URI_1_1;
145  if ($message->namespaces->addAlias(Auth_OpenID_SREG_NS_URI_1_1,
146  'sreg') === null) {
147  // An alias for the string 'sreg' already exists, but
148  // it's defined for something other than simple
149  // registration
150  return null;
151  }
152  }
153 
154  return $found_ns_uri;
155  }
156 }
157 
170 
171  var $ns_alias = 'sreg';
172 
176  static function build($required=null, $optional=null,
177  $policy_url=null,
178  $sreg_ns_uri=Auth_OpenID_SREG_NS_URI,
179  $cls='Auth_OpenID_SRegRequest')
180  {
181  $obj = new $cls();
182 
183  $obj->required = array();
184  $obj->optional = array();
185  $obj->policy_url = $policy_url;
186  $obj->ns_uri = $sreg_ns_uri;
187 
188  if ($required) {
189  if (!$obj->requestFields($required, true, true)) {
190  return null;
191  }
192  }
193 
194  if ($optional) {
195  if (!$obj->requestFields($optional, false, true)) {
196  return null;
197  }
198  }
199 
200  return $obj;
201  }
202 
216  static function fromOpenIDRequest($request, $cls='Auth_OpenID_SRegRequest')
217  {
218 
219  $obj = call_user_func_array(array($cls, 'build'),
220  array(null, null, null, Auth_OpenID_SREG_NS_URI, $cls));
221 
222  // Since we're going to mess with namespace URI mapping, don't
223  // mutate the object that was passed in.
224  $m = $request->message;
225 
226  $obj->ns_uri = $obj->_getSRegNS($m);
227  $args = $m->getArgs($obj->ns_uri);
228 
229  if ($args === null || Auth_OpenID::isFailure($args)) {
230  return null;
231  }
232 
233  $obj->parseExtensionArgs($args);
234 
235  return $obj;
236  }
237 
262  function parseExtensionArgs($args, $strict=false)
263  {
264  foreach (array('required', 'optional') as $list_name) {
265  $required = ($list_name == 'required');
266  $items = Auth_OpenID::arrayGet($args, $list_name);
267  if ($items) {
268  foreach (explode(',', $items) as $field_name) {
269  if (!$this->requestField($field_name, $required, $strict)) {
270  if ($strict) {
271  return false;
272  }
273  }
274  }
275  }
276  }
277 
278  $this->policy_url = Auth_OpenID::arrayGet($args, 'policy_url');
279 
280  return true;
281  }
282 
288  {
289  return array_merge($this->required, $this->optional);
290  }
291 
296  {
297  return count($this->allRequestedFields());
298  }
299 
303  function contains($field_name)
304  {
305  return (in_array($field_name, $this->required) ||
306  in_array($field_name, $this->optional));
307  }
308 
320  function requestField($field_name,
321  $required=false, $strict=false)
322  {
323  if (!Auth_OpenID_checkFieldName($field_name)) {
324  return false;
325  }
326 
327  if ($strict) {
328  if ($this->contains($field_name)) {
329  return false;
330  }
331  } else {
332  if (in_array($field_name, $this->required)) {
333  return true;
334  }
335 
336  if (in_array($field_name, $this->optional)) {
337  if ($required) {
338  unset($this->optional[array_search($field_name,
339  $this->optional)]);
340  } else {
341  return true;
342  }
343  }
344  }
345 
346  if ($required) {
347  $this->required[] = $field_name;
348  } else {
349  $this->optional[] = $field_name;
350  }
351 
352  return true;
353  }
354 
366  function requestFields($field_names, $required=false, $strict=false)
367  {
368  if (!is_array($field_names)) {
369  return false;
370  }
371 
372  foreach ($field_names as $field_name) {
373  if (!$this->requestField($field_name, $required, $strict=$strict)) {
374  return false;
375  }
376  }
377 
378  return true;
379  }
380 
389  function getExtensionArgs()
390  {
391  $args = array();
392 
393  if ($this->required) {
394  $args['required'] = implode(',', $this->required);
395  }
396 
397  if ($this->optional) {
398  $args['optional'] = implode(',', $this->optional);
399  }
400 
401  if ($this->policy_url) {
402  $args['policy_url'] = $this->policy_url;
403  }
404 
405  return $args;
406  }
407 }
408 
418 
419  var $ns_alias = 'sreg';
420 
422  $sreg_ns_uri=Auth_OpenID_SREG_NS_URI)
423  {
424  if ($data === null) {
425  $this->data = array();
426  } else {
427  $this->data = $data;
428  }
429 
430  $this->ns_uri = $sreg_ns_uri;
431  }
432 
445  static function extractResponse($request, $data)
446  {
447  $obj = new Auth_OpenID_SRegResponse();
448  $obj->ns_uri = $request->ns_uri;
449 
450  foreach ($request->allRequestedFields() as $field) {
451  $value = Auth_OpenID::arrayGet($data, $field);
452  if ($value !== null) {
453  $obj->data[$field] = $value;
454  }
455  }
456 
457  return $obj;
458  }
459 
474  static function fromSuccessResponse($success_response, $signed_only=true)
475  {
477 
478  $obj = new Auth_OpenID_SRegResponse();
479  $obj->ns_uri = $obj->_getSRegNS($success_response->message);
480 
481  if ($signed_only) {
482  $args = $success_response->getSignedNS($obj->ns_uri);
483  } else {
484  $args = $success_response->message->getArgs($obj->ns_uri);
485  }
486 
487  if ($args === null || Auth_OpenID::isFailure($args)) {
488  return null;
489  }
490 
491  foreach ($Auth_OpenID_sreg_data_fields as $field_name => $desc) {
492  if (in_array($field_name, array_keys($args))) {
493  $obj->data[$field_name] = $args[$field_name];
494  }
495  }
496 
497  return $obj;
498  }
499 
500  function getExtensionArgs()
501  {
502  return $this->data;
503  }
504 
505  // Read-only dictionary interface
506  function get($field_name, $default=null)
507  {
508  if (!Auth_OpenID_checkFieldName($field_name)) {
509  return null;
510  }
511 
512  return Auth_OpenID::arrayGet($this->data, $field_name, $default);
513  }
514 
515  function contents()
516  {
517  return $this->data;
518  }
519 }
520 
521