Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311]

   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   * @author	  Michael Garvin <JID: gar@netflint.net>
  26   * @copyright  2008 Nathanael C. Fritz
  27   */
  28  
  29  /** XMPPHP_XMLStream */
  30  require_once dirname(__FILE__) . "/XMPP.php";
  31  
  32  /**
  33   * XMPPHP Main Class
  34   * 
  35   * @category   xmpphp 
  36   * @package	 XMPPHP
  37   * @author	  Nathanael C. Fritz <JID: fritzy@netflint.net>
  38   * @author	  Stephan Wentz <JID: stephan@jabber.wentz.it>
  39   * @author	  Michael Garvin <JID: gar@netflint.net>
  40   * @copyright  2008 Nathanael C. Fritz
  41   * @version	 $Id$
  42   */
  43  class XMPPHP_BOSH extends XMPPHP_XMPP {
  44  
  45  	 	 protected $rid;
  46  	 	 protected $sid;
  47  	 	 protected $http_server;
  48  	 	 protected $http_buffer = Array();
  49  	 	 protected $session = false;
  50  
  51  		public function connect($server, $wait='1', $session=false) {
  52  	 	 	 $this->http_server = $server;
  53  	 	 	 $this->use_encryption = false;
  54  	 	 	 $this->session = $session;
  55  
  56  	 	 	 $this->rid = 3001;
  57  	 	 	 $this->sid = null;
  58  	 	 	 if($session)
  59  	 	 	 {
  60  	 	 	 	 $this->loadSession();
  61  	 	 	 }
  62  	 	 	 if(!$this->sid) {
  63  	 	 	 	 $body = $this->__buildBody();
  64  	 	 	 	 $body->addAttribute('hold','1');
  65  	 	 	 	 $body->addAttribute('to', $this->host);
  66  	 	 	 	 $body->addAttribute('route', "xmpp:{$this->host}:{$this->port}");
  67  	 	 	 	 $body->addAttribute('secure','true');
  68  	 	 	 	 $body->addAttribute('xmpp:version','1.6', 'urn:xmpp:xbosh');
  69  	 	 	 	 $body->addAttribute('wait', strval($wait));
  70  	 	 	 	 $body->addAttribute('ack','1');
  71  	 	 	 	 $body->addAttribute('xmlns:xmpp','urn:xmpp:xbosh');
  72  	 	 	 	 $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
  73  	 	 	 	 xml_parse($this->parser, $buff, false);
  74  	 	 	 	 $response = $this->__sendBody($body);
  75  	 	 	 	 $rxml = new SimpleXMLElement($response);
  76  	 	 	 	 $this->sid = $rxml['sid'];
  77  
  78  	 	 	 } else {
  79  	 	 	 	 $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
  80  	 	 	 	 xml_parse($this->parser, $buff, false);
  81  	 	 	 }
  82  	 	 }
  83  
  84  		public function __sendBody($body=null, $recv=true) {
  85  	 	 	 if(!$body) {
  86  	 	 	 	 $body = $this->__buildBody();
  87  	 	 	 }
  88  	 	 	 $ch = curl_init($this->http_server);
  89  	 	 	 curl_setopt($ch, CURLOPT_HEADER, 0);
  90  	 	 	 curl_setopt($ch, CURLOPT_POST, 1);
  91  	 	 	 curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML());
  92  	 	 	 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  93  	 	 	 $header = array('Accept-Encoding: gzip, deflate','Content-Type: text/xml; charset=utf-8');
  94  	 	 	 curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
  95  	 	 	 curl_setopt($ch, CURLOPT_VERBOSE, 0);
  96  	 	 	 $output = '';
  97  	 	 	 if($recv) {
  98  	 	 	 	 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  99  	 	 	 	 $output = curl_exec($ch);
 100  	 	 	 	 $this->http_buffer[] = $output;
 101  	 	 	 }
 102  	 	 	 curl_close($ch);
 103  	 	 	 return $output;
 104  	 	 }
 105  
 106  		public function __buildBody($sub=null) {
 107  	 	 	 $xml = new SimpleXMLElement("<body xmlns='http://jabber.org/protocol/httpbind' xmlns:xmpp='urn:xmpp:xbosh' />");
 108  	 	 	 $xml->addAttribute('content', 'text/xml; charset=utf-8');
 109  	 	 	 $xml->addAttribute('rid', $this->rid);
 110  	 	 	 $this->rid += 1;
 111  	 	 	 if($this->sid) $xml->addAttribute('sid', $this->sid);
 112  	 	 	 #if($this->sid) $xml->addAttribute('xmlns', 'http://jabber.org/protocol/httpbind');
 113  	 	 	 $xml->addAttribute('xml:lang', 'en');
 114  	 	 	 if($sub) { // ok, so simplexml is lame
 115  	 	 	 	 $p = dom_import_simplexml($xml);
 116  	 	 	 	 $c = dom_import_simplexml($sub);
 117  	 	 	 	 $cn = $p->ownerDocument->importNode($c, true);
 118  	 	 	 	 $p->appendChild($cn);
 119  	 	 	 	 $xml = simplexml_import_dom($p);
 120  	 	 	 }
 121  	 	 	 return $xml;
 122  	 	 }
 123  
 124  		public function __process() {
 125  	 	 	 if($this->http_buffer) {
 126  	 	 	 	 $this->__parseBuffer();
 127  	 	 	 } else {
 128  	 	 	 	 $this->__sendBody();
 129  	 	 	 	 $this->__parseBuffer();
 130  	 	 	 }
 131  	 	 }
 132  
 133  		public function __parseBuffer() {
 134  	 	 	 while ($this->http_buffer) {
 135  	 	 	 	 $idx = key($this->http_buffer);
 136  	 	 	 	 $buffer = $this->http_buffer[$idx];
 137  	 	 	 	 unset($this->http_buffer[$idx]);
 138  	 	 	 	 if($buffer) {
 139  	 	 	 	 	 $xml = new SimpleXMLElement($buffer);
 140  	 	 	 	 	 $children = $xml->xpath('child::node()');
 141  	 	 	 	 	 foreach ($children as $child) {
 142  	 	 	 	 	 	 $buff = $child->asXML();
 143  	 	 	 	 	 	 $this->log->log("RECV: $buff",  XMPPHP_Log::LEVEL_VERBOSE);
 144  	 	 	 	 	 	 xml_parse($this->parser, $buff, false);
 145  	 	 	 	 	 }
 146  	 	 	 	 }
 147  	 	 	 }
 148  	 	 }
 149  
 150  		public function send($msg) {
 151  	 	 	 $this->log->log("SEND: $msg",  XMPPHP_Log::LEVEL_VERBOSE);
 152  	 	 	 $msg = new SimpleXMLElement($msg);
 153  	 	 	 #$msg->addAttribute('xmlns', 'jabber:client');
 154  	 	 	 $this->__sendBody($this->__buildBody($msg), true);
 155  	 	 	 #$this->__parseBuffer();
 156  	 	 }
 157  
 158  		public function reset() {
 159  	 	 	 $this->xml_depth = 0;
 160  	 	 	 unset($this->xmlobj);
 161  	 	 	 $this->xmlobj = array();
 162  	 	 	 $this->setupParser();
 163  	 	 	 #$this->send($this->stream_start);
 164  	 	 	 $body = $this->__buildBody();
 165  	 	 	 $body->addAttribute('to', $this->host);
 166  	 	 	 $body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh');
 167  	 	 	 $buff = "<stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
 168  	 	 	 $response = $this->__sendBody($body);
 169  	 	 	 $this->been_reset = true;
 170  	 	 	 xml_parse($this->parser, $buff, false);
 171  	 	 }
 172  
 173  		public function loadSession() {
 174  	 	 	 if(isset($_SESSION['XMPPHP_BOSH_RID'])) $this->rid = $_SESSION['XMPPHP_BOSH_RID'];
 175  	 	 	 if(isset($_SESSION['XMPPHP_BOSH_SID'])) $this->sid = $_SESSION['XMPPHP_BOSH_SID'];
 176  	 	 	 if(isset($_SESSION['XMPPHP_BOSH_authed'])) $this->authed = $_SESSION['XMPPHP_BOSH_authed'];
 177  	 	 	 if(isset($_SESSION['XMPPHP_BOSH_jid'])) $this->jid = $_SESSION['XMPPHP_BOSH_jid'];
 178  	 	 	 if(isset($_SESSION['XMPPHP_BOSH_fulljid'])) $this->fulljid = $_SESSION['XMPPHP_BOSH_fulljid'];
 179  	 	 }
 180  
 181  		public function saveSession() {
 182  	 	 	 $_SESSION['XMPPHP_BOSH_RID'] = (string) $this->rid;
 183  	 	 	 $_SESSION['XMPPHP_BOSH_SID'] = (string) $this->sid;
 184  	 	 	 $_SESSION['XMPPHP_BOSH_authed'] = (boolean) $this->authed;
 185  	 	 	 $_SESSION['XMPPHP_BOSH_jid'] = (string) $this->jid;
 186  	 	 	 $_SESSION['XMPPHP_BOSH_fulljid'] = (string) $this->fulljid;
 187  	 	 }
 188  }