|
| 1 | +<?php |
| 2 | + |
| 3 | +function authCheck($login) |
| 4 | +{ |
| 5 | + global $redis; |
| 6 | + |
| 7 | + try { |
| 8 | + $redis->auth($login['username'], $login['password']); |
| 9 | + } catch (Predis\Response\ServerException $exception) { |
| 10 | + return false; |
| 11 | + } |
| 12 | + return true; |
| 13 | +} |
| 14 | + |
| 15 | +// This fill will perform HTTP basic authentication. The authentication data will be sent and stored as plaintext |
| 16 | +// Please make sure to use HTTPS proxy such as Apache or Nginx to prevent traffic eavesdropping. |
| 17 | +function authHttpBasic() |
| 18 | +{ |
| 19 | + $realm = 'phpRedisAdmin'; |
| 20 | + |
| 21 | + if (!isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) { |
| 22 | + try { |
| 23 | + global $redis; |
| 24 | + $redis->ping(); |
| 25 | + } catch (Predis\Response\ServerException $exception) { |
| 26 | + header('HTTP/1.1 401 Unauthorized'); |
| 27 | + header('WWW-Authenticate: Basic realm="' . $realm . '"'); |
| 28 | + die('NOAUTH -- Authentication required'); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + $login = [ |
| 33 | + 'username' => $_SERVER['PHP_AUTH_USER'], |
| 34 | + 'password' => $_SERVER['PHP_AUTH_PW'], |
| 35 | + ]; |
| 36 | + |
| 37 | + if (!authCheck($login)) { |
| 38 | + header('HTTP/1.1 401 Unauthorized'); |
| 39 | + header('WWW-Authenticate: Basic realm="' . $realm . '"'); |
| 40 | + die('NOAUTH -- Authentication required'); |
| 41 | + } |
| 42 | + |
| 43 | + return $login; |
| 44 | +} |
| 45 | + |
| 46 | +// Perform auth using a standard HTML <form> submission and cookies to save login state |
| 47 | +function authCookie() |
| 48 | +{ |
| 49 | + if (!empty($_COOKIE['phpRedisAdminLogin'])) { |
| 50 | + // We have a cookie; is it correct? |
| 51 | + // Cookie value looks like "username:password-hash" |
| 52 | + $login = explode(':', $_COOKIE['phpRedisAdminLogin'], 2); |
| 53 | + if (count($login) === 2) { |
| 54 | + $login = [ |
| 55 | + 'username' => $login[0], |
| 56 | + 'password' => $login[1], |
| 57 | + ]; |
| 58 | + if (authCheck($login)) { |
| 59 | + return $login; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if (isset($_POST['username'], $_POST['password'])) { |
| 65 | + // Login form submitted; correctly? |
| 66 | + $login = [ |
| 67 | + 'username' => $_POST['username'], |
| 68 | + 'password' => $_POST['password'], |
| 69 | + ]; |
| 70 | + |
| 71 | + if (authCheck($login)) { |
| 72 | + setcookie('phpRedisAdminLogin', $login['username'] . ':' . $login['password']); |
| 73 | + // This should be an absolute URL, but that's a bit of a pain to generate; this will work |
| 74 | + header("Location: index.php"); |
| 75 | + die(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + global $redis; |
| 81 | + $redis->ping(); |
| 82 | + } catch (Predis\Response\ServerException $exception) { |
| 83 | + // If we're here, we don't have a valid login cookie and we don't have a |
| 84 | + // valid form submission, so redirect to the login page if we aren't |
| 85 | + // already on that page |
| 86 | + if (!defined('LOGIN_PAGE')) { |
| 87 | + header("Location: login.php"); |
| 88 | + die(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // We must be on the login page without a valid cookie or submission |
| 93 | + return null; |
| 94 | +} |
| 95 | + |
| 96 | +if (!empty($config['cookie_auth'])) { |
| 97 | + $login = authCookie(); |
| 98 | +} else { |
| 99 | + $login = authHttpBasic(); |
| 100 | +} |
0 commit comments