apiController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. */
  11. public static function updatePassword(string $apiPasswordPlain): string|false {
  12. $username = Minz_User::name();
  13. if ($username == null) {
  14. return _t('feedback.api.password.failed');
  15. }
  16. $apiPasswordHash = FreshRSS_password_Util::hash($apiPasswordPlain);
  17. FreshRSS_Context::userConf()->apiPasswordHash = $apiPasswordHash;
  18. $feverKey = FreshRSS_fever_Util::updateKey($username, $apiPasswordPlain);
  19. if ($feverKey == false) {
  20. return _t('feedback.api.password.failed');
  21. }
  22. FreshRSS_Context::userConf()->feverKey = $feverKey;
  23. if (FreshRSS_Context::userConf()->save()) {
  24. return false;
  25. } else {
  26. return _t('feedback.api.password.failed');
  27. }
  28. }
  29. /**
  30. * This action updates the user API password.
  31. *
  32. * Parameter is:
  33. * - apiPasswordPlain: the new user password
  34. */
  35. public function updatePasswordAction(): void {
  36. if (!FreshRSS_Auth::hasAccess()) {
  37. Minz_Error::error(403);
  38. }
  39. $return_url = ['c' => 'user', 'a' => 'profile'];
  40. if (!Minz_Request::isPost()) {
  41. Minz_Request::forward($return_url, true);
  42. }
  43. $apiPasswordPlain = Minz_Request::paramString('apiPasswordPlain', true);
  44. if ($apiPasswordPlain == '') {
  45. Minz_Request::forward($return_url, true);
  46. }
  47. $error = self::updatePassword($apiPasswordPlain);
  48. if (is_string($error)) {
  49. Minz_Request::bad($error, $return_url);
  50. } else {
  51. Minz_Request::good(
  52. _t('feedback.api.password.updated'),
  53. $return_url,
  54. showNotification: FreshRSS_Context::userConf()->good_notification_timeout > 0
  55. );
  56. }
  57. }
  58. }