update_activitypub_profiles.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * ActivityPub implementation for GNU social
  19. *
  20. * @package GNUsocial
  21. * @author Diogo Cordeiro <diogo@fc.up.pt>
  22. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. * @link http://www.gnu.org/software/social/
  25. */
  26. define('INSTALLDIR', dirname(__DIR__, 3));
  27. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  28. $shortoptions = 'u:af';
  29. $longoptions = ['uri=', 'all', 'force'];
  30. $helptext = <<<END_OF_HELP
  31. update_activitypub_profiles.php [options]
  32. Refetch / update ActivityPub RSA keys, profile info and avatars. Useful if you
  33. do something like accidentally delete your avatars directory when
  34. you have no backup.
  35. -u --uri ActivityPub profile URI to update
  36. -a --all update all
  37. END_OF_HELP;
  38. require_once INSTALLDIR.'/scripts/commandline.inc';
  39. if (!have_option('q', 'quiet')) {
  40. echo "ActivityPub Profiles updater will now start!\n";
  41. echo "Summoning Diogo Cordeiro, Richard Stallman and Chuck Norris to help us with this task!\n";
  42. }
  43. if (have_option('u', 'uri')) {
  44. $uri = get_option_value('u', 'uri');
  45. $user = Activitypub_profile::from_profile(Activitypub_explorer::get_profile_from_url($uri));
  46. try {
  47. $res = Activitypub_explorer::get_remote_user_activity($uri);
  48. } catch (Exception $e) {
  49. echo $e->getMessage()."\n";
  50. exit(1);
  51. }
  52. printfnq('Updated '.Activitypub_profile::update_profile($user, $res)->getBestName()."\n");
  53. exit(0);
  54. } elseif (!have_option('a', 'all')) {
  55. show_help();
  56. exit(1);
  57. }
  58. $user = new Activitypub_profile();
  59. $cnt = $user->find();
  60. if (!empty($cnt)) {
  61. printfnq("Found {$cnt} ActivityPub profiles:\n");
  62. } else {
  63. if (have_option('u', 'uri')) {
  64. printfnq("Couldn't find an existing ActivityPub profile with that URI.\n");
  65. } else {
  66. printfnq("Couldn't find any existing ActivityPub profiles.\n");
  67. }
  68. exit(0);
  69. }
  70. while ($user->fetch()) {
  71. try {
  72. $res = Activitypub_explorer::get_remote_user_activity($user->uri);
  73. $updated_profile = Activitypub_profile::update_profile($user, $res);
  74. printfnq('Updated '.$updated_profile->getBestName()."\n");
  75. } catch (NoProfileException $e) {
  76. printfnq('Deleted '.$user->uri."\n");
  77. } catch (Exception $e) {
  78. // let it go
  79. }
  80. }
  81. $user->free();
  82. unset($user);