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.php';
41 * Hacky class to remove some checks and get public access to
44 class LooseOstatusProfile extends Ostatus_profile
47 * Download and update given avatar image
50 * @throws Exception in various failure cases
52 public function updateAvatar($url)
54 if (!common_valid_http_url($url)) {
55 // TRANS: Server exception. %s is a URL.
56 throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
59 if ($this->isGroup()) {
60 $self = $this->localGroup();
62 $self = $this->localProfile();
65 throw new ServerException(sprintf(
66 // TRANS: Server exception. %s is a URI.
67 _m('Tried to update avatar for unsaved remote profile %s.'),
71 // @fixme this should be better encapsulated
72 // ripped from oauthstore.php (for old OMB client)
73 $temp_filename = tempnam(common_get_temp_dir(), 'listener_avatar');
75 if (!copy($url, $temp_filename)) {
76 // TRANS: Server exception. %s is a URL.
77 throw new ServerException(sprintf(_m('Unable to fetch avatar from %s to %s.'), $url, $temp_filename));
80 if ($this->isGroup()) {
81 $id = $this->group_id;
83 $id = $this->profile_id;
85 // @fixme should we be using different ids?
86 $imagefile = new ImageFile($id, $temp_filename);
87 $filename = Avatar::filename($id,
88 image_type_to_extension($imagefile->type),
91 rename($temp_filename, Avatar::path($filename));
92 } catch (Exception $e) {
93 unlink($temp_filename);
96 // @fixme hardcoded chmod is lame, but seems to be necessary to
97 // keep from accidentally saving images from command-line (queues)
98 // that can't be read from web server, which causes hard-to-notice
101 // http://status.net/open-source/issues/2663
102 chmod(Avatar::path($filename), 0644);
104 $self->setOriginal($filename);
106 $orig = clone($this);
107 $this->avatar = $url;
108 $this->update($orig);
112 * Look up and if necessary create an Ostatus_profile for the remote entity
113 * with the given profile page URL. This should never return null -- you
114 * will either get an object or an exception will be thrown.
116 * @param string $profile_url
117 * @return Ostatus_profile
118 * @throws Exception on various error conditions
119 * @throws OStatusShadowException if this reference would obscure a local user/group
121 public static function updateProfileURL($profile_url, array $hints=array())
125 $hints['profileurl'] = $profile_url;
130 $client = new HTTPClient();
131 $client->setHeader('Accept', 'text/html,application/xhtml+xml');
132 $response = $client->get($profile_url);
134 if (!$response->isOk()) {
135 // TRANS: Exception. %s is a profile URL.
136 throw new Exception(sprintf(_('Could not reach profile page %s.'),$profile_url));
139 // Check if we have a non-canonical URL
141 $finalUrl = $response->getUrl();
143 if ($finalUrl != $profile_url) {
144 $hints['profileurl'] = $finalUrl;
147 // Try to get some hCard data
149 $body = $response->getBody();
151 $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
153 if (!empty($hcardHints)) {
154 $hints = array_merge($hints, $hcardHints);
157 // Check if they've got an LRDD header
159 $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
161 $xrd = new XML_XRD();
162 $xrd->loadFile($lrdd);
163 $xrdHints = DiscoveryHints::fromXRD($xrd);
164 $hints = array_merge($hints, $xrdHints);
165 } catch (Exception $e) {
166 // No hints available from XRD
169 // If discovery found a feedurl (probably from LRDD), use it.
171 if (array_key_exists('feedurl', $hints)) {
172 return self::ensureFeedURL($hints['feedurl'], $hints);
175 // Get the feed URL from HTML
177 $discover = new FeedDiscovery();
179 $feedurl = $discover->discoverFromHTML($finalUrl, $body);
181 if (!empty($feedurl)) {
182 $hints['feedurl'] = $feedurl;
183 return self::ensureFeedURL($feedurl, $hints);
186 // TRANS: Exception. %s is a URL.
187 throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
191 * Look up, and if necessary create, an Ostatus_profile for the remote
192 * entity with the given webfinger address.
193 * This should never return null -- you will either get an object or
194 * an exception will be thrown.
196 * @param string $addr webfinger address
197 * @return Ostatus_profile
198 * @throws Exception on error conditions
199 * @throws OStatusShadowException if this reference would obscure a local user/group
201 public static function updateWebfinger($addr)
203 $disco = new Discovery();
206 $xrd = $disco->lookup($addr);
207 } catch (Exception $e) {
208 // Save negative cache entry so we don't waste time looking it up again.
209 // @fixme distinguish temporary failures?
210 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
212 throw new Exception(_m('Not a valid webfinger address.'));
215 $hints = array('webfinger' => $addr);
218 $dHints = DiscoveryHints::fromXRD($xrd);
219 $hints = array_merge($hints, $xrdHints);
220 } catch (Exception $e) {
221 // No hints available from XRD
224 // If there's an Hcard, let's grab its info
225 if (array_key_exists('hcard', $hints)) {
226 if (!array_key_exists('profileurl', $hints) ||
227 $hints['hcard'] != $hints['profileurl']) {
228 $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
229 $hints = array_merge($hcardHints, $hints);
233 // If we got a feed URL, try that
234 if (array_key_exists('feedurl', $hints)) {
236 common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
237 $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
238 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
240 } catch (Exception $e) {
241 common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
246 // If we got a profile page, try that!
247 if (array_key_exists('profileurl', $hints)) {
249 common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
250 $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
251 self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
253 } catch (OStatusShadowException $e) {
254 // We've ended up with a remote reference to a local user or group.
255 // @fixme ideally we should be able to say who it was so we can
256 // go back and refer to it the regular way
258 } catch (Exception $e) {
259 common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
262 // @fixme this means an error discovering from profile page
263 // may give us a corrupt entry using the webfinger URI, which
264 // will obscure the correct page-keyed profile later on.
267 throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
271 function pullOstatusProfile($uri) {
275 if (Validate::email($uri)) {
276 $oprofile = LooseOstatusProfile::updateWebfinger($uri);
277 } else if (Validate::uri($uri)) {
278 $oprofile = LooseOstatusProfile::updateProfileURL($uri);
280 print "Sorry, we could not reach the address: $uri\n";
287 $quiet = have_option('q', 'quiet');
289 $lop = new LooseOstatusProfile();
291 if (have_option('u', 'uri')) {
292 $lop->uri = get_option_value('u', 'uri');
293 } else if (!have_option('a', 'all')) {
301 if (!$quiet) { echo "Found {$cnt} OStatus profiles:\n"; }
303 if (have_option('u', 'uri')) {
304 if (!$quiet) { echo "Couldn't find an existing OStatus profile with that URI.\n"; }
306 if (!$quiet) { echo "Couldn't find any existing OStatus profiles.\n"; }
311 while($lop->fetch()) {
312 if (!$quiet) { echo "Updating OStatus profile '{$lop->uri}' ... "; }
314 $oprofile = pullOstatusProfile($lop->uri);
316 if (!empty($oprofile)) {
318 $lop->avatar = $oprofile->avatar;
320 $lop->updateAvatar($oprofile->avatar);
321 if (!$quiet) { print "Done.\n"; }
323 } catch (Exception $e) {
324 if (!$quiet) { print $e->getMessage() . "\n"; }
325 common_log(LOG_WARNING, $e->getMessage(), __FILE__);
330 if (!$quiet) { echo "OK.\n"; }