update_ostatus_profiles.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2011, 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. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
  21. $shortoptions = 'u:af';
  22. $longoptions = array('uri=', 'all', 'force');
  23. $helptext = <<<UPDATE_OSTATUS_PROFILES
  24. update_ostatus_profiles.php [options]
  25. Refetch / update OStatus profile info and avatars. Useful if you
  26. do something like accidentally delete your avatars directory when
  27. you have no backup.
  28. -u --uri OStatus profile URI to update
  29. -a --all update all
  30. -f --force Force update (despite identical avatar URLs etc.)
  31. UPDATE_OSTATUS_PROFILES;
  32. require_once INSTALLDIR . '/scripts/commandline.inc';
  33. /*
  34. * Hacky class to remove some checks and get public access to
  35. * protected mentods
  36. */
  37. class LooseOstatusProfile extends Ostatus_profile
  38. {
  39. /**
  40. * Look up and if necessary create an Ostatus_profile for the remote entity
  41. * with the given profile page URL. This should never return null -- you
  42. * will either get an object or an exception will be thrown.
  43. *
  44. * @param string $profile_url
  45. * @return Ostatus_profile
  46. * @throws Exception on various error conditions
  47. * @throws OStatusShadowException if this reference would obscure a local user/group
  48. */
  49. public static function updateProfileURL($profile_url, $hints=array())
  50. {
  51. $oprofile = null;
  52. $hints['profileurl'] = $profile_url;
  53. // Fetch the URL
  54. // XXX: HTTP caching
  55. $client = new HTTPClient();
  56. $client->setHeader('Accept', 'text/html,application/xhtml+xml');
  57. $response = $client->get($profile_url);
  58. if (!$response->isOk()) {
  59. // TRANS: Exception. %s is a profile URL.
  60. throw new Exception(sprintf(_('Could not reach profile page %s.'),$profile_url));
  61. }
  62. // Check if we have a non-canonical URL
  63. $finalUrl = $response->getUrl();
  64. if ($finalUrl != $profile_url) {
  65. $hints['profileurl'] = $finalUrl;
  66. }
  67. // Try to get some hCard data
  68. $body = $response->getBody();
  69. $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
  70. if (!empty($hcardHints)) {
  71. $hints = array_merge($hints, $hcardHints);
  72. }
  73. // Check if they've got an LRDD header
  74. $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
  75. try {
  76. $xrd = new XML_XRD();
  77. $xrd->loadFile($lrdd);
  78. $xrdHints = DiscoveryHints::fromXRD($xrd);
  79. $hints = array_merge($hints, $xrdHints);
  80. } catch (Exception $e) {
  81. // No hints available from XRD
  82. }
  83. // If discovery found a feedurl (probably from LRDD), use it.
  84. if (array_key_exists('feedurl', $hints)) {
  85. return self::ensureFeedURL($hints['feedurl'], $hints);
  86. }
  87. // Get the feed URL from HTML
  88. $discover = new FeedDiscovery();
  89. $feedurl = $discover->discoverFromHTML($finalUrl, $body);
  90. if (!empty($feedurl)) {
  91. $hints['feedurl'] = $feedurl;
  92. return self::ensureFeedURL($feedurl, $hints);
  93. }
  94. // TRANS: Exception. %s is a URL.
  95. throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
  96. }
  97. /**
  98. * Look up, and if necessary create, an Ostatus_profile for the remote
  99. * entity with the given webfinger address.
  100. * This should never return null -- you will either get an object or
  101. * an exception will be thrown.
  102. *
  103. * @param string $addr webfinger address
  104. * @return Ostatus_profile
  105. * @throws Exception on error conditions
  106. * @throws OStatusShadowException if this reference would obscure a local user/group
  107. */
  108. public static function updateWebfinger($addr)
  109. {
  110. $disco = new Discovery();
  111. try {
  112. $xrd = $disco->lookup($addr);
  113. } catch (Exception $e) {
  114. // Save negative cache entry so we don't waste time looking it up again.
  115. // @fixme distinguish temporary failures?
  116. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
  117. // TRANS: Exception.
  118. throw new Exception(_m('Not a valid webfinger address.'));
  119. }
  120. $hints = array('webfinger' => $addr);
  121. try {
  122. $dHints = DiscoveryHints::fromXRD($xrd);
  123. $hints = array_merge($hints, $xrdHints);
  124. } catch (Exception $e) {
  125. // No hints available from XRD
  126. }
  127. // If there's an Hcard, let's grab its info
  128. if (array_key_exists('hcard', $hints)) {
  129. if (!array_key_exists('profileurl', $hints) ||
  130. $hints['hcard'] != $hints['profileurl']) {
  131. $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
  132. $hints = array_merge($hcardHints, $hints);
  133. }
  134. }
  135. // If we got a feed URL, try that
  136. if (array_key_exists('feedurl', $hints)) {
  137. try {
  138. common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
  139. $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
  140. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
  141. return $oprofile;
  142. } catch (Exception $e) {
  143. common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
  144. // keep looking
  145. }
  146. }
  147. // If we got a profile page, try that!
  148. if (array_key_exists('profileurl', $hints)) {
  149. try {
  150. common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
  151. $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
  152. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
  153. return $oprofile;
  154. } catch (OStatusShadowException $e) {
  155. // We've ended up with a remote reference to a local user or group.
  156. // @fixme ideally we should be able to say who it was so we can
  157. // go back and refer to it the regular way
  158. throw $e;
  159. } catch (Exception $e) {
  160. common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
  161. // keep looking
  162. //
  163. // @fixme this means an error discovering from profile page
  164. // may give us a corrupt entry using the webfinger URI, which
  165. // will obscure the correct page-keyed profile later on.
  166. }
  167. }
  168. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
  169. }
  170. }
  171. function pullOstatusProfile($uri) {
  172. $oprofile = null;
  173. if (Validate::email($uri)) {
  174. $oprofile = LooseOstatusProfile::updateWebfinger($uri);
  175. } else if (Validate::uri($uri)) {
  176. $oprofile = LooseOstatusProfile::updateProfileURL($uri);
  177. } else {
  178. print "Sorry, we could not reach the address: $uri\n";
  179. return false;
  180. }
  181. return $oprofile;
  182. }
  183. $quiet = have_option('q', 'quiet');
  184. $lop = new LooseOstatusProfile();
  185. if (have_option('u', 'uri')) {
  186. $lop->uri = get_option_value('u', 'uri');
  187. } else if (!have_option('a', 'all')) {
  188. show_help();
  189. exit(1);
  190. }
  191. $forceUpdates = have_option('f', 'force');
  192. $cnt = $lop->find();
  193. if (!empty($cnt)) {
  194. if (!$quiet) { echo "Found {$cnt} OStatus profiles:\n"; }
  195. } else {
  196. if (have_option('u', 'uri')) {
  197. if (!$quiet) { echo "Couldn't find an existing OStatus profile with that URI.\n"; }
  198. } else {
  199. if (!$quiet) { echo "Couldn't find any existing OStatus profiles.\n"; }
  200. }
  201. exit(0);
  202. }
  203. while($lop->fetch()) {
  204. if (!$quiet) { echo "Updating OStatus profile '{$lop->uri}' ... "; }
  205. try {
  206. $oprofile = pullOstatusProfile($lop->uri);
  207. if (!empty($oprofile)) {
  208. $orig = clone($lop);
  209. $lop->avatar = $oprofile->avatar;
  210. $lop->update($orig);
  211. $lop->updateAvatar($oprofile->avatar, $forceUpdates);
  212. if (!$quiet) { print "Done.\n"; }
  213. }
  214. } catch (Exception $e) {
  215. if (!$quiet) { print $e->getMessage() . "\n"; }
  216. common_log(LOG_WARNING, $e->getMessage(), __FILE__);
  217. // continue on error
  218. }
  219. }
  220. if (!$quiet) { echo "OK.\n"; }