update_activitypub_profiles.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * GNU social - a federating social network
  5. *
  6. * ActivityPubPlugin implementation for GNU Social
  7. *
  8. * LICENCE: This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @category Plugin
  22. * @package GNUsocial
  23. * @author Diogo Cordeiro <diogo@fc.up.pt>
  24. * @copyright 2018 Free Software Foundation http://fsf.org
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link https://www.gnu.org/software/social/
  27. */
  28. define('INSTALLDIR', realpath(__DIR__ . '/../../..'));
  29. $shortoptions = 'u:af';
  30. $longoptions = ['uri=', 'all', 'force'];
  31. $helptext = <<<END_OF_HELP
  32. update_activitypub_profiles.php [options]
  33. Refetch / update ActivityPub RSA keys, profile info and avatars. Useful if you
  34. do something like accidentally delete your avatars directory when
  35. you have no backup.
  36. -u --uri ActivityPub profile URI to update
  37. -a --all update all
  38. END_OF_HELP;
  39. require_once INSTALLDIR.'/scripts/commandline.inc';
  40. $quiet = have_option('q', 'quiet');
  41. if (!$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. $discovery = new Activitypub_explorer();
  48. $discovery = $discovery->lookup($uri);
  49. if (empty($discovery)) {
  50. echo "Bad URI\n";
  51. exit(1);
  52. }
  53. $user = $discovery->lookup($uri)[0];
  54. try {
  55. $res = Activitypub_explorer::get_remote_user_activity($uri);
  56. } catch (Exception $e) {
  57. echo $e->getMessage()."\n";
  58. exit(1);
  59. }
  60. if (!$quiet) {
  61. echo "Updated ".Activitypub_profile::update_profile($user, $res)->getBestName()."\n";
  62. }
  63. } else if (!have_option('a', 'all')) {
  64. show_help();
  65. exit(1);
  66. }
  67. $user = new Activitypub_profile();
  68. $cnt = $user->find();
  69. if (!empty($cnt)) {
  70. if (!$quiet) {
  71. echo "Found {$cnt} ActivityPub profiles:\n";
  72. }
  73. } else {
  74. if (have_option('u', 'uri')) {
  75. if (!$quiet) {
  76. echo "Couldn't find an existing ActivityPub profile with that URI.\n";
  77. }
  78. } else {
  79. if (!$quiet) {
  80. echo "Couldn't find any existing ActivityPub profiles.\n";
  81. }
  82. }
  83. exit(0);
  84. }
  85. while ($user->fetch()) {
  86. try {
  87. $res = Activitypub_explorer::get_remote_user_activity($user->uri);
  88. if (!$quiet) {
  89. echo "Updated ".Activitypub_profile::update_profile($user, $res)->getBestName()."\n";
  90. }
  91. } catch (Exception $e) {
  92. // let it go
  93. }
  94. }