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