source: trunk/expressoMail1_2/inc/class.dynamic_contacts.inc.php @ 413

Revision 413, 5.9 KB checked in by rafaelraymundo, 16 years ago (diff)

Melhoria implementada, relativo a ocorrencia #314 da comunidade.
Contatos dinamicos no envio de emails de forma configuravel.

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       
12$GLOBALS['phpgw_info']['flags'] = array(
13                'currentapp' => 'expressoMail1_2',
14                'noheader'   => True,
15                'nonavbar'   => True,
16                'enable_nextmatchs_class' => True
17        );
18
19include('../header.inc.php');
20include_once('class.db_functions.inc.php');
21include_once('class.functions.inc.php');
22       
23        /**
24         * dynamic_contacts - User's dynamic contact class
25         * @package dynamic_contacts
26         * @author Gustavo S. Linden
27         * @copyright 2008 - Gustavo S. Linden
28         *
29         * TODO: Check if email already exists in contactcenter!!
30         *
31         */     
32        class dynamic_contacts
33        {
34                public $contacts;
35                public $db;
36                public $number_of_contacts;
37                public $functions;
38               
39                /**
40        * Constructor
41        *
42        */
43                function __construct()
44                {
45                        $this->db = new db_functions();
46                        $this->contacts = $this->db->get_dynamic_contacts();
47                        $this->number_of_contacts = $_SESSION['phpgw_info']['server']['expressomail']['expressoMail_Number_of_dynamic_contacts'];
48                        $this->functions = new functions();
49                }
50               
51                /**
52                * get dynamic contacts
53                *
54                * @return array $contacts The list of fields to be returned. The format is:
55                                $contacts = array(
56                                        'timestamp' => '1220558565',
57                                        'email => 'some@email'
58                                );
59                */
60                function get_dynamic_contacts()
61                {
62                        return $this->contacts; 
63                }
64               
65                /**
66                * get number of contacts
67                *
68                * @return int $number_of_contacts
69                * maximum number of contact allowed by administration
70                * if not set return undefined 
71                */
72                function get_number_of_contacts()
73                {
74                        return $this->number_of_contacts; 
75                }
76       
77                /**
78                * get dynamic contacts in string format
79                *
80                * @return string $contact of contacts in format ';some@email,;other@email,'
81                * this is used in js/DropDownContacts.js 
82                */
83                function dynamic_contact_toString()
84                {
85                        $contact='';
86                        if($this->contacts)
87                        {
88                                foreach($this->contacts as $item => $valor)
89                                        $contact .= ';'.$this->contacts[$item]['email'] . ',';
90                                //Retira ultima virgula.
91                                $contact = substr($contact,0,(strlen($contact) - 1));
92                                return $contact;
93                        }
94                        else
95                                return false;
96                }
97               
98                /**
99                * add dynamic contacts in database
100                *
101                * this function runs thru the array and check if the email sent has new contacts
102                * or not. If the contact is new, insert new array $contact in array $contacts else
103                * search existing contact to update timestamp.
104                *
105                * @param string $full_email_address to add eg. 'some@address,other@email,email@address'
106                * @return array $contacts The list of fields to be returned. The format is:
107                                $contacts = array(
108                                        'timestamp' => '1220558565',
109                                        'email => 'some@email'
110                                );
111                * this is used in inc/class.db_function.inc.php to insert/update in database 
112                */
113                function add_dynamic_contacts($full_email_address)
114                {
115                        $parse_address = imap_rfc822_parse_adrlist($full_email_address, "");
116                        foreach ($parse_address as $val)
117                        {
118                                if ($val->mailbox == "INVALID_ADDRESS")
119                                        continue;
120                                if ($this->contact_exist_in_ContactCenter($val->mailbox."@".$val->host))
121                                        continue;
122                                       
123                                if(!$this->contacts) // Used one time to insert the first contact in database
124                                {
125                                        $this->db->insert_contact($val->mailbox."@".$val->host);
126                                        $this->contacts = $this->db->get_dynamic_contacts();
127                                }
128                                else
129                                {
130                                        $older_contact_time=time();
131                                        $new_contact_flag = true; // Assume that all email are new in dynamic contact
132                                        foreach($this->contacts as $item => $valor)
133                                        {
134                                                if($this->contacts[$item]['email'] == $val->mailbox."@".$val->host) // check if email already exists
135                                                {       
136                                                        $this->contacts[$item]['timestamp'] = time(); //update timestamp of email
137                                                        $new_contact_flag = false; //email exist!
138                                                }
139                                                if($this->contacts[$item]['timestamp'] < $older_contact_time) //search for oldest email
140                                                        {
141                                                                $older_contact = $item;
142                                                                $older_contact_time = $this->contacts[$item]['timestamp'];
143                                                        }
144                                        }
145                                        if ($new_contact_flag == true) //new contact!
146                                        {
147                                                if($this->number_of_contacts > count($this->contacts))
148                                                {
149                                                        $this->contacts[] = array( 'timestamp'  => time(),
150                                                                                                                'email'         => $val->mailbox."@".$val->host);
151                                                }
152                                                if($this->number_of_contacts <= count($this->contacts))
153                                                {
154                                                        $this->contacts[$older_contact] = array( 'timestamp'    => time(),
155                                                                                                                                                'email'         => $val->mailbox."@".$val->host);
156                                                }
157                                        }
158                                }
159                        }
160                        $this->db->update_contacts($this->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.