summaryrefslogtreecommitdiff
path: root/lib/Kolab/FreeBusy/SourceURL.php
blob: d165bd5261228914dc3c9b879edb24fb34842380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php

namespace Kolab\FreeBusy;

/**
 * Implementation of a Free/Busy data source reading from remote URLs through HTTP
 */
class SourceURL extends Source
{
	/**
	 * @see Source::getFreeBusyData()
	 */
	public function getFreeBusyData($user, $extended)
	{
		$config = $this->getUserConfig($user);

		// 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;
	}
}