<?php
/**
* XMPPHP: The PHP XMPP Library
* Copyright (C) 2008 Nathanael C. Fritz
* This file is part of SleekXMPP.
*
* 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.
*
* 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.
*
* 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
*
* @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>
* @copyright 2008 Nathanael C. Fritz
*/
/** XMPPHP_Exception */
require_once 'Exception.php';
/** XMPPHP_XMLObj */
require_once 'XMLObj.php';
/** XMPPHP_Log */
require_once 'Log.php';
/**
* XMPPHP XML Stream
*
* @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>
* @copyright 2008 Nathanael C. Fritz
* @version $Id$
*/
class XMPPHP_XMLStream {
/**
* @var resource
*/
protected $socket;
/**
* @var resource
*/
protected $parser;
/**
* @var string
*/
protected $buffer;
/**
* @var integer
*/
protected $xml_depth = 0;
/**
* @var string
*/
protected $host;
/**
* @var integer
*/
protected $port;
/**
* @var string
*/
protected $stream_start = '<stream>';
/**
* @var string
*/
protected $stream_end = '</stream>';
/**
* @var boolean
*/
protected $disconnected = false;
/**
* @var boolean
*/
protected $sent_disconnect = false;
/**
* @var array
*/
protected $ns_map = array();
/**
* @var array
*/
protected $current_ns = array();
/**
* @var array
*/
protected $xmlobj = null;
/**
* @var array
*/
protected $nshandlers = array();
/**
* @var array
*/
protected $xpathhandlers = array();
/**
* @var array
*/
protected $idhandlers = array();
/**
* @var array
*/
protected $eventhandlers = array();
/**
* @var integer
*/
protected $lastid = 0;
/**
* @var string
*/
protected $default_ns;
/**
* @var string
*/
protected $until = '';
/**
* @var string
*/
protected $until_count = '';
/**
* @var array
*/
protected $until_happened = false;
/**
* @var array
*/
protected $until_payload = array();
/**
* @var XMPPHP_Log
*/
protected $log;
/**
* @var boolean
*/
protected $reconnect = true;
/**
* @var boolean
*/
protected $been_reset = false;
/**
* @var boolean
*/
protected $is_server;
/**
* @var float
*/
protected $last_send = 0;
/**
* @var boolean
*/
protected $use_ssl = false;
/**
* @var integer
*/
protected $reconnectTimeout = 30;
/**
* Constructor
*
* @param string $host
* @param string $port
* @param boolean $printlog
* @param string $loglevel
* @param boolean $is_server
*/
public function __construct($host = null, $port = null, $printlog = false, $loglevel = null, $is_server = false) {
$this->reconnect = !$is_server;
$this->is_server = $is_server;
$this->host = $host;
$this->port = $port;
$this->setupParser();
$this->log = new XMPPHP_Log($printlog, $loglevel);
}
/**
* Destructor
* Cleanup connection
*/
public function __destruct() {
if(!$this->disconnected && $this->socket) {
$this->disconnect();
}
}
/**
* Return the log instance
*
* @return XMPPHP_Log
*/
public function getLog() {
return $this->log;
}
/**
* Get next ID
*
* @return integer
*/
public function getId() {
$this->lastid++;
return $this->lastid;
}
/**
* Set SSL
*
* @return integer
*/
public function useSSL($use=true) {
$this->use_ssl = $use;
}
/**
* Add ID Handler
*
* @param integer $id
* @param string $pointer
* @param string $obj
*/
public function addIdHandler($id, $pointer, $obj = null) {
$this->idhandlers[$id] = array($pointer, $obj);
}
/**
* Add Handler
*
* @param string $name
* @param string $ns
* @param string $pointer
* @param string $obj
* @param integer $depth
*/
public function addHandler($name, $ns, $pointer, $obj = null, $depth = 1) {
#TODO deprication warning
$this->nshandlers[] = array($name,$ns,$pointer,$obj, $depth);
}
/**
* Add XPath Handler
*
* @param string $xpath
* @param string $pointer
* @param
*/
public function addXPathHandler($xpath, $pointer, $obj = null) {
if (preg_match_all("/\(?{[^\}]+}\)?(\/?)[^\/]+/", $xpath, $regs)) {
$ns_tags = $regs[0];
} else {
$ns_tags = array($xpath);
}
foreach($ns_tags as $ns_tag) {
list($l, $r) = split("}", $ns_tag);
if ($r != null) {
$xpart = array(substr($l, 1), $r);
} else {
$xpart = array(null, $l);
}
$xpath_array[] = $xpart;
}
$this->xpathhandlers[] = array($xpath_array, $pointer, $obj);
}
/**
* Add Event Handler
*
* @param integer $id
* @param string $pointer
* @param string $obj
*/
public function addEventHandler($name, $pointer, $obj) {
$this->eventhandlers[] = array($name, $pointer, $obj);
}
/**
* Connect to XMPP Host
*
* @param integer $timeout
* @param boolean $persistent
* @param boolean $sendinit
*/
public function connect($timeout = 30, $persistent = false, $sendinit = true) {
$this->sent_disconnect = false;
$starttime = time();
do {
$this->disconnected = false;
$this->sent_disconnect = false;
if($persistent) {
$conflag = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
} else {
$conflag = STREAM_CLIENT_CONNECT;
}
$conntype = 'tcp';
if($this->use_ssl) $conntype = 'ssl';
$this->log->log("Connecting to $conntype://{$this->host}:{$this->port}");
try {
$this->socket = @stream_socket_client("$conntype://{$this->host}:{$this->port}", $errno, $errstr, $timeout, $conflag);
} catch (Exception $e) {
throw new XMPPHP_Exception($e->getMessage());
}
if(!$this->socket) {
$this->log->log("Could not connect.", XMPPHP_Log::LEVEL_ERROR);
$this->disconnected = true;
# Take it easy for a few seconds
sleep(min($timeout, 5));
}
} while (!$this->socket && (time() - $starttime) < $timeout);
if ($this->socket) {
stream_set_blocking($this->socket, 1);
if($sendinit) $this->send($this->stream_start);
} else {
throw new XMPPHP_Exception("Could not connect before timeout.");
}
}
/**
* Reconnect XMPP Host
*/
public function doReconnect() {
if(!$this->is_server) {
$this->log->log("Reconnecting ($this->reconnectTimeout)...", XMPPHP_Log::LEVEL_WARNING);
$this->connect($this->reconnectTimeout, false, false);
$this->reset();
$this->event('reconnect');
}
}
public function setReconnectTimeout($timeout) {
$this->reconnectTimeout = $timeout;
}
/**
* Disconnect from XMPP Host
*/
public function disconnect() {
$this->log->log("Disconnecting...", XMPPHP_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;
}
/**
* Are we are disconnected?
*
* @return boolean
*/
public function isDisconnected() {
return $this->disconnected;
}
/**
* Core reading tool
* 0 -> only read if data is immediately ready
* NULL -> wait forever and ever
* integer -> process for this amount of time
*/
private function __process($maximum=0) {
$remaining = $maximum;
do {
$st
- 1
- 2
前往页