| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | #!/usr/bin/env php
 | 
					
						
							|  |  |  | <?php | 
					
						
							| 
									
										
										
										
											2019-09-18 17:15:00 +03:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @copyright 2010 StatusNet, Inc. | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-18 17:15:00 +03:00
										 |  |  | define('INSTALLDIR', dirname(__DIR__, 3)); | 
					
						
							|  |  |  | define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | $helptext = <<<END_OF_HELP | 
					
						
							|  |  |  | update-profile.php [options] http://example.com/profile/url | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rerun profile and feed info discovery for the given OStatus remote profile, | 
					
						
							| 
									
										
										
										
											2017-05-01 11:04:27 +02:00
										 |  |  | and reinitialize its WebSub subscription for the given feed. This may help get | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | things restarted if the hub or feed URLs have changed for the profile. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | END_OF_HELP; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require_once INSTALLDIR.'/scripts/commandline.inc'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-08 10:33:41 +01:00
										 |  |  | $validate = new Validate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if (empty($args[0]) || !$validate->uri($args[0])) { | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  |     print "$helptext"; | 
					
						
							|  |  |  |     exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $uri = $args[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  | $oprofile = Ostatus_profile::getKV('uri', $uri); | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | if (!$oprofile) { | 
					
						
							|  |  |  |     print "No OStatus remote profile known for URI $uri\n"; | 
					
						
							|  |  |  |     exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print "Old profile state for $oprofile->uri\n"; | 
					
						
							|  |  |  | showProfile($oprofile); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print "\n"; | 
					
						
							|  |  |  | print "Re-running feed discovery for profile URL $oprofile->uri\n"; | 
					
						
							| 
									
										
										
										
											2015-10-04 16:40:37 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | $feedurl = null; | 
					
						
							|  |  |  | $salmonuri = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | // @fixme will bork where the URI isn't the profile URL for now
 | 
					
						
							|  |  |  | $discover = new FeedDiscovery(); | 
					
						
							| 
									
										
										
										
											2015-06-06 16:18:22 +02:00
										 |  |  | try { | 
					
						
							|  |  |  |     $feedurl = $discover->discoverFromURL($oprofile->uri); | 
					
						
							|  |  |  |     $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON) | 
					
						
							|  |  |  |                     ?: $discover->getAtomLink(Salmon::NS_REPLIES);  // NS_REPLIES is deprecated
 | 
					
						
							| 
									
										
										
										
											2019-09-18 17:15:00 +03:00
										 |  |  |     if (empty($salmonuri)) { | 
					
						
							| 
									
										
										
										
											2015-10-04 16:40:37 +02:00
										 |  |  |         throw new FeedSubNoSalmonException('No salmon upstream URI was found'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-06-06 16:18:22 +02:00
										 |  |  | } catch (FeedSubException $e) { | 
					
						
							|  |  |  |     $acct = $oprofile->localProfile()->getAcctUri(); | 
					
						
							| 
									
										
										
										
											2015-10-04 16:40:37 +02:00
										 |  |  |     print "Could not discover feeds HTML response, trying reconstructed acct URI: {$acct}\n"; | 
					
						
							| 
									
										
										
										
											2015-06-06 16:18:22 +02:00
										 |  |  |     $disco = new Discovery(); | 
					
						
							|  |  |  |     $xrd = $disco->lookup($acct); | 
					
						
							|  |  |  |     $hints = DiscoveryHints::fromXRD($xrd); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 23:27:46 +01:00
										 |  |  |     if (array_key_exists('feedurl', $hints)) { | 
					
						
							|  |  |  |         // Prefer $hints['feedurl'] to current $feedurl value
 | 
					
						
							|  |  |  |         $feedurl = $hints['feedurl']; | 
					
						
							|  |  |  |     } elseif (empty($feedurl)) { | 
					
						
							|  |  |  |         // if (empty($feedurl) && !array_key_exists('feedurl', $hints))
 | 
					
						
							| 
									
										
										
										
											2015-06-06 16:18:22 +02:00
										 |  |  |         throw new FeedSubNoFeedException($acct); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-20 23:27:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-04 16:40:37 +02:00
										 |  |  |     $salmonuri = array_key_exists('salmon', $hints) ? $hints['salmon'] : $salmonuri; | 
					
						
							| 
									
										
										
										
											2015-06-06 16:18:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // get the hub data too and put it in the FeedDiscovery object
 | 
					
						
							|  |  |  |     $discover->discoverFromFeedUrl($feedurl); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-02 16:08:54 -07:00
										 |  |  | $huburi = $discover->getHubLink(); | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | print "  Feed URL: $feedurl\n"; | 
					
						
							|  |  |  | print "  Hub URL: $huburi\n"; | 
					
						
							|  |  |  | print "  Salmon URL: $salmonuri\n"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) { | 
					
						
							|  |  |  |     print "\n"; | 
					
						
							|  |  |  |     print "Updating...\n"; | 
					
						
							|  |  |  |     // @fixme update keys :P
 | 
					
						
							| 
									
										
										
										
											2020-06-10 16:52:00 +03:00
										 |  |  |     //$orig = clone($oprofile);
 | 
					
						
							|  |  |  |     //$oprofile->feeduri = $feedurl;
 | 
					
						
							|  |  |  |     //$oprofile->salmonuri = $salmonuri;
 | 
					
						
							|  |  |  |     //$ok = $oprofile->update($orig);
 | 
					
						
							|  |  |  |     $ok = $oprofile->query( | 
					
						
							|  |  |  |         <<<END | 
					
						
							|  |  |  |         UPDATE ostatus_profile | 
					
						
							|  |  |  |         SET feeduri   = '{$oprofile->escape($feedurl)}', | 
					
						
							|  |  |  |             salmonuri = '{$oprofile->escape($salmonuri)}' | 
					
						
							|  |  |  |         WHERE uri = '{$oprofile->escape($uri)}' | 
					
						
							|  |  |  |         END | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!$ok) { | 
					
						
							|  |  |  |         print "Failed to update profile record...\n"; | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $oprofile->decache(); | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     print "\n"; | 
					
						
							|  |  |  |     print "Ok, ostatus_profile record unchanged.\n\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $sub = FeedSub::ensureFeed($feedurl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ($huburi != $sub->huburi) { | 
					
						
							|  |  |  |     print "\n"; | 
					
						
							|  |  |  |     print "Updating hub record for feed; was $sub->huburi\n"; | 
					
						
							|  |  |  |     $orig = clone($sub); | 
					
						
							|  |  |  |     $sub->huburi = $huburi; | 
					
						
							|  |  |  |     $ok = $sub->update($orig); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!$ok) { | 
					
						
							|  |  |  |         print "Failed to update sub record...\n"; | 
					
						
							|  |  |  |         exit(1); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } else { | 
					
						
							|  |  |  |     print "\n"; | 
					
						
							|  |  |  |     print "Feed record ok, not changing.\n\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-06 15:40:57 +02:00
										 |  |  | echo "\n"; | 
					
						
							|  |  |  | echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n"; | 
					
						
							|  |  |  | try { | 
					
						
							|  |  |  |     $sub->subscribe(); | 
					
						
							|  |  |  |     echo "ok\n"; | 
					
						
							|  |  |  | } catch (Exception $e) { | 
					
						
							|  |  |  |     echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n"; | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  | $o2 = Ostatus_profile::getKV('uri', $uri); | 
					
						
							| 
									
										
										
										
											2010-04-23 12:54:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | print "\n"; | 
					
						
							|  |  |  | print "New profile state:\n"; | 
					
						
							|  |  |  | showProfile($o2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print "\n"; | 
					
						
							|  |  |  | print "New feed state:\n"; | 
					
						
							|  |  |  | $sub2 = FeedSub::ensureFeed($feedurl); | 
					
						
							|  |  |  | showSub($sub2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function showProfile($oprofile) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     print "  Feed URL: $oprofile->feeduri\n"; | 
					
						
							|  |  |  |     print "  Salmon URL: $oprofile->salmonuri\n"; | 
					
						
							|  |  |  |     print "  Avatar URL: $oprofile->avatar\n"; | 
					
						
							|  |  |  |     print "  Profile ID: $oprofile->profile_id\n"; | 
					
						
							|  |  |  |     print "  Group ID: $oprofile->group_id\n"; | 
					
						
							|  |  |  |     print "  Record created: $oprofile->created\n"; | 
					
						
							|  |  |  |     print "  Record modified: $oprofile->modified\n"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function showSub($sub) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     print "  Subscription state: $sub->sub_state\n"; | 
					
						
							|  |  |  |     print "  Signature secret: $sub->secret\n"; | 
					
						
							|  |  |  |     print "  Sub start date: $sub->sub_start\n"; | 
					
						
							|  |  |  |     print "  Record created: $sub->created\n"; | 
					
						
							|  |  |  |     print "  Record modified: $sub->modified\n"; | 
					
						
							|  |  |  | } |