source: branches/2.0/expressoMail1_2/inc/class.db_functions.inc.php @ 2092

Revision 2092, 14.1 KB checked in by niltonneto, 14 years ago (diff)

Ticket #923 - Corrigido problema do unset da variável 'db', feita pelo Workflow.

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