Exemple #1
0
    //check for validation errors in custom profile fields
    $cpf_check = cpf_validate_format();
    if ($cpf_check[0] === false) {
        unset($cpf_check[0]);
        foreach ($cpf_check as $cpf_error) {
            $registration_errors[] = $cpf_error;
        }
    }

    // validate HybridAuth provider and user id and check if it's already in the db (shouldn't be
    // because the user would be logged in the system rather than redirected here)
    // check if there are any available alternative providers for authentication
    if (!empty($_POST['provider_id'])) {
        require_once 'modules/auth/methods/hybridauth/config.php';
        require_once 'modules/auth/methods/hybridauth/Hybrid/Auth.php';
        $config = get_hybridauth_config();

        $hybridauth = new Hybrid_Auth( $config );
        $allProviders = $hybridauth->getProviders();
        $provider = '';
        $warning = '';

        // check if $_POST['provider'] is valid and enabled
        if (count($allProviders) && array_key_exists(ucfirst($_POST['provider']), $allProviders)) {
            $provider = strtolower($_POST['provider']);
        }
        if (!empty($_POST['provider_id']) && !empty($provider)) {
            // if !empty($provider), it means the provider is existent and valid - it's checked above
            try {
                // create an instance for Hybridauth with the configuration file path as parameter
                $hybridauth = new Hybrid_Auth($config);
Exemple #2
0
function hybridauth_login() {
    //this is needed so as to include the HybridAuth error codes
    global $language, $language_codes, $siteName, $Institution, $InstitutionUrl;
    if (isset($language)) {
        // include_messages
        include "lang/$language/common.inc.php";
        $extra_messages = "config/{$language_codes[$language]}.inc.php";
        if (file_exists($extra_messages)) {
            include $extra_messages;
        } else {
            $extra_messages = false;
        }
        include "lang/$language/messages.inc.php";
        if ($extra_messages) {
            include $extra_messages;
        }
    }
    // end HybridAuth messages inclusion
    
    
    global $warning;
    
    // include HubridAuth libraries
    require_once 'modules/auth/methods/hybridauth/config.php';
    require_once 'modules/auth/methods/hybridauth/Hybrid/Auth.php';
    $config = get_hybridauth_config();
    
    // check for errors and whatnot
    $warning = '';
    
    if (isset($_GET['error'])) {
        Session::Messages(q(trim(strip_tags($_GET['error']))));
    }

    // if user select a provider to login with
    // then inlcude hybridauth config and main class
    // then try to authenticate te current user
    // finally redirect him to his profile page
    if (isset($_GET['provider'])) {
        try {
            // create an instance for Hybridauth with the configuration file path as parameter
            $hybridauth = new Hybrid_Auth($config);
            
            // set selected provider name
            $provider = @trim(strip_tags($_GET["provider"]));
        
            // try to authenticate the selected $provider
            $adapter = $hybridauth->authenticate( $provider );
            
            // grab the user profile
            $user_data = $adapter->getUserProfile();
            
            //user profile debug print
            //echo $user_data->displayName;
            //echo $user_data->email;
            //echo $user_data->photoURL;
            //echo $user_data->identifier;
            
        } catch (Exception $e) {
            // In case we have errors 6 or 7, then we have to use Hybrid_Provider_Adapter::logout() to
            // let hybridauth forget all about the user so we can try to authenticate again.
        
            // Display the recived error,
            // to know more please refer to Exceptions handling section on the userguide
            switch($e->getCode()) {
                case 0: Session::Messages($GLOBALS['langProviderError1']); break;
                case 1: Session::Messages($GLOBALS['langProviderError2']); break;
                case 2: Session::Messages($GLOBALS['langProviderError3']); break;
                case 3: Session::Messages($GLOBALS['langProviderError4']); break;
                case 4: Session::Messages($GLOBALS['langProviderError5']); break;
                case 5: Session::Messages($GLOBALS['langProviderError6']); break;
                case 6: Session::Messages($GLOBALS['langProviderError7']); $adapter->logout(); break;
                case 7: Session::Messages($GLOBALS['langProviderError8']); $adapter->logout(); break;
            }
        
            // debug messages for hybridauth errors
            //$warning .= "<br /><br /><b>Original error message:</b> " . $e->getMessage();
            //$warning .= "<hr /><pre>Trace:<br />" . $e->getTraceAsString() . "</pre>";
        
            return false;
        }
    } //endif( isset( $_GET["provider"] ) && $_GET["provider"] )
    
    
    // *****************************
    // from here on runs an alternative version of proccess_login() where
    // instead of a password, the provider user id is used and matched against
    // the corresponding field in the db table.
    global $surname, $givenname, $email, $status, $is_admin, $language,
    $langInvalidId, $langAccountInactive1, $langAccountInactive2,
    $langNoCookies, $langEnterPlatform, $urlServer, $langHere,
    $auth_ids, $inactive_uid, $langTooManyFails;
    
    $pass = $user_data->identifier; //password = provider user id
    $auth = get_auth_active_methods();
    //$is_eclass_unique = is_eclass_unique();
    
    unset($_SESSION['uid']);
    $auth_allow = 0;
    
    if (get_config('login_fail_check')) {
        $r = Database::get()->querySingle("SELECT 1 FROM login_failure WHERE ip = '" . $_SERVER['REMOTE_ADDR'] . "'
                                       AND COUNT > " . intval(get_config('login_fail_threshold')) . "
                                       AND DATE_SUB(CURRENT_TIMESTAMP, interval " . intval(get_config('login_fail_deny_interval')) . " minute) < last_fail");
    }
    if (get_config('login_fail_check') && $r) {
        $auth_allow = 8;
    } else {
        $auth_id = array_search(strtolower($provider), $auth_ids);
        $myrow = Database::get()->querySingle("SELECT user.id, surname,
                    givenname, password, username, status, email, lang,
                    verified_mail, uid
                FROM user, user_ext_uid
                WHERE user.id = user_ext_uid.user_id AND
                      user_ext_uid.auth_id = ?d AND
                      user_ext_uid.uid = ?s",
            $auth_id, $user_data->identifier);
        $exists = 0;
        if (!isset($_COOKIE) or count($_COOKIE) == 0) {
            // Disallow login when cookies are disabled
            $auth_allow = 5;
        } elseif ($myrow) {
            $exists = 1;
            if (!empty($auth)) {
                if (in_array($myrow->password, $auth_ids)) {
                    // alternate methods login
                    //$auth_allow = alt_login($myrow, $provider, $pass); //this should NOT be called during HybridAuth!
                } else {
                    // eclass login
                    $auth_allow = login($myrow, $provider, $pass, $provider);
                }
            } else {
                $tool_content .= "<br>$langInvalidAuth<br>";
            }
        }
        if (!$exists and !$auth_allow) {
            // Since HybridAuth was used and there is not user id matched in the db, send the user to the registration form.
            header('Location: ' . $urlServer . 'modules/auth/registration.php?provider=' . $provider);
            
            // from this point and on, the code does not need to run since the user is redirected to the registration page
            $auth_allow = 4;
        }
    }
    
    if (!isset($_SESSION['uid'])) {
        switch ($auth_allow) {
            case 1:
                session_regenerate_id();
                break;
            case 2:
                $warning .= "<p class='alert alert-warning'>$langInvalidId</p>";
                break;
            case 3:
                $warning .= "<p class='alert alert-warning'>$langAccountInactive1 " .
                    "<a href='modules/auth/contactadmin.php?userid=$inactive_uid&amp;h=" .
                    token_generate("userid=$inactive_uid") . "'>$langAccountInactive2</a></p>";
                break;
            case 4:
                $warning .= "<p class='alert alert-warning'>$langInvalidId</p>";
                increaseLoginFailure();
                break;
            case 5:
                $warning .= "<p class='alert alert-warning'>$langNoCookies</p>";
                break;
            case 6:
                $warning .= "<p class='alert alert-info'>$langEnterPlatform <a href='{$urlServer}secure/index.php'>$langHere</a></p>";
                break;
            case 7:
                $warning .= "<p class='alert alert-info'>$langEnterPlatform <a href='{$urlServer}modules/auth/cas.php'>$langHere</a></p>";
                break;
            case 8:
                $warning .= "<p class='alert alert-danger''>$langTooManyFails</p>";
                break;
        }
    } else {
        Database::get()->query("INSERT INTO loginout (loginout.id_user, loginout.ip, loginout.when, loginout.action) "
                . "VALUES ($_SESSION[uid], '$_SERVER[REMOTE_ADDR]', NOW(), 'LOGIN')");
        if (get_config('email_verification_required') and
            get_mail_ver_status($_SESSION['uid']) == EMAIL_VERIFICATION_REQUIRED) {
            $_SESSION['mail_verification_required'] = 1;
            $next = "modules/auth/mail_verify_change.php";
        } elseif (isset($_POST['next'])) {
            $next = $_POST['next'];
        } else {
            $next = '';
        }
        resetLoginFailure();
        redirect_to_home_page($next);
    }
}