1 | <?php |
---|
2 | /************************************************************ |
---|
3 | * Classe Jabber - Expresso * |
---|
4 | * ------------------------------------------------------- * |
---|
5 | * Com Autenticação no Servidor Jabber com Tls * |
---|
6 | * Rodrigo Souza - rodsouza@celepar.pr.gov.br * |
---|
7 | * Alexandre Correia - alexandrecorreia@celepar.pr.gov.br * |
---|
8 | ************************************************************/ |
---|
9 | |
---|
10 | require_once "class.Jabber.inc.php"; |
---|
11 | require_once "class.db_im.inc.php"; |
---|
12 | |
---|
13 | class Ujabber extends Jabber |
---|
14 | { |
---|
15 | |
---|
16 | private $server = "localhost"; |
---|
17 | private $port = "5222"; |
---|
18 | |
---|
19 | private $username = false; |
---|
20 | private $password = false; |
---|
21 | private $resource = false; |
---|
22 | |
---|
23 | private $jid = false; |
---|
24 | |
---|
25 | private $log_error_file = 'erro.log'; |
---|
26 | |
---|
27 | function __construct() |
---|
28 | { |
---|
29 | |
---|
30 | $this->username = $_SESSION['phpgw_info']['instant_messenger']['user']; |
---|
31 | $this->password = $_SESSION['phpgw_info']['instant_messenger']['passwd']; |
---|
32 | $this->server = $_SESSION['phpgw_info']['instant_messenger']['name_jabber']; |
---|
33 | $this->port = $_SESSION['phpgw_info']['instant_messenger']['port_jabber']; |
---|
34 | $this->resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber']; |
---|
35 | $this->jid = $this->username."@".$this->server . "/" . $this->resource; |
---|
36 | |
---|
37 | parent :: __construct($this->jid, $this->password, $this->port); |
---|
38 | |
---|
39 | } |
---|
40 | |
---|
41 | function __destruct() |
---|
42 | { |
---|
43 | parent :: __destruct(); |
---|
44 | } |
---|
45 | |
---|
46 | /* |
---|
47 | * List Users |
---|
48 | */ |
---|
49 | |
---|
50 | function list_user() |
---|
51 | { |
---|
52 | $contacts = $this->UpdateContacts(); |
---|
53 | $group_users = array(); |
---|
54 | $group_nusers = array(); |
---|
55 | |
---|
56 | for($i=0; $i < count($contacts); $i++){ |
---|
57 | $group_users[] = strtolower($contacts[$i]['group']).";".$contacts[$i]['name'].";".$contacts[$i]['jid'].";".$contacts[$i]['subscription'].";".$contacts[$i]['online']; |
---|
58 | } |
---|
59 | |
---|
60 | // VCARD - PESSOAL |
---|
61 | |
---|
62 | $VCARD_P = $this->getVcard($this->username."@".$this->server); |
---|
63 | |
---|
64 | $group_nusers[0]['VCARD_FN'] = $VCARD_P['FN']; |
---|
65 | $group_nusers[0]['VCARD_NICKNAME'] = $VCARD_P['NICKNAME']; |
---|
66 | $group_nusers[0]['VCARD_ORGNAME'] = $VCARD_P['ORGNAME']; |
---|
67 | $group_nusers[0]['VCARD_ORGUNIT'] = $VCARD_P['ORGUNIT']; |
---|
68 | $group_nusers[0]['VCARD_ROLE'] = $VCARD_P['ROLE']; |
---|
69 | $group_nusers[0]['VCARD_BDAY'] = $VCARD_P['BDAY']; |
---|
70 | $group_nusers[0]['VCARD_DESC'] = $VCARD_P['DESC']; |
---|
71 | $group_nusers[0]['VCARD_JID'] = $VCARD_P['JID']; |
---|
72 | |
---|
73 | natcasesort($group_users); |
---|
74 | $i=1; |
---|
75 | |
---|
76 | // VCARD / CONTATOS - USUARIOS |
---|
77 | |
---|
78 | foreach($group_users as $tmp){ |
---|
79 | $aux = explode(";",$tmp); |
---|
80 | $group_nusers[$i]['group'] = $aux[0]; |
---|
81 | $group_nusers[$i]['name'] = $aux[1]; |
---|
82 | $group_nusers[$i]['jid'] = $aux[2]; |
---|
83 | $group_nusers[$i]['subscription'] = trim($aux[3]); |
---|
84 | $group_nusers[$i]['online'] = $aux[4]; |
---|
85 | $aux_jid = explode("@",$aux[2]); |
---|
86 | |
---|
87 | $VCARD_U = $this->getVcard($aux_jid[0]."@".$this->server); |
---|
88 | |
---|
89 | $group_nusers[$i]['VCARD_FN'] = $VCARD_U['FN']; |
---|
90 | $group_nusers[$i]['VCARD_NICKNAME'] = $VCARD_U['NICKNAME']; |
---|
91 | $group_nusers[$i]['VCARD_ORGNAME'] = $VCARD_U['ORGNAME']; |
---|
92 | $group_nusers[$i]['VCARD_ORGUNIT'] = $VCARD_U['ORGUNIT']; |
---|
93 | $group_nusers[$i]['VCARD_ROLE'] = $VCARD_U['ROLE']; |
---|
94 | $group_nusers[$i]['VCARD_BDAY'] = $VCARD_U['BDAY']; |
---|
95 | $group_nusers[$i]['VCARD_DESC'] = $VCARD_U['DESC']; |
---|
96 | $group_nusers[$i]['VCARD_JID'] = $VCARD_U['JID']; |
---|
97 | |
---|
98 | $i++; |
---|
99 | } |
---|
100 | |
---|
101 | return $group_nusers; |
---|
102 | } |
---|
103 | |
---|
104 | /* |
---|
105 | * Contacts - Update - Add - Remove |
---|
106 | */ |
---|
107 | |
---|
108 | function UpdateContacts() |
---|
109 | { |
---|
110 | $array_contacts = array(); |
---|
111 | $array_db = array(); |
---|
112 | $array_return = array(); |
---|
113 | |
---|
114 | $this->sendIq('get', 'update1', NULL, NULL, 'jabber:iq:roster'); |
---|
115 | usleep(50000); |
---|
116 | $result = $this->readSocketFromServer(); |
---|
117 | |
---|
118 | if(count($result['iq'][1]['#']['query'][0]['#']['item']) > 0){ |
---|
119 | $array_contacts = $result['iq'][1]['#']['query'][0]['#']['item']; |
---|
120 | } |
---|
121 | |
---|
122 | $j = 0; |
---|
123 | |
---|
124 | // Consulta no Servidor Jabber; |
---|
125 | if(count($array_contacts) > 0 ){ |
---|
126 | for($i=0 ; $i < count($array_contacts); $i++) |
---|
127 | { |
---|
128 | $array_return[$j]['subscription'] = $array_contacts[$i]['@']['subscription']; |
---|
129 | $array_return[$j]['name'] = $array_contacts[$i]['@']['name']; |
---|
130 | $array_return[$j]['jid'] = $array_contacts[$i]['@']['jid']; |
---|
131 | $array_return[$j]['group'] = $array_contacts[$i]['#']['group']['0']['#']; |
---|
132 | $aux_jid = explode("@",$array_return[$j]['jid']); |
---|
133 | |
---|
134 | $array_return[$j]['online'] = ($this->Last_access_User(trim($aux_jid[0]."@".$this->server))) ? 1:0; |
---|
135 | $j++; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | // Consulta no Bd Postgres; |
---|
140 | $db_im = new db_im(); |
---|
141 | $array_db = $db_im->get_contacts_im(NULL,$this->jid); |
---|
142 | |
---|
143 | if(count($array_db) > 0 ) |
---|
144 | { |
---|
145 | for($i=0 ; $i < count($array_db); $i++) |
---|
146 | { |
---|
147 | $array_return[$j]['subscription'] = 'to'; |
---|
148 | $array_return[$j]['name'] = ''; |
---|
149 | $array_return[$j]['jid'] = $array_db[$i]['im_from']; |
---|
150 | $array_return[$j]['group'] = ''; |
---|
151 | $array_return[$j]['online'] = 0; |
---|
152 | $j++; |
---|
153 | } |
---|
154 | } |
---|
155 | return $array_return; |
---|
156 | } |
---|
157 | |
---|
158 | function AddNewContacts($pUser) |
---|
159 | { |
---|
160 | $jid = explode("@",$pUser['email']); |
---|
161 | $pJid = $jid[0]."@".$this->server; |
---|
162 | $pName = $pUser['name']; |
---|
163 | $pGroup = $pUser['group']; |
---|
164 | |
---|
165 | if(trim($jid[0]) != trim($this->username)){ |
---|
166 | $addid = ( $addid ) ? $addid : "adduser_" . time(); |
---|
167 | if ( $pJid ) |
---|
168 | { |
---|
169 | $newcontact = "<item jid='$pJid'"; |
---|
170 | $newcontact .= ($pName) ? " name='" . $pName . "'" : ''; |
---|
171 | $newcontact .= (($pGroup) ? "><group>" . $pGroup . "</group>\n</item" : "/") . ">"; |
---|
172 | |
---|
173 | if($this->sendIq('set', $addid, NULL, NULL, "jabber:iq:roster", $newcontact)){ |
---|
174 | // Bd; |
---|
175 | $db_im = new db_im(); |
---|
176 | if($db_im->delete_contacts($pJid,$this->jid)){ |
---|
177 | $this->Subscribe($pJid); |
---|
178 | } |
---|
179 | $this->AcceptRequest($request = array("jid" => $pJid,"subscription" => $pUser['subscription'])); |
---|
180 | return true; |
---|
181 | }else{ |
---|
182 | return false; |
---|
183 | } |
---|
184 | } |
---|
185 | else |
---|
186 | { |
---|
187 | $this->writeLog("ERROR: RosterAddUser() #1"); |
---|
188 | return false; |
---|
189 | } |
---|
190 | }else{ |
---|
191 | return false; |
---|
192 | } |
---|
193 | } |
---|
194 | |
---|
195 | function UpdateUser($pUser) |
---|
196 | { |
---|
197 | $pJid = $pUser['jid']; |
---|
198 | $pName = $pUser['name']; |
---|
199 | $pGroup = $pUser['group']; |
---|
200 | |
---|
201 | $upid = ( $upid ) ? $upid : "updateuser_" . time(); |
---|
202 | |
---|
203 | if ( $pJid ) |
---|
204 | { |
---|
205 | $updatecontact = "<item jid='$pJid'"; |
---|
206 | $updatecontact .= ($pName) ? " name='" . $pName . "'" : ''; |
---|
207 | $updatecontact .= (($pGroup) ? "><group>" . $pGroup . "</group>\n</item" : "/") . ">"; |
---|
208 | |
---|
209 | if($this->sendIq('set', $upid, NULL, NULL, "jabber:iq:roster", $updatecontact)){ |
---|
210 | return true; |
---|
211 | }else{ |
---|
212 | return false; |
---|
213 | } |
---|
214 | } |
---|
215 | else |
---|
216 | { |
---|
217 | $this->writeLog("ERROR: RosterUpdateUser() #1"); |
---|
218 | return false; |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | function RosterRemoveUser($pUser) |
---|
223 | { |
---|
224 | $pJid = $pUser['jid']; |
---|
225 | $pId = 'deluser_' . time(); |
---|
226 | |
---|
227 | if ($pJid && $pId) |
---|
228 | { |
---|
229 | $db_im = new db_im(); |
---|
230 | $db_im->delete_contacts($pJid,$this->jid); |
---|
231 | if(!$this->sendIq('set',$pId,NULL,NULL,"jabber:iq:roster","<item jid='$pJid' subscription='remove'/>")){ |
---|
232 | return false; |
---|
233 | } |
---|
234 | return true; |
---|
235 | }else{ |
---|
236 | $this->writeLog("ERROR: RosterRemoveUser() #1"); |
---|
237 | return false; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | /* |
---|
242 | * GetVcard |
---|
243 | */ |
---|
244 | |
---|
245 | function getVcard($pJid) |
---|
246 | { |
---|
247 | $array_vcard = array(); |
---|
248 | $id_n = explode("@",$pJid); |
---|
249 | $id = $id_n[0]; |
---|
250 | |
---|
251 | $this->sendIq('get',$id,$pJid,NULL,"vcard-temp","<vCard xmlns='vcard-temp'/>"); |
---|
252 | usleep(50000); |
---|
253 | $result = $this->readSocketFromServer(); |
---|
254 | $arr = $result['iq'][0]['#']['vCard'][0]['#']; |
---|
255 | |
---|
256 | // vCard |
---|
257 | $array_vcard['FN'] = trim($arr['FN']['0']['#']) ? urldecode($arr['FN']['0']['#']): $id; |
---|
258 | $array_vcard['NICKNAME'] = trim($arr['NICKNAME']['0']['#']) ? urldecode($arr['NICKNAME']['0']['#']) : $pJid; |
---|
259 | $array_vcard['ORGNAME'] = trim($arr['ORG']['0']['#']['ORGNAME']['0']['#']) ? urldecode($arr['ORG']['0']['#']['ORGNAME']['0']['#']) : ""; |
---|
260 | $array_vcard['ORGUNIT'] = trim($arr['ORG']['0']['#']['ORGUNIT']['0']['#']) ? urldecode($arr['ORG']['0']['#']['ORGUNIT']['0']['#']) : ""; |
---|
261 | $array_vcard['ROLE'] = trim($arr['ROLE']['0']['#']) ? urldecode($arr['ROLE']['0']['#']) : ""; |
---|
262 | $array_vcard['BDAY'] = trim($arr['BDAY']['0']['#']) ? urldecode($arr['BDAY']['0']['#']) : ""; |
---|
263 | $array_vcard['DESC'] = trim($arr['DESC']['0']['#']) ? urldecode($arr['DESC']['0']['#']) : ""; |
---|
264 | $array_vcard['JID'] = $pJid; |
---|
265 | return $array_vcard; |
---|
266 | } |
---|
267 | |
---|
268 | function newVcard($pVcard_User) |
---|
269 | { |
---|
270 | |
---|
271 | $pVcard = explode(";",$pVcard_User['vcard']); |
---|
272 | $nid = explode("@",$this->jid); |
---|
273 | $id = $nid[0]; |
---|
274 | $xmlVcard = ""; |
---|
275 | |
---|
276 | // Vcard |
---|
277 | $xmlVcard = "<iq id='$id' type='set'>"; |
---|
278 | $xmlVcard .= "<vCard xmlns='vcard-temp'>"; |
---|
279 | $xmlVcard .= "<FN>".urlencode($pVcard[0])."</FN>"; |
---|
280 | $xmlVcard .= "<NICKNAME>".urlencode($pVcard[1])."</NICKNAME>"; |
---|
281 | $xmlVcard .= "<ORG>"; |
---|
282 | $xmlVcard .= "<ORGNAME>".urlencode($pVcard[2])."</ORGNAME>"; |
---|
283 | $xmlVcard .= "<ORGUNIT>".urlencode($pVcard[3])."</ORGUNIT>"; |
---|
284 | $xmlVcard .= "</ORG>"; |
---|
285 | $xmlVcard .= "<ROLE>".urlencode($pVcard[4])."</ROLE>"; |
---|
286 | $xmlVcard .= "<BDAY>".urlencode($pVcard[5])."</BDAY>"; |
---|
287 | $xmlVcard .= "<DESC>".urlencode($pVcard[6])."</DESC>"; |
---|
288 | $xmlVcard .= "</vCard></iq>"; |
---|
289 | |
---|
290 | if(!$this->SendPacket($xmlVcard)) |
---|
291 | return false; |
---|
292 | return true; |
---|
293 | |
---|
294 | } |
---|
295 | |
---|
296 | /* |
---|
297 | * getMessage |
---|
298 | */ |
---|
299 | |
---|
300 | function getMessages() |
---|
301 | { |
---|
302 | $this->presence(); |
---|
303 | |
---|
304 | $this->sendIq('get','roster',NULL,NULL,"jabber:x:offline"); |
---|
305 | |
---|
306 | $array_return1 = $this->readSocketFromServer(); |
---|
307 | |
---|
308 | $array_message = array(); |
---|
309 | $j = 0; |
---|
310 | for($i=0; $i < count($array_return1['message']); $i++) |
---|
311 | { |
---|
312 | $array_message[$j]['from'] = $array_return1['message'][$i]['@']['from']; |
---|
313 | $array_message[$j]['to'] = $array_return1['message'][$i]['@']['to']; |
---|
314 | $array_message[$j]['body'] = urldecode(($array_return1['message'][$i]['#']['body'][0]['#'])); |
---|
315 | $j++; |
---|
316 | } |
---|
317 | |
---|
318 | return $array_message; |
---|
319 | } |
---|
320 | |
---|
321 | function SendMessage($pSendMessage) |
---|
322 | { |
---|
323 | //$this->presence(); |
---|
324 | $to = $pSendMessage['to']; |
---|
325 | $type = "normal"; |
---|
326 | $id = ""; |
---|
327 | $content = array( |
---|
328 | "subject" => $pSendMessage['subject'] ? $pSendMessage['subject'] : "", |
---|
329 | "thread" => $pSendMessage['thread'] ? $pSendMessage['thread'] : "", |
---|
330 | "body" => $pSendMessage['body'] ? urlencode((trim(preg_replace('/^<br>|<br>$/', '', $pSendMessage['body'])))) : "" |
---|
331 | ); |
---|
332 | $payload = ""; |
---|
333 | |
---|
334 | if ($to && is_array($content)) |
---|
335 | { |
---|
336 | if (!$id) |
---|
337 | { |
---|
338 | $id = $type . "_" . time(); |
---|
339 | } |
---|
340 | |
---|
341 | $xml = "<message to='$to' type='$type' id='$id'>\n"; |
---|
342 | |
---|
343 | if ($content['subject']) |
---|
344 | { |
---|
345 | $xml .= "<subject>" . $content['subject'] . "</subject>\n"; |
---|
346 | } |
---|
347 | |
---|
348 | if ($content['thread']) |
---|
349 | { |
---|
350 | $xml .= "<thread>" . $content['thread'] . "</thread>\n"; |
---|
351 | } |
---|
352 | |
---|
353 | $xml .= "<body>" . $content['body'] . "</body>\n"; |
---|
354 | $xml .= $payload; |
---|
355 | $xml .= "</message>\n"; |
---|
356 | unset($id); |
---|
357 | |
---|
358 | if ($this->SendPacket($xml)) |
---|
359 | { |
---|
360 | return TRUE; |
---|
361 | } |
---|
362 | else |
---|
363 | { |
---|
364 | $this->writeLog("ERROR: SendMessage() #1"); |
---|
365 | return FALSE; |
---|
366 | } |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | $this->writeLog("ERROR: SendMessage() #2"); |
---|
371 | return FALSE; |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | /* |
---|
376 | * Log - |
---|
377 | */ |
---|
378 | |
---|
379 | |
---|
380 | /* |
---|
381 | * Verifica os acessos de cada contato no servidor; |
---|
382 | */ |
---|
383 | |
---|
384 | function Last_access_User($pJid) |
---|
385 | { |
---|
386 | $id = "last_" . time(); |
---|
387 | $result = array(); |
---|
388 | $this->sendIq('get',$id,$pJid,NULL,'jabber:iq:last'); |
---|
389 | usleep(50000); |
---|
390 | |
---|
391 | $result = $this->readSocketFromServer(); |
---|
392 | |
---|
393 | $time_offline = $result['iq'][0]['#']['query'][0]['@']['seconds']; |
---|
394 | return $this->time_off_line($time_offline); |
---|
395 | |
---|
396 | } |
---|
397 | |
---|
398 | function time_off_line($pTime) |
---|
399 | { |
---|
400 | $time_access = $pTime / 15; |
---|
401 | if($time_access < 2) |
---|
402 | return TRUE; |
---|
403 | else |
---|
404 | return FALSE; |
---|
405 | } |
---|
406 | |
---|
407 | /* |
---|
408 | * Permissão - Autorizar / Negar ; |
---|
409 | */ |
---|
410 | |
---|
411 | function AcceptRequest($pRequest) |
---|
412 | { |
---|
413 | $jid = explode("@",$pRequest['jid']); |
---|
414 | $pToJid = $jid[0]."@".$this->server; |
---|
415 | |
---|
416 | if(trim($pRequest['subscription']) == 'none' || trim($pRequest['subscription']) == 'from'){ |
---|
417 | $db_im = new db_im(); |
---|
418 | if(count($db_im->get_contacts_im($this->jid, $pToJid)) == 0){ |
---|
419 | if(!$db_im->insert_contacts($this->jid , $pToJid)) |
---|
420 | return false; |
---|
421 | else |
---|
422 | return $this->SubscriptionAcceptRequest($pToJid); |
---|
423 | }else{ |
---|
424 | return false; |
---|
425 | } |
---|
426 | }else{ |
---|
427 | if(trim($pRequest['subscription']) == 'to'){ |
---|
428 | return $this->SubscriptionAcceptRequest($pToJid); |
---|
429 | }else{ |
---|
430 | return false; |
---|
431 | } |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | function SubscriptionAcceptRequest($to = NULL) |
---|
436 | { |
---|
437 | return ($to) ? $this->presence("subscribed", $to) : FALSE; |
---|
438 | } |
---|
439 | |
---|
440 | function SubscriptionDenyRequest($to = NULL) |
---|
441 | { |
---|
442 | return ($to) ? $this->presence("unsubscribed", $to) : FALSE; |
---|
443 | } |
---|
444 | |
---|
445 | function Subscribe($to = NULL) |
---|
446 | { |
---|
447 | return ($to) ? $this->presence("subscribe", $to) : FALSE; |
---|
448 | } |
---|
449 | |
---|
450 | function Unsubscribe($to = NULL) |
---|
451 | { |
---|
452 | return ($to) ? $this->presence("unsubscribe", $to) : FALSE; |
---|
453 | } |
---|
454 | // TESTE |
---|
455 | |
---|
456 | function encode(&$item, $val = 'encode') |
---|
457 | { |
---|
458 | switch( gettype($item) ) |
---|
459 | { |
---|
460 | case 'object' : |
---|
461 | $item = get_object_vars($item); |
---|
462 | encode($item); |
---|
463 | break; |
---|
464 | case 'array' : |
---|
465 | array_walk_recursive($item, 'encode'); |
---|
466 | break; |
---|
467 | default : $item = utf8_encode($item); |
---|
468 | } |
---|
469 | } |
---|
470 | } |
---|
471 | ?> |
---|