source: trunk/expressoMail1_2/inc/class.db_functions.inc.php @ 2360

Revision 2360, 15.1 KB checked in by amuller, 14 years ago (diff)

Ticket #1008 - Adicionando informações sobre licenças

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2
3/***************************************************************************
4        * Expresso Livre                                                           *
5        * http://www.expressolivre.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        \**************************************************************************/
12if(!isset($_SESSION['phpgw_info']['expressomail']['server']['db_name'])) {
13        include_once('./header.inc.php');
14        $_SESSION['phpgw_info']['expressomail']['server']['db_name'] = $GLOBALS['phpgw_info']['server']['db_name'];
15        $_SESSION['phpgw_info']['expressomail']['server']['db_host'] = $GLOBALS['phpgw_info']['server']['db_host'];
16        $_SESSION['phpgw_info']['expressomail']['server']['db_port'] = $GLOBALS['phpgw_info']['server']['db_port'];
17        $_SESSION['phpgw_info']['expressomail']['server']['db_user'] = $GLOBALS['phpgw_info']['server']['db_user'];
18        $_SESSION['phpgw_info']['expressomail']['server']['db_pass'] = $GLOBALS['phpgw_info']['server']['db_pass'];
19        $_SESSION['phpgw_info']['expressomail']['server']['db_type'] = $GLOBALS['phpgw_info']['server']['db_type'];
20}
21else{
22        define('PHPGW_INCLUDE_ROOT','./');
23        define('PHPGW_API_INC','./phpgwapi/inc');
24        include_once(PHPGW_API_INC.'/class.db.inc.php');
25}
26include_once('class.dynamic_contacts.inc.php');
27       
28class db_functions
29{       
30       
31        var $db;
32        var $user_id;
33        var $related_ids;
34       
35        function db_functions(){
36                $this->db = new db();           
37                $this->db->Halt_On_Error = 'no';
38                $this->db->connect(
39                                $_SESSION['phpgw_info']['expressomail']['server']['db_name'],
40                                $_SESSION['phpgw_info']['expressomail']['server']['db_host'],
41                                $_SESSION['phpgw_info']['expressomail']['server']['db_port'],
42                                $_SESSION['phpgw_info']['expressomail']['server']['db_user'],
43                                $_SESSION['phpgw_info']['expressomail']['server']['db_pass'],
44                                $_SESSION['phpgw_info']['expressomail']['server']['db_type']
45                );             
46                $this -> user_id = $_SESSION['phpgw_info']['expressomail']['user']['account_id'];       
47        }
48
49        // BEGIN of functions.
50        function get_cc_contacts()
51        {                               
52                $result = array();
53                $stringDropDownContacts = '';           
54               
55                $query_related = $this->get_query_related('A.id_owner'); // field name for owner
56                       
57                // Traz os contatos pessoais e compartilhados
58                $query = 'select A.names_ordered, C.connection_value from phpgw_cc_contact A, '.
59                        'phpgw_cc_contact_conns B, phpgw_cc_connections C where '.
60                        'A.id_contact = B.id_contact and B.id_connection = C.id_connection '.
61                        'and B.id_typeof_contact_connection = 1 and ('.$query_related.') group by '.
62                        'A.names_ordered,C.connection_value     order by lower(A.names_ordered)';
63               
64        if (!$this->db->query($query))
65                return null;
66                while($this->db->next_record())
67                        $result[] = $this->db->row();
68
69                if (count($result) != 0)
70                {
71                        // Monta string                         
72                        foreach($result as $contact)
73                                $stringDropDownContacts = $stringDropDownContacts . urldecode(urldecode($contact['names_ordered'])). ';' . $contact['connection_value'] . ',';
74                        //Retira ultima virgula.
75                        $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1));
76                }
77                else
78                        return null;
79
80                return $stringDropDownContacts;
81        }
82        // Get Related Ids for sharing contacts or groups.
83        function get_query_related($field_name){               
84                $query_related = $field_name .'='.$this -> user_id;
85                // Only at first time, it gets all related ids...
86                if(!$this->related_ids) {
87                        $query = 'select id_related from phpgw_cc_contact_rels where id_contact='.$this -> user_id.' and id_typeof_contact_relation=1';         
88                        if (!$this->db->query($query)){
89                return $query_related;
90                        }
91                       
92                        $result = array( );
93                        while($this->db->next_record()){
94                                $row = $this->db->row();
95                                $result[] = $row['id_related'];
96                        }
97                        if($result)
98                                $this->related_ids = implode(",",$result);
99                }
100                if($this->related_ids)
101                        $query_related .= ' or '.$field_name.' in ('.$this->related_ids.')';
102               
103                return $query_related;
104        }
105        function get_cc_groups()
106        {
107                // Pesquisa no CC os Grupos Pessoais.
108                $stringDropDownContacts = '';                   
109                $result = array();
110                $query_related = $this->get_query_related('owner'); // field name for 'owner'           
111                $query = 'select title, short_name, owner from phpgw_cc_groups where '.$query_related.' order by lower(title)';
112
113                // Executa a query
114                if (!$this->db->query($query))
115                return null;
116                // Retorna cada resultado               
117                while($this->db->next_record())
118                        $result[] = $this->db->row();
119
120                // Se houver grupos ....                               
121                if (count($result) != 0)
122                {
123                        // Create Ldap Object, if exists related Ids for sharing groups.
124                        if($this->related_ids){
125                                $_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids']= array();
126                                include_once("class.ldap_functions.inc.php");
127                                $ldap = new ldap_functions();
128                        }
129                        $owneruid = '';
130                        foreach($result as $group){
131                                // Searching uid (LDAP), if exists related Ids for sharing groups.
132                                // Save into user session. It will used before send mail (verify permission).
133                                if(!isset($_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'][$group['owner']]) && isset($ldap)){                                 
134                                        $_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'][$group['owner']] = $ldap -> uidnumber2uid($group['owner']);
135                                }
136                                if($this->user_id != $group['owner'])
137                                        $owneruid = "::".$_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'][$group['owner']];
138                                else
139                                        $owneruid = '';
140
141                                $stringDropDownContacts .=  $group['title']. ';' . ($group['short_name'].$owneruid) . ',';
142                        }
143                        //Retira ultima virgula.
144                        $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1));
145                }
146                else
147                        return null;           
148                return $stringDropDownContacts;
149        }
150       
151        function getContactsByGroupAlias($alias)
152        {
153                list($alias,$uid) = explode("::",$alias);               
154                $cc_related_ids = $_SESSION['phpgw_info']['expressomail']['user']['cc_related_ids'];           
155                // Explode personal group, If exists related ids (the user has permission to send email).
156                if(is_array($cc_related_ids) && $uid){
157                        $owner =  array_search($uid,$cc_related_ids);                   
158                }
159               
160                $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ".
161                "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ".
162                "A.id_contact = B.id_contact and B.id_connection = C.id_connection ".
163                "and B.id_typeof_contact_connection = 1 and ".
164                "A.id_owner =".($owner ? $owner : $this->user_id)." and ".                     
165                "D.id_group = E.id_group and ".
166                "D.id_connection = C.id_connection and E.short_name = '".$alias."'";
167
168                if (!$this->db->query($query))
169                {
170                        exit ('Query failed! File: '.__FILE__.' on line'.__LINE__);
171                }
172
173                $return = false;
174
175                while($this->db->next_record())
176                {
177                        $return[] = $this->db->row();
178                }
179
180                return $return;
181        }
182
183        function getAddrs($array_addrs) {
184                $array_addrs_final = array();                           
185
186                for($i = 0; $i < count($array_addrs); $i++){
187                        $j = count($array_addrs_final);
188
189                        if(!strchr($array_addrs[$i],'@')
190                                        && strchr($array_addrs[$i],'<')
191                                         && strchr($array_addrs[$i],'>')) {             
192
193                                $alias = substr($array_addrs[$i], strpos($array_addrs[$i],'<'), strpos($array_addrs[$i],'>'));                         
194                                $alias = str_replace('<','', str_replace('>','',$alias));
195                                                                                       
196                                $arrayContacts = $this -> getContactsByGroupAlias($alias);
197
198                                if($arrayContacts) {
199                                        foreach($arrayContacts as $index => $contact){
200                                                if($contact['names_ordered']) {
201                                                        $array_addrs_final[$j] = '"'.$contact['names_ordered'].'" <'.$contact['connection_value'].'>';
202                                                }
203                                                else
204                                                        $array_addrs_final[$j] = $contact['connection_value'];
205
206                                                $j++;
207                                        }
208                                }
209                        }
210                        else
211                                $array_addrs_final[$j++] = $array_addrs[$i];                                                   
212                }
213                return $array_addrs_final;
214        }
215
216        //Gera lista de contatos para ser gravado e acessado pelo expresso offline.
217        function get_dropdown_contacts_to_cache() {
218                return $this->get_dropdown_contacts();
219        }
220       
221        function get_dropdown_contacts(){
222               
223                $contacts = $this -> get_cc_contacts();
224                $groups = $this -> get_cc_groups();
225               
226                if(($contacts) && ($groups))
227                        $stringDropDownContacts = $contacts . ',' . $groups;
228                elseif ((!$contacts) && (!$groups))
229                        $stringDropDownContacts = '';
230                elseif (($contacts) && (!$groups))
231                        $stringDropDownContacts = $contacts;
232                elseif ((!$contacts) && ($groups))
233                        $stringDropDownContacts = $groups;
234                                       
235                if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'] &&
236                        $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_dynamic_contacts']) {
237                        $dynamic_contact = new dynamic_contacts();
238                        $dynamic = $dynamic_contact->dynamic_contact_toString();
239                        if ($dynamic)
240                                $stringDropDownContacts .= ($stringDropDownContacts ? ',' : '') . $dynamic;
241                }
242                return $stringDropDownContacts;
243        }
244        function getUserByEmail($params){       
245                // Follow the referral
246                $email = $params['email'];
247                $query = 'select A.names_ordered, C.connection_name, C.connection_value, A.photo'.
248                                ' from phpgw_cc_contact A, phpgw_cc_contact_conns B, '.
249                                'phpgw_cc_connections C where A.id_contact = B.id_contact'.
250                                ' and B.id_connection = C.id_connection and A.id_contact ='.
251                                '(select A.id_contact from phpgw_cc_contact A, phpgw_cc_contact_conns B,'.
252                                'phpgw_cc_connections C where A.id_contact = B.id_contact'.
253                                ' and B.id_connection = C.id_connection and A.id_owner = '.$this -> user_id.
254                                ' and C.connection_value = \''.$email.'\') and '.
255                                'C.connection_is_default = true and B.id_typeof_contact_connection = 2';
256
257        if (!$this->db->query($query))
258                return null;
259
260
261                if($this->db->next_record()) {
262                        $result = $this->db->row();
263
264                        $obj =  array("cn" => $result['names_ordered'],
265                                          "email" => $email,
266                                          "type" => "personal",
267                                          "telefone" =>  $result['connection_value']);
268
269                        if($result['photo'])
270                                $_SESSION['phpgw_info']['expressomail']['contact_photo'] =  array($result['photo']);                           
271
272                        return $obj;
273                }
274                return $result;
275        }
276       
277        function get_dynamic_contacts()
278        {                               
279                // Pesquisa os emails e ultima inserção nos contatos dinamicos.
280                if(!$this->db->select('phpgw_expressomail_contacts','data',
281                                                  'id_owner ='.$this -> user_id,
282                                                  __LINE__,__FILE__))
283                {
284                return $this->db->Error;
285}
286                while($this->db->next_record())
287                {
288                        $result[] = $this->db->row();
289                }
290                if($result) foreach($result as $item)
291                {
292                        $contacts = unserialize($item['data']);
293                }
294                if (count($contacts) == 0)
295                {                       
296                        return null;
297                }       
298                //Sort by email
299                function cmp($a, $b) { return strcmp($a["email"], $b["email"]);}
300                usort($contacts,"cmp");
301                return $contacts;
302        }
303        function update_contacts($contacts=array())
304        {                       
305                // Atualiza um email nos contatos dinamicos.
306                if(!$this->db->update('phpgw_expressomail_contacts ','data=\''.serialize($contacts).'\'',
307                        'id_owner ='.$this -> user_id,
308                        __LINE__,__FILE__))
309                {
310                        return $this->db->Error;
311                }
312                return $contacts;
313        }       
314        function update_preferences($params){
315                $string_serial = urldecode($params['prefe_string']);                           
316                $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial);
317                $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'".
318                        " and preference_owner = '".$this->user_id."'";
319
320                if (!$this->db->query($query))
321                        return $this->db->error;
322                else
323                        return array("success" => true);
324        }
325       
326        function insert_contact($contact)       
327        {
328                $contacts[] = array( 'timestamp'        => time(),
329                                                                'email'         => $contact );
330
331                // Insere um email nos contatos dinamicos.     
332                $query = 'INSERT INTO phpgw_expressomail_contacts (data, id_owner)  ' .
333                                        'values ( \''.serialize($contacts).'\', '.$this->user_id.')';
334               
335                if(!$this->db->query($query,__LINE__,__FILE__))
336                return $this->db->Error;
337        return $contacts;
338        }
339       
340        function remove_dynamic_contact($user_id,$line,$file)
341        {
342                $where = $user_id.' = id_owner';
343                $this->db->delete('phpgw_expressomail_contacts',$where,$line,$file);   
344        }
345       
346        function import_vcard($params){
347                       
348                include_once('class.imap_functions.inc.php');
349                $objImap = new imap_functions();
350                $msg_number = $params['msg_number'];
351                $idx_file = $params['idx_file'];
352                $msg_part = $params['msg_part'];
353                $msg_folder = $params['msg_folder'];
354                $encoding = strtolower($params['encoding']);
355                $fileContent = "";
356
357                if($msg_number && $msg_part && $msg_folder && (intval($idx_file == '0' ? '1' : $idx_file))) {
358                        $mbox_stream = $objImap->open_mbox($msg_folder);       
359                        $fileContent = imap_fetchbody($mbox_stream, $msg_number, $msg_part, FT_UID);
360                        include_once('class.imap_attachment.inc.php');
361                        $imap_attachment = new imap_attachment();
362                        $a = $imap_attachment->download_attachment($mbox_stream, $msg_number);
363                        $filename = $a[$idx_file]['name'];
364                }
365                else
366                        $filename = $idx_file;
367                                       
368                if($fileContent) {
369                        if($encoding == 'base64')
370                                $calendar = imap_base64($fileContent);
371                        else if($encoding == 'quoted-printable')
372                                $calendar = quoted_printable_decode($fileContent);
373                        else
374                                $calendar = $fileContent;
375                }
376                // It's necessary to access calendar method.
377                include_once(PHPGW_INCLUDE_ROOT.'/header.inc.php');             
378                $uiicalendar = CreateObject("calendar.uiicalendar");
379                return $uiicalendar = $uiicalendar->import_from_mail($calendar);
380        }
381
382    function insert_certificate($email,$certificate,$serialnumber,$authoritykeyidentifier=null)
383        {
384                if(!$email || !$certificate || !$serialnumber || !$authoritykeyidentifier)
385                        return false;
386                // Insere uma chave publica na tabela phpgw_certificados.
387                $data = array   ('email' => $email,
388                                                 'chave_publica' => $certificate,
389                                                 'serialnumber' => $serialnumber,
390                                                 'authoritykeyidentifier' => $authoritykeyidentifier);
391
392                if(!$this->db->insert('phpgw_certificados',$data,array(),__LINE__,__FILE__)){
393                return $this->db->Error;
394        }
395        return true;
396        }
397
398        function get_certificate($email=null)
399        {
400                if(!$email) return false;
401                $result = array();
402
403                $where = array ('email' => $email,
404                                                'revogado' => 0,
405                                                'expirado' => 0);
406
407                if(!$this->db->select('phpgw_certificados','chave_publica', $where, __LINE__,__FILE__))
408        {
409            $result['dberr1'] = $this->db->Error;
410            return $result;
411        }
412                $regs = array();
413                while($this->db->next_record())
414        {
415            $regs[] = $this->db->row();
416        }
417                if (count($regs) == 0)
418        {
419            $result['dberr2'] = ' Certificado nao localizado.';
420            return $result;
421        }
422                $result['certs'] = $regs;
423                return $result;
424        }
425
426        function update_certificate($serialnumber=null,$email=null,$authoritykeyidentifier,$expirado,$revogado)
427        {
428                if(!$email || !$serialnumber) return false;
429                if(!$expirado)
430                        $expirado = 0;
431                if(!$revogado)
432                        $revogado = 0;
433
434                $data = array   ('expirado' => $expirado,
435                                                 'revogado' => $revogado);
436
437                $where = array  ('email' => $email,
438                                                 'serialnumber' => $serialnumber,
439                                                 'authoritykeyidentifier' => $authoritykeyidentifier);
440
441                if(!$this->db->update('phpgw_certificados',$data,$where,__LINE__,__FILE__))
442                {
443                        return $this->db->Error;
444                }
445                return true;
446        }
447
448}
449?>
Note: See TracBrowser for help on using the repository browser.