1: <?php
2: /**
3: * Registry connector for Horde backend. Provides the communication between
4: * the Horde Registry on the local machine and the ActiveSync Horde driver.
5: *
6: * See the enclosed file COPYING for license information (LGPL). If you
7: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
8: *
9: * Copyright 2010-2012 Horde LLC (http://www.horde.org/)
10: *
11: * @author Michael J. Rubinsky <mrubinsk@horde.org>
12: * @package Core
13: */
14: class Horde_Core_ActiveSync_Connector
15: {
16: /**
17: * Horde registry
18: *
19: * @var Horde_Registry
20: */
21: private $_registry;
22:
23: /**
24: * Const'r
25: *
26: * @param array $params Configuration parameters. Requires:
27: * <pre>
28: * 'registry' - An instance of Horde_Registry
29: * </pre>
30: *
31: * @return Horde_ActiveSync_Driver_Horde_Connector_Registry
32: */
33: public function __construct($params = array())
34: {
35: if (empty($params['registry'])) {
36: throw new InvalidArgumentException('Missing required Horde_Registry object.');
37: }
38:
39: $this->_registry = $params['registry'];
40: }
41:
42: /**
43: * Get a list of events from horde's calendar api
44: *
45: * @param integer $startstamp The start of time period.
46: * @param integer $endstamp The end of time period
47: *
48: * @return array
49: */
50: public function calendar_listUids($startstamp, $endstamp)
51: {
52: try {
53: return $this->_registry->calendar->listUids(null, $startstamp, $endstamp);
54: } catch (Exception $e) {
55: return array();
56: }
57: }
58:
59: /**
60: * Get a list of event uids that have had $action happen since $from_ts.
61: *
62: * @param string $action The action to check for (add, modify, delete)
63: * @param integer $from_ts The timestamp to start checking from
64: * @param integer $to_ts The ending timestamp
65: *
66: * @return array An array of event uids
67: */
68: public function calendar_listBy($action, $from_ts, $to_ts)
69: {
70: try {
71: $uids = $this->_registry->calendar->listBy($action, $from_ts, null, $to_ts);
72: } catch (Exception $e) {
73: return array();
74: }
75: }
76:
77: /**
78: * Export the specified event as an ActiveSync message
79: *
80: * @param string $uid The calendar id
81: *
82: * @return Horde_ActiveSync_Message_Appointment
83: */
84: public function calendar_export($uid)
85: {
86: return $this->_registry->calendar->export($uid, 'activesync');
87: }
88:
89: /**
90: * Import an event into Horde's calendar store.
91: *
92: * @param Horde_ActiveSync_Message_Appointment $content The event content
93: * @param string $calendar The calendar to import event into
94: *
95: * @return string The event's UID
96: */
97: public function calendar_import($content)
98: {
99: return $this->_registry->calendar->import($content, 'activesync');
100: }
101:
102: /**
103: * Replace the event with new data
104: *
105: * @param string $uid The UID of the event to replace
106: * @param Horde_ActiveSync_Message_Appointment $content The new event content
107: *
108: * @return boolean
109: */
110: public function calendar_replace($uid, $content)
111: {
112: return $this->_registry->calendar->replace($uid, $content, 'activesync');
113: }
114:
115: /**
116: * Delete an event from Horde's calendar storage
117: *
118: * @param string $uid The UID of the event to delete
119: *
120: * @return boolean
121: */
122: public function calendar_delete($uid)
123: {
124: return $this->_registry->calendar->delete($uid);
125: }
126:
127: /**
128: * Return the timestamp for the last time $action was performed.
129: *
130: * @param string $uid The UID of the event we are interested in.
131: * @param string $action The action we are interested in (add, modify...)
132: *
133: * @return integer
134: */
135: public function calendar_getActionTimestamp($uid, $action)
136: {
137: return $this->_registry->calendar->getActionTimestamp($uid, $action);
138: }
139:
140: /**
141: * Get a list of all contacts a user can see
142: *
143: * @return array of contact UIDs
144: */
145: public function contacts_listUids()
146: {
147: return $this->_registry->contacts->listUids();
148: }
149:
150: /**
151: * Export the specified contact from Horde's contacts storage
152: *
153: * @param string $uid The contact's UID
154: *
155: * @return array The contact hash
156: */
157: public function contacts_export($uid)
158: {
159: return $this->_registry->contacts->export($uid, 'activesync');
160: }
161:
162: /**
163: * Import the provided contact data into Horde's contacts storage
164: *
165: * @param string $content The contact data
166: * @param string $source The contact source to import to
167: *
168: * @return boolean
169: */
170: public function contacts_import($content, $import_source = null)
171: {
172: return $this->_registry->contacts->import($content, 'activesync', $import_source);
173: }
174:
175: /**
176: * Replace the specified contact with the data provided.
177: *
178: * @param string $uid The UID of the contact to replace
179: * @param string $content The contact data
180: * @param string $sources The sources where UID will be replaced
181: *
182: * @return boolean
183: */
184: public function contacts_replace($uid, $content, $sources = null)
185: {
186: return $this->_registry->contacts->replace($uid, $content, 'activesync', $sources);
187: }
188:
189: /**
190: * Delete the specified contact
191: *
192: * @param string $uid The UID of the contact to remove
193: *
194: * @return bolean
195: */
196: public function contacts_delete($uid)
197: {
198: return $this->_registry->contacts->delete($uid);
199: }
200:
201: /**
202: * Get the timestamp of the most recent occurance of $action for the
203: * specifed contact
204: *
205: * @param string $uid The UID of the contact to search
206: * @param string $action The action to lookup
207: *
208: * @return integer
209: */
210: public function contacts_getActionTimestamp($uid, $action)
211: {
212: return $this->_registry->contacts->getActionTimestamp($uid, $action);
213: }
214:
215: /**
216: * Get a list of contact uids that have had $action happen since $from_ts.
217: *
218: * @param string $action The action to check for (add, modify, delete)
219: * @param integer $from_ts The timestamp to start checking from
220: * @param integer $to_ts The ending timestamp
221: *
222: * @return array An array of event uids
223: */
224: public function contacts_listBy($action, $from_ts, $to_ts)
225: {
226: return $this->_registry->contacts->listBy($action, $from_ts, null, $to_ts);
227: }
228:
229: public function contacts_search($query)
230: {
231: $gal = $this->contacts_getGal();
232: $fields = array($gal => array('firstname', 'lastname', 'alias', 'name', 'email'));
233: return $this->_registry->contacts->search(array($query), array($gal), $fields, true, true);
234: }
235:
236: /**
237: * Get the GAL source uid.
238: *
239: * @return string | boolean
240: */
241: public function contacts_getGal()
242: {
243: return $this->_registry->contacts->getGalUid();
244: }
245:
246: /**
247: * List all tasks in the user's default tasklist.
248: *
249: * @return array An array of task uids.
250: */
251: public function tasks_listUids()
252: {
253: return $this->_registry->tasks->listUids();
254: }
255:
256: public function tasks_listTaskLists()
257: {
258: return $this->_registry->tasks->listTaskLists();
259: }
260:
261: /**
262: * Export a single task from the backend.
263: *
264: * @param string $uid The task uid
265: *
266: * @return Horde_ActiveSync_Message_Task The task message object
267: */
268: public function tasks_export($uid)
269: {
270: return $this->_registry->tasks->export($uid, 'activesync');
271: }
272:
273: /**
274: * Importa a single task into the backend.
275: *
276: * @param Horde_ActiveSync_Message_Task $message The task message object
277: *
278: * @return string The newly added task's uid.
279: */
280: public function tasks_import($message)
281: {
282: return $this->_registry->tasks->import($message, 'activesync');
283: }
284:
285: /**
286: * Replace an existing task with the provided task.
287: *
288: * @param string $uid The existing tasks's uid
289: * @param Horde_ActiveSync_Message_Task $message The task object
290: *
291: * @return boolean
292: */
293: public function tasks_replace($uid, $message)
294: {
295: return $this->_registry->tasks->replace($uid, $message, 'activesync');
296: }
297:
298: /**
299: * Delete a task from the backend.
300: *
301: * @param string $id The task's uid
302: *
303: * @return boolean
304: */
305: public function tasks_delete($id)
306: {
307: return $this->_registry->tasks->delete($id);
308: }
309:
310: /**
311: * Return the timestamp for the last time $action was performed.
312: *
313: * @param string $uid The UID of the task we are interested in.
314: * @param string $action The action we are interested in (add, modify...)
315: *
316: * @return integer
317: */
318: public function tasks_getActionTimestamp($uid, $action)
319: {
320: return $this->_registry->tasks->getActionTimestamp($uid, $action);
321: }
322:
323: /**
324: * Get a list of task uids that have had $action happen since $from_ts.
325: *
326: * @param string $action The action to check for (add, modify, delete)
327: * @param integer $from_ts The timestamp to start checking from
328: * @param integer $to_ts The ending timestamp
329: *
330: * @return array An array of event uids
331: */
332: public function tasks_listBy($action, $from_ts)
333: {
334: return $this->_registry->tasks->listBy($action, $from_ts, null, $to_ts);
335: }
336:
337: /**
338: * Return all active api interfaces.
339: *
340: * @return array An array of interface names.
341: */
342: public function horde_listApis()
343: {
344: return $this->_registry->horde->listAPIs();
345: }
346:
347: /**
348: * Obtain a user's preference setting.
349: *
350: * @param string $app The Horde application providing the setting.
351: * @param string $pref The name of the preference setting.
352: *
353: * @return mixed The preference value
354: */
355: public function horde_getPref($app, $pref)
356: {
357: return $this->_registry->horde->getPreference($app, $pref);
358: }
359:
360: /**
361: * Obtain the name of the Horde application that provides the specified api
362: * interface.
363: *
364: * @param string $api The interface name
365: *
366: * @return string The application name.
367: */
368: public function horde_hasInterface($api)
369: {
370: return $this->_registry->hasInterface($api);
371: }
372:
373: /**
374: * Get all server changes for the specified collection
375: * @param string $collection The collection type (calendar, contacts, tasks)
376: * @param integer $from_ts Starting timestamp
377: * @param integer $to_ts Ending timestamp
378: *
379: * @return array A hash of add, modify, and delete uids
380: * @throws InvalidArgumentException
381: */
382: public function getChanges($collection, $from_ts, $to_ts)
383: {
384: if (!in_array($collection, array('calendar', 'contacts', 'tasks'))) {
385: throw new InvalidArgumentException('collection must be one of calendar, contacts, or tasks');
386: }
387: try {
388: return $this->_registry->{$collection}->getChanges($from_ts, $to_ts);
389: } catch (Exception $e) {
390: return array('add' => array(),
391: 'modify' => array(),
392: 'delete' => array());
393: }
394: }
395:
396: /**
397: * Clear the authentication and destroy the current session.
398: */
399: public function clearAuth()
400: {
401: $this->_registry->clearAuth(true);
402: }
403:
404: }
405: