public static function get($email, $json = false)
{
$default_domain = empty($GLOBALS['conf']['storage']['default_domain']) ? null : $GLOBALS['conf']['storage']['default_domain'];
$rfc822 = new Horde_Mail_Rfc822();
try {
$res = $rfc822->parseAddressList($email, array('default_domain' => $default_domain));
} catch (Horde_Mail_Exception $e) {
throw new Kronolith_Exception($e);
}
if (!($tmp = $res[0])) {
throw new Kronolith_Exception(_("No valid email address found"));
}
$email = $tmp->bare_address;
/* Check if we can retrieve a VFB from the Free/Busy URL, if one is
* set. */
$url = self::getUrl($email);
if ($url) {
$url = trim($url);
$http = $GLOBALS['injector']->getInstance('Horde_Core_Factory_HttpClient')->create(array('request.verifyPeer' => false));
try {
$response = $http->get($url);
} catch (Horde_Http_Exception $e) {
throw new Kronolith_Exception(sprintf(_("The free/busy url for %s cannot be retrieved."), $email));
}
if ($response->code == 200 && ($data = $response->getBody())) {
// Detect the charset of the iCalendar data.
$contentType = $response->getHeader('Content-Type');
if ($contentType && strpos($contentType, ';') !== false) {
list(, $charset, ) = explode(';', $contentType);
$data = Horde_String::convertCharset($data, trim(str_replace('charset=', '', $charset)), 'UTF-8');
}
$vCal = new Horde_Icalendar();
$vCal->parsevCalendar($data, 'VCALENDAR');
$components = $vCal->getComponents();
$vCal = new Horde_Icalendar();
$vFb = Horde_Icalendar::newComponent('vfreebusy', $vCal);
$vFb->setAttribute('ORGANIZER', $email);
$found = false;
foreach ($components as $component) {
if ($component instanceof Horde_Icalendar_Vfreebusy) {
$found = true;
$vFb->merge($component);
}
}
if ($found) {
// @todo: actually store the results in the storage, so
// that they can be retrieved later. We should store the
// plain iCalendar data though, to avoid versioning
// problems with serialize iCalendar objects.
return $json ? self::toJson($vFb) : $vFb;
}
}
}
/* Check storage driver. */
$storage = $GLOBALS['injector']->getInstance('Kronolith_Factory_Storage')->create();
try {
$fb = $storage->search($email);
return $json ? self::toJson($fb) : $fb;
} catch (Horde_Exception_NotFound $e) {
if ($url) {
throw new Kronolith_Exception(sprintf(_("No free/busy information found at the free/busy url of %s."), $email));
}
throw new Kronolith_Exception(sprintf(_("No free/busy url found for %s."), $email));
}
}