]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OMB/extlib/libomb/plain_xrds_writer.php
Squashed commit of the following:
[quix0rs-gnu-social.git] / plugins / OMB / extlib / libomb / plain_xrds_writer.php
1 <?php
2 /**
3  * This file is part of libomb
4  *
5  * PHP version 5
6  *
7  * LICENSE: This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @package OMB
21  * @author  Adrian Lang <mail@adrianlang.de>
22  * @license http://www.gnu.org/licenses/agpl.html GNU AGPL 3.0
23  * @version 0.1a-20090828
24  * @link    http://adrianlang.de/libomb
25  */
26
27 require_once 'xrds_writer.php';
28
29 /**
30  * Write OMB-specific XRDS using XMLWriter
31  *
32  * This class writes the XRDS file announcing the OMB server. It uses XMLWriter.
33  * An instance of OMB_Plain_XRDS_Writer should be passed to
34  * OMB_Service_Provider->writeXRDS.
35  */
36 class OMB_Plain_XRDS_Writer implements OMB_XRDS_Writer
37 {
38     /**
39      * Write XRDS using XMLWriter
40      *
41      * Outputs a XRDS document specifying an OMB service.
42      *
43      * @param OMB_profile     $user   The target user for the OMB service
44      * @param OMB_XRDS_Mapper $mapper An OMB_XRDS_Mapper providing endpoint URLs
45      */
46     public function writeXRDS($user, $mapper)
47     {
48         header('Content-Type: application/xrds+xml');
49         $xw = new XMLWriter();
50         $xw->openURI('php://output');
51         $xw->setIndent(true);
52
53         $xw->startDocument('1.0', 'UTF-8');
54         $this->_writeFullElement($xw, 'XRDS', array('xmlns' => 'xri://$xrds'), array(
55         array('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
56                             'xml:id' => 'oauth',
57                             'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
58                             'version' => '2.0'), array(
59           array('Type', null, 'xri://$xrds*simple'),
60           array('Service', null, array(
61             array('Type', null, OAUTH_ENDPOINT_REQUEST),
62             array('URI', null, $mapper->getURL(OAUTH_ENDPOINT_REQUEST)),
63             array('Type', null, OAUTH_AUTH_HEADER),
64             array('Type', null, OAUTH_POST_BODY),
65             array('Type', null, OAUTH_HMAC_SHA1),
66             array('LocalID', null, $user->getIdentifierURI())
67           )),
68           array('Service', null, array(
69             array('Type', null, OAUTH_ENDPOINT_AUTHORIZE),
70             array('URI', null, $mapper->getURL(OAUTH_ENDPOINT_AUTHORIZE)),
71             array('Type', null, OAUTH_AUTH_HEADER),
72             array('Type', null, OAUTH_POST_BODY),
73             array('Type', null, OAUTH_HMAC_SHA1)
74           )),
75           array('Service', null, array(
76             array('Type', null, OAUTH_ENDPOINT_ACCESS),
77             array('URI', null, $mapper->getURL(OAUTH_ENDPOINT_ACCESS)),
78             array('Type', null, OAUTH_AUTH_HEADER),
79             array('Type', null, OAUTH_POST_BODY),
80             array('Type', null, OAUTH_HMAC_SHA1)
81           )),
82           array('Service', null, array(
83             array('Type', null, OAUTH_ENDPOINT_RESOURCE),
84             array('Type', null, OAUTH_AUTH_HEADER),
85             array('Type', null, OAUTH_POST_BODY),
86             array('Type', null, OAUTH_HMAC_SHA1)
87           ))
88         )),
89         array('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
90                            'xml:id' => 'omb',
91                            'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
92                            'version' => '2.0'), array(
93           array('Type', null, 'xri://$xrds*simple'),
94           array('Service', null, array(
95             array('Type', null, OMB_ENDPOINT_POSTNOTICE),
96             array('URI', null, $mapper->getURL(OMB_ENDPOINT_POSTNOTICE))
97           )),
98           array('Service', null, array(
99             array('Type', null, OMB_ENDPOINT_UPDATEPROFILE),
100             array('URI', null, $mapper->getURL(OMB_ENDPOINT_UPDATEPROFILE))
101           ))
102         )),
103         array('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
104                            'version' => '2.0'), array(
105           array('Type', null, 'xri://$xrds*simple'),
106           array('Service', null, array(
107             array('Type', null, OAUTH_DISCOVERY),
108             array('URI', null, '#oauth')
109           )),
110           array('Service', null, array(
111             array('Type', null, OMB_VERSION),
112             array('URI', null, '#omb')
113           ))
114         ))));
115         $xw->endDocument();
116         $xw->flush();
117     }
118
119     /**
120      * Write a complex XML element
121      *
122      * Outputs a XML element with attributes and content.
123      *
124      * @param XMLWriter    $xw         The XMLWriter used to output the element
125      * @param string       $tag        The tag name
126      * @param array|null   $attributes A map of XML attributes
127      * @param array|string $content    The content of the element; either an
128      *                                 array of child nodes each specified by a
129      *                                 three entry-array ($tag, $attributes,
130      *                                 $content) or a string
131      */
132     private function _writeFullElement($xw, $tag, $attributes, $content)
133     {
134         $xw->startElement($tag);
135         if (!is_null($attributes)) {
136             foreach ($attributes as $name => $value) {
137                 $xw->writeAttribute($name, $value);
138             }
139         }
140         if (is_array($content)) {
141             foreach ($content as $val) {
142                 $this->_writeFullElement($xw, $val[0], $val[1], $val[2]);
143             }
144         } else {
145             $xw->text($content);
146         }
147         $xw->fullEndElement();
148     }
149 }
150 ?>