source: companies/celepar/expressoMail1_2/inc/class.db_functions.inc.php @ 763

Revision 763, 12.8 KB checked in by niltonneto, 15 years ago (diff)

Importação inicial do Expresso da Celepar

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