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

Revision 2012, 13.5 KB checked in by amuller, 14 years ago (diff)

Ticket #490 - unificação do connector

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