xml = new XML();
$this->charset = 'UTF-8';
$this->jid = substr($pJid, 0, strpos($pJid, '/'));
$this->username = substr($pJid, 0, strpos($pJid, '@'));
$this->password = $pPassword;
$pJid = substr($pJid, strpos($pJid, '@')+1);
$this->resource = substr($pJid, strpos($pJid, '/')+1);
$this->server = substr($pJid, 0, strpos($pJid, '/'));;
$this->port = $pPort ? $pPort : 5222;
if ( $this->open('tcp://'.$this->server.':'.$this->port, 1, 1) )
$this->setConnect();
}
else
{
// LOG ERRO
}
}
function __destruct()
{
}
/*
* funcoes derivadas de outras classes
*/
protected final function xmlize($pXML)
{
return $this->xml->xmlize($pXML);
}
protected final function sendPacket($pPacket)
{
return $this->write($pPacket);
}
/*
* funcoes implementadas nesta classe
*/
/*
* funcoes privadas
*/
private final function _iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false )
{
$xml = "" . $pLoad . "" : "/>";
}
$xml .= "";
return $xml;
}
private final function authPlain()
{
$xml = "" . $this->username . "";
$xml .= "" . $this->password . "";
$xml .= "" . $this->resource . "";
if ( !$this->sendIq('set', 'auth_1', NULL, NULL, "jabber:iq:auth", $xml) )
{
return false;
}
usleep(90000);
}
private function setConnect()
{
$xml = "";
$this->write($xml);
$xml = $this->read();
$xml = $this->xmlize($xml);
if ( @array_key_exists('starttls', $xml['stream:stream']['#']['stream:features']['0']['#']) &&
@array_key_exists('required', $xml['stream:stream']['#']['stream:features']['0']['#']['starttls']['0']['#']) )
$this->setTLS();
else
$this->simple_authentication();
}
private function setTLS()
{
$xml = "";
$this->write($xml);
$xml = $this->read();
$xml = $this->xmlize($xml);
if ( array_key_exists('proceed', $xml) )
{
stream_socket_enable_crypto($this->returnSocket(), TRUE, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
$xml = "";
$this->write($xml);
$xml = $this->read();
$xml = $this->xmlize($xml);
socket_set_blocking($this->returnSocket(), 0);
$this->authPlain();
}
else
{
// LOG ERRO
}
}
private function simple_authentication()
{
socket_set_blocking($this->returnSocket(), 0);
$this->authPlain();
}
/*
* funcoes do pacote
*/
protected final function presence($pType = false, $pTo = false, $pShow = false, $pStatus = false, $pPriority = false)
{
$xml = "" : "/>";
$xml .= ($pStatus) ? " {$pStatus}" : '';
$xml .= ($pShow) ? " {$pShow}" : '';
$xml .= ($pPriority) ? " {$pPriority}" : '';
$xml .= ($pStatus || $pShow || $pPriority) ? "" : '';
if ( $this->sendPacket($xml) )
{
usleep(90000);
return true;
}
else
{
$this->writeLog("ERROR: send_presence() #1");
return false;
}
}
protected final function readSocketFromServer()
{
$retorno = "" . $this->read(true) . "";
$this->array_teste[] = $retorno;
$retorno = $this->xmlize($retorno);
$retorno = $retorno['resposta']['#'];
return $retorno;
}
protected final function sendIq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false )
{
if ( !preg_match("/^(get|set|result|error)$/i", $pType, $matches, PREG_OFFSET_CAPTURE) )
{
$this->writeLog("ERROR: _iq() #2 - type must be 'get', 'set', 'result' or 'error'");
return false;
}
if ( $this->sendPacket($this->_iq($pType, $pId, $pTo, $pFrom, $pXmlns, $pLoad)) )
{
return true;
}
return false;
}
protected final function writeLog($pLog = false)
{
switch ( substr($pLog, 0, strpos($pLog, ':')) )
{
case 'ERROR' :
if ( $this->log_error === true )
{
$log = NULL;
$log .= $pLog . ' :: ';
$log .= $this->jid . ' :: ';
$log .= date('m/d/Y H:i:s');
$log .= "\n";
if ( $fp = fopen ($this->log_error_file, "a+") )
{
fwrite($fp, $log);
fclose($fp);
}
}
// end case 'ERROR'
break;
}
}
}
?>