source: tags/expresso/2.0.0.rc1/jabberit_messenger/inc/webservice.php @ 1139

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