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