Changeset View
Changeset View
Standalone View
Standalone View
lib/Kolab/FreeBusy/Source.php
Show All 23 Lines | |||||
namespace Kolab\FreeBusy; | namespace Kolab\FreeBusy; | ||||
/** | /** | ||||
* Abstract class to fetch free/busy data from a specific source | * Abstract class to fetch free/busy data from a specific source | ||||
*/ | */ | ||||
abstract class Source | abstract class Source | ||||
{ | { | ||||
protected $config = array(); | protected $config = array(); | ||||
protected $cached = false; | |||||
/** | /** | ||||
* Factory method creating an instace of Source according to config | * Factory method creating an instace of Source according to config | ||||
* | * | ||||
* @param string Source URI | * @param string Source URI | ||||
* @param array Hash array with config | * @param array Hash array with config | ||||
*/ | */ | ||||
public static function factory($url, $conf) | public static function factory($url, $conf) | ||||
{ | { | ||||
$config = parse_url($url); | $config = parse_url($url); | ||||
$config['url'] = $url; | $config['url'] = $url; | ||||
switch ($config['scheme']) { | switch ($config['scheme']) { | ||||
case 'file': return new SourceFile($config + $conf); | case 'file': return new SourceFile($config + $conf); | ||||
case 'tls': | case 'tls': | ||||
case 'imap': | case 'imap': | ||||
case 'imaps': return new SourceIMAP($config + $conf); | case 'imaps': return new SourceIMAP($config + $conf); | ||||
case 'http': | case 'http': | ||||
case 'https': return new SourceURL($config + $conf); | case 'https': return new SourceURL($config + $conf); | ||||
case 'fbd': | case 'fbd': | ||||
case 'fbdaemon': return new SourceFBDaemon($config + $conf); | case 'fbdaemon': return new SourceFBDaemon($config + $conf); | ||||
case 'aggregate': return new SourceAggregator($config + $conf); | case 'aggregate': return new SourceAggregator($config + $conf); | ||||
case 'ews': return new SourceEWS($config + $conf); | |||||
} | } | ||||
Logger::get('source')->addError("Invalid source configuration: " . $url); | Logger::get('source')->addError("Invalid source configuration: " . $url); | ||||
return null; | return null; | ||||
} | } | ||||
/** | /** | ||||
* Default constructor | * Default constructor | ||||
*/ | */ | ||||
public function __construct($config) | public function __construct($config) | ||||
{ | { | ||||
$this->config = $config; | $this->config = $config; | ||||
} | } | ||||
/** | /** | ||||
* Retrieve free/busy data for the given user | * Retrieve free/busy data for the given user | ||||
* | * | ||||
* @param array Hash array with user attributes | * @param false|string vCalendar (ICS) file output, or False on error | ||||
*/ | */ | ||||
abstract public function getFreeBusyData($user, $extended); | abstract public function getFreeBusyData($user, $extended); | ||||
/** | /** | ||||
* Replace all %varname strings in config with values from $user | * Replace all %varname strings in config with values from $user | ||||
*/ | */ | ||||
protected function getUserConfig($user) | protected function getUserConfig($user) | ||||
{ | { | ||||
$config = array(); | $config = array(); | ||||
foreach ($this->config as $k => $val) { | foreach ($this->config as $k => $val) { | ||||
if (is_string($val) && strpos($val, '%') !== false) { | if (is_string($val) && strpos($val, '%') !== false) { | ||||
$val = preg_replace_callback( | $val = preg_replace_callback( | ||||
'/%\{?([a-z0-9]+)\}?/', | '/%\{?([a-z]{1}[a-z0-9]*)\}?/', | ||||
function($m) use ($k, $user) { | function($m) use ($k, $user) { | ||||
$enc = $k == 'url' || $k == 'query' || $k == 'fbsource'; | $enc = $k == 'url' || $k == 'query' || $k == 'fbsource'; | ||||
return $enc ? urlencode($user[$m[1]]) : $user[$m[1]]; | return $enc ? urlencode($user[$m[1]]) : $user[$m[1]]; | ||||
}, | }, | ||||
$val); | $val); | ||||
} | } | ||||
$config[$k] = $val; | $config[$k] = $val; | ||||
} | } | ||||
return $config; | return $config; | ||||
} | } | ||||
/** | |||||
* Helper method to check if a cached file exists and is still valid | |||||
* | |||||
* @param array Hash array with (replaced) config properties | |||||
* @return string Cached free-busy data or false if cache file doesn't exist or is expired | |||||
*/ | |||||
protected function getCached($config) | |||||
{ | |||||
if ($config['cacheto'] && file_exists($config['cacheto'])) { | |||||
if (empty($config['expires']) || filemtime($config['cacheto']) + Utils::getOffsetSec($config['expires']) >= time()) { | |||||
$this->cached = true; | |||||
return file_get_contents($config['cacheto']); | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
/** | |||||
* Return the value of the 'cached' flag | |||||
*/ | |||||
public function isCached() | |||||
{ | |||||
return $this->cached; | |||||
} | |||||
} | } |