Skip to content

Commit

Permalink
make username configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
johndoh committed Mar 24, 2012
1 parent 9db2ab9 commit e649df7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -1,6 +1,10 @@
Roundcube Webmail SAUserPrefs
=============================

2012 03 24
==========
* Make username configurable

2012 01 21
==========
* Add inital support for Larry
Expand Down
7 changes: 7 additions & 0 deletions config.inc.php.dist
Expand Up @@ -28,6 +28,13 @@ $rcmail_config['sauserprefs_sql_preference_field'] = "preference";
// name of the value field in the user prefs table, holds the value of the preference
$rcmail_config['sauserprefs_sql_value_field'] = "value";

// username of the current user in the database, normaly %u (username from the session info)
// %u is replaced with the username (from the session info)
// %l is replaced with the local part of the username (if the username is an email address)
// %d is replaced with the domain part of the username (if the username is an email address or default mail domain if not)
// %i is replaced with the email address from the user's default identity
$rcmail_config['sauserprefs_userid'] = "%u";

// username of the "global" or default settings user in the database, normaly $GLOBAL or @GLOBAL
$rcmail_config['sauserprefs_global_userid'] = "\$GLOBAL";

Expand Down
39 changes: 24 additions & 15 deletions sauserprefs.php
Expand Up @@ -18,11 +18,20 @@ class sauserprefs extends rcube_plugin
private $user_prefs;
private $addressbook = '0';
private $sa_locales = array('en', 'ja', 'ko', 'ru', 'th', 'zh');
private $sa_user;

function init()
{
$rcmail = rcmail::get_instance();
$this->load_config();
$this->sa_user = $rcmail->config->get('sauserprefs_userid', "%u");

$identity_arr = $rcmail->user->get_identity();
$identity = $identity_arr['email'];
$this->sa_user = str_replace('%u', $_SESSION['username'], $this->sa_user);
$this->sa_user = str_replace('%l', $rcmail->user->get_username('local'), $this->sa_user);
$this->sa_user = str_replace('%d', $rcmail->user->get_username('domain'), $this->sa_user);
$this->sa_user = str_replace('%i', $identity, $this->sa_user);

if ($rcmail->config->get('sauserprefs_whitelist_abook_id', false))
$this->addressbook = $rcmail->config->get('sauserprefs_whitelist_abook_id');
Expand Down Expand Up @@ -263,15 +272,15 @@ function save()

$this->db->query(
"DELETE FROM ". $rcmail->config->get('sauserprefs_sql_table_name') ."
WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $_SESSION['username'] ."'
WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $this->sa_user ."'
AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name($address['field']) ."'
AND ". $rcmail->config->get('sauserprefs_sql_value_field') ." = '". $address['value'] . "';"
);

$result = $this->db->affected_rows();

if (!$result) {
write_log('errors', 'sauserprefs error: cannot delete "' . $this->_map_pref_name($prefs[$idx]) . '" = "' . $vals[$idx] . '" for ' . $_SESSION['username']);
write_log('errors', 'sauserprefs error: cannot delete "' . $this->_map_pref_name($prefs[$idx]) . '" = "' . $vals[$idx] . '" for ' . $this->sa_user);
break;
}
}
Expand All @@ -281,13 +290,13 @@ function save()
$this->db->query(
"INSERT INTO ". $rcmail->config->get('sauserprefs_sql_table_name') ."
(".$rcmail->config->get('sauserprefs_sql_username_field').", ".$rcmail->config->get('sauserprefs_sql_preference_field').", ".$rcmail->config->get('sauserprefs_sql_value_field').")
VALUES ('". $_SESSION['username']. "', '". $this->_map_pref_name($address['field']) . "', '". $address['value'] ."')"
VALUES ('". $this->sa_user ."', '". $this->_map_pref_name($address['field']) ."', '". $address['value'] ."')"
);

$result = $this->db->affected_rows();

if (!$result) {
write_log('errors', 'sauserprefs error: cannot insert "' . $this->_map_pref_name($prefs[$idx]) . '" = "' . $vals[$idx] . '" for ' . $_SESSION['username']);
write_log('errors', 'sauserprefs error: cannot insert "' . $this->_map_pref_name($prefs[$idx]) . '" = "' . $vals[$idx] . '" for ' . $this->sa_user);
break;
}
}
Expand All @@ -298,14 +307,14 @@ function save()

$this->db->query(
"DELETE FROM ". $rcmail->config->get('sauserprefs_sql_table_name') ."
WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $_SESSION['username'] ."'
WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $this->sa_user ."'
AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name($preference) ."';"
);

$result = $this->db->affected_rows();

if (!$result) {
write_log('errors', 'sauserprefs error: cannot delete "' . $this->_map_pref_name($preference) . '" for "' . $_SESSION['username']);
write_log('errors', 'sauserprefs error: cannot delete "' . $this->_map_pref_name($preference) . '" for "' . $this->sa_user);
break;
}
}
Expand All @@ -315,14 +324,14 @@ function save()
$this->db->query(
"UPDATE ". $rcmail->config->get('sauserprefs_sql_table_name') ."
SET ". $rcmail->config->get('sauserprefs_sql_value_field') ." = '". $value ."'
WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $_SESSION['username'] ."'
WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $this->sa_user ."'
AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name($preference) ."';"
);

$result = $this->db->affected_rows();

if (!$result) {
write_log('errors', 'sauserprefs error: cannot update "' . $this->_map_pref_name($preference) . '" = "' . $value . '" for ' . $_SESSION['username']);
write_log('errors', 'sauserprefs error: cannot update "' . $this->_map_pref_name($preference) . '" = "' . $value . '" for ' . $this->sa_user);
break;
}
}
Expand All @@ -332,13 +341,13 @@ function save()
$this->db->query(
"INSERT INTO ". $rcmail->config->get('sauserprefs_sql_table_name') ."
(".$rcmail->config->get('sauserprefs_sql_username_field').", ".$rcmail->config->get('sauserprefs_sql_preference_field').", ".$rcmail->config->get('sauserprefs_sql_value_field').")
VALUES ('". $_SESSION['username'] ."', '". $this->_map_pref_name($preference) ."', '". $value ."')"
VALUES ('". $this->sa_user ."', '". $this->_map_pref_name($preference) ."', '". $value ."')"
);

$result = $this->db->affected_rows();

if (!$result) {
write_log('errors', 'sauserprefs error: cannot insert "' . $this->_map_pref_name($preference) . '" = "' . $value . '" for ' . $_SESSION['username']);
write_log('errors', 'sauserprefs error: cannot insert "' . $this->_map_pref_name($preference) . '" = "' . $value . '" for ' . $this->sa_user);
break;
}
}
Expand Down Expand Up @@ -391,7 +400,7 @@ function purge_bayes()
$queries = !is_array($rcmail->config->get('sauserprefs_bayes_delete_query')) ? array($rcmail->config->get('sauserprefs_bayes_delete_query')) : $rcmail->config->get('sauserprefs_bayes_delete_query');

foreach ($queries as $sql) {
$sql = str_replace('%u', $this->db->quote($_SESSION['username'],'text'), $sql);
$sql = str_replace('%u', $this->db->quote($this->sa_user, 'text'), $sql);
$this->db->query($sql);

if ($this->db->is_error())
Expand All @@ -417,9 +426,9 @@ function contact_add($args)

foreach ($emails as $email) {
// check address is not already whitelisted
$sql_result = $this->db->query("SELECT value FROM ". $rcmail->config->get('sauserprefs_sql_table_name') ." WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $_SESSION['username'] ."' AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name('whitelist_from') ."' AND ". $rcmail->config->get('sauserprefs_sql_value_field') ." = '". $email ."';");
$sql_result = $this->db->query("SELECT value FROM ". $rcmail->config->get('sauserprefs_sql_table_name') ." WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $this->sa_user ."' AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name('whitelist_from') ."' AND ". $rcmail->config->get('sauserprefs_sql_value_field') ." = '". $email ."';");
if ($this->db->num_rows($sql_result) == 0)
$this->db->query("INSERT INTO ". $rcmail->config->get('sauserprefs_sql_table_name') ." (". $rcmail->config->get('sauserprefs_sql_username_field') .", ". $rcmail->config->get('sauserprefs_sql_preference_field') .", ". $rcmail->config->get('sauserprefs_sql_value_field') .") VALUES ('". $_SESSION['username'] ."', '". $this->_map_pref_name('whitelist_from') ."', '". $email ."');");
$this->db->query("INSERT INTO ". $rcmail->config->get('sauserprefs_sql_table_name') ." (". $rcmail->config->get('sauserprefs_sql_username_field') .", ". $rcmail->config->get('sauserprefs_sql_preference_field') .", ". $rcmail->config->get('sauserprefs_sql_value_field') .") VALUES ('". $this->sa_user ."', '". $this->_map_pref_name('whitelist_from') ."', '". $email ."');");
}
}

Expand All @@ -446,7 +455,7 @@ function contact_delete($args)
$emails = $this->_gen_email_arr($contacts->get_record($id, true));

foreach ($emails as $email)
$this->db->query("DELETE FROM ". $rcmail->config->get('sauserprefs_sql_table_name') ." WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $_SESSION['username'] ."' AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name('whitelist_from') ."' AND ". $rcmail->config->get('sauserprefs_sql_value_field') ." = '". $email ."';");
$this->db->query("DELETE FROM ". $rcmail->config->get('sauserprefs_sql_table_name') ." WHERE ". $rcmail->config->get('sauserprefs_sql_username_field') ." = '". $this->sa_user ."' AND ". $rcmail->config->get('sauserprefs_sql_preference_field') ." = '". $this->_map_pref_name('whitelist_from') ."' AND ". $rcmail->config->get('sauserprefs_sql_value_field') ." = '". $email ."';");
}

$contacts->close();
Expand Down Expand Up @@ -476,7 +485,7 @@ private function _load_global_prefs()

private function _load_user_prefs()
{
$this->user_prefs = $this->_load_prefs($_SESSION['username']);
$this->user_prefs = $this->_load_prefs($this->sa_user);
}

private function _load_prefs($user)
Expand Down

0 comments on commit e649df7

Please sign in to comment.