From 8cd148bdb2be52b16cc4dc57a501f194c1ffb58b Mon Sep 17 00:00:00 2001 From: Christoph Burschka Date: Mon, 4 Apr 2016 18:35:21 +0000 Subject: Add multi-domain support to htpasswd plugin. Each domain can now use its own htpasswd file. --- plugins/htpasswd/BridgeHtpasswd.php | 8 ++++++-- plugins/htpasswd/README.md | 9 +++++++++ plugins/htpasswd/htpasswd.module | 25 ++++++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/plugins/htpasswd/BridgeHtpasswd.php b/plugins/htpasswd/BridgeHtpasswd.php index 51b6a94..62ac1f4 100644 --- a/plugins/htpasswd/BridgeHtpasswd.php +++ b/plugins/htpasswd/BridgeHtpasswd.php @@ -9,11 +9,15 @@ class BridgeHtpasswd extends EjabberdAuthBridge { $this->config = $config; } + function getData($server) { + return array_key_exists($server, $this->data) ? $this->data[$server] : $this->data[NULL]; + } + function isuser($username, $server) { - return array_key_exists($username, $this->data); + return array_key_exists($username, $this->getData($server)); } function auth($username, $server, $password) { - return $this->isuser($username, $server) && htpasswd_check($password, $this->data[$username], $this->config); + return $this->isuser($username, $server) && htpasswd_check($password, $this->getData($server)[$username], $this->config); } } diff --git a/plugins/htpasswd/README.md b/plugins/htpasswd/README.md index b7b8bcb..66f46ca 100644 --- a/plugins/htpasswd/README.md +++ b/plugins/htpasswd/README.md @@ -26,3 +26,12 @@ This configuration must be entered into plugin_conf in config.php: 'htpasswd_file' => '', 'plain' => FALSE, // optional ] + +Each domain can use a separate file, as in: + + 'plugin_conf' => [ + 'htpasswd_file' => [ + 'example.com' => '', + 'example.org' => '', + ] + ] diff --git a/plugins/htpasswd/htpasswd.module b/plugins/htpasswd/htpasswd.module index b97de52..6e1c9d2 100644 --- a/plugins/htpasswd/htpasswd.module +++ b/plugins/htpasswd/htpasswd.module @@ -1,8 +1,23 @@ []]; + $file = $config['file']; + if (!is_array($file)) { + $file = [NULL => $file]; + } + foreach ($file as $d => $f) { + $data[$d] = htpasswd_load($f); + } + + // Load the plugin. + require_once __DIR__ . '/BridgeHtpasswd.php'; + require_once __DIR__ . '/htpasswd.inc'; + return new BridgeHtpasswd($data, $config); +} + +function htpasswd_load($file) { + $data = []; if (file_exists($file) && is_readable($file)) { $lines = explode("\n", trim(file_get_contents($file))); foreach ($lines as $line) { @@ -10,9 +25,5 @@ function htpasswd_init($config) { $data[$user] = $password; } } - - // Load the plugin. - require_once __DIR__ . '/BridgeHtpasswd.php'; - require_once __DIR__ . '/htpasswd.inc'; - return new BridgeHtpasswd($data, $config); + return $data; } -- cgit v1.1