source: sandbox/jabberit_messenger/trophy_expresso/inc/webservice.php @ 2411

Revision 2411, 11.2 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Adicionado a funcionalidade de procurar contatos no ldap.

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