PHP OpenID consumer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ax.ext.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?PHP
  2. /* Poidsy 0.6 - http://chris.smith.name/projects/poidsy
  3. * Copyright (c) 2008-2010 Chris Smith
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. class AttributeExchange {
  24. const NS = 'http://openid.net/srv/ax/1.0';
  25. const USERNAME = 'http://axschema.org/namePerson/friendly';
  26. const EMAIL = 'http://axschema.org/contact/email';
  27. const FULLNAME = 'http://axschema.org/namePerson';
  28. const DATEOFBIRTH = 'http://axschema.org/birthDate';
  29. const GENDER = 'http://axschema.org/person/gender';
  30. const POSTCODE = 'http://axschema.org/contact/postalCode/home';
  31. const COUNTRY = 'http://axschema.org/contact/country/home';
  32. const LANGUAGE = 'http://axschema.org/pref/language';
  33. const TIMEZONE = 'http://axschema.org/pref/timezone';
  34. const NAMEPREFIX = 'http://axschema.org/namePerson/prefix';
  35. const FIRSTNAME = 'http://axschema.org/namePerson/first';
  36. const LASTNAME = 'http://axschema.org/namePerson/last';
  37. const MIDDLENAME = 'http://axschema.org/namePerson/middle';
  38. const NAMESUFFIX = 'http://axschema.org/namePerson/suffix';
  39. const COMPANYNAME = 'http://axschema.org/company/name';
  40. const JOBTITLE = 'http://axschema.org/company/title';
  41. const YEAROFBIRTH = 'http://axschema.org/birthDate/birthYear';
  42. const MONTHOFBIRTH = 'http://axschema.org/birthDate/birthMonth';
  43. const DAYOFBIRTH = 'http://axschema.org/birthDate/birthday';
  44. private static $aliases = array();
  45. private static $required = array();
  46. private static $optional = array();
  47. private static $count = array();
  48. public static function addRequiredType($alias, $uri, $count = null) {
  49. self::$required[] = $alias;
  50. self::addType($alias, $uri, $count);
  51. }
  52. public static function addOptionalType($alias, $uri, $count = null) {
  53. self::$optional[] = $alias;
  54. self::addType($alias, $uri, $count);
  55. }
  56. private static function addType($alias, $uri, $count) {
  57. self::$aliases[$alias] = $uri;
  58. if ($count != null && (ctype_digit($count) || is_int($count) || $count == 'unlimited')) {
  59. self::$count[$alias] = $count;
  60. }
  61. }
  62. public function parseResponse() {
  63. $ns = false;
  64. foreach ($_REQUEST as $k => $v) {
  65. if (substr($k, 0, 10) == 'openid_ns_' && $v == self::NS) {
  66. $ns = substr($k, 10);
  67. break;
  68. }
  69. }
  70. if ($ns === false) { return; }
  71. $_SESSION['openid']['ax'] = array(
  72. 'types' => array(),
  73. 'data' => array(),
  74. 'counts' => array()
  75. );
  76. foreach ($_REQUEST as $k => $v) {
  77. if (substr($k, 0, 8 + strlen($ns)) == "openid_{$ns}_") {
  78. // TODO: Need to check mode etc
  79. $rest = substr($k, 8 + strlen($ns));
  80. if (substr($rest, 0, 5) == 'type_') {
  81. $_SESSION['openid']['ax']['types'][substr($rest, 5)] = $v;
  82. } else if (substr($rest, 0, 6) == 'count_') {
  83. $_SESSION['openid']['ax']['counts'][substr($rest, 6)] = (int) $v;
  84. }
  85. }
  86. }
  87. foreach ($_SESSION['openid']['ax']['types'] as $alias => $uri) {
  88. if (!isset($_SESSION['openid']['ax']['counts'][$alias])) {
  89. $_SESSION['openid']['ax']['counts'][$alias] = 1;
  90. }
  91. $count = $_SESSION['openid']['ax']['counts'][$alias];
  92. if ($count > 1) {
  93. for ($i = 1; $i < $count + 1; $i++) {
  94. $_SESSION['openid']['ax']['data'][$alias][] = $_REQUEST["openid_{$ns}_value_{$alias}_$i"];
  95. }
  96. } else if ($count == 1) {
  97. $_SESSION['openid']['ax']['data'][$alias] = $_REQUEST["openid_{$ns}_value_$alias"];
  98. }
  99. }
  100. }
  101. public function decorate(&$args, $ns) {
  102. $args['openid.ns.' . $ns] = self::NS;
  103. $args['openid.' . $ns . '.mode'] = 'fetch_request';
  104. foreach (array_merge(self::$optional, self::$required) as $alias) {
  105. $args['openid.' . $ns . '.type.' . $alias] = self::$aliases[$alias];
  106. }
  107. foreach (self::$count as $alias => $count) {
  108. $args['openid.' . $ns . '.count.' . $alias] = $count;
  109. }
  110. if (!empty(self::$optional)) {
  111. $args['openid.' . $ns . '.if_available'] = implode(',', self::$optional);
  112. }
  113. if (!empty(self::$required)) {
  114. $args['openid.' . $ns . '.required'] = implode(',', self::$required);
  115. }
  116. }
  117. }
  118. $ax = new AttributeExchange();
  119. $_POIDSY['decorators'][] = $ax;
  120. $_POIDSY['handlers'][] = $ax;
  121. unset($ax);
  122. ?>