summaryrefslogtreecommitdiff
path: root/lib/Kolab/FreeBusy/SourceURL.php
blob: 02f73d7bdbb57fbb5fd68066fc0164062d3b3369 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php

/**
 * This file is part of the Kolab Server Free/Busy Service
 *
 * @author Thomas Bruederli <bruederli@kolabsys.com>
 *
 * Copyright (C) 2013, Kolab Systems AG <contact@kolabsys.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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::composeUrl($config);  // re-compose url without user:pass
		}

		$data = @file_get_contents($config['url'], false, $context);

		// log this...
		Logger::get('url')->addInfo("Fetching data from " . $config['url'] . ": " . ($data ? 'OK' : 'FAILED'));

		return $data;
	}

	/**
	 * Compose a full url from the given config (previously extracted with parse_url())
	 */
	private static function composeUrl($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;
	}
}