open('tcp://' . $pWebjabber . ':' . $PORT, 1, 1)) )
throw new Exception('[connect] #2 can\'t access tcp://' . $pWebjabber . ':' . $PORT . '. File: ' . __FILE__ . ' :: ' . __LINE__);
$this->_socket = $socket;
//stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
fwrite($socket, "{$USER}@{$SERVER}##{$pPassword}##{$pConnectionType}");
$return = fread($socket, 3);
if ( $return == "new" )
{
$xml = "";
$xml .= "write($xml) === false )
throw new Exception('[connect] #4 it isn\'t possible connect in the server because exists a client connected with same user. File: ' . __FILE__ . ' :: ' . __LINE__);
}
$this->_server = $SERVER;
$this->_username = $USER;
$this->_password = $pPassword;
$this->_resource = $RESOURCE;
return $return;
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
return false;
}
}
protected function connect($pUser = false, $pPassword = false, $pConnectionType = false, $pWebjabber = false )
{
try
{
$_connect = $this->_connect($pUser, $pPassword, $pConnectionType, $pWebjabber);
if ( !$_connect )
throw new Exception('[connect] #0. File: ' . __FILE__ . ' :: ' . __LINE__);
if ( $_connect == "new" )
{
if ( ($xml = $this->read()) === false )
throw new Exception('[connect] #1 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__);
if ( preg_match('/(<\/starttls>)/', $xml, $matches) )
{
if ( !$this->starttls() )
throw new Exception('[connect] #2 it isn\'t possible start tls. File: ' . __FILE__ . ' :: ' . __LINE__);
# $this->writeLog('NOTICE', 'Connected TLS');
}
else
{
if ( !$this->_plain() )
throw new Exception('[connect] #3 it isn\'t possible carry out the verification. File: ' . __FILE__ . ' :: ' . __LINE__);
}
}
return $_connect;
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
$this->_disconnect();
return false;
}
}
protected final function _disconnect()
{
try
{
$xml = "";
#if ( $this->write($xml) === false )
# throw new Exception('[disconnect] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__);
#if ( ($xml = $this->read()) === false )
# throw new Exception('[disconnect] #2 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__);
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
return false;
}
}
private final function starttls()
{
try
{
$xml = "";
if ( $this->write($xml) === false )
throw new Exception('[starttls] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__);
usleep(50000);
fread($this->_socket, 4096);
stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
$xml = "";
$xml .= "write($xml) === false )
throw new Exception('[starttls] #2 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__);
usleep(50000);
fread($this->_socket, 4096);
usleep(50000);
fread($this->_socket, 4096);
if ( !$this->_plain() )
throw new Exception('[starttls] #3 it isn\'t possible carry out the verification. File: ' . __FILE__ . ' :: ' . __LINE__);
return true;
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
return false;
}
}
private final function _plain()
{
try
{
$this->blocking($this->_socket, 0);
if ( ($xml = $this->read()) === false )
throw new Exception('[_plain] #1 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__);
$xml = "_username . "]]>";
$xml .= "_password . "]]>";
$xml .= "_resource . "]]>";
unset($this->_password);
if ( !$this->iq('set', 'auth_1', NULL, NULL, "jabber:iq:auth", $xml) )
throw new Exception('[_plain] #2 it isn\'t possible carry out the verification. File: ' . __FILE__ . ' :: ' . __LINE__);
while ( !preg_match("/read()), $matches) )
usleep(10000);
if($matches[1] == "error")
return false;
if ( ($xml = $this->read()) === false )
throw new Exception('[_plain] #3 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__);
return true;
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
return false;
}
}
private final function _iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false )
{
$xml = "" . $pLoad . "" : "/>";
}
$xml .= "";
return $xml;
}
protected final function iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false)
{
try
{
if ( !preg_match("/^(get|set|result|error)$/i", $pType, $matches) )
throw new Exception('[iq] #1 type must be GET, SET, RESULT or ERROR. File: ' . __FILE__ . ' :: ' . __LINE__);
if ( $this->write($this->_iq($pType, $pId, $pTo, $pFrom, $pXmlns, $pLoad)) === false )
throw new Exception('[iq] #2 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__);
else
return true;
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
return false;
}
}
protected final function presence($pType = false, $pTo = false, $pShow = false, $pStatus = false, $pPriority = false)
{
try
{
$xml = "" . $pStatus . "" : '';
$xml .= ($pShow) ? "" . $pShow . "" : '';
$xml .= ($pPriority) ? "" . $pPriority . "" : '';
$xml .= ($pStatus || $pShow || $pPriority) ? "" : '';
}
if ( $this->write($xml) === false )
throw new Exception('[presence] #1 it isn\'t possible send presence for ' . $this->_server . '. File: ' . __FILE__ . ' :: ' . __LINE__);
return true;
}
catch(Exception $e)
{
$this->writeLog('ERROR', $e->getMessage());
return false;
}
}
protected final function read()
{
$return = NULL;
do
{
$line = NULL;
$line = utf8_decode(parent::read($this->_socket, 4096));
if ( $line === false )
{
$return = false;
break;
}
if ( $line != NULL )
$return .= $line;// . "\n";
}
while ( $line != NULL );
return $return;
}
protected final function write($pData = false)
{
if ( !$pData )
return false;
return parent::write($this->_socket, utf8_encode($pData));
}
function __destruct()
{
$this->close($this->_socket);
}
protected final function writeLog($pType = false, $pLog = false)
{
if ( !defined('self::'.$pType) && !$pLog )
return false;
if ( !(bool)constant('self::J_'.$pType) && !(bool)constant('self::J_ALL') )
return false;
$log = date('m/d/Y H:i:s');
$log .= ' [' . constant('self::'.$pType) . '] :: ';
$log .= $pLog . "\n";
if ( $fp = fopen (self::J_FILE, "a+") )
{
fwrite($fp, $log);
fclose($fp);
}
}
}
?>