| 24 | | // Function: evaluateIncomingRequest |
|---|
| 25 | | // Description: decides what kind of user is connecting. |
|---|
| 26 | | // Input: email, modulus, ppid from incoming request |
|---|
| 27 | | // Output: An array of user information. With the |
|---|
| 28 | | // following indices: |
|---|
| 29 | | // array['status'] - contains 1 of these statuses: |
|---|
| 30 | | // 1. valid-user - supplied email address exists |
|---|
| 31 | | // and belongs to same user as supplied modulus/ppid |
|---|
| 32 | | // 2. new-user - email address & modulus/ppid are new |
|---|
| 33 | | // 3. new-card - email address exists but modulus is new |
|---|
| 34 | | // 4. new-email - modulus/ppid exists but email is new |
|---|
| 35 | | // 5. an error code |
|---|
| 36 | | // |
|---|
| 37 | | // array['id'] - contains the user id number (or 0 if the status |
|---|
| 38 | | // is anything other than valid-user) |
|---|
| 39 | | // Side-effects: none |
|---|
| 40 | | function evaluateIncomingRequest() |
|---|
| 41 | | { |
|---|
| 42 | | global $wpdb, $claimslist, $datamap; |
|---|
| 43 | | $incominguser['id'] = 0; |
|---|
| 44 | | $incominguser['status'] = 'UNKNOWNERROR'; |
|---|
| 45 | | $identity_user_id = 0; |
|---|
| 46 | | |
|---|
| 47 | | $cardhash = pw_processing::getCardHash(); |
|---|
| 48 | | pw_utils::printDebug( '<br/>Evaluating Incoming Request:<br/>'); |
|---|
| 49 | | |
|---|
| 50 | | if (!is_email($claimslist['emailaddress']->token_value)) |
|---|
| 51 | | { |
|---|
| 52 | | $incominguser['status'] = "INVALIDEMAIL"; |
|---|
| 53 | | return $incominguser; |
|---|
| 54 | | } |
|---|
| 55 | | |
|---|
| 56 | | $query = "SELECT * FROM `" . PW_TABLE_PREFIX |
|---|
| 57 | | . "infocard_identities` WHERE cardhash = '" |
|---|
| 58 | | . $cardhash . "'"; |
|---|
| 59 | | $identity_info = $wpdb->get_results( $query, OBJECT ); |
|---|
| 60 | | |
|---|
| 61 | | switch (count($identity_info)) |
|---|
| 62 | | { |
|---|
| 63 | | case 1: |
|---|
| 64 | | // could be valid-user, or new-email |
|---|
| 65 | | $identity_user_id = $identity_info[0]->accountid; |
|---|
| 66 | | $datamap['cardhash']->add_db_value($identity_info[0]->cardhash); |
|---|
| 67 | | break; |
|---|
| 68 | | |
|---|
| 69 | | case 0: |
|---|
| 70 | | // could be new-card or new-user |
|---|
| 71 | | $identity_user_id = 0; |
|---|
| 72 | | break; |
|---|
| 73 | | |
|---|
| 74 | | default: |
|---|
| 75 | | $incominguser['status'] = "CARDNOTUNIQUE"; |
|---|
| 76 | | return $incominguser; |
|---|
| 77 | | } |
|---|
| 78 | | |
|---|
| 79 | | pw_utils::printDebug('Card Found: Acct# ' . $identity_user_id . "<br/>" ); |
|---|
| 80 | | |
|---|
| 81 | | $query = "SELECT * from " . PW_TABLE_PREFIX . "users " |
|---|
| 82 | | . "WHERE user_email LIKE '" |
|---|
| 83 | | . $claimslist['emailaddress']->token_value . "'"; |
|---|
| 84 | | |
|---|
| 85 | | $user_info = $wpdb->get_results( $query, ARRAY_A ); |
|---|
| 86 | | |
|---|
| 87 | | switch (count($user_info)) |
|---|
| 88 | | { |
|---|
| 89 | | case 1: |
|---|
| 90 | | $mail_user_id = $user_info[0][ID]; |
|---|
| 91 | | pw_utils::printDebug(' User belongs to account #: ' . $mail_user_id . "<br/>" ); |
|---|
| 92 | | if ($identity_user_id == 0) |
|---|
| 93 | | { |
|---|
| 94 | | $incominguser['id'] = $mail_user_id; |
|---|
| 95 | | $incominguser['status'] = 'new-card'; |
|---|
| 96 | | } |
|---|
| 97 | | elseif ($identity_user_id != $mail_user_id) |
|---|
| 98 | | { |
|---|
| 99 | | $incominguser['status'] = "CLAIMSMISMATCHED"; |
|---|
| 100 | | return $incominguser; |
|---|
| 101 | | } |
|---|
| 102 | | if ($identity_user_id == $mail_user_id) |
|---|
| 103 | | { |
|---|
| 104 | | $incominguser['id'] = $mail_user_id; |
|---|
| 105 | | $incominguser['status'] = 'valid-user'; |
|---|
| 106 | | |
|---|
| 107 | | } |
|---|
| 108 | | foreach (array_keys($datamap) as $key) |
|---|
| 109 | | { |
|---|
| 110 | | if (array_key_exists($datamap[$key]->mappingattribute, $user_info[0])) |
|---|
| 111 | | { |
|---|
| 112 | | pw_utils::printDebug('Existing Value: found '.$datamap[$key]->mappingattribute.' in user_info[0]<br/>'); |
|---|
| 113 | | $datamap[$key]->add_db_value($user_info[0][$datamap[$key]->mappingattribute]); |
|---|
| 114 | | } |
|---|
| 115 | | } |
|---|
| 116 | | |
|---|
| 117 | | // special case for first, last name |
|---|
| 118 | | $first = get_usermeta($mail_user_id, 'first_name'); |
|---|
| 119 | | if ($first) |
|---|
| 120 | | { |
|---|
| 121 | | pw_utils::printDebug('Existing Value: found '.$datamap['first_name']->mappingattribute.' in usermeta ( '.$first.' )<br/>'); |
|---|
| 122 | | $datamap['first_name']->add_db_value( $first ); |
|---|
| 123 | | } |
|---|
| 124 | | |
|---|
| 125 | | $last = get_usermeta($mail_user_id, 'last_name'); |
|---|
| 126 | | if ($last) |
|---|
| 127 | | { |
|---|
| 128 | | pw_utils::printDebug('Existing Value: found '.$datamap['last_name']->mappingattribute.' in usermeta ( '.$last.' )<br/>'); |
|---|
| 129 | | $datamap['last_name']->add_db_value( $last ); |
|---|
| 130 | | } |
|---|
| 131 | | |
|---|
| 132 | | if ( PW_CUSTOM_DATA ) |
|---|
| 133 | | pw_customdata::evaluateIncomingCustomData( $mail_user_id ); |
|---|
| 134 | | break; |
|---|
| 135 | | |
|---|
| 136 | | case 0: |
|---|
| 137 | | $mail_user_id = 0; |
|---|
| 138 | | if ($identity_user_id == 0) |
|---|
| 139 | | { |
|---|
| 140 | | // no user entry or identity entry |
|---|
| 141 | | $incominguser['status'] = 'new-user'; |
|---|
| 142 | | } |
|---|
| 143 | | else |
|---|
| 144 | | { |
|---|
| 145 | | //identity entry but no mail entry |
|---|
| 146 | | $incominguser['id'] = $identity_user_id; |
|---|
| 147 | | $incominguser['status'] = 'new-email'; |
|---|
| 148 | | } |
|---|
| 149 | | break; |
|---|
| 150 | | |
|---|
| 151 | | default: |
|---|
| 152 | | // More than one record returned for an email address |
|---|
| 153 | | $incominguser['status'] = "EMAILNOTUNIQUE"; |
|---|
| 154 | | return $incominguser; |
|---|
| 155 | | } |
|---|
| 156 | | |
|---|
| 157 | | pw_utils::printDebug( " Email address belongs toaccount #: $mail_user_id <br/>" ); |
|---|
| 158 | | pw_utils::printDebug( " Status: " . $incominguser['status'] . "<br/><br/>" ); |
|---|
| 159 | | |
|---|
| 160 | | return $incominguser; |
|---|
| 161 | | } // end function evaluateIncomingRequest |
|---|
| 434 | | function getIdentityFromCardTable($cardhash) |
|---|
| 435 | | { |
|---|
| 436 | | global $wpdb, $datamap; |
|---|
| 437 | | $card_info['found'] = FALSE; |
|---|
| 438 | | $card_info['error'] = ''; |
|---|
| 439 | | |
|---|
| 440 | | if (!$cardhash) |
|---|
| 441 | | { |
|---|
| 442 | | $card_info['error'] = 'INTERNALERROR'; |
|---|
| 443 | | pw_utils::printDebug( '<br/>Error in getIdentityFromCardTable function - no cardhash passed!<br/>'); |
|---|
| 444 | | } |
|---|
| 445 | | pw_utils::printDebug( '<br/>Getting Identity from Card Hash:' . $cardhash . '<br/>'); |
|---|
| 446 | | |
|---|
| 447 | | $query = "SELECT * FROM `" . PW_TABLE_PREFIX |
|---|
| 448 | | . "infocard_identities` WHERE cardhash = '" |
|---|
| 449 | | . $cardhash . "'"; |
|---|
| 450 | | $identity_info = $wpdb->get_results( $query, OBJECT ); |
|---|
| 451 | | switch (count($identity_info)) |
|---|
| 452 | | { |
|---|
| 453 | | case 1: |
|---|
| 454 | | // card is found |
|---|
| 455 | | $datamap['ID']->add_db_value($identity_info[0]->accountid); |
|---|
| 456 | | $datamap['cardhash']->add_db_value($identity_info[0]->cardhash); |
|---|
| 457 | | $datamap['cardhandle']->add_db_value($identity_info[0]->cardhandle); |
|---|
| 458 | | $card_info['found'] = TRUE; |
|---|
| 459 | | pw_utils::printDebug('Card Found: Acct# ' . $identity_info[0]->accountid . "<br/>" ); |
|---|
| 460 | | break; |
|---|
| 461 | | case 0: |
|---|
| 462 | | // Not found: return the defaults |
|---|
| 463 | | pw_utils::printDebug('Card not found via card hash.<br />' ); |
|---|
| 464 | | break; |
|---|
| 465 | | |
|---|
| 466 | | default: |
|---|
| 467 | | pw_utils::printDebug(' Multiple cards match the hash, return an error.<br />' ); |
|---|
| 468 | | $card_info['error'] = "CARDNOTUNIQUE"; |
|---|
| 469 | | } |
|---|
| 470 | | |
|---|
| 471 | | return $card_info; |
|---|
| 472 | | } |
|---|
| 473 | | |
|---|