PageRenderTime 98ms CodeModel.GetById 11ms app.highlight 76ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/rpx/rpx_c.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 1372 lines | 1255 code | 86 blank | 31 comment | 337 complexity | c145c2532d29efdcaf5af6e5a5f9d43c MD5 | raw file
   1<?php
   2function rpx_bootstrap() {
   3  if (defined('RPX_BOOT')) {
   4    return true;
   5  }
   6  define('RPX_PLUGIN_URL', plugins_url('rpx/', RPX_PATH_ROOT));
   7  define('RPX_IMAGE_URL', plugins_url('rpx/images/', RPX_PATH_ROOT));
   8  define('RPX_FILES_URL', plugins_url('rpx/files/', RPX_PATH_ROOT));
   9  if (get_option(RPX_REALM_SCHEME) == 'http'){
  10      $rpx_scheme = 'http://static.';
  11    }else{
  12      $rpx_scheme = 'https://';
  13  }
  14  define('RPX_URL_SCHEME', $rpx_scheme);
  15  define('RPX_BOOT', true);
  16  return RPX_BOOT;
  17}
  18
  19function rpx_stylesheet() {
  20  $rpx_style_url = WP_PLUGIN_URL . '/rpx/files/stylesheet.css';
  21  $rpx_style_file = WP_PLUGIN_DIR . '/rpx/files/stylesheet.css';
  22  if ( file_exists($rpx_style_file) ) {
  23    wp_register_style('rpx_style', $rpx_style_url);
  24    wp_enqueue_style('rpx_style');
  25  }
  26}
  27
  28function rpx_admin_stylesheet() {
  29  $rpx_style_url = WP_PLUGIN_URL . '/rpx/files/stylesheet.css';
  30  $rpx_style_file = WP_PLUGIN_DIR . '/rpx/files/stylesheet.css';
  31  if ( file_exists($rpx_style_file) ) {
  32    wp_register_style('rpx_style', $rpx_style_url);
  33    wp_enqueue_style('rpx_style');
  34  }
  35}
  36
  37function rpx_javascript($rpx_echo=false) {
  38  $rpx_js_url = WP_PLUGIN_URL . '/rpx/files/javascript.js';
  39  $rpx_js_file = WP_PLUGIN_DIR . '/rpx/files/javascript.js';
  40  if ( file_exists($rpx_js_file) ) {
  41    wp_register_script('rpx_javascript', $rpx_js_url);
  42    wp_enqueue_script('rpx_javascript');
  43    $rpx_social_option = get_option(RPX_SOCIAL_OPTION);
  44    $rpx_social_pub = get_option(RPX_SOCIAL_PUB);
  45    if ($rpx_social_option == 'true' && !empty($rpx_social_pub) ){
  46      wp_register_script('rpx_js', RPX_URL_SCHEME.RPX_SERVER.'/js/lib/rpx.js');
  47      wp_enqueue_script('rpx_js');
  48    }
  49  }
  50  if (get_option(RPX_SHARE_COUNT_OPTION) == 'hover') {
  51    $rpx_jq_url = WP_PLUGIN_URL . '/rpx/files/javascript-jquery.js';
  52    $rpx_jq_file = WP_PLUGIN_DIR . '/rpx/files/javascript-jquery.js';
  53    if ( file_exists($rpx_jq_file) ) {
  54      wp_register_script('rpx_javascript_jq', $rpx_jq_url);
  55      wp_enqueue_script('rpx_javascript_jq');
  56    }
  57  }
  58}
  59
  60function rpx_test_api(){
  61/* example successful test array
  62  test {
  63    [curl] = true,
  64    [curl_ssl] = true,
  65    [wp_http] = true,
  66    [php_ssl] = true,
  67    [api_tested] = true,
  68    [post] = true,
  69    [ssl_valid] = true,
  70    [api] = true,
  71    [select] = wp_html
  72  }
  73  the select value is one of the following
  74    'wp_http'
  75    'curl'
  76    false
  77*/
  78  $test = array();
  79  //curl test
  80  if (function_exists('curl_version')){
  81    $test['curl'] = true;
  82    $curl = curl_version();
  83    if ( !empty($curl['ssl_version']) ){
  84      $test['curl_ssl'] = true;
  85    }else{
  86      $test['curl_ssl'] = false;
  87    }
  88  }else{
  89    $test['curl'] = false;
  90  }
  91  //wp http test
  92  if (function_exists('wp_remote_get')){
  93    $test['wp_http'] = true;
  94    $xports = stream_get_transports();
  95    if (in_array('ssl',$xports)){
  96      $test['php_ssl'] = true;
  97    }else{
  98      $test['php_ssl'] = false;
  99    }
 100  }else{
 101    $test['wp_http'] = false;
 102  }
 103  //jr api test
 104  if ($test['php_ssl'] === true || $test['curl_ssl'] === true){
 105    $test['api_tested'] = true;
 106    $rpx_post_array = array('apiKey' => 'JanrainEngagePluginForWordpress','format' => 'json');
 107    if ($rpx_reply = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/plugin/lookup_rp', $rpx_post_array,true,true) !== false){//test with ssl validation
 108      update_option(RPX_SSL_VALID_OPTION, 'true');
 109      $test['ssl_valid'] = true;
 110      $test['post'] = true;
 111    }elseif ($rpx_reply = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/plugin/lookup_rp', $rpx_post_array,false,true) !== false){//test without ssl validation
 112      update_option(RPX_SSL_VALID_OPTION, 'false');
 113      $test['ssl_valid'] = false;
 114      $test['post'] = true;
 115    }else{
 116      $test['post'] = false;
 117      $test['ssl_valid'] = false;
 118    }
 119    if ($rpx_reply == 'No RP found'){
 120      $test['api'] = true;/*in this case getting the error proves connectivity*/
 121    }else{
 122      $test['api'] = false;
 123    }
 124  }else{
 125    $test['api_tested'] = false;
 126  }
 127  //select http method
 128  $http_option = get_option(RPX_HTTP_OPTION);
 129  if (empty($http_option)) {
 130    $http_option = false;
 131  }
 132  if ($test['api_tested'] === true && $http_option === false){
 133    if ($test['wp_http'] === true){
 134      update_option(RPX_HTTP_OPTION, 'wp_http');
 135      $test['select'] = 'wp_http';
 136    }else{
 137      update_option(RPX_HTTP_OPTION, 'curl');
 138      $test['select'] = 'curl';
 139    }
 140  }else{
 141    $test['select'] = $http_option;
 142  }
 143  return $test;
 144}
 145
 146
 147function rpx_configured(){
 148  rpx_bootstrap();
 149  $required_options = array(
 150    RPX_API_KEY_OPTION => 'apiKey',
 151    RPX_REALM_OPTION => 'realm',
 152    RPX_REALM_SCHEME => 'realmScheme',
 153    RPX_ADMIN_URL_OPTION => 'adminUrl'
 154  );
 155  foreach($required_options as $key => $val){
 156    $option = get_option($key);
 157    if ( empty($option) || $option === false){
 158      return false;
 159    }
 160  }
 161  return true;
 162}
 163
 164function rpx_allow_register(){
 165  if (get_option('users_can_register') == 1 && get_option(RPX_AUTOREG_OPTION) == 'true'){
 166    return true;
 167  }
 168  return false;
 169}
 170
 171function rpx_user_data(){
 172  if (is_user_logged_in() == true){
 173    global $current_user;
 174    return $current_user;
 175  }
 176  return false;
 177}
 178
 179function rpx_bp_init(){
 180  define('RPX_BP_ACTIVE', true);
 181}
 182
 183function rpx_redirect($redirect_to=''){
 184  if ( empty($redirect_to) ){
 185    $url = RPX_DEFAULT_REDIRECT;
 186  }else{
 187    $url = $redirect_to;
 188  }
 189  wp_safe_redirect($url);
 190  exit;
 191}
 192
 193function rpx_register_widget(){
 194  return register_widget('RPX_Widget');
 195}
 196
 197function rpx_admin_menu(){
 198  add_utility_page(RPX_OPTIONS_TITLE, RPX_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_MENU_SLUG, 'rpx_admin_menu_view', WP_PLUGIN_URL.RPX_IMAGE_PATH.'janrain_icon_small.png');
 199  add_submenu_page(RPX_MENU_SLUG, RPX_OPTIONS_TITLE, RPX_MENU_MAIN, RPX_OPTIONS_ROLE, RPX_MENU_SLUG, 'rpx_admin_menu_view');
 200  add_submenu_page(RPX_MENU_SLUG, RPX_STRING_OPTIONS_TITLE, RPX_STRING_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_STRING_MENU_SLUG, 'rpx_admin_string_menu_view');
 201  add_submenu_page(RPX_MENU_SLUG, RPX_HELP_OPTIONS_TITLE, RPX_HELP_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_HELP_MENU_SLUG, 'rpx_admin_help_menu_view');
 202  add_submenu_page(RPX_MENU_SLUG, RPX_ADVANCED_OPTIONS_TITLE, RPX_ADVANCED_MENU_LABEL, RPX_OPTIONS_ROLE, RPX_ADVANCED_MENU_SLUG, 'rpx_admin_advanced_menu_view');
 203  add_action( 'admin_init', 'rpx_admin_menu_register' );
 204  return true;
 205}
 206
 207function rpx_admin_menu_register(){
 208  register_setting( 'rpx_settings_group', RPX_API_KEY_OPTION, 'rpx_process_api_key' );
 209  register_setting( 'rpx_settings_group', RPX_VEMAIL_OPTION, 'rpx_process_bool' );
 210  register_setting( 'rpx_settings_group', RPX_COMMENT_OPTION, 'rpx_process_clog' );
 211  register_setting( 'rpx_settings_group', RPX_SOCIAL_OPTION, 'rpx_process_bool' );
 212  register_setting( 'rpx_settings_group', RPX_SOCIAL_COMMENT_OPTION, 'rpx_process_bool' );
 213  register_setting( 'rpx_settings_group', RPX_S_LOC_OPTION, 'rpx_process_sloc' );
 214  register_setting( 'rpx_settings_group', RPX_AUTOREG_OPTION, 'rpx_process_bool' );
 215  register_setting( 'rpx_settings_group', RPX_VERIFYNAME_OPTION, 'rpx_process_bool' );
 216  register_setting( 'rpx_settings_group', RPX_AVATAR_OPTION, 'rpx_process_bool' );
 217  register_setting( 'rpx_settings_group', RPX_S_STYLE_OPTION, 'rpx_process_sstyle' );
 218  register_setting( 'rpx_settings_group', RPX_S_TXT_OPTION, 'rpx_process_txt' );
 219  register_setting( 'rpx_settings_group', RPX_PARAMS_OPTION, 'rpx_process_params' );
 220  register_setting( 'rpx_settings_group', RPX_REMOVABLE_OPTION, 'rpx_process_bool' );
 221  register_setting( 'rpx_settings_group', RPX_SHARE_COUNT_OPTION, 'rpx_process_shct' );
 222  register_setting( 'rpx_string_settings_group', RPX_STRINGS_OPTION, 'rpx_process_strings' );
 223  register_setting( 'rpx_advanced_settings_group', RPX_ADVANCED_OPTION, 'rpx_process_strings' );
 224  return true;
 225}
 226
 227function rpx_process_bool($bool){
 228  if ($bool == 'true' || $bool == 'false'){
 229    return $bool;
 230  }else{
 231    return 'false';
 232  }
 233}
 234
 235function rpx_process_sloc($sloc){
 236  if ($sloc == 'top' || $sloc == 'bottom' || $sloc == 'none'){
 237    return $sloc;
 238  }else{
 239    return 'none';
 240  }
 241}
 242
 243function rpx_process_shct($shct){
 244  if ($shct == 'always' || $shct == 'hover' || $shct == 'false'){
 245    return $shct;
 246  }else{
 247    return 'false';
 248  }
 249}
 250
 251function rpx_process_sstyle($sstyle){
 252  if ($sstyle == 'icon' || $sstyle == 'label'){
 253    return $sstyle;
 254  }else{
 255    return 'none';
 256  }
 257}
 258
 259function rpx_process_clog($clog){
 260  global $rpx_comment_actions;
 261  if (in_array($clog, $rpx_comment_actions)){
 262    return $clog;
 263  }
 264  return false;
 265}
 266
 267function rpx_process_txt($txt){
 268  $clean = strip_tags($txt);
 269  if ($txt === $clean){
 270    return $txt;
 271  }else{
 272    return $clean;
 273  }
 274}
 275
 276function rpx_process_params($params){
 277  if ($params === '') {
 278    return $params;
 279  }
 280  if ($params !== trim($params,'&')) {
 281    return false;
 282  }
 283  if ($params !== str_replace(' ','',$params)) {
 284    return false;
 285  }
 286  if ($params !== strip_tags($params)) {
 287    return false;
 288  }
 289  $pairs = explode('&', $params);
 290  if ($pairs[0] === $params) {
 291    if (strstr($params,'=') === false) {
 292      return false;
 293    }
 294    if (strpos($params,'=') !== strrpos($params,'=')) {
 295      return false;
 296    }
 297    return $params;
 298  }
 299  $param_array = array();
 300  foreach ($pairs as $key=>$val) {
 301    if (strstr($val,'=') === false) {
 302      return false;
 303    }
 304    if (strpos($val,'=') !== strrpos($val,'=')) {
 305      return false;
 306    }
 307  }
 308  return $params;
 309}
 310
 311function rpx_process_strings($strings){
 312  if ( is_array($strings) ) {
 313    return $strings;
 314  }
 315  return false;
 316}
 317
 318function rpx_get_comment_option(){
 319  $rpx_comment_option = get_option(RPX_COMMENT_OPTION);
 320  if ( empty($rpx_comment_option) ){
 321    return RPX_COMMENT_OPTION_DEFAULT;
 322  }
 323  return $rpx_comment_option;
 324}
 325
 326function rpx_update_options($rpx_api_key){
 327  if ($rpx_rp = rpx_get_rp($rpx_api_key)){
 328    update_option(RPX_REALM_OPTION,     $rpx_rp['realm']);
 329    update_option(RPX_REALM_SCHEME,     $rpx_rp['realmScheme']);
 330    update_option(RPX_APP_ID_OPTION,    $rpx_rp['appId']);
 331    update_option(RPX_ADMIN_URL_OPTION, $rpx_rp['adminUrl']);
 332    update_option(RPX_SOCIAL_PUB,       $rpx_rp['socialPub']);
 333    update_option(RPX_PROVIDERS_OPTION, $rpx_rp['signinProviders']);
 334    return true;
 335  }
 336  rpx_message('API key failed test.', 'error');
 337  return false;
 338}
 339
 340function rpx_process_api_key($rpx_api_key){
 341  $rpx_api_key = strip_tags($rpx_api_key);
 342  rpx_update_options($rpx_api_key);
 343  return $rpx_api_key;
 344}
 345
 346function rpx_get_rp($rpx_api_key){
 347  if (strlen($rpx_api_key) == 40){
 348    $rpx_post_array = array('apiKey' => $rpx_api_key,'pluginName' => RPX_PLUGIN_NAME,'pluginVersion' => RPX_PLUGIN_VERSION, 'format' => 'json');
 349    if ($rpx_json = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/plugin/lookup_rp', $rpx_post_array)){
 350      $rpx_rp = json_decode($rpx_json,true);
 351      if ($rpx_rp['apiKey'] == $rpx_api_key){
 352        return $rpx_rp;
 353      }
 354    }
 355    rpx_message('Unable to validate API key. Please verify your PHP CURL version.', 'error');
 356  }
 357  return false;
 358}
 359
 360function rpx_post($url,$post_data,$ssl=NULL,$track=false){
 361  if (get_option(RPX_SSL_VALID_OPTION) == 'false' && $ssl === NULL){
 362    $ssl = false;
 363  }else{
 364    $ssl = true;
 365  }
 366  if ($track === true){
 367    $user_agent = 'Janrain_Engage_Wordpress_Plugin';
 368  }else{
 369    $user_agent = 'Wordpress';
 370  }
 371  if (function_exists('wp_remote_get') && get_option(RPX_HTTP_OPTION) !== 'curl'){
 372    $headers = array('Referer'=>get_bloginfo('url'));
 373    $wp_get_args = array(
 374      'method'      => 'GET',
 375      'timeout'     => 5,
 376      'redirection' => 5,
 377      'user-agent'  => $user_agent,
 378      'blocking'    => true,
 379      'compress'    => true,
 380      'decompress'  => true,
 381      'sslverify'   => $ssl,
 382      'headers'     => $headers
 383    );
 384    $parms = array();
 385    foreach ($post_data as $key => $val){
 386      $parms[] = urlencode($key).'='.urlencode($val);
 387    }
 388    $parms = implode('&',$parms);
 389    if ( !empty($parms) ){
 390      $wp_get_url = $url.'?'.$parms;
 391      $wp_get = @wp_remote_get($wp_get_url,$wp_get_args);
 392      if (is_wp_error($wp_get)){
 393        update_option(RPX_HTTP_OPTION, '');
 394        rpx_message('WP_HTTP error:"'.serialize($wp_get).'"', 'message');
 395      }else{
 396        update_option(RPX_HTTP_OPTION, 'wp_http');
 397        return $wp_get["body"];
 398      }
 399    }else{
 400      rpx_message('WP_HTTP error:"Parameters missing"', 'error');
 401      return false;
 402    }
 403  }
 404  if (function_exists('curl_init') && get_option(RPX_HTTP_OPTION) !== 'wp_http'){
 405    $ch = curl_init();
 406    curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
 407    curl_setopt($ch, CURLOPT_REFERER, get_bloginfo('url'));
 408    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 409    curl_setopt($ch, CURLOPT_URL, $url);
 410    curl_setopt($ch, CURLOPT_POST, true);
 411    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 412    curl_setopt($ch, CURLOPT_HEADER, false);
 413    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 414    if ($ssl === true){
 415      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
 416    }
 417    $post_result = @curl_exec($ch);
 418    $curl_error = curl_error($ch);
 419    curl_close($ch);
 420    if ( empty($curl_error) ){
 421      update_option(RPX_HTTP_OPTION, 'curl');
 422      return $post_result;
 423    }
 424    update_option(RPX_HTTP_OPTION, '');
 425    rpx_message('CURL error:"'.$curl_error.'"', 'error');
 426  }else{
 427    update_option(RPX_HTTP_OPTION, '');
 428    rpx_message('CURL support not found.', 'error');
 429  }
 430  update_option(RPX_HTTP_OPTION, '');
 431  rpx_message('No supported HTTP access found.', 'error');
 432  return false;
 433}
 434
 435/**
 436 * Remove the current user's Engage usermeta.
 437 */
 438function rpx_remove_usermeta(){
 439  global $rpx_http_vars;
 440  global $current_user;
 441  if ( $rpx_http_vars['action'] != RPX_REMOVE_ACTION ) {
 442    return false;
 443  }
 444  $removable = get_option(RPX_REMOVABLE_OPTION);
 445  if($removable !== 'true'){
 446    return false;
 447  }
 448  if ( $current_user->id === '' || $current_user->id === null ) {
 449    return false;
 450  }
 451  $user_id = $current_user->id;
 452  delete_user_meta($user_id, RPX_META_IDENTIFIER);
 453  delete_user_meta($user_id, RPX_META_PROVIDER);
 454  delete_user_meta($user_id, RPX_META_LOCKED);
 455  delete_user_meta($user_id, RPX_META_SESSION);
 456  delete_user_meta($user_id, RPX_META_URL);
 457  delete_user_meta($user_id, RPX_META_PHOTO);
 458  delete_user_meta($user_id, RPX_META_PROFILE);
 459  delete_user_meta($user_id, RPX_META_CONTACTS);
 460  wp_safe_redirect(get_edit_profile_url($user_id));
 461}
 462
 463
 464function rpx_process_token(){
 465  global $rpx_http_vars;
 466  global $rpx_auth_info;
 467  if ( $rpx_http_vars['action'] == RPX_TOKEN_ACTION && !empty($rpx_http_vars['token']) ){
 468    if (RPX_AUTH_INFO_EXTENDED === 'true') {
 469      $extended = 'true';
 470    } else {
 471      $extended = 'false';
 472    }
 473    $post_data = array(
 474      'token' => $rpx_http_vars['token'],
 475      'apiKey' => get_option(RPX_API_KEY_OPTION),
 476      'extended' => $extended,
 477      'format' => 'json'
 478    );
 479    if ($rpx_response = rpx_post(RPX_URL_SCHEME.RPX_SERVER.RPX_API_PATH.'auth_info', $post_data)){
 480      if ($rpx_auth_info = json_decode($rpx_response,true)){
 481        if (rpx_new_profile($rpx_auth_info)){
 482          if ( !empty($rpx_http_vars['rpx_username']) ){
 483            global $rpx_wp_profile;
 484            $rpx_wp_profile['rpx_username'] = $rpx_http_vars['rpx_username'];
 485          }
 486          return rpx_process_user(rpx_test_wp_user());
 487        }
 488      }
 489    }
 490  }
 491  rpx_message(rpx_get_message($rpx_auth_info));
 492  return false;
 493}
 494
 495function rpx_process_session(){
 496  global $rpx_wp_profile;
 497  global $rpx_http_vars;
 498  if ( !empty($rpx_http_vars['rpx_session']) ){
 499    if (rpx_session_identifier() === true){
 500      if (strlen($rpx_http_vars['rpx_email']) > 5){
 501        $rpx_wp_profile['rpx_email'] = $rpx_http_vars['rpx_email'];
 502      }
 503      return rpx_process_user(rpx_test_wp_user());
 504    }
 505  }
 506  rpx_message('unable to process session', 'error');
 507  return false;
 508}
 509
 510function rpx_process_user($action){/*Using a switch for visual clarity, this may be cleaned up when conditionals are no longer in flux.*/
 511  if (RPX_VERBOSE == 'true'){
 512    error_log('WP-RPX '.$action);/*This will enter every RPX action into your php log (usually apache error log).*/
 513  }
 514  switch ($action){
 515    case 'signon':
 516      rpx_signon_wp_user();
 517      return true;
 518      break;
 519    case 'create':
 520      $create = rpx_create_wp_user();
 521      if ($create !== false) $create = rpx_signon_wp_user();
 522      if ($create !== false) return true;
 523      rpx_message('create failure', 'debug');
 524      rpx_process_user('regdirect');
 525      return false;
 526      break;
 527    case 'vemail':
 528      rpx_get_vemail_id();
 529      rpx_update_wp_user();
 530      rpx_signon_wp_user();
 531      return true;
 532      break;
 533    case 'engage':
 534      rpx_add_engage();
 535      rpx_update_wp_user();
 536      rpx_signon_wp_user();
 537      return true;
 538      break;
 539    case 'register':
 540      $register = rpx_unlock_user();
 541      if ($register !== false) $register = rpx_new_session();
 542      if ($register !== false) $register = rpx_update_wp_user(true,true);
 543      if ($register !== false) $register = rpx_signon_wp_user();
 544      if ($register !== false) return true;
 545      rpx_message('register failure', 'debug');
 546      rpx_process_user('regdirect');
 547      return false;
 548      break;
 549    case 'regdirect':
 550      rpx_redirect(rpx_get_reg_url());
 551      break;
 552    case 'getemail':
 553      $getemail = rpx_lock_user();
 554      if ($getemail !== false) $getemail = rpx_new_session();
 555      if ($getemail !== false) $getemail = rpx_placeholder_email();
 556      if ($getemail !== false) $getemail = rpx_create_wp_user();
 557      if ($getemail !== false) $getemail = rpx_register_wp_user();
 558      if ($getemail !== false) return true;
 559      rpx_message('getemail failure', 'debug');
 560      rpx_process_user('regdirect');
 561      return false;
 562      break;
 563    case 'retryemail':
 564      $retryemail = rpx_lock_user();
 565      if ($retryemail !== false) $retryemail = rpx_new_session();
 566      if ($retryemail !== false) $retryemail = rpx_placeholder_email();
 567      if ($retryemail !== false) $retryemail = rpx_update_wp_user(true,true);
 568      if ($retryemail !== false) $retryemail = rpx_register_wp_user();
 569      if ($retryemail !== false) return true;
 570      rpx_message('retryemail failure', 'debug');      
 571      return false;
 572      break;
 573    case 'getuser':
 574      rpx_register_wp_user('username');
 575      return true;
 576      break;
 577    case 'error':
 578      add_action('wp_footer','rpx_message_box',20);
 579      return false;
 580      break;
 581  }
 582  rpx_message('User action unmatched.', 'error');
 583  return false;
 584}
 585
 586function rpx_test_wp_user(){
 587  global $rpx_wp_profile;
 588  global $rpx_http_vars;
 589  $tests = array();
 590  rpx_message('user processing begin', 'debug');
 591
 592  /*The tests all assume this is an Engage auth so the id is required.*/
 593  if ( empty($rpx_wp_profile['rpx_identifier']) ){
 594    rpx_message('Empty identifier.', 'error');
 595    return 'error';
 596  }
 597
 598  /*Sequential state tests, boolean*/
 599  $user_data = rpx_user_data();
 600  if ($user_data === false){
 601    $active_user = false;
 602  }else{
 603    $active_user = true;
 604  }
 605  $tests['active_user'] = $active_user;
 606
 607  if ( empty($rpx_wp_profile['rpx_wp_id']) ){
 608    if(rpx_get_wpid() === true){
 609      $rpx_match = true;
 610      rpx_get_meta();
 611    }else{
 612      $rpx_match = false;
 613    }
 614  }else{
 615    if ($rpx_wp_profile['rpx_wp_id'] > 1 && $rpx_wp_profile['rpx_wp_id'] != '1'){
 616      $rpx_match = true;
 617      rpx_get_meta();
 618    }else{
 619      $rpx_match = false;
 620    }
 621  }
 622  $tests['rpx_match'] = $rpx_match;
 623
 624  if ( empty($rpx_wp_profile['rpx_locked']) ) {
 625    if ($rpx_match === false){
 626      $rpx_wp_profile['rpx_locked'] = false;
 627    }else{
 628      $rpx_wp_profile['rpx_locked'] = true;
 629    }
 630  }
 631  if ($rpx_wp_profile['rpx_locked'] == 'true'){
 632      $rpx_locked = true;
 633  }else{
 634      $rpx_wp_profile['rpx_locked'] = 'false';
 635      $rpx_locked = false;
 636  }
 637  $tests['rpx_locked'] = $rpx_locked;
 638
 639  if ( empty($rpx_wp_profile['rpx_verifiedEmail']) ){
 640    $rpx_verified_email = false;
 641    if ( empty($rpx_wp_profile['rpx_email']) ){
 642      $rpx_email = false;
 643    }else{
 644      $rpx_email = true;
 645    }
 646  }else{
 647    $rpx_verified_email = true;
 648    $rpx_email = true;
 649  }
 650  $tests['rpx_verified_email'] = $rpx_verified_email;
 651  $tests['rpx_email'] = $rpx_email;
 652  
 653  if ( empty($rpx_wp_profile['rpx_username']) ) {
 654    $rpx_wp_profile['rpx_username'] = '';
 655  }
 656  if (strlen(strip_tags($rpx_wp_profile['rpx_username'])) > 1){
 657    $rpx_username = true;
 658    $rpx_wp_username_id = username_exists($rpx_wp_profile['rpx_username']);
 659    if ($rpx_wp_username_id === false || $rpx_wp_username_id === NULL){//Ok who ruturns NULL? Seriously!
 660      $username_match = false;
 661    }else{
 662      $username_match = true;
 663    }
 664  }else{
 665    $rpx_username = false;
 666    $username_match = false;
 667  }
 668  $tests['rpx_username'] = $rpx_username;
 669  $tests['username_match'] = $username_match;
 670
 671  $email_match = false;
 672  if (strlen(strip_tags($rpx_wp_profile['rpx_email'])) > 5){
 673    $wp_email_id = email_exists($rpx_wp_profile['rpx_email']);
 674    if ($wp_email_id != false){
 675      $email_found = true;
 676      $rpx_wp_profile['user_email'] = $rpx_wp_profile['rpx_email'];
 677      if ( !empty($rpx_wp_profile['rpx_wp_id']) ) {
 678        if ( $rpx_wp_profile['rpx_wp_id'] == $wp_email_id){
 679          $email_match = true;
 680        }
 681      }
 682    }else{
 683      $email_found = false;
 684    }
 685  }else{
 686    $email_found = false;
 687  }
 688  $tests['email_found'] = $email_found;
 689  $tests['email_match'] = $email_match;
 690
 691  $wptest = rpx_validate_user();
 692  if ($wptest === false){
 693    $rpx_valid = false;
 694  }else{
 695    $rpx_valid = true;
 696  }
 697  $tests['rpx_valid'] = $rpx_valid;
 698
 699  if (rpx_allow_register() === true){
 700    $autoreg = true;
 701  }else{
 702    $autoreg = false;
 703  }
 704  $tests['autoreg'] = $autoreg;
 705  /*End of sequential tests*/
 706
 707  //var_dump($tests); exit;//expert debug point
 708
 709  /*Sequential conditions for action*/
 710  if ($rpx_match === true && $rpx_locked === false){
 711    return 'signon';
 712  }
 713
 714  if ($active_user === true && $rpx_locked === false){
 715    return 'engage';
 716  }
 717
 718  if ($rpx_match === true && $rpx_locked === true && ($rpx_email === false || $email_found === true || $rpx_valid === false)){
 719    return 'retryemail';
 720  }
 721
 722  if ($rpx_match === true && $rpx_locked === true && $rpx_email === true && $email_found === false){
 723    return 'register';
 724  }
 725
 726  if ($email_found === true && $rpx_verified_email === true && $rpx_locked === false && get_option(RPX_VEMAIL_OPTION) == 'true'){
 727    return 'vemail';
 728  }
 729
 730  if ($autoreg === true && $rpx_match === false && $rpx_email === true && $email_found === false && $rpx_username === true && $username_match === false){
 731    return 'create';
 732  }
 733
 734  if ($autoreg === false && $rpx_match === false){
 735    return 'regdirect';
 736  }
 737
 738  if ($autoreg === true && $rpx_match === false  && ($rpx_username === false || $username_match === true)){
 739    return 'getuser';
 740  }
 741
 742  if ($autoreg === true && $rpx_match === false && ($rpx_email === false || $email_found === true)){
 743    return 'getemail';
 744  }
 745
 746  /*Conditions for error action*/
 747  if ($email_found === true){
 748    rpx_message('The email address '.$rpx_wp_profile['rpx_email'].' is already registered with another account.', 'message');
 749  }
 750
 751  if ($rpx_match === true && $rpx_locked === true && $email_found === false){
 752    rpx_message('Session ID does not match. Unable to unlock unverified account. Contact site admin to reset the account for "'.$rpx_wp_profile['rpx_username'].'"', 'message');
 753  }
 754
 755  rpx_message('user processing end', 'debug');
 756  return 'error';
 757}
 758
 759function rpx_create_wp_user(){
 760  global $rpx_wp_profile;
 761  global $rpx_wp_user;
 762  rpx_new_wp_user();
 763  if ($rpx_wp_user['user_pass'] = wp_generate_password( 12, false )){
 764    $insert_user = wp_insert_user($rpx_wp_user);
 765    if (is_wp_error($insert_user)) {
 766      rpx_message('WP insert user fail', 'debug');
 767      return false;
 768    }
 769    $rpx_wp_profile['rpx_wp_id'] = $insert_user;
 770    if ($rpx_wp_profile['rpx_wp_id'] != false && $rpx_wp_profile['rpx_wp_id'] != 1){
 771      if (rpx_update_meta()){
 772        if (RPX_REQUIRE_EULA == 'true') {
 773          rpx_eula_user();
 774        }
 775        return true;
 776      }
 777    }
 778  }
 779  rpx_message('Create user failed.', 'error');
 780  return false;
 781}
 782
 783function rpx_get_vemail_id(){
 784  global $rpx_wp_profile;
 785  if (get_option(RPX_VEMAIL_OPTION) == 'true' && !empty($rpx_wp_profile['rpx_verifiedEmail']) ){
 786    $rpx_wp_profile['rpx_wp_id'] = email_exists($rpx_wp_profile['rpx_verifiedEmail']);
 787    return true;
 788  }
 789  return false;
 790}
 791
 792function rpx_update_wp_user($force_email=false,$force_reg=false){
 793  global $rpx_wp_profile;
 794  global $rpx_wp_user;
 795  $user_data = rpx_user_data();
 796  if ( $user_data !== false ) {
 797    if ($user_data->id != $rpx_wp_profile['rpx_wp_id'] && $force_reg === false){
 798      rpx_message('ruwu user id '.$user_data->id.'!='.$rpx_wp_profile['rpx_wp_id'], 'debug');
 799      return false;
 800    }
 801  }
 802  $rpx_wp_user['ID'] = $rpx_wp_profile['rpx_wp_id'];
 803  $rpx_wp_user['id'] = $rpx_wp_profile['rpx_wp_id'];
 804  if (!empty($rpx_wp_profile['rpx_provider'])){
 805    $rpx_wp_user['rpx_provider'] = $rpx_wp_profile['rpx_provider'];
 806  }
 807  if (!empty($rpx_wp_profile['rpx_url'])){
 808    $rpx_wp_user['rpx_url'] = $rpx_wp_profile['rpx_url'];
 809  }
 810  if (!empty($rpx_wp_profile['rpx_photo'])){
 811    $rpx_wp_user['rpx_photo'] = $rpx_wp_profile['rpx_photo'];
 812  }
 813  if (!empty($user_data->email) && $force_email === false){
 814    $rpx_wp_user['user_email'] = $user_data->email;
 815  }elseif (!empty($rpx_wp_profile['rpx_email'])){
 816    $rpx_wp_user['user_email'] = $rpx_wp_profile['rpx_email'];
 817  }
 818  if (!empty($user_data->user_url)){
 819    $rpx_wp_user['user_url'] = $user_data->user_url;
 820  }elseif (!empty($rpx_wp_profile['rpx_url'])){
 821    $rpx_wp_user['user_url'] = $rpx_wp_profile['rpx_url'];
 822  }
 823  if ($rpx_wp_profile['rpx_wp_id'] = wp_update_user($rpx_wp_user)){
 824    if (rpx_update_meta()){
 825      return true;
 826    }
 827  }
 828  rpx_message('Update user failed.', 'error');
 829  return false;
 830}
 831
 832function rpx_add_engage(){
 833  global $rpx_wp_profile;
 834  $user_data = rpx_user_data();
 835  if ($user_data === false || empty($user_data->id)){
 836    rpx_message('Add Engage failed.', 'error');
 837    return false;
 838  }
 839  $rpx_wp_profile['rpx_wp_id'] = $user_data->id;
 840}
 841
 842function rpx_register_wp_user($collect='email'){
 843  global $rpx_wp_profile;
 844  global $rpx_http_vars;
 845  global $rpx_wp_user_map;
 846  foreach ($rpx_wp_user_map as $key => $val){
 847    if ( empty($rpx_wp_profile[$key]) && !empty($rpx_http_vars[$key]) ){
 848      $rpx_wp_profile[$key] = $rpx_http_vars[$key];
 849    }
 850  }
 851  if ($collect == 'email'){
 852    $rpx_email = urlencode($rpx_http_vars['rpx_email']);
 853  } else {
 854    $rpx_email = urlencode($rpx_wp_profile['rpx_email']);
 855  }
 856  if ( !empty($rpx_wp_profile['user_email']) ){
 857    $rpx_http_vars['user_email'] = $rpx_wp_profile['user_email'];
 858  }
 859  if ( !empty($rpx_wp_profile['user_name']) ){
 860    $rpx_http_vars['user_name'] = $rpx_wp_profile['user_name'];
 861  }
 862  rpx_set_redirect();
 863  $reg_url = $rpx_http_vars['redirect_to'];
 864  $anchor = strstr($reg_url,'#');
 865  if ($anchor !== false){
 866    $reg_url = str_replace($anchor,'',$reg_url);//strip any anchor tag
 867  }
 868  if (strstr($reg_url, '?') === false){
 869    $connect = '?';
 870  }else{
 871    $connect = '&';
 872  }
 873  $url = $reg_url.$connect.'action='.RPX_REGISTER_FORM_ACTION.
 874    '&rpx_session='.urlencode($rpx_wp_profile['rpx_session']).
 875    '&rpx_username='.urlencode($rpx_wp_profile['rpx_username']).
 876    '&rpx_provider='.urlencode($rpx_wp_profile['rpx_provider']).
 877    '&rpx_email='.urlencode($rpx_email).
 878    '&redirect_to='.urlencode($rpx_http_vars['redirect_to']).
 879    '&rpx_collect='.$collect;
 880  if ( !empty($rpx_http_vars['user_email']) ){
 881    $url .= '&user_email='.urlencode($rpx_http_vars['user_email']);
 882  }
 883  if ( !empty($rpx_http_vars['user_name']) ){
 884    $url .= '&user_name='.urlencode($rpx_http_vars['user_name']);
 885  }
 886  rpx_redirect($url);
 887  return true;
 888}
 889
 890function rpx_get_reg_url(){
 891  $reg_url = site_url().'/';
 892  if (!defined('RPX_BP_ACTIVE')) {
 893    define ('RPX_BP_ACTIVE', false);
 894  }
 895  if (RPX_BP_ACTIVE === true){
 896    $reg_url .= RPX_BP_REG_PATH;
 897  }else{
 898    $reg_url .= RPX_WP_REG_PATH;
 899  }
 900  return $reg_url;
 901}
 902
 903function rpx_reset_session(){
 904  global $rpx_wp_profile;
 905  global $rpx_http_vars;
 906  $rpx_wp_profile['rpx_session'] = $rpx_http_vars['rpx_session'];
 907  $rpx_wp_profile['rpx_username'] = $rpx_http_vars['rpx_username'];
 908  $rpx_wp_profile['rpx_provider'] = $rpx_http_vars['rpx_provider'];
 909  return true;
 910}
 911
 912function rpx_register() {
 913  global $rpx_http_vars;
 914  if ($rpx_http_vars['action'] != RPX_REGISTER_FORM_ACTION){
 915    return true;
 916  }
 917  if ($rpx_http_vars['rpx_collect'] == 'email'){
 918    if ( !empty($rpx_http_vars['rpx_session']) ){
 919      if ( !empty($rpx_http_vars['user_email']) ){
 920          rpx_message($rpx_http_vars['user_email']."\n".'The email address is already in use. '."\n".'Use another email address or login to that account.', 'rpxmessage');
 921      }else{
 922        if ( empty($rpx_http_vars['rpx_email']) ){
 923            rpx_message('This '.$rpx_http_vars['rpx_provider'].' account did not provide an email address. '."\n".'Enter a valid email address to register this account.','rpxmessage');
 924        }else{
 925          $wptest = rpx_validate_user($rpx_http_vars['rpx_email']);
 926          if ($wptest === false){
 927            rpx_message('The email address entered is not valid. '."\n".'Enter a valid email address to register this account.', 'rpxmessage');
 928          }else{
 929            global $rpx_wp_profile;
 930            $rpx_wp_profile['rpx_email'] = $rpx_http_vars['rpx_email'];
 931            return rpx_process_session();
 932          }
 933        }
 934      }
 935    }
 936  }
 937  if ($rpx_http_vars['rpx_collect'] == 'username'){
 938    $eula = true;
 939    if (RPX_REQUIRE_EULA == 'true') {
 940      if ($rpx_http_vars['rpx_eula'] != 'eula') {
 941        $eula = false;
 942        $rpx_http_vars['rpx_username'] = '';
 943      }
 944    }
 945    if ( !empty($rpx_http_vars['rpx_username']) && $eula === true ){
 946      $wptest = rpx_validate_user($rpx_http_vars['rpx_email'],$rpx_http_vars['rpx_username']);
 947      $user_login_result = get_user_by('login', $rpx_http_vars['rpx_username']);
 948      if (!is_object($user_login_result)) {
 949        $wptest = true;
 950      }
 951      if ($wptest === true){
 952        add_action('wp_footer','rpx_open_widget',12);
 953        $rpx_http_vars['action'] = '';
 954        return true;
 955      }
 956    }
 957    if ( !empty($rpx_http_vars['rpx_username']) ){
 958      $username = $rpx_http_vars['rpx_username'];
 959      $message = '"'.$username.'"'."\n".RPX_NAME_EXISTS_REASON."\n".RPX_NAME_PROMPT;
 960    }else{
 961      $message = RPX_NAME_PROMPT;
 962    }
 963    rpx_message($message, 'rpxmessage');
 964  }
 965}
 966
 967function rpx_signon_wp_user(){
 968  global $current_user;
 969  global $rpx_wp_profile;
 970  global $rpx_http_vars;
 971  global $rpx_auth_info;
 972  $user = rpx_wp_signon();
 973  if ($user != false && $user->ID != false && $user->ID != 0 && !empty($user->ID) ){
 974    $current_user =  new WP_User($user->ID, $user->user_login && false);
 975    $current_user = wp_get_current_user();
 976    if ($user->ID == $current_user->id){
 977      if (RPX_SERIAL_PROFILE == 'true') {
 978        rpx_update_user_meta($current_user->id, RPX_META_PROFILE, $rpx_auth_info);
 979      }
 980      if (RPX_GET_CONTACTS == 'true') {
 981        global $rpx_contacts;
 982        $rpx_contacts = rpx_get_contacts();
 983      }
 984      $remember = false;
 985      if (RPX_REMEMBER_WP_SIGNON == 'true'){
 986        $remember = true;
 987      }
 988      wp_set_auth_cookie($current_user->id, $remember);
 989      do_action('wp_login', $user->user_login);
 990      rpx_set_redirect();
 991      rpx_redirect($rpx_http_vars['redirect_to']);
 992      return true;
 993    }else{
 994      error_log('Janrain Engage Wordpress user mismatch '.$user->ID.'!='.$current_user->id);
 995      return false;
 996    }
 997  }else{
 998    rpx_message('Unable to sign on as '.$rpx_wp_profile['rpx_username'].'.', 'error');
 999    return false;
1000  }
1001}
1002
1003function rpx_get_contacts($user_id='') {
1004  $get_contacts_providers = array ('google', 'live_id', 'facebook', 'myspace', 'twitter', 'linkedin', 'yahoo');
1005  if (empty($user_id)) {
1006    $user_id = get_current_user_id();
1007  }
1008  if (!empty($user_id)) {
1009    $provider = get_user_meta($user_id, 'rpx_provider', true);
1010  }
1011  if (!empty($provider)) {
1012    global $rpx_providers;
1013    $provider = $rpx_providers[$provider];
1014    if (in_array($provider, $get_contacts_providers)) {
1015      $identifier = get_user_meta($user_id, 'rpx_identifier', true);
1016    }
1017  }
1018  if (!empty($identifier)) {
1019    $api_key = get_option(RPX_API_KEY_OPTION);
1020  }
1021  if (!empty($api_key)) {
1022    $rpx_post_array = array('apiKey' => $api_key, 'identifier' => $identifier, 'format' => 'json');
1023    $rpx_reply = rpx_post(RPX_URL_SCHEME.RPX_SERVER.'/api/v2/get_contacts', $rpx_post_array);
1024    if ($rpx_reply !== false) {
1025      $rpx_contacts = json_decode($rpx_reply,true);
1026      if (RPX_SERIAL_CONTACTS == 'true') {
1027        rpx_update_user_meta($user_id, RPX_META_CONTACTS, $rpx_contacts);
1028      }
1029      return $rpx_contacts;
1030    }
1031  }
1032  return false;
1033}
1034
1035function rpx_new_profile($rpx_auth_info){
1036  global $rpx_profile_map;
1037  global $rpx_wp_profile;
1038  if ( get_option(RPX_VERIFYNAME_OPTION) == 'true' ) {
1039    $rpx_profile_map['preferredUsername'] = 'blocked';
1040  }
1041  if ($rpx_auth_info['stat'] == 'ok'){
1042    $rpx_profile = $rpx_auth_info['profile'];
1043    foreach ($rpx_profile_map as $key => $value){
1044      if (is_array($value)){
1045        foreach ($value as $skey => $svalue){
1046          if ( !empty($rpx_profile["$key"]["$skey"]) ){
1047            $rpx_wp_profile["$svalue"] = $rpx_profile["$key"]["$skey"];
1048          }else{
1049            $rpx_wp_profile["$svalue"] = '';
1050          }
1051        }
1052      }elseif ( !empty($rpx_profile["$key"]) ){
1053        $rpx_wp_profile["$value"] = $rpx_profile["$key"];
1054      }else{
1055        $rpx_wp_profile["$value"] = '';
1056      }
1057    }
1058    return true;
1059  }
1060  return false;
1061}
1062
1063function rpx_new_wp_user(){
1064  global $rpx_wp_user_map;
1065  global $rpx_wp_profile;
1066  global $rpx_wp_user;
1067  foreach ($rpx_wp_user_map as $key => $value){
1068    if ( !empty($rpx_wp_profile["$key"]) ){
1069      $rpx_wp_user["$value"] = $rpx_wp_profile["$key"];
1070    }
1071  }
1072  return true;
1073}
1074
1075function rpx_validate_user($email='',$username=''){
1076  global $rpx_wp_profile;
1077  if ( empty($email) && !empty($rpx_wp_profile['rpx_email']) ){
1078    $email = $rpx_wp_profile['rpx_email'];
1079  }
1080  if ( empty($username) && !empty($rpx_wp_profile['rpx_username']) ){
1081    $username = $rpx_wp_profile['rpx_username'];
1082  }
1083  if ( !empty($email) ) {
1084    if ( !empty($username) ) {
1085      if (RPX_IS_WPMU === true){
1086        $wpmutest = wpmu_validate_user_signup($username, $email);
1087        if (is_wp_error($wpmutest)){
1088          $errors = $wpmutest->get_error_messages();
1089          /* convert wp errors into rpx messages here */
1090          return false;
1091        }else{
1092          return true;
1093        }
1094      }else{
1095        $wpuser = username_exists($username);
1096        if ($wpuser == NULL){
1097          $wpuser = false;
1098        }else{
1099          $wpuser = true;
1100        }
1101        $wpemail = email_exists($email);
1102        $wptest = is_email($email);
1103        if ($wptest == $email){
1104          $wptest = true;
1105        }else{
1106          $wptest = false;
1107        }
1108        if ($wpuser === false && $wpemail === false && $wptest === true){
1109          return true;
1110        }
1111        return false;
1112      }
1113    }else{
1114      $wptest = is_email($email);
1115      if ($wptest == $email){
1116        return true;
1117      }else{
1118        return false;
1119      }
1120    }
1121  }
1122}
1123
1124function rpx_get_message($rpx_auth_info){
1125  if ($rpx_auth_info['stat'] == 'fail'){
1126    $message = $rpx_auth_info['err']['msg'];
1127  }else{
1128    $message = 'Message missing.';
1129  }
1130  return $message;
1131}
1132
1133function rpx_wp_signon() {
1134  global $rpx_wp_profile;
1135  if ($rpx_wp_profile['rpx_wp_id'] == 1 && RPX_BLOCK_ADMIN == 'true') {
1136    return false;
1137  }
1138  $user = get_userdata($rpx_wp_profile['rpx_wp_id']);
1139  $username = $user->user_login;
1140  if ( is_a($user, 'WP_User') ) {
1141    return $user;
1142  }
1143  if ( empty($username) ) {
1144    rpx_message('The username field is empty.', 'message');
1145    return false;
1146  }
1147  if ( is_multisite() ) {
1148    if ( 1 == $user->spam){
1149      rpx_message('Your account has been marked as a spammer.', 'message');
1150      return false;
1151    }
1152    if ( !is_super_admin( $user->ID ) && isset($user->primary_blog) ) {
1153      $details = get_blog_details( $user->primary_blog );
1154      if ( is_object( $details ) && $details->spam == 1 ){
1155        rpx_message('Site Suspended.', 'message');
1156        return false;
1157      }
1158    }
1159  }
1160  return $user;
1161}
1162
1163function rpx_get_wpid() {
1164  global $wpdb;
1165  global $rpx_wp_profile;
1166  if ( empty($rpx_wp_profile['rpx_identifier']) ){
1167    rpx_message('Empty ID', 'debug');
1168    return false;
1169  }
1170  $sql = 'SELECT user_id FROM '.$wpdb->usermeta.' WHERE meta_key = %s AND meta_value = %s';
1171  $sql = $wpdb->prepare($sql, RPX_META_IDENTIFIER, addslashes($rpx_wp_profile['rpx_identifier']));
1172  $result = $wpdb->get_var($sql);
1173  if ($result != NULL){
1174    if ( !empty($result) && $result != false){
1175      $rpx_wp_profile['rpx_wp_id'] = $result;
1176      return true;
1177    }
1178  }
1179  rpx_message('No user found.', 'debug');
1180  return false;
1181}
1182
1183function rpx_get_wpuser() {
1184  global $rpx_wp_profile;
1185  global $rpx_wp_user_map;
1186  $user = get_userdata($rpx_wp_profile['rpx_wp_id']);
1187  foreach ($rpx_wp_user_map as $key => $val){
1188    if ( empty($rpx_wp_profile[$key]) && !empty($user->$val) ){
1189      $rpx_wp_profile[$key] = $user->$val;
1190    }
1191  }
1192}
1193
1194function rpx_clean_locked() {
1195  $cleanup_age = RPX_CLEANUP_AGE;
1196  settype($cleanup_age, 'int');
1197  global $wpdb;
1198  $count = 0;
1199  $sql = 'SELECT user_id FROM '.$wpdb->usermeta.' WHERE meta_key = %s AND meta_value = %s';
1200  $sql = $wpdb->prepare($sql, RPX_META_LOCKED, 'true');
1201  $result = $wpdb->get_col($sql);
1202  foreach ($result as $key=>$val){
1203    $rpx_clean_meta = false;
1204    if ($val != NULL){
1205      if ($val > 1){
1206        $sql = 'SELECT UTC_TIMESTAMP() FROM '.$wpdb->usermeta;
1207        $sql = $wpdb->prepare($sql);
1208        $sqlnow = $wpdb->get_var($sql);
1209        $user = get_userdata($val);
1210        if (strlen($user->rpx_session) > 1){
1211          $sqlnow = strtotime($sqlnow);
1212          $usertime = $user->user_registered;
1213          $usertime = strtotime($usertime);
1214          $user_aged = 0;
1215          if ($sqlnow >= $usertime && $usertime >= 0){
1216            $user_aged = $sqlnow - $usertime;
1217            $user_aged = $user_aged / 60;
1218          }
1219          if ($user_aged > RPX_CLEANUP_AGE){
1220            if(strpos($user->user_email,$user->rpx_session) === false){//test if the email address contains the session_id
1221              $rpx_clean_meta = true;
1222            }else{
1223              wp_delete_user($val);
1224              $count++;
1225              $rpx_clean_meta = false;
1226            }
1227          }
1228        }
1229      }
1230      if ($rpx_clean_meta === true){
1231        $sql = 'DELETE FROM '.$wpdb->usermeta.' WHERE user_id = %d AND meta_key LIKE %s';
1232        $sql = $wpdb->prepare($sql, $val, 'rpx_%%');
1233        $del_result = $wpdb->query($sql);
1234        $count++;
1235      }
1236    }
1237  }
1238  rpx_message('Cleaned '.$count.' users.', 'message');
1239  return true;
1240}
1241
1242function rpx_session_identifier() {
1243  global $wpdb;
1244  global $rpx_wp_profile;
1245  global $rpx_http_vars;
1246  $sql = 'SELECT user_id FROM '.$wpdb->usermeta.' WHERE meta_key = \''.RPX_META_SESSION.'\' AND meta_value = %s';
1247  $sql = $wpdb->prepare($sql, $rpx_http_vars['rpx_session']);
1248  $result = $wpdb->get_var($sql);
1249  if ($result != NULL){
1250    $rpx_wp_profile['rpx_wp_id'] = $result;
1251    if ($rpx_wp_profile['rpx_identifier'] = rpx_get_user_meta($result, RPX_META_IDENTIFIER)){
1252      return true;
1253    }
1254    rpx_message('identifier not found', 'debug');
1255    return false;
1256  }
1257  rpx_message('session not found', 'debug');
1258  return false;
1259}
1260
1261function rpx_get_meta() {/*no point in trying to catch errors since empty fields return false*/
1262  global $rpx_wp_profile;
1263  $rpx_wp_profile['rpx_identifier'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_IDENTIFIER);
1264  $rpx_wp_profile['rpx_provider'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PROVIDER);
1265  $rpx_wp_profile['rpx_session'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_SESSION);
1266  $rpx_wp_profile['rpx_locked'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_LOCKED);
1267  $rpx_wp_profile['rpx_photo'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PHOTO);
1268  $rpx_wp_profile['rpx_url'] = @rpx_get_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_URL);
1269}
1270
1271function rpx_update_meta() {/*Wordpress uses update for insert and update.*/
1272  global $rpx_wp_profile;
1273  $results = array();
1274  $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_IDENTIFIER, $rpx_wp_profile['rpx_identifier']);
1275  $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PROVIDER, $rpx_wp_profile['rpx_provider']);
1276  $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_SESSION, $rpx_wp_profile['rpx_session']);
1277  $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_LOCKED, $rpx_wp_profile['rpx_locked']);
1278  $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_PHOTO, $rpx_wp_profile['rpx_photo']);
1279  $results[] = rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_URL, $rpx_wp_profile['rpx_url']);
1280  if (in_array(false,$results)){
1281    return false;
1282  }
1283  return true;
1284}
1285
1286function rpx_update_user_meta($wp_id, $meta_label, $value) {/*wrapper for Wordpress update to avoid returning false on updates that match current values*/
1287  $result = rpx_get_user_meta($wp_id, $meta_label);
1288  if ($result == $value){
1289    return true;
1290  }
1291  if(update_user_meta($wp_id, $meta_label, $value, $result) === false){
1292    rpx_message('Meta update failed', 'error');
1293    return false;
1294  }
1295  return true;
1296}
1297
1298function rpx_get_user_meta($wp_id, $meta_label, $single = true) {/*wrapper to make single result the default*/
1299  $result = get_user_meta($wp_id, $meta_label, $single);
1300  return $result;
1301}
1302
1303function rpx_eula_user(){
1304  global $rpx_wp_profile;
1305  rpx_update_user_meta($rpx_wp_profile['rpx_wp_id'], RPX_META_EULA, 'true');
1306}
1307
1308function rpx_lock_user(){
1309  global $rpx_wp_profile;
1310  $rpx_wp_profile['rpx_locked'] = 'true';
1311}
1312
1313function rpx_unlock_user(){
1314  global $rpx_wp_profile;
1315  $rpx_wp_profile['rpx_locked'] = 'false';
1316}
1317
1318function rpx_new_session(){
1319  global $rpx_wp_profile;
1320  $rpx_wp_profile['rpx_session'] = uniqid('rpx_',true);
1321}
1322
1323function rpx_placeholder_email(){
1324  global $rpx_wp_profile;
1325  $rpx_wp_profile['rpx_email'] = $rpx_wp_profile['rpx_session'].'@'.get_option(RPX_REALM_OPTION);
1326}
1327
1328function rpx_set_redirect($url=''){
1329  global $rpx_http_vars;
1330  if ( !empty($url) ){
1331    $rpx_http_vars['redirect_to'] = strip_tags($url);
1332  }
1333  if ( empty($rpx_http_vars['redirect_to']) ){
1334    if (get_post_type() != false){
1335      $rpx_http_vars['redirect_to'] = get_permalink();
1336      $rpx_http_vars['redirect_to'] .= '#respond';
1337    }else{
1338      $rpx_http_vars['redirect_to'] = get_bloginfo('url');
1339    }
1340  }
1341}
1342
1343function rpx_message($message, $class='message') {
1344  global $rpx_messages;
1345  if (RPX_VERBOSE == 'true'){
1346    error_log('WP-RPX '.$class.'='.$message);/*ouput all messages to log*/
1347  }
1348  $rpx_messages[] = array( 'message' => $message, 'class' => $class);
1349}
1350
1351function puke_die($var=''){/*This is a debug function, it is never called in relased code*/
1352  global $rpx_http_vars;
1353  global $rpx_wp_profile;
1354  global $rpx_wp_user;
1355  global $rpx_messages;
1356  echo '<pre>';
1357  echo '$_REQUEST
1358  '; var_dump($_REQUEST);
1359    echo '$rpx_http_vars
1360  '; var_dump($rpx_http_vars);
1361    echo '$rpx_wp_profile
1362  '; var_dump($rpx_wp_profile);
1363    echo '$rpx_wp_user
1364  '; var_dump($rpx_wp_user);
1365    echo '$rpx_messages
1366  '; var_dump($rpx_messages);
1367  var_dump($var);
1368  echo '</pre>';
1369  exit;
1370}
1371
1372?>