[20] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | +-------------------------------------------------------------------------+ |
---|
| 4 | | @FILE_NAME: class.Jabber.inc.php | |
---|
| 5 | | @CLASS_NAME: Jabber | |
---|
| 6 | | | |
---|
| 7 | | @AUTHOR: Alexandre Correia - alexandrecorreia@celepar.pr.gov.br | |
---|
| 8 | | [NUTS] Rodrigo Souza - rodsouza@celepar.pr.gov.br | |
---|
| 9 | | | |
---|
| 10 | | @DATE: Seg 18 Dez 2006 10:10:59 BRST | |
---|
| 11 | | @LAST_CHANGE: Qui 21 Dez 2006 09:10:28 BRST:: | |
---|
| 12 | | | |
---|
| 13 | | @BRIEF: Classe para acesso e comunicação ao | |
---|
| 14 | | Servidor Jabberd2 com TLS | |
---|
| 15 | +-------------------------------------------------------------------------+ |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | require_once "class.Socket.inc.php"; |
---|
| 19 | require_once "class.XML.inc.php"; |
---|
| 20 | |
---|
| 21 | class Jabber extends Socket |
---|
| 22 | { |
---|
| 23 | private $charset; |
---|
| 24 | |
---|
| 25 | private $server; |
---|
| 26 | private $port; |
---|
| 27 | |
---|
| 28 | private $username; |
---|
| 29 | private $password; |
---|
| 30 | |
---|
| 31 | private $jid; |
---|
| 32 | private $resource; |
---|
| 33 | |
---|
| 34 | private $log_error = false; |
---|
| 35 | |
---|
| 36 | function __construct($pJid = false, $pPassword = false, $pPort = false) |
---|
| 37 | { |
---|
| 38 | if ( $pJid && $pPassword ) |
---|
| 39 | { |
---|
| 40 | $this->xml = new XML(); |
---|
| 41 | $this->charset = 'UTF-8'; |
---|
| 42 | |
---|
| 43 | $this->jid = substr($pJid, 0, strpos($pJid, '/')); |
---|
| 44 | |
---|
| 45 | $this->username = substr($pJid, 0, strpos($pJid, '@')); |
---|
| 46 | $this->password = $pPassword; |
---|
| 47 | |
---|
| 48 | $pJid = substr($pJid, strpos($pJid, '@')+1); |
---|
| 49 | $this->resource = substr($pJid, strpos($pJid, '/')+1); |
---|
| 50 | |
---|
| 51 | $this->server = substr($pJid, 0, strpos($pJid, '/'));; |
---|
| 52 | $this->port = $pPort ? $pPort : 5222; |
---|
| 53 | |
---|
| 54 | if ( $this->open('tcp://'.$this->server.':'.$this->port, 1, 1) ) |
---|
| 55 | $this->setConnect(); |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | // LOG ERRO |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | function __destruct() |
---|
| 64 | { |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /* |
---|
| 68 | * funcoes derivadas de outras classes |
---|
| 69 | */ |
---|
| 70 | protected final function xmlize($pXML) |
---|
| 71 | { |
---|
| 72 | return $this->xml->xmlize($pXML); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | protected final function sendPacket($pPacket) |
---|
| 76 | { |
---|
| 77 | return $this->write($pPacket); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /* |
---|
| 81 | * funcoes implementadas nesta classe |
---|
| 82 | */ |
---|
| 83 | |
---|
| 84 | /* |
---|
| 85 | * funcoes privadas |
---|
| 86 | */ |
---|
| 87 | private final function _iq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false ) |
---|
| 88 | { |
---|
| 89 | $xml = "<iq type='" . $pType . "' id='" . $pId . "'"; |
---|
| 90 | $xml .= ( $pTo ) ? " to='" . $pTo . "'" : ""; |
---|
| 91 | $xml .= ( $pFrom ) ? " from='" . $pFrom . "'" : ""; |
---|
| 92 | |
---|
| 93 | if ( $pXmlns == "vcard-temp" ) |
---|
| 94 | { |
---|
| 95 | $xml .= ">"; |
---|
| 96 | $xml .= $pLoad; |
---|
| 97 | } |
---|
| 98 | else |
---|
| 99 | { |
---|
| 100 | $xml .= "><query xmlns='" . $pXmlns . "'"; |
---|
| 101 | $xml .= ( $pLoad ) ? ">" . $pLoad . "</query>" : "/>"; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | $xml .= "</iq>"; |
---|
| 105 | |
---|
| 106 | return $xml; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | private final function authPlain() |
---|
| 110 | { |
---|
| 111 | $xml = "<username>" . $this->username . "</username>"; |
---|
| 112 | $xml .= "<password>" . $this->password . "</password>"; |
---|
| 113 | $xml .= "<resource>" . $this->resource . "</resource>"; |
---|
| 114 | |
---|
| 115 | if ( !$this->sendIq('set', 'auth_1', NULL, NULL, "jabber:iq:auth", $xml) ) |
---|
| 116 | { |
---|
| 117 | return false; |
---|
| 118 | } |
---|
| 119 | usleep(90000); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | private function setConnect() |
---|
| 123 | { |
---|
| 124 | $xml = "<?xml version='1.0' encoding='UTF-8'?><stream:stream to='" . $this->server . "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"; |
---|
| 125 | $this->write($xml); |
---|
| 126 | $xml = $this->read(); |
---|
| 127 | |
---|
| 128 | $xml = $this->xmlize($xml); |
---|
| 129 | |
---|
| 130 | if ( @array_key_exists('starttls', $xml['stream:stream']['#']['stream:features']['0']['#']) && |
---|
| 131 | @array_key_exists('required', $xml['stream:stream']['#']['stream:features']['0']['#']['starttls']['0']['#']) ) |
---|
| 132 | $this->setTLS(); |
---|
| 133 | else |
---|
| 134 | $this->simple_authentication(); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | private function setTLS() |
---|
| 138 | { |
---|
| 139 | $xml = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"; |
---|
| 140 | $this->write($xml); |
---|
| 141 | |
---|
| 142 | $xml = $this->read(); |
---|
| 143 | $xml = $this->xmlize($xml); |
---|
| 144 | |
---|
| 145 | if ( array_key_exists('proceed', $xml) ) |
---|
| 146 | { |
---|
| 147 | stream_socket_enable_crypto($this->returnSocket(), TRUE, STREAM_CRYPTO_METHOD_SSLv23_CLIENT); |
---|
| 148 | |
---|
| 149 | $xml = "<stream:stream to='" . $this->server . "' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>"; |
---|
| 150 | $this->write($xml); |
---|
| 151 | |
---|
| 152 | $xml = $this->read(); |
---|
| 153 | $xml = $this->xmlize($xml); |
---|
| 154 | |
---|
| 155 | socket_set_blocking($this->returnSocket(), 0); |
---|
| 156 | |
---|
| 157 | $this->authPlain(); |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | // LOG ERRO |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | private function simple_authentication() |
---|
| 166 | { |
---|
| 167 | socket_set_blocking($this->returnSocket(), 0); |
---|
| 168 | |
---|
| 169 | $this->authPlain(); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* |
---|
| 173 | * funcoes do pacote |
---|
| 174 | */ |
---|
| 175 | |
---|
| 176 | protected final function presence($pType = false, $pTo = false, $pShow = false, $pStatus = false, $pPriority = false) |
---|
| 177 | { |
---|
| 178 | $xml = "<presence"; |
---|
| 179 | $xml .= ($pTo) ? " to='{$pTo}'" : ''; |
---|
| 180 | $xml .= ($pType) ? " type='{$pType}'" : ''; |
---|
| 181 | $xml .= ($pStatus || $pShow || $pPriority) ? ">" : "/>"; |
---|
| 182 | |
---|
| 183 | $xml .= ($pStatus) ? " <status>{$pStatus}</status>" : ''; |
---|
| 184 | $xml .= ($pShow) ? " <show>{$pShow}</show>" : ''; |
---|
| 185 | $xml .= ($pPriority) ? " <priority>{$pPriority}</priority>" : ''; |
---|
| 186 | $xml .= ($pStatus || $pShow || $pPriority) ? "</presence>" : ''; |
---|
| 187 | |
---|
| 188 | if ( $this->sendPacket($xml) ) |
---|
| 189 | { |
---|
| 190 | usleep(90000); |
---|
| 191 | |
---|
| 192 | return true; |
---|
| 193 | } |
---|
| 194 | else |
---|
| 195 | { |
---|
| 196 | $this->writeLog("ERROR: send_presence() #1"); |
---|
| 197 | return false; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | protected final function readSocketFromServer() |
---|
| 202 | { |
---|
| 203 | $retorno = "<resposta>" . $this->read(true) . "</resposta>"; |
---|
| 204 | $this->array_teste[] = $retorno; |
---|
| 205 | $retorno = $this->xmlize($retorno); |
---|
| 206 | $retorno = $retorno['resposta']['#']; |
---|
| 207 | return $retorno; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | protected final function sendIq($pType = false, $pId = false, $pTo = false, $pFrom = false, $pXmlns = false, $pLoad = false ) |
---|
| 211 | { |
---|
| 212 | if ( !preg_match("/^(get|set|result|error)$/i", $pType, $matches, PREG_OFFSET_CAPTURE) ) |
---|
| 213 | { |
---|
| 214 | $this->writeLog("ERROR: _iq() #2 - type must be 'get', 'set', 'result' or 'error'"); |
---|
| 215 | return false; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | if ( $this->sendPacket($this->_iq($pType, $pId, $pTo, $pFrom, $pXmlns, $pLoad)) ) |
---|
| 219 | { |
---|
| 220 | return true; |
---|
| 221 | } |
---|
| 222 | return false; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | protected final function writeLog($pLog = false) |
---|
| 226 | { |
---|
| 227 | switch ( substr($pLog, 0, strpos($pLog, ':')) ) |
---|
| 228 | { |
---|
| 229 | case 'ERROR' : |
---|
| 230 | if ( $this->log_error === true ) |
---|
| 231 | { |
---|
| 232 | $log = NULL; |
---|
| 233 | $log .= $pLog . ' :: '; |
---|
| 234 | $log .= $this->jid . ' :: '; |
---|
| 235 | $log .= date('m/d/Y H:i:s'); |
---|
| 236 | $log .= "\n"; |
---|
| 237 | if ( $fp = fopen ($this->log_error_file, "a+") ) |
---|
| 238 | { |
---|
| 239 | fwrite($fp, $log); |
---|
| 240 | fclose($fp); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | // end case 'ERROR' |
---|
| 244 | break; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|
| 249 | ?> |
---|