1: <?php
2: /**
3: * This class allows fetching free/busy information from a Microsoft Exchange
4: * server via OWA.
5: *
6: * PHP version 5
7: *
8: * @category Kolab
9: * @package Kolab_FreeBusy
10: * @author Mathieu Parent <math.parent@gmail.com>
11: * @author Gunnar Wrobel <wrobel@pardus.de>
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @link http://pear.horde.org/index.php?package=Kolab_FreeBusy
14: */
15:
16: /**
17: * This class allows fetching free/busy information from a Microsoft Exchange
18: * server via OWA.
19: *
20: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
21: * Copyright 2011 Kolab Systems AG
22: *
23: * See the enclosed file COPYING for license information (LGPL). If you did not
24: * receive this file, see
25: * http://www.horde.org/licenses/lgpl21.
26: *
27: * @category Kolab
28: * @package Kolab_FreeBusy
29: * @author Mathieu Parent <math.parent@gmail.com>
30: * @author Gunnar Wrobel <wrobel@pardus.de>
31: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
32: * @link http://pear.horde.org/index.php?package=Kolab_FreeBusy
33: */
34: class Horde_Kolab_FreeBusy_Resource_Event_Owa
35: implements Horde_Kolab_FreeBusy_Resource_Event
36: {
37: /**
38: * The owner of the free/busy data.
39: *
40: * @var Horde_Kolab_FreeBusy_Owner_Freebusy
41: */
42: private $_owner;
43:
44: /**
45: * The HTTP client for fetching the free/busy data.
46: *
47: * @var Horde_Http_Client
48: */
49: private $_client;
50:
51: /**
52: * The owner of the free/busy data.
53: *
54: * @var Horde_Kolab_FreeBusy_Owner_Freebusy
55: */
56: private $_params;
57:
58: /**
59: * Constructor.
60: *
61: * @param Horde_Kolab_FreeBusy_Owner_Freebusy $owner The resource owner.
62: */
63: public function __construct(
64: Horde_Kolab_FreeBusy_Owner $owner, $params = array()
65: )
66: {
67: if (!isset($params['url'])) {
68: throw new Horde_Kolab_FreeBusy_Exception(
69: 'The URL for the exchange server has been left undefined!'
70: );
71: }
72: if (!isset($params['interval'])) {
73: $params['interval'] = 30;
74: }
75: if (!isset($params['client'])) {
76: $this->_client = new Horde_Http_Client();
77: } else {
78: $this->_client = $params['client'];
79: }
80: $this->_owner = $owner;
81: $this->_params = $params;
82: }
83:
84: /**
85: * Return the owner of the resource.
86: *
87: * @return Horde_Kolab_FreeBusy_Owner The resource owner.
88: */
89: public function getOwner()
90: {
91: return $this->_owner;
92: }
93:
94: /**
95: * Return the name of the resource.
96: *
97: * @return string The name for the resource.
98: */
99: public function getName()
100: {
101: return $this->_owner->getOwner() . '@' . $this->_params['url'];
102: }
103:
104: /**
105: * Return for whom this resource exports relevant data.
106: *
107: * @return string The user type the exported data of this resource is
108: * relevant for.
109: *
110: * @throws Horde_Kolab_FreeBusy_Exception If retrieving the relevance
111: * information failed.
112: *
113: * @todo It would be nice if we would not only have the free/busy specific
114: * relevance but a generic way of setting the relevance of resources.
115: */
116: public function getRelevance()
117: {
118: return 'admins';
119: }
120:
121: /**
122: * Fetch the resource ACL.
123: *
124: * @return array ACL for this resource.
125: *
126: * @throws Horde_Kolab_FreeBusy_Exception If retrieving the ACL information
127: * failed.
128: */
129: public function getAcl()
130: {
131: return array();
132: }
133:
134: /**
135: * Fetch the access controls on specific attributes of this
136: * resource.
137: *
138: * @return array Attribute ACL for this resource.
139: *
140: * @throws Horde_Kolab_FreeBusy_Exception If retrieving the attribute ACL
141: * information failed.
142: *
143: * @todo It would be nice if we would not only have the free/busy specific
144: * attribute acls but a generic way of setting attribute ACL for resources.
145: */
146: public function getAttributeAcl()
147: {
148: return array();
149: }
150:
151: /**
152: * Lists all events in the given time range.
153: *
154: * @param Horde_Date $startDate Start of range date object.
155: * @param Horde_Date $endDate End of range data object.
156: *
157: * @return array Events in the given time range.
158: *
159: * @throws Horde_Kolab_FreeBusy_Exception If retrieving the events failed.
160: */
161: public function listEvents(Horde_Date $startDate, Horde_Date $endDate)
162: {
163: $url = $this->_params['url'] . '/public/?cmd=freebusy'.
164: '&start=' . $startDate->format('c') .
165: '&end=' . $endDate->format('c') .
166: '&interval=' . $this->_params['interval'] .
167: '&u=SMTP:' . $this->_owner->getOwner();
168:
169: $response = $this->_client->get(
170: $url,
171: array(
172: 'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)'
173: )
174: );
175: if ($response->code !== 200) {
176: throw new Horde_Kolab_FreeBusy_Exception_NotFound(
177: sprintf('Unable to fetch free/busy information from %s', $url)
178: );
179: }
180: $owa = new Horde_Kolab_FreeBusy_Freebusy_Helper_Owa(
181: $response->getStream()
182: );
183: $result = $owa->convert(
184: $startDate, $endDate, $this->_params['interval']
185: );
186: if (!isset($result[$this->_owner->getOwner()])) {
187: return array();
188: }
189: $events = array();
190: foreach ($result[$this->_owner->getOwner()] as $item) {
191: $events[] = new Horde_Kolab_FreeBusy_Object_Event($item);
192: }
193: return $events;
194: }
195: }
196: