gnu-social/plugins/LRDD/extlib/XML/XRD/Loader/JSON.php
Mikael Nordfeldth a0e107f17f Implemented WebFinger and replaced our XRD with PEAR XML_XRD
New plugins:
* LRDD
    LRDD implements client-side RFC6415 and RFC7033 resource descriptor
    discovery procedures. I.e. LRDD, host-meta and WebFinger stuff.

    OStatus and OpenID now depend on the LRDD plugin (XML_XRD).

* WebFinger
    This plugin implements the server-side of RFC6415 and RFC7033. Note:
    WebFinger technically doesn't handle XRD, but we serve both that and
    JRD (JSON Resource Descriptor), depending on Accept header and one
    ugly hack to check for old StatusNet installations.

    WebFinger depends on LRDD.

We might make this even prettier by using Net_WebFinger, but it is not
currently RFC7033 compliant (no /.well-known/webfinger resource GETs).

Disabling the WebFinger plugin would effectively render your site non-
federated (which might be desired on a private site).

Disabling the LRDD plugin would make your site unable to do modern web
URI lookups (making life just a little bit harder).
2013-09-30 22:04:52 +02:00

188 lines
4.7 KiB
PHP

<?php
/**
* Part of XML_XRD
*
* PHP version 5
*
* @category XML
* @package XML_XRD
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @link http://pear.php.net/package/XML_XRD
*/
/**
* Loads XRD data from a JSON file
*
* @category XML
* @package XML_XRD
* @author Christian Weiske <cweiske@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @version Release: @package_version@
* @link http://pear.php.net/package/XML_XRD
*/
class XML_XRD_Loader_JSON
{
/**
* Data storage the XML data get loaded into
*
* @var XML_XRD
*/
protected $xrd;
/**
* Init object with xrd object
*
* @param XML_XRD $xrd Data storage the JSON data get loaded into
*/
public function __construct(XML_XRD $xrd)
{
$this->xrd = $xrd;
}
/**
* Loads the contents of the given file
*
* @param string $file Path to an JRD file
*
* @return void
*
* @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
* loaded
*/
public function loadFile($file)
{
$json = file_get_contents($file);
if ($json === false) {
throw new XML_XRD_Loader_Exception(
'Error loading JRD file: ' . $file,
XML_XRD_Loader_Exception::LOAD
);
}
return $this->loadString($json);
}
/**
* Loads the contents of the given string
*
* @param string $json JSON string
*
* @return void
*
* @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
* loaded
*/
public function loadString($json)
{
if ($json == '') {
throw new XML_XRD_Loader_Exception(
'Error loading JRD: string empty',
XML_XRD_Loader_Exception::LOAD
);
}
$obj = json_decode($json);
if ($obj !== null) {
return $this->load($obj);
}
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants['json'] as $name => $value) {
if (!strncmp($name, 'JSON_ERROR_', 11)) {
$json_errors[$value] = $name;
}
}
throw new XML_XRD_Loader_Exception(
'Error loading JRD: ' . $json_errors[json_last_error()],
XML_XRD_Loader_Exception::LOAD
);
}
/**
* Loads the JSON object into the classes' data structures
*
* @param object $j JSON object containing the whole JSON document
*
* @return void
*/
public function load(stdClass $j)
{
if (isset($j->subject)) {
$this->xrd->subject = (string)$j->subject;
}
if (isset($j->aliases)) {
foreach ($j->aliases as $jAlias) {
$this->xrd->aliases[] = (string)$jAlias;
}
}
if (isset($j->links)) {
foreach ($j->links as $jLink) {
$this->xrd->links[] = $this->loadLink($jLink);
}
}
$this->loadProperties($this->xrd, $j);
if (isset($j->expires)) {
$this->xrd->expires = strtotime($j->expires);
}
}
/**
* Loads the Property elements from XML
*
* @param object $store Data store where the properties get stored
* @param object $j JSON element with "properties" variable
*
* @return boolean True when all went well
*/
protected function loadProperties(
XML_XRD_PropertyAccess $store, stdClass $j
) {
if (!isset($j->properties)) {
return true;
}
foreach ($j->properties as $type => $jProp) {
$store->properties[] = new XML_XRD_Element_Property(
$type, (string)$jProp
);
}
return true;
}
/**
* Create a link element object from XML element
*
* @param object $j JSON link object
*
* @return XML_XRD_Element_Link Created link object
*/
protected function loadLink(stdClass $j)
{
$link = new XML_XRD_Element_Link();
foreach (array('rel', 'type', 'href', 'template') as $var) {
if (isset($j->$var)) {
$link->$var = (string)$j->$var;
}
}
if (isset($j->titles)) {
foreach ($j->titles as $lang => $jTitle) {
if (!isset($link->titles[$lang])) {
$link->titles[$lang] = (string)$jTitle;
}
}
}
$this->loadProperties($link, $j);
return $link;
}
}
?>