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).
This commit is contained in:
258
plugins/LRDD/extlib/XML/XRD.php
Normal file
258
plugins/LRDD/extlib/XML/XRD.php
Normal file
@@ -0,0 +1,258 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/PropertyAccess.php';
|
||||
require_once 'XML/XRD/Element/Link.php';
|
||||
require_once 'XML/XRD/Loader.php';
|
||||
require_once 'XML/XRD/Serializer.php';
|
||||
|
||||
/**
|
||||
* Main class used to load XRD documents from string or file.
|
||||
*
|
||||
* After loading the file, access to links is possible with get() and getAll(),
|
||||
* as well as foreach-iterating over the XML_XRD object.
|
||||
*
|
||||
* Property access is possible with getProperties() and array access (foreach)
|
||||
* on the XML_XRD object.
|
||||
*
|
||||
* Verification that the subject/aliases match the requested URL can be done with
|
||||
* describes().
|
||||
*
|
||||
* @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 extends XML_XRD_PropertyAccess implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* XRD file/string loading dispatcher
|
||||
*
|
||||
* @var XML_XRD_Loader
|
||||
*/
|
||||
public $loader;
|
||||
|
||||
/**
|
||||
* XRD serializing dispatcher
|
||||
*
|
||||
* @var XML_XRD_Serializer
|
||||
*/
|
||||
public $serializer;
|
||||
|
||||
/**
|
||||
* XRD subject
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $subject;
|
||||
|
||||
/**
|
||||
* Array of subject alias strings
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $aliases = array();
|
||||
|
||||
/**
|
||||
* Array of link objects
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $links = array();
|
||||
|
||||
/**
|
||||
* Unix timestamp when the document expires.
|
||||
* NULL when no expiry date set.
|
||||
*
|
||||
* @var integer|null
|
||||
*/
|
||||
public $expires;
|
||||
|
||||
/**
|
||||
* xml:id of the XRD document
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $id;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Loads the contents of the given file.
|
||||
*
|
||||
* Note: Only use file type auto-detection for local files.
|
||||
* Do not use it on remote files as the file gets requested several times.
|
||||
*
|
||||
* @param string $file Path to an XRD file
|
||||
* @param string $type File type: xml or json, NULL for auto-detection
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
|
||||
* loaded
|
||||
*/
|
||||
public function loadFile($file, $type = null)
|
||||
{
|
||||
if (!isset($this->loader)) {
|
||||
$this->loader = new XML_XRD_Loader($this);
|
||||
}
|
||||
return $this->loader->loadFile($file, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the contents of the given string
|
||||
*
|
||||
* @param string $str XRD string
|
||||
* @param string $type File type: xml or json, NULL for auto-detection
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
|
||||
* loaded
|
||||
*/
|
||||
public function loadString($str, $type = null)
|
||||
{
|
||||
if (!isset($this->loader)) {
|
||||
$this->loader = new XML_XRD_Loader($this);
|
||||
}
|
||||
return $this->loader->loadString($str, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the XRD document describes the given URI.
|
||||
*
|
||||
* This should always be used to make sure the XRD file
|
||||
* is the correct one for e.g. the given host, and not a copycat.
|
||||
*
|
||||
* Checks against the subject and aliases
|
||||
*
|
||||
* @param string $uri An URI that the document is expected to describe
|
||||
*
|
||||
* @return boolean True or false
|
||||
*/
|
||||
public function describes($uri)
|
||||
{
|
||||
if ($this->subject == $uri) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->aliases as $alias) {
|
||||
if ($alias == $uri) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the link with highest priority for the given relation and type.
|
||||
*
|
||||
* @param string $rel Relation name
|
||||
* @param string $type MIME Type
|
||||
* @param boolean $typeFallback When true and no link with the given type
|
||||
* could be found, the best link without a
|
||||
* type will be returned
|
||||
*
|
||||
* @return XML_XRD_Element_Link Link object or NULL if none found
|
||||
*/
|
||||
public function get($rel, $type = null, $typeFallback = true)
|
||||
{
|
||||
$links = $this->getAll($rel, $type, $typeFallback);
|
||||
if (count($links) == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $links[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all links with the given relation and type, highest priority first.
|
||||
*
|
||||
* @param string $rel Relation name
|
||||
* @param string $type MIME Type
|
||||
* @param boolean $typeFallback When true and no link with the given type
|
||||
* could be found, the best link without a
|
||||
* type will be returned
|
||||
*
|
||||
* @return array Array of XML_XRD_Element_Link objects
|
||||
*/
|
||||
public function getAll($rel, $type = null, $typeFallback = true)
|
||||
{
|
||||
$links = array();
|
||||
$exactType = false;
|
||||
foreach ($this->links as $link) {
|
||||
if ($link->rel == $rel
|
||||
&& ($type === null || $link->type == $type
|
||||
|| $typeFallback && $link->type === null)
|
||||
) {
|
||||
$links[] = $link;
|
||||
$exactType |= $typeFallback && $type !== null
|
||||
&& $link->type == $type;
|
||||
}
|
||||
}
|
||||
if ($exactType) {
|
||||
//remove all links without type
|
||||
$exactlinks = array();
|
||||
foreach ($links as $link) {
|
||||
if ($link->type !== null) {
|
||||
$exactlinks[] = $link;
|
||||
}
|
||||
}
|
||||
$links = $exactlinks;
|
||||
}
|
||||
return $links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the iterator object to loop over the links
|
||||
*
|
||||
* Part of the IteratorAggregate interface
|
||||
*
|
||||
* @return Traversable Iterator for the links
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new ArrayIterator($this->links);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this XRD object to XML or JSON.
|
||||
*
|
||||
* @param string $type Serialization type: xml or json
|
||||
*
|
||||
* @return string Generated content
|
||||
*/
|
||||
public function to($type)
|
||||
{
|
||||
if (!isset($this->serializer)) {
|
||||
$this->serializer = new XML_XRD_Serializer($this);
|
||||
}
|
||||
return $this->serializer->to($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts this XRD object to XML.
|
||||
*
|
||||
* @return string Generated XML
|
||||
*
|
||||
* @deprecated use to('xml')
|
||||
*/
|
||||
public function toXML()
|
||||
{
|
||||
return $this->to('xml');
|
||||
}
|
||||
}
|
||||
?>
|
120
plugins/LRDD/extlib/XML/XRD/Element/Link.php
Normal file
120
plugins/LRDD/extlib/XML/XRD/Element/Link.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/PropertyAccess.php';
|
||||
|
||||
/**
|
||||
* Link element in a XRD file. Attribute access via object properties.
|
||||
*
|
||||
* Retrieving the title of a link is possible with the getTitle() convenience
|
||||
* method.
|
||||
*
|
||||
* @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_Element_Link extends XML_XRD_PropertyAccess
|
||||
{
|
||||
/**
|
||||
* Link relation
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $rel;
|
||||
|
||||
/**
|
||||
* Link type (MIME type)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* Link URL
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $href;
|
||||
|
||||
/**
|
||||
* Link URL template
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $template;
|
||||
|
||||
/**
|
||||
* Array of key-value pairs: Key is the language, value the title
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $titles = array();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Create a new instance and load data from the XML element
|
||||
*
|
||||
* @param string $relOrXml string with the relation name/URL
|
||||
* @param string $href HREF value
|
||||
* @param string $type Type value
|
||||
* @param boolean $isTemplate When set to true, the $href is
|
||||
* used as template
|
||||
*/
|
||||
public function __construct(
|
||||
$rel = null, $href = null, $type = null, $isTemplate = false
|
||||
) {
|
||||
$this->rel = $rel;
|
||||
if ($isTemplate) {
|
||||
$this->template = $href;
|
||||
} else {
|
||||
$this->href = $href;
|
||||
}
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the title of the link in the given language.
|
||||
* If the language is not available, the first title without the language
|
||||
* is returned. If no such one exists, the first title is returned.
|
||||
*
|
||||
* @param string $lang 2-letter language name
|
||||
*
|
||||
* @return string|null Link title
|
||||
*/
|
||||
public function getTitle($lang = null)
|
||||
{
|
||||
if (count($this->titles) == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($lang == null) {
|
||||
return reset($this->titles);
|
||||
}
|
||||
|
||||
if (isset($this->titles[$lang])) {
|
||||
return $this->titles[$lang];
|
||||
}
|
||||
if (isset($this->titles[''])) {
|
||||
return $this->titles[''];
|
||||
}
|
||||
|
||||
//return first
|
||||
return reset($this->titles);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
55
plugins/LRDD/extlib/XML/XRD/Element/Property.php
Normal file
55
plugins/LRDD/extlib/XML/XRD/Element/Property.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Property element in a XRD document.
|
||||
*
|
||||
* The <XRD> root element as well as <Link> tags may have <Property> children.
|
||||
*
|
||||
* @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_Element_Property
|
||||
{
|
||||
/**
|
||||
* Value of the property.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
public $value;
|
||||
|
||||
/**
|
||||
* Type of the propery.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
*
|
||||
* @param string $type String representing the property type
|
||||
* @param string $value Value of the property, may be NULL
|
||||
*/
|
||||
public function __construct($type = null, $value = null)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
30
plugins/LRDD/extlib/XML/XRD/Exception.php
Normal file
30
plugins/LRDD/extlib/XML/XRD/Exception.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base exception interface for all XML_XRD related exceptions.
|
||||
* With that interface, it is possible to catch all XML_XRD exceptions
|
||||
* with a single catch statement.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
interface XML_XRD_Exception
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
156
plugins/LRDD/extlib/XML/XRD/Loader.php
Normal file
156
plugins/LRDD/extlib/XML/XRD/Loader.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/Loader/Exception.php';
|
||||
|
||||
/**
|
||||
* File/string loading dispatcher.
|
||||
* Loads the correct loader for the type of XRD file (XML or JSON).
|
||||
* Also provides type auto-detection.
|
||||
*
|
||||
* @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
|
||||
{
|
||||
public function __construct(XML_XRD $xrd)
|
||||
{
|
||||
$this->xrd = $xrd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the contents of the given file.
|
||||
*
|
||||
* Note: Only use file type auto-detection for local files.
|
||||
* Do not use it on remote files as the file gets requested several times.
|
||||
*
|
||||
* @param string $file Path to an XRD file
|
||||
* @param string $type File type: xml or json, NULL for auto-detection
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
|
||||
* loaded
|
||||
*/
|
||||
public function loadFile($file, $type = null)
|
||||
{
|
||||
if ($type === null) {
|
||||
$type = $this->detectTypeFromFile($file);
|
||||
}
|
||||
$loader = $this->getLoader($type);
|
||||
$loader->loadFile($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the contents of the given string
|
||||
*
|
||||
* @param string $str XRD string
|
||||
* @param string $type File type: xml or json, NULL for auto-detection
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
|
||||
* loaded
|
||||
*/
|
||||
public function loadString($str, $type = null)
|
||||
{
|
||||
if ($type === null) {
|
||||
$type = $this->detectTypeFromString($str);
|
||||
}
|
||||
$loader = $this->getLoader($type);
|
||||
$loader->loadString($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a XRD loader object for the given type
|
||||
*
|
||||
* @param string $type File type: xml or json
|
||||
*
|
||||
* @return XML_XRD_Loader
|
||||
*/
|
||||
protected function getLoader($type)
|
||||
{
|
||||
$class = 'XML_XRD_Loader_' . strtoupper($type);
|
||||
$file = str_replace('_', '/', $class) . '.php';
|
||||
include_once $file;
|
||||
if (class_exists($class)) {
|
||||
return new $class($this->xrd);
|
||||
}
|
||||
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'No loader for XRD type "' . $type . '"',
|
||||
XML_XRD_Loader_Exception::NO_LOADER
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to detect the file type (xml or json) from the file content
|
||||
*
|
||||
* @param string $file File name to check
|
||||
*
|
||||
* @return string File type ('xml' or 'json')
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When opening the file fails.
|
||||
*/
|
||||
public function detectTypeFromFile($file)
|
||||
{
|
||||
if (!file_exists($file)) {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Error loading XRD file: File does not exist',
|
||||
XML_XRD_Loader_Exception::OPEN_FILE
|
||||
);
|
||||
}
|
||||
$handle = fopen($file, 'r');
|
||||
if (!$handle) {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Cannot open file to determine type',
|
||||
XML_XRD_Loader_Exception::OPEN_FILE
|
||||
);
|
||||
}
|
||||
|
||||
$str = (string)fgets($handle, 10);
|
||||
fclose($handle);
|
||||
return $this->detectTypeFromString($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to detect the file type from the content of the file
|
||||
*
|
||||
* @param string $str Content of XRD file
|
||||
*
|
||||
* @return string File type ('xml' or 'json')
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the type cannot be detected
|
||||
*/
|
||||
public function detectTypeFromString($str)
|
||||
{
|
||||
if (substr($str, 0, 1) == '{') {
|
||||
return 'json';
|
||||
} else if (substr($str, 0, 5) == '<?xml') {
|
||||
return 'xml';
|
||||
}
|
||||
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Detecting file type failed',
|
||||
XML_XRD_Loader_Exception::DETECT_TYPE
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
59
plugins/LRDD/extlib/XML/XRD/Loader/Exception.php
Normal file
59
plugins/LRDD/extlib/XML/XRD/Loader/Exception.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/Exception.php';
|
||||
|
||||
/**
|
||||
* XML_XRD exception that's thrown when loading the XRD fails.
|
||||
*
|
||||
* @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_Exception extends Exception implements XML_XRD_Exception
|
||||
{
|
||||
/**
|
||||
* The document namespace is not the XRD 1.0 namespace
|
||||
*/
|
||||
const DOC_NS = 10;
|
||||
|
||||
/**
|
||||
* The document root element is not XRD
|
||||
*/
|
||||
const DOC_ROOT = 11;
|
||||
|
||||
/**
|
||||
* Error loading the XML|JSON file|string
|
||||
*/
|
||||
const LOAD = 12;
|
||||
|
||||
/**
|
||||
* Unsupported XRD file/string type (no loader)
|
||||
*/
|
||||
const NO_LOADER = 13;
|
||||
|
||||
/**
|
||||
* Error opening file
|
||||
*/
|
||||
const OPEN_FILE = 14;
|
||||
|
||||
/**
|
||||
* Detecting the file type failed
|
||||
*/
|
||||
const DETECT_TYPE = 20;
|
||||
}
|
||||
|
||||
?>
|
187
plugins/LRDD/extlib/XML/XRD/Loader/JSON.php
Normal file
187
plugins/LRDD/extlib/XML/XRD/Loader/JSON.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
?>
|
218
plugins/LRDD/extlib/XML/XRD/Loader/XML.php
Normal file
218
plugins/LRDD/extlib/XML/XRD/Loader/XML.php
Normal file
@@ -0,0 +1,218 @@
|
||||
<?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 an XML 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_XML
|
||||
{
|
||||
/**
|
||||
* Data storage the XML data get loaded into
|
||||
*
|
||||
* @var XML_XRD
|
||||
*/
|
||||
protected $xrd;
|
||||
|
||||
/**
|
||||
* XRD 1.0 namespace
|
||||
*/
|
||||
const NS_XRD = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Init object with xrd object
|
||||
*
|
||||
* @param XML_XRD $xrd Data storage the XML 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 XRD file
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
|
||||
* loaded
|
||||
*/
|
||||
public function loadFile($file)
|
||||
{
|
||||
$old = libxml_use_internal_errors(true);
|
||||
$x = simplexml_load_file($file);
|
||||
libxml_use_internal_errors($old);
|
||||
if ($x === false) {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Error loading XML file: ' . libxml_get_last_error()->message,
|
||||
XML_XRD_Loader_Exception::LOAD
|
||||
);
|
||||
}
|
||||
return $this->load($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the contents of the given string
|
||||
*
|
||||
* @param string $xml XML string
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
|
||||
* loaded
|
||||
*/
|
||||
public function loadString($xml)
|
||||
{
|
||||
if ($xml == '') {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Error loading XML string: string empty',
|
||||
XML_XRD_Loader_Exception::LOAD
|
||||
);
|
||||
}
|
||||
$old = libxml_use_internal_errors(true);
|
||||
$x = simplexml_load_string($xml);
|
||||
libxml_use_internal_errors($old);
|
||||
if ($x === false) {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Error loading XML string: ' . libxml_get_last_error()->message,
|
||||
XML_XRD_Loader_Exception::LOAD
|
||||
);
|
||||
}
|
||||
return $this->load($x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the XML element into the classes' data structures
|
||||
*
|
||||
* @param object $x XML element containing the whole XRD document
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_Loader_Exception When the XML is invalid
|
||||
*/
|
||||
public function load(SimpleXMLElement $x)
|
||||
{
|
||||
$ns = $x->getDocNamespaces();
|
||||
if ($ns[''] !== self::NS_XRD) {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'Wrong document namespace', XML_XRD_Loader_Exception::DOC_NS
|
||||
);
|
||||
}
|
||||
if ($x->getName() != 'XRD') {
|
||||
throw new XML_XRD_Loader_Exception(
|
||||
'XML root element is not "XRD"', XML_XRD_Loader_Exception::DOC_ROOT
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($x->Subject)) {
|
||||
$this->xrd->subject = (string)$x->Subject;
|
||||
}
|
||||
foreach ($x->Alias as $xAlias) {
|
||||
$this->xrd->aliases[] = (string)$xAlias;
|
||||
}
|
||||
|
||||
foreach ($x->Link as $xLink) {
|
||||
$this->xrd->links[] = $this->loadLink($xLink);
|
||||
}
|
||||
|
||||
$this->loadProperties($this->xrd, $x);
|
||||
|
||||
if (isset($x->Expires)) {
|
||||
$this->xrd->expires = strtotime($x->Expires);
|
||||
}
|
||||
|
||||
$xmlAttrs = $x->attributes('http://www.w3.org/XML/1998/namespace');
|
||||
if (isset($xmlAttrs['id'])) {
|
||||
$this->xrd->id = (string)$xmlAttrs['id'];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the Property elements from XML
|
||||
*
|
||||
* @param object $store Data store where the properties get stored
|
||||
* @param object $x XML element
|
||||
*
|
||||
* @return boolean True when all went well
|
||||
*/
|
||||
protected function loadProperties(
|
||||
XML_XRD_PropertyAccess $store, SimpleXMLElement $x
|
||||
) {
|
||||
foreach ($x->Property as $xProp) {
|
||||
$store->properties[] = $this->loadProperty($xProp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a link element object from XML element
|
||||
*
|
||||
* @param object $x XML link element
|
||||
*
|
||||
* @return XML_XRD_Element_Link Created link object
|
||||
*/
|
||||
protected function loadLink(SimpleXMLElement $x)
|
||||
{
|
||||
$link = new XML_XRD_Element_Link();
|
||||
foreach (array('rel', 'type', 'href', 'template') as $var) {
|
||||
if (isset($x[$var])) {
|
||||
$link->$var = (string)$x[$var];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($x->Title as $xTitle) {
|
||||
$xmlAttrs = $xTitle->attributes('http://www.w3.org/XML/1998/namespace');
|
||||
$lang = '';
|
||||
if (isset($xmlAttrs['lang'])) {
|
||||
$lang = (string)$xmlAttrs['lang'];
|
||||
}
|
||||
if (!isset($link->titles[$lang])) {
|
||||
$link->titles[$lang] = (string)$xTitle;
|
||||
}
|
||||
}
|
||||
$this->loadProperties($link, $x);
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a property element object from XML element
|
||||
*
|
||||
* @param object $x XML property element
|
||||
*
|
||||
* @return XML_XRD_Element_Property Created link object
|
||||
*/
|
||||
protected function loadProperty(SimpleXMLElement $x)
|
||||
{
|
||||
$prop = new XML_XRD_Element_Property();
|
||||
if (isset($x['type'])) {
|
||||
$prop->type = (string)$x['type'];
|
||||
}
|
||||
$s = (string)$x;
|
||||
if ($s != '') {
|
||||
$prop->value = $s;
|
||||
}
|
||||
|
||||
return $prop;
|
||||
}
|
||||
}
|
||||
?>
|
30
plugins/LRDD/extlib/XML/XRD/LogicException.php
Normal file
30
plugins/LRDD/extlib/XML/XRD/LogicException.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/Exception.php';
|
||||
|
||||
/**
|
||||
* XML_XRD exception that's thrown when something is not supported
|
||||
*
|
||||
* @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_LogicException extends LogicException implements XML_XRD_Exception
|
||||
{
|
||||
}
|
||||
|
||||
?>
|
133
plugins/LRDD/extlib/XML/XRD/PropertyAccess.php
Normal file
133
plugins/LRDD/extlib/XML/XRD/PropertyAccess.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/LogicException.php';
|
||||
require_once 'XML/XRD/Element/Property.php';
|
||||
|
||||
/**
|
||||
* Provides ArrayAccess to extending classes (XML_XRD and XML_XRD_Element_Link).
|
||||
*
|
||||
* By extending PropertyAccess, access to properties is possible with
|
||||
* "$object['propertyType']" array access notation.
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
abstract class XML_XRD_PropertyAccess implements ArrayAccess
|
||||
{
|
||||
|
||||
/**
|
||||
* Array of property objects
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $properties = array();
|
||||
|
||||
|
||||
/**
|
||||
* Check if the property with the given type exists
|
||||
*
|
||||
* Part of the ArrayAccess interface
|
||||
*
|
||||
* @param string $type Property type to check for
|
||||
*
|
||||
* @return boolean True if it exists
|
||||
*/
|
||||
public function offsetExists($type)
|
||||
{
|
||||
foreach ($this->properties as $prop) {
|
||||
if ($prop->type == $type) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the highest ranked property with the given type
|
||||
*
|
||||
* Part of the ArrayAccess interface
|
||||
*
|
||||
* @param string $type Property type to check for
|
||||
*
|
||||
* @return string Property value or NULL if empty
|
||||
*/
|
||||
public function offsetGet($type)
|
||||
{
|
||||
foreach ($this->properties as $prop) {
|
||||
if ($prop->type == $type) {
|
||||
return $prop->value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Not implemented.
|
||||
*
|
||||
* Part of the ArrayAccess interface
|
||||
*
|
||||
* @param string $type Property type to check for
|
||||
* @param string $value New property value
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_LogicException Always
|
||||
*/
|
||||
public function offsetSet($type, $value)
|
||||
{
|
||||
throw new XML_XRD_LogicException('Changing properties not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Not implemented.
|
||||
*
|
||||
* Part of the ArrayAccess interface
|
||||
*
|
||||
* @param string $type Property type to check for
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws XML_XRD_LogicException Always
|
||||
*/
|
||||
public function offsetUnset($type)
|
||||
{
|
||||
throw new XML_XRD_LogicException('Changing properties not implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all properties with the given type
|
||||
*
|
||||
* @param string $type Property type to filter by
|
||||
*
|
||||
* @return array Array of XML_XRD_Element_Property objects
|
||||
*/
|
||||
public function getProperties($type = null)
|
||||
{
|
||||
if ($type === null) {
|
||||
return $this->properties;
|
||||
}
|
||||
$properties = array();
|
||||
foreach ($this->properties as $prop) {
|
||||
if ($prop->type == $type) {
|
||||
$properties[] = $prop;
|
||||
}
|
||||
}
|
||||
return $properties;
|
||||
}
|
||||
}
|
||||
?>
|
79
plugins/LRDD/extlib/XML/XRD/Serializer.php
Normal file
79
plugins/LRDD/extlib/XML/XRD/Serializer.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/Serializer/Exception.php';
|
||||
|
||||
/**
|
||||
* Serialization dispatcher - loads the correct serializer for saving XRD data.
|
||||
*
|
||||
* @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_Serializer
|
||||
{
|
||||
/**
|
||||
* XRD data storage
|
||||
*
|
||||
* @var XML_XRD
|
||||
*/
|
||||
protected $xrd;
|
||||
|
||||
/**
|
||||
* Init object with xrd object
|
||||
*
|
||||
* @param XML_XRD $xrd Data storage the data are fetched from
|
||||
*/
|
||||
public function __construct(XML_XRD $xrd)
|
||||
{
|
||||
$this->xrd = $xrd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the XRD data into a string of the given type
|
||||
*
|
||||
* @param string $type File type: xml or json
|
||||
*
|
||||
* @return string Serialized data
|
||||
*/
|
||||
public function to($type)
|
||||
{
|
||||
return (string)$this->getSerializer($type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a XRD loader object for the given type
|
||||
*
|
||||
* @param string $type File type: xml or json
|
||||
*
|
||||
* @return XML_XRD_Loader
|
||||
*/
|
||||
protected function getSerializer($type)
|
||||
{
|
||||
$class = 'XML_XRD_Serializer_' . strtoupper($type);
|
||||
$file = str_replace('_', '/', $class) . '.php';
|
||||
include_once $file;
|
||||
if (class_exists($class)) {
|
||||
return new $class($this->xrd);
|
||||
}
|
||||
|
||||
throw new XML_XRD_Serializer_Exception(
|
||||
'No serializer for type "' . $type . '"',
|
||||
XML_XRD_Loader_Exception::NO_LOADER
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
29
plugins/LRDD/extlib/XML/XRD/Serializer/Exception.php
Normal file
29
plugins/LRDD/extlib/XML/XRD/Serializer/Exception.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
require_once 'XML/XRD/Exception.php';
|
||||
|
||||
/**
|
||||
* XML_XRD exception that's thrown when saving an XRD file fails.
|
||||
*
|
||||
* @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_Serializer_Exception extends Exception implements XML_XRD_Exception
|
||||
{
|
||||
}
|
||||
?>
|
94
plugins/LRDD/extlib/XML/XRD/Serializer/JSON.php
Normal file
94
plugins/LRDD/extlib/XML/XRD/Serializer/JSON.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generate JSON from a XML_XRD object (for JRD files).
|
||||
*
|
||||
* @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
|
||||
* @link http://tools.ietf.org/html/rfc6415#appendix-A
|
||||
*/
|
||||
class XML_XRD_Serializer_JSON
|
||||
{
|
||||
protected $xrd;
|
||||
|
||||
/**
|
||||
* Create new instance
|
||||
*
|
||||
* @param XML_XRD $xrd XRD instance to convert to JSON
|
||||
*/
|
||||
public function __construct(XML_XRD $xrd)
|
||||
{
|
||||
$this->xrd = $xrd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate JSON.
|
||||
*
|
||||
* @return string JSON code
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$o = new stdClass();
|
||||
if ($this->xrd->expires !== null) {
|
||||
$o->expires = gmdate('Y-m-d\TH:i:s\Z', $this->xrd->expires);
|
||||
}
|
||||
if ($this->xrd->subject !== null) {
|
||||
$o->subject = $this->xrd->subject;
|
||||
}
|
||||
foreach ($this->xrd->aliases as $alias) {
|
||||
$o->aliases[] = $alias;
|
||||
}
|
||||
foreach ($this->xrd->properties as $property) {
|
||||
$o->properties[$property->type] = $property->value;
|
||||
}
|
||||
$o->links = array();
|
||||
foreach ($this->xrd->links as $link) {
|
||||
$lid = count($o->links);
|
||||
$o->links[$lid] = new stdClass();
|
||||
if ($link->rel) {
|
||||
$o->links[$lid]->rel = $link->rel;
|
||||
}
|
||||
if ($link->type) {
|
||||
$o->links[$lid]->type = $link->type;
|
||||
}
|
||||
if ($link->href) {
|
||||
$o->links[$lid]->href = $link->href;
|
||||
}
|
||||
if ($link->template !== null && $link->href === null) {
|
||||
$o->links[$lid]->template = $link->template;
|
||||
}
|
||||
|
||||
foreach ($link->titles as $lang => $value) {
|
||||
if ($lang == null) {
|
||||
$lang = 'default';
|
||||
}
|
||||
$o->links[$lid]->titles[$lang] = $value;
|
||||
}
|
||||
foreach ($link->properties as $property) {
|
||||
$o->links[$lid]->properties[$property->type] = $property->value;
|
||||
}
|
||||
}
|
||||
if (count($o->links) == 0) {
|
||||
unset($o->links);
|
||||
}
|
||||
|
||||
return json_encode($o);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
137
plugins/LRDD/extlib/XML/XRD/Serializer/XML.php
Normal file
137
plugins/LRDD/extlib/XML/XRD/Serializer/XML.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generate XML from a XML_XRD object.
|
||||
*
|
||||
* @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_Serializer_XML
|
||||
{
|
||||
protected $xrd;
|
||||
|
||||
/**
|
||||
* Create new instance
|
||||
*
|
||||
* @param XML_XRD $xrd XRD instance to convert to XML
|
||||
*/
|
||||
public function __construct(XML_XRD $xrd)
|
||||
{
|
||||
$this->xrd = $xrd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate XML.
|
||||
*
|
||||
* @return string Full XML code
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$hasXsi = false;
|
||||
$x = new XMLWriter();
|
||||
$x->openMemory();
|
||||
//no encoding means UTF-8
|
||||
//http://www.w3.org/TR/2008/REC-xml-20081126/#sec-guessing-no-ext-info
|
||||
$x->startDocument('1.0', 'UTF-8');
|
||||
$x->setIndent(true);
|
||||
$x->startElement('XRD');
|
||||
$x->writeAttribute('xmlns', 'http://docs.oasis-open.org/ns/xri/xrd-1.0');
|
||||
$x->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
|
||||
if ($this->xrd->id) {
|
||||
$x->writeAttribute('xml:id', $this->xrd->id);
|
||||
}
|
||||
|
||||
if ($this->xrd->expires !== null) {
|
||||
$x->writeElement(
|
||||
'Expires', gmdate('Y-m-d\TH:i:s\Z', $this->xrd->expires)
|
||||
);
|
||||
}
|
||||
if ($this->xrd->subject !== null) {
|
||||
$x->writeElement('Subject', $this->xrd->subject);
|
||||
}
|
||||
foreach ($this->xrd->aliases as $alias) {
|
||||
$x->writeElement('Alias', $alias);
|
||||
}
|
||||
foreach ($this->xrd->properties as $property) {
|
||||
$this->writeProperty($x, $property, $hasXsi);
|
||||
}
|
||||
|
||||
foreach ($this->xrd->links as $link) {
|
||||
$x->startElement('Link');
|
||||
$x->writeAttribute('rel', $link->rel);
|
||||
if ($link->type !== null) {
|
||||
$x->writeAttribute('type', $link->type);
|
||||
}
|
||||
if ($link->href !== null) {
|
||||
$x->writeAttribute('href', $link->href);
|
||||
}
|
||||
//template only when no href
|
||||
if ($link->template !== null && $link->href === null) {
|
||||
$x->writeAttribute('template', $link->template);
|
||||
}
|
||||
|
||||
foreach ($link->titles as $lang => $value) {
|
||||
$x->startElement('Title');
|
||||
if ($lang) {
|
||||
$x->writeAttribute('xml:lang', $lang);
|
||||
}
|
||||
$x->text($value);
|
||||
$x->endElement();
|
||||
}
|
||||
foreach ($link->properties as $property) {
|
||||
$this->writeProperty($x, $property, $hasXsi);
|
||||
}
|
||||
$x->endElement();
|
||||
}
|
||||
|
||||
$x->endElement();
|
||||
$x->endDocument();
|
||||
$s = $x->flush();
|
||||
if (!$hasXsi) {
|
||||
$s = str_replace(
|
||||
' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"', '', $s
|
||||
);
|
||||
}
|
||||
return $s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a property in the XMLWriter stream output
|
||||
*
|
||||
* @param XMLWriter $x Writer object to write to
|
||||
* @param XML_XRD_Element_Property $property Property to write
|
||||
* @param boolean &$hasXsi If an xsi: attribute is used
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function writeProperty(
|
||||
XMLWriter $x, XML_XRD_Element_Property $property, &$hasXsi
|
||||
) {
|
||||
$x->startElement('Property');
|
||||
$x->writeAttribute('type', $property->type);
|
||||
if ($property->value === null) {
|
||||
$x->writeAttribute('xsi:nil', 'true');
|
||||
$hasXsi = true;
|
||||
} else {
|
||||
$x->text($property->value);
|
||||
}
|
||||
$x->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user