2 /***************************************************************************
3 * Copyright (C) 2003-2004 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)
37 $res = $globals->xdb
->query("SELECT COUNT(*) FROM aliases WHERE id={?} AND FIND_IN_SET('bestalias',flags) AND type!='homonyme'", $uid);
38 if ($n = $res->fetchOneCell()) {
41 $globals->xdb
->execute("UPDATE aliases
42 SET flags=CONCAT(flags,',','bestalias')
43 WHERE id={?} AND type!='homonyme'
44 ORDER BY !FIND_IN_SET('usage',flags),alias LIKE '%.%', LENGTH(alias)
49 // {{{ function valide_email()
51 function valide_email($str)
53 $em = trim(rtrim($str));
54 $em = str_replace('<', '', $em);
55 $em = str_replace('>', '', $em);
56 list($ident, $dom) = explode('@', $em);
57 if ($dom == $globals->mail
->domain
or $dom == $globals->mail
->domain2
) {
58 list($ident1) = explode('_', $ident);
59 list($ident) = explode('+', $ident1);
61 return $ident . '@' . $dom;
72 var $_states = Array('let_spams', 'tag_spams', 'tag_and_drop_spams', 'drop_spams');
80 $res = $globals->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 = $globals->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)
96 $this->state
= is_int($state) ?
$this->_states
[$state] : $state;
97 $globals->xdb
->execute('UPDATE emails SET email={?} WHERE uid={?} AND flags = "filter"', $this->state
, $uid);
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)
135 if (!$this->active
) {
136 $globals->xdb
->execute("UPDATE emails SET flags = 'active'
137 WHERE uid={?} AND email={?}", $uid, $this->email
);
138 $_SESSION['log']->log("email_on", $this->email
.($uid!=Session
::getInt('uid') ?
"(admin on $uid)" : ""));
139 $this->active
= true
;
144 // {{{ function deactivate()
146 function deactivate($uid)
150 $globals->xdb
->execute("UPDATE emails SET flags =''
151 WHERE uid={?} AND email={?}", $uid, $this->email
);
152 $_SESSION['log']->log("email_off",$this->email
.($uid!=Session
::getInt('uid') ?
"(admin on $uid)" : "") );
153 $this->active
= false
;
158 // {{{ function rewrite()
160 function rewrite($rew, $uid)
163 if ($this->rewrite
== $rew) {
166 $globals->xdb
->execute('UPDATE emails SET rewrite={?} WHERE uid={?} AND email={?}', $rew, $uid, $this->email
);
167 $this->rewrite
= $rew;
175 // {{{ class Redirect
181 var $flag_active = 'active';
187 // {{{ function Redirect()
189 function Redirect($_uid)
193 $res = $globals->xdb
->iterRow("
194 SELECT email, flags='active', rewrite, panne
195 FROM emails WHERE uid = {?} AND flags != 'filter'", $_uid);
196 $this->emails
=Array();
197 while ($row = $res->next()) {
198 $this->emails
[] = new Email($row);
200 $this->bogo
= new Bogo($_uid);
204 // {{{ function other_active()
206 function other_active($email)
208 foreach ($this->emails
as $mail) {
209 if ($mail->email
!=$email && $mail->active
) {
217 // {{{ function delete_email()
219 function delete_email($email)
222 if (!$this->other_active($email)) {
223 return ERROR_INACTIVE_REDIRECTION
;
225 $globals->xdb
->execute('DELETE FROM emails WHERE uid={?} AND email={?}', $this->uid
, $email);
226 $_SESSION['log']->log('email_del',$email.($this->uid
!=Session
::getInt('uid') ?
" (admin on {$this->uid})" : ""));
227 foreach ($this->emails
as $i=>$mail) {
228 if ($email==$mail->email
) {
229 unset($this->emails
[$i]);
236 // {{{ function add_email()
238 function add_email($email)
241 $email_stripped = strtolower(trim($email));
242 if (!isvalid_email($email_stripped)) {
243 return ERROR_INVALID_EMAIL
;
245 if (!isvalid_email_redirection($email_stripped)) {
246 return ERROR_LOOP_EMAIL
;
248 $globals->xdb
->execute('REPLACE INTO emails (uid,email,flags) VALUES({?},{?},"active")', $this->uid
, $email);
249 if ($logger = Session
::getMixed('log', null
)) { // may be absent --> step4.php
250 $logger->log('email_add',$email.($this->uid
!=Session
::getInt('uid') ?
" (admin on {$this->uid})" : ""));
252 foreach ($this->emails
as $mail) {
253 if ($mail->email
== $email_stripped) {
257 $this->emails
[] = new Email(array($email,1,'','0000-00-00'));
262 // {{{ function modify_email()
264 function modify_email($emails_actifs,$emails_rewrite)
267 foreach ($this->emails
as $i=>$mail) {
268 if (in_array($mail->email
,$emails_actifs)) {
269 $this->emails
[$i]->activate($this->uid
);
271 $this->emails
[$i]->deactivate($this->uid
);
273 $this->emails
[$i]->rewrite($emails_rewrite[$mail->email
], $this->uid
);
282 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: