#485: can moderate pure-HTML mails
[platal.git] / include / emails.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 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')
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
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 {
08cce2ff 80 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
0337d704 81 if ($res->numRows()) {
82 $this->state = $res->fetchOneCell();
83 } else {
94f3f9ba 84 $this->state = 'tag_and_drop_spams';
08cce2ff 85 $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
94f3f9ba 86 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
0337d704 87 }
88 }
89
90 // }}}
91 // {{{ function change()
92
93 function change($uid, $state)
94 {
0337d704 95 $this->state = is_int($state) ? $this->_states[$state] : $state;
a3a049fc 96 XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
97 $this->state, $uid);
0337d704 98 }
99
100 // }}}
101 // {{{ function level()
102
103 function level()
104 { return array_search($this->state, $this->_states); }
105
106 // }}}
107}
108
109// }}}
110// {{{ class Email
111
112class Email
113{
114 // {{{ properties
115
116 var $email;
117 var $active;
dc557110 118 var $broken;
0337d704 119 var $rewrite;
120 var $panne;
dc557110 121 var $last;
2069538b 122 var $panne_level;
0337d704 123
124 // }}}
125 // {{{ constructor
126
127 function Email($row)
128 {
2069538b 129 list($this->email, $flags, $this->rewrite, $this->panne, $this->last, $this->panne_level) = $row;
dc557110 130 $this->active = ($flags == 'active');
131 $this->broken = ($flags == 'panne');
0337d704 132 }
133
134 // }}}
135 // {{{ function activate()
136
137 function activate($uid)
138 {
0337d704 139 if (!$this->active) {
dc557110 140 XDB::execute("UPDATE emails
141 SET panne_level = IF(flags = 'panne', panne_level - 1, panne_level),
142 flags = 'active'
143 WHERE uid={?} AND email={?}", $uid, $this->email);
cab08090 144 $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : ""));
0337d704 145 $this->active = true;
dc557110 146 $this->broken = false;
0337d704 147 }
148 }
149
150 // }}}
151 // {{{ function deactivate()
152
153 function deactivate($uid)
154 {
0337d704 155 if ($this->active) {
08cce2ff 156 XDB::execute("UPDATE emails SET flags =''
0337d704 157 WHERE uid={?} AND email={?}", $uid, $this->email);
ca6d07f4 158 $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") );
0337d704 159 $this->active = false;
160 }
161 }
162
163 // }}}
164 // {{{ function rewrite()
165
166 function rewrite($rew, $uid)
167 {
ca6d07f4 168 if ($this->rewrite == $rew) {
0337d704 169 return;
170 }
ca6d07f4 171 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email);
172 $this->rewrite = $rew;
173 return;
0337d704 174 }
175
176 // }}}
177}
178
179// }}}
180// {{{ class Redirect
181
182class Redirect
183{
184 // {{{ properties
185
186 var $flag_active = 'active';
187 var $emails;
188 var $bogo;
189 var $uid;
190
191 // }}}
192 // {{{ function Redirect()
193
194 function Redirect($_uid)
195 {
ca6d07f4 196 $this->uid=$_uid;
08cce2ff 197 $res = XDB::iterRow("
ca6d07f4 198 SELECT email, flags, rewrite, panne, last, panne_level
199 FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid);
200 $this->emails=Array();
0337d704 201 while ($row = $res->next()) {
ca6d07f4 202 $this->emails[] = new Email($row);
0337d704 203 }
ca6d07f4 204 $this->bogo = new Bogo($_uid);
0337d704 205 }
206
207 // }}}
208 // {{{ function other_active()
209
210 function other_active($email)
211 {
212 foreach ($this->emails as $mail) {
213 if ($mail->email!=$email && $mail->active) {
214 return true;
215 }
216 }
217 return false;
218 }
219
220 // }}}
221 // {{{ function delete_email()
222
223 function delete_email($email)
224 {
0337d704 225 if (!$this->other_active($email)) {
226 return ERROR_INACTIVE_REDIRECTION;
227 }
08cce2ff 228 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
cab08090 229 $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
ca6d07f4 230 foreach ($this->emails as $i=>$mail) {
231 if ($email==$mail->email) {
0337d704 232 unset($this->emails[$i]);
233 }
ca6d07f4 234 }
0337d704 235 return SUCCESS;
236 }
237
238 // }}}
239 // {{{ function add_email()
240
241 function add_email($email)
242 {
0337d704 243 $email_stripped = strtolower(trim($email));
244 if (!isvalid_email($email_stripped)) {
245 return ERROR_INVALID_EMAIL;
246 }
247 if (!isvalid_email_redirection($email_stripped)) {
248 return ERROR_LOOP_EMAIL;
249 }
08cce2ff 250 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
ca6d07f4 251 if ($logger = S::v('log', null)) { // may be absent --> step4.php
252 $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
0337d704 253 }
ca6d07f4 254 foreach ($this->emails as $mail) {
255 if ($mail->email == $email_stripped) {
0337d704 256 return SUCCESS;
257 }
ca6d07f4 258 }
0337d704 259 $this->emails[] = new Email(array($email,1,'','0000-00-00'));
ca6d07f4 260
261 // security stuff
262 $res = XDB::query("SELECT state, description
263 FROM emails_watch
264 WHERE state != 'safe' AND email = {?}", $email);
265 if ($res->numRows()) {
266 $row = $res->fetchOneAssoc();
267 $message = "L'email $email vient d'être ajouté aux redirections de ". S::v('forlife')
268 . ". Cette adresse est surveillée avec l'état *" . $row['state']
269 . "* et la description :\n" . $row['description'];
270 $message = wordwrap($message);
271 require_once("diogenes/diogenes.hermes.inc.php");
272 $mailer = new HermesMailer();
273 $mailer->setFrom("webmaster@polytechnique.org");
c33efb7c 274 $mailer->addTo("hotliners@staff.polytechnique.org");
ca6d07f4 275 $mailer->setSubject("ALERTE LORS DE L'AJOUT DE REDIRECTION de "
276 . S::v('prenom') . ' ' . S::v('nom') . '(' . S::v('promo') . ')');
277 $mailer->setTxtBody($message
278 . "\n\nInformations de connexion :\n" . var_export($_SERVER, true));
279 $mailer->send();
280 }
0337d704 281 return SUCCESS;
282 }
283
284 // }}}
285 // {{{ function modify_email()
286
287 function modify_email($emails_actifs,$emails_rewrite)
288 {
ca6d07f4 289 foreach ($this->emails as $i=>$mail) {
0337d704 290 if (in_array($mail->email,$emails_actifs)) {
291 $this->emails[$i]->activate($this->uid);
ca6d07f4 292 } else {
0337d704 293 $this->emails[$i]->deactivate($this->uid);
ca6d07f4 294 }
295 $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid);
0337d704 296 }
297 }
298
8ffa657a 299 function modify_one_email($email, $activate) {
300 foreach ($this->emails as $i=>$mail) {
301 if ($mail->email == $email) {
302 if ($activate)
303 $this->emails[$i]->activate($this->uid);
304 else
305 $this->emails[$i]->deactivate($this->uid);
306 }
307 }
308 }
309
0337d704 310 // }}}
311}
312
313// }}}
314
315// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
316?>