update_activitypub_profiles.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. *
  22. * @author Diogo Cordeiro <diogo@fc.up.pt>
  23. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. *
  26. * @see http://www.gnu.org/software/social/
  27. */
  28. define('INSTALLDIR', dirname(__DIR__, 3));
  29. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  30. $shortoptions = 'u:af';
  31. $longoptions = ['uri=', 'all', 'force'];
  32. $helptext = <<<END_OF_HELP
  33. update_activitypub_profiles.php [options]
  34. Refetch / update ActivityPub RSA keys, profile info and avatars. Useful if you
  35. do something like accidentally delete your avatars directory when
  36. you have no backup.
  37. -u --uri ActivityPub profile URI to update
  38. -a --all update all
  39. END_OF_HELP;
  40. require_once INSTALLDIR . '/scripts/commandline.inc';
  41. if (!have_option('q', 'quiet')) {
  42. echo "ActivityPub Profiles updater will now start!\n";
  43. echo "Summoning Diogo Cordeiro, Richard Stallman and Chuck Norris to help us with this task!\n";
  44. }
  45. if (have_option('u', 'uri')) {
  46. $uri = get_option_value('u', 'uri');
  47. $user = Activitypub_profile::from_profile(Activitypub_explorer::get_profile_from_url($uri));
  48. try {
  49. $res = Activitypub_explorer::get_remote_user_activity($uri);
  50. } catch (Exception $e) {
  51. echo $e->getMessage() . "\n";
  52. exit(1);
  53. }
  54. printfnq('Updated ' . Activitypub_profile::update_profile($user, $res)->getBestName() . "\n");
  55. exit(0);
  56. } elseif (!have_option('a', 'all')) {
  57. show_help();
  58. exit(1);
  59. }
  60. $user = new Activitypub_profile();
  61. $cnt = $user->find();
  62. if (!empty($cnt)) {
  63. printfnq("Found {$cnt} ActivityPub profiles:\n");
  64. } else {
  65. if (have_option('u', 'uri')) {
  66. printfnq("Couldn't find an existing ActivityPub profile with that URI.\n");
  67. } else {
  68. printfnq("Couldn't find any existing ActivityPub profiles.\n");
  69. }
  70. exit(0);
  71. }
  72. while ($user->fetch()) {
  73. try {
  74. $res = Activitypub_explorer::get_remote_user_activity($user->uri);
  75. $updated_profile = Activitypub_profile::update_profile($user, $res);
  76. printfnq('Updated ' . $updated_profile->getBestName() . "\n");
  77. } catch (NoProfileException $e) {
  78. printfnq('Deleted ' . $user->uri . "\n");
  79. } catch (Exception $e) {
  80. // let it go
  81. }
  82. }
  83. $user->free();
  84. unset($user);