PHP5-ize all base classes...
[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
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{
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
0337d704 41 SET flags=CONCAT(flags,',','bestalias')
3c1e6a1e 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
50function 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
68class Bogo
69{
70 // {{{ properties
a3a049fc 71
0337d704 72 var $state;
94f3f9ba 73 var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
0337d704 74
75 // }}}
76 // {{{ constructor
a3a049fc 77
0337d704 78 function Bogo($uid)
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()) {
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 }
0337d704 91 }
92
93 // }}}
94 // {{{ function change()
95
96 function change($uid, $state)
97 {
3c1e6a1e 98 $this->state = is_int($state) ? $this->_states[$state] : $state;
99 XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
a3a049fc 100 $this->state, $uid);
0337d704 101 }
102
103 // }}}
104 // {{{ function level()
105
106 function level()
107 { return array_search($this->state, $this->_states); }
108
109 // }}}
110}
111
112// }}}
113// {{{ class Email
114
115class Email
116{
117 // {{{ properties
118
119 var $email;
120 var $active;
dc557110 121 var $broken;
0337d704 122 var $rewrite;
123 var $panne;
dc557110 124 var $last;
2069538b 125 var $panne_level;
0337d704 126
127 // }}}
128 // {{{ constructor
129
130 function Email($row)
131 {
2069538b 132 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
dc557110 133 $this->active = ($flags == 'active');
134 $this->broken = ($flags == 'panne');
0337d704 135 }
136
137 // }}}
138 // {{{ function activate()
139
140 function activate($uid)
141 {
0337d704 142 if (!$this->active) {
dc557110 143 XDB::execute("UPDATE emails
144 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
145 flags = 'active'
146 WHERE uid={?} AND email={?}", $uid, $this->email);
3c1e6a1e 147 $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : ""));
0337d704 148 $this->active = true;
dc557110 149 $this->broken = false;
0337d704 150 }
151 }
152
153 // }}}
154 // {{{ function deactivate()
155
156 function deactivate($uid)
157 {
0337d704 158 if ($this->active) {
08cce2ff 159 XDB::execute("UPDATE emails SET flags =''
3c1e6a1e 160 WHERE uid={?} AND email={?}", $uid, $this->email);
161 $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") );
0337d704 162 $this->active = false;
163 }
164 }
165
166 // }}}
167 // {{{ function rewrite()
168
169 function rewrite($rew, $uid)
170 {
3c1e6a1e 171 if ($this->rewrite == $rew) {
0337d704 172 return;
173 }
26dc6c43 174 if (!$rew || !isvalid_email($rew)) {
12acff5d 175 $rew = '';
176 }
3c1e6a1e 177 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email);
178 $this->rewrite = $rew;
179 return;
0337d704 180 }
181
182 // }}}
183}
184
185// }}}
186// {{{ class Redirect
187
188class Redirect
189{
190 // {{{ properties
191
192 var $flag_active = 'active';
193 var $emails;
194 var $bogo;
195 var $uid;
196
197 // }}}
198 // {{{ function Redirect()
199
200 function Redirect($_uid)
201 {
3c1e6a1e 202 $this->uid=$_uid;
08cce2ff 203 $res = XDB::iterRow("
3c1e6a1e 204 SELECT email, flags, rewrite, panne, last, panne_level
205 FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid);
206 $this->emails=Array();
0337d704 207 while ($row = $res->next()) {
3c1e6a1e 208 $this->emails[] = new Email($row);
0337d704 209 }
3c1e6a1e 210 $this->bogo = new Bogo($_uid);
0337d704 211 }
212
213 // }}}
214 // {{{ function other_active()
215
216 function other_active($email)
217 {
218 foreach ($this->emails as $mail) {
219 if ($mail->email!=$email && $mail->active) {
220 return true;
221 }
222 }
223 return false;
224 }
225
226 // }}}
227 // {{{ function delete_email()
228
229 function delete_email($email)
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
245 // }}}
246 // {{{ function add_email()
247
248 function add_email($email)
249 {
0337d704 250 $email_stripped = strtolower(trim($email));
251 if (!isvalid_email($email_stripped)) {
252 return ERROR_INVALID_EMAIL;
253 }
254 if (!isvalid_email_redirection($email_stripped)) {
255 return ERROR_LOOP_EMAIL;
256 }
08cce2ff 257 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
3c1e6a1e 258 if ($logger = S::v('log', null)) { // may be absent --> step4.php
259 $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
0337d704 260 }
3c1e6a1e 261 foreach ($this->emails as $mail) {
262 if ($mail->email == $email_stripped) {
0337d704 263 return SUCCESS;
264 }
3c1e6a1e 265 }
d2367745 266 $this->emails[] = new Email(array($email, 'active', '', '0000-00-00', '0000-00-00', 0));
ca6d07f4 267
268 // security stuff
a7de4ef7 269 check_email($email, "Ajout d'une adresse surveillée aux redirections de " . $this->uid);
ccdbc270 270 check_redirect($this);
0337d704 271 return SUCCESS;
272 }
273
274 // }}}
275 // {{{ function modify_email()
276
277 function modify_email($emails_actifs,$emails_rewrite)
278 {
3c1e6a1e 279 foreach ($this->emails as $i=>$mail) {
0337d704 280 if (in_array($mail->email,$emails_actifs)) {
281 $this->emails[$i]->activate($this->uid);
3c1e6a1e 282 } else {
0337d704 283 $this->emails[$i]->deactivate($this->uid);
3c1e6a1e 284 }
285 $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid);
0337d704 286 }
ccdbc270 287 check_redirect($this);
0337d704 288 }
289
ccdbc270 290 function modify_one_email($email, $activate)
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;
313 }
8ffa657a 314 }
315
b7582015 316 function modify_one_email_redirect($email, $redirect) {
317 foreach ($this->emails as $i=>$mail) {
318 if ($mail->email == $email) {
319 $this->emails[$i]->rewrite($redirect, $this->uid);
ccdbc270 320 check_redirect($this);
321 return;
b7582015 322 }
323 }
324 }
0337d704 325 // }}}
ccdbc270 326 // {{{ function get_broken_mx()
327
328 function get_broken_mx()
329 {
120bd636 330 $res = XDB::query("SELECT host, text
331 FROM mx_watch
332 WHERE state != 'ok'");
333 if (!$res->numRows()) {
ccdbc270 334 return array();
335 }
c754cf5b 336 $mxs = $res->fetchAllAssoc();
ccdbc270 337 $mails = array();
338 foreach ($this->emails as &$mail) {
339 if ($mail->active) {
340 list(,$domain) = explode('@', $mail->email);
341 getmxrr($domain, $lcl_mxs);
342 if (empty($lcl_mxs)) {
343 $lcl_mxs = array($domain);
344 }
345 $broken = false;
346 foreach ($mxs as &$mx) {
347 foreach ($lcl_mxs as $lcl) {
c754cf5b 348 if (fnmatch($mx['host'], $lcl)) {
ccdbc270 349 $broken = $mx['text'];
350 break;
351 }
352 }
353 if ($broken) {
354 $mails[] = array('mail' => $mail->email, 'text' => $broken);
355 }
356 }
357 }
358 }
359 return $mails;
360 }
361
362 // }}}
0337d704 363}
364
365// }}}
366
a7de4ef7 367// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
0337d704 368?>