open('tcp://' . $SERVER . ':' . $PORT, 1, 1)) ) throw new Exception('[connect] #2 can\'t access tcp://' . $SERVER . ':' . $PORT . '. File: ' . __FILE__ . ' :: ' . __LINE__); $this->_socket = $socket; $xml = ""; $xml .= "write($xml) === false ) throw new Exception('[connect] #3 it isn\'t possible write in the socket ' . $socket . '. File: ' . __FILE__ . ' :: ' . __LINE__); $this->_server = $SERVER; $this->_username = $USER; $this->_password = $pPassword; $this->_resource = $RESOURCE; } catch(Exception $e) { $this->writeLog('ERROR', $e->getMessage()); return false; } } protected function connect( $pUser = false, $pPassword = false ) { try { $this->_connect($pUser, $pPassword); 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 true; } 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__); if ( ($xml = $this->read()) === false ) throw new Exception('[starttls] #2 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); if ( !preg_match('//', $xml, $matches) ) throw new Exception('[starttls] #3 can\'t start tls in the socket ' .$this->_socket . '. File: ' . __FILE__ . ' :: ' . __LINE__); stream_socket_enable_crypto($this->_socket, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); $xml = ""; $xml .= "write($xml) === false ) throw new Exception('[starttls] #4 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__); usleep(90000); $this->blocking($this->_socket, 0); if ( ($xml = $this->read()) === false ) throw new Exception('[starttls] #5 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); if ( !$this->_plain() ) throw new Exception('[starttls] #6 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 = "" . $this->_username . ""; $xml .= "" . $this->_password . ""; $xml .= "" . $this->_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 = "" : "/>"; if ( !($pStatus || $pShow || $pPriority) ) $xml .= "/>"; else { $xml .= ">"; $xml .= ($pStatus) ? "" . $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)); } 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); } } } ?>