f699f2ca1c5cf497806260209d667b3a33b8f1b7
2 /********************************************************************************
3 * include/emails.inc.php : Gestion of the email Pool of the User
4 * ----------------------
6 * This file is part of the philter distribution
7 * Copyright: See COPYING files that comes with this distribution
8 ********************************************************************************/
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
16 function email_cmp($m1,$m2) {
17 return strcmp($m1->email
,$m2->email
);
27 * @param $_email the email string
28 * @param $_flags the flags
30 function Email($_email, $_flags) {
31 $this->email
= $_email;
32 $this->flags
= $_flags;
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
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);
45 /** returns if the email is active or not.
46 * @return true if $this->flags contains active
48 function is_active() {
49 return (stristr($this->flags
, 'active')!==false
);
52 /** toggle the state of a flag.
53 * @param $_flag the flag to toggle
56 function toggle_flag($_flag) {
57 $flags = explode(",", $this->flags
);
58 $key = array_search($_flag,$flags);
59 if($key===NULL ||
$key===false
)
64 $this->flags
= implode(",",$flags);
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
72 function commit($_uid,$_mid) {
73 mysql_query("REPLACE INTO emails "
74 ."SET uid='{$_uid}',mid='$_mid',email='{$this->email}',flags='{$this->flags}'");
85 * @param $_uid id of the user
87 function EmailPool($_uid) {
90 $this->emails
= array();
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']);
98 /** create the part of the form for the mail pool.
99 * @return the string containing the 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> </th>\n"
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"
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"
131 $val = _i18n('your_email');
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"
144 /** create the part of the small form for the mail pool.
145 * @return the string containing the form
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"
155 foreach($this->emails
as $id => $email) {
157 $res .= "<tr class=\"".($pair?
"pair":"impair")."\">\n"
158 . "<td>".($email->is_active() ?
"<strong>" : "")
159 .$email->email
.($email->is_active() ?
"</strong>" : "")."</td>\n"
164 $val = _i18n('your_email');
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"
176 /** return the string containing the list of $this->emails in javascript.
177 * @return the JS code
181 foreach($this->emails
as $id=>$mail)
182 $res .= "mail_pool[$id] = '{$mail->email}';\n";
187 /** compute a new free id for a new mail.
188 * @return the new free id
190 function new_mail_id() {
192 while(array_key_exists($i,$this->emails
))
197 /** handle the data from the form
198 * @return true if all is ok, false and sets $philter->error() else
200 function handle_form() {
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']);
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'));
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");
220 $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid'));
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
228 foreach($this->emails
as $id=>$mail) {
229 if($del_id!=$id && $mail->is_active()) {
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'"));
240 $philter->set_error(_i18n('pool_err_active'));
243 mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
244 ."AND email='{$this->emails[$del_id]->email}'");
245 unset($this->emails
[$del_id]);
248 $philter->set_error(_i18n('pool_err_need_one'));
251 } elseif(isset($_POST['emails']['apply'])) { // apply actives changes
253 foreach($this->emails
as $id=>$mail)
254 if(isset($_POST['emails'][$id]['active'])) {
260 $philter->set_error(_i18n('pool_err_need_one'));
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);
274 /** handle the data from the small form
275 * @return true if all is ok, false and sets $philter->error() else
277 function handle_small_form() {
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']);
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'));
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");
297 $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid'));
304 /********************************************************************************
306 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
307 ********************************************************************************/