]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/LRDD/extlib/XML/XRD/Loader/JSON.php
Implemented WebFinger and replaced our XRD with PEAR XML_XRD
[quix0rs-gnu-social.git] / plugins / LRDD / extlib / XML / XRD / Loader / JSON.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 /**
15  * Loads XRD data from a JSON file
16  *
17  * @category XML
18  * @package  XML_XRD
19  * @author   Christian Weiske <cweiske@php.net>
20  * @license  http://www.gnu.org/copyleft/lesser.html LGPL
21  * @version  Release: @package_version@
22  * @link     http://pear.php.net/package/XML_XRD
23  */
24 class XML_XRD_Loader_JSON
25 {
26     /**
27      * Data storage the XML data get loaded into
28      *
29      * @var XML_XRD
30      */
31     protected $xrd;
32
33
34
35     /**
36      * Init object with xrd object
37      *
38      * @param XML_XRD $xrd Data storage the JSON data get loaded into
39      */
40     public function __construct(XML_XRD $xrd)
41     {
42         $this->xrd = $xrd;
43     }
44
45     /**
46      * Loads the contents of the given file
47      *
48      * @param string $file Path to an JRD file
49      *
50      * @return void
51      *
52      * @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
53      *                                   loaded
54      */
55     public function loadFile($file)
56     {
57         $json = file_get_contents($file);
58         if ($json === false) {
59             throw new XML_XRD_Loader_Exception(
60                 'Error loading JRD file: ' . $file,
61                 XML_XRD_Loader_Exception::LOAD
62             );
63         }
64         return $this->loadString($json);
65     }
66
67     /**
68      * Loads the contents of the given string
69      *
70      * @param string $json JSON string
71      *
72      * @return void
73      *
74      * @throws XML_XRD_Loader_Exception When the JSON is invalid or cannot be
75      *                                   loaded
76      */
77     public function loadString($json)
78     {
79         if ($json == '') {
80             throw new XML_XRD_Loader_Exception(
81                 'Error loading JRD: string empty',
82                 XML_XRD_Loader_Exception::LOAD
83             );
84         }
85
86         $obj = json_decode($json);
87         if ($obj !== null) {
88             return $this->load($obj);
89         }
90
91         $constants = get_defined_constants(true);
92         $json_errors = array();
93         foreach ($constants['json'] as $name => $value) {
94             if (!strncmp($name, 'JSON_ERROR_', 11)) {
95                 $json_errors[$value] = $name;
96             }
97         }
98         throw new XML_XRD_Loader_Exception(
99             'Error loading JRD: ' . $json_errors[json_last_error()],
100             XML_XRD_Loader_Exception::LOAD
101         );
102     }
103
104     /**
105      * Loads the JSON object into the classes' data structures
106      *
107      * @param object $j JSON object containing the whole JSON document
108      *
109      * @return void
110      */
111     public function load(stdClass $j)
112     {
113         if (isset($j->subject)) {
114             $this->xrd->subject = (string)$j->subject;
115         }
116         if (isset($j->aliases)) {
117             foreach ($j->aliases as $jAlias) {
118                 $this->xrd->aliases[] = (string)$jAlias;
119             }
120         }
121
122         if (isset($j->links)) {
123             foreach ($j->links as $jLink) {
124                 $this->xrd->links[] = $this->loadLink($jLink);
125             }
126         }
127
128         $this->loadProperties($this->xrd, $j);
129
130         if (isset($j->expires)) {
131             $this->xrd->expires = strtotime($j->expires);
132         }
133     }
134
135     /**
136      * Loads the Property elements from XML
137      *
138      * @param object $store Data store where the properties get stored
139      * @param object $j     JSON element with "properties" variable
140      *
141      * @return boolean True when all went well
142      */
143     protected function loadProperties(
144         XML_XRD_PropertyAccess $store, stdClass $j
145     ) {
146         if (!isset($j->properties)) {
147             return true;
148         }
149
150         foreach ($j->properties as $type => $jProp) {
151             $store->properties[] = new XML_XRD_Element_Property(
152                 $type, (string)$jProp
153             );
154         }
155         
156         return true;
157     }
158
159     /**
160      * Create a link element object from XML element
161      *
162      * @param object $j JSON link object
163      *
164      * @return XML_XRD_Element_Link Created link object
165      */
166     protected function loadLink(stdClass $j)
167     {
168         $link = new XML_XRD_Element_Link();
169         foreach (array('rel', 'type', 'href', 'template') as $var) {
170             if (isset($j->$var)) {
171                 $link->$var = (string)$j->$var;
172             }
173         }
174
175         if (isset($j->titles)) {
176             foreach ($j->titles as $lang => $jTitle) {
177                 if (!isset($link->titles[$lang])) {
178                     $link->titles[$lang] = (string)$jTitle;
179                 }
180             }
181         }
182         $this->loadProperties($link, $j);
183
184         return $link;
185     }
186 }
187 ?>