]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/XMPPHP/XMPP.php
cf4e7acd9d7abae0fa5b179081bc692f638e30a5
[quix0rs-gnu-social.git] / extlib / XMPPHP / XMPP.php
1 <?php
2 /**
3  * XMPPHP: The PHP XMPP Library
4  * Copyright (C) 2008  Nathanael C. Fritz
5  * This file is part of SleekXMPP.
6  * 
7  * XMPPHP is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * 
12  * XMPPHP 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 General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with XMPPHP; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  * @category   xmpphp 
22  * @package     XMPPHP
23  * @author       Nathanael C. Fritz <JID: fritzy@netflint.net>
24  * @author       Stephan Wentz <JID: stephan@jabber.wentz.it>
25  * @copyright  2008 Nathanael C. Fritz
26  */
27
28 /** XMPPHP_XMLStream */
29 require_once "XMLStream.php";
30
31 /**
32  * XMPPHP Main Class
33  * 
34  * @category   xmpphp 
35  * @package     XMPPHP
36  * @author       Nathanael C. Fritz <JID: fritzy@netflint.net>
37  * @author       Stephan Wentz <JID: stephan@jabber.wentz.it>
38  * @copyright  2008 Nathanael C. Fritz
39  * @version     $Id$
40  */
41 class XMPPHP_XMPP extends XMPPHP_XMLStream {
42         /**
43          * @var string
44          */
45         protected $server;
46
47         /**
48          * @var string
49          */
50         protected $user;
51         
52         /**
53          * @var string
54          */
55         protected $password;
56         
57         /**
58          * @var string
59          */
60         protected $resource;
61         
62         /**
63          * @var string
64          */
65         protected $fulljid;
66         
67         /**
68          * @var string
69          */
70         protected $basejid;
71         
72         /**
73          * @var boolean
74          */
75         protected $authed = false;
76         
77         /**
78          * @var boolean
79          */
80         protected $auto_subscribe = false;
81         
82         /**
83          * @var boolean
84          */
85         protected $use_encryption = true;
86         
87         /**
88          * Constructor
89          *
90          * @param string  $host
91          * @param integer $port
92          * @param string  $user
93          * @param string  $password
94          * @param string  $resource
95          * @param string  $server
96          * @param boolean $printlog
97          * @param string  $loglevel
98          */
99         public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null) {
100                 parent::__construct($host, $port, $printlog, $loglevel);
101                 
102                 $this->user      = $user;
103                 $this->password = $password;
104                 $this->resource = $resource;
105                 if(!$server) $server = $host;
106                 $this->basejid = $this->user . '@' . $this->host;
107
108                 $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
109                 $this->stream_end   = '</stream:stream>';
110                 $this->default_ns   = 'jabber:client';
111                 
112                 $this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler');
113                 $this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler');
114                 $this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler');
115                 $this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler');
116                 $this->addHandler('message', 'jabber:client', 'message_handler');
117                 $this->addHandler('presence', 'jabber:client', 'presence_handler');
118         }
119
120         /**
121          * Turn encryption on/ff
122          *
123          * @param boolean $useEncryption
124          */
125         public function useEncryption($useEncryption = true) {
126                 $this->use_encryption = $useEncryption;
127         }
128         
129         /**
130          * Turn on auto-authorization of subscription requests.
131          *
132          * @param boolean $autoSubscribe
133          */
134         public function autoSubscribe($autoSubscribe = true) {
135                 $this->auto_subscribe = $autoSubscribe;
136         }
137
138         /**
139          * Send XMPP Message
140          *
141          * @param string $to
142          * @param string $body
143          * @param string $type
144          * @param string $subject
145          */
146         public function message($to, $body, $type = 'chat', $subject = null, $payload = null) {
147                 $to       = htmlspecialchars($to);
148                 $body   = htmlspecialchars($body);
149                 $subject = htmlspecialchars($subject);
150                 
151                 $out = "<message from='{$this->fulljid}' to='$to' type='$type'>";
152                 if($subject) $out .= "<subject>$subject</subject>";
153                 $out .= "<body>$body</body>";
154                 if($payload) $out .= $payload;
155                 $out .= "</message>";
156                 
157                 $this->send($out);
158         }
159
160         /**
161          * Set Presence
162          *
163          * @param string $status
164          * @param string $show
165          * @param string $to
166          */
167         public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) {
168                 if($type == 'available') $type = '';
169                 $to      = htmlspecialchars($to);
170                 $status = htmlspecialchars($status);
171                 if($show == 'unavailable') $type = 'unavailable';
172                 
173                 $out = "<presence";
174                 if($to) $out .= " to='$to'";
175                 if($type) $out .= " type='$type'";
176                 if($show == 'available' and !$status and is_null($priority)) {
177                         $out .= "/>";
178                 } else {
179                         $out .= ">";
180                         if($show != 'available') $out .= "<show>$show</show>";
181                         if($status) $out .= "<status>$status</status>";
182                         if(!is_null($priority)) $out .= "<priority>$priority</priority>";
183                         $out .= "</presence>";
184                 }
185                 
186                 $this->send($out);
187         }
188
189         /**
190          * Message handler
191          *
192          * @param string $xml
193          */
194         public function message_handler($xml) {
195                 if(isset($xml->attrs['type'])) {
196                         $payload['type'] = $xml->attrs['type'];
197                 } else {
198                         $payload['type'] = 'chat';
199                 }
200                 $payload['from'] = $xml->attrs['from'];
201                 $payload['body'] = $xml->sub('body')->data;
202                 $payload['raw'] = $xml;
203                 $this->log->log("Message: {$xml->sub('body')->data}", XMPPHP_Log::LEVEL_DEBUG);
204                 $this->event('message', $payload);
205         }
206
207         /**
208          * Presence handler
209          *
210          * @param string $xml
211          */
212         public function presence_handler($xml) {
213                 $payload['type'] = (isset($xml->attrs['type'])) ? $xml->attrs['type'] : 'available';
214                 $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
215                 $payload['from'] = $xml->attrs['from'];
216                 $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
217                 $payload['raw'] = $xml;         
218                 $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}",  XMPPHP_Log::LEVEL_DEBUG);
219                 if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') {
220                         if($this->auto_subscribe) $this->send("<presence type='subscribed' to='{$xml->attrs['from']}' from='{$this->fulljid}' /><presence type='subscribe' to='{$xml->attrs['from']}' from='{$this->fulljid}' />");
221                         $this->event('subscription_requested', $payload);
222                 } elseif(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribed') {
223                         $this->event('subscription_accepted', $payload);
224                 } else {
225                         $this->event('presence', $payload);
226                 }
227         }
228
229         /**
230          * Features handler
231          *
232          * @param string $xml
233          */
234         protected function features_handler($xml) {
235                 if($xml->hasSub('starttls') and $this->use_encryption) {
236                         $this->send("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>");
237                 } elseif($xml->hasSub('bind')) {
238                         $id = $this->getId();
239                         $this->addIdHandler($id, 'resource_bind_handler');
240                         $this->send("<iq xmlns=\"jabber:client\" type=\"set\" id=\"$id\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>{$this->resource}</resource></bind></iq>");
241                 } else {
242                         $this->log->log("Attempting Auth...");
243                         $this->send("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>" . base64_encode("\x00" . $this->user . "\x00" . $this->password) . "</auth>");
244                 }
245         }
246
247         /**
248          * SASL success handler
249          *
250          * @param string $xml
251          */
252         protected function sasl_success_handler($xml) {
253                 $this->log->log("Auth success!");
254                 $this->authed = true;
255                 $this->reset();
256         }
257         
258         /**
259          * SASL feature handler
260          *
261          * @param string $xml
262          */
263         protected function sasl_failure_handler($xml) {
264                 $this->log->log("Auth failed!",  XMPPHP_Log::LEVEL_ERROR);
265                 $this->disconnect();
266                 
267                 throw new XMPPHP_Exception('Auth failed!');
268         }
269
270         /**
271          * Resource bind handler
272          *
273          * @param string $xml
274          */
275         protected function resource_bind_handler($xml) {
276                 if($xml->attrs['type'] == 'result') {
277                         $this->log->log("Bound to " . $xml->sub('bind')->sub('jid')->data);
278                         $this->fulljid = $xml->sub('bind')->sub('jid')->data;
279                 }
280                 $id = $this->getId();
281                 $this->addIdHandler($id, 'session_start_handler');
282                 $this->send("<iq xmlns='jabber:client' type='set' id='$id'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>");
283         }
284
285         /**
286         * Retrieves the roster
287         *
288         */
289         public function getRoster() {
290                 $id = $this->getID();
291                 $this->addIdHandler($id, 'roster_get_handler');
292                 $this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
293         }
294
295         /**
296         * Roster retrieval handler
297         *
298         * @param string $xml
299         */
300         protected function roster_get_handler($xml) {
301                 // TODO: make this work
302         }
303
304         /**
305          * Session start handler
306          *
307          * @param string $xml
308          */
309         protected function session_start_handler($xml) {
310                 $this->log->log("Session started");
311                 $this->event('session_start');
312         }
313
314         /**
315          * TLS proceed handler
316          *
317          * @param string $xml
318          */
319         protected function tls_proceed_handler($xml) {
320                 $this->log->log("Starting TLS encryption");
321                 stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
322                 $this->reset();
323         }
324 }