File: kolab.php

package info (click to toggle)
mnemo2 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,400 kB
  • ctags: 854
  • sloc: php: 2,718; xml: 842; sql: 211; makefile: 65
file content (781 lines) | stat: -rw-r--r-- 21,852 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<?php

require_once 'Horde/Kolab.php';

/**
 * Horde Mnemo driver for the Kolab IMAP server.
 *
 * $Horde: mnemo/lib/Driver/kolab.php,v 1.7.2.11 2008/03/12 08:16:03 wrobel Exp $
 *
 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Gunnar Wrobel <wrobel@pardus.de>
 * @author  Thomas Jarosch <thomas.jarosch@intra2net.com>
 * @author  Stuart Binge <omicron@mighty.co.za>
 * @since   Mnemo 2.0
 * @package Mnemo
 */
class Mnemo_Driver_kolab extends Mnemo_Driver {

    /**
     * Our Kolab server connection.
     *
     * @var Kolab
     */
    var $_kolab = null;

    /**
     * The wrapper to decide between the Kolab implementation
     *
     * @var Mnemo_Driver_kolab_wrapper
     */
    var $_wrapper = null;

    function Mnemo_Driver_kolab($notepad, $params = array())
    {
        if (empty($notepad)) {
            $notepad = Auth::getAuth();
        }

        $this->_notepad = $notepad;

        $this->_kolab = &new Kolab();
        if (empty($this->_kolab->version)) {
            $wrapper = "Mnemo_Driver_kolab_wrapper_old";
        } else {
            $wrapper = "Mnemo_Driver_kolab_wrapper_new";
        }

        $this->_wrapper = &new $wrapper($this);

    }

    /**
     * Connect to the Kolab backend
     *
     * @return boolean  True on success, PEAR_Error on failure.
     */
    function initialize()
    {
        return $this->_wrapper->connect();
    }

    /**
     * Retrieve one note from the store.
     *
     * @param string $noteId      The ID of the note to retrieve.
     * @param string $passphrase  A passphrase with which this note was
     *                            supposed to be encrypted.
     *
     * @return array  The array of note attributes.
     */
    function get($noteId, $passphrase = null)
    {
        return $this->_wrapper->get($noteId, $passphrase);
    }

    /**
     * Retrieve one note by UID.
     *
     * @param string $uid         The UID of the note to retrieve.
     * @param string $passphrase  A passphrase with which this note was
     *                            supposed to be encrypted.
     *
     * @return array  The array of note attributes.
     */
    function getByUID($uid, $passphrase = null)
    {
        return $this->_wrapper->getByUID($uid, $passphrase);
    }

    /**
     * Add a note to the backend storage.
     *
     * @param string $desc        The description (long) of the note.
     * @param string $body        The description (long) of the note.
     * @param string $category    The category of the note.
     * @param string $passphrase  The passphrase to encrypt the note with.
     *
     * @return mixed The id of the note if successful, a PEAR error
     * otherwise
     */
    function add($desc, $body, $category = '', $passphrase = null)
    {
        return $this->_wrapper->add($desc, $body, $category, $passphrase);
    }

    /**
     * Modify an existing note.
     *
     * @param integer $noteId   The note to modify.
     * @param string $desc      The description (long) of the note.
     * @param string $body      The description (long) of the note.
     * @param string $category  The category of the note.
     * @param string $passphrase  The passphrase to encrypt the note with.
     *
     * @return booelan  True if successful, a PEAR error otherwise.
     */
    function modify($noteId, $desc, $body, $category = '', $passphrase = null)
    {
        return $this->_wrapper->modify($noteId, $desc, $body, $category, $passphrase);
    }

    /**
     * Move a note to a new notepad.
     *
     * @param string $noteId      The note to move.
     * @param string $newNotepad  The new notepad.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function move($noteId, $newNotepad)
    {
        return $this->_wrapper->move($noteId, $newNotepad);
    }

    /**
     * Delete the specified note from the current notepad
     *
     * @param string $noteId      The note to delete.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function delete($noteId)
    {
        return $this->_wrapper->delete($noteId);
    }

    /**
     * Delete all notes from the current notepad
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function deleteAll()
    {
        return $this->_wrapper->deleteAll();
    }

    /**
     * Retrieves all of the notes from $this->_notepad from the database.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function retrieve()
    {
        $this->_memos = array();

        $memos = $this->_wrapper->retrieve();
        if (is_a($memos, 'PEAR_Error')) {
            return $memos;
        }

        $this->_memos = $memos;

        return true;
    }
}

/**
 * Horde Mnemo wrapper to distinguish between both Kolab driver implementations.
 *
 * $Horde: mnemo/lib/Driver/kolab.php,v 1.7.2.11 2008/03/12 08:16:03 wrobel Exp $
 *
 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Gunnar Wrobel <wrobel@pardus.de>
 * @since   Mnemo 2.0
 * @package Mnemo
 */
class Mnemo_Driver_kolab_wrapper {

    /**
     * Indicates if the wrapper has connected or not
     *
     * @var boolean
     */
    var $_connected = false;

    /**
     * String containing the current notepad name.
     *
     * @var string
     */
    var $_notepad = '';

    /**
     * Our Kolab server connection.
     *
     * @var Kolab
     */
    var $_kolab = null;

    /**
     * Our parent driver.
     *
     * @var Mnemo_Driver
     */
    var $_driver;

    /**
     * Constructor
     *
     * @param string      $notepad  The notepad to load.
     * @param Horde_Kolab $kolab    The Kolab connection object
     */
    function Mnemo_Driver_kolab_wrapper(&$driver)
    {
        $this->_notepad = $driver->_notepad;
        $this->_kolab = &$driver->_kolab;
        // Required for the encrypt() function
        $this->_driver = &$driver;
    }

    /**
     * Connect to the Kolab backend
     *
     * @param int    $loader         The version of the XML
     *                               loader
     *
     * @return mixed True on success, a PEAR error otherwise
     */
    function connect($loader = 0)
    {
        if ($this->_connected) {
            return true;
        }

        $result = $this->_kolab->open($this->_notepad, $loader);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        $this->_connected = true;

        return true;
    }

    /**
     * Encrypts a note.
     *
     * @param string $note        The note text.
     * @param string $passphrase  The passphrase to encrypt the note with.
     *
     * @return string|PEAR_Error  The encrypted text or PEAR_Error on failure.
     */
    function encrypt($note, $passphrase)
    {
        return $this->_driver->encrypt($note, $passphrase);
    }

    /**
     * Decrypts a note.
     *
     * @param string $note        The encrypted note text.
     * @param string $passphrase  The passphrase to decrypt the note with.
     *
     * @return string|PEAR_Error  The decrypted text or PEAR_Error on failure.
     */
    function decrypt($note, $passphrase)
    {
        return $this->_driver->decrypt($note, $passphrase);
    }
}


/**
 * Old Horde Mnemo driver for the Kolab IMAP server.
 *
 * $Horde: mnemo/lib/Driver/kolab.php,v 1.7.2.11 2008/03/12 08:16:03 wrobel Exp $
 *
 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Stuart Binge <omicron@mighty.co.za>
 * @since   Mnemo 2.0
 * @package Mnemo
 */
class Mnemo_Driver_kolab_wrapper_old extends Mnemo_Driver_kolab_wrapper {

    function _buildNote()
    {
        return array(
            'memolist_id' => $this->_notepad,
            'memo_id' => $this->_kolab->getUID(),
            'uid' => $this->_kolab->getUID(),
            'desc' => $this->_kolab->getStr('summary'),
            'body' => $this->_kolab->getStr('body'),
            'category' => $this->_kolab->getStr('categories'),
            'encrypted' => false,
        );
    }

    /**
     * Retrieve one note from the store.
     *
     * @param string $noteId  The ID of the note to retrieve.
     *
     * @return array  The array of note attributes.
     */
    function get($noteId)
    {
        $result = $this->_kolab->loadObject($noteId);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        return $this->_buildNote();
    }

    /**
     * Retrieve one note by UID.
     *
     * @param string $uid  The UID of the note to retrieve.
     *
     * @return array  The array of note attributes.
     */
    function getByUID($uid)
    {
        return PEAR::raiseError('Not supported');
    }

    function _setObject($desc, $body, $category = '', $uid = null)
    {
        if (isset($uid)) {
            $result = $this->_kolab->loadObject($uid);
        } else {
            $uid = md5(uniqid(mt_rand(), true));
            $result = $this->_kolab->newObject($uid);
        }
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        $this->_kolab->setStr('summary', $desc);
        $this->_kolab->setStr('body', $body);
        $this->_kolab->setStr('categories', $category);

        $result = $this->_kolab->saveObject();
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        return $uid;
    }

    /**
     * Add a note to the backend storage.
     *
     * @param string $desc      The description (long) of the note.
     * @param string $body      The description (long) of the note.
     * @param string $category  The category of the note.
     *
     * @return integer  The numeric ID of the new note.
     */
    function add($desc, $body, $category = '')
    {
        return $this->_setObject($desc, $body, $category);
    }

    /**
     * Modify an existing note.
     *
     * @param integer $noteId   The note to modify.
     * @param string $desc      The description (long) of the note.
     * @param string $body      The description (long) of the note.
     * @param string $category  The category of the note.
     */
    function modify($noteId, $desc, $body, $category = '')
    {
        $result = $this->_setObject($desc, $body, $category, $noteId);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        return $result == $noteId;
    }

    /**
     * Move a note to a new notepad.
     *
     * @param string $noteId      The note to move.
     * @param string $newNotepad  The new notepad.
     */
    function move($noteId, $newNotepad)
    {
        return $this->_kolab->moveObject($noteId, $newNotepad);
    }

    function delete($noteId)
    {
        return $this->_kolab->removeObjects($noteId);
    }

    function deleteAll()
    {
        return $this->_kolab->removeAllObjects();
    }

    /**
     * Retrieves all of the notes from $this->_notepad from the database.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function retrieve()
    {
        $memos = array();

        $msg_list = $this->_kolab->listObjects();
        if (is_a($msg_list, 'PEAR_Error')) {
            return $msg_list;
        }

        if (empty($msg_list)) {
            return $memos;
        }

        foreach ($msg_list as $msg) {
            $xml = &$this->_kolab->loadObject($msg, true);
            if (is_a($xml, 'PEAR_Error')) {
                return $xml;
            }

            $memos[$this->_kolab->getUID()] = $this->_buildNote($xml);
        }

        return $memos;
    }

}

/**
 * New Horde Mnemo driver for the Kolab IMAP server.
 *
 * $Horde: mnemo/lib/Driver/kolab.php,v 1.7.2.11 2008/03/12 08:16:03 wrobel Exp $
 *
 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Gunnar Wrobel <wrobel@pardus.de>
 * @since   Mnemo 2.0
 * @package Mnemo
 */
class Mnemo_Driver_kolab_wrapper_new extends Mnemo_Driver_kolab_wrapper {

    /**
     * Shortcut to the imap connection
     *
     * @var Kolab_IMAP
     */
    var $_store = null;

    /**
     * Connect to the Kolab backend
     *
     * @return mixed True on success, a PEAR error otherwise
     */
    function connect()
    {
        if ($this->_connected) {
            return true;
        }

        $result = parent::connect(1);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        $this->_store = &$this->_kolab->_storage;

        return true;
    }

    /**
     * Split the notepad name of the id. We use this to make ids
     * unique across folders.
     *
     * @param string $id The ID of the note appended with the notepad
     *                   name.
     *
     * @return array  The note id and notepad name
     */
    function _splitId($id)
    {
        $split = split('@', $id, 2);
        if (count($split) == 2) {
            list($id, $notepad) = $split;
        } else if (count($split) == 1) {
            $notepad = Auth::getAuth();
        }
        return array($id, $notepad);
    }

    /**
     * Append the notepad name to the id. We use this to make ids
     * unique across folders.
     *
     * @param string $id The ID of the note
     *
     * @return string  The note id appended with the notepad
     *                 name.
     */
    function _uniqueId($id)
    {
        if ($this->_notepad == Auth::getAuth()) {
            return $id;
        }
        return $id . '@' . $this->_notepad;
    }

    /**
     * Retrieve one note from the store.
     *
     * @param string $noteId      The ID of the note to retrieve.
     * @param string $passphrase  A passphrase with which this note was
     *                            supposed to be encrypted.
     *
     * @return array  The array of note attributes.
     */
    function get($noteId, $passphrase = null)
    {
        list($noteId, $notepad) = $this->_splitId($noteId);

        if ($this->_store->objectUidExists($noteId)) {
            $note = $this->_store->getObject($noteId);
            return $this->_buildNote($note, $passphrase);
        } else {
            return PEAR::raiseError(sprintf(_('Did not find note %s'), $noteId));
        }
    }

    /**
     * Retrieve one note by UID.
     *
     * @param string $uid         The UID of the note to retrieve.
     * @param string $passphrase  A passphrase with which this note was
     *                            supposed to be encrypted.
     *
     * @return array  The array of note attributes.
     */
    function getByUID($uid, $passphrase = null)
    {
        list($noteId, $notepad) = $this->_splitId($uid);

        if ($this->_notepad != $notepad) {
            $this->_notepad = $notepad;
            $this->_connected = false;
            $this->connect();
        }

        return $this->get($noteId, $passphrase);
    }

    /**
     * Add or modify a note.
     *
     * @param string $desc        The description (long) of the note.
     * @param string $body        The description (long) of the note.
     * @param string $category    The category of the note.
     * @param string $uid         The note to modify.
     * @param string $passphrase  The passphrase to encrypt the note with.
     *
     * @return mixed The id of the note if successful, a PEAR error
     * otherwise
     */
    function _setObject($desc, $body, $category = '', $uid = null, $passphrase = null)
    {
        if (empty($uid)) {
            $note_uid = $this->_store->generateUID();
            $old_uid = null;
            $action = array('action' => 'add');
        } else {
            list($note_uid, $notepad) = $this->_splitId($uid);
            $old_uid = $note_uid;
            $action = array('action' => 'modify');
        }

        if ($passphrase) {
            $body = $this->encrypt($body, $passphrase);
            if (is_a($body, 'PEAR_Error')) {
                return $body;
            }
            Mnemo::storePassphrase($note_uid, $passphrase);
        }

        $result = $this->_store->save(array('uid' => $note_uid,
                                            'desc' => $desc,
                                            'body' => $body,
                                            'categories' => $category,
                                            ),
                                      $old_uid);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        /* Log the action in the history log. */
        $history = &Horde_History::singleton();
        $history->log('mnemo:' . $this->_notepad . ':' . $this->_uniqueId($note_uid), $action, true);

        return $this->_uniqueId($note_uid);
    }

    /**
     * Add a note to the backend storage.
     *
     * @param string $desc        The description (long) of the note.
     * @param string $body        The description (long) of the note.
     * @param string $category    The category of the note.
     * @param string $passphrase  The passphrase to encrypt the note with.
     *
     * @return mixed The id of the note if successful, a PEAR error
     * otherwise
     */
    function add($desc, $body, $category = '', $passphrase = null)
    {
        return $this->_setObject($desc, $body, $category, null, $passphrase);
    }

    /**
     * Modify an existing note.
     *
     * @param integer $noteId   The note to modify.
     * @param string $desc      The description (long) of the note.
     * @param string $body      The description (long) of the note.
     * @param string $category  The category of the note.
     * @param string $passphrase  The passphrase to encrypt the note with.
     *
     * @return booelan  True if successful, a PEAR error otherwise.
     */
    function modify($noteId, $desc, $body, $category = '', $passphrase = null)
    {
        $result = $this->_setObject($desc, $body, $category, $noteId, $passphrase);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        return $result == $noteId;
    }

    /**
     * Move a note to a new notepad.
     *
     * @param string $noteId      The note to move.
     * @param string $newNotepad  The new notepad.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function move($noteId, $newNotepad)
    {
        list($noteId, $notepad) = $this->_splitId($noteId);

        return $this->_store->move($noteId, $newNotepad);
    }

    /**
     * Delete the specified note from the current notepad
     *
     * @param string $noteId      The note to delete.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function delete($noteId)
    {
        list($noteId, $notepad) = $this->_splitId($noteId);

        $result = $this->_store->delete($noteId);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        $history = &Horde_History::singleton();
        $history->log('mnemo:' . $this->_notepad . ':' . $this->_uniqueId($noteId), array('action' => 'delete'), true);

        return $result;
    }

    /**
     * Delete all notes from the current notepad
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function deleteAll()
    {
        return $this->_store->deleteAll();
    }

    /**
     * Retrieves all of the notes from $this->_notepad from the database.
     *
     * @return mixed  True on success, PEAR_Error on failure.
     */
    function retrieve()
    {
        $memos = array();

        $note_list = $this->_store->getObjects();
        if (is_a($note_list, 'PEAR_Error')) {
            return $note_list;
        }

        if (empty($note_list)) {
            return $memos;
        }

        foreach ($note_list as $note) {
            $nuid = $this->_uniqueId($note['uid']);
            $memos[$nuid] = $this->_buildNote($note);
        }

        return $memos;
    }

    /**
     * Build a note based on data array
     *
     * @param array  $note     The data for the note
     * @param string $passphrase A passphrase for decrypting a note
     *
     * @return array  The converted data array representing the note
     */
    function _buildNote($note, $passphrase = null)
    {
        $note['memolist_id'] = $this->_notepad;
        $note['memo_id'] = $this->_uniqueId($note['uid']);

        $note['category'] = $note['categories'];
        unset($note['categories']);

        $note['encrypted'] = false;
        $body = $note['body'];

        if (strpos($body, '-----BEGIN PGP MESSAGE-----') === 0) {
            $note['encrypted'] = true;
            if (empty($passphrase)) {
                $passphrase = Mnemo::getPassphrase($note['uid']);
            }
            if (empty($passphrase)) {
                $body = PEAR::raiseError(_("This note has been encrypted."), MNEMO_ERR_NO_PASSPHRASE);
            } else {
                $body = $this->decrypt($body, $passphrase);
                if (is_a($body, 'PEAR_Error')) {
                    $body->code = MNEMO_ERR_DECRYPT;
                } else {
                    $body = $body->message;
                    Mnemo::storePassphrase($note['memo_id'], $passphrase);
                }
            }
        }
        $note['body'] = $body;

        return $note;
    }
}