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