4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
23 $shortoptions = 'u:af';
24 $longoptions = array('uri=', 'all', 'force');
26 $helptext = <<<UPDATE_OSTATUS_PROFILES
27 update_ostatus_profiles.php [options]
28 Refetch / update OStatus profile info and avatars. Useful if you
29 do something like accidentally delete your avatars directory when
32 -u --uri OStatus profile URI to update
35 -f --force Force update (despite identical avatar URLs etc.)
37 UPDATE_OSTATUS_PROFILES;
39 require_once INSTALLDIR . '/scripts/commandline.inc.php';
42 * Hacky class to remove some checks and get public access to
45 class LooseOstatusProfile extends Ostatus_profile
48 * Look up and if necessary create an Ostatus_profile for the remote entity
49 * with the given profile page URL. This should never return null -- you
50 * will either get an object or an exception will be thrown.
52 * @param string $profile_url
53 * @return Ostatus_profile
54 * @throws Exception on various error conditions
55 * @throws OStatusShadowException if this reference would obscure a local user/group
57 public static function updateProfileURL($profile_url, array $hints=array())
61 $hints['profileurl'] = $profile_url;
66 $client = new HTTPClient();
67 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
68 $response = $client->get($profile_url);
70 if (!$response->isOk()) {
71 // TRANS: Exception. %s is a profile URL.
72 throw new Exception(sprintf(_('Could not reach profile page %s.'),$profile_url));
75 // Check if we have a non-canonical URL
77 $finalUrl = $response->getUrl();
79 if ($finalUrl != $profile_url) {
80 $hints['profileurl'] = $finalUrl;
83 // Try to get some hCard data
85 $body = $response->getBody();
87 $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
89 if (!empty($hcardHints)) {
90 $hints = array_merge($hints, $hcardHints);
93 // Check if they've got an LRDD header
95 $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
98 $xrd->loadFile($lrdd);
99 $xrdHints = DiscoveryHints::fromXRD($xrd);
100 $hints = array_merge($hints, $xrdHints);
101 } catch (Exception $e) {
102 // No hints available from XRD
105 // If discovery found a feedurl (probably from LRDD), use it.
107 if (array_key_exists('feedurl', $hints)) {
108 return self::ensureFeedURL($hints['feedurl'], $hints);
111 // Get the feed URL from HTML
113 $discover = new FeedDiscovery();
115 $feedurl = $discover->discoverFromHTML($finalUrl, $body);
117 if (!empty($feedurl)) {
118 $hints['feedurl'] = $feedurl;
119 return self::ensureFeedURL($feedurl, $hints);
122 // TRANS: Exception. %s is a URL.
123 throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
127 * Look up, and if necessary create, an Ostatus_profile for the remote
128 * entity with the given webfinger address.
129 * This should never return null -- you will either get an object or
130 * an exception will be thrown.
132 * @param string $addr webfinger address
133 * @return Ostatus_profile
134 * @throws Exception on error conditions
135 * @throws OStatusShadowException if this reference would obscure a local user/group
137 public static function updateWebfinger($addr)
139 $disco = new Discovery();
142 $xrd = $disco->lookup($addr);
143 } catch (Exception $e) {
144 // Save negative cache entry so we don't waste time looking it up again.
145 // @fixme distinguish temporary failures?
146 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
148 throw new Exception(_m('Not a valid webfinger address.'));
151 $hints = array('webfinger' => $addr);
154 $dHints = DiscoveryHints::fromXRD($xrd);
155 $hints = array_merge($hints, $xrdHints);
156 } catch (Exception $e) {
157 // No hints available from XRD
160 // If there's an Hcard, let's grab its info
161 if (array_key_exists('hcard', $hints)) {
162 if (!array_key_exists('profileurl', $hints) ||
163 $hints['hcard'] != $hints['profileurl']) {
164 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
165 $hints = array_merge($hcardHints, $hints);
169 // If we got a feed URL, try that
170 if (array_key_exists('feedurl', $hints)) {
172 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
173 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
174 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
176 } catch (Exception $e) {
177 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
182 // If we got a profile page, try that!
183 if (array_key_exists('profileurl', $hints)) {
185 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
186 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
187 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
189 } catch (OStatusShadowException $e) {
190 // We've ended up with a remote reference to a local user or group.
191 // @fixme ideally we should be able to say who it was so we can
192 // go back and refer to it the regular way
194 } catch (Exception $e) {
195 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
198 // @fixme this means an error discovering from profile page
199 // may give us a corrupt entry using the webfinger URI, which
200 // will obscure the correct page-keyed profile later on.
203 throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
207 function pullOstatusProfile($uri) {
211 if (Validate::email($uri)) {
212 $oprofile = LooseOstatusProfile::updateWebfinger($uri);
213 } else if (Validate::uri($uri)) {
214 $oprofile = LooseOstatusProfile::updateProfileURL($uri);
216 print "Sorry, we could not reach the address: $uri\n";
223 $quiet = have_option('q', 'quiet');
225 $lop = new LooseOstatusProfile();
227 if (have_option('u', 'uri')) {
228 $lop->uri = get_option_value('u', 'uri');
229 } else if (!have_option('a', 'all')) {
234 $forceUpdates = have_option('f', 'force');
239 if (!$quiet) { echo "Found {$cnt} OStatus profiles:\n"; }
241 if (have_option('u', 'uri')) {
242 if (!$quiet) { echo "Couldn't find an existing OStatus profile with that URI.\n"; }
244 if (!$quiet) { echo "Couldn't find any existing OStatus profiles.\n"; }
249 while($lop->fetch()) {
250 if (!$quiet) { echo "Updating OStatus profile '{$lop->uri}' ... "; }
252 $oprofile = pullOstatusProfile($lop->uri);
254 if (!empty($oprofile)) {
256 $lop->avatar = $oprofile->avatar;
258 $lop->updateAvatar($oprofile->avatar, $forceUpdates);
259 if (!$quiet) { print "Done.\n"; }
261 } catch (Exception $e) {
262 if (!$quiet) { print $e->getMessage() . "\n"; }
263 common_log(LOG_WARNING, $e->getMessage(), __FILE__);
268 if (!$quiet) { echo "OK.\n"; }