Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/jabber/XMPP/BOSH.php
Go to the documentation of this file.
00001 <?php
00030 require_once dirname(__FILE__) . "/XMPP.php";
00031 
00043 class XMPPHP_BOSH extends XMPPHP_XMPP {
00044 
00045                 protected $rid;
00046                 protected $sid;
00047                 protected $http_server;
00048                 protected $http_buffer = Array();
00049                 protected $session = false;
00050 
00051                 public function connect($server, $wait='1', $session=false) {
00052                         $this->http_server = $server;
00053                         $this->use_encryption = false;
00054                         $this->session = $session;
00055 
00056                         $this->rid = 3001;
00057                         $this->sid = null;
00058                         if($session)
00059                         {
00060                                 $this->loadSession();
00061                         }
00062                         if(!$this->sid) {
00063                                 $body = $this->__buildBody();
00064                                 $body->addAttribute('hold','1');
00065                                 $body->addAttribute('to', $this->host);
00066                                 $body->addAttribute('route', "xmpp:{$this->host}:{$this->port}");
00067                                 $body->addAttribute('secure','true');
00068                                 $body->addAttribute('xmpp:version','1.6', 'urn:xmpp:xbosh');
00069                                 $body->addAttribute('wait', strval($wait));
00070                                 $body->addAttribute('ack','1');
00071                                 $body->addAttribute('xmlns:xmpp','urn:xmpp:xbosh');
00072                                 $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
00073                                 xml_parse($this->parser, $buff, false);
00074                                 $response = $this->__sendBody($body);
00075                                 $rxml = new SimpleXMLElement($response);
00076                                 $this->sid = $rxml['sid'];
00077 
00078                         } else {
00079                                 $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
00080                                 xml_parse($this->parser, $buff, false);
00081                         }
00082                 }
00083 
00084                 public function __sendBody($body=null, $recv=true) {
00085                         if(!$body) {
00086                                 $body = $this->__buildBody();
00087                         }
00088                         $ch = curl_init($this->http_server);
00089                         curl_setopt($ch, CURLOPT_HEADER, 0);
00090                         curl_setopt($ch, CURLOPT_POST, 1);
00091                         curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML());
00092                         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
00093                         $header = array('Accept-Encoding: gzip, deflate','Content-Type: text/xml; charset=utf-8');
00094                         curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
00095                         curl_setopt($ch, CURLOPT_VERBOSE, 0);
00096                         $output = '';
00097                         if($recv) {
00098                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00099                                 $output = curl_exec($ch);
00100                                 $this->http_buffer[] = $output;
00101                         }
00102                         curl_close($ch);
00103                         return $output;
00104                 }
00105 
00106                 public function __buildBody($sub=null) {
00107                         $xml = new SimpleXMLElement("<body xmlns='http://jabber.org/protocol/httpbind' xmlns:xmpp='urn:xmpp:xbosh' />");
00108                         $xml->addAttribute('content', 'text/xml; charset=utf-8');
00109                         $xml->addAttribute('rid', $this->rid);
00110                         $this->rid += 1;
00111                         if($this->sid) $xml->addAttribute('sid', $this->sid);
00112                         #if($this->sid) $xml->addAttribute('xmlns', 'http://jabber.org/protocol/httpbind');
00113                         $xml->addAttribute('xml:lang', 'en');
00114                         if($sub) { // ok, so simplexml is lame
00115                                 $p = dom_import_simplexml($xml);
00116                                 $c = dom_import_simplexml($sub);
00117                                 $cn = $p->ownerDocument->importNode($c, true);
00118                                 $p->appendChild($cn);
00119                                 $xml = simplexml_import_dom($p);
00120                         }
00121                         return $xml;
00122                 }
00123 
00124                 public function __process() {
00125                         if($this->http_buffer) {
00126                                 $this->__parseBuffer();
00127                         } else {
00128                                 $this->__sendBody();
00129                                 $this->__parseBuffer();
00130                         }
00131                 }
00132 
00133                 public function __parseBuffer() {
00134                         while ($this->http_buffer) {
00135                                 $idx = key($this->http_buffer);
00136                                 $buffer = $this->http_buffer[$idx];
00137                                 unset($this->http_buffer[$idx]);
00138                                 if($buffer) {
00139                                         $xml = new SimpleXMLElement($buffer);
00140                                         $children = $xml->xpath('child::node()');
00141                                         foreach ($children as $child) {
00142                                                 $buff = $child->asXML();
00143                                                 $this->log->log("RECV: $buff",  XMPPHP_Log::LEVEL_VERBOSE);
00144                                                 xml_parse($this->parser, $buff, false);
00145                                         }
00146                                 }
00147                         }
00148                 }
00149 
00150                 public function send($msg) {
00151                         $this->log->log("SEND: $msg",  XMPPHP_Log::LEVEL_VERBOSE);
00152                         $msg = new SimpleXMLElement($msg);
00153                         #$msg->addAttribute('xmlns', 'jabber:client');
00154                         $this->__sendBody($this->__buildBody($msg), true);
00155                         #$this->__parseBuffer();
00156                 }
00157 
00158                 public function reset() {
00159                         $this->xml_depth = 0;
00160                         unset($this->xmlobj);
00161                         $this->xmlobj = array();
00162                         $this->setupParser();
00163                         #$this->send($this->stream_start);
00164                         $body = $this->__buildBody();
00165                         $body->addAttribute('to', $this->host);
00166                         $body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh');
00167                         $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
00168                         $response = $this->__sendBody($body);
00169                         $this->been_reset = true;
00170                         xml_parse($this->parser, $buff, false);
00171                 }
00172 
00173                 public function loadSession() {
00174                         if(isset($_SESSION['XMPPHP_BOSH_RID'])) $this->rid = $_SESSION['XMPPHP_BOSH_RID'];
00175                         if(isset($_SESSION['XMPPHP_BOSH_SID'])) $this->sid = $_SESSION['XMPPHP_BOSH_SID'];
00176                         if(isset($_SESSION['XMPPHP_BOSH_authed'])) $this->authed = $_SESSION['XMPPHP_BOSH_authed'];
00177                         if(isset($_SESSION['XMPPHP_BOSH_jid'])) $this->jid = $_SESSION['XMPPHP_BOSH_jid'];
00178                         if(isset($_SESSION['XMPPHP_BOSH_fulljid'])) $this->fulljid = $_SESSION['XMPPHP_BOSH_fulljid'];
00179                 }
00180 
00181                 public function saveSession() {
00182                         $_SESSION['XMPPHP_BOSH_RID'] = (string) $this->rid;
00183                         $_SESSION['XMPPHP_BOSH_SID'] = (string) $this->sid;
00184                         $_SESSION['XMPPHP_BOSH_authed'] = (boolean) $this->authed;
00185                         $_SESSION['XMPPHP_BOSH_jid'] = (string) $this->jid;
00186                         $_SESSION['XMPPHP_BOSH_fulljid'] = (string) $this->fulljid;
00187                 }
00188 }
 All Data Structures Namespaces Files Functions Variables Enumerations