0337d704 |
1 | <?php |
2 | /*************************************************************************** |
5ddeb07c |
3 | * Copyright (C) 2003-2007 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 | |
22 | require_once("xorg.misc.inc.php"); |
23 | |
24 | // {{{ defines |
25 | |
26 | define("SUCCESS", 1); |
27 | define("ERROR_INACTIVE_REDIRECTION", 2); |
28 | define("ERROR_INVALID_EMAIL", 3); |
29 | define("ERROR_LOOP_EMAIL", 4); |
30 | |
31 | // }}} |
32 | // {{{ function fix_bestalias() |
33 | |
34 | function fix_bestalias($uid) |
35 | { |
08cce2ff |
36 | $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid); |
0337d704 |
37 | if ($n = $res->fetchOneCell()) { |
38 | return; |
39 | } |
08cce2ff |
40 | XDB::execute("UPDATE aliases |
612a2d8a |
41 | SET flags=CONCAT(flags,',','bestalias') |
42 | WHERE id={?} AND type!='homonyme' |
43 | ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias) |
44 | LIMIT 1", $uid); |
0337d704 |
45 | } |
46 | |
47 | // }}} |
48 | // {{{ function valide_email() |
49 | |
50 | function valide_email($str) |
51 | { |
a3a049fc |
52 | global $globals; |
53 | |
54 | $em = trim(rtrim($str)); |
55 | $em = str_replace('<', '', $em); |
56 | $em = str_replace('>', '', $em); |
57 | list($ident, $dom) = explode('@', $em); |
58 | if ($dom == $globals->mail->domain or $dom == $globals->mail->domain2) { |
59 | list($ident1) = explode('_', $ident); |
60 | list($ident) = explode('+', $ident1); |
61 | } |
62 | return $ident . '@' . $dom; |
0337d704 |
63 | } |
64 | |
65 | // }}} |
66 | // {{{ class Bogo |
67 | |
68 | class Bogo |
69 | { |
70 | // {{{ properties |
a3a049fc |
71 | |
612a2d8a |
72 | private $state; |
73 | private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams'); |
0337d704 |
74 | |
75 | // }}} |
76 | // {{{ constructor |
a3a049fc |
77 | |
612a2d8a |
78 | public function __construct($uid) |
0337d704 |
79 | { |
3c1e6a1e |
80 | if (!$uid) { |
81 | return; |
82 | } |
83 | $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid); |
84 | if ($res->numRows()) { |
612a2d8a |
85 | $this->state = $res->fetchOneCell(); |
3c1e6a1e |
86 | } else { |
87 | $this->state = 'tag_and_drop_spams'; |
88 | $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags) |
89 | VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid); |
90 | } |
0337d704 |
91 | } |
92 | |
93 | // }}} |
94 | // {{{ function change() |
95 | |
612a2d8a |
96 | public function change($uid, $state) |
0337d704 |
97 | { |
612a2d8a |
98 | $this->state = is_int($state) ? $this->_states[$state] : $state; |
99 | XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"', |
100 | $this->state, $uid); |
0337d704 |
101 | } |
102 | |
103 | // }}} |
104 | // {{{ function level() |
105 | |
612a2d8a |
106 | public function level() |
107 | { |
108 | return array_search($this->state, $this->_states); |
109 | } |
0337d704 |
110 | |
111 | // }}} |
112 | } |
113 | |
114 | // }}} |
115 | // {{{ class Email |
116 | |
117 | class Email |
118 | { |
119 | // {{{ properties |
612a2d8a |
120 | |
121 | public $email; |
122 | public $active; |
123 | public $broken; |
124 | public $rewrite; |
125 | public $panne; |
126 | public $last; |
127 | public $panne_level; |
0337d704 |
128 | |
129 | // }}} |
130 | // {{{ constructor |
131 | |
612a2d8a |
132 | public function __construct($row) |
0337d704 |
133 | { |
2069538b |
134 | list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row; |
dc557110 |
135 | $this->active = ($flags == 'active'); |
136 | $this->broken = ($flags == 'panne'); |
0337d704 |
137 | } |
138 | |
139 | // }}} |
140 | // {{{ function activate() |
141 | |
612a2d8a |
142 | public function activate($uid) |
0337d704 |
143 | { |
0337d704 |
144 | if (!$this->active) { |
dc557110 |
145 | XDB::execute("UPDATE emails |
146 | SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level), |
147 | flags = 'active' |
148 | WHERE uid={?} AND email={?}", $uid, $this->email); |
612a2d8a |
149 | $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "")); |
0337d704 |
150 | $this->active = true; |
dc557110 |
151 | $this->broken = false; |
0337d704 |
152 | } |
153 | } |
154 | |
155 | // }}} |
156 | // {{{ function deactivate() |
157 | |
612a2d8a |
158 | public function deactivate($uid) |
0337d704 |
159 | { |
0337d704 |
160 | if ($this->active) { |
08cce2ff |
161 | XDB::execute("UPDATE emails SET flags ='' |
612a2d8a |
162 | WHERE uid={?} AND email={?}", $uid, $this->email); |
3c1e6a1e |
163 | $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") ); |
0337d704 |
164 | $this->active = false; |
165 | } |
166 | } |
612a2d8a |
167 | |
0337d704 |
168 | // }}} |
169 | // {{{ function rewrite() |
170 | |
612a2d8a |
171 | public function rewrite($rew, $uid) |
0337d704 |
172 | { |
3c1e6a1e |
173 | if ($this->rewrite == $rew) { |
0337d704 |
174 | return; |
175 | } |
26dc6c43 |
176 | if (!$rew || !isvalid_email($rew)) { |
12acff5d |
177 | $rew = ''; |
178 | } |
3c1e6a1e |
179 | XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email); |
180 | $this->rewrite = $rew; |
181 | return; |
0337d704 |
182 | } |
183 | |
184 | // }}} |
185 | } |
186 | |
187 | // }}} |
188 | // {{{ class Redirect |
189 | |
190 | class Redirect |
191 | { |
192 | // {{{ properties |
612a2d8a |
193 | |
194 | private $flag_active = 'active'; |
195 | private $uid; |
196 | |
197 | public $emails; |
198 | public $bogo; |
0337d704 |
199 | |
200 | // }}} |
201 | // {{{ function Redirect() |
202 | |
612a2d8a |
203 | public function __construct($_uid) |
0337d704 |
204 | { |
3c1e6a1e |
205 | $this->uid=$_uid; |
612a2d8a |
206 | $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level |
207 | FROM emails |
208 | WHERE uid = {?} AND flags != 'filter'", $_uid); |
3c1e6a1e |
209 | $this->emails=Array(); |
0337d704 |
210 | while ($row = $res->next()) { |
3c1e6a1e |
211 | $this->emails[] = new Email($row); |
0337d704 |
212 | } |
3c1e6a1e |
213 | $this->bogo = new Bogo($_uid); |
0337d704 |
214 | } |
215 | |
216 | // }}} |
217 | // {{{ function other_active() |
218 | |
612a2d8a |
219 | public function other_active($email) |
0337d704 |
220 | { |
221 | foreach ($this->emails as $mail) { |
222 | if ($mail->email!=$email && $mail->active) { |
223 | return true; |
224 | } |
225 | } |
226 | return false; |
227 | } |
228 | |
229 | // }}} |
230 | // {{{ function delete_email() |
231 | |
612a2d8a |
232 | public function delete_email($email) |
0337d704 |
233 | { |
0337d704 |
234 | if (!$this->other_active($email)) { |
235 | return ERROR_INACTIVE_REDIRECTION; |
236 | } |
08cce2ff |
237 | XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email); |
cab08090 |
238 | $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : "")); |
3c1e6a1e |
239 | foreach ($this->emails as $i=>$mail) { |
240 | if ($email==$mail->email) { |
0337d704 |
241 | unset($this->emails[$i]); |
242 | } |
3c1e6a1e |
243 | } |
ccdbc270 |
244 | check_redirect($this); |
0337d704 |
245 | return SUCCESS; |
246 | } |
247 | |
248 | // }}} |
249 | // {{{ function add_email() |
612a2d8a |
250 | |
251 | public function add_email($email) |
0337d704 |
252 | { |
0337d704 |
253 | $email_stripped = strtolower(trim($email)); |
254 | if (!isvalid_email($email_stripped)) { |
255 | return ERROR_INVALID_EMAIL; |
256 | } |
257 | if (!isvalid_email_redirection($email_stripped)) { |
258 | return ERROR_LOOP_EMAIL; |
259 | } |
08cce2ff |
260 | XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email); |
3c1e6a1e |
261 | if ($logger = S::v('log', null)) { // may be absent --> step4.php |
262 | $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : "")); |
0337d704 |
263 | } |
3c1e6a1e |
264 | foreach ($this->emails as $mail) { |
265 | if ($mail->email == $email_stripped) { |
0337d704 |
266 | return SUCCESS; |
267 | } |
3c1e6a1e |
268 | } |
d2367745 |
269 | $this->emails[] = new Email(array($email, 'active', '', '0000-00-00', '0000-00-00', 0)); |
ca6d07f4 |
270 | |
271 | // security stuff |
a7de4ef7 |
272 | check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid); |
ccdbc270 |
273 | check_redirect($this); |
0337d704 |
274 | return SUCCESS; |
275 | } |
276 | |
277 | // }}} |
278 | // {{{ function modify_email() |
279 | |
612a2d8a |
280 | public function modify_email($emails_actifs, $emails_rewrite) |
0337d704 |
281 | { |
3c1e6a1e |
282 | foreach ($this->emails as $i=>$mail) { |
0337d704 |
283 | if (in_array($mail->email,$emails_actifs)) { |
284 | $this->emails[$i]->activate($this->uid); |
3c1e6a1e |
285 | } else { |
0337d704 |
286 | $this->emails[$i]->deactivate($this->uid); |
3c1e6a1e |
287 | } |
288 | $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid); |
0337d704 |
289 | } |
ccdbc270 |
290 | check_redirect($this); |
0337d704 |
291 | } |
292 | |
612a2d8a |
293 | public function modify_one_email($email, $activate) |
ccdbc270 |
294 | { |
b7582015 |
295 | $allinactive = true; |
296 | $thisone = false; |
8ffa657a |
297 | foreach ($this->emails as $i=>$mail) { |
298 | if ($mail->email == $email) { |
b7582015 |
299 | $thisone = $i; |
8ffa657a |
300 | } |
b7582015 |
301 | $allinactive &= !$mail->active || $mail->email == $email; |
8ffa657a |
302 | } |
b7582015 |
303 | if ($thisone === false) { |
304 | return ERROR_INVALID_EMAIL; |
305 | } |
ccdbc270 |
306 | if ($allinactive || $activate) { |
b7582015 |
307 | $this->emails[$thisone]->activate($this->uid); |
ccdbc270 |
308 | } else { |
b7582015 |
309 | $this->emails[$thisone]->deactivate($this->uid); |
ccdbc270 |
310 | } |
311 | check_redirect($this); |
b7582015 |
312 | if ($allinactive && !$activate) { |
313 | return ERROR_INACTIVE_REDIRECTION; |
314 | } else { |
315 | return SUCCESS; |
316 | } |
8ffa657a |
317 | } |
318 | |
612a2d8a |
319 | public function modify_one_email_redirect($email, $redirect) |
320 | { |
321 | foreach ($this->emails as $i=>$mail) { |
322 | if ($mail->email == $email) { |
323 | $this->emails[$i]->rewrite($redirect, $this->uid); |
ccdbc270 |
324 | check_redirect($this); |
325 | return; |
612a2d8a |
326 | } |
327 | } |
328 | } |
0337d704 |
329 | // }}} |
ccdbc270 |
330 | // {{{ function get_broken_mx() |
331 | |
612a2d8a |
332 | public function get_broken_mx() |
ccdbc270 |
333 | { |
120bd636 |
334 | $res = XDB::query("SELECT host, text |
612a2d8a |
335 | FROM mx_watch |
336 | WHERE state != 'ok'"); |
120bd636 |
337 | if (!$res->numRows()) { |
ccdbc270 |
338 | return array(); |
339 | } |
c754cf5b |
340 | $mxs = $res->fetchAllAssoc(); |
ccdbc270 |
341 | $mails = array(); |
342 | foreach ($this->emails as &$mail) { |
343 | if ($mail->active) { |
344 | list(,$domain) = explode('@', $mail->email); |
345 | getmxrr($domain, $lcl_mxs); |
346 | if (empty($lcl_mxs)) { |
347 | $lcl_mxs = array($domain); |
348 | } |
349 | $broken = false; |
350 | foreach ($mxs as &$mx) { |
351 | foreach ($lcl_mxs as $lcl) { |
c754cf5b |
352 | if (fnmatch($mx['host'], $lcl)) { |
ccdbc270 |
353 | $broken = $mx['text']; |
354 | break; |
355 | } |
356 | } |
357 | if ($broken) { |
358 | $mails[] = array('mail' => $mail->email, 'text' => $broken); |
359 | } |
360 | } |
361 | } |
362 | } |
363 | return $mails; |
364 | } |
365 | |
366 | // }}} |
0337d704 |
367 | } |
368 | |
369 | // }}} |
370 | |
a7de4ef7 |
371 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 |
372 | ?> |