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

Revision 1308, 13.3 KB checked in by amuller, 15 years ago (diff)

Ticket #475 - Revertendo código usado para fora de escritório

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