roundcube-identity_db/identity_db.php

116 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2019-06-01 15:36:52 +02:00
<?php
/**
* Identity DB
2019-06-01 15:36:52 +02:00
*
* Updates user's identities on each login from a database request
2019-06-01 15:36:52 +02:00
*
* @author Alice Gaudon
* @license MIT
*/
class identity_db extends rcube_plugin
2019-06-01 15:36:52 +02:00
{
private $rc;
private $config;
private $db;
function init()
{
$this->rc = rcmail::get_instance();
$this->load_config();
$this->config = $this->rc->config->get('identity_db');
2019-06-01 15:36:52 +02:00
$this->add_hook('login_after', array($this, 'login_after'));
}
function login_after($args)
{
if (!$this->config['update_on_login']) {
return $args;
}
$user = $this->rc->user;
2019-06-01 15:36:52 +02:00
$current_identities = $this->rc->user->list_emails();
$target_identities = $this->fetch_aliases($user->data['username']);
// If enabled, remove unknown identities
if ($this->config['remove_unknown_identities']) {
2019-06-01 15:36:52 +02:00
foreach ($current_identities as $existing_identity) {
if (!in_array($existing_identity['email'], $target_identities)) {
2019-06-01 15:36:52 +02:00
// Remove
$id = $existing_identity['identity_id'];
$hook_result = $this->rc->plugins->exec_hook('identity_delete', $id);
if (!$hook_result['abort']) {
$this->rc->user->delete_identity($id);
}
}
}
}
$identity_name = '';
// Add new identities
foreach ($target_identities as $new_identity) {
$exists = false;
foreach ($current_identities as $existing_identity) {
$identity_name = $existing_identity['name']; // Make it a bit random. TODO: pick it from the alias request OR a custom request
if ($existing_identity['email'] == $new_identity) {
$exists = true;
break;
}
}
if ($exists) continue;
2019-06-01 15:36:52 +02:00
// Add
$hook_result = $this->rc->plugins->exec_hook('identity_create', array(
'login' => false, // Most often triggered when the use was created long ago
'record' => array(
'user_id' => $this->rc->user->ID,
'standard' => $new_identity == $user->data['username'] ? 1 : 0,
2019-06-01 15:36:52 +02:00
'email' => $new_identity,
'name' => $identity_name
),
));
if (!$hook_result['abort'] && $hook_result['record']['email']) {
$user->insert_identity($hook_result['record']);
2019-06-01 15:36:52 +02:00
}
}
return $args;
}
function fetch_aliases($username) {
$aliases = array();
$dbh = $this->get_dbh();
$result = $dbh->query(preg_replace('/%u/', $dbh->escape($username), $this->config['identities_query']));
2019-06-01 15:36:52 +02:00
while ($row = $dbh->fetch_array($result)) {
array_push($aliases, $row[0]);
}
if(!in_array($username, $aliases)) {
array_push($aliases, $username);
}
return $aliases;
}
function get_dbh() {
if (!$this->db) {
if ($dsn = $this->config['dsn']) {
$this->db = rcube_db::factory($dsn);
$this->db->set_debug((bool)$this->rc->config->get('sql_debug'));
$this->db->db_connect('r'); // We only need to read
} else {
$this->db = $this->rc->get_dbh();
}
}
return $this->db;
}
}