source: trunk/jabberit_messenger/inc/webservice.php @ 1871

Revision 1871, 10.9 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #808 - Busca implementada utilizando somente os grupos cadastrados.

  • Property svn:executable set to *
Line 
1<?php
2  /***************************************************************************\
3  *  Expresso - Expresso Messenger                                            *
4  *     - Alexandre Correia / Rodrigo Souza                                                               *
5  *     - JETI - http://jeti-im.org/                                                                              *
6  * ------------------------------------------------------------------------- *
7  *  This program is free software; you can redistribute it and/or modify it  *
8  *  under the terms of the GNU General Public License as published by the    *
9  *  Free Software Foundation; either version 2 of the License, or (at your   *
10  *  option) any later version.                                               *
11  \***************************************************************************/
12
13class webService
14{
15        private $conn;
16        private $contextLdap;
17        private $userLDAP;     
18        private $jabberName = null;
19        private $passwd;
20        private $photo_user = array();
21        private $refer;
22        private $serverLdap;
23        private $fileLdapInternal = false;
24        private $fileLdapExternal = false;
25       
26        function __construct()
27        {
28                if ( file_exists('confLDAPInternal.php') )
29                {
30                        require_once('confLDAPInternal.php');
31                        $handle   = unserialize(base64_decode($LDAP_INTERNAL));
32       
33                        $this->jabberName       = $handle['jabberName'];
34                        $this->serverLdap       = $handle['serverLdap'];
35                        $this->contextLdap      = $handle['contextLdap'];
36                        $this->userLDAP         = $handle['user'];
37                        $this->passwd           = $handle['password'];
38                       
39                        $this->refer    = true;
40                        $this->version3 = true;
41                       
42                        $this->fileLdapInternal = true;
43                }
44        }
45       
46        private final function ldapConnect()
47        {
48                if(!function_exists('ldap_connect'))
49                        return False;
50               
51                if(!$this->conn = ldap_connect($this->serverLdap))
52                        return False;
53
54                if( $this->version3 )
55                        if( !ldap_set_option($this->conn,LDAP_OPT_PROTOCOL_VERSION,3) )
56                                $this->version = false;
57
58                ldap_set_option($this->conn, LDAP_OPT_REFERRALS, $this->refer);
59
60                // Bind as Admin
61                if( $this->userLDAP && $this->passwd && !ldap_bind($this->conn, $this->userLDAP . "," . $this->contextLdap, $this->passwd) )
62                        return False;
63               
64                // Bind as Anonymous
65                if( !$this->userLDAP && !$this->passwd && !@ldap_bind($this->conn) )
66                        return False;
67        }
68
69        private final function ldapConnectExternal($pHostJabber)
70        {
71                if( file_exists('confLDAPExternal.php'))
72                {
73                        require_once('confLDAPExternal.php');
74                        $handle   = unserialize(base64_decode($LDAP_EXTERNAL));
75       
76                        foreach($handle as $itens)
77                        {
78                                if(trim($pHostJabber) == $itens['jabberName'])
79                                {
80                                        $this->jabberName       = $itens['jabberName'];
81                                        $this->serverLdap       = $itens['serverLdap'];
82                                        $this->contextLdap      = $itens['contextLdap'];
83                                        $this->userLDAP         = $itens['user'];
84                                        $this->passwd           = $itens['password'];
85                                       
86                                        $this->fileLdapExternal = true;
87                                }
88                        }               
89       
90                        $this->refer    = true;
91                        $this->version3 = true;
92                       
93                        $this->ldapConnect();
94                }
95        }
96       
97        public final function CallVoipConnect($pVoipFrom, $pVoipTo)
98        {
99                $this->ldapConnect();
100
101                if( $this->conn )
102                {
103                        $filter  = "(|(&(phpgwaccounttype=u)(uid=".$pVoipFrom."))(&(phpgwaccounttype=u)(uid=".$pVoipTo.")))";
104                        $justthese = array("telephoneNumber", "uid");
105                        $search = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
106                        $entry = ldap_get_entries($this->conn,$search);
107
108                        $fromNumber = $entry[0]['telephonenumber'][0];
109                        $toNumber = $entry[1]['telephonenumber'][0];
110
111                        if ( trim($entry[0]['uid'][0]) !== trim($pVoipFrom) )
112                        {
113                                $fromNumber = $entry[1]['telephonenumber'][0];
114                                $toNumber = $entry[0]['telephonenumber'][0];
115                        }
116                }
117               
118                if( $fromNumber && $toNumber )
119                {
120                        $voipServer     = "www.pabx.celepar.parana";
121                        $voipUrl        = "/telefoniaip/servicos/voip.php";
122                        $voipPort       = "80";
123       
124                        if( !$voipServer || !$voipUrl || !$voipPort )
125                                return false;
126                       
127                        $url            = "http://".$voipServer.":".$voipPort.$voipUrl."?magic=1333&acao=liga&ramal=".$fromNumber."&numero=".$toNumber;
128                        $sMethod        = 'GET ';
129                        $crlf           = "\r\n";
130                        $sRequest       = " HTTP/1.1" . $crlf;
131                        $sRequest       .= "Host: localhost" . $crlf;
132                        $sRequest       .= "Accept: */* " . $crlf;
133                        $sRequest       .= "Connection: Close" . $crlf . $crlf;           
134                        $sRequest       = $sMethod . $url . $sRequest;   
135                        $sockHttp       = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);           
136                       
137                        if ( !$sockHttp )
138                            return false;
139                       
140                        $resSocketConnect = socket_connect($sockHttp, $voipServer, $voipPort);
141                       
142                        if ( !$resSocketConnect )
143                            return false;
144       
145                        $resSocketWrite = socket_write($sockHttp, $sRequest, strlen($sRequest));
146       
147                        if ( !$resSocketWrite )
148                            return false;
149           
150                        $sResponse = '';   
151       
152                        while ($sRead = socket_read($sockHttp, 512))
153                        {
154                            $sResponse .= $sRead;
155                        }           
156                       
157                        socket_close($sockHttp);           
158                       
159                        $pos = strpos($sResponse, $crlf . $crlf);
160                       
161                        return substr($sResponse, $pos + 2 * strlen($crlf));
162                }
163               
164                return "ERRO";                                                                 
165        }
166       
167       
168        public final function getNameOrganization($pJid, $pCharset)
169        {
170                $uid = substr($pJid, 0, strpos($pJid,"@"));
171                $return = utf8_encode("Nome : Não Identificado ;Organização : Não Identificado");
172               
173                if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
174                {
175                        $this->ldapConnect();
176
177                        if( $this->fileLdapInternal )
178                        {
179                                if( $this->conn )
180                                {
181                                        $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
182                                        $justthese = array("uid","cn","dn");
183                                        $search = ldap_search( $this->conn, $this->contextLdap, $filter,$justthese);
184                                        $get_entries = ldap_get_entries( $this->conn, $search);
185               
186                                        if( $get_entries['count'] > 0 )
187                                        {                                       
188                                                $cn = $get_entries[0]['cn'][0];
189                                                $ou = explode("dc=", $get_entries[0]['dn']);
190                                                $ou = explode("ou=",$ou[0]);
191                                                $ou = array_pop($ou);
192                                                $dn = strtoupper(substr($ou,0,strlen($ou)-1));
193                                                $return = utf8_encode("Nome : " . $cn . ";Organização : " . $dn);
194                                        }
195                                }
196                        }
197                }
198                else
199                {
200                        $this->ldapConnectExternal(substr($pJid, strpos($pJid, "@") + 1 ));
201                       
202                        if( $this->fileLdapExternal )
203                        {
204                                if( $this->conn )
205                                {
206                                        $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
207                                        $justthese = array("uid","cn","dn");
208                                        $search = ldap_search( $this->conn, $this->contextLdap, $filter, $justthese);
209                                        $get_entries = ldap_get_entries( $this->conn, $search);
210                                       
211                                        if( $get_entries['count'] > 0 )
212                                        {
213                                                $cn = $get_entries[0]['cn'][0];
214                                                $ou = explode("dc=", $get_entries[0]['dn']);
215                                                $ou = explode("ou=",$ou[0]);
216                                                $ou = array_pop($ou);
217                                                $dn = strtoupper(substr($ou,0,strlen($ou)-1));
218                                                $return = utf8_encode("Nome : " . $cn . ";Organização : " . $dn);
219                                        }
220                                }
221                        }
222                }
223
224                if( $pCharset === "1" || $pCharset === 1 )
225                        return $return;
226                else
227                        return mb_convert_encoding($return, "ISO-8859-1", "UTF-8");
228
229        }
230       
231        public final function getPhotoLdap( $pJid , $pLdapInternal )
232        {
233                $uid = substr($pJid, 0, strpos($pJid, "@"));
234
235                if( $pLdapInternal )
236                {
237                        if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
238                        {
239                                if( ! $this->fileLdapInternal )
240                                        return false;
241                               
242                                $this->ldapConnect();
243                               
244                                if( $this->conn )
245                                {
246                                        $filter                 = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
247                                        $justthese              = array("uid","jpegPhoto");
248                                        $search                 = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
249                                        $get_entries    = ldap_get_entries($this->conn,$search);
250                                       
251                                        if( $get_entries['count'] > 0 )
252                                        {
253                                                $first_entry = ldap_first_entry( $this->conn, $search );
254                                                $photo = @ldap_get_values_len($this->conn, $first_entry, 'jpegphoto');
255                                               
256                                                if ( $photo )
257                                                        return $photo[0];
258                                               
259                                                return false;                                                           
260                                        }
261                                }
262                        }
263                }
264                else
265                {                                       
266                        $jabberName = substr($pJid, strpos($pJid, "@") + 1 );
267
268                        if( strpos($jabberName, "/") )
269                                $jabberName = substr($jabberName, 0, strpos($jabberName, "/"));
270
271                        $this->ldapConnectExternal($jabberName);
272
273                        if( !$this->fileLdapExternal )
274                                return false;
275
276                        if( $this->conn )
277                        {
278                                $filter                 = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
279                                $justthese              = array("uid","jpegPhoto");
280                                $search                 = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
281                                $get_entries    = ldap_get_entries($this->conn,$search);
282                               
283                                if( $get_entries['count'] > 0 )
284                                {
285                                        $first_entry = ldap_first_entry( $this->conn, $search );
286                                        $photo = @ldap_get_values_len($this->conn, $first_entry, 'jpegphoto');
287                                       
288                                        if ( $photo )
289                                                return $photo[0];
290                                       
291                                        return false;                                                           
292                                }
293                        }
294                }
295               
296                return false;
297        }
298       
299        public final function getPhotoSession($pUid, $pOu)
300        {
301                $uid = $pUid;
302                if( strpos($pUid, "@") )
303                        $uid = substr($pUid, 0, strpos($pUid, "@"));
304               
305                require_once("../../header.session.inc.php");
306               
307                if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]) )
308                {
309                        $photo = imagecreatefromstring($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
310
311                        header("Content-Type: image/jpeg");
312                        $width = imagesx($photo);
313                        $height = imagesy($photo);
314                        $twidth = 60;
315                        $theight = 80;
316                        $small_photo = imagecreatetruecolor ($twidth, $theight);
317                        imagecopyresampled($small_photo, $photo, 0, 0, 0, 0,$twidth, $theight, $width, $height);
318                        imagejpeg($small_photo,'',100);
319
320                        unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
321
322                        return;
323                }
324        }
325}
326
327// Applet - utilizando o serviço Voip;
328if(trim($_REQUEST['javaVoipFrom']) != "" && trim($_REQUEST['javaVoipTo']) != "" )
329{
330        $obj = new webService();
331        $voipFrom = $_REQUEST['javaVoipFrom'];
332        $voipTo = $_REQUEST['javaVoipTo'];
333        printf("%s",$obj->CallVoipConnect($voipFrom, $voipTo));
334}
335
336// Applet - fotos pelo applet;
337if(trim($_REQUEST['javaPhoto']) != "" )
338{
339        $obj = new webService();
340        $jid = $_REQUEST['javaPhoto'];
341        $jid = ( strpos($jid, "/") !== false ) ? substr($jid, 0, strpos($jid, "/")) : $jid;
342       
343        $photo = $obj->getPhotoLdap( $jid, true );
344        $photoWidth = 70;
345        $photoHeight = 90;
346        $newImage = imagecreatetruecolor($photoWidth,$photoHeight);             
347
348        if( $photo )
349        {
350                $photo = imagecreatefromstring($photo);
351                imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
352        }
353        else
354        {
355                $photo = $obj->getPhotoLdap($jid, false);
356                if( $photo )
357                {
358                        $photo = imagecreatefromstring($photo);
359                        imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
360                }
361                else
362                {
363                        $photo = @imagecreatefrompng("../templates/default/images/photo.png");
364                        imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
365                }
366        }
367       
368        ob_start();
369        imagepng($newImage);
370        $imagePhoto = ob_get_contents();
371        imagedestroy($newImage);
372        ob_end_clean();
373        printf("%s",base64_encode($imagePhoto));
374}
375
376// Applet - jid;
377if(trim($_REQUEST['jid']) != "")
378{
379       
380        $jid = trim($_REQUEST['jid']);
381        $charset = trim($_REQUEST['charset']);
382        $obj = new webService();
383       
384        printf("%s",$obj->getNameOrganization($jid, $charset));
385}
386
387// Php - fotos pelo php;
388if(trim($_REQUEST['phpPhoto']) != "")
389{
390        $obj = new webservice();
391        $ou = $_REQUEST['phpOu'];
392        $jid = $_REQUEST['phpPhoto'];
393       
394        $obj->getPhotoSession($jid, $ou);
395}
396
397?>
Note: See TracBrowser for help on using the repository browser.