source: companies/serpro/expressoMail1_2/inc/class.dynamic_contacts.inc.php @ 903

Revision 903, 6.3 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

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_once('../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']['user']['preferences']['expressoMail']['number_of_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                        // Trim all whitespaces and duplicated commas from full_email_address
116                        $full_email_address = preg_replace('{(,)\1+}',',',ereg_replace( ' +', '', $full_email_address));
117                        $parse_address = imap_rfc822_parse_adrlist($full_email_address, "");
118                        $new_contacts = array();
119                        foreach ($parse_address as $val)
120                        {
121                                if ($val->mailbox == "INVALID_ADDRESS")
122                                        continue;
123                                if ($this->contact_exist_in_ContactCenter($val->mailbox."@".$val->host))
124                                        continue;
125
126                                if(!$this->contacts) // Used one time to insert the first contact in database
127                                {
128                                        $this->db->insert_contact($val->mailbox."@".$val->host);
129                                        // Just new contact added.
130                                        $new_contacts[] = $val->mailbox."@".$val->host;
131                                        $this->contacts = $this->db->get_dynamic_contacts();
132                                }
133                                else
134                                {
135                                        $older_contact_time=time();
136                                        $new_contact_flag = true; // Assume that all email are new in dynamic contact
137                                        foreach($this->contacts as $item => $valor)
138                                        {
139                                                if($this->contacts[$item]['email'] == $val->mailbox."@".$val->host) // check if email already exists
140                                                {       
141                                                        $this->contacts[$item]['timestamp'] = time(); //update timestamp of email
142                                                        $new_contact_flag = false; //email exist!
143                                                }
144                                                if($this->contacts[$item]['timestamp'] < $older_contact_time) //search for oldest email
145                                                {
146                                                        $older_contact = $item;
147                                                        $older_contact_time = $this->contacts[$item]['timestamp'];
148                                                }
149                                        }
150                                        if ($new_contact_flag == true) //new contact!
151                                        {
152                                                // Just new contact added.
153                                                $new_contacts[] = $val->mailbox."@".$val->host;
154                                                if($this->number_of_contacts > count($this->contacts))
155                                                {
156                                                        $this->contacts[] = array( 'timestamp'  => time(),
157                                                                                                                'email'         => $val->mailbox."@".$val->host);
158                                                }
159                                                if($this->number_of_contacts <= count($this->contacts))
160                                                {
161                                                        $this->contacts[$older_contact] = array( 'timestamp'    => time(),
162                                                                                                                                                'email'         => $val->mailbox."@".$val->host);
163                                                }
164                                        }
165                                }
166                        }
167                        $this->db->update_contacts($this->contacts);
168                        return implode(",;",$new_contacts);
169                }
170               
171                /**
172                * Verify if contact exist in ContactCenter
173                *
174                * this function gets an email and check if the email sent is already on users's personal contacts
175                * or not. 
176                *
177                * @param string $full_email_address to add eg. 'some@address,other@email,email@address'
178                * @return boolean 
179                */
180                function contact_exist_in_ContactCenter($email)
181                {
182                        $contactcenter_string = $this->db->get_cc_contacts();
183                        $cc_email = explode(",",$contactcenter_string);
184                        foreach ($cc_email as $item => $valor)
185                        {
186                                $aux = explode(";",$cc_email[$item]);
187                                if($email == $aux[1])
188                                {
189                                        return true;   
190                                }
191                                 
192                        }
193                        return false;
194                }
195               
196                /**
197                * delete dynamic contacts from database
198                *
199                * This function removes the dynamic contacts of the current user from the database.
200                * It uses inc/class.db_function.inc.php to remove.
201                *
202                */
203                function delete_dynamic_contacts()
204                {
205                        $this->db->remove_dynamic_contact($this->db->user_id,__LINE__,__FILE__);
206                }
207        }
208?>
Note: See TracBrowser for help on using the repository browser.