summaryrefslogtreecommitdiff
path: root/web/index.php
blob: 93115f2e043cede8d55fa094cba9bc3661e96a12 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php

/**
 * Kolab Server Free/Busy Service Endpoint
 *
 * This is the public API to provide Free/Busy information for Kolab users.
 *
 * @version 0.1.0
 * @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/>.
 */


define('KOLAB_FREEBUSY_ROOT', realpath('../'));

// suppress error notices
ini_set('error_reporting', E_ALL &~ E_NOTICE);

// use composer's autoloader for both dependencies and local lib
require_once KOLAB_FREEBUSY_ROOT . '/vendor/autoload.php';

use Kolab\FreeBusy\Utils;
use Kolab\FreeBusy\Config;
use Kolab\FreeBusy\Logger;
use Kolab\FreeBusy\Directory;
use Kolab\FreeBusy\HTTPAuth;


// load config
$config = Config::getInstance(KOLAB_FREEBUSY_ROOT . '/config');
if ($config->isValid()) {
	// check for trusted IP first
	$remote_ip = Utils::remoteIP();
	$trusted_ip = $config->trustednetworks ? Utils::checkIPRange($remote_ip, $config->trustednetworks['allow']) : false;

	$log = Logger::get('web');
	$log->addDebug('Request: ' . $_SERVER['REDIRECT_URL'], array('ip' => $remote_ip, 'trusted' => $trusted_ip));

	// check HTTP authentication
	if (!$trusted_ip && $config->httpauth) {
		if (!HTTPAuth::check($config->httpauth)) {
			$log->addDebug("Abort with 401 Unauthorized");
			header('WWW-Authenticate: Basic realm="Kolab Free/Busy Service"');
			header($_SERVER['SERVER_PROTOCOL'] . " 401 Unauthorized", true);
			exit;
		}
	}

	#header('Content-type: text/calendar; charset=utf-8', true);
	header('Content-type: text/plain', true);

	// analyse request
	$url = array_filter(explode('/', $_SERVER['REDIRECT_URL']));
	$user = strtolower(array_pop($url));
	$action = strtolower(array_pop($url));
	$extended = false;

	// remove file extension
	if (preg_match('/^(.+)\.([ipx]fb)$/i', $user, $m)) {
		$user = $m[1];
		$extended = $m[2] == 'xfb';
	}

	// iterate over directories
	foreach ($config->directories as $key => $dirconfig) {
		$log->addDebug("Trying directory $key", $dirconfig);

		$directory = Directory::factory($dirconfig);
		if ($directory && ($fbdata = $directory->getFreeBusyData($user, $extended))) {
			$log->addInfo("Found valid data for user $user in directory $key");
			echo $fbdata;
			exit;
		}
	}

	// return 404 if request was sent from a trusted IP
	if ($trusted_ip) {
		$log->addDebug("Returning '404 Not Found' for user $user");
		header($_SERVER['SERVER_PROTOCOL'] . " 404 Not found", true);
	}
	else {
		$log->addInfo("Returning empty Free/Busy list for user $user");

		$now = time();
		$dtformat = 'Ymd\THis\Z';

		// NOTE: The following settings should probably correspond with
		// whatever period of time kolab-freebusyd thinks it should use.

		// Should probably be a setting. For now, do 8 weeks in the past
		$start = $now - (60 * 60 * 24 * 7 * 8);
		// Should probably be a setting. For now, do 16 weeks into the future
		$end = $now + (60 * 60 * 24 * 7 * 16);

		// Return an apparent empty Free/Busy list.
		print "BEGIN:VCALENDAR\n";
		print "VERSION:2.0\n";
		print "PRODID:-//kolab.org//NONSGML Kolab Server 3//EN\n";
		print "METHOD:PUBLISH\n";
		print "BEGIN:VFREEBUSY\n";
		print "ORGANIZER:MAILTO:" . $user . ".ifb\n";
		print "DTSTAMP:" . gmdate($dtformat) . "\n";
		print "DTSTART:" . gmdate($dtformat, $start) . "\n";
		print "DTEND:" . gmdate($dtformat, $end) . "\n";
		print "COMMENT:This is a dummy vfreebusy that indicates an empty calendar\n";
		print "FREEBUSY:19700101T000000Z/19700101T000000Z\n";
		print "END:VFREEBUSY\n";
		print "END:VCALENDAR\n";
	}
}

// exit with error
# header($_SERVER['SERVER_PROTOCOL'] . " 500 Internal Server Error", true);