apiController.php 1.5 KB

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