update_ostatus_profiles.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @package GNUsocial
  19. * @copyright 2011 StatusNet, Inc.
  20. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  21. */
  22. define('INSTALLDIR', dirname(__DIR__, 3));
  23. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  24. $shortoptions = 'u:af';
  25. $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
  30. you have no backup.
  31. -u --uri OStatus profile URI to update
  32. -a --all update all
  33. -f --force Force update (despite identical avatar URLs etc.)
  34. UPDATE_OSTATUS_PROFILES;
  35. require_once INSTALLDIR . '/scripts/commandline.inc';
  36. /*
  37. * Hacky class to remove some checks and get public access to
  38. * protected mentods
  39. */
  40. class LooseOstatusProfile extends Ostatus_profile
  41. {
  42. /**
  43. * Look up and if necessary create an Ostatus_profile for the remote entity
  44. * with the given profile page URL. This should never return null -- you
  45. * will either get an object or an exception will be thrown.
  46. *
  47. * @param string $profile_url
  48. * @return Ostatus_profile
  49. * @throws Exception on various error conditions
  50. * @throws OStatusShadowException if this reference would obscure a local user/group
  51. */
  52. public static function updateProfileURL($profile_url, $hints=array())
  53. {
  54. $oprofile = null;
  55. $hints['profileurl'] = $profile_url;
  56. // Fetch the URL
  57. // XXX: HTTP caching
  58. $client = new HTTPClient();
  59. $client->setHeader('Accept', 'text/html,application/xhtml+xml');
  60. $response = $client->get($profile_url);
  61. if (!$response->isOk()) {
  62. // TRANS: Exception. %s is a profile URL.
  63. throw new Exception(sprintf(_('Could not reach profile page %s.'), $profile_url));
  64. }
  65. // Check if we have a non-canonical URL
  66. $finalUrl = $response->getEffectiveUrl();
  67. if ($finalUrl != $profile_url) {
  68. $hints['profileurl'] = $finalUrl;
  69. }
  70. // Try to get some hCard data
  71. $body = $response->getBody();
  72. $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
  73. if (!empty($hcardHints)) {
  74. $hints = array_merge($hints, $hcardHints);
  75. }
  76. // Check if they've got an LRDD header
  77. $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
  78. try {
  79. $xrd = new XML_XRD();
  80. $xrd->loadFile($lrdd);
  81. $xrdHints = DiscoveryHints::fromXRD($xrd);
  82. $hints = array_merge($hints, $xrdHints);
  83. } catch (Exception $e) {
  84. // No hints available from XRD
  85. }
  86. // If discovery found a feedurl (probably from LRDD), use it.
  87. if (array_key_exists('feedurl', $hints)) {
  88. return self::ensureFeedURL($hints['feedurl'], $hints);
  89. }
  90. // Get the feed URL from HTML
  91. $discover = new FeedDiscovery();
  92. $feedurl = $discover->discoverFromHTML($finalUrl, $body);
  93. if (!empty($feedurl)) {
  94. $hints['feedurl'] = $feedurl;
  95. return self::ensureFeedURL($feedurl, $hints);
  96. }
  97. // TRANS: Exception. %s is a URL.
  98. throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'), $finalUrl));
  99. }
  100. /**
  101. * Look up, and if necessary create, an Ostatus_profile for the remote
  102. * entity with the given webfinger address.
  103. * This should never return null -- you will either get an object or
  104. * an exception will be thrown.
  105. *
  106. * @param string $addr webfinger address
  107. * @return Ostatus_profile
  108. * @throws Exception on error conditions
  109. * @throws OStatusShadowException if this reference would obscure a local user/group
  110. */
  111. public static function updateWebfinger($addr)
  112. {
  113. $disco = new Discovery();
  114. try {
  115. $xrd = $disco->lookup($addr);
  116. } catch (Exception $e) {
  117. // Save negative cache entry so we don't waste time looking it up again.
  118. // @fixme distinguish temporary failures?
  119. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
  120. // TRANS: Exception.
  121. throw new Exception(_m('Not a valid webfinger address.'));
  122. }
  123. $hints = array('webfinger' => $addr);
  124. try {
  125. $dHints = DiscoveryHints::fromXRD($xrd);
  126. $hints = array_merge($hints, $xrdHints);
  127. } catch (Exception $e) {
  128. // No hints available from XRD
  129. }
  130. // If there's an Hcard, let's grab its info
  131. if (array_key_exists('hcard', $hints)) {
  132. if (!array_key_exists('profileurl', $hints) ||
  133. $hints['hcard'] != $hints['profileurl']) {
  134. $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
  135. $hints = array_merge($hcardHints, $hints);
  136. }
  137. }
  138. // If we got a feed URL, try that
  139. if (array_key_exists('feedurl', $hints)) {
  140. try {
  141. common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
  142. $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
  143. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
  144. return $oprofile;
  145. } catch (Exception $e) {
  146. common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
  147. // keep looking
  148. }
  149. }
  150. // If we got a profile page, try that!
  151. if (array_key_exists('profileurl', $hints)) {
  152. try {
  153. common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
  154. $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
  155. self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
  156. return $oprofile;
  157. } catch (OStatusShadowException $e) {
  158. // We've ended up with a remote reference to a local user or group.
  159. // @fixme ideally we should be able to say who it was so we can
  160. // go back and refer to it the regular way
  161. throw $e;
  162. } catch (Exception $e) {
  163. common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
  164. // keep looking
  165. //
  166. // @fixme this means an error discovering from profile page
  167. // may give us a corrupt entry using the webfinger URI, which
  168. // will obscure the correct page-keyed profile later on.
  169. }
  170. }
  171. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
  172. }
  173. }
  174. function pullOstatusProfile($uri)
  175. {
  176. $oprofile = null;
  177. $validate = new Validate();
  178. if ($validate->email($uri)) {
  179. $oprofile = LooseOstatusProfile::updateWebfinger($uri);
  180. } elseif ($validate->uri($uri)) {
  181. $oprofile = LooseOstatusProfile::updateProfileURL($uri);
  182. } else {
  183. print "Sorry, we could not reach the address: $uri\n";
  184. return false;
  185. }
  186. return $oprofile;
  187. }
  188. $quiet = have_option('q', 'quiet');
  189. $lop = new LooseOstatusProfile();
  190. if (have_option('u', 'uri')) {
  191. $lop->uri = get_option_value('u', 'uri');
  192. } elseif (!have_option('a', 'all')) {
  193. show_help();
  194. exit(1);
  195. }
  196. $forceUpdates = have_option('f', 'force');
  197. $cnt = $lop->find();
  198. if (!empty($cnt)) {
  199. if (!$quiet) {
  200. echo 'Found ' . $cnt . ' OStatus profiles:' . "\n";
  201. }
  202. } else {
  203. if (have_option('u', 'uri')) {
  204. if (!$quiet) {
  205. echo "Couldn't find an existing OStatus profile with that URI.\n";
  206. }
  207. } else {
  208. if (!$quiet) {
  209. echo "Couldn't find any existing OStatus profiles.\n";
  210. }
  211. }
  212. exit(0);
  213. }
  214. while ($lop->fetch()) {
  215. if (!$quiet) {
  216. echo "Updating OStatus profile '" . $lop->uri . "' ... ";
  217. }
  218. try {
  219. $oprofile = pullOstatusProfile($lop->uri);
  220. if (!empty($oprofile)) {
  221. $orig = clone($lop);
  222. $lop->avatar = $oprofile->avatar;
  223. $lop->update($orig);
  224. $lop->updateAvatar($oprofile->avatar, $forceUpdates);
  225. if (!$quiet) {
  226. echo 'Done.' . "\n";
  227. }
  228. }
  229. } catch (Exception $e) {
  230. if (!$quiet) {
  231. echo $e->getMessage() . "\n";
  232. }
  233. common_log(LOG_WARNING, $e->getMessage(), __FILE__);
  234. // continue on error
  235. }
  236. }
  237. if (!$quiet) {
  238. echo 'OK.' . "\n";
  239. }