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

Revision 1885, 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->fileLdapInternal )
238                                return false;
239
240                        if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
241                        {
242                               
243                                $this->ldapConnect();
244                               
245                                if( $this->conn )
246                                {
247                                        $filter                 = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
248                                        $justthese              = array("uid","jpegPhoto");
249                                        $search                 = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
250                                        $get_entries    = ldap_get_entries($this->conn,$search);
251                                       
252                                        if( $get_entries['count'] > 0 )
253                                        {
254                                                $first_entry = ldap_first_entry( $this->conn, $search );
255                                                $photo = @ldap_get_values_len($this->conn, $first_entry, 'jpegphoto');
256                                               
257                                                if ( $photo )
258                                                        return $photo[0];
259                                               
260                                                return false;                                                           
261                                        }
262                                }
263                        }
264                }
265                else
266                {                                       
267                        if( !$this->fileLdapExternal )
268                                return false;
269                               
270                        $jabberName = substr($pJid, strpos($pJid, "@") + 1 );
271
272                        if( strpos($jabberName, "/") )
273                                $jabberName = substr($jabberName, 0, strpos($jabberName, "/"));
274
275                        $this->ldapConnectExternal($jabberName);
276
277                        if( $this->conn )
278                        {
279                                $filter                 = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
280                                $justthese              = array("uid","jpegPhoto");
281                                $search                 = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
282                                $get_entries    = ldap_get_entries($this->conn,$search);
283                               
284                                if( $get_entries['count'] > 0 )
285                                {
286                                        $first_entry = ldap_first_entry( $this->conn, $search );
287                                        $photo = @ldap_get_values_len($this->conn, $first_entry, 'jpegphoto');
288                                       
289                                        if ( $photo )
290                                                return $photo[0];
291                                       
292                                        return false;                                                           
293                                }
294                        }
295                }
296               
297                return false;
298        }
299       
300        public final function getPhotoSession($pUid, $pOu)
301        {
302                $uid = $pUid;
303                if( strpos($pUid, "@") )
304                        $uid = substr($pUid, 0, strpos($pUid, "@"));
305               
306                require_once("../../header.session.inc.php");
307               
308                if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]) )
309                {
310                        $photo = imagecreatefromstring($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
311
312                        header("Content-Type: image/jpeg");
313                        $width = imagesx($photo);
314                        $height = imagesy($photo);
315                        $twidth = 60;
316                        $theight = 80;
317                        $small_photo = imagecreatetruecolor ($twidth, $theight);
318                        imagecopyresampled($small_photo, $photo, 0, 0, 0, 0,$twidth, $theight, $width, $height);
319                        imagejpeg($small_photo,'',100);
320
321                        unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
322
323                        return;
324                }
325        }
326}
327
328// Applet - utilizando o serviço Voip;
329if(trim($_REQUEST['javaVoipFrom']) != "" && trim($_REQUEST['javaVoipTo']) != "" )
330{
331        $obj = new webService();
332        $voipFrom = $_REQUEST['javaVoipFrom'];
333        $voipTo = $_REQUEST['javaVoipTo'];
334        printf("%s",$obj->CallVoipConnect($voipFrom, $voipTo));
335}
336
337// Applet - fotos pelo applet;
338if(trim($_REQUEST['javaPhoto']) != "" )
339{
340        $obj = new webService();
341        $jid = $_REQUEST['javaPhoto'];
342        $jid = ( strpos($jid, "/") !== false ) ? substr($jid, 0, strpos($jid, "/")) : $jid;
343       
344        $photo = $obj->getPhotoLdap( $jid, true );
345        $photoWidth = 70;
346        $photoHeight = 90;
347        $newImage = imagecreatetruecolor($photoWidth,$photoHeight);             
348
349        if( $photo )
350        {
351                $photo = imagecreatefromstring($photo);
352                imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
353        }
354        else
355        {
356                $photo = $obj->getPhotoLdap($jid, false);
357                if( $photo )
358                {
359                        $photo = imagecreatefromstring($photo);
360                        imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
361                }
362                else
363                {
364                        $photo = @imagecreatefrompng("../templates/default/images/photo.png");
365                        imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
366                }
367        }
368       
369        ob_start();
370        imagepng($newImage);
371        $imagePhoto = ob_get_contents();
372        imagedestroy($newImage);
373        ob_end_clean();
374        printf("%s",base64_encode($imagePhoto));
375}
376
377// Applet - jid;
378if(trim($_REQUEST['jid']) != "")
379{
380       
381        $jid = trim($_REQUEST['jid']);
382        $charset = trim($_REQUEST['charset']);
383        $obj = new webService();
384       
385        printf("%s",$obj->getNameOrganization($jid, $charset));
386}
387
388// Php - fotos pelo php;
389if(trim($_REQUEST['phpPhoto']) != "")
390{
391        $obj = new webservice();
392        $ou = $_REQUEST['phpOu'];
393        $jid = $_REQUEST['phpPhoto'];
394       
395        $obj->getPhotoSession($jid, $ou);
396}
397
398?>
Note: See TracBrowser for help on using the repository browser.