source: branches/2.0/jabberit_messenger/inc/webservice.php @ 1822

Revision 1822, 10.8 KB checked in by niltonneto, 14 years ago (diff)

Ticket #559 - Replicação da correção efetuada em Trunk.

  • 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 $userLDAP;     
18        private $jabberName = null;
19        private $passwd;
20        private $photo_user = array();
21        private $refer;
22        private $serverLdap;
23        private $fileLdapInternal = false;
24        private $fileLdapExternal = false;
25       
26        function __construct()
27        {
28                if ( file_exists('confLDAPInternal.php') )
29                {
30                        require_once('confLDAPInternal.php');
31                        $handle   = unserialize(base64_decode($LDAP_INTERNAL));
32       
33                        $this->jabberName       = $handle['jabberName'];
34                        $this->serverLdap       = $handle['serverLdap'];
35                        $this->contextLdap      = $handle['contextLdap'];
36                        $this->userLDAP         = $handle['user'];
37                        $this->passwd           = $handle['password'];
38                       
39                        $this->refer    = true;
40                        $this->version3 = true;
41                       
42                        $this->fileLdapInternal = true;
43                }
44        }
45       
46        private final function ldapConnect()
47        {
48                if(!function_exists('ldap_connect'))
49                        return False;
50               
51                if(!$this->conn = ldap_connect($this->serverLdap))
52                        return False;
53
54                if( $this->version3 )
55                        if( !ldap_set_option($this->conn,LDAP_OPT_PROTOCOL_VERSION,3) )
56                                $this->version = false;
57
58                ldap_set_option($this->conn, LDAP_OPT_REFERRALS, $this->refer);
59
60                // Bind as Admin
61                if( $this->userLDAP && $this->passwd && !ldap_bind($this->conn, $this->userLDAP . "," . $this->contextLdap, $this->passwd) )
62                        return False;
63               
64                // Bind as Anonymous
65                if( !$this->userLDAP && !$this->passwd && !@ldap_bind($this->conn) )
66                        return False;
67        }
68
69        private final function ldapConnectExternal($pHostJabber)
70        {
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                }
93        }
94       
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.")))";
102                        $justthese = array("telephoneNumber", "uid");
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];
108
109                        if ( trim($entry[0]['uid'][0]) !== trim($pVoipFrom) )
110                        {
111                                $fromNumber = $entry[1]['telephonenumber'][0];
112                                $toNumber = $entry[0]['telephonenumber'][0];
113                        }
114                }
115               
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                       
125                        $url            = "http://".$voipServer.":".$voipPort.$voipUrl."?magic=1333&acao=liga&ramal=".$fromNumber."&numero=".$toNumber;
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       
166        public final function getNameOrganization($pJid, $pCharset)
167        {
168                $uid = substr($pJid, 0, strpos($pJid,"@"));
169                $return = utf8_encode("Nome : Não Identificado ;Organização : Não Identificado");
170               
171                if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
172                {
173                        $this->ldapConnect();
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                }
196                else
197                {
198                        $this->ldapConnectExternal(substr($pJid, strpos($pJid, "@") + 1 ));
199                       
200                        if( $this->fileLdapExternal )
201                        {
202                                if( $this->conn )
203                                {
204                                        $filter = "(&(phpgwaccounttype=u)(uid=".$uid.")(!(phpgwaccountvisible=-1)))";
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);
208                                       
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                                }
219                        }
220                }
221
222                if( $pCharset === "1" || $pCharset === 1 )
223                        return $return;
224                else
225                        return mb_convert_encoding($return, "ISO-8859-1", "UTF-8");
226
227        }
228       
229        public final function getPhotoLdap( $pJid , $pLdapInternal )
230        {
231                $uid = substr($pJid, 0, strpos($pJid, "@"));
232
233                if( $pLdapInternal )
234                {
235                        if( $this->jabberName == (substr($pJid, strpos($pJid, "@") + 1 )))
236                        {
237                                if( ! $this->fileLdapInternal )
238                                        return false;
239                               
240                                $this->ldapConnect();
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                                }
260                        }
261                }
262                else
263                {                                       
264                        $jabberName = substr($pJid, strpos($pJid, "@") + 1 );
265
266                        if( strpos($jabberName, "/") )
267                                $jabberName = substr($jabberName, 0, strpos($jabberName, "/"));
268
269                        $this->ldapConnectExternal($jabberName);
270
271                        if( !$this->fileLdapExternal )
272                                return false;
273
274                        if( $this->conn )
275                        {
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);
280                               
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                                }
291                        }
292                }
293               
294                return false;
295        }
296       
297        public final function getPhotoSession($pUid, $pOu)
298        {
299                $uid = $pUid;
300                if( strpos($pUid, "@") )
301                        $uid = substr($pUid, 0, strpos($pUid, "@"));
302               
303                require_once("../../header.session.inc.php");
304               
305                if( isset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]) )
306                {
307                        $photo = imagecreatefromstring($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
308
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);
317
318                        unset($_SESSION['phpgw_info']['jabberit_messenger']['photo'][$pOu][$uid]);
319
320                        return;
321                }
322        }
323}
324
325// Applet - utilizando o serviço Voip;
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
334// Applet - fotos pelo applet;
335if(trim($_REQUEST['javaPhoto']) != "" )
336{
337        $obj = new webService();
338        $jid = $_REQUEST['javaPhoto'];
339        $jid = ( strpos($jid, "/") !== false ) ? substr($jid, 0, strpos($jid, "/")) : $jid;
340       
341        $photo = $obj->getPhotoLdap( $jid, true );
342        $photoWidth = 70;
343        $photoHeight = 90;
344        $newImage = imagecreatetruecolor($photoWidth,$photoHeight);             
345
346        if( $photo )
347        {
348                $photo = imagecreatefromstring($photo);
349                imagecopyresized($newImage,$photo,0,0,0,0,$photoWidth,$photoHeight,imagesx($photo),imagesy($photo));
350        }
351        else
352        {
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                }
364        }
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));
372}
373
374// Applet - jid;
375if(trim($_REQUEST['jid']) != "")
376{
377       
378        $jid = trim($_REQUEST['jid']);
379        $charset = trim($_REQUEST['charset']);
380        $obj = new webService();
381       
382        printf("%s",$obj->getNameOrganization($jid, $charset));
383}
384
385// Php - fotos pelo php;
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
395?>
Note: See TracBrowser for help on using the repository browser.