apiController.php 1.6 KB

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