]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/scripts/update-profile-data.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / plugins / OStatus / scripts / update-profile-data.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 $longoptions = array('all', 'suspicious', 'quiet');
24
25 $helptext = <<<END_OF_HELP
26 update-profile-data.php [options] [http://example.com/profile/url]
27
28 Rerun profile discovery for the given OStatus remote profile, and save the
29 updated profile data (nickname, fullname, avatar, bio, etc).
30 Doesn't touch feed state.
31 Can be used to clean up after breakages.
32
33 Options:
34   --all        Run for all known OStatus profiles
35   --suspicious Run for OStatus profiles with all-numeric nicknames
36                (fixes 0.9.7 prerelease back-compatibility bug)
37
38 END_OF_HELP;
39
40 require_once INSTALLDIR.'/scripts/commandline.inc';
41
42 function showProfileInfo(Ostatus_profile $oprofile) {
43     if ($oprofile->isGroup()) {
44         echo "group\n";
45     } else {
46         $profile = $oprofile->localProfile();
47         try {
48             foreach (array('nickname', 'fullname', 'bio', 'homepage', 'location') as $field) {
49                 print "  $field: {$profile->$field}\n";
50             }
51         } catch (NoProfileException $e) {
52             print "local profile not found";
53         }
54     }
55     echo "\n";
56 }
57
58 function fixProfile($uri) {
59     $oprofile = Ostatus_profile::getKV('uri', $uri);
60
61     if (!$oprofile) {
62         print "No OStatus remote profile known for URI $uri\n";
63         return false;
64     }
65
66     echo "Before:\n";
67     showProfileInfo($oprofile);
68
69     $feedurl = $oprofile->feeduri;
70     $client = new HttpClient();
71     $response = $client->get($feedurl);
72     if ($response->isOk()) {
73         echo "Updating profile from feed: $feedurl\n";
74         $dom = new DOMDocument();
75         if ($dom->loadXML($response->getBody())) {
76             $feed = $dom->documentElement;
77             $entries = $dom->getElementsByTagNameNS(Activity::ATOM, 'entry');
78             if ($entries->length) {
79                 $entry = $entries->item(0);
80                 $activity = new Activity($entry, $feed);
81                 $oprofile->checkAuthorship($activity);
82                 echo "  (ok)\n";
83             } else {
84                 echo "  (no entry; skipping)\n";
85                 return false;
86             }
87         } else {
88             echo "  (bad feed; skipping)\n";
89             return false;
90         }
91     } else {
92         echo "Failed feed fetch: {$response->getStatus()} for $feedurl\n";
93         return false;
94     }
95
96     echo "After:\n";
97     showProfileInfo($oprofile);
98     return true;
99 }
100
101 $ok = true;
102 $validate = new Validate();
103 if (have_option('all')) {
104     $oprofile = new Ostatus_profile();
105     $oprofile->find();
106     echo "Found $oprofile->N profiles:\n\n";
107     while ($oprofile->fetch()) {
108         $ok = fixProfile($oprofile->uri) && $ok;
109     }
110 } else if (have_option('suspicious')) {
111     $oprofile = new Ostatus_profile();
112     $oprofile->joinAdd(array('profile_id', 'profile:id'));
113     $oprofile->whereAdd("nickname rlike '^[0-9]$'");
114     $oprofile->find();
115     echo "Found $oprofile->N matching profiles:\n\n";
116     while ($oprofile->fetch()) {
117         $ok = fixProfile($oprofile->uri) && $ok;
118     }
119 } else if (!empty($args[0]) && $validate->uri($args[0])) {
120     $uri = $args[0];
121     $ok = fixProfile($uri);
122 } else {
123     print "$helptext";
124     $ok = false;
125 }
126
127 exit($ok ? 0 : 1);