123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class Discovery
- {
- const LRDD_REL = 'lrdd';
- const UPDATESFROM = 'http://schemas.google.com/g/2010#updates-from';
- const HCARD = 'http://microformats.org/profile/hcard';
- const MF2_HCARD = 'http://microformats.org/profile/h-card';
- const JRD_MIMETYPE_OLD = 'application/json';
- const JRD_MIMETYPE = 'application/jrd+json';
- const XRD_MIMETYPE = 'application/xrd+xml';
- public $methods = array();
-
- public function __construct()
- {
- if (Event::handle('StartDiscoveryMethodRegistration', array($this))) {
- Event::handle('EndDiscoveryMethodRegistration', array($this));
- }
- }
- public static function supportedMimeTypes()
- {
- return array('json'=>self::JRD_MIMETYPE,
- 'jsonold'=>self::JRD_MIMETYPE_OLD,
- 'xml'=>self::XRD_MIMETYPE);
- }
-
- public function registerMethod($class)
- {
- $this->methods[] = $class;
- }
-
- public function lookup($id)
- {
-
- $uri = self::normalize($id);
- foreach ($this->methods as $class) {
- try {
- $xrd = new XML_XRD();
- common_debug("LRDD discovery method for '$uri': {$class}");
- $lrdd = new $class;
- $links = call_user_func(array($lrdd, 'discover'), $uri);
- $link = Discovery::getService($links, Discovery::LRDD_REL);
-
- if (!empty($link->template)) {
- $xrd_uri = Discovery::applyTemplate($link->template, $uri);
- } elseif (!empty($link->href)) {
- $xrd_uri = $link->href;
- } else {
- throw new Exception('No resource descriptor URI in link.');
- }
- $client = new HTTPClient();
- $headers = array();
- if (!is_null($link->type)) {
- $headers[] = "Accept: {$link->type}";
- }
- $response = $client->get($xrd_uri, $headers);
- if ($response->getStatus() != 200) {
- throw new Exception('Unexpected HTTP status code.');
- }
- $xrd->loadString($response->getBody());
- return $xrd;
- } catch (Exception $e) {
- continue;
- }
- }
-
- throw new Exception(sprintf(_('Unable to find services for %s.'), $id));
- }
-
- public static function getService(array $links, $service)
- {
- foreach ($links as $link) {
- if ($link->rel === $service) {
- return $link;
- }
- common_debug('LINK: rel '.$link->rel.' !== '.$service);
- }
- throw new Exception('No service link found');
- }
-
- public static function normalize($uri)
- {
- if (is_null($uri) || $uri==='') {
- throw new Exception(_('No resource given.'));
- }
- $parts = parse_url($uri);
-
-
- if (!isset($parts['scheme']) && isset($parts['path'])
- && preg_match('/[\w@\w]/u', $parts['path'])) {
- return 'acct:' . $uri;
- }
- return $uri;
- }
- public static function isAcct($uri)
- {
- return (mb_strtolower(mb_substr($uri, 0, 5)) == 'acct:');
- }
-
- public static function applyTemplate($template, $uri)
- {
- $template = str_replace('{uri}', urlencode($uri), $template);
- return $template;
- }
- }
|