You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
12 KiB
PHTML
316 lines
12 KiB
PHTML
16 years ago
|
<?php
|
||
14 years ago
|
# $Id$
|
||
16 years ago
|
|
||
|
/**
|
||
|
* Simple class to represent a user.
|
||
|
*/
|
||
14 years ago
|
class MailboxHandler {
|
||
16 years ago
|
|
||
|
protected $username = null;
|
||
14 years ago
|
|
||
|
public $errormsg = array();
|
||
16 years ago
|
|
||
|
public function __construct($username) {
|
||
14 years ago
|
$this->username = strtolower($username);
|
||
|
}
|
||
|
|
||
|
public function change_pass($old_password, $new_password) {
|
||
14 years ago
|
error_log('MailboxHandler->change_pass is deprecated. Please use MailboxHandler->change_pw!');
|
||
14 years ago
|
return $this->change_pw($new_password, $old_password);
|
||
16 years ago
|
}
|
||
|
|
||
16 years ago
|
/**
|
||
|
* @return boolean true on success; false on failure
|
||
|
* @param string $old_password
|
||
|
* @param string $new_passwords
|
||
14 years ago
|
* @param bool $match = true
|
||
16 years ago
|
*
|
||
|
* All passwords need to be plain text; they'll be hashed appropriately
|
||
|
* as per the configuration in config.inc.php
|
||
|
*/
|
||
14 years ago
|
public function change_pw($new_password, $old_password, $match = true) {
|
||
14 years ago
|
list(/*NULL*/,$domain) = explode('@', $username);
|
||
16 years ago
|
|
||
14 years ago
|
$E_username = escape_string($this->username);
|
||
16 years ago
|
$table_mailbox = table_by_key('mailbox');
|
||
14 years ago
|
|
||
|
if ($match == true) {
|
||
|
$active = db_get_boolean(True);
|
||
14 years ago
|
$result = db_query("SELECT password FROM $table_mailbox WHERE username='$E_username' AND active='$active'");
|
||
14 years ago
|
$result = db_assoc($result['result']);
|
||
14 years ago
|
|
||
14 years ago
|
if (pacrypt($old_password, $result['password']) != $result['password']) {
|
||
14 years ago
|
db_log ($domain, 'edit_password', "MATCH FAILURE: " . $this->username);
|
||
14 years ago
|
$this->errormsg[] = 'Passwords do not match'; # TODO: make translatable
|
||
|
return false;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
|
$set = array(
|
||
14 years ago
|
'password' => pacrypt($new_password) ,
|
||
14 years ago
|
);
|
||
14 years ago
|
|
||
14 years ago
|
$result = db_update('mailbox', 'username', $this->username, $set );
|
||
16 years ago
|
|
||
14 years ago
|
if ($result != 1) {
|
||
14 years ago
|
db_log ($domain, 'edit_password', "FAILURE: " . $this->username);
|
||
14 years ago
|
$this->errormsg[] = Lang::read('pEdit_mailbox_result_error');
|
||
14 years ago
|
return false;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
db_log ($domain, 'edit_password', $this->username);
|
||
14 years ago
|
return true;
|
||
16 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Attempt to log a user in.
|
||
|
* @param string $username
|
||
|
* @param string $password
|
||
|
* @return boolean true on successful login (i.e. password matches etc)
|
||
|
*/
|
||
|
public static function login($username, $password) {
|
||
|
$username = escape_string($username);
|
||
|
|
||
|
$table_mailbox = table_by_key('mailbox');
|
||
|
$active = db_get_boolean(True);
|
||
15 years ago
|
$query = "SELECT password FROM $table_mailbox WHERE username='$username' AND active='$active'";
|
||
16 years ago
|
|
||
|
$result = db_query ($query);
|
||
|
if ($result['rows'] == 1)
|
||
|
{
|
||
|
$row = db_array ($result['result']);
|
||
16 years ago
|
$crypt_password = pacrypt ($password, $row['password']);
|
||
16 years ago
|
|
||
16 years ago
|
if($row['password'] == $crypt_password) {
|
||
16 years ago
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
14 years ago
|
/**
|
||
|
* Add mailbox
|
||
|
* @param password string password of account
|
||
|
* @param gen boolean
|
||
|
* @param name string
|
||
|
*
|
||
|
*/
|
||
14 years ago
|
public function add($password, $name = '', $quota = -999, $active = true, $mail = true ) {
|
||
|
# FIXME: default value of $quota (-999) is intentionally invalid. Add fallback to default quota.
|
||
14 years ago
|
# Solution: Invent an sub config class with additional informations about domain based configs like default qouta.
|
||
14 years ago
|
# FIXME: Should the parameters be optional at all?
|
||
14 years ago
|
# TODO: check if parameters are valid/allowed (quota?).
|
||
|
# TODO: most code should live in a separate function that can be used by add and edit.
|
||
14 years ago
|
# TODO: On the longer term, the web interface should also use this class.
|
||
14 years ago
|
|
||
|
# TODO: copy/move all checks and validations from create-mailbox.php here
|
||
|
|
||
14 years ago
|
$username = $this->username;
|
||
14 years ago
|
list($local_part,$domain) = explode ('@', $username);
|
||
14 years ago
|
|
||
14 years ago
|
|
||
|
#TODO: more self explaining language strings!
|
||
14 years ago
|
if(!check_mailbox ($domain)) {
|
||
|
$this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error3');
|
||
14 years ago
|
return false;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
# check if an alias with this name already exists
|
||
|
$result = db_query ("SELECT * FROM " . table_by_key('alias') . " WHERE address='" . escape_string($username) . "'");
|
||
14 years ago
|
if ($result['rows'] == 1){
|
||
|
$this->errormsg[] = Lang::read('pCreate_mailbox_username_text_error2');
|
||
14 years ago
|
return false;
|
||
14 years ago
|
}
|
||
|
|
||
|
$plain = $password;
|
||
|
$password = pacrypt ($password);
|
||
|
|
||
14 years ago
|
# TODO: if we want to have the encryption method in the encrypted password string, it should be done in pacrypt(). No special handling here!
|
||
|
# if ( preg_match("/^dovecot:/", Config::read('encrypt')) ) {
|
||
|
# $split_method = preg_split ('/:/', Config::read('encrypt'));
|
||
|
# $method = strtoupper($split_method[1]);
|
||
|
# $password = '{' . $method . '}' . $password;
|
||
|
# }
|
||
14 years ago
|
|
||
14 years ago
|
#TODO: 2nd clause should be the first for self explaining code.
|
||
|
#TODO: When calling config::Read with parameter we sould be right that read return false if the parameter isn't in our config file.
|
||
14 years ago
|
if(Config::read('maildir_name_hook') != 'NO' && function_exists(Config::read('maildir_name_hook')) ) {
|
||
|
$hook_func = $CONF['maildir_name_hook'];
|
||
|
$maildir = $hook_func ($fDomain, $fUsername);
|
||
|
}
|
||
|
elseif (Config::read('domain_path') == "YES")
|
||
14 years ago
|
{
|
||
|
if (Config::read('domain_in_mailbox') == "YES")
|
||
|
{
|
||
14 years ago
|
$maildir = $domain . "/" . $username . "/";
|
||
14 years ago
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
$maildir = $domain . "/" . $local_part . "/";
|
||
14 years ago
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
14 years ago
|
$maildir = $username . "/";
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
db_begin();
|
||
14 years ago
|
|
||
14 years ago
|
$active = db_get_boolean($active);
|
||
|
$quota = multiply_quota ($quota);
|
||
|
|
||
|
$alias_data = array(
|
||
|
'address' => $username,
|
||
|
'goto' => $username,
|
||
14 years ago
|
'domain' => $domain,
|
||
|
'active' => $active,
|
||
|
);
|
||
|
|
||
14 years ago
|
$result = db_insert('alias', $alias_data);
|
||
14 years ago
|
#MARK: db_insert returns true/false??
|
||
14 years ago
|
if ($result != 1)
|
||
|
{
|
||
14 years ago
|
$this->errormsg[] = Lang::read('pAlias_result_error') . "\n($username -> $username)\n";
|
||
|
return false;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
$mailbox_data = array(
|
||
|
'username' => $username,
|
||
14 years ago
|
'password' => $password,
|
||
|
'name' => $name,
|
||
|
'maildir' => $maildir,
|
||
|
'local_part' => $local_part,
|
||
|
'quota' => $quota,
|
||
|
'domain' => $domain,
|
||
|
'active' => $active,
|
||
14 years ago
|
);
|
||
|
$result = db_insert('mailbox', $mailbox_data);
|
||
14 years ago
|
#MARK: Same here!
|
||
14 years ago
|
if ($result != 1 || !mailbox_postcreation($username,$domain,$maildir, $quota)) {
|
||
|
$this->errormsg[] = Lang::read('pCreate_mailbox_result_error') . "\n($username)\n";
|
||
|
db_rollback();
|
||
|
return false;
|
||
|
} else {
|
||
|
db_commit();
|
||
14 years ago
|
db_log ($domain, 'create_mailbox', $username);
|
||
14 years ago
|
|
||
|
|
||
|
if ($mail == true)
|
||
|
{
|
||
14 years ago
|
# TODO: move "send the mail" to a function
|
||
|
$fTo = $username;
|
||
14 years ago
|
$fFrom = Config::read('admin_email');
|
||
14 years ago
|
$fSubject = Lang::read('pSendmail_subject_text');
|
||
|
$fBody = Config::read('welcome_text');
|
||
14 years ago
|
|
||
14 years ago
|
if (!smtp_mail ($fTo, $fFrom, $fSubject, $fBody))
|
||
14 years ago
|
{
|
||
|
$this->errormsg[] = Lang::read('pSendmail_result_error');
|
||
14 years ago
|
return false;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
create_mailbox_subfolders($username,$plain);
|
||
14 years ago
|
|
||
|
}
|
||
14 years ago
|
return true;
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
public function view() {
|
||
|
|
||
|
$username = $this->username;
|
||
|
$table_mailbox = table_by_key('mailbox');
|
||
14 years ago
|
|
||
|
# TODO: check if DATE_FORMAT works in MySQL and PostgreSQL
|
||
|
# TODO: maybe a more fine-grained date format would be better for non-CLI usage
|
||
14 years ago
|
$result = db_query("SELECT username, name, maildir, quota, local_part, domain, DATE_FORMAT(created, '%d.%m.%y') AS created, DATE_FORMAT(modified, '%d.%m.%y') AS modified, active FROM $table_mailbox WHERE username='$username'");
|
||
|
if ($result['rows'] != 0) {
|
||
|
$this->return = db_array($result['result']);
|
||
14 years ago
|
return true;
|
||
14 years ago
|
}
|
||
|
$this->errormsg = $result['error'];
|
||
14 years ago
|
return false;
|
||
14 years ago
|
}
|
||
|
|
||
|
public function delete() {
|
||
|
$username = $this->username;
|
||
14 years ago
|
list(/*$local_part*/,$domain) = explode ('@', $username);
|
||
14 years ago
|
|
||
14 years ago
|
$E_username = escape_string($username);
|
||
|
$E_domain = escape_string($domain);
|
||
|
|
||
14 years ago
|
#TODO: At this level of table by key calls we should think about a solution in our query function and drupal like {mailbox} {alias}.
|
||
|
# Pseudocode for db_query etc.
|
||
|
# if {} in query then
|
||
|
# table_by_key( content between { and } )
|
||
|
# else error
|
||
|
|
||
14 years ago
|
$table_mailbox = table_by_key('mailbox');
|
||
|
$table_alias = table_by_key('alias');
|
||
|
$table_vacation = table_by_key('vacation');
|
||
|
$table_vacation_notification = table_by_key('vacation_notification');
|
||
|
|
||
14 years ago
|
db_begin();
|
||
14 years ago
|
|
||
|
#TODO: ture/false replacement!
|
||
14 years ago
|
$error = 0;
|
||
|
|
||
|
$result = db_query("SELECT * FROM $table_alias WHERE address = '$E_username' AND domain = '$domain'");
|
||
14 years ago
|
if($result['rows'] == 1) {
|
||
|
$result = db_delete('alias', 'address', $username);
|
||
14 years ago
|
db_log ($domain, 'delete_alias', $username);
|
||
14 years ago
|
} else {
|
||
|
$this->errormsg[] = "no alias $username"; # todo: better message, make translatable
|
||
|
$error = 1;
|
||
14 years ago
|
}
|
||
|
|
||
|
/* is there a mailbox? if do delete it from orbit; it's the only way to be sure */
|
||
14 years ago
|
$result = db_query ("SELECT * FROM $table_mailbox WHERE username='$E_username' AND domain='$domain'");
|
||
14 years ago
|
if ($result['rows'] == 1)
|
||
|
{
|
||
|
$result = db_delete('mailbox', 'username', $username);
|
||
|
$postdel_res=mailbox_postdeletion($username,$domain);
|
||
|
if ($result != 1 || !$postdel_res)
|
||
|
{
|
||
|
|
||
|
$tMessage = Lang::read('pDelete_delete_error') . "$username (";
|
||
14 years ago
|
if ($result['rows']!=1) # TODO: invalid test, $result is from db_delete and only contains the number of deleted rows
|
||
14 years ago
|
{
|
||
|
$tMessage.='mailbox';
|
||
|
if (!$postdel_res) $tMessage.=', ';
|
||
14 years ago
|
$this->errormsg[] = "no mailbox $username"; # todo: better message, make translatable
|
||
|
$error = 1;
|
||
14 years ago
|
}
|
||
|
if (!$postdel_res)
|
||
|
{
|
||
|
$tMessage.='post-deletion';
|
||
14 years ago
|
$this->errormsg[] = "post-deletion script failed"; # todo: better message, make translatable
|
||
|
$error = 1;
|
||
14 years ago
|
}
|
||
|
$this->errormsg[] = $tMessage.')';
|
||
14 years ago
|
# TODO: does db_rollback(); make sense? Not sure because mailbox_postdeletion was already called (move the call checking the db_delete result?)
|
||
|
# TODO: maybe mailbox_postdeletion should be run after all queries, just before commit/rollback
|
||
|
$error = 1;
|
||
|
# return false; # TODO: does this make sense? Or should we still cleanup vacation and vacation_notification?
|
||
14 years ago
|
}
|
||
14 years ago
|
db_log ($domain, 'delete_mailbox', $username);
|
||
14 years ago
|
} else {
|
||
|
$this->errormsg[] = "no mailbox $username"; # TODO: better message, make translatable
|
||
|
$error = 1;
|
||
14 years ago
|
}
|
||
14 years ago
|
$result = db_query("SELECT * FROM $table_vacation WHERE email = '$E_username' AND domain = '$domain'");
|
||
14 years ago
|
if($result['rows'] == 1) {
|
||
|
db_delete('vacation', 'email', $username);
|
||
14 years ago
|
db_delete('vacation_notification', 'on_vacation', $username); # TODO: delete vacation_notification independent of vacation? (in case of "forgotten" vacation_notification entries)
|
||
14 years ago
|
}
|
||
14 years ago
|
db_commit();
|
||
|
if ($error != 0) return false;
|
||
|
return true;
|
||
14 years ago
|
}
|
||
|
|
||
16 years ago
|
}
|
||
14 years ago
|
|
||
|
/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
|