]> git.wh0rd.org Git - tt-rss.git/commitdiff
auth_internal: use PDO + other fixes
authorAndrew Dolgov <noreply@fakecake.org>
Sat, 2 Dec 2017 21:18:08 +0000 (00:18 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Sat, 2 Dec 2017 21:18:08 +0000 (00:18 +0300)
classes/plugin.php
include/functions.php
plugins/auth_internal/init.php

index fcf329ca10c2b356a8628d8ee42ae2e8785dc40d..fb4e08fb15d7f1a924e07d03b03d10901faa5e4a 100644 (file)
@@ -2,6 +2,9 @@
 abstract class Plugin {
        const API_VERSION_COMPAT = 1;
 
+       /** @var PDO */
+       protected $pdo;
+
        abstract function init($host);
 
        abstract function about();
index 1d7033c899b46e773d4677a4df9c62947b3630b1..f88321ec9844ac490387981595e9483e16439795 100644 (file)
                                        (access_key, feed_id, is_cat, owner_uid)
                                        VALUES (?, ?, ?, ?)");
 
-                       $sth->execute([$key, $feed_id, $is_cat, $owner_uid]);
+                       $sth->execute([$key, $feed_id, (int)$is_cat, $owner_uid]);
 
                        return $key;
                }
index dc49229f623b21d2772a103816184551be50f846..0374295f3ec103ab20e824fc602e290bd3d4d967 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 class Auth_Internal extends Plugin implements IAuthModule {
+
        private $host;
 
        function about() {
@@ -9,8 +10,9 @@ class Auth_Internal extends Plugin implements IAuthModule {
                        true);
        }
 
-       function init($host) {
+    function init($host) {
                $this->host = $host;
+               $this->pdo = Db::pdo();
 
                $host->add_hook($host::HOOK_AUTH_USER, $this);
        }
@@ -19,16 +21,16 @@ class Auth_Internal extends Plugin implements IAuthModule {
 
                $pwd_hash1 = encrypt_password($password);
                $pwd_hash2 = encrypt_password($password, $login);
-               $login = db_escape_string($login);
-               $otp = db_escape_string($_REQUEST["otp"]);
+               $otp = $_REQUEST["otp"];
 
                if (get_schema_version() > 96) {
                        if (!defined('AUTH_DISABLE_OTP') || !AUTH_DISABLE_OTP) {
 
-                               $result = db_query("SELECT otp_enabled,salt FROM ttrss_users WHERE
-                                       login = '$login'");
+                               $sth = $this->pdo->prepare("SELECT otp_enabled,salt FROM ttrss_users WHERE
+                                       login = ?");
+                               $sth->execute([$login]);
 
-                               if (db_num_rows($result) > 0) {
+                               if ($row = $sth->fetch()) {
 
                                        require_once "lib/otphp/vendor/base32.php";
                                        require_once "lib/otphp/lib/otp.php";
@@ -36,8 +38,8 @@ class Auth_Internal extends Plugin implements IAuthModule {
 
                                        $base32 = new Base32();
 
-                                       $otp_enabled = sql_bool_to_bool(db_fetch_result($result, 0, "otp_enabled"));
-                                       $secret = $base32->encode(sha1(db_fetch_result($result, 0, "salt")));
+                                       $otp_enabled = $row['otp_enabled'];
+                                       $secret = $base32->encode(sha1($row['salt']));
 
                                        $topt = new \OTPHP\TOTP($secret);
                                        $otp_check = $topt->now();
@@ -79,109 +81,127 @@ class Auth_Internal extends Plugin implements IAuthModule {
 
                if (get_schema_version() > 87) {
 
-                       $result = db_query("SELECT salt FROM ttrss_users WHERE
-                               login = '$login'");
+                       $sth = $this->pdo->prepare("SELECT salt FROM ttrss_users WHERE login = ?");
+                       $sth->execute([$login]);
 
-                       if (db_num_rows($result) != 1) {
-                               return false;
-                       }
+                       if ($row = $sth->fetch()) {
+                               $salt = $row['salt'];
 
-                       $salt = db_fetch_result($result, 0, "salt");
+                               if ($salt == "") {
 
-                       if ($salt == "") {
+                                       $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
+                        login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
 
-                               $query = "SELECT id
-                   FROM ttrss_users WHERE
-                                       login = '$login' AND (pwd_hash = '$pwd_hash1' OR
-                                       pwd_hash = '$pwd_hash2')";
+                                       $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
 
-                               // verify and upgrade password to new salt base
+                                       // verify and upgrade password to new salt base
 
-                               $result = db_query($query);
+                                       if ($row = $sth->fetch()) {
+                                               // upgrade password to MODE2
 
-                               if (db_num_rows($result) == 1) {
-                                       // upgrade password to MODE2
+                                               $user_id = $row['id'];
 
-                                       $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
-                                       $pwd_hash = encrypt_password($password, $salt, true);
+                                               $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
+                                               $pwd_hash = encrypt_password($password, $salt, true);
+
+                                               $sth = $this->pdo->prepare("UPDATE ttrss_users SET
+                                               pwd_hash = ?, salt = ? WHERE login = ?");
 
-                                       db_query("UPDATE ttrss_users SET
-                                               pwd_hash = '$pwd_hash', salt = '$salt' WHERE login = '$login'");
+                                               $sth->execute([$pwd_hash, $salt, $login]);
 
-                                       $query = "SELECT id
-                           FROM ttrss_users WHERE
-                                               login = '$login' AND pwd_hash = '$pwd_hash'";
+                                               return $user_id;
+
+                                       } else {
+                                               return false;
+                                       }
 
                                } else {
-                                       return false;
+                                       $pwd_hash = encrypt_password($password, $salt, true);
+
+                                       $sth = $this->pdo->prepare("SELECT id
+                                 FROM ttrss_users WHERE
+                                             login = ? AND pwd_hash = ?");
+                                       $sth->execute([$login, $pwd_hash]);
+
+                                       if ($row = $sth->fetch()) {
+                                               return $row['id'];
+                                       }
                                }
 
                        } else {
+                               $sth = $this->pdo->prepare("SELECT id
+                    FROM ttrss_users WHERE
+                      login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
 
-                               $pwd_hash = encrypt_password($password, $salt, true);
-
-                               $query = "SELECT id
-                        FROM ttrss_users WHERE
-                                       login = '$login' AND pwd_hash = '$pwd_hash'";
+                               $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
 
+                               if ($row = $sth->fetch()) {
+                                       return $row['id'];
+                               }
                        }
-
                } else {
-                       $query = "SELECT id
-                FROM ttrss_users WHERE
-                               login = '$login' AND (pwd_hash = '$pwd_hash1' OR
-                                       pwd_hash = '$pwd_hash2')";
-               }
+                       $sth = $this->pdo->prepare("SELECT id
+                    FROM ttrss_users WHERE
+                      login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
 
-               $result = db_query($query);
+                       $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
 
-               if (db_num_rows($result) == 1) {
-                       return db_fetch_result($result, 0, "id");
-               }
+                       if ($row = $sth->fetch()) {
+                               return $row['id'];
+                       }
+        }
 
                return false;
        }
 
        function check_password($owner_uid, $password) {
-               $owner_uid = db_escape_string($owner_uid);
 
-               $result = db_query("SELECT salt,login FROM ttrss_users WHERE
-                       id = '$owner_uid'");
+               $sth = $this->pdo->prepare("SELECT salt,login FROM ttrss_users WHERE
+                       id = ?");
+               $sth->execute([$owner_uid]);
 
-               $salt = db_fetch_result($result, 0, "salt");
-               $login = db_fetch_result($result, 0, "login");
+               if ($row = $sth->fetch()) {
 
-               if (!$salt) {
-                       $password_hash1 = encrypt_password($password);
-                       $password_hash2 = encrypt_password($password, $login);
+                       $salt = $row['salt'];
+                       $login = $row['login'];
 
-                       $query = "SELECT id FROM ttrss_users WHERE
-                               id = '$owner_uid' AND (pwd_hash = '$password_hash1' OR
-                               pwd_hash = '$password_hash2')";
+                       if (!$salt) {
+                               $password_hash1 = encrypt_password($password);
+                               $password_hash2 = encrypt_password($password, $login);
 
-               } else {
-                       $password_hash = encrypt_password($password, $salt, true);
+                               $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
+                    id = ? AND (pwd_hash = ? OR pwd_hash = ?)");
 
-                       $query = "SELECT id FROM ttrss_users WHERE
-                               id = '$owner_uid' AND pwd_hash = '$password_hash'";
-               }
+                               $sth->execute([$owner_uid, $password_hash1, $password_hash2]);
+
+                               return $sth->fetch();
 
-               $result = db_query($query);
+                       } else {
+                               $password_hash = encrypt_password($password, $salt, true);
+
+                               $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
+                    id = ? AND pwd_hash = ?");
+
+                               $sth->execute([$owner_uid, $password_hash]);
+
+                               return $sth->fetch();
+                       }
+               }
 
-               return db_num_rows($result) != 0;
+        return false;
        }
 
        function change_password($owner_uid, $old_password, $new_password) {
-               $owner_uid = db_escape_string($owner_uid);
 
                if ($this->check_password($owner_uid, $old_password)) {
 
                        $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
                        $new_password_hash = encrypt_password($new_password, $new_salt, true);
 
-                       db_query("UPDATE ttrss_users SET
-                               pwd_hash = '$new_password_hash', salt = '$new_salt', otp_enabled = false
-                                       WHERE id = '$owner_uid'");
+                       $sth = $this->pdo->prepare("UPDATE ttrss_users SET
+                               pwd_hash = ?, salt = ?, otp_enabled = false
+                                       WHERE id = ?");
+                       $sth->execute([$new_password_hash, $new_salt, $owner_uid]);
 
                        $_SESSION["pwd_hash"] = $new_password_hash;