public function testXrdRfc6415A() { $filePath = __DIR__ . '/../../../'; $x = new XML_XRD(); $x->loadFile($filePath . 'xrd/rfc6415-A.xrd'); $this->assertEquals(json_decode(file_get_contents($filePath . 'jrd/rfc6415-A.jrd')), 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)); }
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();
<?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); }