1: <?php
2: /**
3: * Provides methods to retrieve free/busy data for resources on a Kolab server.
4: *
5: * PHP version 5
6: *
7: * @category Kolab
8: * @package Kolab_Filter
9: * @author Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
10: * @author Gunnar Wrobel <wrobel@pardus.de>
11: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
12: * @link http://pear.horde.org/index.php?package=Kolab_Server
13: */
14:
15: /**
16: * Retrieves free/busy data for an email address on a Kolab server.
17: *
18: * Copyright 2004-2009 Klarälvdalens Datakonsult AB
19: *
20: * See the enclosed file COPYING for license information (LGPL>=2.1). If you
21: * did not receive this file,
22: * see http://www.horde.org/licenses/lgpl21.
23: *
24: * @category Kolab
25: * @package Kolab_Filter
26: * @author Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
27: * @author Gunnar Wrobel <wrobel@pardus.de>
28: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
29: * @link http://pear.horde.org/index.php?package=Kolab_Server
30: */
31: class Horde_Kolab_Resource_Freebusy_Kolab extends Horde_Kolab_Resource_Freebusy
32: {
33: /**
34: * Retrieve Free/Busy URL for the specified resource id.
35: *
36: * @param string $resource The id of the resource (usually a mail address).
37: *
38: * @return string The Free/Busy URL for that resource.
39: */
40: protected function getUrl($resource)
41: {
42: $server = Horde_Kolab_Server::singleton();
43: $uid = $server->uidForMailAddress($resource);
44: $result = $server->fetch($uid)->getServer('freebusy');
45: return sprintf('%s/%s.xfb', $result, $resource);
46: }
47:
48: /**
49: * Retrieve Free/Busy data for the specified resource.
50: *
51: * @param string $resource Fetch the Free/Busy data for this resource.
52: *
53: * @return Horde_Icalendar_Vfreebusy The Free/Busy data.
54: */
55: public function get($resource)
56: {
57: global $conf;
58:
59: $url = self::getUrl($resource);
60:
61: Horde::logMessage(sprintf('Freebusy URL for resource %s is %s',
62: $resource, $url), 'DEBUG');
63:
64: list($user, $domain) = explode('@', $resource);
65: if (empty($domain)) {
66: $domain = $conf['kolab']['filter']['email_domain'];
67: }
68:
69: /**
70: * This section matches Kronolith_Freebusy and should be merged with it
71: * again in a single Horde_Freebusy module.
72: */
73: $options = array(
74: 'method' => 'GET',
75: 'timeout' => 5,
76: 'allowRedirects' => true
77: );
78:
79: if (!empty($conf['http']['proxy']['proxy_host'])) {
80: $options = array_merge($options, $conf['http']['proxy']);
81: }
82:
83: $http = new HTTP_Request($url, $options);
84: $http->setBasicAuth($conf['kolab']['filter']['calendar_id'] . '@' . $domain,
85: $conf['kolab']['filter']['calendar_pass']);
86: @$http->sendRequest();
87: if ($http->getResponseCode() != 200) {
88: throw new Horde_Kolab_Resource_Exception(sprintf('Unable to retrieve free/busy information for %s',
89: $resource),
90: Horde_Kolab_Resource_Exception::NO_FREEBUSY);
91: }
92: $vfb_text = $http->getResponseBody();
93:
94: // Detect the charset of the iCalendar data.
95: $contentType = $http->getResponseHeader('Content-Type');
96: if ($contentType && strpos($contentType, ';') !== false) {
97: list(,$charset,) = explode(';', $contentType);
98: $vfb_text = Horde_String::convertCharset($vfb_text, trim(str_replace('charset=', '', $charset)), 'UTF-8');
99: }
100:
101: $iCal = new Horde_Icalendar();
102: $iCal->parsevCalendar($vfb_text, 'VCALENDAR');
103:
104: $vfb = &$iCal->findComponent('VFREEBUSY');
105:
106: if ($vfb === false) {
107: throw new Horde_Kolab_Resource_Exception(sprintf('Invalid or no free/busy information available for %s',
108: $resource),
109: Horde_Kolab_Resource_Exception::NO_FREEBUSY);
110: }
111: $vfb->simplify();
112:
113: return $vfb;
114: }
115: }
116: