]> git.mxchange.org Git - friendica.git/blobdiff - include/auth.php
"system_unavailable.php" is unavailable now
[friendica.git] / include / auth.php
index 90509468c5a16dba15977ac659303e760c0c5939..a02c18d1dbf0f64aa045e418e9155cd91af3773d 100644 (file)
@@ -4,6 +4,7 @@ use Friendica\App;
 use Friendica\Core\System;
 use Friendica\Core\Config;
 use Friendica\Database\DBM;
+use Friendica\Model\User;
 
 require_once 'include/security.php';
 require_once 'include/datetime.php';
@@ -12,13 +13,21 @@ require_once 'include/datetime.php';
 if (isset($_COOKIE["Friendica"])) {
        $data = json_decode($_COOKIE["Friendica"]);
        if (isset($data->uid)) {
-               $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
-               FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
-                       intval($data->uid)
+
+               $user = dba::select('user',
+                       [],
+                       [
+                               'uid'             => $data->uid,
+                               'blocked'         => false,
+                               'account_expired' => false,
+                               'account_removed' => false,
+                               'verified'        => true,
+                       ],
+                       ['limit' => 1]
                );
 
-               if ($r) {
-                       if ($data->hash != cookie_hash($r[0])) {
+               if (DBM::is_result($user)) {
+                       if ($data->hash != cookie_hash($user)) {
                                logger("Hash for user " . $data->uid . " doesn't fit.");
                                nuke_session();
                                goaway(System::baseUrl());
@@ -28,11 +37,11 @@ if (isset($_COOKIE["Friendica"])) {
                        // Expires after 7 days by default,
                        // can be set via system.auth_cookie_lifetime
                        $authcookiedays = Config::get('system', 'auth_cookie_lifetime', 7);
-                       new_cookie($authcookiedays * 24 * 60 * 60, $r[0]);
+                       new_cookie($authcookiedays * 24 * 60 * 60, $user);
 
                        // Do the authentification if not done by now
                        if (!isset($_SESSION) || !isset($_SESSION['authenticated'])) {
-                               authenticate_success($r[0]);
+                               authenticate_success($user);
 
                                if (Config::get('system', 'paranoia')) {
                                        $_SESSION['addr'] = $data->ip;
@@ -74,12 +83,18 @@ if (isset($_SESSION) && x($_SESSION, 'authenticated') && (!x($_POST, 'auth-param
                        goaway(System::baseUrl());
                }
 
-               $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
-               FROM `user` WHERE `uid` = %d AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
-                       intval($_SESSION['uid'])
+               $user = dba::select('user',
+                       [],
+                       [
+                               'uid'             => $_SESSION['uid'],
+                               'blocked'         => false,
+                               'account_expired' => false,
+                               'account_removed' => false,
+                               'verified'        => true,
+                       ],
+                       ['limit' => 1]
                );
-
-               if (!DBM::is_result($r)) {
+               if (!DBM::is_result($user)) {
                        nuke_session();
                        goaway(System::baseUrl());
                }
@@ -94,45 +109,48 @@ if (isset($_SESSION) && x($_SESSION, 'authenticated') && (!x($_POST, 'auth-param
                        $_SESSION['last_login_date'] = datetime_convert('UTC', 'UTC');
                        $login_refresh = true;
                }
-               authenticate_success($r[0], false, false, $login_refresh);
+               authenticate_success($user, false, false, $login_refresh);
        }
 } else {
        session_unset();
-       if (x($_POST, 'password') && strlen($_POST['password'])) {
-               $encrypted = hash('whirlpool', trim($_POST['password']));
-       } else {
-               if ((x($_POST, 'openid_url')) && strlen($_POST['openid_url']) ||
-                       (x($_POST, 'username')) && strlen($_POST['username'])) {
+       if (
+               !(x($_POST, 'password') && strlen($_POST['password']))
+               && (
+                       x($_POST, 'openid_url') && strlen($_POST['openid_url'])
+                       || x($_POST, 'username') && strlen($_POST['username'])
+               )
+       ) {
+               $noid = Config::get('system', 'no_openid');
 
-                       $noid = Config::get('system', 'no_openid');
+               $openid_url = trim(strlen($_POST['openid_url']) ? $_POST['openid_url'] : $_POST['username']);
 
-                       $openid_url = trim((strlen($_POST['openid_url']) ? $_POST['openid_url'] : $_POST['username']));
+               // validate_url alters the calling parameter
 
-                       // validate_url alters the calling parameter
-                       $temp_string = $openid_url;
+               $temp_string = $openid_url;
 
-                       // if it's an email address or doesn't resolve to a URL, fail.
-                       if ($noid || strpos($temp_string, '@') || !validate_url($temp_string)) {
-                               $a = get_app();
-                               notice(t('Login failed.') . EOL);
-                               goaway(System::baseUrl());
-                               // NOTREACHED
-                       }
+               // if it's an email address or doesn't resolve to a URL, fail.
 
-                       // Otherwise it's probably an openid.
-                       try {
-                               require_once('library/openid.php');
-                               $openid = new LightOpenID;
-                               $openid->identity = $openid_url;
-                               $_SESSION['openid'] = $openid_url;
-                               $_SESSION['remember'] = $_POST['remember'];
-                               $openid->returnUrl = System::baseUrl(true) . '/openid';
-                               goaway($openid->authUrl());
-                       } catch (Exception $e) {
-                               notice(t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . '<br /><br >' . t('The error message was:') . ' ' . $e->getMessage());
-                       }
+               if ($noid || strpos($temp_string, '@') || !validate_url($temp_string)) {
+                       $a = get_app();
+                       notice(t('Login failed.') . EOL);
+                       goaway(System::baseUrl());
                        // NOTREACHED
                }
+
+               // Otherwise it's probably an openid.
+
+               try {
+                       require_once('library/openid.php');
+                       $openid = new LightOpenID;
+                       $openid->identity = $openid_url;
+                       $_SESSION['openid'] = $openid_url;
+                       $_SESSION['remember'] = $_POST['remember'];
+                       $openid->returnUrl = System::baseUrl(true) . '/openid';
+                       goaway($openid->authUrl());
+               } catch (Exception $e) {
+                       notice(t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . '<br /><br >' . t('The error message was:') . ' ' . $e->getMessage());
+               }
+               // NOTREACHED
        }
 
        if (x($_POST, 'auth-params') && $_POST['auth-params'] === 'login') {
@@ -157,18 +175,9 @@ if (isset($_SESSION) && x($_SESSION, 'authenticated') && (!x($_POST, 'auth-param
                if ($addon_auth['authenticated'] && count($addon_auth['user_record'])) {
                        $record = $addon_auth['user_record'];
                } else {
-
-                       // process normal login request
-
-                       $r = q("SELECT `user`.*, `user`.`pubkey` as `upubkey`, `user`.`prvkey` as `uprvkey`
-                               FROM `user` WHERE (`email` = '%s' OR `nickname` = '%s')
-                               AND `password` = '%s' AND NOT `blocked` AND NOT `account_expired` AND NOT `account_removed` AND `verified` LIMIT 1",
-                               dbesc(trim($_POST['username'])),
-                               dbesc(trim($_POST['username'])),
-                               dbesc($encrypted)
-                       );
-                       if (DBM::is_result($r)) {
-                               $record = $r[0];
+                       $user_id = User::authenticate(trim($_POST['username']), trim($_POST['password']));
+                       if ($user_id) {
+                               $record = dba::select('user', [], ['uid' => $user_id], ['limit' => 1]);
                        }
                }