feverUtil.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. class FreshRSS_fever_Util {
  3. const FEVER_PATH = DATA_PATH . '/fever';
  4. /**
  5. * Make sure the fever path exists and is writable.
  6. *
  7. * @return boolean true if the path is writable, else false.
  8. */
  9. public static function checkFeverPath() {
  10. if (!file_exists(self::FEVER_PATH)) {
  11. @mkdir(self::FEVER_PATH, 0770, true);
  12. }
  13. $ok = is_writable(self::FEVER_PATH);
  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. * @param string $feverKey
  23. * @return string
  24. */
  25. public static function getKeyPath($feverKey) {
  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. *
  32. * @param string $username
  33. * @param string $passwordPlain
  34. * @return string|false the Fever key, or false if the update failed
  35. */
  36. public static function updateKey($username, $passwordPlain) {
  37. $ok = self::checkFeverPath();
  38. if (!$ok) {
  39. return false;
  40. }
  41. self::deleteKey($username);
  42. $feverKey = strtolower(md5("{$username}:{$passwordPlain}"));
  43. $feverKeyPath = self::getKeyPath($feverKey);
  44. $res = file_put_contents($feverKeyPath, $username);
  45. if ($res !== false) {
  46. return $feverKey;
  47. } else {
  48. Minz_Log::warning('Could not save Fever API credentials. Unknown error.', ADMIN_LOG);
  49. return false;
  50. }
  51. }
  52. /**
  53. * Delete the Fever key of a user.
  54. *
  55. * @param string $username
  56. * @return boolean true if the deletion succeeded, else false.
  57. */
  58. public static function deleteKey($username) {
  59. $userConfig = get_user_configuration($username);
  60. if ($userConfig === null) {
  61. return false;
  62. }
  63. $feverKey = $userConfig->feverKey;
  64. if (!ctype_xdigit($feverKey)) {
  65. return false;
  66. }
  67. $feverKeyPath = self::getKeyPath($feverKey);
  68. return @unlink($feverKeyPath);
  69. }
  70. }