pending commit, finished during MQ/S download ...
[platal.git] / include / emails.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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 var $state;
73 var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
74
75 // }}}
76 // {{{ constructor
77
78 function Bogo($uid)
79 {
80 $res = XDB::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
81 if ($res->numRows()) {
82 $this->state = $res->fetchOneCell();
83 } else {
84 $this->state = 'tag_and_drop_spams';
85 $res = XDB::query("INSERT INTO emails (uid,email,rewrite,panne,flags)
86 VALUES ({?},'tag_and_drop_spams','','0000-00-00','filter')", $uid);
87 }
88 }
89
90 // }}}
91 // {{{ function change()
92
93 function change($uid, $state)
94 {
95 $this->state = is_int($state) ? $this->_states[$state] : $state;
96 XDB::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
97 $this->state, $uid);
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
112 class Email
113 {
114 // {{{ properties
115
116 var $email;
117 var $active;
118 var $rewrite;
119 var $panne;
120
121 // }}}
122 // {{{ constructor
123
124 function Email($row)
125 {
126 list($this->email, $this->active, $this->rewrite, $this->panne) = $row;
127 }
128
129 // }}}
130 // {{{ function activate()
131
132 function activate($uid)
133 {
134 if (!$this->active) {
135 XDB::execute("UPDATE emails SET flags = 'active'
136 WHERE uid={?} AND email={?}", $uid, $this->email);
137 $_SESSION['log']->log("email_on", $this->email.($uid!=S::v('uid') ? "(admin on $uid)" : ""));
138 $this->active = true;
139 }
140 }
141
142 // }}}
143 // {{{ function deactivate()
144
145 function deactivate($uid)
146 {
147 if ($this->active) {
148 XDB::execute("UPDATE emails SET flags =''
149 WHERE uid={?} AND email={?}", $uid, $this->email);
150 $_SESSION['log']->log("email_off",$this->email.($uid!=S::v('uid') ? "(admin on $uid)" : "") );
151 $this->active = false;
152 }
153 }
154
155 // }}}
156 // {{{ function rewrite()
157
158 function rewrite($rew, $uid)
159 {
160 if ($this->rewrite == $rew) {
161 return;
162 }
163 XDB::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email);
164 $this->rewrite = $rew;
165 return;
166 }
167
168 // }}}
169 }
170
171 // }}}
172 // {{{ class Redirect
173
174 class Redirect
175 {
176 // {{{ properties
177
178 var $flag_active = 'active';
179 var $emails;
180 var $bogo;
181 var $uid;
182
183 // }}}
184 // {{{ function Redirect()
185
186 function Redirect($_uid)
187 {
188 $this->uid=$_uid;
189 $res = XDB::iterRow("
190 SELECT email, flags='active', rewrite, panne
191 FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid);
192 $this->emails=Array();
193 while ($row = $res->next()) {
194 $this->emails[] = new Email($row);
195 }
196 $this->bogo = new Bogo($_uid);
197 }
198
199 // }}}
200 // {{{ function other_active()
201
202 function other_active($email)
203 {
204 foreach ($this->emails as $mail) {
205 if ($mail->email!=$email && $mail->active) {
206 return true;
207 }
208 }
209 return false;
210 }
211
212 // }}}
213 // {{{ function delete_email()
214
215 function delete_email($email)
216 {
217 if (!$this->other_active($email)) {
218 return ERROR_INACTIVE_REDIRECTION;
219 }
220 XDB::execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid, $email);
221 $_SESSION['log']->log('email_del',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
222 foreach ($this->emails as $i=>$mail) {
223 if ($email==$mail->email) {
224 unset($this->emails[$i]);
225 }
226 }
227 return SUCCESS;
228 }
229
230 // }}}
231 // {{{ function add_email()
232
233 function add_email($email)
234 {
235 $email_stripped = strtolower(trim($email));
236 if (!isvalid_email($email_stripped)) {
237 return ERROR_INVALID_EMAIL;
238 }
239 if (!isvalid_email_redirection($email_stripped)) {
240 return ERROR_LOOP_EMAIL;
241 }
242 XDB::execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid, $email);
243 if ($logger = S::v('log', null)) { // may be absent --> step4.php
244 $logger->log('email_add',$email.($this->uid!=S::v('uid') ? " (admin on {$this->uid})" : ""));
245 }
246 foreach ($this->emails as $mail) {
247 if ($mail->email == $email_stripped) {
248 return SUCCESS;
249 }
250 }
251 $this->emails[] = new Email(array($email,1,'','0000-00-00'));
252 return SUCCESS;
253 }
254
255 // }}}
256 // {{{ function modify_email()
257
258 function modify_email($emails_actifs,$emails_rewrite)
259 {
260 foreach ($this->emails as $i=>$mail) {
261 if (in_array($mail->email,$emails_actifs)) {
262 $this->emails[$i]->activate($this->uid);
263 } else {
264 $this->emails[$i]->deactivate($this->uid);
265 }
266 $this->emails[$i]->rewrite($emails_rewrite[$mail->email], $this->uid);
267 }
268 }
269
270 function modify_one_email($email, $activate) {
271 foreach ($this->emails as $i=>$mail) {
272 if ($mail->email == $email) {
273 if ($activate)
274 $this->emails[$i]->activate($this->uid);
275 else
276 $this->emails[$i]->deactivate($this->uid);
277 }
278 }
279 }
280
281 // }}}
282 }
283
284 // }}}
285
286 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
287 ?>