$m2 */ function email_cmp($m1,$m2) { return strcmp($m1->email,$m2->email); } class Email { /** email */ var $email; /** flags */ var $flags; /** constructor. * @param $_email the email string * @param $_flags the flags */ function Email($_email, $_flags) { $this->email = $_email; $this->flags = $_flags; } /** checks if the given string is a valid email. * @param $_string the string to check * @return true if the string is a valid email, false else */ function Check($_string) { if(function_exists('isvalid_email_redirection')) return isvalid_email_redirection($_string); return preg_match("/^[\w\-.]+(\+[\w\-.]+)?@[\w\-.]+\.[a-zA-Z]{2,44}$/", $_string); } /** returns if the email is active or not. * @return true if $this->flags contains active */ function is_active() { return (stristr($this->flags, 'active')!==false); } /** toggle the state of a flag. * @param $_flag the flag to toggle * @return void */ function toggle_flag($_flag) { $flags = explode(",", $this->flags); $key = array_search($_flag,$flags); if($key===NULL || $key===false) $flags[]=$_flag; else unset($flags[$key]); $this->flags = implode(",",$flags); } /** add the email in the database. * @param $_uid the uid of the user * @param $_mid the id of the mail into the user mail list * @return void */ function commit($_uid,$_mid) { mysql_query("REPLACE INTO emails " ."SET uid='{$_uid}',mid='$_mid',email='{$this->email}',flags='{$this->flags}'"); } } class EmailPool { /** emails list */ var $emails; /** uid */ var $uid; /** constructor * @param $_uid id of the user */ function EmailPool($_uid) { global $philter; $this->uid = $_uid; $this->emails = array(); $sql = mysql_query("SELECT * FROM emails WHERE uid='$_uid' ORDER BY email"); while($res = mysql_fetch_assoc($sql)) { $this->emails[$res['mid']] = new Email($res['email'], $res['flags']); } } /** create the part of the form for the mail pool. * @return the string containing the form */ function to_form() { $res = "
\n" . "\n" . "\n" . " \n" . " \n" . "\n"; $pair = true; foreach($this->emails as $id => $email) { $res .= "\n" . "\n" . "\n" . "\n" . "\n"; $pair = !$pair; } $res .= "
"._i18n('email').""._i18n('active')." 
".$email->email."\n" . " is_active() ? " checked=\"checked\"" : "")." />
\n" . "
\n" . " \n" . " \n" . "

\n" . "
\n"; $val = _i18n('your_email'); $res .= "
\n" . _i18n('pool_help2') . "\n" . "\n" . "
\n"; return $res; } /** return the string containing the list of $this->emails in javascript. * @return the JS code */ function to_js() { $res = ""; foreach($this->emails as $id=>$mail) $res .= "mail_pool[$id] = '{$mail->email}';\n"; return $res; } /** compute a new free id for a new mail. * @return the new free id */ function new_mail_id() { $i=0; while(array_key_exists($i,$this->emails)) $i++; return $i; } /** handle the data from the form * @return true if all is ok, false and sets $philter->error() else */ function handle_form() { global $philter; if(isset($_POST['emails']['add'])) { // add an email to the pool if(Email::Check($_POST['emails']['new'])) { $new_mail = strtolower($_POST['emails']['new']); // we check that the email is not already there foreach($this->emails as $id=>$key) if($key->email == $new_mail) { $philter->set_error("$new_mail "._i18n('pool_err_already')); return false; } // then we compute one free id, and we add it to the list $mid = $this->new_mail_id(); $this->emails[$mid] = new Email($new_mail, 'active'); $this->emails[$mid]->commit($this->uid, $mid); uasort($this->emails, "email_cmp"); } else { $philter->set_error($_POST['emails']['new'].' '._i18n('pool_err_not_valid')); return false; } } elseif(isset($_POST['emails']['del']) && $_POST['emails']['del'] != -1) { // delete one email $del_id = $_POST['emails']['del']; // we compute a list of all actives emails $allow = false; foreach($this->emails as $id=>$mail) { if($del_id!=$id && $mail->is_active()) { $allow = true; break; } } if($allow && isset($this->emails[$del_id])) { list($count) = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM actions " ."WHERE uid='{$this->uid}' AND pid='" .FwdPlugin::rtti()."' AND data='$del_id'")); if($count) { $philter->set_error(_i18n('pool_err_active')); return false; } else { mysql_query("DELETE FROM emails WHERE uid='{$this->uid}' " ."AND email='{$this->emails[$del_id]->email}'"); unset($this->emails[$del_id]); } } else { $philter->set_error(_i18n('pool_err_need_one')); return false; } } elseif(isset($_POST['emails']['apply'])) { // apply actives changes $one_active = false; foreach($this->emails as $id=>$mail) if(isset($_POST['emails'][$id]['active'])) { $one_active = true; break; } if(!$one_active) { $philter->set_error(_i18n('pool_err_need_one')); return false; } foreach($this->emails as $id=>$key) { if(isset($_POST['emails'][$id]['active']) xor $this->emails[$id]->is_active()) { $this->emails[$id]->toggle_flag('active'); $this->emails[$id]->commit($this->uid, $id); } } return true; } } } /******************************************************************************** * $Id$ * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100: ********************************************************************************/ ?>