apiController.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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::paramString('apiPasswordPlain', true);
  42. if ($apiPasswordPlain == '') {
  43. Minz_Request::forward($return_url, true);
  44. }
  45. $error = self::updatePassword($apiPasswordPlain);
  46. if ($error) {
  47. Minz_Request::bad($error, $return_url);
  48. } else {
  49. Minz_Request::good(_t('feedback.api.password.updated'), $return_url);
  50. }
  51. }
  52. }