handle require_once and include_once, allow single quotes
[old-projects.git] / philter / philter / include / emails.inc.php
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 */
16 function email_cmp($m1,$m2) {
17 return strcmp($m1->email,$m2->email);
18 }
19
20 class 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 if(function_exists('isvalid_email_redirection'))
41 return isvalid_email_redirection($_string);
42 return preg_match("/^[\w\-.]+(\+[\w\-.]+)?@[\w\-.]+\.[a-zA-Z]{2,44}$/", $_string);
43 }
44
45 /** returns if the email is active or not.
46 * @return true if $this->flags contains active
47 */
48 function is_active() {
49 return (stristr($this->flags, 'active')!==false);
50 }
51
52 /** toggle the state of a flag.
53 * @param $_flag the flag to toggle
54 * @return void
55 */
56 function toggle_flag($_flag) {
57 $flags = explode(",", $this->flags);
58 $key = array_search($_flag,$flags);
59 if($key===NULL || $key===false)
60 $flags[]=$_flag;
61 else
62 unset($flags[$key]);
63
64 $this->flags = implode(",",$flags);
65 }
66
67 /** add the email in the database.
68 * @param $_uid the uid of the user
69 * @param $_mid the id of the mail into the user mail list
70 * @return void
71 */
72 function commit($_uid,$_mid) {
73 mysql_query("REPLACE INTO emails "
74 ."SET uid='{$_uid}',mid='$_mid',email='{$this->email}',flags='{$this->flags}'");
75 }
76 }
77
78 class EmailPool {
79 /** emails list */
80 var $emails;
81 /** uid */
82 var $uid;
83
84 /** constructor
85 * @param $_uid id of the user
86 */
87 function EmailPool($_uid) {
88 global $philter;
89 $this->uid = $_uid;
90 $this->emails = array();
91
92 $sql = mysql_query("SELECT * FROM emails WHERE uid='$_uid' ORDER BY email");
93 while($res = mysql_fetch_assoc($sql)) {
94 $this->emails[$res['mid']] = new Email($res['email'], $res['flags']);
95 }
96 }
97
98 /** create the part of the form for the mail pool.
99 * @return the string containing the form
100 */
101 function to_form() {
102 $res = "<form id=\"emails\" action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
103 . "<table class=\"bicol\" cellpadding=\"1\" cellspacing=\"1\" width=\"95%\" align=\"center\">\n"
104 . "<tr><th>"._i18n('email')."</th>\n"
105 . " <th>"._i18n('active')."</th>\n"
106 . " <th>&nbsp;</th>\n"
107 . "</tr>\n";
108
109 $pair = true;
110
111 foreach($this->emails as $id => $email) {
112 $res .= "<tr class=\"".($pair?"pair":"impair")."\">\n"
113 . "<td>".$email->email."</td>\n"
114 . "<td align=\"center\" width=\"10%\">\n"
115 . " <input type=\"checkbox\" name=\"emails[$id][active]\""
116 . ($email->is_active() ? " checked=\"checked\"" : "")." /></td>\n"
117 . "<td align=\"center\"><input type=\"button\" "
118 . "value=\""._i18n('del')."\" onclick=\"del_onclick($id)\" /></td>\n"
119 . "</tr>\n";
120 $pair = !$pair;
121 }
122
123 $res .= "</table>\n"
124 . "<br /><center>\n"
125 . " <input id=\"emailsDel\" type=\"hidden\" name=\"emails[del]\" value=\"-1\" />\n"
126 . " <input type=\"submit\" name=\"emails[apply]\" value=\""
127 . _i18n('apply_changes')."\" />\n"
128 . "</center><br />\n"
129 . "</form>\n";
130
131 $val = _i18n('your_email');
132
133 $res .= "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
134 . _i18n('pool_help2')
135 . "<input type=\"text\" name=\"emails[new]\" size=\"50\" value='$val' "
136 . " onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
137 . "<input type=\"submit\" name=\"emails[add]\" value=\"".
138 _i18n('add')."\" />\n"
139 . "</form>\n";
140
141 return $res;
142 }
143
144 /** create the part of the small form for the mail pool.
145 * @return the string containing the form
146 */
147 function to_small_form() {
148 $res = "<form id=\"emails\" action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
149 . "<table class=\"bicol\" cellpadding=\"1\" cellspacing=\"1\" width=\"95%\" align=\"center\">\n"
150 . "<tr><th>"._i18n('email')."</th>\n"
151 . "</tr>\n";
152
153 $pair = true;
154
155 foreach($this->emails as $id => $email) {
156
157 $res .= "<tr class=\"".($pair?"pair":"impair")."\">\n"
158 . "<td>".($email->is_active() ? "<strong>" : "")
159 .$email->email.($email->is_active() ? "</strong>" : "")."</td>\n"
160 . "</tr>\n";
161 $pair = !$pair;
162 }
163
164 $val = _i18n('your_email');
165 $res .= "<tr><th>\n"
166 . "<input type=\"text\" name=\"emails[new]\" size=\"50\" value='$val' "
167 . " onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
168 . "<input type=\"submit\" name=\"emails[add]\" value=\""._i18n('add')."\" />\n"
169 . "</th></tr>\n"
170 . "</table>\n"
171 . "</form>\n";
172
173 return $res;
174 }
175
176 /** return the string containing the list of $this->emails in javascript.
177 * @return the JS code
178 */
179 function to_js() {
180 $res = "";
181 foreach($this->emails as $id=>$mail)
182 $res .= "mail_pool[$id] = '{$mail->email}';\n";
183
184 return $res;
185 }
186
187 /** compute a new free id for a new mail.
188 * @return the new free id
189 */
190 function new_mail_id() {
191 $i=0;
192 while(array_key_exists($i,$this->emails))
193 $i++;
194 return $i;
195 }
196
197 /** handle the data from the form
198 * @return true if all is ok, false and sets $philter->error() else
199 */
200 function handle_form() {
201 global $philter;
202
203 if(isset($_POST['emails']['add'])) { // add an email to the pool
204 if(Email::Check($_POST['emails']['new'])) {
205 $new_mail = strtolower($_POST['emails']['new']);
206
207 // we check that the email is not already there
208 foreach($this->emails as $id=>$key)
209 if($key->email == $new_mail) {
210 $philter->set_error("$new_mail "._i18n('pool_err_already'));
211 return false;
212 }
213
214 // then we compute one free id, and we add it to the list
215 $mid = $this->new_mail_id();
216 $this->emails[$mid] = new Email($new_mail, 'active');
217 $this->emails[$mid]->commit($this->uid, $mid);
218 uasort($this->emails, "email_cmp");
219 } else {
220 $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid'));
221 return false;
222 }
223 } elseif(isset($_POST['emails']['del']) && $_POST['emails']['del'] != -1) { // delete one email
224 $del_id = $_POST['emails']['del'];
225 // we compute a list of all actives emails
226 $allow = false;
227
228 foreach($this->emails as $id=>$mail) {
229 if($del_id!=$id && $mail->is_active()) {
230 $allow = true;
231 break;
232 }
233 }
234
235 if($allow && isset($this->emails[$del_id])) {
236 list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions "
237 ."WHERE uid='{$this->uid}' AND pid='"
238 .FwdPlugin::rtti()."' AND data='$del_id'"));
239 if($count) {
240 $philter->set_error(_i18n('pool_err_active'));
241 return false;
242 } else {
243 mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
244 ."AND email='{$this->emails[$del_id]->email}'");
245 unset($this->emails[$del_id]);
246 }
247 } else {
248 $philter->set_error(_i18n('pool_err_need_one'));
249 return false;
250 }
251 } elseif(isset($_POST['emails']['apply'])) { // apply actives changes
252 $one_active = false;
253 foreach($this->emails as $id=>$mail)
254 if(isset($_POST['emails'][$id]['active'])) {
255 $one_active = true;
256 break;
257 }
258
259 if(!$one_active) {
260 $philter->set_error(_i18n('pool_err_need_one'));
261 return false;
262 }
263
264 foreach($this->emails as $id=>$key) {
265 if(isset($_POST['emails'][$id]['active']) xor $this->emails[$id]->is_active()) {
266 $this->emails[$id]->toggle_flag('active');
267 $this->emails[$id]->commit($this->uid, $id);
268 }
269 }
270 return true;
271 }
272 }
273
274 /** handle the data from the small form
275 * @return true if all is ok, false and sets $philter->error() else
276 */
277 function handle_small_form() {
278 global $philter;
279
280 if(isset($_POST['emails']['add'])) { // add an email to the pool
281 if(Email::Check($_POST['emails']['new'])) {
282 $new_mail = strtolower($_POST['emails']['new']);
283
284 // we check that the email is not already there
285 foreach($this->emails as $id=>$key)
286 if($key->email == $new_mail) {
287 $philter->set_error("$new_mail "._i18n('pool_err_already'));
288 return false;
289 }
290
291 // then we compute one free id, and we add it to the list
292 $mid = $this->new_mail_id();
293 $this->emails[$mid] = new Email($new_mail, '');
294 $this->emails[$mid]->commit($this->uid, $mid);
295 uasort($this->emails, "email_cmp");
296 } else {
297 $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid'));
298 return false;
299 }
300 }
301 }
302 }
303
304 /********************************************************************************
305 * $Id$
306 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
307 ********************************************************************************/
308 ?>