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

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

Ticket #1113 - Correcao para a busca da foto na janela de chat para Ldaps Externos.

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