relooking
[old-projects.git] / philter / philter / include / emails.inc.php
CommitLineData
dd8de1ec
PH
1<?php
2/********************************************************************************
3* include/emails.inc.php : Gestion of the email Pool of the User
4* ----------------------
5*
6* This file is part of the philter distribution
7* Copyright: See COPYING files that comes with this distribution
8********************************************************************************/
9
10/** sorting function to compare to Email objects.
11 * I wanted to use it as Email::cmp but the fonction uasort didn't seem to accept such a name as argument
12 * @param $m1 the first Email object
13 * @param $m2 the second Email object
14 * @return -1,0,1 whether $m1 <,==,> $m2
15 */
16function email_cmp($m1,$m2) {
17 return strcmp($m1->email,$m2->email);
18}
19
20class Email {
21 /** email */
22 var $email;
23 /** flags */
24 var $flags;
25
26 /** constructor.
27 * @param $_email the email string
28 * @param $_flags the flags
29 */
30 function Email($_email, $_flags) {
31 $this->email = $_email;
32 $this->flags = $_flags;
33 }
34
35 /** checks if the given string is a valid email.
36 * @param $_string the string to check
37 * @return true if the string is a valid email, false else
38 */
39 function Check($_string) {
40 return preg_match("/^[\w\-.]+(\+[\w\-.]+)?@[\w\-.]+\.[a-zA-Z]{2,44}$/", $_string);
41 }
42
43 /** returns if the email is active or not.
44 * @return true if $this->flags contains active
45 */
46 function is_active() {
47 return (stristr($this->flags, 'active')!==false);
48 }
49
50 /** toggle the state of a flag.
51 * @param $_flag the flag to toggle
52 * @return void
53 */
54 function toggle_flag($_flag) {
55 $flags = explode(",", $this->flags);
56 $key = array_search($_flag,$flags);
57 if($key===false)
58 $flags[]=$_flag;
59 else
60 unset($flags[$key]);
61
62 $this->flags = implode(",",$flags);
63 }
64
65 /** add the email in the database.
66 * @param $_uid the uid of the user
67 * @param $_mid the id of the mail into the user mail list
68 * @return void
69 */
70 function commit($_uid,$_mid) {
71 mysql_query("REPLACE INTO emails "
72 ."SET uid='{$_uid}',mid='$_mid',email='{$this->email}',flags='{$this->flags}'");
73 }
74}
75
76class EmailPool {
77 /** emails list */
78 var $emails;
79 /** uid */
80 var $uid;
81
82 /** constructor
83 * @param $_uid id of the user
84 */
85 function EmailPool($_uid) {
86 global $philter;
87 $this->uid = $_uid;
88 $this->emails = array();
89
90 $sql = mysql_query("SELECT * FROM emails WHERE uid='$_uid' ORDER BY email");
91 while($res = mysql_fetch_assoc($sql)) {
92 $this->emails[$res['mid']] = new Email($res['email'], $res['flags']);
93 }
94 }
95
96 /** create the part of the form for the mail pool.
97 * @return the string containing the form
98 */
99 function to_form() {
01f3d5ec 100 $res = "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\" onsubmit=\"return del_onsubmit()\">\n"
86b5093c
PH
101 . "<table class=\"bicol\" cellpadding=\"1\" cellspacing=\"1\" width=\"95%\" align=\"center\">\n"
102 . "<tr><th>Email</th>\n"
103 . " <th>Active</th>\n"
104 . " <th>&nbsp;</th>\n"
105 . "</tr>\n";
dd8de1ec
PH
106
107 $pair = true;
86b5093c 108
dd8de1ec 109 foreach($this->emails as $id => $email) {
86b5093c
PH
110 $res .= "<tr class=\"".($pair?"pair":"impair")."\">\n"
111 . "<td>".$email->email."</td>\n"
112 . "<td align=\"center\" width=\"10%\">\n"
113 . " <input type=\"checkbox\" name=\"emails[$id][active]\""
114 . ($email->is_active() ? " checked=\"checked\"" : "")." /></td>\n"
115 . "<td><input type=\"submit\" name=\"emails[$id][del]\" value=\"Del\" /></td>\n"
dd8de1ec
PH
116 . "</tr>\n";
117 $pair = !$pair;
118 }
119
86b5093c
PH
120 $res .= "</table>\n"
121 . "<center>\n"
122 . " <input type=\"submit\" name=\"emails[apply]\" value=\"Apply changes\" />\n"
123 . "</center>\n"
124 . "</form>\n";
01f3d5ec 125
dd8de1ec 126 $val = 'your email ...';
01f3d5ec
PH
127
128 $res .= "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
86b5093c
PH
129 . "<input type=\"text\" name=\"emails[new]\" size=\"50\" value='$val' "
130 . " onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
131 . "<input type=\"submit\" name=\"emails[add]\" value=\"Add\" />\n"
132 . "</form>\n";
dd8de1ec 133
dd8de1ec
PH
134 return $res;
135 }
136
137 /** return the string containing the list of $this->emails in javascript.
138 * @return the JS code
139 */
140 function to_js() {
141 $res = "";
142 foreach($this->emails as $id=>$mail)
143 $res .= "mail_pool[$id] = '{$mail->email}';\n";
144
145 return $res;
146 }
147
148 /** compute a new free id for a new mail.
149 * @return the new free id
150 */
151 function new_mail_id() {
152 $i=0;
153 while(array_key_exists($i,$this->emails))
154 $i++;
155 return $i;
156 }
157
158 /** handle the data from the form
159 * @return true if all is ok, false and sets $philter->error() else
160 */
161 function handle_form() {
162 global $philter;
163
164 if(isset($_POST['emails']['add'])) { // add an email to the pool
165 if(Email::Check($_POST['emails']['new'])) {
166 $new_mail = strtolower($_POST['emails']['new']);
167
168 // we check that the email is not already there
169 foreach($this->emails as $id=>$key)
170 if($key->email == $new_mail) {
171 $philter->set_error("$new_mail is already in the Email Pool");
172 return false;
173 }
174
175 // then we compute one free id, and we add it to the list
176 $mid = $this->new_mail_id();
177 $this->emails[$mid] = new Email($new_mail, 'active');
178 $this->emails[$mid]->commit($this->uid, $mid);
179 uasort($this->emails, "email_cmp");
180 } else {
181 $philter->set_error($_POST['emails']['new']." is not a valid email");
182 return false;
183 }
184 } elseif(isset($_POST['emails']['apply'])) { // apply actives changes
185 $one_active = false;
186 foreach($this->emails as $id=>$mail)
187 if(isset($_POST['emails'][$id]['active'])) {
188 $one_active = true;
189 break;
190 }
191
192 if(!$one_active) {
193 $philter->set_error("you must have at least one active email !");
194 return false;
195 }
196
197 foreach($this->emails as $id=>$key)
198 if(isset($_POST['emails'][$id]['active']) xor $this->emails[$id]->is_active()) {
199 $this->emails[$id]->toggle_flag('active');
200 $this->emails[$id]->commit($this->uid, $id);
201 }
202 } else { // delete one email
203 // we compute a list of all actives emails
204 $actives = array();
205 foreach($this->emails as $id=>$mail)
206 if($mail->is_active())
207 $actives[$id] = true;
208
209 foreach($this->emails as $id=>$key)
210 if(isset($_POST['emails'][$id]['del'])) {
211 if(count($this->emails)>0 && (count($actives)>1 || empty($actives[$id]))) {
212 list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions "
213 ."WHERE uid='{$this->uid}' AND pid='"
214 .FwdPlugin::rtti()."' AND data='$id'"));
215 if($count) {
216 $philter->set_error("This email is still in use and can't be deleted !");
217 return false;
218 } else {
219 mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
220 ."AND email='{$this->emails[$id]->email}'");
221 unset($this->emails[$id]);
222 unset($actives[$id]);
223 }
224 } else {
225 $philter->set_error("you must have at least one active email !");
226 return false;
227 }
228 }
229 }
230 return true;
231 }
232}
233
234/********************************************************************************
235* $Id$
236* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
237********************************************************************************/
238?>