Changeset View
Changeset View
Standalone View
Standalone View
public_html/index.php
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
use Kolab\FreeBusy\Logger; | use Kolab\FreeBusy\Logger; | ||||
use Kolab\FreeBusy\Directory; | use Kolab\FreeBusy\Directory; | ||||
use Kolab\FreeBusy\HTTPAuth; | use Kolab\FreeBusy\HTTPAuth; | ||||
// load config | // load config | ||||
$config = Config::get_instance(KOLAB_FREEBUSY_ROOT . '/config'); | $config = Config::get_instance(KOLAB_FREEBUSY_ROOT . '/config'); | ||||
if ($config->valid()) { | if ($config->valid()) { | ||||
// check for trusted IP first | // check for trusted IP first | ||||
$remote_ip = Utils::remoteIP(); | $remote_ip = Utils::remoteIP(); | ||||
$trusted_ip = $config->trustednetworks ? Utils::checkIPRange($remote_ip, $config->get('trustednetworks.allow', array(), Config::ARR)) : false; | $trusted_ip = $config->trustednetworks ? Utils::checkIPRange($remote_ip, $config->get('trustednetworks.allow', array(), Config::ARR)) : false; | ||||
$log = Logger::get('web'); | $log = Logger::get('web'); | ||||
$uri = $_SERVER['REDIRECT_URL']; | $uri = isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : ''; | ||||
// we're not always redirected here | // we're not always redirected here | ||||
if (empty($uri)) { | if (empty($uri)) { | ||||
$uri = $_SERVER['REQUEST_URI']; | $uri = $_SERVER['REQUEST_URI']; | ||||
$log->addDebug('Request (direct): ' . $uri, array('ip' => $remote_ip, 'trusted' => $trusted_ip)); | $log->addDebug('Request (direct): ' . $uri, array('ip' => $remote_ip, 'trusted' => $trusted_ip)); | ||||
} else { | } else { | ||||
$log->addDebug('Request (redirect): ' . $uri, array('ip' => $remote_ip, 'trusted' => $trusted_ip)); | $log->addDebug('Request (redirect): ' . $uri, array('ip' => $remote_ip, 'trusted' => $trusted_ip)); | ||||
} | } | ||||
list($uri, $args) = explode('?', $uri); | list($uri, ) = explode('?', $uri); | ||||
// check HTTP authentication | // check HTTP authentication | ||||
if (!$trusted_ip && $config->httpauth) { | if (!$trusted_ip && $config->httpauth) { | ||||
$_SERVER['FREEBUSY_URI'] = urldecode(rtrim($uri, '/')); | $_SERVER['FREEBUSY_URI'] = urldecode(rtrim($uri, '/')); | ||||
if (!HTTPAuth::check($config->httpauth)) { | if (!HTTPAuth::check($config->httpauth)) { | ||||
$log->addDebug("Abort with 401 Unauthorized"); | $log->addDebug("Abort with 401 Unauthorized"); | ||||
header('WWW-Authenticate: Basic realm="Kolab Free/Busy Service"'); | header('WWW-Authenticate: Basic realm="Kolab Free/Busy Service"'); | ||||
header($_SERVER['SERVER_PROTOCOL'] . " 401 Unauthorized", true); | header($_SERVER['SERVER_PROTOCOL'] . " 401 Unauthorized", true); | ||||
exit; | exit; | ||||
} | } | ||||
} | } | ||||
#header('Content-type: text/calendar; charset=utf-8', true); | //header('Content-type: text/calendar; charset=utf-8', true); | ||||
header('Content-type: text/plain; charset=utf-8', true); | header('Content-type: text/plain; charset=utf-8', true); | ||||
// analyse request | // analyse request | ||||
$user = $_SERVER['FREEBUSY_USER']; | $user = isset($_SERVER['FREEBUSY_USER']) ? $_SERVER['FREEBUSY_USER'] : null; | ||||
$extended = !empty($_SERVER['FREEBUSY_EXTENDED']); | $extended = !empty($_SERVER['FREEBUSY_EXTENDED']); | ||||
if (!$user) { | if (!$user) { | ||||
$url = array_filter(explode('/', $uri)); | $url = array_filter(explode('/', $uri)); | ||||
$user = strtolower(array_pop($url)); | $user = strtolower(array_pop($url)); | ||||
// remove file extension | // remove file extension | ||||
if (preg_match('/^(.+)\.([ipx]fb)$/i', $user, $m)) { | if (preg_match('/^(.+)\.([ipx]fb)$/i', $user, $m)) { | ||||
$user = urldecode($m[1]); | $user = urldecode($m[1]); | ||||
$extended = $m[2] == 'xfb'; | $extended = $m[2] == 'xfb'; | ||||
} | } | ||||
} | } | ||||
// iterate over directories | // iterate over directories | ||||
foreach ($config->directory as $key => $dirconfig) { | foreach ($config->directory as $key => $dirconfig) { | ||||
$log->addDebug("Trying directory $key", $dirconfig); | $log->addDebug("Trying directory $key", $dirconfig); | ||||
$directory = Directory::factory($dirconfig); | $directory = Directory::factory($dirconfig); | ||||
if ($directory && ($fbdata = $directory->getFreeBusyData($user, $extended))) { | if ($directory && ($fbdata = $directory->getFreeBusyData($user, $extended))) { | ||||
$log->addInfo("Found valid data for user $user in directory $key"); | $log->addInfo("Found valid data for user $user in directory $key"); | ||||
echo $fbdata; | echo $fbdata; | ||||
exit; | exit; | ||||
} | } | ||||
} | } | ||||
// return 404 if request was sent from a trusted IP | // return 404 if request was sent from a trusted IP | ||||
if ($trusted_ip) { | if ($trusted_ip) { | ||||
$log->addDebug("Returning '404 Not Found' for user $user"); | $log->addDebug("Returning '404 Not Found' for user $user"); | ||||
header($_SERVER['SERVER_PROTOCOL'] . " 404 Not found", true); | header($_SERVER['SERVER_PROTOCOL'] . " 404 Not found", true); | ||||
} | } else { | ||||
else { | |||||
$log->addInfo("Returning empty Free/Busy list for user $user"); | $log->addInfo("Returning empty Free/Busy list for user $user"); | ||||
// Return an apparent empty Free/Busy list. | // Return an apparent empty Free/Busy list. | ||||
print Utils::dummyVFreebusy($user); | print Utils::dummyVFreebusy($user); | ||||
} | } | ||||
} | } | ||||
// exit with error | // exit with error | ||||
# header($_SERVER['SERVER_PROTOCOL'] . " 500 Internal Server Error", true); | # header($_SERVER['SERVER_PROTOCOL'] . " 500 Internal Server Error", true); | ||||