123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class FetchRemotePlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
- static function fetchNoticeFromUrl($url)
- {
- if (!common_valid_http_url($url)) {
- throw new InvalidUrlException($url);
- }
- $host = parse_url($url, PHP_URL_HOST);
-
-
-
- if (!$stored instanceof Notice) {
- common_log(LOG_INFO, 'Could not fetch remote notice from URL: '._ve($url));
- throw new ServerException('Could not fetch remote notice.');
- }
- return $stored;
- }
- public function onFetchRemoteNoticeWithSource($uri, Profile $source, &$stored)
- {
- if (common_valid_http_url($uri) && !Event::handle('FetchRemoteNoticeFromUrl', array($url, &$stored))) {
-
- return false;
- }
-
-
- try {
- $source_url = parse_url($source->getUrl());
- } catch (InvalidUrlException $e) {
- return true;
- }
- if ($source_url['scheme'] !== 'https') {
- common_debug('Will not try to fetch remote notice from non-HTTPS capable profile source');
- return true;
- }
- try {
- $port = isset($source_url['port']) ? ":{$source_url['port']}" : '';
- $rfc7033 = "https://{$source_url['host']}{$port}/.well-known/webfinger";
- $params = ['resource' => $uri];
- common_debug(__METHOD__ . ": getting json data about notice from: {$rfc7033}?resource=$uri");
- $json = HTTPClient::quickGetJson($rfc7033, $params);
- } catch (Exception $e) {
-
-
-
- return true;
- }
- if (!isset($json->aliases)) {
-
- return true;
- }
- common_debug(__METHOD__ . ": Found these aliases: "._ve($json->aliases));
- foreach ($json->aliases as $alias) {
- try {
- $stored = self::fetchNoticeFromUrl($url);
- if ($stored instanceof Notice) {
-
- return false;
- }
- } catch (InvalidUrlException $e) {
-
- } catch (Exception $e) {
-
- common_debug(__METHOD__ . ": {$e->getMessage()}");
- }
- }
-
- return true;
- }
- public function onPluginVersion(array &$versions): bool
- {
- $versions[] = array('name' => 'FetchRemote',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Mikael Nordfeldth',
- 'homepage' => GNUSOCIAL_ENGINE_URL,
-
- 'rawdescription' => _m('Retrieves remote notices (and serves local) via WebFinger'));
- return true;
- }
- }
|