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

Revision 1135, 10.4 KB checked in by alexandrecorreia, 15 years ago (diff)

Ticket #577 - Adicionado botão "click to call" na janela de conversa, arquivos php modificados.

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