2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once("xorg.misc.inc.php");
27 define("ERROR_INACTIVE_REDIRECTION", 2);
28 define("ERROR_INVALID_EMAIL", 3);
29 define("ERROR_LOOP_EMAIL", 4);
32 // {{{ function fix_bestalias()
34 function fix_bestalias($uid)
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()) {
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)
48 // {{{ function valide_email()
50 function valide_email($str)
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);
62 return $ident . '@' . $dom;
73 var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
80 $res = XDB
::query('SELECT email FROM emails WHERE uid={?} AND flags="filter"', $uid);
81 if ($res->numRows()) {
82 $this->state
= $res->fetchOneCell();
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);
91 // {{{ function change()
93 function change($uid, $state)
95 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
96 XDB
::execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"',
101 // {{{ function level()
104 { return array_search($this->state
, $this->_states
); }
126 list($this->email
, $this->active
, $this->rewrite
, $this->panne
) = $row;
130 // {{{ function activate()
132 function activate($uid)
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
;
143 // {{{ function deactivate()
145 function deactivate($uid)
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
;
156 // {{{ function rewrite()
158 function rewrite($rew, $uid)
160 if ($this->rewrite
== $rew) {
163 XDB
::execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email
);
164 $this->rewrite
= $rew;
172 // {{{ class Redirect
178 var $flag_active = 'active';
184 // {{{ function Redirect()
186 function Redirect($_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);
196 $this->bogo
= new Bogo($_uid);
200 // {{{ function other_active()
202 function other_active($email)
204 foreach ($this->emails
as $mail) {
205 if ($mail->email
!=$email && $mail->active
) {
213 // {{{ function delete_email()
215 function delete_email($email)
217 if (!$this->other_active($email)) {
218 return ERROR_INACTIVE_REDIRECTION
;
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]);
231 // {{{ function add_email()
233 function add_email($email)
235 $email_stripped = strtolower(trim($email));
236 if (!isvalid_email($email_stripped)) {
237 return ERROR_INVALID_EMAIL
;
239 if (!isvalid_email_redirection($email_stripped)) {
240 return ERROR_LOOP_EMAIL
;
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})" : ""));
246 foreach ($this->emails
as $mail) {
247 if ($mail->email
== $email_stripped) {
251 $this->emails
[] = new Email(array($email,1,'','0000-00-00'));
256 // {{{ function modify_email()
258 function modify_email($emails_actifs,$emails_rewrite)
260 foreach ($this->emails
as $i=>$mail) {
261 if (in_array($mail->email
,$emails_actifs)) {
262 $this->emails
[$i]->activate($this->uid
);
264 $this->emails
[$i]->deactivate($this->uid
);
266 $this->emails
[$i]->rewrite($emails_rewrite[$mail->email
], $this->uid
);
270 function modify_one_email($email, $activate) {
271 foreach ($this->emails
as $i=>$mail) {
272 if ($mail->email
== $email) {
274 $this->emails
[$i]->activate($this->uid
);
276 $this->emails
[$i]->deactivate($this->uid
);
286 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: