1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: class Mnemo_Api extends Horde_Registry_Api
18: {
19: 20: 21: 22: 23: 24: 25: 26: 27:
28: public function removeUserData($user)
29: {
30: try {
31: $GLOBALS['registry']->removeUserData($user, 'mnemo');
32: } catch (Horde_Exception $e) {
33: throw new Mnemo_Exception($e);
34: }
35: }
36:
37: 38: 39: 40: 41: 42: 43:
44: public function listNotepads($owneronly, $permission)
45: {
46: return Mnemo::listNotepads($owneronly, $permission);
47: }
48:
49: 50: 51: 52: 53: 54: 55: 56: 57: 58:
59: public function listUids($notepad = null)
60: {
61: global $conf;
62:
63: if (!isset($conf['storage']['driver'])) {
64: throw new Mnemo_Exception('Not configured');
65: }
66:
67:
68: if (empty($notepad)) {
69: $notepad = Mnemo::getDefaultNotepad();
70: }
71:
72: if (!array_key_exists($notepad, Mnemo::listNotepads(false, Horde_Perms::READ))) {
73: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
74: }
75:
76:
77: $GLOBALS['display_notepads'] = array($notepad);
78: $memos = Mnemo::listMemos();
79: $uids = array();
80: foreach ($memos as $memo) {
81: $uids[] = $memo['uid'];
82: }
83:
84: return $uids;
85: }
86:
87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97:
98: public function getChanges($start, $end)
99: {
100: return array('add' => $this->listBy('add', $start, null, $end),
101: 'modify' => $this->listBy('modify', $start, null, $end),
102: 'delete' => $this->listBy('delete', $start, null, $end));
103: }
104:
105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115:
116: public function listBy($action, $timestamp, $notepad = null, $end = null)
117: {
118:
119: if (empty($notepad)) {
120: $notepad = Mnemo::getDefaultNotepad();
121: }
122:
123: if (!array_key_exists($notepad, Mnemo::listNotepads(false, Horde_Perms::READ))) {
124: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
125: }
126:
127: $filter = array(array('op' => '=', 'field' => 'action', 'value' => $action));
128: if (!empty($end)) {
129: $filter[] = array('op' => '<', 'field' => 'ts', 'value' => $end);
130: }
131: $history = $GLOBALS['injector']->getInstance('Horde_History');
132: $histories = $history->getByTimestamp('>', $timestamp, $filter, 'mnemo:' . $notepad);
133:
134:
135: return preg_replace('/^([^:]*:){2}/', '', array_keys($histories));
136: }
137:
138: 139: 140: 141: 142: 143: 144: 145: 146: 147:
148: public function getActionTimestamp($uid, $action, $notepad = null)
149: {
150:
151: if (empty($notepad)) {
152: $notepad = Mnemo::getDefaultNotepad();
153: }
154:
155: if (!array_key_exists($notepad, Mnemo::listNotepads(false, Horde_Perms::READ))) {
156: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
157: }
158:
159: $history = $GLOBALS['injector']->getInstance('Horde_History');
160: return $history->getActionTimestamp('mnemo:' . $notepad . ':' . $uid, $action);
161: }
162:
163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175:
176: public function import($content, $contentType, $notepad = null)
177: {
178: global $prefs;
179:
180: 181:
182: if (empty($notepad)) {
183: $notepad = Mnemo::getDefaultNotepad(Horde_Perms::EDIT);
184: }
185:
186: if (!array_key_exists($notepad, Mnemo::listNotepads(false, Horde_Perms::EDIT))) {
187: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
188: }
189:
190:
191: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create($notepad);
192:
193: switch ($contentType) {
194: case 'text/plain':
195: $noteId = $storage->add($storage->getMemoDescription($content), $content);
196: break;
197:
198: case 'text/x-vnote':
199: if (!($content instanceof Horde_Icalendar_Vnote)) {
200: $iCal = new Horde_Icalendar();
201: if (!$iCal->parsevCalendar($content)) {
202: throw new Mnemo_Exception(_("There was an error importing the iCalendar data."));
203: }
204:
205: $components = $iCal->getComponents();
206: switch (count($components)) {
207: case 0:
208: throw new Mnemo_Exception(_("No iCalendar data was found."));
209:
210: case 1:
211: $content = $components[0];
212: break;
213:
214: default:
215: $ids = array();
216: foreach ($components as $content) {
217: if ($content instanceof Horde_Icalendar_Vnote) {
218: $note = $storage->fromiCalendar($content);
219: $noteId = $storage->add($note['desc'],
220: $note['body'],
221: !empty($note['category']) ? $note['category'] : '');
222: $ids[] = $noteId;
223: }
224: }
225: return $ids;
226: }
227: }
228:
229: $note = $storage->fromiCalendar($content);
230: $noteId = $storage->add($note['desc'],
231: $note['body'], !empty($note['category']) ? $note['category'] : '');
232: break;
233:
234: default:
235: throw new Mnemo_Exception(sprintf(_("Unsupported Content-Type: %s"), $contentType));
236: }
237: $note = $storage->get($noteId);
238:
239: return $note['uid'];
240: }
241:
242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256:
257: public function export($uid, $contentType)
258: {
259: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create();
260: try {
261: $memo = $storage->getByUID($uid);
262: } catch (Mnemo_Exception $e) {
263: if ($e->getCode() == Mnemo::ERR_NO_PASSPHRASE ||
264: $e->getCode() == Mnemo::ERR_DECRYPT) {
265:
266: $memo['body'] = _("This note has been encrypted.");
267: } else {
268: throw $e;
269: }
270: }
271: if (!array_key_exists($memo['memolist_id'], Mnemo::listNotepads(false, Horde_Perms::READ))) {
272: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
273: }
274:
275: switch ($contentType) {
276: case 'text/plain':
277: return $memo['body'];
278:
279: case 'text/x-vnote':
280:
281: $iCal = new Horde_Icalendar('1.1');
282: $iCal->setAttribute('VERSION', '1.1');
283: $iCal->setAttribute('PRODID', '-//The Horde Project//Mnemo ' . $registry->getVersion() . '//EN');
284: $iCal->setAttribute('METHOD', 'PUBLISH');
285:
286:
287: $vNote = $storage->toiCalendar($memo, $iCal);
288: return $vNote->exportvCalendar();
289: }
290:
291: throw new Mnemo_Exception(sprintf(_("Unsupported Content-Type: %s"),$contentType));
292: }
293:
294: 295: 296: 297: 298: 299: 300:
301: public function delete($uid)
302: {
303:
304:
305: if (is_array($uid)) {
306: foreach ($uid as $u) {
307: $result = $this->delete($u);
308: }
309: }
310:
311:
312: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create();
313: $memo = $storage->getByUID($uid);
314: if (!$GLOBALS['registry']->isAdmin() &&
315: !array_key_exists($memo['memolist_id'], Mnemo::listNotepads(false, Horde_Perms::DELETE))) {
316:
317: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
318: }
319:
320: $storage->delete($memo['memo_id']);
321: }
322:
323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334:
335: public function replace($uid, $content, $contentType)
336: {
337: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create();
338: $memo = $storage->getByUID($uid);
339: if (!array_key_exists($memo['memolist_id'], Mnemo::listNotepads(false, Horde_Perms::EDIT))) {
340: throw new Horde_Exception_PermissionDenied(_("Permission Denied"));
341: }
342:
343: switch ($contentType) {
344: case 'text/plain':
345: $storage->modify($memo['memo_id'], $storage->getMemoDescription($content), $content, null);
346: break;
347: case 'text/x-vnote':
348: if (!($content instanceof Horde_Icalendar_Vnote)) {
349: $iCal = new Horde_Icalendar();
350: if (!$iCal->parsevCalendar($content)) {
351: throw new Mnemo_Exception(_("There was an error importing the iCalendar data."));
352: }
353:
354: $components = $iCal->getComponents();
355: switch (count($components)) {
356: case 0:
357: throw new Mnemo_Exception(_("No iCalendar data was found."));
358:
359: case 1:
360: $content = $components[0];
361: break;
362:
363: default:
364: throw new Mnemo_Exception(_("Multiple iCalendar components found; only one vNote is supported."));
365: }
366: }
367: $note = $storage->fromiCalendar($content);
368: $storage->modify($memo['memo_id'],
369: $note['desc'],
370: $note['body'],
371: !empty($note['category']) ? $note['category'] : '');
372: break;
373: default:
374: throw new Mnemo_Exception(sprintf(_("Unsupported Content-Type: %s"),$contentType));
375: }
376: }
377:
378: }
379: