]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/update-profile.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / OStatus / scripts / update-profile.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2010, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 $helptext = <<<END_OF_HELP
24 update-profile.php [options] http://example.com/profile/url
25
26 Rerun profile and feed info discovery for the given OStatus remote profile,
27 and reinitialize its PuSH subscription for the given feed. This may help get
28 things restarted if the hub or feed URLs have changed for the profile.
29
30
31 END_OF_HELP;
32
33 require_once INSTALLDIR.'/scripts/commandline.inc';
34
35 $validate = new Validate();
36
37 if (empty($args[0]) || !$validate->uri($args[0])) {
38     print "$helptext";
39     exit(1);
40 }
41
42 $uri = $args[0];
43
44
45 $oprofile = Ostatus_profile::getKV('uri', $uri);
46
47 if (!$oprofile) {
48     print "No OStatus remote profile known for URI $uri\n";
49     exit(1);
50 }
51
52 print "Old profile state for $oprofile->uri\n";
53 showProfile($oprofile);
54
55 print "\n";
56 print "Re-running feed discovery for profile URL $oprofile->uri\n";
57
58 $feedurl = null;
59 $salmonuri = null;
60
61 // @fixme will bork where the URI isn't the profile URL for now
62 $discover = new FeedDiscovery();
63 try {
64     $feedurl = $discover->discoverFromURL($oprofile->uri);
65     $salmonuri = $discover->getAtomLink(Salmon::REL_SALMON)
66                     ?: $discover->getAtomLink(Salmon::NS_REPLIES);  // NS_REPLIES is deprecated
67     if (empty($salmonuri) ) {
68         throw new FeedSubNoSalmonException('No salmon upstream URI was found');
69     }
70 } catch (FeedSubException $e) {
71     $acct = $oprofile->localProfile()->getAcctUri();
72     print "Could not discover feeds HTML response, trying reconstructed acct URI: {$acct}\n";
73     $disco = new Discovery();
74     $xrd = $disco->lookup($acct);
75     $hints = DiscoveryHints::fromXRD($xrd);
76
77     if (empty($feedurl) && !array_key_exists('feedurl', $hints)) {
78         throw new FeedSubNoFeedException($acct);
79     }
80     $feedurl = $feedurl ?: $hints['feedurl'];
81     $salmonuri = array_key_exists('salmon', $hints) ? $hints['salmon'] : $salmonuri;
82
83     // get the hub data too and put it in the FeedDiscovery object
84     $discover->discoverFromFeedUrl($feedurl);
85 }
86
87 $huburi = $discover->getHubLink();
88
89 print "  Feed URL: $feedurl\n";
90 print "  Hub URL: $huburi\n";
91 print "  Salmon URL: $salmonuri\n";
92
93 if ($feedurl != $oprofile->feeduri || $salmonuri != $oprofile->salmonuri) {
94     print "\n";
95     print "Updating...\n";
96     // @fixme update keys :P
97     #$orig = clone($oprofile);
98     #$oprofile->feeduri = $feedurl;
99     #$oprofile->salmonuri = $salmonuri;
100     #$ok = $oprofile->update($orig);
101     $ok = $oprofile->query('UPDATE ostatus_profile SET ' .
102         'feeduri=\'' . $oprofile->escape($feedurl) . '\',' .
103         'salmonuri=\'' . $oprofile->escape($salmonuri) . '\' ' .
104         'WHERE uri=\'' . $oprofile->escape($uri) . '\'');
105
106     if (!$ok) {
107         print "Failed to update profile record...\n";
108         exit(1);
109     }
110
111     $oprofile->decache();
112 } else {
113     print "\n";
114     print "Ok, ostatus_profile record unchanged.\n\n";
115 }
116
117 $sub = FeedSub::ensureFeed($feedurl);
118
119 if ($huburi != $sub->huburi) {
120     print "\n";
121     print "Updating hub record for feed; was $sub->huburi\n";
122     $orig = clone($sub);
123     $sub->huburi = $huburi;
124     $ok = $sub->update($orig);
125
126     if (!$ok) {
127         print "Failed to update sub record...\n";
128         exit(1);
129     }
130 } else {
131     print "\n";
132     print "Feed record ok, not changing.\n\n";
133 }
134
135 echo "\n";
136 echo "Pinging hub {$sub->huburi} with new subscription for {$sub->uri}\n";
137 try {
138     $sub->subscribe();
139     echo "ok\n";
140 } catch (Exception $e) {
141     echo 'Could not confirm. '.get_class($e).': '.$e->getMessage()."\n";
142 }
143
144 $o2 = Ostatus_profile::getKV('uri', $uri);
145
146 print "\n";
147 print "New profile state:\n";
148 showProfile($o2);
149
150 print "\n";
151 print "New feed state:\n";
152 $sub2 = FeedSub::ensureFeed($feedurl);
153 showSub($sub2);
154
155 function showProfile($oprofile)
156 {
157     print "  Feed URL: $oprofile->feeduri\n";
158     print "  Salmon URL: $oprofile->salmonuri\n";
159     print "  Avatar URL: $oprofile->avatar\n";
160     print "  Profile ID: $oprofile->profile_id\n";
161     print "  Group ID: $oprofile->group_id\n";
162     print "  Record created: $oprofile->created\n";
163     print "  Record modified: $oprofile->modified\n";
164 }
165
166 function showSub($sub)
167 {
168     print "  Subscription state: $sub->sub_state\n";
169     print "  Verify token: $sub->verify_token\n";
170     print "  Signature secret: $sub->secret\n";
171     print "  Sub start date: $sub->sub_start\n";
172     print "  Record created: $sub->created\n";
173     print "  Record modified: $sub->modified\n";
174 }