update_activitypub_profiles.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. $quiet = have_option('q', 'quiet');
  40. if (!$quiet) {
  41. echo "ActivityPub Profiles updater will now start!\n";
  42. echo "Summoning Diogo Cordeiro, Richard Stallman and Chuck Norris to help us with this task!\n";
  43. }
  44. if (have_option('u', 'uri')) {
  45. $uri = get_option_value('u', 'uri');
  46. $discovery = new Activitypub_explorer();
  47. $discovery = $discovery->lookup($uri);
  48. if (empty($discovery)) {
  49. echo "Bad URI\n";
  50. exit(1);
  51. }
  52. $user = $discovery->lookup($uri)[0];
  53. try {
  54. $res = Activitypub_explorer::get_remote_user_activity($uri);
  55. } catch (Exception $e) {
  56. echo $e->getMessage()."\n";
  57. exit(1);
  58. }
  59. if (!$quiet) {
  60. echo "Updated ".Activitypub_profile::update_profile($user, $res)->getBestName()."\n";
  61. }
  62. } elseif (!have_option('a', 'all')) {
  63. show_help();
  64. exit(1);
  65. }
  66. $user = new Activitypub_profile();
  67. $cnt = $user->find();
  68. if (!empty($cnt)) {
  69. if (!$quiet) {
  70. echo "Found {$cnt} ActivityPub profiles:\n";
  71. }
  72. } else {
  73. if (have_option('u', 'uri')) {
  74. if (!$quiet) {
  75. echo "Couldn't find an existing ActivityPub profile with that URI.\n";
  76. }
  77. } else {
  78. if (!$quiet) {
  79. echo "Couldn't find any existing ActivityPub profiles.\n";
  80. }
  81. }
  82. exit(0);
  83. }
  84. while ($user->fetch()) {
  85. try {
  86. $res = Activitypub_explorer::get_remote_user_activity($user->uri);
  87. $updated_profile = Activitypub_profile::update_profile($user, $res);
  88. if (!$quiet) {
  89. echo "Updated ".$updated_profile->getBestName()."\n";
  90. }
  91. } catch (Exception $e) {
  92. // let it go
  93. }
  94. }