function check_associate($useraccount)
 {
     $userid = $useraccount['userid'];
     $action = null;
     $key = null;
     if (!empty($_REQUEST['hauth_start'])) {
         $key = trim(strip_tags($_REQUEST['hauth_start']));
         $action = 'process';
     } else {
         if (!empty($_REQUEST['hauth_done'])) {
             $key = trim(strip_tags($_REQUEST['hauth_done']));
             $action = 'process';
         } else {
             if (!empty($_GET['link'])) {
                 $key = trim(strip_tags($_GET['link']));
                 $action = 'login';
             }
         }
     }
     if ($key == null) {
         return false;
     }
     $provider = $this->get_ha_provider($key);
     $source = strtolower($provider);
     if ($action == 'login') {
         // handle the login
         // after login come back to the same page
         $loginCallback = qa_path('', array(), qa_opt('site_url'));
         require_once $this->directory . 'Hybrid/Auth.php';
         require_once $this->directory . 'qa-open-utils.php';
         // prepare the configuration of HybridAuth
         $config = $this->get_ha_config($provider, $loginCallback);
         try {
             // try to login
             $hybridauth = new Hybrid_Auth($config);
             $adapter = $hybridauth->authenticate($provider);
             // if ok, create/refresh the user account
             $user = $adapter->getUserProfile();
             $duplicates = 0;
             if (!empty($user)) {
                 // prepare some data
                 $ohandle = null;
             }
             $oemail = null;
             if (empty($user->displayName)) {
                 $ohandle = $provider;
             } else {
                 $ohandle = preg_replace('/[\\@\\+\\/]/', ' ', $user->displayName);
             }
             if (strlen(@$user->email) && $user->emailVerified) {
                 // only if email is confirmed
                 $oemail = $user->email;
             }
             $duplicate = qa_db_user_login_find_duplicate__open($source, $user->identifier);
             if ($duplicate == null) {
                 // simply create a new login
                 qa_db_user_login_sync(true);
                 qa_db_user_login_add($userid, $source, $user->identifier);
                 if ($oemail) {
                     qa_db_user_login_set__open($source, $user->identifier, 'oemail', $oemail);
                 }
                 qa_db_user_login_set__open($source, $user->identifier, 'ohandle', $ohandle);
                 qa_db_user_login_sync(false);
                 // now that everything was added, log out to allow for multiple accounts
                 $adapter->logout();
                 // redirect to get rid of parameters
                 qa_redirect('logins');
             } else {
                 if ($duplicate['userid'] == $userid) {
                     // trying to add the same account, just update the email/handle
                     qa_db_user_login_sync(true);
                     if ($oemail) {
                         qa_db_user_login_set__open($source, $user->identifier, 'oemail', $oemail);
                     }
                     qa_db_user_login_set__open($source, $user->identifier, 'ohandle', $ohandle);
                     qa_db_user_login_sync(false);
                     // log out to allow for multiple accounts
                     $adapter->logout();
                     // redirect to get rid of parameters
                     qa_redirect('logins');
                 } else {
                     if (qa_get('confirm') == 2) {
                         return $duplicate;
                     } else {
                         qa_redirect('logins', array('link' => qa_get('link'), 'confirm' => 2));
                     }
                 }
             }
         } catch (Exception $e) {
             qa_redirect('logins', array('provider' => $provider, 'code' => $e->getCode()));
         }
     }
     if ($action == 'process') {
         require_once "Hybrid/Auth.php";
         require_once "Hybrid/Endpoint.php";
         Hybrid_Endpoint::process();
     }
     return false;
 }
 /**
  * 
  * @param string $userId
  * @param NKUser $userData
  */
 private function join_user_data($userId, NKUser $userData)
 {
     $user = qa_db_user_login_find(Q2ANKConnect::LOGIN_SOURCE, $userData->id());
     if (count($user) === 0) {
         qa_db_user_login_add($userId, Q2ANKConnect::LOGIN_SOURCE, $userData->id());
     }
 }
Example #3
0
 function qa_log_in_external_user($source, $identifier, $fields)
 {
     if (qa_to_override(__FUNCTION__)) {
         $args = func_get_args();
         return qa_call_override(__FUNCTION__, $args);
     }
     require_once QA_INCLUDE_DIR . 'db/users.php';
     $users = qa_db_user_login_find($source, $identifier);
     $countusers = count($users);
     if ($countusers > 1) {
         qa_fatal_error('External login mapped to more than one user');
     }
     // should never happen
     if ($countusers) {
         // user exists so log them in
         qa_set_logged_in_user($users[0]['userid'], $users[0]['handle'], false, $source);
     } else {
         // create and log in user
         require_once QA_INCLUDE_DIR . 'app/users-edit.php';
         qa_db_user_login_sync(true);
         $users = qa_db_user_login_find($source, $identifier);
         // check again after table is locked
         if (count($users) == 1) {
             qa_db_user_login_sync(false);
             qa_set_logged_in_user($users[0]['userid'], $users[0]['handle'], false, $source);
         } else {
             $handle = qa_handle_make_valid(@$fields['handle']);
             if (strlen(@$fields['email'])) {
                 // remove email address if it will cause a duplicate
                 $emailusers = qa_db_user_find_by_email($fields['email']);
                 if (count($emailusers)) {
                     qa_redirect('login', array('e' => $fields['email'], 'ee' => '1'));
                     unset($fields['email']);
                     unset($fields['confirmed']);
                 }
             }
             $userid = qa_create_new_user((string) @$fields['email'], null, $handle, isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC, @$fields['confirmed']);
             qa_db_user_login_add($userid, $source, $identifier);
             qa_db_user_login_sync(false);
             $profilefields = array('name', 'location', 'website', 'about');
             foreach ($profilefields as $fieldname) {
                 if (strlen(@$fields[$fieldname])) {
                     qa_db_user_profile_set($userid, $fieldname, $fields[$fieldname]);
                 }
             }
             if (strlen(@$fields['avatar'])) {
                 qa_set_user_avatar($userid, $fields['avatar']);
             }
             qa_set_logged_in_user($userid, $handle, false, $source);
         }
     }
 }
Example #4
0
 function qa_log_in_external_user($source, $identifier, $fields)
 {
     require_once QA_INCLUDE_DIR . 'qa-db-users.php';
     $users = qa_db_user_login_find($source, $identifier);
     $countusers = count($users);
     if ($countusers > 1) {
         qa_fatal_error('External login mapped to more than one user');
     }
     // should never happen
     if ($countusers) {
         // user exists so log them in
         qa_set_logged_in_user($users[0]['userid'], $users[0]['handle'], false, $source);
     } else {
         // create and log in user
         require_once QA_INCLUDE_DIR . 'qa-app-users-edit.php';
         $handle = qa_handle_make_valid(@$fields['handle']);
         $userid = qa_create_new_user((string) @$fields['email'], null, $handle, isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC, @$fields['confirmed']);
         qa_db_user_login_add($userid, $source, $identifier);
         $profilefields = array('name', 'location', 'website', 'about');
         foreach ($profilefields as $fieldname) {
             if (strlen(@$fields[$fieldname])) {
                 qa_db_user_profile_set($userid, $fieldname, $fields[$fieldname]);
             }
         }
         if (strlen(@$fields['avatar'])) {
             qa_set_user_avatar($userid, $fields['avatar']);
         }
         qa_set_logged_in_user($userid, $handle, false, $source);
     }
 }
/**
 * Overrides the default mechanism of logging in from external sources.
 *
 * Adds a different way of tracking the sessions and performs some 
 * additional tasks when creating an user account (setting new fields,
 * extra checks, etc).
 */
function qa_log_in_external_user($source, $identifier, $fields)
{
    require_once QA_INCLUDE_DIR . 'qa-db-users.php';
    $remember = qa_opt('open_login_remember') ? true : false;
    $users = qa_db_user_login_find($source, $identifier);
    $countusers = count($users);
    if ($countusers > 1) {
        qa_fatal_error('External login mapped to more than one user');
    }
    // should never happen
    /*
     * To allow for more than one account from the same openid/openauth provider to be 
     * linked to an Q2A user, we need to override the way session source is stored
     * Supposing userid 01 is linked to 2 yahoo accounts, the session source will be
     * something like 'yahoo-xyz' when logging in with the first yahoo account and
     * 'yahoo-xyt' when logging in with the other.
     */
    $aggsource = qa_open_login_get_new_source($source, $identifier);
    // prepare some data
    if (empty($fields['handle'])) {
        $ohandle = ucfirst($source);
    } else {
        $ohandle = preg_replace('/[\\@\\+\\/]/', ' ', $fields['handle']);
    }
    $oemail = null;
    if (strlen(@$fields['email']) && $fields['confirmed']) {
        // only if email is confirmed
        $oemail = $fields['email'];
    }
    if ($countusers) {
        // user exists so log them in
        //always update email and handle
        if ($oemail) {
            qa_db_user_login_set__open($source, $identifier, 'oemail', $oemail);
        }
        qa_db_user_login_set__open($source, $identifier, 'ohandle', $ohandle);
        qa_set_logged_in_user($users[0]['userid'], $users[0]['handle'], $remember, $aggsource);
    } else {
        // create and log in user
        require_once QA_INCLUDE_DIR . 'qa-app-users-edit.php';
        qa_db_user_login_sync(true);
        $users = qa_db_user_login_find($source, $identifier);
        // check again after table is locked
        if (count($users) == 1) {
            //always update email and handle
            if ($oemail) {
                qa_db_user_login_set__open($source, $identifier, 'oemail', $oemail);
            }
            qa_db_user_login_set__open($source, $identifier, 'ohandle', $ohandle);
            qa_db_user_login_sync(false);
            qa_set_logged_in_user($users[0]['userid'], $users[0]['handle'], $remember, $aggsource);
        } else {
            $handle = qa_handle_make_valid(@$fields['handle']);
            // check if email address already exists
            $emailusers = array();
            if (strlen(@$fields['email']) && $fields['confirmed']) {
                // only if email is confirmed
                $emailusers = qa_db_user_find_by_email_or_oemail__open($fields['email']);
                if (count($emailusers)) {
                    // unset regular email to prevent duplicates
                    unset($fields['email']);
                }
            }
            $userid = qa_create_new_user((string) @$fields['email'], null, $handle, isset($fields['level']) ? $fields['level'] : QA_USER_LEVEL_BASIC, @$fields['confirmed']);
            qa_db_user_set($userid, 'oemail', $oemail);
            qa_db_user_login_add($userid, $source, $identifier);
            qa_db_user_login_set__open($source, $identifier, 'oemail', $oemail);
            qa_db_user_login_set__open($source, $identifier, 'ohandle', $ohandle);
            qa_db_user_login_sync(false);
            $profilefields = array('name', 'location', 'website', 'about');
            foreach ($profilefields as $fieldname) {
                if (strlen(@$fields[$fieldname])) {
                    qa_db_user_profile_set($userid, $fieldname, $fields[$fieldname]);
                }
            }
            if (strlen(@$fields['avatar'])) {
                qa_set_user_avatar($userid, $fields['avatar']);
            }
            qa_set_logged_in_user($userid, $handle, $remember, $aggsource);
            return count($emailusers);
        }
    }
    return 0;
}