2008-12-12 03:15:00 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XMPPHP: The PHP XMPP Library
|
|
|
|
* Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
* This file is part of SleekXMPP.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-12-12 03:15:00 +09:00
|
|
|
* XMPPHP is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-12-12 03:15:00 +09:00
|
|
|
* XMPPHP is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
2008-12-12 03:15:00 +09:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with XMPPHP; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
2019-04-21 09:23:50 +09:00
|
|
|
* @category xmpphp
|
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
|
|
|
* @author Michael Garvin <JID: gar@netflint.net>
|
|
|
|
* @author Alexander Birkner (https://github.com/BirknerAlex)
|
|
|
|
* @author zorn-v (https://github.com/zorn-v/xmpphp/)
|
|
|
|
* @author GNU social
|
2008-12-12 03:15:00 +09:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
*/
|
|
|
|
|
2019-04-21 09:23:50 +09:00
|
|
|
namespace XMPPHP;
|
|
|
|
|
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2008-12-12 03:15:00 +09:00
|
|
|
/** XMPPHP_XMLStream */
|
2019-11-17 01:51:01 +09:00
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'XMPP.php';
|
2008-12-12 03:15:00 +09:00
|
|
|
|
|
|
|
/**
|
2019-04-21 09:23:50 +09:00
|
|
|
* XMPPHP BOSH
|
|
|
|
*
|
|
|
|
* @property int lat
|
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
|
|
|
* @author Michael Garvin <JID: gar@netflint.net>
|
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
* @version $Id$
|
2008-12-12 03:15:00 +09:00
|
|
|
*/
|
2019-04-21 09:23:50 +09:00
|
|
|
class BOSH extends XMPP
|
|
|
|
{
|
|
|
|
/**
|
2019-11-17 01:51:01 +09:00
|
|
|
* @var int
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
|
|
|
protected $rid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $sid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $http_server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
protected $http_buffer = [];
|
2019-04-21 09:23:50 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $session = false;
|
|
|
|
|
|
|
|
/**
|
2019-11-17 01:51:01 +09:00
|
|
|
* @var int
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
|
|
|
protected $inactivity;
|
|
|
|
|
2019-11-17 01:51:01 +09:00
|
|
|
public function __construct(
|
|
|
|
string $host,
|
|
|
|
int $port,
|
|
|
|
string $user,
|
|
|
|
string $password,
|
|
|
|
string $resource,
|
|
|
|
?string $server = null,
|
|
|
|
bool $print_log = false,
|
|
|
|
?string $log_level = null
|
|
|
|
) {
|
|
|
|
parent::__construct($host, $port, $user, $password, $resource, $server, $print_log, $log_level);
|
2019-04-21 09:23:50 +09:00
|
|
|
if (is_null($server)) {
|
|
|
|
// If we aren't given the server http url, try and guess it
|
2019-11-17 01:51:01 +09:00
|
|
|
$port_string = ($this->port and $this->port != 80) ? ':' . $this->port : '';
|
2019-04-21 09:23:50 +09:00
|
|
|
$this->http_server = 'http://' . $this->host . $port_string . '/http-bind/';
|
|
|
|
} else {
|
|
|
|
$this->http_server = $server;
|
|
|
|
}
|
2019-11-17 01:51:01 +09:00
|
|
|
}
|
2019-04-21 09:23:50 +09:00
|
|
|
|
2019-11-17 01:51:01 +09:00
|
|
|
/**
|
|
|
|
* Connect
|
|
|
|
*
|
|
|
|
* @param bool $persistent
|
|
|
|
* @param bool $send_init
|
|
|
|
* @param int $timeout
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function connect(bool $persistent = false, bool $send_init = true, int $timeout = 30): void
|
|
|
|
{
|
2019-04-21 09:23:50 +09:00
|
|
|
$this->use_encryption = false;
|
2019-11-17 01:51:01 +09:00
|
|
|
$this->session = $persistent;
|
2019-04-21 09:23:50 +09:00
|
|
|
$this->rid = 3001;
|
|
|
|
$this->sid = null;
|
|
|
|
$this->inactivity = 0;
|
|
|
|
|
2019-11-17 01:51:01 +09:00
|
|
|
if ($persistent) {
|
2019-04-21 09:23:50 +09:00
|
|
|
$this->loadSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->sid) {
|
|
|
|
$body = $this->__buildBody();
|
|
|
|
$body->addAttribute('hold', '1');
|
|
|
|
$body->addAttribute('to', $this->server);
|
|
|
|
$body->addAttribute('route', 'xmpp:' . $this->host . ':' . $this->port);
|
|
|
|
$body->addAttribute('secure', 'true');
|
|
|
|
$body->addAttribute('xmpp:version', '1.0', 'urn:xmpp:xbosh');
|
2019-11-17 01:51:01 +09:00
|
|
|
$body->addAttribute('wait', strval($timeout));
|
2019-04-21 09:23:50 +09:00
|
|
|
$body->addAttribute('ack', '1');
|
|
|
|
$body->addAttribute('xmlns:xmpp', 'urn:xmpp:xbosh');
|
|
|
|
$buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
|
|
|
|
xml_parse($this->parser, $buff, false);
|
|
|
|
$response = $this->__sendBody($body);
|
|
|
|
$rxml = new SimpleXMLElement($response);
|
|
|
|
$this->sid = $rxml['sid'];
|
|
|
|
$this->inactivity = $rxml['inactivity'];
|
|
|
|
} else {
|
|
|
|
$buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
|
|
|
|
xml_parse($this->parser, $buff, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load session
|
|
|
|
*
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function loadSession(): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
if ($this->session == 'ON_FILE') {
|
|
|
|
// Session not started so use session_file
|
|
|
|
$session_file = $this->getSessionFile();
|
|
|
|
|
|
|
|
// manage multiple accesses
|
|
|
|
if (!file_exists($session_file)) {
|
|
|
|
file_put_contents($session_file, '');
|
|
|
|
}
|
|
|
|
$session_file_fp = fopen($session_file, 'r');
|
|
|
|
flock($session_file_fp, LOCK_EX);
|
|
|
|
$session_serialized = file_get_contents($session_file, null, null, 6);
|
|
|
|
flock($session_file_fp, LOCK_UN);
|
|
|
|
fclose($session_file_fp);
|
|
|
|
|
|
|
|
$this->log->log('SESSION: reading ' . $session_serialized . ' from ' . $session_file, Log::LEVEL_VERBOSE);
|
|
|
|
if ($session_serialized != '') {
|
|
|
|
$_SESSION['XMPPHP_BOSH'] = unserialize($session_serialized);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($_SESSION['XMPPHP_BOSH']['inactivity'])) {
|
|
|
|
$this->inactivity = $_SESSION['XMPPHP_BOSH']['inactivity'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->lat = (time() - (isset($_SESSION['XMPPHP_BOSH']['lat']))) ? $_SESSION['XMPPHP_BOSH']['lat'] : 0;
|
|
|
|
|
|
|
|
if ($this->lat < $this->inactivity) {
|
|
|
|
if (isset($_SESSION['XMPPHP_BOSH']['RID'])) {
|
|
|
|
$this->rid = $_SESSION['XMPPHP_BOSH']['RID'];
|
|
|
|
}
|
|
|
|
if (isset($_SESSION['XMPPHP_BOSH']['SID'])) {
|
|
|
|
$this->sid = $_SESSION['XMPPHP_BOSH']['SID'];
|
|
|
|
}
|
|
|
|
if (isset($_SESSION['XMPPHP_BOSH']['authed'])) {
|
|
|
|
$this->authed = $_SESSION['XMPPHP_BOSH']['authed'];
|
|
|
|
}
|
|
|
|
if (isset($_SESSION['XMPPHP_BOSH']['basejid'])) {
|
|
|
|
$this->basejid = $_SESSION['XMPPHP_BOSH']['basejid'];
|
|
|
|
}
|
|
|
|
if (isset($_SESSION['XMPPHP_BOSH']['fulljid'])) {
|
|
|
|
$this->fulljid = $_SESSION['XMPPHP_BOSH']['fulljid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-17 01:51:01 +09:00
|
|
|
* Get the session file location
|
2019-04-21 09:23:50 +09:00
|
|
|
*
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function getSessionFile(): string
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
return sys_get_temp_dir() . '/' . $this->user . '_' . $this->server . '_session';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build body
|
|
|
|
*
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param SimpleXMLElement|null $sub
|
|
|
|
* @return SimpleXMLElement
|
2019-04-21 09:23:50 +09:00
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
private function __buildBody(?SimpleXMLElement $sub = null): SimpleXMLElement
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
2019-11-17 01:51:01 +09:00
|
|
|
$xml = new SimpleXMLElement('<body xmlns="http://jabber.org/protocol/httpbind" xmlns:xmpp="urn:xmpp:xbosh" />');
|
2019-04-21 09:23:50 +09:00
|
|
|
$xml->addAttribute('content', 'text/xml; charset=utf-8');
|
|
|
|
$xml->addAttribute('rid', $this->rid);
|
2019-11-17 01:51:01 +09:00
|
|
|
++$this->rid;
|
2019-04-21 09:23:50 +09:00
|
|
|
if ($this->sid) {
|
|
|
|
$xml->addAttribute('sid', $this->sid);
|
|
|
|
}
|
|
|
|
|
|
|
|
$xml->addAttribute('xml:lang', 'en');
|
|
|
|
|
|
|
|
if ($sub !== null) {
|
|
|
|
// Ok, so simplexml is lame
|
|
|
|
$parent = dom_import_simplexml($xml);
|
|
|
|
$content = dom_import_simplexml($sub);
|
|
|
|
$child = $parent->ownerDocument->importNode($content, true);
|
|
|
|
$parent->appendChild($child);
|
|
|
|
$xml = simplexml_import_dom($parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $xml;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send body
|
|
|
|
*
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param SimpleXMLElement|null $body
|
|
|
|
* @param bool $recv
|
2019-04-21 09:23:50 +09:00
|
|
|
* @return bool|string
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
private function __sendBody(?SimpleXMLElement $body = null, bool $recv = true)
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
if (!$body) {
|
|
|
|
$body = $this->__buildBody();
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = '';
|
2019-11-17 01:51:01 +09:00
|
|
|
$header = ['Accept-Encoding: gzip, deflate', 'Content-Type: text/xml; charset=utf-8'];
|
2019-04-21 09:23:50 +09:00
|
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->http_server);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body->asXML());
|
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, 0);
|
|
|
|
|
|
|
|
if ($recv) {
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$output = curl_exec($ch);
|
|
|
|
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
|
|
|
|
throw new Exception('Wrong response from server!');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->http_buffer[] = $output;
|
|
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process
|
|
|
|
*
|
|
|
|
* @param $null1
|
|
|
|
* @param $null2
|
|
|
|
*
|
|
|
|
* null params are not used and just to statify Strict Function Declaration
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
private function __process($null1 = null, $null2 = null)
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
if ($this->http_buffer) {
|
|
|
|
$this->__parseBuffer();
|
|
|
|
} else {
|
|
|
|
$this->__sendBody();
|
|
|
|
$this->__parseBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->saveSession();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-17 01:51:01 +09:00
|
|
|
private function __parseBuffer()
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
while ($this->http_buffer) {
|
|
|
|
$idx = key($this->http_buffer);
|
|
|
|
$buffer = $this->http_buffer[$idx];
|
|
|
|
unset($this->http_buffer[$idx]);
|
|
|
|
|
|
|
|
if ($buffer) {
|
|
|
|
$xml = new SimpleXMLElement($buffer);
|
|
|
|
$children = $xml->xpath('child::node()');
|
|
|
|
|
|
|
|
foreach ($children as $child) {
|
|
|
|
$buff = $child->asXML();
|
|
|
|
$this->log->log('RECV: ' . $buff, Log::LEVEL_VERBOSE);
|
|
|
|
xml_parse($this->parser, $buff, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save session
|
|
|
|
*
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function saveSession(): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$_SESSION['XMPPHP_BOSH']['RID'] = (string)$this->rid;
|
|
|
|
$_SESSION['XMPPHP_BOSH']['SID'] = (string)$this->sid;
|
|
|
|
$_SESSION['XMPPHP_BOSH']['authed'] = (boolean)$this->authed;
|
|
|
|
$_SESSION['XMPPHP_BOSH']['basejid'] = (string)$this->basejid;
|
|
|
|
$_SESSION['XMPPHP_BOSH']['fulljid'] = (string)$this->fulljid;
|
|
|
|
$_SESSION['XMPPHP_BOSH']['inactivity'] = (string)$this->inactivity;
|
|
|
|
$_SESSION['XMPPHP_BOSH']['lat'] = (string)time();
|
|
|
|
|
|
|
|
if ($this->session == 'ON_FILE') {
|
|
|
|
$session_file = $this->getSessionFile();
|
|
|
|
$session_file_fp = fopen($session_file, 'r');
|
|
|
|
flock($session_file_fp, LOCK_EX);
|
|
|
|
// <?php prefix used to mask the content of the session file
|
|
|
|
$session_serialized = '<?php ' . serialize($_SESSION);
|
|
|
|
file_put_contents($session_file, $session_serialized);
|
|
|
|
flock($session_file_fp, LOCK_UN);
|
|
|
|
fclose($session_file_fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process
|
|
|
|
*
|
|
|
|
* @param $msg
|
2019-11-17 01:51:01 +09:00
|
|
|
* @param int|null $_ unused
|
2019-04-21 09:23:50 +09:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function send($msg, ?int $_ = null)
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$this->log->log('SEND: ' . $msg, Log::LEVEL_VERBOSE);
|
|
|
|
$msg = new SimpleXMLElement($msg);
|
|
|
|
$this->__sendBody($this->__buildBody($msg), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function reset(): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
$this->xml_depth = 0;
|
|
|
|
unset($this->xmlobj);
|
2019-11-17 01:51:01 +09:00
|
|
|
$this->xmlobj = [];
|
2019-04-21 09:23:50 +09:00
|
|
|
$this->setupParser();
|
|
|
|
$body = $this->__buildBody();
|
|
|
|
$body->addAttribute('to', $this->host);
|
|
|
|
$body->addAttribute('xmpp:restart', 'true', 'urn:xmpp:xbosh');
|
|
|
|
$buff = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams">';
|
2019-11-17 01:51:01 +09:00
|
|
|
$this->__sendBody($body);
|
2019-04-21 09:23:50 +09:00
|
|
|
$this->been_reset = true;
|
|
|
|
xml_parse($this->parser, $buff, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-17 01:51:01 +09:00
|
|
|
public function disconnect(): void
|
2019-04-21 09:23:50 +09:00
|
|
|
{
|
|
|
|
parent::disconnect();
|
|
|
|
|
|
|
|
if ($this->session == 'ON_FILE') {
|
|
|
|
unlink($this->getSessionFile());
|
|
|
|
} else {
|
2019-11-17 01:51:01 +09:00
|
|
|
$keys = ['RID', 'SID', 'authed', 'basejid', 'fulljid', 'inactivity', 'lat'];
|
2019-04-21 09:23:50 +09:00
|
|
|
foreach ($keys as $key) {
|
|
|
|
unset($_SESSION['XMPPHP_BOSH'][$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-12-12 03:15:00 +09:00
|
|
|
}
|