]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LRDD/extlib/XML/XRD/Loader.php
Implemented WebFinger and replaced our XRD with PEAR XML_XRD
[quix0rs-gnu-social.git] / plugins / LRDD / extlib / XML / XRD / Loader.php
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
14 require_once 'XML/XRD/Loader/Exception.php';
15
16 /**
17  * File/string loading dispatcher.
18  * Loads the correct loader for the type of XRD file (XML or JSON).
19  * Also provides type auto-detection.
20  *
21  * @category XML
22  * @package  XML_XRD
23  * @author   Christian Weiske <cweiske@php.net>
24  * @license  http://www.gnu.org/copyleft/lesser.html LGPL
25  * @version  Release: @package_version@
26  * @link     http://pear.php.net/package/XML_XRD
27  */
28 class XML_XRD_Loader
29 {
30     public function __construct(XML_XRD $xrd)
31     {
32         $this->xrd = $xrd;
33     }
34
35     /**
36      * Loads the contents of the given file.
37      *
38      * Note: Only use file type auto-detection for local files.
39      * Do not use it on remote files as the file gets requested several times.
40      *
41      * @param string $file Path to an XRD file
42      * @param string $type File type: xml or json, NULL for auto-detection
43      *
44      * @return void
45      *
46      * @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
47      *                                   loaded
48      */
49     public function loadFile($file, $type = null)
50     {
51         if ($type === null) {
52             $type = $this->detectTypeFromFile($file);
53         }
54         $loader = $this->getLoader($type);
55         $loader->loadFile($file);
56     }
57
58     /**
59      * Loads the contents of the given string
60      *
61      * @param string $str  XRD string
62      * @param string $type File type: xml or json, NULL for auto-detection
63      *
64      * @return void
65      *
66      * @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
67      *                                   loaded
68      */
69     public function loadString($str, $type = null)
70     {
71         if ($type === null) {
72             $type = $this->detectTypeFromString($str);
73         }
74         $loader = $this->getLoader($type);
75         $loader->loadString($str);
76     }
77
78     /**
79      * Creates a XRD loader object for the given type
80      *
81      * @param string $type File type: xml or json
82      *
83      * @return XML_XRD_Loader
84      */
85     protected function getLoader($type)
86     {
87         $class = 'XML_XRD_Loader_' . strtoupper($type);
88         $file = str_replace('_', '/', $class) . '.php';
89         include_once $file;
90         if (class_exists($class)) {
91             return new $class($this->xrd);
92         }
93
94         throw new XML_XRD_Loader_Exception(
95             'No loader for XRD type "' . $type . '"',
96             XML_XRD_Loader_Exception::NO_LOADER
97         );
98     }
99
100     /**
101      * Tries to detect the file type (xml or json) from the file content
102      *
103      * @param string $file File name to check
104      *
105      * @return string File type ('xml' or 'json')
106      *
107      * @throws XML_XRD_Loader_Exception When opening the file fails.
108      */
109     public function detectTypeFromFile($file)
110     {
111         if (!file_exists($file)) {
112             throw new XML_XRD_Loader_Exception(
113                 'Error loading XRD file: File does not exist',
114                 XML_XRD_Loader_Exception::OPEN_FILE
115             );
116         }
117         $handle = fopen($file, 'r');
118         if (!$handle) {
119             throw new XML_XRD_Loader_Exception(
120                 'Cannot open file to determine type',
121                 XML_XRD_Loader_Exception::OPEN_FILE
122             );
123         }
124
125         $str = (string)fgets($handle, 10);
126         fclose($handle);
127         return $this->detectTypeFromString($str);
128     }
129
130     /**
131      * Tries to detect the file type from the content of the file
132      *
133      * @param string $str Content of XRD file
134      *
135      * @return string File type ('xml' or 'json')
136      *
137      * @throws XML_XRD_Loader_Exception When the type cannot be detected
138      */
139     public function detectTypeFromString($str)
140     {
141         if (substr($str, 0, 1) == '{') {
142             return 'json';
143         } else if (substr($str, 0, 5) == '<?xml') {
144             return 'xml';
145         }
146
147         throw new XML_XRD_Loader_Exception(
148             'Detecting file type failed',
149             XML_XRD_Loader_Exception::DETECT_TYPE
150         );
151     }
152
153
154 }
155
156 ?>