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. --- core/JabberAuth.php | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 core/JabberAuth.php (limited to 'core/JabberAuth.php') 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(); + } + } +} -- cgit v1.1