Merge branch 'master' of /home/git/platal into profile_edit
[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 define("SUCCESS", 1);
25 define("ERROR_INACTIVE_REDIRECTION", 2);
26 define("ERROR_INVALID_EMAIL", 3);
27 define("ERROR_LOOP_EMAIL", 4);
28
29 // function fix_bestalias() {{{1
30
31 function fix_bestalias($uid)
32 {
33 $res = XDB::query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid);
34 if ($n = $res->fetchOneCell()) {
35 return;
36 }
37 XDB::execute("UPDATE aliases
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);
42 }
43
44 // function valide_email() {{{1
45
46 function valide_email($str)
47 {
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;
59 }
60
61 // class Bogo {{{1
62
63 class Bogo
64 {
65 // properties {{{2
66
67 private $state;
68 private $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
69
70 // constructor {{{2
71
72 public function __construct($uid)
73 {
74 if (!$uid) {
75 return;
76 }
77 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
78 if ($res->numRows()) {
79 $this->state = $res->fetchOneCell();
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 }
85 }
86
87 // public function change() {{{2
88
89 public function change($uid, $state)
90 {
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);
94 }
95
96 // pubic function level() {{{2
97
98 public function level()
99 {
100 return array_search($this->state, $this->_states);
101 }
102 }
103
104 // class Email {{{1
105
106 class Email
107 {
108 // properties {{{2
109
110 public $email;
111 public $active;
112 public $broken;
113 public $disabled;
114 public $rewrite;
115 public $panne;
116 public $last;
117 public $panne_level;
118
119 // constructor {{{2
120
121 public function __construct($row)
122 {
123 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
124 $this->active = ($flags == 'active');
125 $this->broken = ($flags == 'panne');
126 $this->disabled = ($flags == 'disable');
127 }
128
129 // public function activate() {{{2
130
131 public function activate($uid)
132 {
133 if (!$this->active) {
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);
138 $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : ""));
139 $this->active = true;
140 $this->broken = false;
141 }
142 }
143
144 // public function deactivate() {{{2
145
146 public function deactivate($uid)
147 {
148 if ($this->active) {
149 XDB::execute("UPDATE emails SET flags =''
150 WHERE uid={?} AND email={?}", $uid, $this->email);
151 $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") );
152 $this->active = false;
153 }
154 }
155
156 // public function rewrite() {{{2
157
158 public function rewrite($rew, $uid)
159 {
160 if ($this->rewrite == $rew) {
161 return;
162 }
163 if (!$rew || !isvalid_email($rew)) {
164 $rew = '';
165 }
166 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email);
167 $this->rewrite = $rew;
168 return;
169 }
170
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 }
186 }
187
188 // class Redirect {{{1
189
190 class Redirect
191 {
192 // properties {{{2
193
194 private $flag_active = 'active';
195 private $uid;
196
197 public $emails;
198 public $bogo;
199
200 // constructor {{{2
201
202 public function __construct($_uid)
203 {
204 $this->uid=$_uid;
205 $res = XDB::iterRow("SELECT email, flags, rewrite, panne, last, panne_level
206 FROM emails
207 WHERE uid = {?} AND flags != 'filter'", $_uid);
208 $this->emails=Array();
209 while ($row = $res->next()) {
210 $this->emails[] = new Email($row);
211 }
212 $this->bogo = new Bogo($_uid);
213 }
214
215 // public function other_active() {{{2
216
217 public function other_active($email)
218 {
219 foreach ($this->emails as $mail) {
220 if ($mail->email!=$email && $mail->active) {
221 return true;
222 }
223 }
224 return false;
225 }
226
227 // public function delete_email() {{{2
228
229 public function delete_email($email)
230 {
231 if (!$this->other_active($email)) {
232 return ERROR_INACTIVE_REDIRECTION;
233 }
234 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
235 $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
236 foreach ($this->emails as $i=>$mail) {
237 if ($email==$mail->email) {
238 unset($this->emails[$i]);
239 }
240 }
241 check_redirect($this);
242 return SUCCESS;
243 }
244
245 // public function add_email() {{{2
246
247 public function add_email($email)
248 {
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 }
256 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
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})" : ""));
259 }
260 foreach ($this->emails as $mail) {
261 if ($mail->email == $email_stripped) {
262 return SUCCESS;
263 }
264 }
265 $this->emails[] = new Email(array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
266
267 // security stuff
268 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
269 check_redirect($this);
270 return SUCCESS;
271 }
272
273 // public function modify_email() {{{2
274
275 public function modify_email($emails_actifs, $emails_rewrite)
276 {
277 foreach ($this->emails as &$mail) {
278 if (in_array($mail->email, $emails_actifs)) {
279 $mail->activate($this->uid);
280 } else {
281 $mail->deactivate($this->uid);
282 }
283 $mail->rewrite($emails_rewrite[$mail->email], $this->uid);
284 }
285 check_redirect($this);
286 }
287
288 // public function modify_one_email() {{{2
289
290 public function modify_one_email($email, $activate)
291 {
292 $allinactive = true;
293 $thisone = false;
294 foreach ($this->emails as $i=>$mail) {
295 if ($mail->email == $email) {
296 $thisone = $i;
297 }
298 $allinactive &= !$mail->active || $mail->email == $email;
299 }
300 if ($thisone === false) {
301 return ERROR_INVALID_EMAIL;
302 }
303 if ($allinactive || $activate) {
304 $this->emails[$thisone]->activate($this->uid);
305 } else {
306 $this->emails[$thisone]->deactivate($this->uid);
307 }
308 check_redirect($this);
309 if ($allinactive && !$activate) {
310 return ERROR_INACTIVE_REDIRECTION;
311 } else {
312 return SUCCESS;
313 }
314 }
315
316 // public function modify_one_email_redirect() {{{2
317
318 public function modify_one_email_redirect($email, $redirect)
319 {
320 foreach ($this->emails as &$mail) {
321 if ($mail->email == $email) {
322 $mail->rewrite($redirect, $this->uid);
323 check_redirect($this);
324 return;
325 }
326 }
327 }
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
372
373 public function get_broken_mx()
374 {
375 $res = XDB::query("SELECT host, text
376 FROM mx_watch
377 WHERE state != 'ok'");
378 if (!$res->numRows()) {
379 return array();
380 }
381 $mxs = $res->fetchAllAssoc();
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;
391 foreach ($mxs as &$mx) {
392 foreach ($lcl_mxs as $lcl) {
393 if (fnmatch($mx['host'], $lcl)) {
394 $broken = $mx['text'];
395 break;
396 }
397 }
398 if ($broken) {
399 $mails[] = array('mail' => $mail->email, 'text' => $broken);
400 }
401 }
402 }
403 }
404 return $mails;
405 }
406 }
407
408 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
409 ?>