apiController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This controller manage API-related features.
  5. */
  6. class FreshRSS_api_Controller extends FreshRSS_ActionController {
  7. /**
  8. * Update the user API password.
  9. * Return an error message, or `false` if no error.
  10. * @return false|string
  11. */
  12. public static function updatePassword(string $apiPasswordPlain) {
  13. $username = Minz_User::name();
  14. $userConfig = FreshRSS_Context::$user_conf;
  15. $apiPasswordHash = FreshRSS_password_Util::hash($apiPasswordPlain);
  16. $userConfig->apiPasswordHash = $apiPasswordHash;
  17. $feverKey = FreshRSS_fever_Util::updateKey($username, $apiPasswordPlain);
  18. if (!$feverKey) {
  19. return _t('feedback.api.password.failed');
  20. }
  21. $userConfig->feverKey = $feverKey;
  22. if ($userConfig->save()) {
  23. return false;
  24. } else {
  25. return _t('feedback.api.password.failed');
  26. }
  27. }
  28. /**
  29. * This action updates the user API password.
  30. *
  31. * Parameter is:
  32. * - apiPasswordPlain: the new user password
  33. */
  34. public function updatePasswordAction(): void {
  35. if (!FreshRSS_Auth::hasAccess()) {
  36. Minz_Error::error(403);
  37. }
  38. $return_url = ['c' => 'user', 'a' => 'profile'];
  39. if (!Minz_Request::isPost()) {
  40. Minz_Request::forward($return_url, true);
  41. }
  42. $apiPasswordPlain = Minz_Request::paramString('apiPasswordPlain', true);
  43. if ($apiPasswordPlain == '') {
  44. Minz_Request::forward($return_url, true);
  45. }
  46. $error = self::updatePassword($apiPasswordPlain);
  47. if ($error) {
  48. Minz_Request::bad($error, $return_url);
  49. } else {
  50. Minz_Request::good(_t('feedback.api.password.updated'), $return_url);
  51. }
  52. }
  53. }