source: branches/2.2/expressoMail1_2/inc/class.dynamic_contacts.inc.php @ 4901

Revision 4901, 6.4 KB checked in by roberto.santosjunior, 13 years ago (diff)

Ticket #2031 - Contato Dinâmico, trat. dos contatos antigos e espaços entre nomes

Line 
1<?php
2/**********************************************************************************\
3        * ExpressoMail1_2                                                                                                  *
4        * by Gustavo Sandini Linden (gustavo.linden@serpro.gov.br)                                                 *
5        * ---------------------------------------------------------------------------------*
6        *  This program is free software; you can redistribute it and/or modify it         *
7        *  under the terms of the GNU General Public License as published by the           *
8        *  Free Software Foundation; either version 2 of the License, or (at your          *
9        *  option) any later version.                                                                                              *
10        \**********************************************************************************/
11       
12include_once('class.db_functions.inc.php');
13include_once('class.functions.inc.php');
14       
15        /**
16         * dynamic_contacts - User's dynamic contact class
17         * @package dynamic_contacts
18         * @author Gustavo S. Linden
19         * @copyright 2008 - Gustavo S. Linden
20         *
21         * TODO: Check if email already exists in contactcenter!!
22         *
23         */     
24        class dynamic_contacts
25        {
26                public $contacts;
27                public $db;
28                public $number_of_contacts;
29                public $functions;
30               
31                /**
32        * Constructor
33        *
34        */
35                function __construct()
36                {
37                        $this->db = new db_functions();
38                        $this->contacts = $this->db->get_dynamic_contacts();
39                        $this->number_of_contacts = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'];
40                        $this->functions = new functions();
41                }
42               
43                /**
44                * get dynamic contacts
45                *
46                * @return array $contacts The list of fields to be returned. The format is:
47                                $contacts = array(
48                                        'timestamp' => '1220558565',
49                                        'email => 'some@email'
50                                );
51                */
52                function get_dynamic_contacts()
53                {
54                        return $this->contacts; 
55                }
56               
57                /**
58                * get number of contacts
59                *
60                * @return int $number_of_contacts
61                * maximum number of contact allowed by administration
62                * if not set return undefined 
63                */
64                function get_number_of_contacts()
65                {
66                        return $this->number_of_contacts; 
67                }
68       
69                /**
70                * get dynamic contacts in string format
71                *
72                * @return string $contact of contacts in format ';some@email,;other@email,'
73                * this is used in js/DropDownContacts.js 
74                */
75                function dynamic_contact_toString()
76                {
77                        $contact='';
78                        if($this->contacts)
79                        {
80                                foreach($this->contacts as $item => $valor)
81                                        //
82                                        if (strstr($this->contacts[$item]['email'], '#')){
83                                                $contact .= str_replace("#",";",$this->contacts[$item]['email']) . ',';
84                                        }else{
85                                                $contact .= ';'.$this->contacts[$item]['email'] . ',';
86                                        }       
87                                       
88                                //Retira ultima virgula.
89                                $contact = substr($contact,0,(strlen($contact) - 1));
90                                return $contact;
91                        }
92                        else
93                                return false;
94                }
95               
96                /**
97                * add dynamic contacts in database
98                *
99                * this function runs thru the array and check if the email sent has new contacts
100                * or not. If the contact is new, insert new array $contact in array $contacts else
101                * search existing contact to update timestamp.
102                *
103                * @param string $full_email_address to add eg. 'some@address,other@email,email@address'
104                * @return array $contacts The list of fields to be returned. The format is:
105                                $contacts = array(
106                                        'timestamp' => '1220558565',
107                                        'email => 'some@email'
108                                );
109                * this is used in inc/class.db_function.inc.php to insert/update in database 
110                */
111                function add_dynamic_contacts($full_email_address)
112                {
113                        // Trim all whitespaces and duplicated commas from full_email_address
114                        //$full_email_address = preg_replace('{(,)\1+}',',',ereg_replace( ' +', '', $full_email_address));
115                        $parse_address = imap_rfc822_parse_adrlist($full_email_address, "");
116                        $new_contacts = array();
117                        foreach ($parse_address as $val)
118                        {
119                                if ($val->mailbox == "INVALID_ADDRESS")
120                                        continue;
121                                if ($this->contact_exist_in_ContactCenter($val->mailbox."@".$val->host))
122                                        continue;
123
124                                if(!$this->contacts) // Used one time to insert the first contact in database
125                                {
126                                        $this->db->insert_contact(ltrim(rtrim($val->personal))."#".$val->mailbox."@".$val->host);
127                                        // Just new contact added.
128                                        $new_contacts[] = ltrim(rtrim($val->personal)).";".$val->mailbox."@".$val->host;
129                                        $this->contacts = $this->db->get_dynamic_contacts();
130                                }
131                                else
132                                {
133                                        $older_contact_time=time();
134                                        $new_contact_flag = true; // Assume that all email are new in dynamic contact
135                                        foreach($this->contacts as $item => $valor)
136                                        {
137                                                if($this->contacts[$item]['email'] == ltrim(rtrim($val->personal))."#".$val->mailbox."@".$val->host) // check if email already exists
138                                                {       
139                                                        $this->contacts[$item]['timestamp'] = time(); //update timestamp of email
140                                                        $new_contact_flag = false; //email exist!
141                                                }
142                                                if($this->contacts[$item]['timestamp'] < $older_contact_time) //search for oldest email
143                                                {
144                                                        $older_contact = $item;
145                                                        $older_contact_time = $this->contacts[$item]['timestamp'];
146                                                }
147                                        }
148                                        if ($new_contact_flag == true) //new contact!
149                                        {
150                                                // Just new contact added.
151                                                $new_contacts[] = ltrim(rtrim($val->personal)).";".$val->mailbox."@".$val->host;
152                                                if($this->number_of_contacts > count($this->contacts))
153                                                {
154                                                        $this->contacts[] = array( 'timestamp'  => time(),
155                                                                                                                'email'         => ltrim(rtrim($val->personal))."#".$val->mailbox."@".$val->host);
156                                                }
157                                                if($this->number_of_contacts <= count($this->contacts))
158                                                {
159                                                        $this->contacts[$older_contact] = array( 'timestamp'    => time(),
160                                                                                                                                                'email'         => ltrim(rtrim($val->personal))."#".$val->mailbox."@".$val->host);
161                                                }
162                                        }
163                                }
164                        }
165                        $this->db->update_contacts($this->contacts);
166                        return implode(",;",$new_contacts);
167                }
168               
169                /**
170                * Verify if contact exist in ContactCenter
171                *
172                * this function gets an email and check if the email sent is already on users's personal contacts
173                * or not. 
174                *
175                * @param string $full_email_address to add eg. 'some@address,other@email,email@address'
176                * @return boolean 
177                */
178                function contact_exist_in_ContactCenter($email)
179                {
180                        $contactcenter_string = $this->db->get_cc_contacts();
181                        $cc_email = explode(",",$contactcenter_string);
182                        foreach ($cc_email as $item => $valor)
183                        {
184                                $aux = explode(";",$cc_email[$item]);
185                                if($email == $aux[1])
186                                {
187                                        return true;   
188                                }
189                                 
190                        }
191                        return false;
192                }
193               
194                /**
195                * delete dynamic contacts from database
196                *
197                * This function removes the dynamic contacts of the current user from the database.
198                * It uses inc/class.db_function.inc.php to remove.
199                *
200                */
201                function delete_dynamic_contacts()
202                {
203                        $this->db->remove_dynamic_contact($this->db->user_id,__LINE__,__FILE__);
204                }
205        }
206?>
Note: See TracBrowser for help on using the repository browser.