auth: reusable functions for password verification
[minimedit.git] / auth.inc.php
index 94f0dcc8547f190d5758e7fdad1881a56ce72717..fd11b1353a1dfe1feaa82dc81bad675cc714a9ea 100644 (file)
@@ -1,4 +1,19 @@
 <?php
+function login_password_verify($input, $test)
+{
+       if (substr($test, 0, 1) != '$') {
+               # plaintext match for uncrypted passwords
+               return $input === $test;
+       }
+       return password_verify($input, $test);
+}
+
+function login_setcookie()
+{
+       global $User;
+       return setcookie('login', $User['auth'], 0, '/');
+}
+
 function login($inuser, $inpass = NULL)
 {
        if (empty($inuser)) return;
@@ -16,12 +31,7 @@ function login($inuser, $inpass = NULL)
        # verify password
        $authhash = md5($usertest);
        if (isset($inpass)) {
-               if (substr($usertest, 0, 1) == '$') {
-                       if (!password_verify($inpass, $usertest)) return;
-               }
-               else {
-                       if ($inpass !== $usertest) return;
-               }
+               if (!login_password_verify($inpass, $usertest)) return;
        }
        else {
                if ($inauth !== $authhash) return;