summaryrefslogtreecommitdiff
path: root/lib/Kolab
diff options
context:
space:
mode:
authorThomas Bruederli <bruederli@kolabsys.com>2013-01-16 14:37:00 (GMT)
committerThomas Bruederli <bruederli@kolabsys.com>2013-01-16 14:37:00 (GMT)
commita261ddd670c5617c996f4155dfea483228628ca6 (patch)
tree7b2234366da9cabadb2ff6f592b6465d8b40b0a2 /lib/Kolab
downloadkolab-freebusy-a261ddd670c5617c996f4155dfea483228628ca6.tar.gz
Initial import
Diffstat (limited to 'lib/Kolab')
-rw-r--r--lib/Kolab/FreeBusy/Config.php92
-rw-r--r--lib/Kolab/FreeBusy/Directory.php63
-rw-r--r--lib/Kolab/FreeBusy/DirectoryLDAP.php40
-rw-r--r--lib/Kolab/FreeBusy/DirectoryStatic.php35
-rw-r--r--lib/Kolab/FreeBusy/Logger.php55
-rw-r--r--lib/Kolab/FreeBusy/Source.php65
-rw-r--r--lib/Kolab/FreeBusy/SourceFile.php19
-rw-r--r--lib/Kolab/FreeBusy/SourceIMAP.php19
-rw-r--r--lib/Kolab/FreeBusy/SourceURL.php19
9 files changed, 407 insertions, 0 deletions
diff --git a/lib/Kolab/FreeBusy/Config.php b/lib/Kolab/FreeBusy/Config.php
new file mode 100644
index 0000000..c0101e9
--- /dev/null
+++ b/lib/Kolab/FreeBusy/Config.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace Kolab;
+
+/**
+ * Wrapper class for service configuration
+ */
+class FBConfig
+{
+ private static $instance;
+
+ private $data = array();
+ private $valid = false;
+
+ /**
+ * Singelton getter
+ */
+ public static function getInstance($dir = null)
+ {
+ if (!isset(self::$instance)) {
+ self::$instance = new FBConfig($dir);
+ }
+
+ if ($dir && !self::$instance->valid)
+ self::$instance->_load($dir);
+
+ return self::$instance;
+ }
+
+ /**
+ * Default constructor
+ *
+ * @param string Path to load config from
+ */
+ function __construct($configdir = null)
+ {
+ if ($configdir) {
+ $this->_load($configdir);
+ }
+ }
+
+ private function _load($configdir)
+ {
+ if ($raw = @parse_ini_file($configdir . '/config.ini', true)) {
+ foreach ($raw as $section => $values) {
+ // check for known sections
+ if (in_array($section, array('httpauth','trustednetworks','log'))) {
+ $this->data[$section] = $values;
+ }
+ else if (isset($values['fbsource'])){
+ $this->data['directories'][] = $values;
+ }
+ }
+
+ $this->valid = !empty($this->data['directories']);
+ }
+ else {
+ trigger_error("Failed to parse configuration from $config_dir/config.ini", E_USER_ERROR);
+ }
+ }
+
+ /**
+ * Magic getter for direct read-only access to config options
+ */
+ public function __get($name)
+ {
+ return $this->data[$name];
+ }
+
+ /**
+ * Common getter for config options with fallback in default values
+ *
+ * @param string Config option name
+ * @param mixed Default value if option isn't set in config
+ * @return mixed Config option value
+ */
+ public function get($name, $default = null)
+ {
+ return array_key_exists($name, $this->data) ? $this->data[$name] : $default;
+ }
+
+ /**
+ * Determines whether we have a valid configuration loaded
+ *
+ * @return boolean True if valid, False otherwise
+ */
+ public function isValid()
+ {
+ return !empty($this->data);
+ }
+}
+
diff --git a/lib/Kolab/FreeBusy/Directory.php b/lib/Kolab/FreeBusy/Directory.php
new file mode 100644
index 0000000..25b9038
--- /dev/null
+++ b/lib/Kolab/FreeBusy/Directory.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Kolab;
+
+/**
+ * Abstract class representing an address directory for free/busy data lookups
+ */
+abstract class FBDirectory
+{
+ protected $config;
+
+ /**
+ * Factory method creating an instace of FBDirectory according to config
+ *
+ * @param array Hash array with config
+ */
+ public static function factory($config)
+ {
+ switch (strtolower($config['type'])) {
+ case 'ldap':
+ return new FBDirectoryLDAP($config);
+
+ case 'default':
+ case 'external':
+ return new FBDirectoryDefault($config);
+
+ default:
+ trigger_error("Invalid directory type '" . $config['type'] . "'!", E_USER_ERROR);
+ }
+
+ return null;
+ }
+
+ /**
+ * Resolve the given username to a FBEntity object
+ *
+ * @param string Username/Email to resolve
+ * @return object FBEntity if found, otherwise False
+ */
+ abstract public function resolve($user);
+
+ /**
+ * Retrieve free/busy data for the given user.
+ *
+ * @param string Username or email to resolve
+ * @param boolean Get extemded free-busy if possible
+ * @return string VCalendar container if found, False otherwise
+ */
+ public function getFreeBusyData($user, $extended = false)
+ {
+ // resolve user record first
+ if ($user = $this->resolve($user)) {
+ $fbsource = $this->config['fbsource'];
+ if ($source = FBSource::Factory($fbsource)) {
+ // foward request to FBSource instance
+ return $source->getFreeBusyData($user, $extended);
+ }
+ }
+
+ return false;
+ }
+
+} \ No newline at end of file
diff --git a/lib/Kolab/FreeBusy/DirectoryLDAP.php b/lib/Kolab/FreeBusy/DirectoryLDAP.php
new file mode 100644
index 0000000..f7dfb95
--- /dev/null
+++ b/lib/Kolab/FreeBusy/DirectoryLDAP.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Kolab;
+
+// PEAR modules operate in global namespace
+use \Net_LDAP3;
+
+/**
+ *
+ */
+class FBDirectoryLDAP extends FBDirectory
+{
+ private $ldap;
+
+ /**
+ * Default constructor loading directory configuration
+ */
+ public function __construct($config)
+ {
+ $ldap_config = array(
+
+ );
+
+ $this->ldap = new Net_LDAP3($ldap_config);
+ # $this->ldap->config_set_log_hook();
+ $this->ldap->connect();
+ }
+
+ /**
+ * @see FBDirectory::resolve()
+ */
+ public function resolve($user)
+ {
+ $result = array('u' => $user);
+
+ return false;
+ }
+
+}
+
diff --git a/lib/Kolab/FreeBusy/DirectoryStatic.php b/lib/Kolab/FreeBusy/DirectoryStatic.php
new file mode 100644
index 0000000..cb89972
--- /dev/null
+++ b/lib/Kolab/FreeBusy/DirectoryStatic.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Kolab;
+
+/**
+ *
+ */
+class FBDirectoryDefault extends FBDirectory
+{
+ /**
+ * Default constructor loading directory configuration
+ */
+ public function __construct($config)
+ {
+ $this->config = $config;
+ }
+
+ /**
+ * @see FBDirectory::resolve()
+ */
+ public function resolve($user)
+ {
+ $result = array('u' => $user);
+
+ // check if user matches the filter property (if configured)
+ if (!empty($this->config['filter'])) {
+ if (!preg_match('!'.$this->config['filter'].'!i', $user))
+ $result = false;
+ }
+
+ return $result;
+ }
+
+}
+
diff --git a/lib/Kolab/FreeBusy/Logger.php b/lib/Kolab/FreeBusy/Logger.php
new file mode 100644
index 0000000..97596c3
--- /dev/null
+++ b/lib/Kolab/FreeBusy/Logger.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Kolab;
+
+use Monolog\Logger;
+use Monolog\Handler\NullHandler;
+use Monolog\Handler\StreamHandler;
+
+/**
+ * Helper class for creating up Monolog instanced with local configration
+ */
+class FBLogger
+{
+ private static $instances = array();
+
+ /**
+ * Static getter for a Monolog\Logger instance
+ */
+ public static function get($name, $level = 0)
+ {
+ if (!isset(self::$instances[$name])) {
+ $logger = new Logger($name);
+
+ // TODO: support more log drivers
+ $config = FBConfig::getInstance();
+ switch ($config->get('log.driver')) {
+ case 'file':
+ $logdir = self::realpath($config->get('log.path'));
+ $loglevel = $level ?: $config->get("log.level", Logger::INFO);
+ $logger->pushHandler(new StreamHandler($logdir . $name. '.log', $loglevel));
+ break;
+
+ default:
+ // null handler if logging is disabled
+ $logger->pushHandler(new NullHandler);
+ }
+
+ self::$instances[$name] = $logger;
+ }
+
+ return self::$instances[$name];
+ }
+
+ /**
+ * Resolve the given directory to a real path ending with a /
+ */
+ private static function realpath($dirname)
+ {
+ if ($dirname[0] != '/')
+ $dirname = realpath(KOLAB_FREEBUSY_ROOT . '/' . $dirname);
+
+ return rtrim($dirname, '/') . '/';
+ }
+}
+
diff --git a/lib/Kolab/FreeBusy/Source.php b/lib/Kolab/FreeBusy/Source.php
new file mode 100644
index 0000000..6d5d062
--- /dev/null
+++ b/lib/Kolab/FreeBusy/Source.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace Kolab;
+
+/**
+ * Abstract class to fetch free/busy data from a specific source
+ */
+abstract class FBSource
+{
+ protected $config = array();
+
+ /**
+ * Factory method creating an instace of FBSource according to config
+ *
+ * @param array Hash array with config
+ */
+ public static function factory($url)
+ {
+ $config = parse_url($url);
+ switch ($config['scheme']) {
+ case 'file': return new FBSourceFile($config);
+ case 'imap': return new FBSourceIMAP($config);
+ case 'http':
+ case 'https': return new FBSourceURL($url);
+ }
+
+ trigger_error("Invalid source configuration: " . $url, E_USER_ERROR);
+ return null;
+ }
+
+ /**
+ * Default constructor
+ */
+ public function __construct($config)
+ {
+ $this->config = $config;
+ }
+
+ /**
+ * Retrieve free/busy data for the given user
+ *
+ * @param array Hash array with user attributes
+ */
+ abstract public function getFreeBusyData($user, $extended);
+
+ /**
+ * Replace all %varname strings in config with values from $user
+ */
+ protected function getUserConfig($user)
+ {
+ $config = array();
+ foreach ($this->config as $k => $val) {
+ if (is_string($val) && strpos($val, '%') !== false) {
+ $val = preg_replace_callback(
+ '/%\{?([a-z0-9]+)\}?/',
+ function($m) use ($user) { return $user[$m[1]]; },
+ $val);
+ }
+
+ $config[$k] = $val;
+ }
+
+ return $config;
+ }
+} \ No newline at end of file
diff --git a/lib/Kolab/FreeBusy/SourceFile.php b/lib/Kolab/FreeBusy/SourceFile.php
new file mode 100644
index 0000000..0cdaf02
--- /dev/null
+++ b/lib/Kolab/FreeBusy/SourceFile.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Kolab;
+
+/**
+ *
+ */
+class FBSourceFile extends FBSource
+{
+ /**
+ * @see FBSource::getFreeBusyData()
+ */
+ public function getFreeBusyData($user, $extended)
+ {
+ $config = $this->getUserConfig($user);
+
+ // TODO: implement this
+ }
+}
diff --git a/lib/Kolab/FreeBusy/SourceIMAP.php b/lib/Kolab/FreeBusy/SourceIMAP.php
new file mode 100644
index 0000000..90602f8
--- /dev/null
+++ b/lib/Kolab/FreeBusy/SourceIMAP.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Kolab;
+
+/**
+ *
+ */
+class FBSourceIMAP extends FBSource
+{
+ /**
+ * @see FBSource::getFreeBusyData()
+ */
+ public function getFreeBusyData($user, $extended)
+ {
+ $config = $this->getUserConfig($user);
+
+ // TODO: implement this
+ }
+}
diff --git a/lib/Kolab/FreeBusy/SourceURL.php b/lib/Kolab/FreeBusy/SourceURL.php
new file mode 100644
index 0000000..ecd0df5
--- /dev/null
+++ b/lib/Kolab/FreeBusy/SourceURL.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace Kolab;
+
+/**
+ *
+ */
+class FBSourceURL extends FBSource
+{
+ /**
+ * @see FBSource::getFreeBusyData()
+ */
+ public function getFreeBusyData($user, $extended)
+ {
+ $config = $this->getUserConfig($user);
+
+ // TODO: implement this
+ }
+}