admin wiki, delete pages or rename pages
[platal.git] / include / emails.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
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 {
36 $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid);
37 if ($n = $res->fetchOneCell()) {
38 return;
39 }
40 XDB::execute("UPDATE aliases
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);
45 }
46
47 // }}}
48 // {{{ function valide_email()
49
50 function valide_email($str)
51 {
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;
63 }
64
65 // }}}
66 // {{{ class Bogo
67
68 class Bogo
69 {
70 // {{{ properties
71
72 private $state;
73 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
74
75 // }}}
76 // {{{ constructor
77
78 public function __construct($uid)
79 {
80 if (!$uid) {
81 return;
82 }
83 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
84 if ($res->numRows()) {
85 $this->state = $res->fetchOneCell();
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 }
91 }
92
93 // }}}
94 // {{{ function change()
95
96 public function change($uid, $state)
97 {
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);
101 }
102
103 // }}}
104 // {{{ function level()
105
106 public function level()
107 {
108 return array_search($this->state, $this->_states);
109 }
110
111 // }}}
112 }
113
114 // }}}
115 // {{{ class Email
116
117 class Email
118 {
119 // {{{ properties
120
121 public $email;
122 public $active;
123 public $broken;
124 public $rewrite;
125 public $panne;
126 public $last;
127 public $panne_level;
128
129 // }}}
130 // {{{ constructor
131
132 public function __construct($row)
133 {
134 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
135 $this->active = ($flags == 'active');
136 $this->broken = ($flags == 'panne');
137 }
138
139 // }}}
140 // {{{ function activate()
141
142 public function activate($uid)
143 {
144 if (!$this->active) {
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);
149 $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : ""));
150 $this->active = true;
151 $this->broken = false;
152 }
153 }
154
155 // }}}
156 // {{{ function deactivate()
157
158 public function deactivate($uid)
159 {
160 if ($this->active) {
161 XDB::execute("UPDATE emails SET flags =''
162 WHERE uid={?} AND email={?}", $uid, $this->email);
163 $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") );
164 $this->active = false;
165 }
166 }
167
168 // }}}
169 // {{{ function rewrite()
170
171 public function rewrite($rew, $uid)
172 {
173 if ($this->rewrite == $rew) {
174 return;
175 }
176 if (!$rew || !isvalid_email($rew)) {
177 $rew = '';
178 }
179 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email);
180 $this->rewrite = $rew;
181 return;
182 }
183
184 // }}}
185 }
186
187 // }}}
188 // {{{ class Redirect
189
190 class Redirect
191 {
192 // {{{ properties
193
194 private $flag_active = 'active';
195 private $uid;
196
197 public $emails;
198 public $bogo;
199
200 // }}}
201 // {{{ function Redirect()
202
203 public function __construct($_uid)
204 {
205 $this->uid=$_uid;
206 $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
207 FROM emails
208 WHERE uid = {?} AND flags != 'filter'", $_uid);
209 $this->emails=Array();
210 while ($row = $res->next()) {
211 $this->emails[] = new Email($row);
212 }
213 $this->bogo = new Bogo($_uid);
214 }
215
216 // }}}
217 // {{{ function other_active()
218
219 public function other_active($email)
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
232 public function delete_email($email)
233 {
234 if (!$this->other_active($email)) {
235 return ERROR_INACTIVE_REDIRECTION;
236 }
237 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
238 $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
239 foreach ($this->emails as $i=>$mail) {
240 if ($email==$mail->email) {
241 unset($this->emails[$i]);
242 }
243 }
244 check_redirect($this);
245 return SUCCESS;
246 }
247
248 // }}}
249 // {{{ function add_email()
250
251 public function add_email($email)
252 {
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 }
260 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
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})" : ""));
263 }
264 foreach ($this->emails as $mail) {
265 if ($mail->email == $email_stripped) {
266 return SUCCESS;
267 }
268 }
269 $this->emails[] = new Email(array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
270
271 // security stuff
272 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
273 check_redirect($this);
274 return SUCCESS;
275 }
276
277 // }}}
278 // {{{ function modify_email()
279
280 public function modify_email($emails_actifs, $emails_rewrite)
281 {
282 foreach ($this->emails as $i=>$mail) {
283 if (in_array($mail->email,$emails_actifs)) {
284 $this->emails[$i]->activate($this->uid);
285 } else {
286 $this->emails[$i]->deactivate($this->uid);
287 }
288 $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid);
289 }
290 check_redirect($this);
291 }
292
293 public function modify_one_email($email, $activate)
294 {
295 $allinactive = true;
296 $thisone = false;
297 foreach ($this->emails as $i=>$mail) {
298 if ($mail->email == $email) {
299 $thisone = $i;
300 }
301 $allinactive &= !$mail->active || $mail->email == $email;
302 }
303 if ($thisone === false) {
304 return ERROR_INVALID_EMAIL;
305 }
306 if ($allinactive || $activate) {
307 $this->emails[$thisone]->activate($this->uid);
308 } else {
309 $this->emails[$thisone]->deactivate($this->uid);
310 }
311 check_redirect($this);
312 if ($allinactive && !$activate) {
313 return ERROR_INACTIVE_REDIRECTION;
314 } else {
315 return SUCCESS;
316 }
317 }
318
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);
324 check_redirect($this);
325 return;
326 }
327 }
328 }
329 // }}}
330 // {{{ function get_broken_mx()
331
332 public function get_broken_mx()
333 {
334 $res = XDB::query("SELECT host, text
335 FROM mx_watch
336 WHERE state != 'ok'");
337 if (!$res->numRows()) {
338 return array();
339 }
340 $mxs = $res->fetchAllAssoc();
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) {
352 if (fnmatch($mx['host'], $lcl)) {
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 // }}}
367 }
368
369 // }}}
370
371 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
372 ?>