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

Revision 1059, 6.1 KB checked in by amuller, 15 years ago (diff)

Ticket #559 - Removendo inclusão de header.inc.php nas classes e colocando no controler

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                                        $contact .= ';'.$this->contacts[$item]['email'] . ',';
82                                //Retira ultima virgula.
83                                $contact = substr($contact,0,(strlen($contact) - 1));
84                                return $contact;
85                        }
86                        else
87                                return false;
88                }
89               
90                /**
91                * add dynamic contacts in database
92                *
93                * this function runs thru the array and check if the email sent has new contacts
94                * or not. If the contact is new, insert new array $contact in array $contacts else
95                * search existing contact to update timestamp.
96                *
97                * @param string $full_email_address to add eg. 'some@address,other@email,email@address'
98                * @return array $contacts The list of fields to be returned. The format is:
99                                $contacts = array(
100                                        'timestamp' => '1220558565',
101                                        'email => 'some@email'
102                                );
103                * this is used in inc/class.db_function.inc.php to insert/update in database 
104                */
105                function add_dynamic_contacts($full_email_address)
106                {
107                        // Trim all whitespaces and duplicated commas from full_email_address
108                        $full_email_address = preg_replace('{(,)\1+}',',',ereg_replace( ' +', '', $full_email_address));
109                        $parse_address = imap_rfc822_parse_adrlist($full_email_address, "");
110                        $new_contacts = array();
111                        foreach ($parse_address as $val)
112                        {
113                                if ($val->mailbox == "INVALID_ADDRESS")
114                                        continue;
115                                if ($this->contact_exist_in_ContactCenter($val->mailbox."@".$val->host))
116                                        continue;
117
118                                if(!$this->contacts) // Used one time to insert the first contact in database
119                                {
120                                        $this->db->insert_contact($val->mailbox."@".$val->host);
121                                        // Just new contact added.
122                                        $new_contacts[] = $val->mailbox."@".$val->host;
123                                        $this->contacts = $this->db->get_dynamic_contacts();
124                                }
125                                else
126                                {
127                                        $older_contact_time=time();
128                                        $new_contact_flag = true; // Assume that all email are new in dynamic contact
129                                        foreach($this->contacts as $item => $valor)
130                                        {
131                                                if($this->contacts[$item]['email'] == $val->mailbox."@".$val->host) // check if email already exists
132                                                {       
133                                                        $this->contacts[$item]['timestamp'] = time(); //update timestamp of email
134                                                        $new_contact_flag = false; //email exist!
135                                                }
136                                                if($this->contacts[$item]['timestamp'] < $older_contact_time) //search for oldest email
137                                                {
138                                                        $older_contact = $item;
139                                                        $older_contact_time = $this->contacts[$item]['timestamp'];
140                                                }
141                                        }
142                                        if ($new_contact_flag == true) //new contact!
143                                        {
144                                                // Just new contact added.
145                                                $new_contacts[] = $val->mailbox."@".$val->host;
146                                                if($this->number_of_contacts > count($this->contacts))
147                                                {
148                                                        $this->contacts[] = array( 'timestamp'  => time(),
149                                                                                                                'email'         => $val->mailbox."@".$val->host);
150                                                }
151                                                if($this->number_of_contacts <= count($this->contacts))
152                                                {
153                                                        $this->contacts[$older_contact] = array( 'timestamp'    => time(),
154                                                                                                                                                'email'         => $val->mailbox."@".$val->host);
155                                                }
156                                        }
157                                }
158                        }
159                        $this->db->update_contacts($this->contacts);
160                        return implode(",;",$new_contacts);
161                }
162               
163                /**
164                * Verify if contact exist in ContactCenter
165                *
166                * this function gets an email and check if the email sent is already on users's personal contacts
167                * or not. 
168                *
169                * @param string $full_email_address to add eg. 'some@address,other@email,email@address'
170                * @return boolean 
171                */
172                function contact_exist_in_ContactCenter($email)
173                {
174                        $contactcenter_string = $this->db->get_cc_contacts();
175                        $cc_email = explode(",",$contactcenter_string);
176                        foreach ($cc_email as $item => $valor)
177                        {
178                                $aux = explode(";",$cc_email[$item]);
179                                if($email == $aux[1])
180                                {
181                                        return true;   
182                                }
183                                 
184                        }
185                        return false;
186                }
187               
188                /**
189                * delete dynamic contacts from database
190                *
191                * This function removes the dynamic contacts of the current user from the database.
192                * It uses inc/class.db_function.inc.php to remove.
193                *
194                */
195                function delete_dynamic_contacts()
196                {
197                        $this->db->remove_dynamic_contact($this->db->user_id,__LINE__,__FILE__);
198                }
199        }
200?>
Note: See TracBrowser for help on using the repository browser.