From 12f6c2acb29cbea6f41d1152eb27affa358c196f Mon Sep 17 00:00:00 2001 From: Christoph Burschka Date: Tue, 30 Oct 2012 15:28:52 +0100 Subject: Refactor to a general, flexible and extensible architecture. --- classes/JabberAuth.php | 91 ----------------------------------- classes/JabberAuthPhpBB.php | 38 --------------- config.php | 4 -- config.sample.php | 25 ++++++++++ core/JabberAuth.php | 97 ++++++++++++++++++++++++++++++++++++++ main.php | 27 +++++++---- phpbb-bridge/noweb_user.php | 17 ------- phpbb-bridge/phpbb_bootstrap.php | 40 ---------------- plugins/phpbb3/JabberAuthPhpBB.php | 38 +++++++++++++++ plugins/phpbb3/noweb_user.php | 17 +++++++ plugins/phpbb3/phpbb_bootstrap.php | 40 ++++++++++++++++ 11 files changed, 234 insertions(+), 200 deletions(-) delete mode 100644 classes/JabberAuth.php delete mode 100644 classes/JabberAuthPhpBB.php delete mode 100644 config.php create mode 100644 config.sample.php create mode 100644 core/JabberAuth.php delete mode 100644 phpbb-bridge/noweb_user.php delete mode 100644 phpbb-bridge/phpbb_bootstrap.php create mode 100644 plugins/phpbb3/JabberAuthPhpBB.php create mode 100644 plugins/phpbb3/noweb_user.php create mode 100644 plugins/phpbb3/phpbb_bootstrap.php diff --git a/classes/JabberAuth.php b/classes/JabberAuth.php deleted file mode 100644 index 69bb858..0000000 --- a/classes/JabberAuth.php +++ /dev/null @@ -1,91 +0,0 @@ -stdin = fopen('php://stdin', 'r'); - $this->stdout = fopen('php://stdout', 'w'); - $this->logfile = fopen($this->logpath . 'activity-' . date('Y-m-d') . '.log', 'a'); - $this->log('Starting...'); - $this->running = TRUE; - } - - function stop() { - $this->log("Stopping..."); - $this->running = FALSE; - } - - function run() { - while ($this->running) { - $data = $this->read(); - if ($data) { - $result = $this->execute($data); - $this->write((int)$result); - } - } - $this->log("Stopped"); - } - - function read() { - $input = fread($this->stdin, 2); - if (!$input) { - return $this->stop(); - } - - $input = unpack('n', $input); - $length = $input[1]; - if($length > 0) { - $this->log("Reading $length bytes..."); - $data = fread($this->stdin, $length); - return $data; - } - } - - function write($data) { - $this->log("OUT: $data"); - fwrite($this->stdout, pack("nn", 2, $data)); - } - - function log($data) { - fwrite($this->logfile, sprintf("%s [%d] - %s\n", date('Y-m-d H:i:s'), getmypid(), $data)); - } - - function execute($data) { - $args = explode(':', $data); - $command = array_shift($args); - // Only log the username for security. - $this->log("Executing $command on {$args[0]}"); - - switch ($command) { - case 'isuser': - list($username, $server) = $args; - return $this->isuser($username, $server); - case 'auth': - list($username, $server, $password) = $args; - return $this->auth($username, $server, $password); - case 'setpass': - list($username, $server, $password) = $args; - return $this->setpass($username, $server, $password); - case 'tryregister': - list($username, $server, $password) = $args; - return $this->tryregister($username, $server, $password); - case 'removeuser': - list($username, $server) = $args; - return $this->removeuser($username, $server); - case 'removeuser3': - list($username, $server, $password) = $args; - return $this->auth($username, $server, $password) && $this->removeuser($username, $password); - default: - $this->stop(); - } - } -} diff --git a/classes/JabberAuthPhpBB.php b/classes/JabberAuthPhpBB.php deleted file mode 100644 index e60cbae..0000000 --- a/classes/JabberAuthPhpBB.php +++ /dev/null @@ -1,38 +0,0 @@ -logpath = $logpath; - parent::init(); - $this->auth = $auth; - $this->db = $db; - } - - function isuser($username, $server) { - $username_clean = utf8_clean_string($username); - $row = $this->db->sql_fetchrow($this->db->sql_query('SELECT username FROM ' . USERS_TABLE . ' WHERE username_clean = ' . "'" . $this->db->sql_escape($username_clean) . "'" . ';')); - return !empty($row); - } - - function auth($username, $server, $password) { - $result = $this->auth->login($username, $password); - return $result['status'] == LOGIN_SUCCESS; - } - - // The following functions are disabled. This script will not change the phpBB user database. - - function setpass($username, $server, $password) { - return FALSE; - } - - function tryregister($username, $server, $password) { - return FALSE; - } - - function removeuser($username, $server) { - return FALSE; - } -} diff --git a/config.php b/config.php deleted file mode 100644 index dd5f8f1..0000000 --- a/config.php +++ /dev/null @@ -1,4 +0,0 @@ - '' /* path to your phpBB3 installation */, +); + +$config['phpbb4'] = array( + 'root_path' => '' /* path to your phpBB4 installation */, +); + +$config['drupal7'] = array( + 'root_path' => '' /* path to your Drupal 7 installation */, + 'site' => 'default' /* site directory */, +); + +$config['drupal8'] = array( + 'root_path' => '' /* path to your Drupal 7 installation */, + 'site' => 'default' /* site directory */, +); + diff --git a/core/JabberAuth.php b/core/JabberAuth.php new file mode 100644 index 0000000..3e9d4df --- /dev/null +++ b/core/JabberAuth.php @@ -0,0 +1,97 @@ +stdin = fopen('php://stdin', 'r'); + $this->stdout = fopen('php://stdout', 'w'); + if (!empty($config['log_path']) && is_dir($config['log_path']) && is_writable($config['log_path'])) { + $this->logfile = fopen($config['log_path'] . 'activity-' . date('Y-m-d') . '.log', 'a'); + } + else { + $this->logfile = fopen('php://stderr', 'w'); + } + $this->log('Starting...'); + $this->running = TRUE; + } + + function stop() { + $this->log("Stopping..."); + $this->running = FALSE; + } + + function run() { + while ($this->running) { + $data = $this->read(); + if ($data) { + $result = $this->execute($data); + $this->write((int)$result); + } + } + $this->log("Stopped"); + } + + function read() { + $input = fread($this->stdin, 2); + if (!$input) { + return $this->stop(); + } + + $input = unpack('n', $input); + $length = $input[1]; + if($length > 0) { + $this->log("Reading $length bytes..."); + $data = fread($this->stdin, $length); + return $data; + } + } + + function write($data) { + $this->log("OUT: $data"); + fwrite($this->stdout, pack("nn", 2, $data)); + } + + function log($data) { + fwrite($this->logfile, sprintf("%s [%d] - %s\n", date('Y-m-d H:i:s'), getmypid(), $data)); + } + + function execute($data) { + $args = explode(':', $data); + $command = array_shift($args); + // Only log the username for security. + $this->log("Executing $command on {$args[0]}"); + + switch ($command) { + case 'isuser': + list($username, $server) = $args; + return $this->isuser($username, $server); + case 'auth': + list($username, $server, $password) = $args; + return $this->auth($username, $server, $password); + case 'setpass': + list($username, $server, $password) = $args; + return $this->setpass($username, $server, $password); + case 'tryregister': + list($username, $server, $password) = $args; + return $this->tryregister($username, $server, $password); + case 'removeuser': + list($username, $server) = $args; + return $this->removeuser($username, $server); + case 'removeuser3': + list($username, $server, $password) = $args; + return $this->auth($username, $server, $password) && $this->removeuser($username, $password); + default: + $this->stop(); + } + } +} diff --git a/main.php b/main.php index fee0f33..2388e8f 100644 --- a/main.php +++ b/main.php @@ -2,15 +2,22 @@ run(); +if (!empty($config['plugin']) && !empty($config[$config['plugin']])) { + $plugin_file = 'plugins/' . $config['plugin'] . '/' . $config['plugin'] . '.module'; + if (file_exists(ROOT . $plugin_file)) { + require_once ROOT . $plugin_file; + $function = $config['plugin'] . '_init'; + $function($config[$config['plugin']])->run(); + } + else { + fwrite($err, "Plugin <{$plugin_file}> not found.\n"); + } +} +else { + fwrite($err, 'Incomplete configuration: $config[\'plugin\'] must be set to , and $config[] populated.' . "\n"); +} diff --git a/phpbb-bridge/noweb_user.php b/phpbb-bridge/noweb_user.php deleted file mode 100644 index 3291222..0000000 --- a/phpbb-bridge/noweb_user.php +++ /dev/null @@ -1,17 +0,0 @@ -sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined('PHPBB_DB_NEW_LINK') ? PHPBB_DB_NEW_LINK : false); -// We do not need this any longer, unset for safety purposes -unset($dbpasswd); -$config = $cache->obtain_config(); diff --git a/plugins/phpbb3/JabberAuthPhpBB.php b/plugins/phpbb3/JabberAuthPhpBB.php new file mode 100644 index 0000000..e60cbae --- /dev/null +++ b/plugins/phpbb3/JabberAuthPhpBB.php @@ -0,0 +1,38 @@ +logpath = $logpath; + parent::init(); + $this->auth = $auth; + $this->db = $db; + } + + function isuser($username, $server) { + $username_clean = utf8_clean_string($username); + $row = $this->db->sql_fetchrow($this->db->sql_query('SELECT username FROM ' . USERS_TABLE . ' WHERE username_clean = ' . "'" . $this->db->sql_escape($username_clean) . "'" . ';')); + return !empty($row); + } + + function auth($username, $server, $password) { + $result = $this->auth->login($username, $password); + return $result['status'] == LOGIN_SUCCESS; + } + + // The following functions are disabled. This script will not change the phpBB user database. + + function setpass($username, $server, $password) { + return FALSE; + } + + function tryregister($username, $server, $password) { + return FALSE; + } + + function removeuser($username, $server) { + return FALSE; + } +} diff --git a/plugins/phpbb3/noweb_user.php b/plugins/phpbb3/noweb_user.php new file mode 100644 index 0000000..3291222 --- /dev/null +++ b/plugins/phpbb3/noweb_user.php @@ -0,0 +1,17 @@ +sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined('PHPBB_DB_NEW_LINK') ? PHPBB_DB_NEW_LINK : false); +// We do not need this any longer, unset for safety purposes +unset($dbpasswd); +$config = $cache->obtain_config(); -- cgit v1.1