123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?php
- define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
- $shortoptions = 'u:af';
- $longoptions = array('uri=', 'all', 'force');
- $helptext = <<<UPDATE_OSTATUS_PROFILES
- update_ostatus_profiles.php [options]
- Refetch / update OStatus profile info and avatars. Useful if you
- do something like accidentally delete your avatars directory when
- you have no backup.
- -u --uri OStatus profile URI to update
- -a --all update all
- -f --force Force update (despite identical avatar URLs etc.)
- UPDATE_OSTATUS_PROFILES;
- require_once INSTALLDIR . '/scripts/commandline.inc';
- class LooseOstatusProfile extends Ostatus_profile
- {
-
- public static function updateProfileURL($profile_url, $hints=array())
- {
- $oprofile = null;
- $hints['profileurl'] = $profile_url;
-
-
- $client = new HTTPClient();
- $client->setHeader('Accept', 'text/html,application/xhtml+xml');
- $response = $client->get($profile_url);
- if (!$response->isOk()) {
-
- throw new Exception(sprintf(_('Could not reach profile page %s.'),$profile_url));
- }
-
- $finalUrl = $response->getEffectiveUrl();
- if ($finalUrl != $profile_url) {
- $hints['profileurl'] = $finalUrl;
- }
-
- $body = $response->getBody();
- $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl);
- if (!empty($hcardHints)) {
- $hints = array_merge($hints, $hcardHints);
- }
-
- $lrdd = LinkHeader::getLink($response, 'lrdd', 'application/xrd+xml');
- try {
- $xrd = new XML_XRD();
- $xrd->loadFile($lrdd);
- $xrdHints = DiscoveryHints::fromXRD($xrd);
- $hints = array_merge($hints, $xrdHints);
- } catch (Exception $e) {
-
- }
-
- if (array_key_exists('feedurl', $hints)) {
- return self::ensureFeedURL($hints['feedurl'], $hints);
- }
-
- $discover = new FeedDiscovery();
- $feedurl = $discover->discoverFromHTML($finalUrl, $body);
- if (!empty($feedurl)) {
- $hints['feedurl'] = $feedurl;
- return self::ensureFeedURL($feedurl, $hints);
- }
-
- throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'),$finalUrl));
- }
-
- public static function updateWebfinger($addr)
- {
- $disco = new Discovery();
- try {
- $xrd = $disco->lookup($addr);
- } catch (Exception $e) {
-
-
- self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), null);
-
- throw new Exception(_m('Not a valid webfinger address.'));
- }
- $hints = array('webfinger' => $addr);
- try {
- $dHints = DiscoveryHints::fromXRD($xrd);
- $hints = array_merge($hints, $xrdHints);
- } catch (Exception $e) {
-
- }
-
- if (array_key_exists('hcard', $hints)) {
- if (!array_key_exists('profileurl', $hints) ||
- $hints['hcard'] != $hints['profileurl']) {
- $hcardHints = DiscoveryHints::fromHcardUrl($hints['hcard']);
- $hints = array_merge($hcardHints, $hints);
- }
- }
-
- if (array_key_exists('feedurl', $hints)) {
- try {
- common_log(LOG_INFO, "Discovery on acct:$addr with feed URL " . $hints['feedurl']);
- $oprofile = self::ensureFeedURL($hints['feedurl'], $hints);
- self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
- return $oprofile;
- } catch (Exception $e) {
- common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
-
- }
- }
-
- if (array_key_exists('profileurl', $hints)) {
- try {
- common_log(LOG_INFO, "Discovery on acct:$addr with profile URL $profileUrl");
- $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
- self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
- return $oprofile;
- } catch (OStatusShadowException $e) {
-
-
-
- throw $e;
- } catch (Exception $e) {
- common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
-
-
-
-
-
- }
- }
- throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'),$addr));
- }
- }
- function pullOstatusProfile($uri) {
- $oprofile = null;
- $validate = new Validate();
- if ($validate->email($uri)) {
- $oprofile = LooseOstatusProfile::updateWebfinger($uri);
- } else if ($validate->uri($uri)) {
- $oprofile = LooseOstatusProfile::updateProfileURL($uri);
- } else {
- print "Sorry, we could not reach the address: $uri\n";
- return false;
- }
- return $oprofile;
- }
- $quiet = have_option('q', 'quiet');
- $lop = new LooseOstatusProfile();
- if (have_option('u', 'uri')) {
- $lop->uri = get_option_value('u', 'uri');
- } else if (!have_option('a', 'all')) {
- show_help();
- exit(1);
- }
- $forceUpdates = have_option('f', 'force');
- $cnt = $lop->find();
- if (!empty($cnt)) {
- if (!$quiet) { echo "Found {$cnt} OStatus profiles:\n"; }
- } else {
- if (have_option('u', 'uri')) {
- if (!$quiet) { echo "Couldn't find an existing OStatus profile with that URI.\n"; }
- } else {
- if (!$quiet) { echo "Couldn't find any existing OStatus profiles.\n"; }
- }
- exit(0);
- }
- while($lop->fetch()) {
- if (!$quiet) { echo "Updating OStatus profile '{$lop->uri}' ... "; }
- try {
- $oprofile = pullOstatusProfile($lop->uri);
- if (!empty($oprofile)) {
- $orig = clone($lop);
- $lop->avatar = $oprofile->avatar;
- $lop->update($orig);
- $lop->updateAvatar($oprofile->avatar, $forceUpdates);
- if (!$quiet) { print "Done.\n"; }
- }
- } catch (Exception $e) {
- if (!$quiet) { print $e->getMessage() . "\n"; }
- common_log(LOG_WARNING, $e->getMessage(), __FILE__);
-
- }
- }
- if (!$quiet) { echo "OK.\n"; }
|