login.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/auth.utils.php");
  3. require_once ($_SERVER['DOCUMENT_ROOT']."/inc/languages/" . HTML_LANG . ".php");
  4. $error = '';
  5. function getSafeRedirectUrl(string $default = '/'): string {
  6. $url = filter_input(INPUT_GET, 'redirect_url', FILTER_SANITIZE_URL)
  7. ?? filter_input(INPUT_POST, 'redirect_url', FILTER_SANITIZE_URL)
  8. ?? $default;
  9. $decodedUrl = urldecode($url);
  10. // Проверяем:
  11. // 1. URL начинается с `/` (но не `//` или `http://`)
  12. // 2. Содержит только разрешённые символы (a-z, 0-9, -, _, /, ?, =, &, ., ~)
  13. if (!preg_match('/^\/(?!\/)[a-z0-9\-_\/?=&.~]*$/i', $decodedUrl)) {
  14. return $default;
  15. }
  16. return $url;
  17. }
  18. // Использование
  19. $redirect_url = getSafeRedirectUrl(DEFAULT_PAGE);
  20. if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit'])) {
  21. $login = trim($_POST['login']);
  22. $password = trim($_POST['password']);
  23. // validate if login is empty
  24. if (empty($login)) {
  25. $error .= '<p class="error">'.WEB_msg_login_hint.'.</p>';
  26. }
  27. // validate if password is empty
  28. if (empty($password)) {
  29. $error .= '<p class="error">'.WEB_msg_password_hint.'.</p>';
  30. }
  31. if (empty($error)) {
  32. if (login($db_link)) {
  33. $redirect_url = urldecode($redirect_url);
  34. header("Location: $redirect_url");
  35. }
  36. }
  37. }
  38. ?>
  39. <!DOCTYPE html>
  40. <html>
  41. <head>
  42. <title><?php echo WEB_site_title; ?> login</title>
  43. <link rel="stylesheet" type="text/css" href="/css/<?php echo HTML_STYLE.'.css'; ?>">
  44. <link rel="stylesheet" type="text/css" href="/login.css" >
  45. <meta http-equiv="content-type" content="application/xhtml+xml" />
  46. <meta charset="UTF-8" />
  47. </head>
  48. <body>
  49. <div class="login">
  50. <h1><?php echo WEB_msg_login; ?></h1>
  51. <form action="" method="post">
  52. <label for="username">
  53. <i class="fas fa-user"></i>
  54. </label>
  55. <input type="text" name="login" placeholder="<?php echo WEB_msg_username; ?>" id="login" required>
  56. <label for="password">
  57. <i class="fas fa-lock"></i>
  58. </label>
  59. <input type="password" name="password" placeholder="<?php echo WEB_msg_password; ?>" id="password" required>
  60. <input type="hidden" name="redirect_url" value="<?php print htmlspecialchars($redirect_url); ?>">
  61. <input type="submit" name="submit" value="<?php echo WEB_btn_login; ?>">
  62. </form>
  63. </div>
  64. </body>
  65. </html>