source: tags/expresso/2.0.0.rc2/jabberit_messenger/inc/webservice.php @ 1198

Revision 1198, 10.9 KB checked in by niltonneto, 15 years ago (diff)

Ticket #586 - Correções para adaptar template e funcionar com API 2.0

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