]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Comet/bayeux.class.inc.php
Localisation updates from http://translatewiki.net.
[quix0rs-gnu-social.git] / plugins / Comet / bayeux.class.inc.php
1 <?php
2 /*
3  * Phomet: a php comet client
4  *
5  * Copyright (C) 2008 Morgan 'ARR!' Allen <morganrallen@gmail.com> http://morglog.alleycatracing.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  *
21  */
22
23 class Bayeux
24 {
25     private $oCurl = '';
26     private $nNextId = 0;
27
28     private $sUser = '';
29     private $sPassword = '';
30
31     public $sUrl = '';
32
33     function __construct($sUrl, $sUser='', $sPassword='')
34     {
35         $this->sUrl = $sUrl;
36
37         $this->oCurl = curl_init();
38
39         $aHeaders = array();
40         $aHeaders[] = 'Connection: Keep-Alive';
41
42         curl_setopt($this->oCurl, CURLOPT_URL, $sUrl);
43         curl_setopt($this->oCurl, CURLOPT_HTTPHEADER, $aHeaders);
44         curl_setopt($this->oCurl, CURLOPT_HEADER, 0);
45         curl_setopt($this->oCurl, CURLOPT_POST, 1);
46         curl_setopt($this->oCurl, CURLOPT_RETURNTRANSFER,1);
47
48         if (!is_null($sUser) && mb_strlen($sUser) > 0) {
49             curl_setopt($this->oCurl, CURLOPT_USERPWD,"$sUser:$sPassword");
50         }
51
52         $this->handShake();
53     }
54
55     function __destruct()
56     {
57         $this->disconnect();
58     }
59
60     function handShake()
61     {
62         $msgHandshake = array();
63         $msgHandshake['channel'] = '/meta/handshake';
64         $msgHandshake['version'] = "1.0";
65         $msgHandshake['minimumVersion'] = "0.9";
66         $msgHandshake['supportedConnectionTypes'] = array('long-polling');
67         $msgHandshake['id'] = $this->nNextId++;
68
69         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
70
71         $data = curl_exec($this->oCurl);
72
73         if(curl_errno($this->oCurl))
74           die("Error: " . curl_error($this->oCurl));
75
76         $oReturn = json_decode($data);
77
78         if (is_array($oReturn)) {
79             $oReturn = $oReturn[0];
80         }
81
82         $bSuccessful = ($oReturn->successful) ? true : false;
83
84         if($bSuccessful)
85         {
86             $this->clientId = $oReturn->clientId;
87
88             $this->connect();
89         }
90     }
91
92     public function connect()
93     {
94         $aMsg['channel'] = '/meta/connect';
95         $aMsg['id'] = $this->nNextId++;
96         $aMsg['clientId'] = $this->clientId;
97         $aMsg['connectionType'] = 'long-polling';
98
99         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
100
101         $data = curl_exec($this->oCurl);
102     }
103
104     function disconnect()
105     {
106         $msgHandshake = array();
107         $msgHandshake['channel'] = '/meta/disconnect';
108         $msgHandshake['id'] = $this->nNextId++;
109         $msgHandshake['clientId'] = $this->clientId;
110
111         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($msgHandshake)))));
112
113         curl_exec($this->oCurl);
114     }
115
116     public function publish($sChannel, $oData)
117     {
118         if(!$sChannel || !$oData)
119           return;
120
121         $aMsg = array();
122
123         $aMsg['channel'] = $sChannel;
124         $aMsg['id'] = $this->nNextId++;
125         $aMsg['data'] = $oData;
126         $aMsg['clientId'] = $this->clientId;
127
128         curl_setopt($this->oCurl, CURLOPT_POSTFIELDS, "message=".urlencode(str_replace('\\', '', json_encode(array($aMsg)))));
129
130         $data = curl_exec($this->oCurl);
131 //        var_dump($data);
132     }
133 }