[55] | 1 | <?php |
---|
| 2 | /* |
---|
[151] | 3 | +---------------------------------------------------------------------------------+ |
---|
| 4 | | | |
---|
| 5 | | @FILE: Jabber.abstract.php | |
---|
| 6 | | @ABSTRACT: Jabber | |
---|
| 7 | | @EXTENDS: Socket (Socket.class.php) | |
---|
| 8 | | | |
---|
| 9 | | @DATE: Wed May,16 2007 08:18:59 | |
---|
| 10 | | @LAST_CHANGE: Wed May,16 2007 09:03:28 | |
---|
| 11 | | | |
---|
| 12 | | @AUTHOR: [NUTS] Rodrigo Souza - rodrigosouzadossantos [at] gmail [dot] com | |
---|
| 13 | | | |
---|
| 14 | | @BRIEF: Abstract class for connect with any Jabber server | |
---|
| 15 | | | |
---|
| 16 | +---------------------------------------------------------------------------------+ |
---|
[55] | 17 | */ |
---|
| 18 | |
---|
| 19 | require_once 'Socket.abstract.php'; |
---|
| 20 | |
---|
| 21 | abstract class Jabber extends Socket |
---|
| 22 | { |
---|
[151] | 23 | const J_FILE = 'Jabber.log'; |
---|
| 24 | const J_ALL = false; |
---|
| 25 | const J_ERROR = true; |
---|
| 26 | const J_WARNING = true; |
---|
| 27 | const J_NOTICE = true; |
---|
[55] | 28 | |
---|
[151] | 29 | const ALL = 'ALL'; |
---|
| 30 | const ERROR = 'ERROR'; |
---|
| 31 | const WARNING = 'WARNING'; |
---|
| 32 | const NOTICE = 'NOTICE'; |
---|
[55] | 33 | |
---|
[164] | 34 | private final function _connect( $pUser = false, $pPassword = false, $pConnectionType = false ) |
---|
[151] | 35 | { |
---|
| 36 | try |
---|
| 37 | { |
---|
| 38 | if ( !preg_match('/^(.+)@(.+)\/(.+):(\d+)$/',$pUser, $matches) ) |
---|
| 39 | throw new Exception('[connect] #1 ' . $pUser . ' must be [USER]@[DOMAIN]/[RESOURCE]:[PORT] and [PORT] is integer. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 40 | |
---|
[151] | 41 | list($subject, $USER, $SERVER, $RESOURCE, $PORT) = $matches; |
---|
[55] | 42 | |
---|
[151] | 43 | if ( !($socket = $this->open('tcp://' . $SERVER . ':' . $PORT, 1, 1)) ) |
---|
| 44 | throw new Exception('[connect] #2 can\'t access tcp://' . $SERVER . ':' . $PORT . '. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 45 | |
---|
[151] | 46 | $this->_socket = $socket; |
---|
[55] | 47 | |
---|
[151] | 48 | $return = fread($socket, 4096); |
---|
| 49 | fwrite($socket, $pUser); |
---|
[64] | 50 | |
---|
[151] | 51 | $return = fread($socket, 4096); |
---|
| 52 | fwrite($socket, $pPassword); |
---|
[55] | 53 | |
---|
[151] | 54 | $return = fread($socket, 4096); |
---|
[164] | 55 | fwrite($socket, $pConnectionType); |
---|
[55] | 56 | |
---|
[164] | 57 | $return = fread($socket, 4096); |
---|
| 58 | |
---|
[151] | 59 | if ( $return == "new" ) |
---|
| 60 | { |
---|
| 61 | $xml = "<?xml version='1.0' encoding='UTF-8'?>"; |
---|
| 62 | $xml .= "<stream:stream to='" . $SERVER . "' xmlns='jabber:client' "; |
---|
| 63 | $xml .= "xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"; |
---|
| 64 | |
---|
| 65 | if ( $this->write($xml) === false ) |
---|
| 66 | throw new Exception('[connect] #4 it isn\'t possible connect in the server becase exists a client connected with same user. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | $this->_server = $SERVER; |
---|
| 70 | $this->_username = $USER; |
---|
| 71 | $this->_password = $pPassword; |
---|
| 72 | $this->_resource = $RESOURCE; |
---|
| 73 | |
---|
| 74 | return $return; |
---|
| 75 | } |
---|
| 76 | catch(Exception $e) |
---|
| 77 | { |
---|
| 78 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 79 | return false; |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
[164] | 83 | protected function connect($pUser = false, $pPassword = false, $pConnectionType = false) |
---|
[151] | 84 | { |
---|
| 85 | try |
---|
| 86 | { |
---|
[164] | 87 | $_connect = $this->_connect($pUser, $pPassword, $pConnectionType); |
---|
| 88 | |
---|
[151] | 89 | if ( !$_connect ) |
---|
| 90 | throw new Exception('[connect] #0. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 91 | |
---|
[151] | 92 | if ( $_connect == "new" ) |
---|
| 93 | { |
---|
| 94 | if ( ($xml = $this->read()) === false ) |
---|
| 95 | throw new Exception('[connect] #1 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 96 | |
---|
[151] | 97 | if ( preg_match('/(<starttls .*<required\/><\/starttls>)/', $xml, $matches) ) |
---|
| 98 | { |
---|
| 99 | if ( !$this->starttls() ) |
---|
| 100 | throw new Exception('[connect] #2 it isn\'t possible start tls. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 101 | |
---|
[151] | 102 | # $this->writeLog('NOTICE', 'Connected TLS'); |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | if ( !$this->_plain() ) |
---|
| 107 | throw new Exception('[connect] #3 it isn\'t possible carry out the verification. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
| 108 | } |
---|
| 109 | } |
---|
[55] | 110 | |
---|
[151] | 111 | return $_connect; |
---|
| 112 | } |
---|
| 113 | catch(Exception $e) |
---|
| 114 | { |
---|
| 115 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 116 | $this->_disconnect(); |
---|
| 117 | return false; |
---|
| 118 | } |
---|
| 119 | } |
---|
[55] | 120 | |
---|
[151] | 121 | protected final function _disconnect() |
---|
| 122 | { |
---|
| 123 | try |
---|
| 124 | { |
---|
| 125 | $xml = "</stream:stream>"; |
---|
[55] | 126 | |
---|
[151] | 127 | if ( $this->write($xml) === false ) |
---|
| 128 | throw new Exception('[disconnect] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 129 | |
---|
[151] | 130 | if ( ($xml = $this->read()) === false ) |
---|
| 131 | throw new Exception('[disconnect] #2 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
| 132 | } |
---|
| 133 | catch(Exception $e) |
---|
| 134 | { |
---|
| 135 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 136 | return false; |
---|
| 137 | } |
---|
| 138 | } |
---|
[55] | 139 | |
---|
[151] | 140 | private final function starttls() |
---|
| 141 | { |
---|
| 142 | try |
---|
| 143 | { |
---|
| 144 | $xml = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"; |
---|
| 145 | if ( $this->write($xml) === false ) |
---|
| 146 | throw new Exception('[starttls] #1 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 147 | |
---|
[151] | 148 | if ( ($xml = $this->read()) === false ) |
---|
| 149 | throw new Exception('[starttls] #2 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 150 | |
---|
[151] | 151 | if ( !preg_match('/<proceed.*\/>/', $xml, $matches) ) |
---|
| 152 | throw new Exception('[starttls] #3 can\'t start tls in the socket ' .$this->_socket . '. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 153 | |
---|
[151] | 154 | stream_socket_enable_crypto($this->_socket, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT); |
---|
[55] | 155 | |
---|
[151] | 156 | $xml = "<?xml version='1.0' encoding='UTF-8'?>"; |
---|
| 157 | $xml .= "<stream:stream to='" . $this->_server . "' xmlns='jabber:client' "; |
---|
| 158 | $xml .= "xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"; |
---|
| 159 | if ( $this->write($xml) === false ) |
---|
| 160 | throw new Exception('[starttls] #4 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 161 | |
---|
[151] | 162 | usleep(90000); |
---|
| 163 | $this->blocking($this->_socket, 0); |
---|
[55] | 164 | |
---|
[151] | 165 | if ( ($xml = $this->read()) === false ) |
---|
| 166 | throw new Exception('[starttls] #5 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 167 | |
---|
[151] | 168 | if ( !$this->_plain() ) |
---|
| 169 | throw new Exception('[starttls] #6 it isn\'t possible carry out the verification. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 170 | |
---|
[151] | 171 | return true; |
---|
| 172 | } |
---|
| 173 | catch(Exception $e) |
---|
| 174 | { |
---|
| 175 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | } |
---|
[55] | 179 | |
---|
[151] | 180 | private final function _plain() |
---|
| 181 | { |
---|
| 182 | try |
---|
| 183 | { |
---|
| 184 | $this->blocking($this->_socket, 0); |
---|
[55] | 185 | |
---|
[151] | 186 | if ( ($xml = $this->read()) === false ) |
---|
| 187 | throw new Exception('[_plain] #1 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 188 | |
---|
[151] | 189 | $xml = "<username>" . $this->_username . "</username>"; |
---|
| 190 | $xml .= "<password>" . $this->_password . "</password>"; |
---|
| 191 | $xml .= "<resource>" . $this->_resource . "</resource>"; |
---|
[55] | 192 | |
---|
[151] | 193 | unset($this->_password); |
---|
[55] | 194 | |
---|
[151] | 195 | if ( !$this->iq('set', 'auth_1', NULL, NULL, "jabber:iq:auth", $xml) ) |
---|
| 196 | throw new Exception('[_plain] #2 it isn\'t possible carry out the verification. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
| 197 | |
---|
| 198 | while ( !preg_match("/<iq xmlns='jabber:client' id='auth_1' type='(result|error)'/", ($readSocket = $this->read()), $matches) ) |
---|
| 199 | usleep(10000); |
---|
| 200 | |
---|
[64] | 201 | if($matches[1] == "error") |
---|
| 202 | return false; |
---|
| 203 | |
---|
[151] | 204 | if ( ($xml = $this->read()) === false ) |
---|
| 205 | throw new Exception('[_plain] #3 it isn\'t possible read the socket. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 206 | |
---|
[151] | 207 | return true; |
---|
| 208 | } |
---|
| 209 | catch(Exception $e) |
---|
| 210 | { |
---|
| 211 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 212 | return false; |
---|
| 213 | } |
---|
| 214 | } |
---|
[55] | 215 | |
---|
| 216 | private final function _iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false ) |
---|
| 217 | { |
---|
| 218 | $xml = "<iq type='" . $pType . "' id='" . $pId . "'"; |
---|
| 219 | $xml .= ( $pTo ) ? " to='" . $pTo . "'" : ""; |
---|
| 220 | $xml .= ( $pFrom ) ? " from='" . $pFrom . "'" : ""; |
---|
| 221 | |
---|
| 222 | if ( $pXmlns == "vcard-temp" ) |
---|
[151] | 223 | { |
---|
[55] | 224 | $xml .= ">"; |
---|
| 225 | $xml .= $pLoad; |
---|
| 226 | } |
---|
[151] | 227 | else |
---|
| 228 | { |
---|
[55] | 229 | $xml .= "><query xmlns='" . $pXmlns . "'"; |
---|
| 230 | $xml .= ( $pLoad ) ? ">" . $pLoad . "</query>" : "/>"; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | $xml .= "</iq>"; |
---|
[151] | 234 | |
---|
| 235 | return $xml; |
---|
[55] | 236 | } |
---|
| 237 | |
---|
[151] | 238 | protected final function iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false) |
---|
| 239 | { |
---|
| 240 | try |
---|
| 241 | { |
---|
| 242 | if ( !preg_match("/^(get|set|result|error)$/i", $pType, $matches) ) |
---|
| 243 | throw new Exception('[iq] #1 type must be GET, SET, RESULT or ERROR. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 244 | |
---|
[151] | 245 | if ( $this->write($this->_iq($pType, $pId, $pTo, $pFrom, $pXmlns, $pLoad)) === false ) |
---|
| 246 | throw new Exception('[iq] #2 Cannot write to socket (' . $this->_socket . '). File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
| 247 | else |
---|
| 248 | return true; |
---|
| 249 | } |
---|
| 250 | catch(Exception $e) |
---|
| 251 | { |
---|
| 252 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 253 | return false; |
---|
| 254 | } |
---|
| 255 | } |
---|
[55] | 256 | |
---|
| 257 | protected final function presence($pType = false, $pTo = false, $pShow = false, $pStatus = false, $pPriority = false) |
---|
| 258 | { |
---|
[151] | 259 | try |
---|
| 260 | { |
---|
| 261 | $xml = "<presence"; |
---|
| 262 | $xml .= ($pTo) ? " to='{$pTo}'" : ''; |
---|
| 263 | $xml .= ($pType) ? " type='{$pType}'" : ''; |
---|
| 264 | |
---|
| 265 | if ( !($pStatus || $pShow || $pPriority) ) |
---|
[55] | 266 | $xml .= "/>"; |
---|
| 267 | else |
---|
| 268 | { |
---|
[151] | 269 | $xml .= ">"; |
---|
| 270 | $xml .= ($pStatus) ? "<status>" . $pStatus . "</status>" : ''; |
---|
| 271 | $xml .= ($pShow) ? "<show>" . $pShow . "</show>" : ''; |
---|
| 272 | $xml .= ($pPriority) ? "<priority>" . $pPriority . "</priority>" : ''; |
---|
| 273 | $xml .= ($pStatus || $pShow || $pPriority) ? "</presence>" : ''; |
---|
[55] | 274 | } |
---|
[164] | 275 | |
---|
[151] | 276 | if ( $this->write($xml) === false ) |
---|
| 277 | throw new Exception('[presence] #1 it isn\'t possible send presence for ' . $this->_server . '. File: ' . __FILE__ . ' :: ' . __LINE__); |
---|
[55] | 278 | |
---|
[151] | 279 | return true; |
---|
| 280 | } |
---|
| 281 | catch(Exception $e) |
---|
| 282 | { |
---|
| 283 | $this->writeLog('ERROR', $e->getMessage()); |
---|
| 284 | return false; |
---|
| 285 | } |
---|
[55] | 286 | } |
---|
| 287 | |
---|
[151] | 288 | protected final function read() |
---|
| 289 | { |
---|
| 290 | $return = NULL; |
---|
| 291 | do |
---|
| 292 | { |
---|
| 293 | $line = NULL; |
---|
| 294 | $line = utf8_decode(parent::read($this->_socket, 4096)); |
---|
[55] | 295 | |
---|
[151] | 296 | if ( $line === false ) |
---|
| 297 | { |
---|
| 298 | $return = false; |
---|
| 299 | break; |
---|
| 300 | } |
---|
[55] | 301 | |
---|
[151] | 302 | if ( $line != NULL ) |
---|
| 303 | $return .= $line;// . "\n"; |
---|
| 304 | } |
---|
| 305 | while ( $line != NULL ); |
---|
[55] | 306 | |
---|
[151] | 307 | return $return; |
---|
| 308 | } |
---|
[55] | 309 | |
---|
| 310 | protected final function write($pData = false) |
---|
[151] | 311 | { |
---|
| 312 | if ( !$pData ) |
---|
| 313 | return false; |
---|
[55] | 314 | |
---|
[151] | 315 | return parent::write($this->_socket, utf8_encode($pData)); |
---|
| 316 | } |
---|
[55] | 317 | |
---|
[151] | 318 | function __destruct() |
---|
| 319 | { |
---|
| 320 | $this->close($this->_socket); |
---|
| 321 | } |
---|
[55] | 322 | |
---|
[151] | 323 | protected final function writeLog($pType = false, $pLog = false) |
---|
| 324 | { |
---|
| 325 | if ( !defined('self::'.$pType) && !$pLog ) |
---|
| 326 | return false; |
---|
[55] | 327 | |
---|
[151] | 328 | if ( !(bool)constant('self::J_'.$pType) && !(bool)constant('self::J_ALL') ) |
---|
| 329 | return false; |
---|
| 330 | |
---|
| 331 | $log = date('m/d/Y H:i:s'); |
---|
| 332 | $log .= ' [' . constant('self::'.$pType) . '] :: '; |
---|
| 333 | $log .= $pLog . "\n"; |
---|
| 334 | if ( $fp = fopen (self::J_FILE, "a+") ) |
---|
| 335 | { |
---|
| 336 | fwrite($fp, $log); |
---|
| 337 | fclose($fp); |
---|
| 338 | } |
---|
| 339 | } |
---|
[55] | 340 | } |
---|
| 341 | ?> |
---|