Changeset View
Changeset View
Standalone View
Standalone View
lib/Kolab/FreeBusy/Directory.php
Show All 24 Lines | |||||
use Kolab\Config; | use Kolab\Config; | ||||
/** | /** | ||||
* Abstract class representing an address directory for free/busy data lookups | * Abstract class representing an address directory for free/busy data lookups | ||||
*/ | */ | ||||
abstract class Directory | abstract class Directory | ||||
{ | { | ||||
protected $config; | protected $config; | ||||
/** | /** | ||||
* Factory method creating an instace of Directory according to config | * Factory method creating an instace of Directory according to config | ||||
* | * | ||||
* @param array Hash array with config | * @param array Hash array with config | ||||
*/ | */ | ||||
public static function factory($config) | public static function factory($config) | ||||
{ | { | ||||
switch (strtolower($config['type'])) { | switch (strtolower($config['type'])) { | ||||
case 'ldap': | case 'ldap': | ||||
return new DirectoryLDAP($config); | return new DirectoryLDAP($config); | ||||
case 'static': | case 'static': | ||||
case 'external': | case 'external': | ||||
return new DirectoryStatic($config); | return new DirectoryStatic($config); | ||||
default: | default: | ||||
Logger::get('directory')->addError("Invalid directory type '" . $config['type'] . "'!"); | Logger::get('directory')->addError("Invalid directory type '" . $config['type'] . "'!"); | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
/** | /** | ||||
* Resolve the given username to a Entity object | * Resolve the given username to a Entity object | ||||
* | * | ||||
* @param string Username/Email to resolve | * @param string Username/Email to resolve | ||||
* @return object Entity if found, otherwise False | * @return object Entity if found, otherwise False | ||||
*/ | */ | ||||
abstract public function resolve($user); | abstract public function resolve($user); | ||||
/** | /** | ||||
* Retrieve free/busy data for the given user. | * Retrieve free/busy data for the given user. | ||||
* | * | ||||
* @param string Username or email to resolve | * @param string Username or email to resolve | ||||
* @param boolean Get extemded free-busy if possible | * @param boolean Get extemded free-busy if possible | ||||
* @return string VCalendar container if found, False otherwise | * @return string VCalendar container if found, False otherwise | ||||
*/ | */ | ||||
public function getFreeBusyData($user, $extended = false) | public function getFreeBusyData($user, $extended = false) | ||||
{ | { | ||||
$logger = Logger::get('directory'); | |||||
// resolve user record first | // resolve user record first | ||||
if ($user = $this->resolve($user)) { | if ($user = $this->resolve($user)) { | ||||
$fbsource = $this->config['fbsource']; | $fbsource = $this->config['fbsource']; | ||||
if ($source = Source::Factory($fbsource, $this->config)) { | if ($source = Source::Factory($fbsource, $this->config)) { | ||||
// forward request to Source instance | $user = $this->postprocessAttrib($user); | ||||
if ($data = $source->getFreeBusyData($this->postprocessAttrib($user), $extended)) { | |||||
// send data through the according format converter | |||||
$converter = Format::factory($this->config['format']); | |||||
$data = $converter->toVCalendar($data); | |||||
// cache the generated data | // Find the cached data | ||||
if ($data && $this->config['cacheto'] && !$source->isCached()) { | if (!empty($this->config['cacheto'])) { | ||||
$path = preg_replace_callback( | $path = preg_replace_callback( | ||||
'/%\{?([a-z0-9]+)\}?/', | '/%\{?([a-z0-9]+)\}?/', | ||||
function($m) use ($user) { return $user[$m[1]]; }, | function($m) use ($user) { return $user[$m[1]]; }, | ||||
$this->config['cacheto'] | $this->config['cacheto'] | ||||
); | ); | ||||
if (!@file_put_contents($path, $data, LOCK_EX)) { | // check for cached data | ||||
Logger::get('directory')->addError("Failed to write to cache file '" . $path . "'!"); | if (file_exists($path)) { | ||||
if (empty($this->config['expires']) || filemtime($path) + Utils::getOffsetSec($this->config['expires']) >= time()) { | |||||
$logger->addInfo("Deliver cached data from {$path}"); | |||||
return file_get_contents($path); | |||||
} | |||||
} | |||||
} | |||||
// forward request to Source instance | |||||
if ($data = $source->getFreeBusyData($user, $extended)) { | |||||
// send data through the according format converter | |||||
$converter = Format::factory($this->config['format']); | |||||
$data = $converter->toVCalendar($data); | |||||
// cache the generated data | |||||
if ($data && !empty($path)) { | |||||
if (!file_put_contents($path, $data, LOCK_EX)) { | |||||
$logger->addError("Failed to write to cache in {$path}"); | |||||
} else { | |||||
$logger->addInfo("Cached data in {$path}"); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
return $data; | return $data; | ||||
} | } | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
/** | /** | ||||
* Modify attribute values according to config | * Modify attribute values according to config | ||||
*/ | */ | ||||
protected function postprocessAttrib($attrib) | protected function postprocessAttrib($attrib) | ||||
{ | { | ||||
if (!empty($this->config['lc_attributes'])) { | if (!empty($this->config['lc_attributes'])) { | ||||
foreach (Config::convert($this->config['lc_attributes'], Config::ARR) as $key) { | foreach (Config::convert($this->config['lc_attributes'], Config::ARR) as $key) { | ||||
if (!empty($attrib[$key])) | if (!empty($attrib[$key])) { | ||||
$attrib[$key] = strtolower($attrib[$key]); | $attrib[$key] = strtolower($attrib[$key]); | ||||
} | } | ||||
} | } | ||||
} | |||||
return $attrib; | return $attrib; | ||||
} | } | ||||
} | |||||
} | |||||
No newline at end of file |