summaryrefslogtreecommitdiff
path: root/lib/Kolab/FreeBusy/Source.php
blob: 2d12ebc7217a8cd82a706a895c1c973e246b1fe5 (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
<?php

namespace Kolab\FreeBusy;

/**
 * Abstract class to fetch free/busy data from a specific source
 */
abstract class Source
{
	protected $config = array();

	/**
	 * Factory method creating an instace of Source according to config
	 *
	 * @param array Hash array with config
	 */
	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($config);
		}

		Logger::get('source')->addError("Invalid source configuration: " . $url);
		return null;
	}

	/**
	 * Default constructor
	 */
	public function __construct($config)
	{
		$this->config = $config;
	}

	/**
	 * Retrieve free/busy data for the given user
	 *
	 * @param array Hash array with user attributes
	 */
	abstract public function getFreeBusyData($user, $extended);

	/**
	 * Replace all %varname strings in config with values from $user
	 */
	protected function getUserConfig($user)
	{
		$config = array();
		foreach ($this->config as $k => $val) {
			if (is_string($val) && strpos($val, '%') !== false) {
				$val = preg_replace_callback(
					'/%\{?([a-z0-9]+)\}?/',
					function($m) use ($user) { return $user[$m[1]]; },
					$val);
			}

			$config[$k] = $val;
		}

		return $config;
	}
}