Browse Source

Add basic attribute exchange support

Closes #7
tags/0.6
Chris Smith 14 years ago
parent
commit
480323323d
2 changed files with 185 additions and 0 deletions
  1. 153
    0
      ax.ext.php
  2. 32
    0
      examples/basic/index.php

+ 153
- 0
ax.ext.php View File

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

+ 32
- 0
examples/basic/index.php View File

@@ -58,6 +58,10 @@
58 58
   // Include the simple registration extension
59 59
   require('../../sreg.ext.php');
60 60
 
61
+  // Include and configure the attribute exchange extension
62
+  require('../../ax.ext.php');
63
+  AttributeExchange::addRequiredType('email', AttributeExchange::EMAIL);
64
+
61 65
   require('../../processor.php');
62 66
 
63 67
  } else {
@@ -129,6 +133,34 @@
129 133
    unset($_SESSION['openid']['sreg']);
130 134
   }
131 135
 
136
+  // Show the attribute exchange data returned, if any.
137
+  if (isset($_SESSION['openid']['ax'])) {
138
+   echo '<table>';
139
+   echo '<caption>Attribute Exchange Extension data</caption>';
140
+
141
+   foreach ($_SESSION['openid']['ax']['types'] as $type => $uri) {
142
+    echo '<tr><th>', htmlentities($type), '</th>';
143
+    echo '<td>', htmlentities($uri), '</td>';
144
+    echo '<td>', $count = $_SESSION['openid']['ax']['counts'][$type], '</td>';
145
+    echo '<td>';
146
+    if ($count == 1) {
147
+     echo htmlentities($_SESSION['openid']['ax']['data'][$type]);
148
+    } else if ($count > 1) {
149
+     echo '<ol>';
150
+     foreach ($_SESSION['openid']['ax']['data'][$type] as $value) {
151
+      echo '<li>', htmlentities($value), '</li>';
152
+     }
153
+     echo '</ol>';
154
+    }
155
+    echo '</td>';
156
+    echo '</tr>';
157
+   }
158
+
159
+   echo '</table>';
160
+
161
+   unset($_SESSION['openid']['sreg']);
162
+  }
163
+
132 164
  }
133 165
 ?>
134 166
   <form action="<?PHP echo htmlentities($_SERVER['REQUEST_URI']); ?>"

Loading…
Cancel
Save