feverUtil.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. class FreshRSS_fever_Util {
  3. private const FEVER_PATH = DATA_PATH . '/fever';
  4. /**
  5. * Make sure the fever path exists and is writable.
  6. *
  7. * @return bool true if the path is writable, false otherwise.
  8. */
  9. public static function checkFeverPath(): bool {
  10. if (!file_exists(self::FEVER_PATH)) {
  11. @mkdir(self::FEVER_PATH, 0770, true);
  12. }
  13. $ok = touch(self::FEVER_PATH . '/index.html'); // is_writable() is not reliable for a folder on NFS
  14. if (!$ok) {
  15. Minz_Log::error("Could not save Fever API credentials. The directory does not have write access.");
  16. }
  17. return $ok;
  18. }
  19. /**
  20. * Return the corresponding path for a fever key.
  21. */
  22. public static function getKeyPath(string $feverKey): string {
  23. if (FreshRSS_Context::$system_conf === null) {
  24. throw new FreshRSS_Context_Exception('System configuration not initialised!');
  25. }
  26. $salt = sha1(FreshRSS_Context::$system_conf->salt);
  27. return self::FEVER_PATH . '/.key-' . $salt . '-' . $feverKey . '.txt';
  28. }
  29. /**
  30. * Update the fever key of a user.
  31. * @return string|false the Fever key, or false if the update failed
  32. */
  33. public static function updateKey(string $username, string $passwordPlain) {
  34. if (!self::checkFeverPath()) {
  35. return false;
  36. }
  37. self::deleteKey($username);
  38. $feverKey = strtolower(md5("{$username}:{$passwordPlain}"));
  39. $feverKeyPath = self::getKeyPath($feverKey);
  40. $result = file_put_contents($feverKeyPath, $username);
  41. if (is_int($result) && $result > 0) {
  42. return $feverKey;
  43. }
  44. Minz_Log::warning('Could not save Fever API credentials. Unknown error.', ADMIN_LOG);
  45. return false;
  46. }
  47. /**
  48. * Delete the Fever key of a user.
  49. *
  50. * @return bool true if the deletion succeeded, else false.
  51. */
  52. public static function deleteKey(string $username): bool {
  53. $userConfig = get_user_configuration($username);
  54. if ($userConfig === null) {
  55. return false;
  56. }
  57. $feverKey = $userConfig->feverKey;
  58. if (!ctype_xdigit($feverKey)) {
  59. return false;
  60. }
  61. $feverKeyPath = self::getKeyPath($feverKey);
  62. return @unlink($feverKeyPath);
  63. }
  64. }