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 return preg_match("/^[\w\-.]+(\+[\w\-.]+)?@[\w\-.]+\.[a-zA-Z]{2,44}$/", $_string);
43 /** returns if the email is active or not.
44 * @return true if $this->flags contains active
46 function is_active() {
47 return (stristr($this->flags
, 'active')!==false
);
50 /** toggle the state of a flag.
51 * @param $_flag the flag to toggle
54 function toggle_flag($_flag) {
55 $flags = explode(",", $this->flags
);
56 $key = array_search($_flag,$flags);
62 $this->flags
= implode(",",$flags);
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
70 function commit($_uid,$_mid) {
71 mysql_query("REPLACE INTO emails "
72 ."SET uid='{$_uid}',mid='$_mid',email='{$this->email}',flags='{$this->flags}'");
83 * @param $_uid id of the user
85 function EmailPool($_uid) {
88 $this->emails
= array();
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']);
96 /** create the part of the form for the mail pool.
97 * @return the string containing the form
100 $res = "<form id=\"emails\" action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
101 . "<table class=\"bicol\" cellpadding=\"1\" cellspacing=\"1\" width=\"95%\" align=\"center\">\n"
102 . "<tr><th>"._i18n('email')."</th>\n"
103 . " <th>"._i18n('active')."</th>\n"
104 . " <th> </th>\n"
109 foreach($this->emails
as $id => $email) {
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 align=\"center\"><input type=\"button\" "
116 . "value=\""._i18n('del')."\" onclick=\"del_onclick($id)\" /></td>\n"
123 . " <input id=\"emailsDel\" type=\"hidden\" name=\"emails[del]\" value=\"-1\" />\n"
124 . " <input type=\"submit\" name=\"emails[apply]\" value=\""
125 . _i18n('apply_changes')."\" />\n"
126 . "</center><br />\n"
129 $val = _i18n('your_email');
131 $res .= "<form action=\"{$_SERVER['REQUEST_URI']}\" method=\"post\">\n"
132 . _i18n('pool_help2')
133 . "<input type=\"text\" name=\"emails[new]\" size=\"50\" value='$val' "
134 . " onfocus=\"text_onfocus(this,'$val')\" onblur=\"text_onblur(this,'$val')\" />\n"
135 . "<input type=\"submit\" name=\"emails[add]\" value=\"".
136 _i18n('add')."\" />\n"
142 /** return the string containing the list of $this->emails in javascript.
143 * @return the JS code
147 foreach($this->emails
as $id=>$mail)
148 $res .= "mail_pool[$id] = '{$mail->email}';\n";
153 /** compute a new free id for a new mail.
154 * @return the new free id
156 function new_mail_id() {
158 while(array_key_exists($i,$this->emails
))
163 /** handle the data from the form
164 * @return true if all is ok, false and sets $philter->error() else
166 function handle_form() {
169 if(isset($_POST['emails']['add'])) { // add an email to the pool
170 if(Email
::Check($_POST['emails']['new'])) {
171 $new_mail = strtolower($_POST['emails']['new']);
173 // we check that the email is not already there
174 foreach($this->emails
as $id=>$key)
175 if($key->email
== $new_mail) {
176 $philter->set_error("$new_mail is already in the Email Pool");
180 // then we compute one free id, and we add it to the list
181 $mid = $this->new_mail_id();
182 $this->emails
[$mid] = new Email($new_mail, 'active');
183 $this->emails
[$mid]->commit($this->uid
, $mid);
184 uasort($this->emails
, "email_cmp");
186 $philter->set_error($_POST['emails']['new']." is not a valid email");
189 } elseif(isset($_POST['emails']['del']) && $_POST['emails']['del'] != -1) { // delete one email
190 $del_id = $_POST['emails']['del'];
191 // we compute a list of all actives emails
194 foreach($this->emails
as $id=>$mail) {
195 if($del_id!=$id && $mail->is_active()) {
201 if($allow && isset($this->emails
[$del_id])) {
202 list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions "
203 ."WHERE uid='{$this->uid}' AND pid='"
204 .FwdPlugin
::rtti()."' AND data='$del_id'"));
206 $philter->set_error("This email is still in use and can't be deleted !");
209 mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' "
210 ."AND email='{$this->emails[$del_id]->email}'");
211 unset($this->emails
[$del_id]);
214 $philter->set_error("you must have at least one active email !");
217 } elseif(isset($_POST['emails']['apply'])) { // apply actives changes
220 foreach($this->emails
as $id=>$mail)
221 if(isset($_POST['emails'][$id]['active'])) {
227 $philter->set_error("you must have at least one active email !");
231 foreach($this->emails
as $id=>$key)
232 if(isset($_POST['emails'][$id]['active']) xor $this->emails
[$id]->is_active()) {
233 $this->emails
[$id]->toggle_flag('active');
234 $this->emails
[$id]->commit($this->uid
, $id);
241 /********************************************************************************
243 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
244 ********************************************************************************/