update-profile.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. * @package GNUsocial
  19. * @copyright 2010 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. define('INSTALLDIR', dirname(__DIR__, 3));
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. $helptext = <<<END_OF_HELP
  25. update-profile.php [options] http://example.com/profile/url
  26. Rerun profile and feed info discovery for the given OStatus remote profile,
  27. and reinitialize its WebSub subscription for the given feed. This may help get
  28. things restarted if the hub or feed URLs have changed for the profile.
  29. END_OF_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. $validate = new Validate();
  32. if (empty($args[0]) || !$validate->uri($args[0])) {
  33. print "$helptext";
  34. exit(1);
  35. }
  36. $uri = $args[0];
  37. $oprofile = Ostatus_profile::getKV('uri', $uri);
  38. if (!$oprofile) {
  39. print "No OStatus remote profile known for URI $uri\n";
  40. exit(1);
  41. }
  42. print "Old profile state for $oprofile->uri\n";
  43. showProfile($oprofile);
  44. print "\n";
  45. print "Re-running feed discovery for profile URL $oprofile->uri\n";
  46. $feedurl = null;
  47. $salmonuri = null;
  48. // @fixme will bork where the URI isn't the profile URL for now
  49. $discover = new FeedDiscovery();
  50. try {
  51. $feedurl = $discover->discoverFromURL($oprofile->uri);
  52. $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
  53. ?: $discover->getAtomLink(Salmon::NS_REPLIES); // NS_REPLIES is deprecated
  54. if (empty($salmonuri)) {
  55. throw new FeedSubNoSalmonException('No salmon upstream URI was found');
  56. }
  57. } catch (FeedSubException $e) {
  58. $acct = $oprofile->localProfile()->getAcctUri();
  59. print "Could not discover feeds HTML response, trying reconstructed acct URI: {$acct}\n";
  60. $disco = new Discovery();
  61. $xrd = $disco->lookup($acct);
  62. $hints = DiscoveryHints::fromXRD($xrd);
  63. if (array_key_exists('feedurl', $hints)) {
  64. // Prefer $hints['feedurl'] to current $feedurl value
  65. $feedurl = $hints['feedurl'];
  66. } elseif (empty($feedurl)) {
  67. // if (empty($feedurl) && !array_key_exists('feedurl', $hints))
  68. throw new FeedSubNoFeedException($acct);
  69. }
  70. $salmonuri = array_key_exists('salmon', $hints) ? $hints['salmon'] : $salmonuri;
  71. // get the hub data too and put it in the FeedDiscovery object
  72. $discover->discoverFromFeedUrl($feedurl);
  73. }
  74. $huburi = $discover->getHubLink();
  75. print " Feed URL: $feedurl\n";
  76. print " Hub URL: $huburi\n";
  77. print " Salmon URL: $salmonuri\n";
  78. if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
  79. print "\n";
  80. print "Updating...\n";
  81. // @fixme update keys :P
  82. #$orig = clone($oprofile);
  83. #$oprofile->feeduri = $feedurl;
  84. #$oprofile->salmonuri = $salmonuri;
  85. #$ok = $oprofile->update($orig);
  86. $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
  87. 'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
  88. 'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
  89. 'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
  90. if (!$ok) {
  91. print "Failed to update profile record...\n";
  92. exit(1);
  93. }
  94. $oprofile->decache();
  95. } else {
  96. print "\n";
  97. print "Ok, ostatus_profile record unchanged.\n\n";
  98. }
  99. $sub = FeedSub::ensureFeed($feedurl);
  100. if ($huburi != $sub->huburi) {
  101. print "\n";
  102. print "Updating hub record for feed; was $sub->huburi\n";
  103. $orig = clone($sub);
  104. $sub->huburi = $huburi;
  105. $ok = $sub->update($orig);
  106. if (!$ok) {
  107. print "Failed to update sub record...\n";
  108. exit(1);
  109. }
  110. } else {
  111. print "\n";
  112. print "Feed record ok, not changing.\n\n";
  113. }
  114. echo "\n";
  115. echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
  116. try {
  117. $sub->subscribe();
  118. echo "ok\n";
  119. } catch (Exception $e) {
  120. echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
  121. }
  122. $o2 = Ostatus_profile::getKV('uri', $uri);
  123. print "\n";
  124. print "New profile state:\n";
  125. showProfile($o2);
  126. print "\n";
  127. print "New feed state:\n";
  128. $sub2 = FeedSub::ensureFeed($feedurl);
  129. showSub($sub2);
  130. function showProfile($oprofile)
  131. {
  132. print " Feed URL: $oprofile->feeduri\n";
  133. print " Salmon URL: $oprofile->salmonuri\n";
  134. print " Avatar URL: $oprofile->avatar\n";
  135. print " Profile ID: $oprofile->profile_id\n";
  136. print " Group ID: $oprofile->group_id\n";
  137. print " Record created: $oprofile->created\n";
  138. print " Record modified: $oprofile->modified\n";
  139. }
  140. function showSub($sub)
  141. {
  142. print " Subscription state: $sub->sub_state\n";
  143. print " Signature secret: $sub->secret\n";
  144. print " Sub start date: $sub->sub_start\n";
  145. print " Record created: $sub->created\n";
  146. print " Record modified: $sub->modified\n";
  147. }