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:a';
24 $longoptions = array('uri=', 'all');
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
36 UPDATE_OSTATUS_PROFILES;
38 require_once INSTALLDIR . '/scripts/commandline.inc';
41 * Hacky class to remove some checks and get public access to
44 class LooseOstatusProfile extends Ostatus_profile
47 * Look up and if necessary create an Ostatus_profile for the remote entity
48 * with the given profile page URL. This should never return null -- you
49 * will either get an object or an exception will be thrown.
51 * @param string $profile_url
52 * @return Ostatus_profile
53 * @throws Exception on various error conditions
54 * @throws OStatusShadowException if this reference would obscure a local user/group
56 public static function updateProfileURL($profile_url, $hints=array())
60 $hints['profileurl'] = $profile_url;
65 $client = new HTTPClient();
66 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
67 $response = $client->get($profile_url);
69 if (!$response->isOk()) {
70 // TRANS: Exception. %s is a profile URL.
71 throw new Exception(sprintf(_('Could not reach profile page %s.'),$profile_url));
74 // Check if we have a non-canonical URL
76 $finalUrl = $response->getUrl();
78 if ($finalUrl != $profile_url) {
79 $hints['profileurl'] = $finalUrl;
82 // Try to get some hCard data
84 $body = $response->getBody();
86 $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
88 if (!empty($hcardHints)) {
89 $hints = array_merge($hints, $hcardHints);
92 // Check if they've got an LRDD header
94 $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
97 $xrd->loadFile($lrdd);
98 $xrdHints = DiscoveryHints::fromXRD($xrd);
99 $hints = array_merge($hints, $xrdHints);
100 } catch (Exception $e) {
101 // No hints available from XRD
104 // If discovery found a feedurl (probably from LRDD), use it.
106 if (array_key_exists('feedurl', $hints)) {
107 return self::ensureFeedURL($hints['feedurl'], $hints);
110 // Get the feed URL from HTML
112 $discover = new FeedDiscovery();
114 $feedurl = $discover->discoverFromHTML($finalUrl, $body);
116 if (!empty($feedurl)) {
117 $hints['feedurl'] = $feedurl;
118 return self::ensureFeedURL($feedurl, $hints);
121 // TRANS: Exception. %s is a URL.
122 throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
126 * Look up, and if necessary create, an Ostatus_profile for the remote
127 * entity with the given webfinger address.
128 * This should never return null -- you will either get an object or
129 * an exception will be thrown.
131 * @param string $addr webfinger address
132 * @return Ostatus_profile
133 * @throws Exception on error conditions
134 * @throws OStatusShadowException if this reference would obscure a local user/group
136 public static function updateWebfinger($addr)
138 $disco = new Discovery();
141 $xrd = $disco->lookup($addr);
142 } catch (Exception $e) {
143 // Save negative cache entry so we don't waste time looking it up again.
144 // @fixme distinguish temporary failures?
145 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
147 throw new Exception(_m('Not a valid webfinger address.'));
150 $hints = array('webfinger' => $addr);
153 $dHints = DiscoveryHints::fromXRD($xrd);
154 $hints = array_merge($hints, $xrdHints);
155 } catch (Exception $e) {
156 // No hints available from XRD
159 // If there's an Hcard, let's grab its info
160 if (array_key_exists('hcard', $hints)) {
161 if (!array_key_exists('profileurl', $hints) ||
162 $hints['hcard'] != $hints['profileurl']) {
163 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
164 $hints = array_merge($hcardHints, $hints);
168 // If we got a feed URL, try that
169 if (array_key_exists('feedurl', $hints)) {
171 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
172 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
173 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
175 } catch (Exception $e) {
176 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
181 // If we got a profile page, try that!
182 if (array_key_exists('profileurl', $hints)) {
184 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
185 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
186 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
188 } catch (OStatusShadowException $e) {
189 // We've ended up with a remote reference to a local user or group.
190 // @fixme ideally we should be able to say who it was so we can
191 // go back and refer to it the regular way
193 } catch (Exception $e) {
194 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
197 // @fixme this means an error discovering from profile page
198 // may give us a corrupt entry using the webfinger URI, which
199 // will obscure the correct page-keyed profile later on.
202 throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
206 function pullOstatusProfile($uri) {
210 if (Validate::email($uri)) {
211 $oprofile = LooseOstatusProfile::updateWebfinger($uri);
212 } else if (Validate::uri($uri)) {
213 $oprofile = LooseOstatusProfile::updateProfileURL($uri);
215 print "Sorry, we could not reach the address: $uri\n";
222 $quiet = have_option('q', 'quiet');
224 $lop = new LooseOstatusProfile();
226 if (have_option('u', 'uri')) {
227 $lop->uri = get_option_value('u', 'uri');
228 } else if (!have_option('a', 'all')) {
236 if (!$quiet) { echo "Found {$cnt} OStatus profiles:\n"; }
238 if (have_option('u', 'uri')) {
239 if (!$quiet) { echo "Couldn't find an existing OStatus profile with that URI.\n"; }
241 if (!$quiet) { echo "Couldn't find any existing OStatus profiles.\n"; }
246 while($lop->fetch()) {
247 if (!$quiet) { echo "Updating OStatus profile '{$lop->uri}' ... "; }
249 $oprofile = pullOstatusProfile($lop->uri);
251 if (!empty($oprofile)) {
253 $lop->avatar = $oprofile->avatar;
255 $lop->updateAvatar($oprofile->avatar);
256 if (!$quiet) { print "Done.\n"; }
258 } catch (Exception $e) {
259 if (!$quiet) { print $e->getMessage() . "\n"; }
260 common_log(LOG_WARNING, $e->getMessage(), __FILE__);
265 if (!$quiet) { echo "OK.\n"; }