update-user.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env php
  2. <?php
  3. declare(strict_types=1);
  4. require(__DIR__ . '/_cli.php');
  5. $cliOptions = new class extends CliOptionsParser {
  6. public string $user;
  7. public string $password;
  8. public string $apiPassword;
  9. public string $language;
  10. public string $email;
  11. public string $token;
  12. public int $purgeAfterMonths;
  13. public int $feedMinArticles;
  14. public int $feedTtl;
  15. public int $sinceHoursPostsPerRss;
  16. public int $maxPostsPerRss;
  17. public function __construct() {
  18. $this->addRequiredOption('user', (new CliOption('user')));
  19. $this->addOption('password', (new CliOption('password')));
  20. $this->addOption('apiPassword', (new CliOption('api-password'))->deprecatedAs('api_password'));
  21. $this->addOption('language', (new CliOption('language')));
  22. $this->addOption('email', (new CliOption('email')));
  23. $this->addOption('token', (new CliOption('token')));
  24. $this->addOption(
  25. 'purgeAfterMonths',
  26. (new CliOption('purge-after-months'))->typeOfInt()->deprecatedAs('purge_after_months')
  27. );
  28. $this->addOption(
  29. 'feedMinArticles',
  30. (new CliOption('feed-min-articles-default'))->typeOfInt()->deprecatedAs('feed_min_articles_default')
  31. );
  32. $this->addOption(
  33. 'feedTtl',
  34. (new CliOption('feed-ttl-default'))->typeOfInt()->deprecatedAs('feed_ttl_default')
  35. );
  36. $this->addOption(
  37. 'sinceHoursPostsPerRss',
  38. (new CliOption('since-hours-posts-per-rss'))->typeOfInt()->deprecatedAs('since_hours_posts_per_rss')
  39. );
  40. $this->addOption(
  41. 'maxPostsPerRss',
  42. (new CliOption('max-posts-per-rss'))->typeOfInt()->deprecatedAs('max_posts_per_rss')
  43. );
  44. parent::__construct();
  45. }
  46. };
  47. if (!empty($cliOptions->errors)) {
  48. fail('FreshRSS error: ' . array_shift($cliOptions->errors) . "\n" . $cliOptions->usage);
  49. }
  50. $username = cliInitUser($cliOptions->user);
  51. echo 'FreshRSS updating user “', $username, "”…\n";
  52. $values = [
  53. 'language' => $cliOptions->language ?? null,
  54. 'mail_login' => $cliOptions->email ?? null,
  55. 'token' => $cliOptions->token ?? null,
  56. 'old_entries' => $cliOptions->purgeAfterMonths ?? null,
  57. 'keep_history_default' => $cliOptions->feedMinArticles ?? null,
  58. 'ttl_default' => $cliOptions->feedTtl ?? null,
  59. 'since_hours_posts_per_rss' => $cliOptions->sinceHoursPostsPerRss ?? null,
  60. 'max_posts_per_rss' => $cliOptions->maxPostsPerRss ?? null,
  61. ];
  62. $values = array_filter($values, fn($value): bool => $value !== null && $value !== '');
  63. $ok = FreshRSS_user_Controller::updateUser(
  64. $username,
  65. $cliOptions->email ?? null,
  66. $cliOptions->password ?? '',
  67. $values);
  68. if (!$ok) {
  69. fail('FreshRSS could not update user!');
  70. }
  71. if (isset($cliOptions->apiPassword)) {
  72. $error = FreshRSS_api_Controller::updatePassword($cliOptions->apiPassword);
  73. if ($error !== false) {
  74. fail($error);
  75. }
  76. }
  77. invalidateHttpCache($username);
  78. accessRights();
  79. done($ok);