PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/LRDD/extlib/XML/XRD.php

https://gitlab.com/windigo-gs/windigos-gnu-social
PHP | 258 lines | 90 code | 21 blank | 147 comment | 21 complexity | e496102540eb6d4fa937ff15560ffa55 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. require_once 'XML/XRD/PropertyAccess.php';
  14. require_once 'XML/XRD/Element/Link.php';
  15. require_once 'XML/XRD/Loader.php';
  16. require_once 'XML/XRD/Serializer.php';
  17. /**
  18. * Main class used to load XRD documents from string or file.
  19. *
  20. * After loading the file, access to links is possible with get() and getAll(),
  21. * as well as foreach-iterating over the XML_XRD object.
  22. *
  23. * Property access is possible with getProperties() and array access (foreach)
  24. * on the XML_XRD object.
  25. *
  26. * Verification that the subject/aliases match the requested URL can be done with
  27. * describes().
  28. *
  29. * @category XML
  30. * @package XML_XRD
  31. * @author Christian Weiske <cweiske@php.net>
  32. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  33. * @version Release: @package_version@
  34. * @link http://pear.php.net/package/XML_XRD
  35. */
  36. class XML_XRD extends XML_XRD_PropertyAccess implements IteratorAggregate
  37. {
  38. /**
  39. * XRD file/string loading dispatcher
  40. *
  41. * @var XML_XRD_Loader
  42. */
  43. public $loader;
  44. /**
  45. * XRD serializing dispatcher
  46. *
  47. * @var XML_XRD_Serializer
  48. */
  49. public $serializer;
  50. /**
  51. * XRD subject
  52. *
  53. * @var string
  54. */
  55. public $subject;
  56. /**
  57. * Array of subject alias strings
  58. *
  59. * @var array
  60. */
  61. public $aliases = array();
  62. /**
  63. * Array of link objects
  64. *
  65. * @var array
  66. */
  67. public $links = array();
  68. /**
  69. * Unix timestamp when the document expires.
  70. * NULL when no expiry date set.
  71. *
  72. * @var integer|null
  73. */
  74. public $expires;
  75. /**
  76. * xml:id of the XRD document
  77. *
  78. * @var string|null
  79. */
  80. public $id;
  81. /**
  82. * Loads the contents of the given file.
  83. *
  84. * Note: Only use file type auto-detection for local files.
  85. * Do not use it on remote files as the file gets requested several times.
  86. *
  87. * @param string $file Path to an XRD file
  88. * @param string $type File type: xml or json, NULL for auto-detection
  89. *
  90. * @return void
  91. *
  92. * @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
  93. * loaded
  94. */
  95. public function loadFile($file, $type = null)
  96. {
  97. if (!isset($this->loader)) {
  98. $this->loader = new XML_XRD_Loader($this);
  99. }
  100. return $this->loader->loadFile($file, $type);
  101. }
  102. /**
  103. * Loads the contents of the given string
  104. *
  105. * @param string $str XRD string
  106. * @param string $type File type: xml or json, NULL for auto-detection
  107. *
  108. * @return void
  109. *
  110. * @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
  111. * loaded
  112. */
  113. public function loadString($str, $type = null)
  114. {
  115. if (!isset($this->loader)) {
  116. $this->loader = new XML_XRD_Loader($this);
  117. }
  118. return $this->loader->loadString($str, $type);
  119. }
  120. /**
  121. * Checks if the XRD document describes the given URI.
  122. *
  123. * This should always be used to make sure the XRD file
  124. * is the correct one for e.g. the given host, and not a copycat.
  125. *
  126. * Checks against the subject and aliases
  127. *
  128. * @param string $uri An URI that the document is expected to describe
  129. *
  130. * @return boolean True or false
  131. */
  132. public function describes($uri)
  133. {
  134. if ($this->subject == $uri) {
  135. return true;
  136. }
  137. foreach ($this->aliases as $alias) {
  138. if ($alias == $uri) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * Get the link with highest priority for the given relation and type.
  146. *
  147. * @param string $rel Relation name
  148. * @param string $type MIME Type
  149. * @param boolean $typeFallback When true and no link with the given type
  150. * could be found, the best link without a
  151. * type will be returned
  152. *
  153. * @return XML_XRD_Element_Link Link object or NULL if none found
  154. */
  155. public function get($rel, $type = null, $typeFallback = true)
  156. {
  157. $links = $this->getAll($rel, $type, $typeFallback);
  158. if (count($links) == 0) {
  159. return null;
  160. }
  161. return $links[0];
  162. }
  163. /**
  164. * Get all links with the given relation and type, highest priority first.
  165. *
  166. * @param string $rel Relation name
  167. * @param string $type MIME Type
  168. * @param boolean $typeFallback When true and no link with the given type
  169. * could be found, the best link without a
  170. * type will be returned
  171. *
  172. * @return array Array of XML_XRD_Element_Link objects
  173. */
  174. public function getAll($rel, $type = null, $typeFallback = true)
  175. {
  176. $links = array();
  177. $exactType = false;
  178. foreach ($this->links as $link) {
  179. if ($link->rel == $rel
  180. && ($type === null || $link->type == $type
  181. || $typeFallback && $link->type === null)
  182. ) {
  183. $links[] = $link;
  184. $exactType |= $typeFallback && $type !== null
  185. && $link->type == $type;
  186. }
  187. }
  188. if ($exactType) {
  189. //remove all links without type
  190. $exactlinks = array();
  191. foreach ($links as $link) {
  192. if ($link->type !== null) {
  193. $exactlinks[] = $link;
  194. }
  195. }
  196. $links = $exactlinks;
  197. }
  198. return $links;
  199. }
  200. /**
  201. * Return the iterator object to loop over the links
  202. *
  203. * Part of the IteratorAggregate interface
  204. *
  205. * @return Traversable Iterator for the links
  206. */
  207. public function getIterator()
  208. {
  209. return new ArrayIterator($this->links);
  210. }
  211. /**
  212. * Converts this XRD object to XML or JSON.
  213. *
  214. * @param string $type Serialization type: xml or json
  215. *
  216. * @return string Generated content
  217. */
  218. public function to($type)
  219. {
  220. if (!isset($this->serializer)) {
  221. $this->serializer = new XML_XRD_Serializer($this);
  222. }
  223. return $this->serializer->to($type);
  224. }
  225. /**
  226. * Converts this XRD object to XML.
  227. *
  228. * @return string Generated XML
  229. *
  230. * @deprecated use to('xml')
  231. */
  232. public function toXML()
  233. {
  234. return $this->to('xml');
  235. }
  236. }
  237. ?>