apiController.php 1.6 KB

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