some tweaks and bugfixes
[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) {
fe9450a2
PH
40 if(function_exists('isvalid_email_redirection'))
41 return isvalid_email_redirection($_string);
dd8de1ec
PH
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);
f0a83324 59 if($key===NULL || $key===false)
dd8de1ec
PH
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
78class 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() {
4956445b 102 $res = "<form id=\"emails\" action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
86b5093c 103 . "<table class=\"bicol\" cellpadding=\"1\" cellspacing=\"1\" width=\"95%\" align=\"center\">\n"
6398953d
PH
104 . "<tr><th>"._i18n('email')."</th>\n"
105 . " <th>"._i18n('active')."</th>\n"
86b5093c
PH
106 . " <th>&nbsp;</th>\n"
107 . "</tr>\n";
dd8de1ec
PH
108
109 $pair = true;
86b5093c 110
dd8de1ec 111 foreach($this->emails as $id => $email) {
86b5093c
PH
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"
0eb037c8 117 . "<td align=\"center\"><input type=\"button\" "
6398953d 118 . "value=\""._i18n('del')."\" onclick=\"del_onclick($id)\" /></td>\n"
dd8de1ec
PH
119 . "</tr>\n";
120 $pair = !$pair;
121 }
122
86b5093c 123 $res .= "</table>\n"
0cff7bb6 124 . "<br /><center>\n"
0eb037c8 125 . " <input id=\"emailsDel\" type=\"hidden\" name=\"emails[del]\" value=\"-1\" />\n"
5ae3e923 126 . " <input type=\"submit\" name=\"emails[apply]\" value=\""
6398953d 127 . _i18n('apply_changes')."\" />\n"
0cff7bb6 128 . "</center><br />\n"
86b5093c 129 . "</form>\n";
01f3d5ec 130
6398953d 131 $val = _i18n('your_email');
01f3d5ec
PH
132
133 $res .= "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
0cff7bb6 134 . _i18n('pool_help2')
86b5093c
PH
135 . "<input type=\"text\" name=\"emails[new]\" size=\"50\" value='$val' "
136 . " onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
5ae3e923 137 . "<input type=\"submit\" name=\"emails[add]\" value=\"".
6398953d 138 _i18n('add')."\" />\n"
86b5093c 139 . "</form>\n";
dd8de1ec 140
dd8de1ec
PH
141 return $res;
142 }
143
144 /** return the string containing the list of $this->emails in javascript.
145 * @return the JS code
146 */
147 function to_js() {
148 $res = "";
149 foreach($this->emails as $id=>$mail)
150 $res .= "mail_pool[$id] = '{$mail->email}';\n";
151
152 return $res;
153 }
154
155 /** compute a new free id for a new mail.
156 * @return the new free id
157 */
158 function new_mail_id() {
159 $i=0;
160 while(array_key_exists($i,$this->emails))
161 $i++;
162 return $i;
163 }
164
165 /** handle the data from the form
166 * @return true if all is ok, false and sets $philter->error() else
167 */
168 function handle_form() {
169 global $philter;
170
171 if(isset($_POST['emails']['add'])) { // add an email to the pool
172 if(Email::Check($_POST['emails']['new'])) {
173 $new_mail = strtolower($_POST['emails']['new']);
174
175 // we check that the email is not already there
176 foreach($this->emails as $id=>$key)
177 if($key->email == $new_mail) {
f0a83324 178 $philter->set_error("$new_mail "._i18n('pool_err_already'));
dd8de1ec
PH
179 return false;
180 }
181
182 // then we compute one free id, and we add it to the list
183 $mid = $this->new_mail_id();
184 $this->emails[$mid] = new Email($new_mail, 'active');
185 $this->emails[$mid]->commit($this->uid, $mid);
186 uasort($this->emails, "email_cmp");
187 } else {
f0a83324 188 $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid'));
dd8de1ec
PH
189 return false;
190 }
0eb037c8 191 } elseif(isset($_POST['emails']['del']) && $_POST['emails']['del'] != -1) { // delete one email
4956445b
PH
192 $del_id = $_POST['emails']['del'];
193 // we compute a list of all actives emails
194 $allow = false;
0eb037c8
PH
195
196 foreach($this->emails as $id=>$mail) {
197 if($del_id!=$id && $mail->is_active()) {
4956445b 198 $allow = true;
0eb037c8
PH
199 break;
200 }
201 }
4956445b
PH
202
203 if($allow && isset($this->emails[$del_id])) {
204 list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions "
205 ."WHERE uid='{$this->uid}' AND pid='"
206 .FwdPlugin::rtti()."' AND data='$del_id'"));
207 if($count) {
f0a83324 208 $philter->set_error(_i18n('pool_err_active'));
4956445b
PH
209 return false;
210 } else {
211 mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
212 ."AND email='{$this->emails[$del_id]->email}'");
213 unset($this->emails[$del_id]);
214 }
215 } else {
f0a83324 216 $philter->set_error(_i18n('pool_err_need_one'));
4956445b
PH
217 return false;
218 }
dd8de1ec
PH
219 } elseif(isset($_POST['emails']['apply'])) { // apply actives changes
220 $one_active = false;
221 foreach($this->emails as $id=>$mail)
222 if(isset($_POST['emails'][$id]['active'])) {
223 $one_active = true;
224 break;
225 }
226
227 if(!$one_active) {
f0a83324 228 $philter->set_error(_i18n('pool_err_need_one'));
dd8de1ec
PH
229 return false;
230 }
231
f0a83324 232 foreach($this->emails as $id=>$key) {
dd8de1ec
PH
233 if(isset($_POST['emails'][$id]['active']) xor $this->emails[$id]->is_active()) {
234 $this->emails[$id]->toggle_flag('active');
235 $this->emails[$id]->commit($this->uid, $id);
236 }
f0a83324 237 }
dd8de1ec 238 return true;
4956445b 239 }
dd8de1ec
PH
240 }
241}
242
243/********************************************************************************
244* $Id$
245* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
246********************************************************************************/
247?>