1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: abstract class Mnemo_Driver
15: {
16: 17: 18: 19: 20: 21:
22: protected $_memos = array();
23:
24: 25: 26: 27: 28:
29: protected $_notepad = '';
30:
31: 32: 33: 34: 35:
36: protected $_pgp;
37:
38: 39: 40: 41: 42: 43:
44: public function listMemos()
45: {
46: return $this->_memos;
47: }
48:
49: 50: 51: 52: 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:
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: 79: 80: 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: 95: 96: 97: 98: 99: 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: 109: 110: 111: 112: 113: 114: 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: 129: 130: 131: 132: 133: 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: 147: 148: 149: 150: 151: 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:
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: 196: 197: 198: 199: 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: 230: 231: 232:
233: abstract public function retrieve();
234: }
235: