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

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

Ticket #860 - Corrigir quando a autenticacao do jabber nao e pelo campo uid do ldap.

  • 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'))
82                {
83                        require_once('confLDAPExternal.php');
84                        $handle   = unserialize(base64_decode($LDAP_EXTERNAL));
85       
86                        foreach($handle as $itens)
[1868]87                        {
[1160]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'];
[1871]95                                       
96                                        $this->fileLdapExternal = true;
[1868]97                                }
98                        }               
[1160]99       
100                        $this->refer    = true;
101                        $this->version3 = true;
102                       
103                        $this->ldapConnect();
104                }
[417]105        }
106       
[1130]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.")))";
[1135]114                        $justthese = array("telephoneNumber", "uid");
[1130]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];
[1135]120
121                        if ( trim($entry[0]['uid'][0]) !== trim($pVoipFrom) )
122                        {
123                                $fromNumber = $entry[1]['telephonenumber'][0];
124                                $toNumber = $entry[0]['telephonenumber'][0];
125                        }
[1130]126                }
[1135]127               
[1130]128                if( $fromNumber && $toNumber )
129                {
[1818]130                        $voipServer     = "www.pabx.celepar.parana";
[1130]131                        $voipUrl        = "/telefoniaip/servicos/voip.php";
132                        $voipPort       = "80";
133       
134                        if( !$voipServer || !$voipUrl || !$voipPort )
135                                return false;
136                       
[1214]137                        $url            = "http://".$voipServer.":".$voipPort.$voipUrl."?magic=1333&acao=liga&ramal=".$fromNumber."&numero=".$toNumber;
[1130]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       
[946]178        public final function getNameOrganization($pJid, $pCharset)
[551]179        {
[946]180                $uid = substr($pJid, 0, strpos($pJid,"@"));
[1160]181                $return = utf8_encode("Nome : Não Identificado ;Organização : Não Identificado");
[551]182               
[946]183                if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
[1504]184                {
[946]185                        $this->ldapConnect();
[1504]186
187                        if( $this->fileLdapInternal )
188                        {
189                                if( $this->conn )
190                                {
[1900]191                                        $filter = "(&(phpgwaccounttype=u)(".$this->attribute."=".$uid.")(!(phpgwaccountvisible=-1)))";
192                                        $justthese = array($this->attribute,"cn","dn");
[1504]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                }
[946]208                else
[1504]209                {
[946]210                        $this->ldapConnectExternal(substr($pJid, strpos($pJid, "@") + 1 ));
[1504]211                       
212                        if( $this->fileLdapExternal )
[946]213                        {
[1504]214                                if( $this->conn )
215                                {
[1900]216                                        $filter = "(&(phpgwaccounttype=u)(".$this->attribute."=".$uid.")(!(phpgwaccountvisible=-1)))";
217                                        $justthese = array($this->attribute,"cn","dn");
[1504]218                                        $search = ldap_search( $this->conn, $this->contextLdap, $filter, $justthese);
219                                        $get_entries = ldap_get_entries( $this->conn, $search);
[1160]220                                       
[1504]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                                }
[946]231                        }
232                }
[551]233
[1160]234                if( $pCharset === "1" || $pCharset === 1 )
[946]235                        return $return;
236                else
237                        return mb_convert_encoding($return, "ISO-8859-1", "UTF-8");
[551]238
239        }
240       
[1160]241        public final function getPhotoLdap( $pJid , $pLdapInternal )
[417]242        {
[1160]243                $uid = substr($pJid, 0, strpos($pJid, "@"));
[957]244
[1160]245                if( $pLdapInternal )
[417]246                {
[1885]247                        if( !$this->fileLdapInternal )
248                                return false;
249
[1160]250                        if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
[417]251                        {
[948]252                               
[1160]253                                $this->ldapConnect();
[1504]254                               
255                                if( $this->conn )
256                                {
[1900]257                                        $filter                 = "(&(phpgwaccounttype=u)(".$this->attribute."=".$uid.")(!(phpgwaccountvisible=-1)))";
258                                        $justthese              = array($this->attribute,"jpegPhoto");
[1504]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                                }
[417]273                        }
274                }
[946]275                else
[948]276                {                                       
[1885]277                        if( !$this->fileLdapExternal )
278                                return false;
279                               
[946]280                        $jabberName = substr($pJid, strpos($pJid, "@") + 1 );
[1160]281
[946]282                        if( strpos($jabberName, "/") )
283                                $jabberName = substr($jabberName, 0, strpos($jabberName, "/"));
[948]284
[1542]285                        $this->ldapConnectExternal($jabberName);
286
[1504]287                        if( $this->conn )
[946]288                        {
[1900]289                                $filter                 = "(&(phpgwaccounttype=u)(".$this->attribute."=".$uid.")(!(phpgwaccountvisible=-1)))";
290                                $justthese              = array($this->attribute,"jpegPhoto");
[1504]291                                $search                 = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
292                                $get_entries    = ldap_get_entries($this->conn,$search);
[1160]293                               
[1504]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                                }
[946]304                        }
305                }
[1160]306               
[417]307                return false;
308        }
309       
[946]310        public final function getPhotoSession($pUid, $pOu)
[417]311        {
[946]312                $uid = $pUid;
313                if( strpos($pUid, "@") )
314                        $uid = substr($pUid, 0, strpos($pUid, "@"));
315               
[1530]316                require_once("../../header.session.inc.php");
317               
[946]318                if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]) )
[382]319                {
[946]320                        $photo = imagecreatefromstring($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
321
[382]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);
[946]330
331                        unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
332
[382]333                        return;
334                }
335        }
[417]336}
[382]337
[1160]338// Applet - utilizando o serviço Voip;
[1130]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
[1160]347// Applet - fotos pelo applet;
[946]348if(trim($_REQUEST['javaPhoto']) != "" )
[417]349{
[946]350        $obj = new webService();
351        $jid = $_REQUEST['javaPhoto'];
[1160]352        $jid = ( strpos($jid, "/") !== false ) ? substr($jid, 0, strpos($jid, "/")) : $jid;
[417]353       
[1160]354        $photo = $obj->getPhotoLdap( $jid, true );
[946]355        $photoWidth = 70;
356        $photoHeight = 90;
357        $newImage = imagecreatetruecolor($photoWidth,$photoHeight);             
358
359        if( $photo )
[382]360        {
[946]361                $photo = imagecreatefromstring($photo);
362                imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
[382]363        }
[417]364        else
365        {
[986]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                }
[417]377        }
[946]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));
[417]385}
[382]386
[1160]387// Applet - jid;
388if(trim($_REQUEST['jid']) != "")
[551]389{
[1160]390       
391        $jid = trim($_REQUEST['jid']);
392        $charset = trim($_REQUEST['charset']);
[946]393        $obj = new webService();
[551]394       
[946]395        printf("%s",$obj->getNameOrganization($jid, $charset));
[551]396}
[946]397
[1160]398// Php - fotos pelo php;
[946]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
[1504]408?>
Note: See TracBrowser for help on using the repository browser.