Overview

Packages

  • Mnemo
  • None

Classes

  • Mnemo
  • Mnemo_Ajax_Application
  • Mnemo_Ajax_Imple_EditNote
  • Mnemo_Api
  • Mnemo_Driver
  • Mnemo_Driver_Kolab
  • Mnemo_Driver_Sql
  • Mnemo_Exception
  • Mnemo_Factory_Driver
  • Mnemo_Factory_Notepads
  • Mnemo_Form_CreateNotepad
  • Mnemo_Form_DeleteNotepad
  • Mnemo_Form_EditNotepad
  • Mnemo_Notepads_Base
  • Mnemo_Notepads_Default
  • Mnemo_Notepads_Kolab
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Mnemo_Driver:: defines an API for implementing storage backends for Mnemo.
  4:  *
  5:  * Copyright 2001-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file LICENSE for license information (ASL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/apache.
  9:  *
 10:  * @author  Jon Parise <jon@horde.org>
 11:  * @author  Michael J. Rubinsky <mrubinsk@horde.org>
 12:  * @package Mnemo
 13:  */
 14: abstract class Mnemo_Driver
 15: {
 16:     /**
 17:      * Array holding the current memo list.  Each array entry is a hash
 18:      * describing a memo.  The array is indexed numerically by memo ID.
 19:      *
 20:      * @var array
 21:      */
 22:     protected $_memos = array();
 23: 
 24:     /**
 25:      * String containing the current notepad name.
 26:      *
 27:      * @var string
 28:      */
 29:     protected $_notepad = '';
 30: 
 31:     /**
 32:      * Crypting processor.
 33:      *
 34:      * @var Horde_Crypt_pgp
 35:      */
 36:     protected $_pgp;
 37: 
 38:     /**
 39:      * Lists memos based on the given criteria. All memos will be
 40:      * returned by default.
 41:      *
 42:      * @return array    Returns a list of the requested memos.
 43:      */
 44:     public function listMemos()
 45:     {
 46:         return $this->_memos;
 47:     }
 48: 
 49:     /**
 50:      * Update the description (short summary) of a memo.
 51:      *
 52:      * @param integer $memo_id  The memo to update.
 53:      */
 54:     public function getMemoDescription($body)
 55:     {
 56:         if (!strstr($body, "\n") && Horde_String::length($body) <= 64) {
 57:             return trim($body);
 58:         }
 59: 
 60:         $lines = explode("\n", $body);
 61:         if (!is_array($lines)) {
 62:             return trim(Horde_String::substr($body, 0, 64));
 63:         }
 64: 
 65:         // Move to a line with more than spaces.
 66:         $i = 0;
 67:         while (isset($lines[$i]) && !preg_match('|[^\s]|', $lines[$i])) {
 68:             $i++;
 69:         }
 70:         if (Horde_String::length($lines[$i]) <= 64) {
 71:             return trim($lines[$i]);
 72:         } else {
 73:             return trim(Horde_String::substr($lines[$i], 0, 64));
 74:         }
 75:     }
 76: 
 77:     /**
 78:      * Loads the PGP encryption driver.
 79:      *
 80:      * @TODO: Inject *into* driver from the factory binder
 81:      */
 82:     protected function _loadPGP()
 83:     {
 84:         if (empty($GLOBALS['conf']['gnupg']['path'])) {
 85:             throw new Mnemo_Exception(_("Encryption support has not been configured, please contact your administrator."));
 86:         }
 87:  
 88:         $this->_pgp = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Crypt')->create('pgp', array(
 89:             'program' => $GLOBALS['conf']['gnupg']['path']
 90:         ));
 91:     }
 92: 
 93:     /**
 94:      * Encrypts a note.
 95:      *
 96:      * @param string $note        The note text.
 97:      * @param string $passphrase  The passphrase to encrypt the note with.
 98:      *
 99:      * @return string  The encrypted text.
100:      */
101:     protected function _encrypt($note, $passphrase)
102:     {
103:         $this->_loadPGP();
104:         return $this->_pgp->encrypt($note, array('type' => 'message', 'symmetric' => true, 'passphrase' => $passphrase));
105:     }
106: 
107:     /**
108:      * Decrypts a note.
109:      *
110:      * @param string $note        The encrypted note text.
111:      * @param string $passphrase  The passphrase to decrypt the note with.
112:      *
113:      * @return string  The decrypted text.
114:      * @throws Mnemo_Exception
115:      */
116:     protected function _decrypt($note, $passphrase)
117:     {
118:         $this->_loadPGP();
119: 
120:         try {
121:             return $this->_pgp->decrypt($note, array('type' => 'message', 'passphrase' => $passphrase));
122:         } catch (Horde_Crypt_Exception $e) {
123:             throw new Mnemo_Exception($e->getMessage(), Mnemo::ERR_DECRYPT);
124:         }
125:     }
126: 
127:     /**
128:      * Returns whether note encryption is supported.
129:      *
130:      * Checks if PGP support could be loaded, if it supports symmetric
131:      * encryption, and if we have a secure connection.
132:      *
133:      * @return boolean  Whether encryption is suppoted.
134:      */
135:     public function encryptionSupported()
136:     {
137:         try {
138:             $this->_loadPGP();
139:         } catch (Mnemo_Exception $e) {
140:         }
141:         return (is_callable(array($this->_pgp, 'encryptedSymmetrically')) &&
142:                 Horde::isConnectionSecure());
143:     }
144: 
145:     /**
146:      * Export this memo in iCalendar format.
147:      *
148:      * @param array  memo      The memo (hash array) to export
149:      * @param Horde_Icalendar  A Horde_Icalendar object that acts as container.
150:      *
151:      * @return Horde_Icalendar_Vnote  object for this event.
152:      */
153:     public function toiCalendar($memo, $calendar)
154:     {
155:         global $prefs;
156: 
157:         $vnote = Horde_Icalendar::newComponent('vnote', $calendar);
158: 
159:         $vnote->setAttribute('UID', $memo['uid']);
160:         $vnote->setAttribute('BODY', $memo['body']);
161:         $vnote->setAttribute('SUMMARY', $this->getMemoDescription($memo['body']));
162: 
163:         if (!empty($memo['category'])) {
164:             $vnote->setAttribute('CATEGORIES', $memo['category']);
165:         }
166: 
167:         /* Get the note's history. */
168:         $history = $GLOBALS['injector']->getInstance('Horde_History');
169:         $log = $history->getHistory('mnemo:' . $memo['memolist_id'] . ':' . $memo['uid']);
170:         if ($log) {
171:             foreach ($log->getData() as $entry) {
172:                 switch ($entry['action']) {
173:                 case 'add':
174:                     $created = $entry['ts'];
175:                     break;
176: 
177:                 case 'modify':
178:                     $modified = $entry['ts'];
179:                     break;
180:                 }
181:             }
182:         }
183: 
184:         if (!empty($created)) {
185:             $vnote->setAttribute('DCREATED', $created);
186:         }
187:         if (!empty($modified)) {
188:             $vnote->setAttribute('LAST-MODIFIED', $modified);
189:         }
190: 
191:         return $vnote;
192:     }
193: 
194:     /**
195:      * Create a memo (hash array) from a Horde_Icalendar_Vnote object.
196:      *
197:      * @param Horde_Icalendar_Vnote $vnote  The iCalendar data to update from.
198:      *
199:      * @return array  Memo (hash array) created from the vNote.
200:      */
201:     public function fromiCalendar(Horde_Icalendar_Vnote $vNote)
202:     {
203:         $memo = array();
204: 
205:         try {
206:             $body = $vNote->getAttribute('BODY');
207:         } catch (Horde_Icalendar_Exception $e) {
208:         }
209:         if (!is_array($body)) {
210:             $memo['body'] = $body;
211:         } else {
212:             $memo['body'] = '';
213:         }
214: 
215:         $memo['desc'] = $this->getMemoDescription($memo['body']);
216: 
217:         try {
218:             $cat = $vNote->getAttribute('CATEGORIES');
219:         } catch (Horde_Icalendar_Exception $e) {
220:         }
221:         if (!is_array($cat)) {
222:             $memo['category'] = $cat;
223:         }
224: 
225:         return $memo;
226:     }
227: 
228:     /**
229:      * Retrieves notes from the database.
230:      *
231:      * @thows Mnemo_Exception
232:      */
233:     abstract public function retrieve();
234: }
235: 
API documentation generated by ApiGen