public function testRemoveEmptyLinksArray() { $x = new XML_XRD(); $x->subject = 'foo'; $res = new stdClass(); $res->subject = 'foo'; $this->assertEquals($res, json_decode($x->to('json'))); }
public function testArrayAccess() { $xrd = new XML_XRD(); $xrd->loadFile(__DIR__ . '/../../../xrd/properties.xrd'); $link = $xrd->get('link'); $this->assertEquals('Stevie', $link['name']); $this->assertEquals('green', $link['color']); $this->assertNull($link['empty']); $this->assertNull($link['doesnotexist']); }
/** * Look up and if necessary create an Ostatus_profile for the remote entity * with the given profile page URL. This should never return null -- you * will either get an object or an exception will be thrown. * * @param string $profile_url * @return Ostatus_profile * @throws Exception on various error conditions * @throws OStatusShadowException if this reference would obscure a local user/group */ public static function updateProfileURL($profile_url, $hints = array()) { $oprofile = null; $hints['profileurl'] = $profile_url; // Fetch the URL // XXX: HTTP caching $client = new HTTPClient(); $client->setHeader('Accept', 'text/html,application/xhtml+xml'); $response = $client->get($profile_url); if (!$response->isOk()) { // TRANS: Exception. %s is a profile URL. throw new Exception(sprintf(_('Could not reach profile page %s.'), $profile_url)); } // Check if we have a non-canonical URL $finalUrl = $response->getUrl(); if ($finalUrl != $profile_url) { $hints['profileurl'] = $finalUrl; } // Try to get some hCard data $body = $response->getBody(); $hcardHints = DiscoveryHints::hcardHints($body, $finalUrl); if (!empty($hcardHints)) { $hints = array_merge($hints, $hcardHints); } // Check if they've got an LRDD header $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) { // No hints available from XRD } // If discovery found a feedurl (probably from LRDD), use it. if (array_key_exists('feedurl', $hints)) { return self::ensureFeedURL($hints['feedurl'], $hints); } // Get the feed URL from HTML $discover = new FeedDiscovery(); $feedurl = $discover->discoverFromHTML($finalUrl, $body); if (!empty($feedurl)) { $hints['feedurl'] = $feedurl; return self::ensureFeedURL($feedurl, $hints); } // TRANS: Exception. %s is a URL. throw new Exception(sprintf(_m('Could not find a feed URL for profile page %s.'), $finalUrl)); }
public function onMagicsigPublicKeyFromXRD(XML_XRD $xrd, &$pubkey) { // See if we have a Diaspora public key in the XRD response $link = $xrd->get(self::REL_PUBLIC_KEY, 'RSA'); if (!is_null($link)) { // If we do, decode it so we have the PKCS1 format (starts with -----BEGIN PUBLIC KEY-----) $pkcs1 = base64_decode($link->href); $magicsig = new Magicsig(Magicsig::DEFAULT_SIGALG); // Diaspora uses RSA-SHA256 (we do too) try { // Try to load the public key so we can get it in the standard Magic signature format $magicsig->loadPublicKeyPKCS1($pkcs1); // We found it and will now store it in $pubkey in a proper format! // This is how it would be found in a well implemented XRD according to the standard. $pubkey = 'data:application/magic-public-key,' . $magicsig->toString(); common_debug('magic-public-key found in diaspora-public-key: ' . $pubkey); return false; } catch (ServerException $e) { common_log(LOG_WARNING, $e->getMessage()); } } return true; }
<?php /** * Generates a .well-known/host-meta file that's described * in "RFC 6415: Web Host Metadata". * * @author Christian Weiske <*****@*****.**> * @link http://tools.ietf.org/html/rfc6415 */ require_once 'XML/XRD.php'; $x = new XML_XRD(); $x->subject = 'example.org'; $x->aliases[] = 'example.com'; $x->links[] = new XML_XRD_Element_Link('lrdd', 'http://example.org/gen-lrdd.php?a={uri}', 'application/xrd+xml', true); echo $x->toXML();
/** * Given a user ID, return the first available resource descriptor * * @param string $id User ID URI * * @return XML_XRD object for the resource descriptor of the id */ public function lookup($id) { // Normalize the incoming $id to make sure we have a uri $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); // Load the LRDD XRD 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; } } // TRANS: Exception. %s is an ID. throw new Exception(sprintf(_('Unable to find services for %s.'), $id)); }
protected function assertXmlIsCorrect($file) { $xrd = new XML_XRD(); $xrd->loadFile($file); $this->assertXmlStringEqualsXmlFile($file, $xrd->to('xml'), 'Generated XML does not match the expected XML for ' . $file); }
<?php /** * Test script to load an XRD file and save it again. * The result should be equal. */ if (is_dir(__DIR__ . '/../src/')) { set_include_path(__DIR__ . '/../src/' . PATH_SEPARATOR . get_include_path()); } require_once 'XML/XRD.php'; $x = new XML_XRD(); $file = __DIR__ . '/xrd-1.0-b1.xrd'; $x->loadFile($file); echo $x->toXML();
/** * Loads the user XRD file for a given identifier * * The XRD is stored in the reaction object's $userXrd property, * any error is stored in its $error property. * * @param string $identifier E-mail address like identifier ("user@host") * @param string $host Hostname of $identifier * @param object $hostMeta host-meta XRD object * * @return Net_WebFinger_Reaction Reaction object * * @see Net_WebFinger_Reaction::$error */ protected function loadLrdd($identifier, $host, XML_XRD $hostMeta) { $link = $hostMeta->get('lrdd', 'application/xrd+xml'); if ($link === null || !$link->template) { $react = new Net_WebFinger_Reaction(); $react->error = new Net_WebFinger_Error('No lrdd link in host-meta for ' . $host, Net_WebFinger_Error::NO_LRDD_LINK); $this->mergeHostMeta($react, $hostMeta); return $react; } $account = $identifier; $userUrl = str_replace('{uri}', urlencode($account), $link->template); $react = $this->loadXrdCached($userUrl); if ($react->error && $this->isHttps($userUrl)) { //fall back to HTTP $userUrl = 'http://' . substr($userUrl, 8); $react = $this->loadXrdCached($userUrl); } if ($react->error) { $react->error = new Net_WebFinger_Error('LRDD file not found', Net_WebFinger_Error::NO_LRDD, $react->error); $this->mergeHostMeta($react, $hostMeta); return $react; } if (!$this->isHttps($userUrl)) { $react->secure = false; } $this->verifyDescribes($react, $account); $this->mergeHostMeta($react, $hostMeta); return $react; }
<?php /** * Basic WebFinger implementation to discover a user's OpenID provider * from just his email address */ if ($argc < 2) { echo "Usage: {$argv['0']} user@example.com\n"; exit(1); } $email = $argv[1]; $host = substr($email, strpos($email, '@') + 1); require_once 'XML/XRD.php'; $xrd = new XML_XRD(); try { $xrd->loadFile('https://' . $host . '/.well-known/webfinger?resource=acct:' . $email, 'json'); } catch (XML_XRD_Exception $e) { echo 'Loading JRD file failed: ' . $e->getMessage() . "\n"; exit(1); } $openIdLink = $xrd->get('http://specs.openid.net/auth/2.0/provider'); if ($openIdLink === null) { echo "No OpenID provider found for {$email}\n"; exit(2); } echo $email . '\'s OpenID provider is: ' . $openIdLink->href . "\n";
<?php /** * Convert a XRD file to JRD or vice versa. */ if ($argc < 2) { echo "Usage: {$argv['0']} path/to/file.(xrd|jrd)\n"; exit(1); } $file = $argv[1]; require_once 'XML/XRD.php'; require_once 'XML/XRD/Loader.php'; $xrd = new XML_XRD(); try { $xl = new XML_XRD_Loader($xrd); $type = $xl->detectTypeFromFile($file); $xrd->loadFile($file, $type); $targetType = $type == 'xml' ? 'json' : 'xml'; echo $xrd->to($targetType); } catch (XML_XRD_Exception $e) { echo 'Converting (X|J)RD file failed: ' . $e->getMessage() . "\n"; exit(1); }