base.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. class Auth_Base {
  3. private $dbh;
  4. private $pdo;
  5. function __construct() {
  6. $this->dbh = Db::get();
  7. $this->pdo = Db::pdo();
  8. }
  9. /**
  10. * @SuppressWarnings(unused)
  11. */
  12. function check_password($owner_uid, $password) {
  13. return false;
  14. }
  15. /**
  16. * @SuppressWarnings(unused)
  17. */
  18. function authenticate($login, $password) {
  19. return false;
  20. }
  21. // Auto-creates specified user if allowed by system configuration
  22. // Can be used instead of find_user_by_login() by external auth modules
  23. function auto_create_user($login, $password = false) {
  24. if ($login && defined('AUTH_AUTO_CREATE') && AUTH_AUTO_CREATE) {
  25. $user_id = $this->find_user_by_login($login);
  26. if (!$password) $password = make_password();
  27. if (!$user_id) {
  28. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  29. $pwd_hash = encrypt_password($password, $salt, true);
  30. $sth = $this->pdo->prepare("INSERT INTO ttrss_users
  31. (login,access_level,last_login,created,pwd_hash,salt)
  32. VALUES (?, 0, null, NOW(), ?,?)");
  33. $sth->execute([$login, $pwd_hash, $salt]);
  34. return $this->find_user_by_login($login);
  35. } else {
  36. return $user_id;
  37. }
  38. }
  39. return $this->find_user_by_login($login);
  40. }
  41. function find_user_by_login($login) {
  42. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  43. login = ?");
  44. $sth->execute([$login]);
  45. if ($row = $sth->fetch()) {
  46. return $row["id"];
  47. } else {
  48. return false;
  49. }
  50. }
  51. }