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

Revision 1040, 14.9 KB checked in by amuller, 15 years ago (diff)

Ticket #559 - Atualização de download de arquivos e sessão

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