12 require_once
'Auth/OpenID.php';
13 require_once
'Auth/OpenID/KVForm.php';
14 require_once
'Auth/Yadis/XML.php';
15 require_once
'Auth/OpenID/Consumer.php';
18 define(
'Auth_OpenID_IDENTIFIER_SELECT',
19 "http://specs.openid.net/auth/2.0/identifier_select");
23 define(
'Auth_OpenID_SREG_URI',
'http://openid.net/sreg/1.0');
26 define(
'Auth_OpenID_OPENID1_NS',
'http://openid.net/signon/1.0');
27 define(
'Auth_OpenID_THE_OTHER_OPENID1_NS',
'http://openid.net/signon/1.1');
36 define(
'Auth_OpenID_OPENID2_NS',
'http://specs.openid.net/auth/2.0');
40 define(
'Auth_OpenID_NULL_NAMESPACE',
'Null namespace');
43 define(
'Auth_OpenID_OPENID_NS',
'OpenID namespace');
47 define(
'Auth_OpenID_BARE_NS',
'Bare namespace');
51 define(
'Auth_OpenID_NO_DEFAULT',
'NO DEFAULT ALLOWED');
55 define(
'Auth_OpenID_OPENID1_URL_LIMIT', 2047);
60 'ns',
'mode',
'error',
'return_to',
'contact',
'reference',
61 'signed',
'assoc_type',
'session_type',
'dh_modulus',
'dh_gen',
62 'dh_consumer_public',
'claimed_id',
'identity',
'realm',
63 'invalidate_handle',
'op_endpoint',
'response_nonce',
'sig',
64 'assoc_handle',
'trust_root',
'openid');
83 $alias) == $namespace_uri) {
87 if (in_array($namespace_uri,
88 array_values($Auth_OpenID_registered_aliases))) {
92 if (in_array($alias, array_keys($Auth_OpenID_registered_aliases))) {
96 $Auth_OpenID_registered_aliases[$alias] = $namespace_uri;
110 $alias) === $namespace_uri) {
111 unset($Auth_OpenID_registered_aliases[$alias]);
132 $this->
keys = array();
135 if (is_array($classic_array)) {
136 foreach ($classic_array as $key => $value) {
137 $this->
set($key, $value);
146 static function isA($thing)
148 return (is_object($thing) &&
149 strtolower(get_class($thing)) ==
'auth_openid_mapping');
165 return $this->values;
175 for ($i = 0; $i < count($this->
keys); $i++) {
176 $temp[] = array($this->
keys[$i],
187 return count($this->
keys);
194 function set($key, $value)
196 $index = array_search($key, $this->
keys);
198 if ($index !==
false) {
199 $this->
values[$index] = $value;
201 $this->
keys[] = $key;
211 function get($key, $default = null)
213 $index = array_search($key, $this->
keys);
215 if ($index !==
false) {
216 return $this->
values[$index];
229 $old_keys = $this->keys;
230 $old_values = $this->values;
232 $this->
keys = array();
235 foreach ($old_keys as $k) {
239 foreach ($old_values as $v) {
250 $index = array_search($key, $this->
keys);
252 if ($index !==
false) {
253 unset($this->
keys[$index]);
254 unset($this->
values[$index]);
267 return (array_search($value, $this->
keys) !==
false);
281 $this->implicit_namespaces = array();
286 return $this->namespace_to_alias->get($namespace_uri);
291 return $this->alias_to_namespace->get($alias);
297 return $this->namespace_to_alias->keys();
303 return $this->alias_to_namespace->keys();
308 return $this->namespace_to_alias->items();
313 return in_array($namespace_uri, $this->implicit_namespaces);
316 function addAlias($namespace_uri, $desired_alias, $implicit=
false)
323 if (in_array($desired_alias, $Auth_OpenID_OPENID_PROTOCOL_FIELDS)) {
331 if (strpos($desired_alias,
'.') !==
false) {
338 $current_namespace_uri =
339 $this->alias_to_namespace->get($desired_alias);
341 if (($current_namespace_uri !== null) &&
342 ($current_namespace_uri != $namespace_uri)) {
350 $alias = $this->namespace_to_alias->get($namespace_uri);
352 if (($alias !== null) && ($alias != $desired_alias)) {
354 'It is already mapped to alias %s',
355 $namespace_uri, $desired_alias, $alias);
360 is_string($desired_alias));
362 $this->alias_to_namespace->set($desired_alias, $namespace_uri);
363 $this->namespace_to_alias->set($namespace_uri, $desired_alias);
365 array_push($this->implicit_namespaces, $namespace_uri);
368 return $desired_alias;
371 function add($namespace_uri)
377 $alias = $this->namespace_to_alias->get($namespace_uri);
379 if ($alias !== null) {
386 $alias =
'ext' . strval($i);
387 if ($this->
addAlias($namespace_uri, $alias) === null) {
405 return $this->namespace_to_alias->contains($namespace_uri);
420 $this->allowed_openid_namespaces = array(
427 if ($openid_namespace === null) {
428 $this->_openid_ns_uri = null;
451 $openid_args = array();
452 foreach ($args as $key => $value) {
454 if (is_array($value)) {
458 $parts = explode(
'.', $key, 2);
460 if (count($parts) == 2) {
461 list($prefix,
$rest) = $parts;
466 if ($prefix !=
'openid') {
469 $openid_args[
$rest] = $value;
473 if ($obj->_fromOpenIDArgs($openid_args)) {
486 if ($obj->_fromOpenIDArgs($openid_args)) {
509 foreach ($openid_args->items() as $pair) {
510 list(
$rest, $value) = $pair;
512 $parts = explode(
'.',
$rest, 2);
514 if (count($parts) == 2) {
515 list($ns_alias, $ns_key) = $parts;
521 if ($ns_alias ==
'ns') {
522 if ($this->namespaces->addAlias($value, $ns_key) === null) {
532 $ns_args[] = array($ns_alias, $ns_key, $value);
544 foreach ($ns_args as $triple) {
545 list($ns_alias, $ns_key, $value) = $triple;
546 $ns_uri = $this->namespaces->getNamespaceURI($ns_alias);
547 if ($ns_uri === null) {
549 if ($ns_uri === null) {
552 $ns_key = sprintf(
'%s.%s', $ns_alias, $ns_key);
554 $this->namespaces->addAlias($ns_uri, $ns_alias,
true);
558 $this->
setArg($ns_uri, $ns_key, $value);
568 return @$Auth_OpenID_registered_aliases[$mystery_alias];
575 if (!in_array($openid_ns_uri, $this->allowed_openid_namespaces)) {
580 $succeeded = $this->namespaces->addAlias($openid_ns_uri,
583 if ($succeeded ===
false) {
587 $this->_openid_ns_uri = $openid_ns_uri;
594 return $this->_openid_ns_uri;
617 foreach ($this->namespaces->iteritems() as $pair) {
618 list($ns_uri, $alias) = $pair;
619 if ($this->namespaces->isImplicit($ns_uri)) {
623 $ns_key =
'openid.ns';
625 $ns_key =
'openid.ns.' . $alias;
627 $args[$ns_key] = $ns_uri;
630 foreach ($this->args->items() as $pair) {
631 list($ns_parts, $value) = $pair;
632 list($ns_uri, $ns_key) = $ns_parts;
633 $key = $this->
getKey($ns_uri, $ns_key);
634 $args[$key] = $value;
646 foreach ($post_args as $k => $v) {
647 if (strpos($k,
'openid.') !== 0) {
653 $kvargs[substr($k, 7)] = $v;
660 function toFormMarkup($action_url, $form_tag_attrs = null,
661 $submit_text =
"Continue")
663 $form =
"<form accept-charset=\"UTF-8\" ".
664 "enctype=\"application/x-www-form-urlencoded\"";
666 if (!$form_tag_attrs) {
667 $form_tag_attrs = array();
670 $form_tag_attrs[
'action'] = $action_url;
671 $form_tag_attrs[
'method'] =
'post';
673 unset($form_tag_attrs[
'enctype']);
674 unset($form_tag_attrs[
'accept-charset']);
676 if ($form_tag_attrs) {
677 foreach ($form_tag_attrs as $name => $attr) {
678 $form .= sprintf(
" %s=\"%s\"", $name, $attr);
684 foreach ($this->
toPostArgs() as $name => $value) {
686 "<input type=\"hidden\" name=\"%s\" value=\"%s\" />\n",
690 $form .= sprintf(
"<input type=\"submit\" value=\"%s\" />\n",
693 $form .=
"</form>\n";
698 function toURL($base_url)
719 $args[] = array($k, $v);
735 if ($this->_openid_ns_uri === null) {
737 'OpenID namespace not set');
746 $err_msg = sprintf(
"Namespace must be Auth_OpenID_BARE_NS, ".
747 "Auth_OpenID_OPENID_NS or a string. got %s",
774 return $this->args->contains(array(
$namespace, $ns_key));
789 $ns_alias = $this->namespaces->getAlias(
$namespace);
792 if ($ns_alias === null) {
799 $tail = sprintf(
'%s.%s', $ns_alias, $ns_key);
802 return 'openid.' . $tail;
813 if ((!$this->args->contains(array(
$namespace, $key))) &&
815 $err_msg = sprintf(
"Namespace %s missing required field %s",
819 return $this->args->get(array(
$namespace, $key), $default);
833 foreach ($this->args->items() as $pair) {
834 list($key, $value) = $pair;
835 list($pair_ns, $ns_key) = $key;
837 $stuff[$ns_key] = $value;
854 foreach ($updates as $k => $v) {
869 $this->args->set(array(
$namespace, $key), $value);
884 return $this->args->del(array(
$namespace, $key));
890 if ($aliased_key ==
'ns') {
895 $parts = explode(
'.', $aliased_key, 2);
897 if (count($parts) != 2) {
900 list($alias, $key) = $parts;
902 if ($alias ==
'ns') {
905 return $this->namespaces->getNamespaceURI($key);
907 $ns = $this->namespaces->getNamespaceURI($alias);
916 return $this->
getArg($ns, $key, $default);