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

Revision 1530, 10.8 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #370 - Melhoria do layout das janelas, para criação de preferencia(on/offline).

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