| 1 |
<?php
|
| 2 |
// $Id: openid_ax.pages.inc,v 1.14 2008/08/10 16:38:13 anshuprateek Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Page callbacks for OpenID AX.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Primary endpoint callback - serves op_endpoint.
|
| 11 |
*
|
| 12 |
* @param array $request (Optional) request parameters.
|
| 13 |
*/
|
| 14 |
function openid_ax_endpoint($request = array()) {
|
| 15 |
module_load_include('inc', 'openid');
|
| 16 |
module_load_include('inc', 'openid_ax');
|
| 17 |
if (count($request) == 0) {
|
| 18 |
$request = _openid_response();
|
| 19 |
}
|
| 20 |
$ax_request = openid_ax_process($request);
|
| 21 |
return $ax_request;
|
| 22 |
}
|
| 23 |
|
| 24 |
/**
|
| 25 |
* Menu callback to continue authentication process after user login. This
|
| 26 |
* callback is encountered when a user tries to login to an RP but does not yet
|
| 27 |
* have a valid local session
|
| 28 |
*/
|
| 29 |
function openid_ax_continue() {
|
| 30 |
if (isset($_SESSION['openid_ax']['request'])) {
|
| 31 |
$request_ax = $_SESSION['openid_ax']['request'];
|
| 32 |
unset($_SESSION['openid_ax']['request']);
|
| 33 |
return openid_ax_endpoint($request_ax);
|
| 34 |
}
|
| 35 |
else {
|
| 36 |
drupal_set_message(t('Session expired'));
|
| 37 |
drupal_goto();
|
| 38 |
}
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Form for user interaction before sending AX values to RP
|
| 43 |
*/
|
| 44 |
function openid_ax_form(&$form_state,&$ax_response) {
|
| 45 |
global $user;
|
| 46 |
$form_state['ax_response'] = $ax_response;
|
| 47 |
$realm = $form_state['post']['openid_realm'];
|
| 48 |
if($realm == '') {
|
| 49 |
$realm = $_SESSION['openid_ax']['realm'];
|
| 50 |
unset($_SESSION['openid_ax']['realm']);
|
| 51 |
}
|
| 52 |
$form = array();
|
| 53 |
$form['intro'] = array(
|
| 54 |
'#type' => 'markup',
|
| 55 |
'#value' => '<p>'. t('Your following details are being send to %site, would you like to continue?', array('%site' => $realm)) . '</p>'
|
| 56 |
);
|
| 57 |
foreach($ax_response as $name=>$value) {
|
| 58 |
if(strstr($name,'value')){
|
| 59 |
$form[$name] = array(
|
| 60 |
'#type' => 'textfield',
|
| 61 |
'#title' => t(substr(strrchr($name,'value.'),6)),
|
| 62 |
'#value' => $value,
|
| 63 |
'#size' => 14,
|
| 64 |
'#maxlength' => 100
|
| 65 |
);
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$form[$name] = array(
|
| 69 |
'#type' => 'hidden',
|
| 70 |
'#title' => $name,
|
| 71 |
'#value' => $value
|
| 72 |
);
|
| 73 |
}
|
| 74 |
}
|
| 75 |
$form['#action'] = url('openid/ax/send');
|
| 76 |
$form['submit'] = array(
|
| 77 |
'#type' => 'submit',
|
| 78 |
'#value' => t('Yes; Send my data'),
|
| 79 |
);
|
| 80 |
$form['cancel'] = array(
|
| 81 |
'#type' => 'submit',
|
| 82 |
'#value' => t('No! Don\'t send my data'),
|
| 83 |
'#submit' => array('openid_ax_form_submit_cancel')
|
| 84 |
);
|
| 85 |
return $form;
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Submit handler for openid_ax_form through page call back for openid_ax_send
|
| 90 |
*/
|
| 91 |
function openid_ax_send() {
|
| 92 |
module_load_include('inc', 'openid');
|
| 93 |
module_load_include('inc', 'openid_ax');
|
| 94 |
$response = _openid_response();
|
| 95 |
$ax_response = openid_ax_fetch_response($response);
|
| 96 |
drupal_goto($_SESSION['openid_ax']['return_to'],$ax_response);
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Menu callback for openid_ax_persona
|
| 101 |
*/
|
| 102 |
function openid_ax_persona() {
|
| 103 |
return drupal_get_form('openid_ax_persona_form');
|
| 104 |
}
|
| 105 |
|
| 106 |
/**
|
| 107 |
* Form for creating/editing a persona
|
| 108 |
*/
|
| 109 |
function openid_ax_persona_form(&$form_state) {
|
| 110 |
global $user;
|
| 111 |
$form = array();
|
| 112 |
if(isset($form_state['storage']['personas'])) {
|
| 113 |
$personas = $form_state['storage']['personas'];
|
| 114 |
}
|
| 115 |
elseif(isset($_GET['persona'])) {
|
| 116 |
$personas = $_GET['persona'];
|
| 117 |
}
|
| 118 |
else {
|
| 119 |
$personas = 0;
|
| 120 |
}
|
| 121 |
$persona = db_query("SELECT * FROM {openid_ax_persona} where uid=%d", $user->uid);
|
| 122 |
$options = array(t('Default'), 'openid_ax_create_new' =>t('Create new'), 'manage_ax_personas' => t('Manage AX Personas'));
|
| 123 |
while($persona_value = db_fetch_array($persona)) {
|
| 124 |
$options[$persona_value['persona_id']] = $persona_value['persona_name'];
|
| 125 |
}
|
| 126 |
if(count($options) == 3) {
|
| 127 |
unset($options['manage_ax_personas']);
|
| 128 |
}
|
| 129 |
$form['personas'] = array(
|
| 130 |
'#type' => 'select',
|
| 131 |
'#title' => 'Personas',
|
| 132 |
'#options' => $options,
|
| 133 |
'#default_value' => $personas
|
| 134 |
);
|
| 135 |
$form['select'] = array(
|
| 136 |
'#type' => 'submit',
|
| 137 |
'#value' => 'Select Persona',
|
| 138 |
'#submit' => array('openid_ax_persona_form_submit_select_persona')
|
| 139 |
);
|
| 140 |
$identifiers = db_query("SELECT * FROM {openid_ax_attributes}");
|
| 141 |
while($id = db_fetch_array($identifiers)) {
|
| 142 |
$identifier[$id['ax_id']] = $id['identifier'];
|
| 143 |
}
|
| 144 |
$ax_values = db_query("SELECT * FROM {openid_ax_values} WHERE uid=%d and persona_id='%d'",$user->uid, $personas);
|
| 145 |
$valueExist = array();
|
| 146 |
while($row = db_fetch_array($ax_values)) {
|
| 147 |
$valueExist[] = $row['ax_id'];
|
| 148 |
$form[$row['vid']] = array(
|
| 149 |
'#type' => 'textfield',
|
| 150 |
'#title' => t($identifier[$row['ax_id']]),
|
| 151 |
'#default_value' => $row['ax_values'],
|
| 152 |
'#size' => 25,
|
| 153 |
'#maxlength' => 100
|
| 154 |
);
|
| 155 |
$form['has_value'.$row['vid']] = array(
|
| 156 |
'#type' => 'hidden',
|
| 157 |
'#value' => $row['vid'],
|
| 158 |
);
|
| 159 |
static $count = 0;
|
| 160 |
$count++;
|
| 161 |
static $submit_btn = 1;
|
| 162 |
if($count == 10) {
|
| 163 |
$form['submit'.$submit_btn] = array(
|
| 164 |
'#type' => 'submit',
|
| 165 |
'#value' => t('Submit'),
|
| 166 |
);
|
| 167 |
$count = 0;
|
| 168 |
$submit_btn++;
|
| 169 |
}
|
| 170 |
$hasValues = TRUE;
|
| 171 |
}
|
| 172 |
if($hasValues){
|
| 173 |
$form['intro'] = array(
|
| 174 |
'#type' => 'markup',
|
| 175 |
'#value' => t('<strong>To delete a value, delete the value in the particular field and then submit</strong>'),
|
| 176 |
'#weight' => -1
|
| 177 |
);
|
| 178 |
}
|
| 179 |
foreach($identifier as $key => $value) {
|
| 180 |
if(!(in_array($key, $valueExist))){
|
| 181 |
$form['vac'.$key] = array(
|
| 182 |
'#type' => 'textfield',
|
| 183 |
'#title' => t($value),
|
| 184 |
'#default_value' => '',
|
| 185 |
'#size' => 25,
|
| 186 |
'#maxlength' => 100
|
| 187 |
);
|
| 188 |
$form['no_value'.$key] = array(
|
| 189 |
'#type' => 'hidden',
|
| 190 |
'#value' => $key,
|
| 191 |
);
|
| 192 |
static $count = 0;
|
| 193 |
$count++;
|
| 194 |
static $submit_btn = 1;
|
| 195 |
if($count == 10) {
|
| 196 |
$form['submit'.$submit_btn] = array(
|
| 197 |
'#type' => 'submit',
|
| 198 |
'#value' => t('Submit'),
|
| 199 |
);
|
| 200 |
$count = 0;
|
| 201 |
$submit_btn++;
|
| 202 |
}
|
| 203 |
}
|
| 204 |
}
|
| 205 |
$form['submit'] = array(
|
| 206 |
'#type' => 'submit',
|
| 207 |
'#value' => t('Submit'),
|
| 208 |
);
|
| 209 |
return $form;
|
| 210 |
}
|
| 211 |
|
| 212 |
/**
|
| 213 |
* Select submit handler for openid_ax_form
|
| 214 |
*/
|
| 215 |
function openid_ax_persona_form_submit_select_persona(&$form, &$form_state) {
|
| 216 |
global $user;
|
| 217 |
if($form_state['values']['personas']=='openid_ax_create_new') {
|
| 218 |
drupal_goto('user/'.$user->uid.'/persona/create');
|
| 219 |
}
|
| 220 |
elseif($form_state['values']['personas']=='manage_ax_personas') {
|
| 221 |
drupal_goto('user/'.$user->uid.'/persona/manage');
|
| 222 |
}
|
| 223 |
else {
|
| 224 |
$form_state['storage']['personas'] = $form_state['values']['personas'];
|
| 225 |
}
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* Submit handler for openid_ax_persona form
|
| 230 |
*/
|
| 231 |
function openid_ax_persona_form_submit(&$form, &$form_state) {
|
| 232 |
global $user;
|
| 233 |
$result = db_query("SELECT vid FROM {openid_ax_values} WHERE uid='%d' and persona_id='%d'",$user->uid, $form_state['values']['personas']);
|
| 234 |
static $success;
|
| 235 |
$success = FALSE;
|
| 236 |
while($vid = db_fetch_array($result)) {
|
| 237 |
$ax_value = trim($form_state['values'][$vid['vid']]);
|
| 238 |
if(isset($form_state['values'][has_value.$vid['vid']])){
|
| 239 |
if($ax_value != ''){
|
| 240 |
$success = db_query("UPDATE {openid_ax_values} SET ax_values='%s' WHERE vid='%d'", $form_state['values'][$vid['vid']], $vid['vid']);
|
| 241 |
}
|
| 242 |
else {
|
| 243 |
$success = db_query("DELETE FROM {openid_ax_values} WHERE vid='%d'", $vid['vid']);
|
| 244 |
}
|
| 245 |
}
|
| 246 |
}
|
| 247 |
$ax_ids = db_query("SELECT ax_id FROM {openid_ax_attributes}");
|
| 248 |
while($ax_id=db_fetch_array($ax_ids)) {
|
| 249 |
if((isset($form_state['values'][no_value.$ax_id['ax_id']]))&&($form_state['values'][vac.$ax_id['ax_id']] != '')){
|
| 250 |
$success = db_query("INSERT INTO {openid_ax_values}(ax_id, uid, persona_id, ax_values) VALUES ('%d', '%d', '%d', '%s')",$ax_id['ax_id'], $user->uid, $form_state['values']['personas'], $form_state['values'][$ax_id['ax_id']]);
|
| 251 |
}
|
| 252 |
}
|
| 253 |
if($success) {
|
| 254 |
drupal_set_message(t('Your persona values have been saved'));
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Page callback for creating new persona
|
| 260 |
*/
|
| 261 |
function openid_ax_create_persona() {
|
| 262 |
return drupal_get_form('openid_ax_new_persona_form');
|
| 263 |
}
|
| 264 |
|
| 265 |
/**
|
| 266 |
* Form for creating new persona
|
| 267 |
*/
|
| 268 |
function openid_ax_new_persona_form() {
|
| 269 |
$form = array();
|
| 270 |
$form['persona'] = array(
|
| 271 |
'#type' =>'textfield',
|
| 272 |
'#title' => t('New Persona name'),
|
| 273 |
'#default_value' => '',
|
| 274 |
'#required' => TRUE,
|
| 275 |
'#size' => 20
|
| 276 |
);
|
| 277 |
$form['submit'] = array(
|
| 278 |
'#type' => 'submit',
|
| 279 |
'#value' => t('Submit'),
|
| 280 |
);
|
| 281 |
return $form;
|
| 282 |
}
|
| 283 |
|
| 284 |
/**
|
| 285 |
* Submit handler for openid_ax_new_persona
|
| 286 |
*/
|
| 287 |
function openid_ax_new_persona_form_submit(&$form, $form_state) {
|
| 288 |
global $user;
|
| 289 |
$persona_exists = db_result(db_query("SELECT * FROM {openid_ax_persona} WHERE uid=%d AND persona_name='%s'", $user->uid, $form_state['values']['persona']));
|
| 290 |
if($persona_exists) {
|
| 291 |
drupal_set_message(t('A persona with that name already exists. Please select another persona name.'),'error');
|
| 292 |
}
|
| 293 |
else {
|
| 294 |
$max_persona_id = db_result(db_query("SELECT MAX(persona_id) FROM {openid_ax_persona} WHERE uid='%d'", $user->uid));
|
| 295 |
$create_persona = db_query("INSERT INTO {openid_ax_persona} (uid, persona_id, persona_name) VALUES ('%d', '%d', '%s')", $user->uid, $max_persona_id+1, $form_state['values']['persona']);
|
| 296 |
if($create_persona) {
|
| 297 |
drupal_set_message(t('New Persona created'));
|
| 298 |
drupal_goto('user/'.$user->uid.'/persona');
|
| 299 |
}
|
| 300 |
}
|
| 301 |
}
|
| 302 |
|
| 303 |
/**
|
| 304 |
* Page to manage AX personas, viz, rename, delete, copy, etc.
|
| 305 |
*/
|
| 306 |
function openid_ax_manage_personas() {
|
| 307 |
global $user;
|
| 308 |
$profiles = (db_query("SELECT * FROM {openid_ax_persona} WHERE uid=%d",$user->uid));
|
| 309 |
$header = array (
|
| 310 |
'Persona Name',
|
| 311 |
'Edit Persona Values',
|
| 312 |
'Rename Persona',
|
| 313 |
'Delete Persona',
|
| 314 |
);
|
| 315 |
$rows['default'] = array (
|
| 316 |
'Default',
|
| 317 |
'<a href="edit?persona=0">Edit</a>',
|
| 318 |
'-N/A-',
|
| 319 |
'-N/A-'
|
| 320 |
);
|
| 321 |
while($profile = db_fetch_array($profiles)) {
|
| 322 |
$rows[$profile['persona_name']] = array (
|
| 323 |
$profile['persona_name'],
|
| 324 |
'<a href="edit?persona='.$profile['persona_id'].'">Edit </a>',
|
| 325 |
'<a href="rename?p='.$profile['persona_id'].'">Rename</a>',
|
| 326 |
'<a href="delete?p='.$profile['persona_id'].'">Delete</a>',
|
| 327 |
);
|
| 328 |
}
|
| 329 |
$content = theme_table($header, $rows);
|
| 330 |
$content .= drupal_get_form('copy_persona_values_form');
|
| 331 |
return $content;
|
| 332 |
}
|
| 333 |
|
| 334 |
/**
|
| 335 |
* Form for selecting from to persona for values to be copied
|
| 336 |
*/
|
| 337 |
function copy_persona_values_form() {
|
| 338 |
global $user;
|
| 339 |
$form = array();
|
| 340 |
$persona = db_query("SELECT * FROM {openid_ax_persona} WHERE uid='%d'",$user->uid);
|
| 341 |
$options = array(t('Default'));
|
| 342 |
while($persona_value = db_fetch_array($persona)) {
|
| 343 |
$options[$persona_value['persona_id']] = $persona_value['persona_name'];
|
| 344 |
}
|
| 345 |
$form['intro'] = array(
|
| 346 |
'#type' => 'markup',
|
| 347 |
'#value' => t('<b>Copy values from one persona into another:</b>'),
|
| 348 |
);
|
| 349 |
$form['from_personas'] = array(
|
| 350 |
'#type' => 'select',
|
| 351 |
'#title' => 'From',
|
| 352 |
'#options' => $options,
|
| 353 |
);
|
| 354 |
$form['to_personas'] = array(
|
| 355 |
'#type' => 'select',
|
| 356 |
'#title' => 'To',
|
| 357 |
'#options' => $options,
|
| 358 |
);
|
| 359 |
$form['submit'] = array(
|
| 360 |
'#type' => 'submit',
|
| 361 |
'#value' => t('Copy'),
|
| 362 |
);
|
| 363 |
return $form;
|
| 364 |
}
|
| 365 |
|
| 366 |
/**
|
| 367 |
* copy_persona_values form submit handler
|
| 368 |
*/
|
| 369 |
function copy_persona_values_form_submit(&$form, $form_state) {
|
| 370 |
global $user;
|
| 371 |
$target_persona = $form_state['values']['to_personas'];
|
| 372 |
$copySrc = db_query("SELECT ax_id,ax_values FROM {openid_ax_values} WHERE uid='%d' AND persona_id='%d'",$user->uid, $form_state['values']['from_personas']);
|
| 373 |
while($vals = db_fetch_array($copySrc)) {
|
| 374 |
$copyTrg = db_query("INSERT INTO {openid_ax_values}(ax_id, uid, persona_id, ax_values) VALUES ('%d','%d','%d','%s')", $vals['ax_id'], $target_persona, $user->uid, $vals['ax_values']);
|
| 375 |
}
|
| 376 |
drupal_set_message(t('Persona values copied.'));
|
| 377 |
}
|
| 378 |
|
| 379 |
/**
|
| 380 |
* Page call back for renaming persona
|
| 381 |
*/
|
| 382 |
function openid_ax_rename_personas() {
|
| 383 |
$content = drupal_get_form('openid_ax_rename_persona_form', $_GET['p']);
|
| 384 |
$content .= openid_ax_manage_personas();
|
| 385 |
return $content;
|
| 386 |
}
|
| 387 |
|
| 388 |
/**
|
| 389 |
* Form for renaming persona
|
| 390 |
*/
|
| 391 |
function openid_ax_rename_persona_form(&$form, $form_state) {
|
| 392 |
global $user;
|
| 393 |
isset($_GET['p'])?$persona_id=$_GET['p']:$persona_id=$form_state['storage']['persona_id'];
|
| 394 |
$form = array();
|
| 395 |
$persona = db_result(db_query("SELECT persona_name FROM {openid_ax_persona} WHERE persona_id='%d' AND uid='%d'",$persona_id, $user->uid));
|
| 396 |
$form['persona'] = array(
|
| 397 |
'#type' => 'textfield',
|
| 398 |
'#default_value' => $persona,
|
| 399 |
'#size' => 15,
|
| 400 |
);
|
| 401 |
$form['persona_id'] = array(
|
| 402 |
'#type' => 'hidden',
|
| 403 |
'#value' => $_GET['p'],
|
| 404 |
);
|
| 405 |
$form['submit'] = array (
|
| 406 |
'#type' => 'submit',
|
| 407 |
'#value' => 'Rename',
|
| 408 |
);
|
| 409 |
return $form;
|
| 410 |
}
|
| 411 |
|
| 412 |
/**
|
| 413 |
* Submit handler for openid_ax_rename_persona_form
|
| 414 |
*/
|
| 415 |
function openid_ax_rename_persona_form_submit(&$form, &$form_state) {
|
| 416 |
global $user;
|
| 417 |
$rename = db_query("UPDATE {openid_ax_persona} SET persona_name='%s' WHERE uid='%d' AND persona_id='%d'", $form_state['values']['persona'], $user->uid, $form_state['values']['persona_id']);
|
| 418 |
drupal_set_message(t('Your persona has been renamed'));
|
| 419 |
$form_state['storage']['persona_id'] = $form_state['values']['persona_id'];
|
| 420 |
}
|
| 421 |
|
| 422 |
/**
|
| 423 |
* Function for deleting persona
|
| 424 |
*/
|
| 425 |
function openid_ax_persona_delete() {
|
| 426 |
global $user;
|
| 427 |
$persona_id=$_GET['p'];
|
| 428 |
$persona = db_result(db_query("SELECT persona_name FROM {openid_ax_persona} WHERE persona_id='%d' AND uid='%d'",$persona_id, $user->uid));
|
| 429 |
$content = t('Are you sure you want to delete your <a href="edit?persona='.$persona_id.'">%profile</a> profile.', array('%profile' => $persona));
|
| 430 |
$content .= t(' This cannot be reversed and all your profile related details will be lost.');
|
| 431 |
$content .= drupal_get_form('openid_ax_persona_delete_confirm_form');
|
| 432 |
return $content;
|
| 433 |
}
|
| 434 |
|
| 435 |
/**
|
| 436 |
* Form for confirming if the user wants to delete the persona
|
| 437 |
*/
|
| 438 |
function openid_ax_persona_delete_confirm_form() {
|
| 439 |
$form = array ();
|
| 440 |
$form['persona_id'] = array(
|
| 441 |
'#type' => 'hidden',
|
| 442 |
'#value' => $_GET['p'],
|
| 443 |
);
|
| 444 |
$form['delete'] = array(
|
| 445 |
'#type' => 'submit',
|
| 446 |
'#value' => t('Delete'),
|
| 447 |
);
|
| 448 |
$form['cancel'] =array(
|
| 449 |
'#type' => 'submit',
|
| 450 |
'#value' => t('Cancel'),
|
| 451 |
'#submit' => array ('openid_ax_persona_delete_confirm_form_cancel'),
|
| 452 |
);
|
| 453 |
return $form;
|
| 454 |
}
|
| 455 |
|
| 456 |
/**
|
| 457 |
* Submit handler for openid_ax_persona_delete_confirm_form
|
| 458 |
*/
|
| 459 |
function openid_ax_persona_delete_confirm_form_submit($form, &$form_state) {
|
| 460 |
global $user;
|
| 461 |
$delValues = db_query("DELETE FROM {openid_ax_values} WHERE uid='%d' AND persona_id='%d'", $user->uid, $form_state['values']['persona_id']);
|
| 462 |
$deletePersona = db_query("DELETE FROM {openid_ax_persona} WHERE uid='%d' AND persona_id='%d'", $user->uid, $form_state['values']['persona_id']);
|
| 463 |
if($delValues && $deletePersona) {
|
| 464 |
drupal_set_message(t('Persona deleted.'));
|
| 465 |
drupal_goto('user/'.$user->uid.'/persona/manage');
|
| 466 |
}
|
| 467 |
}
|
| 468 |
|
| 469 |
/**
|
| 470 |
* Cancel handler for openid_ax_persona_delete_confirm_form
|
| 471 |
*/
|
| 472 |
function openid_ax_persona_delete_confirm_form_cancel() {
|
| 473 |
global $user;
|
| 474 |
drupal_goto('user/'.$user->uid.'/persona/manage');
|
| 475 |
}
|