00001 <?php
00002
00045 require_once 'Auth/OpenID/Message.php';
00046 require_once 'Auth/OpenID/Extension.php';
00047
00048
00049 global $Auth_OpenID_sreg_data_fields;
00050 $Auth_OpenID_sreg_data_fields = array(
00051 'fullname' => 'Full Name',
00052 'nickname' => 'Nickname',
00053 'dob' => 'Date of Birth',
00054 'email' => 'E-mail Address',
00055 'gender' => 'Gender',
00056 'postcode' => 'Postal Code',
00057 'country' => 'Country',
00058 'language' => 'Language',
00059 'timezone' => 'Time Zone');
00060
00065 function Auth_OpenID_checkFieldName($field_name)
00066 {
00067 global $Auth_OpenID_sreg_data_fields;
00068
00069 if (!in_array($field_name, array_keys($Auth_OpenID_sreg_data_fields))) {
00070 return false;
00071 }
00072 return true;
00073 }
00074
00075
00076
00077 define('Auth_OpenID_SREG_NS_URI_1_0', 'http://openid.net/sreg/1.0');
00078
00079
00080
00081 define('Auth_OpenID_SREG_NS_URI_1_1', 'http://openid.net/extensions/sreg/1.1');
00082
00083
00084
00085
00086 define('Auth_OpenID_SREG_NS_URI', Auth_OpenID_SREG_NS_URI_1_1);
00087
00088 Auth_OpenID_registerNamespaceAlias(Auth_OpenID_SREG_NS_URI_1_1, 'sreg');
00089
00097 function Auth_OpenID_supportsSReg(&$endpoint)
00098 {
00099 return ($endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_1) ||
00100 $endpoint->usesExtension(Auth_OpenID_SREG_NS_URI_1_0));
00101 }
00102
00109 class Auth_OpenID_SRegBase extends Auth_OpenID_Extension {
00125 function _getSRegNS(&$message)
00126 {
00127 $alias = null;
00128 $found_ns_uri = null;
00129
00130
00131
00132 foreach (array(Auth_OpenID_SREG_NS_URI_1_1,
00133 Auth_OpenID_SREG_NS_URI_1_0) as $sreg_ns_uri) {
00134 $alias = $message->namespaces->getAlias($sreg_ns_uri);
00135 if ($alias !== null) {
00136 $found_ns_uri = $sreg_ns_uri;
00137 break;
00138 }
00139 }
00140
00141 if ($alias === null) {
00142
00143
00144 $found_ns_uri = Auth_OpenID_SREG_NS_URI_1_1;
00145 if ($message->namespaces->addAlias(Auth_OpenID_SREG_NS_URI_1_1,
00146 'sreg') === null) {
00147
00148
00149
00150 return null;
00151 }
00152 }
00153
00154 return $found_ns_uri;
00155 }
00156 }
00157
00169 class Auth_OpenID_SRegRequest extends Auth_OpenID_SRegBase {
00170
00171 var $ns_alias = 'sreg';
00172
00176 function build($required=null, $optional=null,
00177 $policy_url=null,
00178 $sreg_ns_uri=Auth_OpenID_SREG_NS_URI,
00179 $cls='Auth_OpenID_SRegRequest')
00180 {
00181 $obj = new $cls();
00182
00183 $obj->required = array();
00184 $obj->optional = array();
00185 $obj->policy_url = $policy_url;
00186 $obj->ns_uri = $sreg_ns_uri;
00187
00188 if ($required) {
00189 if (!$obj->requestFields($required, true, true)) {
00190 return null;
00191 }
00192 }
00193
00194 if ($optional) {
00195 if (!$obj->requestFields($optional, false, true)) {
00196 return null;
00197 }
00198 }
00199
00200 return $obj;
00201 }
00202
00216 function fromOpenIDRequest($request, $cls='Auth_OpenID_SRegRequest')
00217 {
00218
00219 $obj = call_user_func_array(array($cls, 'build'),
00220 array(null, null, null, Auth_OpenID_SREG_NS_URI, $cls));
00221
00222
00223
00224 $m = $request->message;
00225
00226 $obj->ns_uri = $obj->_getSRegNS($m);
00227 $args = $m->getArgs($obj->ns_uri);
00228
00229 if ($args === null || Auth_OpenID::isFailure($args)) {
00230 return null;
00231 }
00232
00233 $obj->parseExtensionArgs($args);
00234
00235 return $obj;
00236 }
00237
00262 function parseExtensionArgs($args, $strict=false)
00263 {
00264 foreach (array('required', 'optional') as $list_name) {
00265 $required = ($list_name == 'required');
00266 $items = Auth_OpenID::arrayGet($args, $list_name);
00267 if ($items) {
00268 foreach (explode(',', $items) as $field_name) {
00269 if (!$this->requestField($field_name, $required, $strict)) {
00270 if ($strict) {
00271 return false;
00272 }
00273 }
00274 }
00275 }
00276 }
00277
00278 $this->policy_url = Auth_OpenID::arrayGet($args, 'policy_url');
00279
00280 return true;
00281 }
00282
00287 function allRequestedFields()
00288 {
00289 return array_merge($this->required, $this->optional);
00290 }
00291
00295 function wereFieldsRequested()
00296 {
00297 return count($this->allRequestedFields());
00298 }
00299
00303 function contains($field_name)
00304 {
00305 return (in_array($field_name, $this->required) ||
00306 in_array($field_name, $this->optional));
00307 }
00308
00320 function requestField($field_name,
00321 $required=false, $strict=false)
00322 {
00323 if (!Auth_OpenID_checkFieldName($field_name)) {
00324 return false;
00325 }
00326
00327 if ($strict) {
00328 if ($this->contains($field_name)) {
00329 return false;
00330 }
00331 } else {
00332 if (in_array($field_name, $this->required)) {
00333 return true;
00334 }
00335
00336 if (in_array($field_name, $this->optional)) {
00337 if ($required) {
00338 unset($this->optional[array_search($field_name,
00339 $this->optional)]);
00340 } else {
00341 return true;
00342 }
00343 }
00344 }
00345
00346 if ($required) {
00347 $this->required[] = $field_name;
00348 } else {
00349 $this->optional[] = $field_name;
00350 }
00351
00352 return true;
00353 }
00354
00366 function requestFields($field_names, $required=false, $strict=false)
00367 {
00368 if (!is_array($field_names)) {
00369 return false;
00370 }
00371
00372 foreach ($field_names as $field_name) {
00373 if (!$this->requestField($field_name, $required, $strict=$strict)) {
00374 return false;
00375 }
00376 }
00377
00378 return true;
00379 }
00380
00389 function getExtensionArgs()
00390 {
00391 $args = array();
00392
00393 if ($this->required) {
00394 $args['required'] = implode(',', $this->required);
00395 }
00396
00397 if ($this->optional) {
00398 $args['optional'] = implode(',', $this->optional);
00399 }
00400
00401 if ($this->policy_url) {
00402 $args['policy_url'] = $this->policy_url;
00403 }
00404
00405 return $args;
00406 }
00407 }
00408
00417 class Auth_OpenID_SRegResponse extends Auth_OpenID_SRegBase {
00418
00419 var $ns_alias = 'sreg';
00420
00421 function Auth_OpenID_SRegResponse($data=null,
00422 $sreg_ns_uri=Auth_OpenID_SREG_NS_URI)
00423 {
00424 if ($data === null) {
00425 $this->data = array();
00426 } else {
00427 $this->data = $data;
00428 }
00429
00430 $this->ns_uri = $sreg_ns_uri;
00431 }
00432
00445 function extractResponse($request, $data)
00446 {
00447 $obj = new Auth_OpenID_SRegResponse();
00448 $obj->ns_uri = $request->ns_uri;
00449
00450 foreach ($request->allRequestedFields() as $field) {
00451 $value = Auth_OpenID::arrayGet($data, $field);
00452 if ($value !== null) {
00453 $obj->data[$field] = $value;
00454 }
00455 }
00456
00457 return $obj;
00458 }
00459
00474 function fromSuccessResponse(&$success_response, $signed_only=true)
00475 {
00476 global $Auth_OpenID_sreg_data_fields;
00477
00478 $obj = new Auth_OpenID_SRegResponse();
00479 $obj->ns_uri = $obj->_getSRegNS($success_response->message);
00480
00481 if ($signed_only) {
00482 $args = $success_response->getSignedNS($obj->ns_uri);
00483 } else {
00484 $args = $success_response->message->getArgs($obj->ns_uri);
00485 }
00486
00487 if ($args === null || Auth_OpenID::isFailure($args)) {
00488 return null;
00489 }
00490
00491 foreach ($Auth_OpenID_sreg_data_fields as $field_name => $desc) {
00492 if (in_array($field_name, array_keys($args))) {
00493 $obj->data[$field_name] = $args[$field_name];
00494 }
00495 }
00496
00497 return $obj;
00498 }
00499
00500 function getExtensionArgs()
00501 {
00502 return $this->data;
00503 }
00504
00505
00506 function get($field_name, $default=null)
00507 {
00508 if (!Auth_OpenID_checkFieldName($field_name)) {
00509 return null;
00510 }
00511
00512 return Auth_OpenID::arrayGet($this->data, $field_name, $default);
00513 }
00514
00515 function contents()
00516 {
00517 return $this->data;
00518 }
00519 }
00520
00521 ?>