summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Kolab/FreeBusy/Source.php3
-rw-r--r--lib/Kolab/FreeBusy/SourceURL.php37
2 files changed, 37 insertions, 3 deletions
diff --git a/lib/Kolab/FreeBusy/Source.php b/lib/Kolab/FreeBusy/Source.php
index 1af5ba3..53c4d0f 100644
--- a/lib/Kolab/FreeBusy/Source.php
+++ b/lib/Kolab/FreeBusy/Source.php
@@ -17,11 +17,12 @@ abstract class Source
public static function factory($url)
{
$config = parse_url($url);
+ $config['url'] = $url;
switch ($config['scheme']) {
case 'file': return new SourceFile($config);
case 'imap': return new SourceIMAP($config);
case 'http':
- case 'https': return new SourceURL($url);
+ case 'https': return new SourceURL($config);
}
trigger_error("Invalid source configuration: " . $url, E_USER_ERROR);
diff --git a/lib/Kolab/FreeBusy/SourceURL.php b/lib/Kolab/FreeBusy/SourceURL.php
index 1051dbf..d165bd5 100644
--- a/lib/Kolab/FreeBusy/SourceURL.php
+++ b/lib/Kolab/FreeBusy/SourceURL.php
@@ -3,7 +3,7 @@
namespace Kolab\FreeBusy;
/**
- *
+ * Implementation of a Free/Busy data source reading from remote URLs through HTTP
*/
class SourceURL extends Source
{
@@ -14,6 +14,39 @@ class SourceURL extends Source
{
$config = $this->getUserConfig($user);
- // TODO: implement this
+ // prepare HTTP stream context
+ $context = stream_context_create(array(
+ 'http' => array(
+ 'user_agent' => "Kolab Free-Busy Service/0.1.0",
+ 'timeout' => 10,
+ ),
+ ));
+
+ // set HTTP auth credentials
+ if (!empty($config['user'])) {
+ stream_context_set_option($context, array(
+ 'http' => array(
+ 'header' => "Authorization: Basic " . base64_encode($config['user'] . ':' . $config['pass']) . "\r\n",
+ ),
+ ));
+ $config['url'] = self::getUrl($config); // re-compose url without user:pass
+ }
+
+ // TODO: so some logging
+
+ return file_get_contents($config['url'], false, $context);
+ }
+
+ /**
+ * Compose a full url from the given config (previously extracted with parse_url())
+ */
+ private static function getUrl($config)
+ {
+ $scheme = isset($config['scheme']) ? $config['scheme'] . '://' : '';
+ $host = isset($config['host']) ? $config['host'] : '';
+ $port = isset($config['port']) ? ':' . $config['port'] : '';
+ $path = isset($config['path']) ? $config['path'] : '';
+ $query = isset($config['query']) ? '?' . $config['query'] : '';
+ return $scheme . $host . $port . $path . $query;
}
}