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

Revision 1130, 10.3 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");
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
102                if( $fromNumber && $toNumber )
103                {
104                        $fromNumber = substr($fromNumber,strlen($fromNumber) - 4, strlen($fromNumber) - 1);
105                        $toNumber       = substr($toNumber,strlen($toNumber) - 4, strlen($toNumber) - 1);
106
107                        $voipServer     = "10.15.151.106";
108                        $voipUrl        = "/telefoniaip/servicos/voip.php";
109                        $voipPort       = "80";
110       
111                        if( !$voipServer || !$voipUrl || !$voipPort )
112                                return false;
113                       
114                        $url            = "http://".$voipServer.":".$voipPort.$voipUrl."?magic=1333&acao=liga&ramal=".$fromNumber."&numero=".$toNumber;                 
115                        $sMethod        = 'GET ';
116                        $crlf           = "\r\n";
117                        $sRequest       = " HTTP/1.1" . $crlf;
118                        $sRequest       .= "Host: localhost" . $crlf;
119                        $sRequest       .= "Accept: */* " . $crlf;
120                        $sRequest       .= "Connection: Close" . $crlf . $crlf;           
121                        $sRequest       = $sMethod . $url . $sRequest;   
122                        $sockHttp       = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);           
123                       
124                        if ( !$sockHttp )
125                            return false;
126                       
127                        $resSocketConnect = socket_connect($sockHttp, $voipServer, $voipPort);
128                       
129                        if ( !$resSocketConnect )
130                            return false;
131       
132                        $resSocketWrite = socket_write($sockHttp, $sRequest, strlen($sRequest));
133       
134                        if ( !$resSocketWrite )
135                            return false;
136           
137                        $sResponse = '';   
138       
139                        while ($sRead = socket_read($sockHttp, 512))
140                        {
141                            $sResponse .= $sRead;
142                        }           
143                       
144                        socket_close($sockHttp);           
145                       
146                        $pos = strpos($sResponse, $crlf . $crlf);
147                       
148                        return substr($sResponse, $pos + 2 * strlen($crlf));
149                }
150               
151                return "ERRO";                                                                 
152        }
153       
154       
155        public final function getNameOrganization($pJid, $pCharset)
156        {
157                $uid = substr($pJid, 0, strpos($pJid,"@"));
158               
159                if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
160                {
161                        $this->ldapConnect();
162                       
163                        if( $this->conn )
164                        {
165                                $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
166                                $justthese = array("uid","cn","dn");
167                                $search = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
168                                $entry = ldap_get_entries($this->conn,$search);
169                                $cn = $entry[0]['cn'][0];
170                                $ou = explode("dc=", $entry[0]['dn']);
171                                $ou = explode("ou=",$ou[0]);
172                                $ou = array_pop($ou);
173                                $dn = strtoupper(substr($ou,0,strlen($ou)-1));
174       
175                                $return = utf8_encode("Nome : " . $cn . ";Organização : " . $dn);
176                        }
177                }
178                else
179                {
180                        $this->ldapConnectExternal(substr($pJid, strpos($pJid, "@") + 1 ));
181
182                        if( $this->conn )
183                        {
184                                $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
185                                $justthese = array("uid","cn","dn");
186                                $search = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
187                                $entry = ldap_get_entries($this->conn,$search);
188                                $cn = $entry[0]['cn'][0];
189                                $ou = explode("dc=", $entry[0]['dn']);
190                                $ou = explode("ou=",$ou[0]);
191                                $ou = array_pop($ou);
192                                $dn = strtoupper(substr($ou,0,strlen($ou)-1));
193       
194                                $return = utf8_encode("Nome : " . $cn . ";Organização : " . $dn);
195                        }
196                }
197
198                if( $pCharset === "1" )
199                        return $return;
200                else
201                        return mb_convert_encoding($return, "ISO-8859-1", "UTF-8");
202
203                $return = utf8_encode("Nome : Não Identificado ;Organização : Não Identificado");                       
204
205                if( $pCharset === 1 )
206                        return $return;
207                else
208                        return mb_convert_encoding($return, "ISO-8859-1", "UTF-8");
209
210        }
211       
212        public final function getPhotoLdap( $pJid, $pCatalog )
213        {
214                if ( ! $this->jabberName )
215                        return false;
216
217                $uid = substr($pJid, 0, strpos($pJid,"@"));
218               
219                if( ($this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 ))) && $pCatalog)
220                {
221                        $this->ldapConnect();
222                       
223                        if( $this->conn )
224                        {
225                                $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
226                                $justthese = array("uid","jpegPhoto");
227                               
228                                $search = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
229                                $entry1 = ldap_get_entries($this->conn,$search);
230                                $entry = ldap_first_entry( $this->conn, $search );
231                                if( $entry1['count'] > 0 )
232                                {
233                                        $i = 0;
234                                        $photo = "";
235                                        $return_photo = "";
236                                        $photo = @ldap_get_values_len($this->conn, $entry, 'jpegphoto');
237
238                                        if ($photo)
239                                                $return_photo = $photo[0];                                                             
240                                        return $return_photo;
241                                }
242                        }
243                }
244                else
245                {                                       
246                        $jabberName = substr($pJid, strpos($pJid, "@") + 1 );
247                       
248                        if( strpos($jabberName, "/") )
249                                $jabberName = substr($jabberName, 0, strpos($jabberName, "/"));
250
251                        $this->ldapConnectExternal($jabberName);
252
253                        if( $this->conn )
254                        {
255                                $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
256                                $justthese = array("uid","jpegPhoto");
257                                $search = ldap_search($this->conn,$this->contextLdap,$filter,$justthese);
258                                $entry1 = ldap_get_entries($this->conn,$search);
259                                $entry = ldap_first_entry( $this->conn, $search );
260                                if( $entry1['count'] > 0 )
261                                {
262                                        $i = 0;
263                                        $photo = "";
264                                        $return_photo = "";
265                                        $photo = @ldap_get_values_len($this->conn, $entry, 'jpegphoto');
266                                        if ($photo)
267                                                $return_photo = $photo[0];                                                             
268                                        return $return_photo;
269                                }
270                        }
271                }
272                                       
273                return false;
274        }
275       
276        public final function getPhotoSession($pUid, $pOu)
277        {
278                $uid = $pUid;
279                if( strpos($pUid, "@") )
280                        $uid = substr($pUid, 0, strpos($pUid, "@"));
281               
282                if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]) )
283                {
284                        $photo = imagecreatefromstring($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
285
286                        header("Content-Type: image/jpeg");
287                        $width = imagesx($photo);
288                        $height = imagesy($photo);
289                        $twidth = 60;
290                        $theight = 80;
291                        $small_photo = imagecreatetruecolor ($twidth, $theight);
292                        imagecopyresampled($small_photo, $photo, 0, 0, 0, 0,$twidth, $theight, $width, $height);
293                        imagejpeg($small_photo,'',100);
294
295                        unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
296
297                        return;
298                }
299        }
300}
301
302// Utilizando o serviço Asteriks;
303if(trim($_REQUEST['javaVoipFrom']) != "" && trim($_REQUEST['javaVoipTo']) != "" )
304{
305        $obj = new webService();
306        $voipFrom = $_REQUEST['javaVoipFrom'];
307        $voipTo = $_REQUEST['javaVoipTo'];
308        printf("%s",$obj->CallVoipConnect($voipFrom, $voipTo));
309}
310
311// Fotos pelo applet;
312if(trim($_REQUEST['javaPhoto']) != "" )
313{
314        $obj = new webService();
315        $jid = $_REQUEST['javaPhoto'];
316        $jid = strpos($jid, "/") ? substr($jid, 0, strpos($jid, "/")) : $jid;
317       
318        $photo = $obj->getPhotoLdap($jid, true);
319        $photoWidth = 70;
320        $photoHeight = 90;
321        $newImage = imagecreatetruecolor($photoWidth,$photoHeight);             
322
323        if( $photo )
324        {
325                $photo = imagecreatefromstring($photo);
326                imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
327        }
328        else
329        {
330                $photo = $obj->getPhotoLdap($jid, false);
331                if( $photo )
332                {
333                        $photo = imagecreatefromstring($photo);
334                        imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
335                }
336                else
337                {
338                        $photo = @imagecreatefrompng("../templates/default/images/photo.png");
339                        imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
340                }
341        }
342       
343        ob_start();
344        imagepng($newImage);
345        $imagePhoto = ob_get_contents();
346        imagedestroy($newImage);
347        ob_end_clean();
348        printf("%s",base64_encode($imagePhoto));
349       
350}
351
352// Jid pelo applet;
353if(trim($_POST['jid']) != "")
354{
355        $jid = trim($_POST['jid']);
356        $charset = trim($_POST['charset']);
357        $obj = new webService();
358       
359        printf("%s",$obj->getNameOrganization($jid, $charset));
360}
361
362// Fotos pelo php;
363if(trim($_REQUEST['phpPhoto']) != "")
364{
365        $obj = new webservice();
366        $ou = $_REQUEST['phpOu'];
367        $jid = $_REQUEST['phpPhoto'];
368       
369        $obj->getPhotoSession($jid, $ou);
370}
371
372?>
Note: See TracBrowser for help on using the repository browser.