| 1 |
<?php
|
| 2 |
// $Id: openid_ax.inc,v 1.12 2008/07/12 13:18:12 anshuprateek Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* OpenID Attribute Exchange utility functions.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* openid_ax_request() could be a redundant function
|
| 11 |
* TODO ANSHU Check at the end of code if we need this function
|
| 12 |
*/
|
| 13 |
function openid_ax_required($request) {
|
| 14 |
if(in_array('http://openid.net/srv/ax/1.0',$request))
|
| 15 |
return TRUE;
|
| 16 |
else
|
| 17 |
return FALSE;
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Process the $request to filter out the AX elements
|
| 22 |
* Check if its a fetch or a store request
|
| 23 |
*
|
| 24 |
* @param $request
|
| 25 |
* Associative array of requests send to the OP
|
| 26 |
* @return array
|
| 27 |
* The corresponding response message or nothing if openid.ax.mode is not set
|
| 28 |
*/
|
| 29 |
function openid_ax_process($request) {
|
| 30 |
$alias = _openid_ax_namespace_alias($request);
|
| 31 |
$request_ax = _openid_ax_namespace_request($request);
|
| 32 |
if(isset($request_ax[openid.$alias.mode])) {
|
| 33 |
switch($request_ax[openid.$alias.mode]) {
|
| 34 |
case 'fetch_request':
|
| 35 |
$response_ax = openid_ax_fetch_request($request_ax);
|
| 36 |
module_load_include('pages.inc','openid_ax');
|
| 37 |
return drupal_get_form('openid_ax_form',$response_ax);
|
| 38 |
break;
|
| 39 |
case 'store_request':
|
| 40 |
return openid_ax_store_request($request_ax);
|
| 41 |
break;
|
| 42 |
}
|
| 43 |
}
|
| 44 |
//if openid.ax.mode is not set, do nothing
|
| 45 |
return;
|
| 46 |
}
|
| 47 |
|
| 48 |
/**
|
| 49 |
* Find out the AX namespace alias used by the Relying Party
|
| 50 |
* @param $request
|
| 51 |
* Associative array of requests send to the OP
|
| 52 |
* @return .alias.
|
| 53 |
* A string containing initial and trailing dots.
|
| 54 |
*/
|
| 55 |
function _openid_ax_namespace_alias($request) {
|
| 56 |
foreach($request as $key => $val) {
|
| 57 |
$request[$key] = urldecode($val);
|
| 58 |
}
|
| 59 |
$alias_key = array_search('http://openid.net/srv/ax/1.0',$request);
|
| 60 |
if(isset($request['openid.return_to'])) {
|
| 61 |
$_SESSION['openid_ax']['return_to'] = $request['openid.return_to'];
|
| 62 |
}
|
| 63 |
if(isset($request['openid.realm'])) {
|
| 64 |
$_SESSION['openid_ax']['realm'] = $request['openid.realm'];
|
| 65 |
}
|
| 66 |
$alias = strrchr($alias_key,'.');//returns .alias
|
| 67 |
$alias .= '.';
|
| 68 |
return $alias;
|
| 69 |
}
|
| 70 |
|
| 71 |
/**
|
| 72 |
* Separate the AX requests from the rest of $request
|
| 73 |
* @param $request
|
| 74 |
* Associative array of requests send to the OP
|
| 75 |
* @return
|
| 76 |
* Associative array with only requests containing openid.ns.axalias
|
| 77 |
*/
|
| 78 |
function _openid_ax_namespace_request($request) {
|
| 79 |
foreach($request as $key => $val) {
|
| 80 |
$request[$key] = urldecode($val);
|
| 81 |
}
|
| 82 |
$alias_key = array_search('http://openid.net/srv/ax/1.0',$request);
|
| 83 |
$request_ax = array();
|
| 84 |
$request_ax[$alias_key] = $request[$alias_key];
|
| 85 |
$alias = _openid_ax_namespace_alias($request);
|
| 86 |
foreach($request as $var=>$val) {
|
| 87 |
if(strpos($var,$alias)) {
|
| 88 |
$request_ax[$var] = $val;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
return $request_ax;
|
| 92 |
}
|
| 93 |
|
| 94 |
/**
|
| 95 |
* Get the list of attribute identifiers from $request_ax
|
| 96 |
* @param $request_ax
|
| 97 |
* Associative array with only requests containing openid.ns.axalias
|
| 98 |
* @return
|
| 99 |
* Array containing placeholder URI of the identifiers.
|
| 100 |
* It has been escaped here otherwise difficult to escape and form a proper SQL query
|
| 101 |
* later using inbuilt drupal functions.
|
| 102 |
* */
|
| 103 |
function _openid_ax_identifiers($request_ax) {
|
| 104 |
$alias = _openid_ax_namespace_alias($request_ax);
|
| 105 |
$identifier = array();
|
| 106 |
foreach($request_ax as $var=>$val) {
|
| 107 |
if(strpos($var,'type')) {
|
| 108 |
$identifier[] = mysql_escape_string($val);
|
| 109 |
}
|
| 110 |
}
|
| 111 |
return $identifier;
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Get the ax_id from table openid_ax_attributes
|
| 116 |
* @param $identifier
|
| 117 |
* Array containing placeholder URI of the identifiers
|
| 118 |
* @return
|
| 119 |
* Associative array containing requested identifers=>ax_id
|
| 120 |
* */
|
| 121 |
function _openid_ax_id($identifier) {
|
| 122 |
$identifiers = (implode("','",$identifier));
|
| 123 |
$identifiers_id = db_query("SELECT * FROM {openid_ax_attributes} WHERE identifier IN ('$identifiers')");
|
| 124 |
while($row = db_fetch_array($identifiers_id)) {
|
| 125 |
$attr_axid[$row['identifier']] = $row['ax_id'];
|
| 126 |
}
|
| 127 |
|
| 128 |
return $attr_axid;
|
| 129 |
}
|
| 130 |
|
| 131 |
/**
|
| 132 |
*Process the fetch_request calls.
|
| 133 |
* @param array $request
|
| 134 |
* Request array containing the fetch_request
|
| 135 |
* @return array
|
| 136 |
* fetch_response
|
| 137 |
*/
|
| 138 |
function openid_ax_fetch_request($request_ax) {
|
| 139 |
global $user;
|
| 140 |
// If the user is not yet logged in, redirect to the login page before continuing.
|
| 141 |
if (!$user->uid) {
|
| 142 |
$_SESSION['openid_ax']['request'] = $request_ax;
|
| 143 |
drupal_goto('user/login', 'destination=openid/ax/continue');
|
| 144 |
}
|
| 145 |
$attr_axid = _openid_ax_id(_openid_ax_identifiers($request_ax));
|
| 146 |
$values = db_query("SELECT a.ax_values, ax_id FROM {openid_ax_values} a WHERE uid=%d AND ax_id IN (".implode(",",$attr_axid).")",$user->uid);
|
| 147 |
$axid_value = array();
|
| 148 |
while($axvalues = db_fetch_array($values)) {
|
| 149 |
$axid_value[] = $axvalues['ax_id'];
|
| 150 |
$attribute_value[] = $axvalues['ax_values'];
|
| 151 |
}
|
| 152 |
$ax_response = array();
|
| 153 |
$alias = _openid_ax_namespace_alias($request_ax);
|
| 154 |
$alias_key = array_search('http://openid.net/srv/ax/1.0',$request_ax);
|
| 155 |
$ax_response[$alias_key] = $request_ax[$alias_key];
|
| 156 |
$ax_response['openid'.$alias.'mode'] = 'fetch_response';
|
| 157 |
$count_alias = array();
|
| 158 |
foreach($request_ax as $var=>$val) {
|
| 159 |
if(strpos($var,'type')) {
|
| 160 |
$ax_response[$var] = $val;
|
| 161 |
}
|
| 162 |
elseif(strpos($var,'count')) {
|
| 163 |
$count_alias[strrchr($var,'.')] = $val;
|
| 164 |
}
|
| 165 |
}
|
| 166 |
foreach($request_ax as $var=>$val) {
|
| 167 |
$response_val = array();
|
| 168 |
$key_value = (str_replace('type','value',$var));
|
| 169 |
$key_count = (str_replace('type','count',$var));
|
| 170 |
$axid_keys = array_keys($axid_value,$attr_axid[$val]);
|
| 171 |
if(count($axid_keys)>1) {
|
| 172 |
foreach($axid_keys as $key) {
|
| 173 |
$response_val[] = $attribute_value[$key];
|
| 174 |
}
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
$response_val[] = $attribute_value[$axid_keys[0]];
|
| 178 |
}
|
| 179 |
if(strpos($var,'type')) {
|
| 180 |
$attr_alias = strrchr($var,'.');
|
| 181 |
//If a value was not supplied or available from the user,
|
| 182 |
//the associated "openid.ax.value.<alias>" field SHOULD NOT be included by the OP in the fetch response.
|
| 183 |
//An "openid.ax.count.<alias>" with a value of "0" together with its corresponding
|
| 184 |
//"openid.ax.type.<alias>" field MAY be included to explicitly state
|
| 185 |
//that no values are provided for an attribute.
|
| 186 |
if($response_val[0] != ''||1) {//TODO Check what conditions to be used here!
|
| 187 |
//The name of array_key_exists() function is key_exists() in PHP 4.0.6. Drupal requires min 4.3.3
|
| 188 |
if((array_key_exists($attr_alias,$count_alias))===FALSE) {
|
| 189 |
$ax_response[$key_value] = $response_val[0];
|
| 190 |
}
|
| 191 |
else {
|
| 192 |
$num = count($response_val);
|
| 193 |
//OpenID Providers MAY return less than or the exact number of values speficied by this field
|
| 194 |
//for the associated attribute,
|
| 195 |
//but MUST NOT return more than the number of requested values for the attribute.
|
| 196 |
if($count_alias[$attr_alias] != 'unlimited') {
|
| 197 |
($num>($count_alias[$attr_alias]))?$num = $count_alias[$attr_alias]:$num;
|
| 198 |
}
|
| 199 |
$ax_response[$key_count] = $num;
|
| 200 |
for($i=0;$i<$num;$i++) {
|
| 201 |
$ax_response[$key_value.'.'.($i+1)] = $response_val[$i];
|
| 202 |
}
|
| 203 |
}
|
| 204 |
}
|
| 205 |
else {
|
| 206 |
$ax_response[$key_count] = 0;
|
| 207 |
}
|
| 208 |
}
|
| 209 |
}
|
| 210 |
return $ax_response;
|
| 211 |
}
|
| 212 |
|
| 213 |
/**
|
| 214 |
* Process the store_request
|
| 215 |
* @param $request_ax
|
| 216 |
* http request only containing the AX namespace alias
|
| 217 |
* @return $ax_response
|
| 218 |
* store_response
|
| 219 |
*/
|
| 220 |
function openid_ax_store_request($request_ax) {
|
| 221 |
global $user;
|
| 222 |
// If the user is not yet logged in, redirect to the login page before continuing.
|
| 223 |
if (!$user->uid) {
|
| 224 |
$_SESSION['openid_ax']['request'] = $request_ax;
|
| 225 |
drupal_goto('user/login', 'destination=openid/ax/continue');
|
| 226 |
}
|
| 227 |
return drupal_get_form('openid_store_info_form',$request_ax);
|
| 228 |
}
|
| 229 |
|
| 230 |
function openid_store_info_form(&$form_state, &$request_ax) {
|
| 231 |
$form = array();
|
| 232 |
$realm = $form_state['post']['openid_realm'];
|
| 233 |
if($realm == '') {
|
| 234 |
$realm = $_SESSION['openid_ax']['realm'];
|
| 235 |
unset($_SESSION['openid_ax']['realm']);
|
| 236 |
}
|
| 237 |
$form['intro'] = array(
|
| 238 |
'#type' => 'markup',
|
| 239 |
'#value' => t('%site is requesting to store the following information into your profile. You want to continue?', array('%site' => $realm))
|
| 240 |
);
|
| 241 |
foreach($request_ax as $key => $value) {
|
| 242 |
if(strstr($key,'value')){
|
| 243 |
$form[$key] = array(
|
| 244 |
'#type' => 'textfield',
|
| 245 |
'#title' => t(substr(strrchr($key,'value.'),6)),
|
| 246 |
'#value' => $value,
|
| 247 |
'#size' => 14,
|
| 248 |
'#maxlength' => 100
|
| 249 |
);
|
| 250 |
}
|
| 251 |
else {
|
| 252 |
$form[$key] = array(
|
| 253 |
'#type' => 'hidden',
|
| 254 |
'#title' => $key,
|
| 255 |
'#value' => $value
|
| 256 |
);
|
| 257 |
}
|
| 258 |
}
|
| 259 |
$essentials = array(
|
| 260 |
'openid.return_to' => $form_state['post']['openid.return_to'],
|
| 261 |
'openid.realm' => $realm
|
| 262 |
);
|
| 263 |
foreach($essentials as $key => $value) {
|
| 264 |
$form[$key] = array (
|
| 265 |
'#type' => 'hidden',
|
| 266 |
'#name' => $key,
|
| 267 |
'#value' => $value
|
| 268 |
);
|
| 269 |
}
|
| 270 |
$form['submit'] = array(
|
| 271 |
'#type' => 'submit',
|
| 272 |
'#value' => t('Submit'),
|
| 273 |
'#submit' => array('openid_store_info_form_submit')
|
| 274 |
);
|
| 275 |
$form['cancel'] = array(
|
| 276 |
'#type' => 'submit',
|
| 277 |
'#value' => t('Cancel'),
|
| 278 |
'#submit' => array('openid_store_info_form_submit_cancel')
|
| 279 |
);
|
| 280 |
return $form;
|
| 281 |
}
|
| 282 |
|
| 283 |
function openid_store_info_form_submit(&$form, $form_state) {
|
| 284 |
return openid_ax_store_process($form_state['values']);
|
| 285 |
}
|
| 286 |
|
| 287 |
function openid_store_info_form_submit_cancel(&$form, $form_state) {
|
| 288 |
$user_agreed = FALSE;
|
| 289 |
return openid_ax_store_process($form_state['values'], $user_agreed);
|
| 290 |
}
|
| 291 |
|
| 292 |
function openid_ax_store_process($form_state, $user_agreed = TRUE) {
|
| 293 |
global $user;
|
| 294 |
$request_ax = $form_state;
|
| 295 |
$alias = _openid_ax_namespace_alias($request_ax);
|
| 296 |
if($user_agreed){
|
| 297 |
$attr_axid = _openid_ax_id(_openid_ax_identifiers($request_ax));
|
| 298 |
foreach($request_ax as $var=>$val) {
|
| 299 |
$attr_alias = strrchr($var,'.');
|
| 300 |
if(strpos($var,'type')) {
|
| 301 |
$ax_values[$attr_axid[$val]] = $attr_alias;
|
| 302 |
}
|
| 303 |
elseif(strpos($var,'count')) {
|
| 304 |
$count_alias[$attr_alias] = $val;
|
| 305 |
}
|
| 306 |
elseif(strpos($var,'value')) {
|
| 307 |
if(!(strpos(substr(strstr($var,'value.'),6),'.'))) {
|
| 308 |
$ax_values[array_search($attr_alias,$ax_values)] = $val;
|
| 309 |
}
|
| 310 |
else {
|
| 311 |
$attr_alias_count = $attr_alias;
|
| 312 |
$count_pos = strrpos($var,$attr_alias);
|
| 313 |
$var1 = substr($var,0,$count_pos);
|
| 314 |
$attr_alias = strrchr($var1,'.');
|
| 315 |
$ax_values[array_search($attr_alias,$ax_values).$attr_alias_count] = $val;
|
| 316 |
$to_remove[] = array_search($attr_alias,$ax_values);
|
| 317 |
}
|
| 318 |
}
|
| 319 |
}
|
| 320 |
foreach($to_remove as $del) {
|
| 321 |
unset($ax_values[$del]);
|
| 322 |
}
|
| 323 |
foreach($ax_values as $ax_id=>$value) {
|
| 324 |
$value = trim($value);
|
| 325 |
if($value != '') {
|
| 326 |
$res = db_query("INSERT INTO {openid_ax_values} (ax_id, uid, ax_values) VALUES ('%d','%d','%s')",$ax_id,$user->uid,$value);
|
| 327 |
if($res) {
|
| 328 |
$result[] = $res;
|
| 329 |
}
|
| 330 |
}
|
| 331 |
}
|
| 332 |
$alias_key = array_search('http://openid.net/srv/ax/1.0',$request_ax);
|
| 333 |
$ax_store_response[$alias_key] = $request_ax[$alias_key];
|
| 334 |
if(count($result) == count($ax_values)) {
|
| 335 |
$ax_store_response['openid'.$alias.'mode'] = 'store_response_success';
|
| 336 |
}
|
| 337 |
else {
|
| 338 |
$ax_store_response['openid'.$alias.'mode'] = 'store_response_failure';
|
| 339 |
}
|
| 340 |
}
|
| 341 |
else {
|
| 342 |
$alias_key = array_search('http://openid.net/srv/ax/1.0',$request_ax);
|
| 343 |
$ax_store_response[$alias_key] = $request_ax[$alias_key];
|
| 344 |
$ax_store_response['openid'.$alias.'mode'] = 'store_response_failure';
|
| 345 |
}
|
| 346 |
$destination = ($request_ax['openid.return_to'] != '')?$request_ax['openid.return_to']:$request_ax['openid.realm'];
|
| 347 |
drupal_goto($destination, $ax_store_response) ;
|
| 348 |
|
| 349 |
}
|
| 350 |
|
| 351 |
function openid_ax_fetch_response($response) {
|
| 352 |
$alias = _openid_ax_namespace_alias($response);
|
| 353 |
$ax_user_response = _openid_ax_namespace_request($response);
|
| 354 |
foreach($ax_user_response as $key => $val) {
|
| 355 |
if(strpos($key,'value.')) {
|
| 356 |
//value.alias.1 or value.alias format
|
| 357 |
$var_name = substr(strstr($key,'value.'),6);
|
| 358 |
if(strpos($var_name,'.')) {
|
| 359 |
$var_name_pure = substr($var_name,0,strpos($var_name,'.'));
|
| 360 |
}
|
| 361 |
else {
|
| 362 |
$var_name_pure = $var_name;
|
| 363 |
}
|
| 364 |
$property_count[$var_name_pure]++;
|
| 365 |
}
|
| 366 |
}
|
| 367 |
if(strstr($response['op'],'No')) {
|
| 368 |
foreach($property_count as $property => $value) {
|
| 369 |
if($value == 1) {
|
| 370 |
unset($ax_user_response['openid'.$alias.'value.'.$property]);
|
| 371 |
$ax_user_response['openid'.$alias.'count.'.$property] = 0;
|
| 372 |
}
|
| 373 |
else {
|
| 374 |
for($key=0;$key<$value;$key++) {
|
| 375 |
unset($ax_user_response['openid'.$alias.'value.'.$property.'.'.($key+1)]);
|
| 376 |
}
|
| 377 |
$ax_user_response['openid'.$alias.'count.'.$property] = 0;
|
| 378 |
}
|
| 379 |
}
|
| 380 |
}
|
| 381 |
else{
|
| 382 |
foreach($property_count as $property => $value) {
|
| 383 |
if($value == 1) {
|
| 384 |
if($ax_user_response['openid'.$alias.'value.'.$property]=='') {
|
| 385 |
unset($ax_user_response['openid'.$alias.'value.'.$property]);
|
| 386 |
$ax_user_response['openid'.$alias.'count.'.$property] = 0;
|
| 387 |
}
|
| 388 |
}
|
| 389 |
else {
|
| 390 |
//$property_set_indexes = indexes of all value.alias.num's which are != ''
|
| 391 |
$new_key=0;$old_key=0;
|
| 392 |
for(;$old_key<=$value;$old_key++) {
|
| 393 |
if(trim($ax_user_response['openid'.$alias.'value.'.$property.'.'.$old_key]) != '') {
|
| 394 |
$property_set_indexes[$new_key]=$old_key;
|
| 395 |
$new_key++;
|
| 396 |
}
|
| 397 |
}
|
| 398 |
if($new_key != $old_key) {
|
| 399 |
for($key=0;$key<$new_key;$key++) {
|
| 400 |
$ax_user_response['openid'.$alias.'value.'.$property.'.'.($key+1)] = $ax_user_response['openid'.$alias.'value.'.$property.'.'.$property_set_indexes[$key]];
|
| 401 |
}
|
| 402 |
for($key=0;$key<$old_key-$new_key;$key++) {
|
| 403 |
unset($ax_user_response['openid'.$alias.'value.'.$property.'.'.($new_key+$key+1)]);
|
| 404 |
}
|
| 405 |
$ax_user_response['openid'.$alias.'count.'.$property] = $new_key;
|
| 406 |
}
|
| 407 |
}
|
| 408 |
}
|
| 409 |
}
|
| 410 |
return $ax_user_response;
|
| 411 |
}
|