2008-08-22 14:17:14 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XMPPHP: The PHP XMPP Library
|
|
|
|
* Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
* This file is part of SleekXMPP.
|
2019-04-21 01:23:50 +01:00
|
|
|
*
|
2008-08-22 14:17:14 +01: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 01:23:50 +01:00
|
|
|
*
|
2008-08-22 14:17:14 +01: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 01:23:50 +01:00
|
|
|
*
|
2008-08-22 14:17:14 +01: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 01:23:50 +01: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-08-22 14:17:14 +01:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
*/
|
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
namespace XMPPHP;
|
|
|
|
|
|
|
|
/** Exception */
|
2019-04-19 15:25:01 +01:00
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Exception.php';
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/** XMLObj */
|
2019-04-19 15:25:01 +01:00
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'XMLObj.php';
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/** Log */
|
2019-04-19 21:16:42 +01:00
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Log.php';
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
2019-04-21 01:23:50 +01:00
|
|
|
* XMPPHP XMLStream
|
|
|
|
*
|
|
|
|
* @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-08-22 14:17:14 +01:00
|
|
|
*/
|
2019-04-21 01:23:50 +01:00
|
|
|
class XMLStream
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
protected $socket;
|
|
|
|
/**
|
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
protected $parser;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $buffer;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var int
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $xml_depth = 0;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $host;
|
|
|
|
/**
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $port;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $stream_start = '<stream>';
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $stream_end = '</stream>';
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $disconnected = false;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $sent_disconnect = false;
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $ns_map = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $current_ns = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $xmlobj = null;
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $nshandlers = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $xpathhandlers = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $idhandlers = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $eventhandlers = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var int
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $lastid = 0;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $default_ns;
|
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $until = [];
|
|
|
|
/**
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
protected $until_count = [];
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $until_happened = false;
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $until_payload = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* @var Log
|
|
|
|
*/
|
|
|
|
protected $log;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $reconnect = true;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $been_reset = false;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $is_server;
|
|
|
|
/**
|
|
|
|
* @var float
|
|
|
|
*/
|
|
|
|
protected $last_send = 0;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $use_ssl = false;
|
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var int
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $reconnectTimeout = 30;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param string|null $host (optional)
|
|
|
|
* @param string|null $port (optional)
|
|
|
|
* @param bool $print_log (optional)
|
|
|
|
* @param string $log_level (optional)
|
|
|
|
* @param bool $is_server (optional)
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
?string $host = null,
|
|
|
|
?string $port = null,
|
|
|
|
bool $print_log = false,
|
|
|
|
?string $log_level = null,
|
|
|
|
bool $is_server = false
|
|
|
|
) {
|
2019-04-21 01:23:50 +01:00
|
|
|
$this->reconnect = !$is_server;
|
|
|
|
$this->is_server = $is_server;
|
|
|
|
$this->host = $host;
|
|
|
|
$this->port = $port;
|
|
|
|
$this->setupParser();
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->log = new Log($print_log, $log_level);
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the XML parser
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function setupParser(): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$this->parser = xml_parser_create('UTF-8');
|
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
|
|
|
|
xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
|
|
|
|
xml_set_object($this->parser, $this);
|
|
|
|
xml_set_element_handler($this->parser, 'startXML', 'endXML');
|
|
|
|
xml_set_character_data_handler($this->parser, 'charXML');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
* Cleanup connection
|
2019-11-16 16:51:01 +00:00
|
|
|
* @throws Exception
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
if (!$this->disconnected && $this->socket) {
|
|
|
|
$this->disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from XMPP Host
|
2019-11-16 16:51:01 +00:00
|
|
|
* @throws Exception
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function disconnect(): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$this->log->log("Disconnecting...", Log::LEVEL_VERBOSE);
|
|
|
|
if (false == (bool)$this->socket) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->reconnect = false;
|
|
|
|
$this->send($this->stream_end);
|
|
|
|
$this->sent_disconnect = true;
|
|
|
|
$this->processUntil('end_stream', 5);
|
|
|
|
$this->disconnected = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send to socket
|
|
|
|
*
|
|
|
|
* @param string $msg
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param int|null $timeout
|
2019-04-21 01:23:50 +01:00
|
|
|
* @return bool|int
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function send(string $msg, ?int $timeout = null)
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
if (is_null($timeout)) {
|
2019-11-16 16:51:01 +00:00
|
|
|
$secs = null;
|
|
|
|
$usecs = null;
|
|
|
|
} elseif ($timeout == 0) {
|
2019-04-21 01:23:50 +01:00
|
|
|
$secs = 0;
|
|
|
|
$usecs = 0;
|
|
|
|
} else {
|
|
|
|
$maximum = $timeout * 1000000;
|
|
|
|
$usecs = $maximum % 1000000;
|
|
|
|
$secs = floor(($maximum - $usecs) / 1000000);
|
|
|
|
}
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
$read = [];
|
|
|
|
$write = [$this->socket];
|
|
|
|
$except = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
$select = @stream_select($read, $write, $except, $secs, $usecs);
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
if ($select === false) {
|
2019-04-21 01:23:50 +01:00
|
|
|
$this->log->log("ERROR sending message; reconnecting.");
|
|
|
|
$this->doReconnect();
|
2019-11-16 16:51:01 +00:00
|
|
|
// TODO: retry send here
|
2019-04-21 01:23:50 +01:00
|
|
|
return false;
|
|
|
|
} elseif ($select > 0) {
|
|
|
|
$this->log->log("Socket is ready; send it.", Log::LEVEL_VERBOSE);
|
|
|
|
} else {
|
|
|
|
$this->log->log("Socket is not ready; break.", Log::LEVEL_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sentbytes = @fwrite($this->socket, $msg);
|
|
|
|
$this->log->log("SENT: " . mb_substr($msg, 0, $sentbytes, '8bit'), Log::LEVEL_VERBOSE);
|
2019-11-16 16:51:01 +00:00
|
|
|
if ($sentbytes === false) {
|
2019-04-21 01:23:50 +01:00
|
|
|
$this->log->log("ERROR sending message; reconnecting.", Log::LEVEL_ERROR);
|
|
|
|
$this->doReconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->log->log("Successfully sent $sentbytes bytes.", Log::LEVEL_VERBOSE);
|
|
|
|
return $sentbytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reconnect XMPP Host
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function doReconnect()
|
|
|
|
{
|
|
|
|
if (!$this->is_server) {
|
|
|
|
$this->log->log("Reconnecting ($this->reconnectTimeout)...", Log::LEVEL_WARNING);
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->connect(false, false, $this->reconnectTimeout);
|
2019-04-21 01:23:50 +01:00
|
|
|
$this->reset();
|
|
|
|
$this->event('reconnect');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to XMPP Host
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param bool $persistent (optional)
|
|
|
|
* @param bool $send_init (optional)
|
|
|
|
* @param int $timeout (optional)
|
2019-04-21 01:23:50 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function connect(bool $persistent = false, bool $send_init = true, int $timeout = 30): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$this->sent_disconnect = false;
|
2019-11-16 16:51:01 +00:00
|
|
|
$start_time = time();
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
do {
|
|
|
|
$this->disconnected = false;
|
|
|
|
$this->sent_disconnect = false;
|
|
|
|
if ($persistent) {
|
|
|
|
$conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
|
|
|
|
} else {
|
|
|
|
$conflag = STREAM_CLIENT_CONNECT;
|
|
|
|
}
|
2019-11-16 16:51:01 +00:00
|
|
|
$conn_type = 'tcp';
|
|
|
|
if ($this->use_ssl) {
|
|
|
|
$conn_type = 'ssl';
|
|
|
|
}
|
|
|
|
$this->log->log("Connecting to $conn_type://{$this->host}:{$this->port}");
|
|
|
|
$this->socket = @stream_socket_client("$conn_type://{$this->host}:{$this->port}", $errno, $errstr, $timeout, $conflag);
|
2019-04-21 01:23:50 +01:00
|
|
|
if (!$this->socket) {
|
|
|
|
$this->log->log("Could not connect.", Log::LEVEL_ERROR);
|
|
|
|
$this->disconnected = true;
|
|
|
|
# Take it easy for a few seconds
|
|
|
|
sleep(min($timeout, 5));
|
|
|
|
}
|
2019-11-16 16:51:01 +00:00
|
|
|
} while (!$this->socket && (time() - $start_time) < $timeout);
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
if ($this->socket) {
|
|
|
|
stream_set_blocking($this->socket, 1);
|
2019-11-16 16:51:01 +00:00
|
|
|
if ($send_init) {
|
|
|
|
$this->send($this->stream_start);
|
|
|
|
}
|
2019-04-21 01:23:50 +01:00
|
|
|
} else {
|
|
|
|
throw new Exception("Could not connect before timeout.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset connection
|
2019-11-16 16:51:01 +00:00
|
|
|
* @throws Exception
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function reset(): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$this->xml_depth = 0;
|
|
|
|
unset($this->xmlobj);
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->xmlobj = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
$this->setupParser();
|
|
|
|
if (!$this->is_server) {
|
|
|
|
$this->send($this->stream_start);
|
|
|
|
}
|
|
|
|
$this->been_reset = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event?
|
|
|
|
*
|
|
|
|
* @param string $name
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param array|null $payload
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function event(string $name, ?array $payload = null): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2020-09-26 22:16:08 +01:00
|
|
|
$this->log->log("EVENT: {$name}", Log::LEVEL_DEBUG);
|
2019-04-21 01:23:50 +01:00
|
|
|
foreach ($this->eventhandlers as $handler) {
|
2020-09-26 22:16:08 +01:00
|
|
|
if ($handler[0] === $name) {
|
|
|
|
call_user_func_array($handler[1], [&$payload]);
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($this->until as $key => $until) {
|
|
|
|
if (is_array($until)) {
|
|
|
|
if (in_array($name, $until)) {
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->until_payload[$key][] = [$name, $payload];
|
2019-04-21 01:23:50 +01:00
|
|
|
if (!isset($this->until_count[$key])) {
|
|
|
|
$this->until_count[$key] = 0;
|
|
|
|
}
|
|
|
|
$this->until_count[$key] += 1;
|
2019-11-16 16:51:01 +00:00
|
|
|
//$this->until[$key] = false;
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process until a specified event or a timeout occurs
|
|
|
|
*
|
|
|
|
* @param string|array $event
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param int $timeout (optional)
|
|
|
|
* @return array
|
2019-04-21 01:23:50 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function processUntil($event, int $timeout = -1): array
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$start = time();
|
2019-11-16 16:51:01 +00:00
|
|
|
if (!is_array($event)) {
|
|
|
|
$event = array($event);
|
|
|
|
}
|
2019-04-21 01:23:50 +01:00
|
|
|
$this->until[] = $event;
|
|
|
|
end($this->until);
|
|
|
|
$event_key = key($this->until);
|
|
|
|
reset($this->until);
|
|
|
|
$this->until_count[$event_key] = 0;
|
|
|
|
while (!$this->disconnected and $this->until_count[$event_key] < 1 and (time() - $start < $timeout or $timeout == -1)) {
|
|
|
|
$this->__process();
|
|
|
|
}
|
|
|
|
if (array_key_exists($event_key, $this->until_payload)) {
|
|
|
|
$payload = $this->until_payload[$event_key];
|
|
|
|
unset($this->until_payload[$event_key]);
|
|
|
|
unset($this->until_count[$event_key]);
|
|
|
|
unset($this->until[$event_key]);
|
|
|
|
} else {
|
2019-11-16 16:51:01 +00:00
|
|
|
$payload = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
return $payload;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core reading tool
|
|
|
|
* 0 -> only read if data is immediately ready
|
|
|
|
* NULL -> wait forever and ever
|
|
|
|
* integer -> process for this amount of time
|
|
|
|
* @param int $maximum
|
|
|
|
* @return bool
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
private function __process(int $maximum = 5): bool
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$remaining = $maximum;
|
|
|
|
|
|
|
|
do {
|
|
|
|
$starttime = (microtime(true) * 1000000);
|
|
|
|
$read = array($this->socket);
|
2019-11-16 16:51:01 +00:00
|
|
|
$write = [];
|
|
|
|
$except = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
if (is_null($maximum)) {
|
2019-11-16 16:51:01 +00:00
|
|
|
$secs = null;
|
|
|
|
$usecs = null;
|
|
|
|
} elseif ($maximum == 0) {
|
2019-04-21 01:23:50 +01:00
|
|
|
$secs = 0;
|
|
|
|
$usecs = 0;
|
|
|
|
} else {
|
|
|
|
$usecs = $remaining % 1000000;
|
|
|
|
$secs = floor(($remaining - $usecs) / 1000000);
|
|
|
|
}
|
|
|
|
$updated = @stream_select($read, $write, $except, $secs, $usecs);
|
|
|
|
if ($updated === false) {
|
|
|
|
$this->log->log("Error on stream_select()", Log::LEVEL_VERBOSE);
|
|
|
|
if ($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->socket = null;
|
2019-04-21 01:23:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-16 16:51:01 +00:00
|
|
|
} elseif ($updated > 0) {
|
2019-04-21 01:23:50 +01:00
|
|
|
# XXX: Is this big enough?
|
|
|
|
$buff = @fread($this->socket, 4096);
|
|
|
|
if (!$buff) {
|
|
|
|
if ($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->socket = null;
|
2019-04-21 01:23:50 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->log->log("RECV: $buff", Log::LEVEL_VERBOSE);
|
|
|
|
xml_parse($this->parser, $buff, false);
|
|
|
|
} // Otherwise,
|
2019-11-16 16:51:01 +00:00
|
|
|
// $updated == 0 means no changes during timeout.
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
$endtime = (microtime(true) * 1000000);
|
|
|
|
$time_past = $endtime - $starttime;
|
|
|
|
$remaining = $remaining - $time_past;
|
|
|
|
} while (is_null($maximum) || $remaining > 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the log instance
|
|
|
|
*
|
|
|
|
* @return Log
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function getLog(): Log
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
return $this->log;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get next ID
|
|
|
|
*
|
2020-09-26 22:16:08 +01:00
|
|
|
* @return string
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2020-09-26 22:16:08 +01:00
|
|
|
public function getId(): string
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2020-09-26 22:16:08 +01:00
|
|
|
++$this->lastid;
|
|
|
|
return (string) $this->lastid;
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set SSL
|
|
|
|
* @param bool $use
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function useSSL(bool $use = true): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$this->use_ssl = $use;
|
|
|
|
}
|
|
|
|
|
2020-09-26 22:16:08 +01:00
|
|
|
/**
|
|
|
|
* Compose a proper callable if given legacy syntax
|
|
|
|
*
|
|
|
|
* @param callable|string $pointer
|
|
|
|
* @param object|null|bool $obj
|
|
|
|
* @return callable
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function ensureHandler($pointer, $obj = false): callable
|
|
|
|
{
|
|
|
|
$handler = $pointer;
|
|
|
|
|
|
|
|
if (is_string($pointer)) {
|
|
|
|
if (is_object($obj)) {
|
|
|
|
$handler = [$obj, $pointer];
|
|
|
|
} elseif (is_null($obj)) {
|
|
|
|
// Questionable behaviour for backwards compatibility
|
|
|
|
$handler = [$this, $pointer];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_callable($handler)) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Cannot compose a proper callable'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* Add ID Handler
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param int $id
|
2020-09-26 22:16:08 +01:00
|
|
|
* @param callable|string $pointer
|
|
|
|
* @param object|bool|null $obj
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2020-09-26 22:16:08 +01:00
|
|
|
public function addIdHandler(string $id, $pointer, $obj = null): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2020-09-26 22:16:08 +01:00
|
|
|
$this->idhandlers[$id] = [$this->ensureHandler($pointer, $obj)];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Handler
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $ns
|
|
|
|
* @param string $pointer
|
2020-09-26 22:16:08 +01:00
|
|
|
* @param object|bool|null $obj
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param int $depth
|
|
|
|
*
|
2020-09-26 22:16:08 +01:00
|
|
|
* public function addHandler(string $name, string $ns, $pointer, $obj = null, int $depth = 1): void
|
2019-11-16 16:51:01 +00:00
|
|
|
* {
|
2020-09-26 22:16:08 +01:00
|
|
|
* // TODO deprecation warning
|
|
|
|
* $this->nshandlers[] = [$name, $ns, $this->ensureHandler($pointer, $obj), $depth];
|
2019-11-16 16:51:01 +00:00
|
|
|
* }*/
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add XPath Handler
|
|
|
|
*
|
|
|
|
* @param string $xpath
|
2020-09-26 22:16:08 +01:00
|
|
|
* @param callable|string $pointer
|
|
|
|
* @param object|bool|null $obj
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2020-09-26 22:16:08 +01:00
|
|
|
public function addXPathHandler(string $xpath, $pointer, $obj = null): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2020-09-26 22:16:08 +01:00
|
|
|
if (preg_match_all('/\/?(\{[^\}]+\})?[^\/]+/', $xpath, $regs)) {
|
|
|
|
$tag = $regs[0];
|
2019-04-21 01:23:50 +01:00
|
|
|
} else {
|
2020-09-26 22:16:08 +01:00
|
|
|
$tag = [$xpath];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
2019-11-16 16:51:01 +00:00
|
|
|
$xpath_array = [];
|
2020-09-26 22:16:08 +01:00
|
|
|
foreach ($tag as $t) {
|
|
|
|
$t = ltrim($t, '/');
|
|
|
|
preg_match('/(\{([^\}]+)\})?(.*)/', $t, $regs);
|
|
|
|
$xpath_array[] = [$regs[2], $regs[3]];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
2020-09-26 22:16:08 +01:00
|
|
|
|
|
|
|
$this->xpathhandlers[] = [$xpath_array, $this->ensureHandler($pointer, $obj)];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Event Handler
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param string $name
|
2020-09-26 22:16:08 +01:00
|
|
|
* @param callable|string $pointer
|
|
|
|
* @param object|bool|null $obj
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2020-09-26 22:16:08 +01:00
|
|
|
public function addEventHandler(string $name, $pointer, $obj = null): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2020-09-26 22:16:08 +01:00
|
|
|
$this->eventhandlers[] = [$name, $this->ensureHandler($pointer, $obj)];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
/**
|
|
|
|
* @param int $timeout
|
|
|
|
*/
|
|
|
|
public function setReconnectTimeout(int $timeout): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$this->reconnectTimeout = $timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Are we are disconnected?
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @return bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function isDisconnected(): bool
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
return $this->disconnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @throws Exception
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function process(): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->__process(null);
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process until a timeout occurs
|
|
|
|
*
|
|
|
|
* @param integer $timeout
|
|
|
|
* @return string
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function processTime($timeout = null): string
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
if (is_null($timeout)) {
|
2019-11-16 16:51:01 +00:00
|
|
|
return $this->__process(null);
|
2019-04-21 01:23:50 +01:00
|
|
|
} else {
|
|
|
|
return $this->__process($timeout * 1000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obsolete?
|
|
|
|
* @param $socket
|
2019-11-16 16:51:01 +00:00
|
|
|
*
|
|
|
|
* public function Xapply_socket($socket)
|
|
|
|
* {
|
|
|
|
* $this->socket = $socket;
|
|
|
|
* }*/
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* XML start callback
|
|
|
|
*
|
|
|
|
* @param resource $parser
|
|
|
|
* @param string $name
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param array $attr
|
2019-04-21 01:23:50 +01:00
|
|
|
* @see xml_set_element_handler
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function startXML($parser, string $name, array $attr): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
if ($this->been_reset) {
|
|
|
|
$this->been_reset = false;
|
|
|
|
$this->xml_depth = 0;
|
|
|
|
}
|
|
|
|
$this->xml_depth++;
|
|
|
|
if (array_key_exists('XMLNS', $attr)) {
|
|
|
|
$this->current_ns[$this->xml_depth] = $attr['XMLNS'];
|
|
|
|
} else {
|
|
|
|
$this->current_ns[$this->xml_depth] = $this->current_ns[$this->xml_depth - 1];
|
2019-11-16 16:51:01 +00:00
|
|
|
if (!$this->current_ns[$this->xml_depth]) {
|
|
|
|
$this->current_ns[$this->xml_depth] = $this->default_ns;
|
|
|
|
}
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
$ns = $this->current_ns[$this->xml_depth];
|
|
|
|
foreach ($attr as $key => $value) {
|
|
|
|
if (strstr($key, ":")) {
|
|
|
|
$key = explode(':', $key);
|
|
|
|
$key = $key[1];
|
|
|
|
$this->ns_map[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strstr($name, ":") === false) {
|
|
|
|
$name = explode(':', $name);
|
|
|
|
$ns = $this->ns_map[$name[0]];
|
|
|
|
$name = $name[1];
|
|
|
|
}
|
|
|
|
$obj = new XMLObj($name, $ns, $attr);
|
|
|
|
if ($this->xml_depth > 1) {
|
|
|
|
$this->xmlobj[$this->xml_depth - 1]->subs[] = $obj;
|
|
|
|
}
|
|
|
|
$this->xmlobj[$this->xml_depth] = $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML end callback
|
|
|
|
*
|
|
|
|
* @param resource $parser
|
|
|
|
* @param string $name
|
|
|
|
* @throws Exception
|
|
|
|
* @see xml_set_element_handler
|
|
|
|
*
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function endXML($parser, string $name): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
#$this->log->log("Ending $name", Log::LEVEL_DEBUG);
|
|
|
|
#print "$name\n";
|
|
|
|
if ($this->been_reset) {
|
|
|
|
$this->been_reset = false;
|
|
|
|
$this->xml_depth = 0;
|
|
|
|
}
|
|
|
|
$this->xml_depth--;
|
|
|
|
if ($this->xml_depth == 1) {
|
|
|
|
#clean-up old objects
|
|
|
|
#$found = false; #FIXME This didn't appear to be in use --Gar
|
2019-11-16 16:51:01 +00:00
|
|
|
$searchxml = null;
|
2019-04-21 01:23:50 +01:00
|
|
|
foreach ($this->xpathhandlers as $handler) {
|
|
|
|
if (is_array($this->xmlobj) && array_key_exists(2, $this->xmlobj)) {
|
|
|
|
$searchxml = $this->xmlobj[2];
|
|
|
|
$nstag = array_shift($handler[0]);
|
|
|
|
if (($nstag[0] == null or $searchxml->ns == $nstag[0]) and ($nstag[1] == "*" or $nstag[1] == $searchxml->name)) {
|
|
|
|
foreach ($handler[0] as $nstag) {
|
|
|
|
if ($searchxml !== null and $searchxml->hasSub($nstag[1], $ns = $nstag[0])) {
|
|
|
|
$searchxml = $searchxml->sub($nstag[1], $ns = $nstag[0]);
|
|
|
|
} else {
|
|
|
|
$searchxml = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-26 22:16:08 +01:00
|
|
|
if (!is_null($searchxml)) {
|
|
|
|
call_user_func_array($handler[1], [&$this->xmlobj[2]]);
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($this->nshandlers as $handler) {
|
|
|
|
if ($handler[4] != 1 and array_key_exists(2, $this->xmlobj) and $this->xmlobj[2]->hasSub($handler[0])) {
|
|
|
|
$searchxml = $this->xmlobj[2]->sub($handler[0]);
|
|
|
|
} elseif (is_array($this->xmlobj) and array_key_exists(2, $this->xmlobj)) {
|
|
|
|
$searchxml = $this->xmlobj[2];
|
|
|
|
}
|
2020-09-26 22:16:08 +01:00
|
|
|
if (
|
|
|
|
!is_null($searchxml)
|
|
|
|
&& $searchxml->name === $handler[0]
|
|
|
|
&& (
|
|
|
|
(!$handler[1] && $searchxml->ns === $this->default_ns)
|
|
|
|
|| $searchxml->ns === $handler[1]
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
call_user_func_array($handler[2], [&$this->xmlobj[2]]);
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($this->idhandlers as $id => $handler) {
|
2020-09-26 22:16:08 +01:00
|
|
|
if (
|
|
|
|
array_key_exists(2, $this->xmlobj)
|
|
|
|
&& array_key_exists('id', $this->xmlobj[2]->attrs)
|
|
|
|
&& $this->xmlobj[2]->attrs['id'] === (string) $id
|
|
|
|
) {
|
|
|
|
call_user_func_array($handler[0], [&$this->xmlobj[2]]);
|
|
|
|
// id handlers are only used once
|
2019-04-21 01:23:50 +01:00
|
|
|
unset($this->idhandlers[$id]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_array($this->xmlobj)) {
|
|
|
|
$this->xmlobj = array_slice($this->xmlobj, 0, 1);
|
|
|
|
if (isset($this->xmlobj[0]) && $this->xmlobj[0] instanceof XMLObj) {
|
|
|
|
$this->xmlobj[0]->subs = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($this->xmlobj[2]);
|
|
|
|
}
|
|
|
|
if ($this->xml_depth == 0 and !$this->been_reset) {
|
|
|
|
if (!$this->disconnected) {
|
|
|
|
if (!$this->sent_disconnect) {
|
|
|
|
$this->send($this->stream_end);
|
|
|
|
}
|
|
|
|
$this->disconnected = true;
|
|
|
|
$this->sent_disconnect = true;
|
|
|
|
fclose($this->socket);
|
|
|
|
if ($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->event('end_stream');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XML character callback
|
|
|
|
* @param resource $parser
|
|
|
|
* @param string $data
|
|
|
|
* @see xml_set_character_data_handler
|
|
|
|
*
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function charXML($parser, string $data): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
if (array_key_exists($this->xml_depth, $this->xmlobj)) {
|
|
|
|
$this->xmlobj[$this->xml_depth]->data .= $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read from socket
|
2019-11-16 16:51:01 +00:00
|
|
|
* @return bool Did read
|
|
|
|
* @throws Exception
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function read(): bool
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$buff = @fread($this->socket, 1024);
|
|
|
|
if (!$buff) {
|
|
|
|
if ($this->reconnect) {
|
|
|
|
$this->doReconnect();
|
|
|
|
} else {
|
|
|
|
fclose($this->socket);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->log->log("RECV: $buff", Log::LEVEL_VERBOSE);
|
|
|
|
xml_parse($this->parser, $buff, false);
|
2019-11-16 16:51:01 +00:00
|
|
|
return true;
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
public function time(): float
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
list($usec, $sec) = explode(" ", microtime());
|
|
|
|
return (float)$sec + (float)$usec;
|
|
|
|
}
|
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
public function readyToProcess(): bool
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$read = array($this->socket);
|
2019-11-16 16:51:01 +00:00
|
|
|
$write = [];
|
|
|
|
$except = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
$updated = @stream_select($read, $write, $except, 0);
|
|
|
|
return (($updated !== false) && ($updated > 0));
|
|
|
|
}
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|