bugs 396, 416 et 417 : interface de gestion des spams dans les ML + admin peuvent...
[platal.git] / include / emails.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 Polytechnique.org *
0337d704 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22require_once("xorg.misc.inc.php");
23
24// {{{ defines
25
26define("SUCCESS", 1);
27define("ERROR_INACTIVE_REDIRECTION", 2);
28define("ERROR_INVALID_EMAIL", 3);
29define("ERROR_LOOP_EMAIL", 4);
30
31// }}}
32// {{{ function fix_bestalias()
33
34function fix_bestalias($uid)
35{
36 global $globals;
37 $res = $globals->xdb->query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid);
38 if ($n = $res->fetchOneCell()) {
39 return;
40 }
41 $globals->xdb->execute("UPDATE aliases
42 SET flags=CONCAT(flags,',','bestalias')
43 WHERE id={?} AND type!='homonyme'
44 ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias)
45 LIMIT 1", $uid);
46}
47
48// }}}
49// {{{ function valide_email()
50
51function valide_email($str)
52{
53 $em = trim(rtrim($str));
54 $em = str_replace('<', '', $em);
55 $em = str_replace('>', '', $em);
56 list($ident, $dom) = explode('@', $em);
57 if ($dom == $globals->mail->domain or $dom == $globals->mail->domain2) {
58 list($ident1) = explode('_', $ident);
59 list($ident) = explode('+', $ident1);
60 }
61 return $ident . '@' . $dom;
62}
63
64// }}}
65// {{{ class Bogo
66
67class Bogo
68{
69 // {{{ properties
70
71 var $state;
94f3f9ba 72 var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
0337d704 73
74 // }}}
75 // {{{ constructor
76
77 function Bogo($uid)
78 {
79 global $globals;
80 $res = $globals->xdb->query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
81 if ($res->numRows()) {
82 $this->state = $res->fetchOneCell();
83 } else {
94f3f9ba 84 $this->state = 'tag_and_drop_spams';
0337d704 85 $res = $globals->xdb->query("INSERT INTO emails (uid,email,rewrite,panne,flags)
94f3f9ba 86 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
0337d704 87 }
88 }
89
90 // }}}
91 // {{{ function change()
92
93 function change($uid, $state)
94 {
95 global $globals;
96 $this->state = is_int($state) ? $this->_states[$state] : $state;
97 $globals->xdb->execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"', $this->state, $uid);
98 }
99
100 // }}}
101 // {{{ function level()
102
103 function level()
104 { return array_search($this->state, $this->_states); }
105
106 // }}}
107}
108
109// }}}
110// {{{ class Email
111
112class Email
113{
114 // {{{ properties
115
116 var $email;
117 var $active;
118 var $rewrite;
119 var $panne;
120
121 // }}}
122 // {{{ constructor
123
124 function Email($row)
125 {
126 list($this->email, $this->active, $this->rewrite, $this->panne) = $row;
127 }
128
129 // }}}
130 // {{{ function activate()
131
132 function activate($uid)
133 {
134 global $globals;
135 if (!$this->active) {
136 $globals->xdb->execute("UPDATE emails SET flags = 'active'
137 WHERE uid={?} AND email={?}", $uid, $this->email);
138 $_SESSION['log']->log("email_on", $this->email.($uid!=Session::getInt('uid') ? "(admin on $uid)" : ""));
139 $this->active = true;
140 }
141 }
142
143 // }}}
144 // {{{ function deactivate()
145
146 function deactivate($uid)
147 {
148 global $globals;
149 if ($this->active) {
150 $globals->xdb->execute("UPDATE emails SET flags =''
151 WHERE uid={?} AND email={?}", $uid, $this->email);
152 $_SESSION['log']->log("email_off",$this->email.($uid!=Session::getInt('uid') ? "(admin on $uid)" : "") );
153 $this->active = false;
154 }
155 }
156
157 // }}}
158 // {{{ function rewrite()
159
160 function rewrite($rew, $uid)
161 {
162 global $globals;
163 if ($this->rewrite == $rew) {
164 return;
165 }
166 $globals->xdb->execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email);
167 $this->rewrite = $rew;
168 return;
169 }
170
171 // }}}
172}
173
174// }}}
175// {{{ class Redirect
176
177class Redirect
178{
179 // {{{ properties
180
181 var $flag_active = 'active';
182 var $emails;
183 var $bogo;
184 var $uid;
185
186 // }}}
187 // {{{ function Redirect()
188
189 function Redirect($_uid)
190 {
191 global $globals;
192 $this->uid=$_uid;
193 $res = $globals->xdb->iterRow("
194 SELECT email, flags='active', rewrite, panne
195 FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid);
196 $this->emails=Array();
197 while ($row = $res->next()) {
198 $this->emails[] = new Email($row);
199 }
200 $this->bogo = new Bogo($_uid);
201 }
202
203 // }}}
204 // {{{ function other_active()
205
206 function other_active($email)
207 {
208 foreach ($this->emails as $mail) {
209 if ($mail->email!=$email && $mail->active) {
210 return true;
211 }
212 }
213 return false;
214 }
215
216 // }}}
217 // {{{ function delete_email()
218
219 function delete_email($email)
220 {
221 global $globals;
222 if (!$this->other_active($email)) {
223 return ERROR_INACTIVE_REDIRECTION;
224 }
225 $globals->xdb->execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
226 $_SESSION['log']->log('email_del',$email.($this->uid!=Session::getInt('uid') ? " (admin on {$this->uid})" : ""));
227 foreach ($this->emails as $i=>$mail) {
228 if ($email==$mail->email) {
229 unset($this->emails[$i]);
230 }
231 }
232 return SUCCESS;
233 }
234
235 // }}}
236 // {{{ function add_email()
237
238 function add_email($email)
239 {
240 global $globals;
241 $email_stripped = strtolower(trim($email));
242 if (!isvalid_email($email_stripped)) {
243 return ERROR_INVALID_EMAIL;
244 }
245 if (!isvalid_email_redirection($email_stripped)) {
246 return ERROR_LOOP_EMAIL;
247 }
248 $globals->xdb->execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
249 if ($logger = Session::getMixed('log', null)) { // may be absent --> step4.php
250 $logger->log('email_add',$email.($this->uid!=Session::getInt('uid') ? " (admin on {$this->uid})" : ""));
251 }
252 foreach ($this->emails as $mail) {
253 if ($mail->email == $email_stripped) {
254 return SUCCESS;
255 }
256 }
257 $this->emails[] = new Email(array($email,1,'','0000-00-00'));
258 return SUCCESS;
259 }
260
261 // }}}
262 // {{{ function modify_email()
263
264 function modify_email($emails_actifs,$emails_rewrite)
265 {
266 global $globals;
267 foreach ($this->emails as $i=>$mail) {
268 if (in_array($mail->email,$emails_actifs)) {
269 $this->emails[$i]->activate($this->uid);
270 } else {
271 $this->emails[$i]->deactivate($this->uid);
272 }
273 $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid);
274 }
275 }
276
8ffa657a 277 function modify_one_email($email, $activate) {
278 foreach ($this->emails as $i=>$mail) {
279 if ($mail->email == $email) {
280 if ($activate)
281 $this->emails[$i]->activate($this->uid);
282 else
283 $this->emails[$i]->deactivate($this->uid);
284 }
285 }
286 }
287
0337d704 288 // }}}
289}
290
291// }}}
292
293// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
294?>