1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: 11: 12: 13: 14: 15:
16: class Mnemo
17: {
18: 19: 20:
21: const SORT_DESC = 0;
22:
23: 24: 25:
26: const SORT_CATEGORY = 1;
27:
28: 29: 30:
31: const SORT_NOTEPAD = 2;
32:
33: 34: 35:
36: const SORT_MOD_DATE = 3;
37:
38: 39: 40:
41: const SORT_ASCEND = 0;
42:
43: 44: 45:
46: const SORT_DESCEND = 1;
47:
48: 49: 50:
51: const ERR_NO_PASSPHRASE = 100;
52:
53: 54: 55:
56: const ERR_DECRYPT = 101;
57:
58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: public static function listMemos($sortby = self::SORT_DESC,
73: $sortdir = self::SORT_ASCEND)
74: {
75: global $conf, $display_notepads;
76: $memos = array();
77:
78:
79: $sort_functions = array(
80: self::SORT_DESC => 'ByDesc',
81: self::SORT_CATEGORY => 'ByCategory',
82: self::SORT_NOTEPAD => 'ByNotepad',
83: self::SORT_MOD_DATE => 'ByModDate'
84: );
85:
86: foreach ($display_notepads as $notepad) {
87: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create($notepad);
88: try {
89: $storage->retrieve();
90: } catch (Mnemo_Exception $e) {
91: $GLOBALS['notification']->push($e, 'horde.error');
92: }
93: $newmemos = $storage->listMemos();
94: $memos = array_merge($memos, $newmemos);
95: }
96:
97:
98: if (isset($sort_functions[$sortby])) {
99: $prefix = ($sortdir == self::SORT_DESCEND) ? '_rsort' : '_sort';
100: uasort($memos, array('Mnemo', $prefix . $sort_functions[$sortby]));
101: }
102:
103: return $memos;
104: }
105:
106: 107: 108: 109: 110:
111: public static function countMemos()
112: {
113: static $count;
114: if (isset($count)) {
115: return $count;
116: }
117:
118: $notepads = self::listNotepads(true, Horde_Perms::ALL);
119: $count = 0;
120: foreach (array_keys($notepads) as $notepad) {
121: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create($notepad);
122: $storage->retrieve();
123: $count += count($storage->listMemos());
124: }
125:
126: return $count;
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136: 137: 138:
139: public static function getMemo($notepad, $noteId, $passphrase = null)
140: {
141: $storage = $GLOBALS['injector']->getInstance('Mnemo_Factory_Driver')->create($notepad);
142: return $storage->get($noteId, $passphrase);
143: }
144:
145: 146: 147: 148: 149: 150: 151:
152: public static function getNotePreview($note)
153: {
154: $body = $note['body'];
155: if ($body instanceof Mnemo_Exception) {
156: $body = $body->getMessage();
157: }
158: $lines = explode("\n", wordwrap($body));
159: return implode("\n", array_splice($lines, 0, 20));
160: }
161:
162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176:
177: public static function listNotepads($owneronly = false,
178: $permission = Horde_Perms::SHOW)
179: {
180: if ($owneronly && !$GLOBALS['registry']->getAuth()) {
181: return array();
182: }
183: if ($owneronly || empty($GLOBALS['conf']['share']['hidden'])) {
184: try {
185: $notepads = $GLOBALS['mnemo_shares']->listShares(
186: $GLOBALS['registry']->getAuth(),
187: array('perm' => $permission,
188: 'attributes' => $owneronly ? $GLOBALS['registry']->getAuth() : null,
189: 'sort_by' => 'name'));
190: } catch (Horde_Share_Exception $e) {
191: Horde::logMessage($e->getMessage(), 'ERR');
192: return array();
193: }
194: } else {
195: try {
196: $notepads = $GLOBALS['mnemo_shares']->listShares(
197: $GLOBALS['registry']->getAuth(),
198: array('perm' => $permission,
199: 'attributes' => $GLOBALS['registry']->getAuth(),
200: 'sort_by' => 'name'));
201: } catch (Horde_Share_Exception $e) {
202: Horde::logMessage($e);
203: return array();
204: }
205: $display_notepads = @unserialize($GLOBALS['prefs']->getValue('display_notepads'));
206: if (is_array($display_notepads)) {
207: foreach ($display_notepads as $id) {
208: try {
209: $notepad = $GLOBALS['mnemo_shares']->getShare($id);
210: if ($notepad->hasPermission($GLOBALS['registry']->getAuth(), $permission)) {
211: $notepads[$id] = $notepad;
212: }
213: } catch (Horde_Exception_NotFound $e) {
214: } catch (Horde_Share_Exception $e) {
215: Horde::logMessage($e);
216: return array();
217: }
218: }
219: }
220: }
221:
222: return $notepads;
223: }
224:
225: 226: 227: 228: 229: 230:
231: public static function getDefaultNotepad($permission = Horde_Perms::SHOW)
232: {
233: global $prefs;
234:
235: $default_notepad = $prefs->getValue('default_notepad');
236: $notepads = self::listNotepads(false, $permission);
237:
238: if (isset($notepads[$default_notepad])) {
239: return $default_notepad;
240: } elseif ($prefs->isLocked('default_notepad')) {
241: return $GLOBALS['registry']->getAuth();
242: } elseif (count($notepads)) {
243: reset($notepads);
244: return key($notepads);
245: }
246:
247: return false;
248: }
249:
250: 251: 252: 253: 254:
255: public static function getUserName($uid)
256: {
257: static $names = array();
258:
259: if (!isset($names[$uid])) {
260: $ident = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Identity')->create($uid);
261: $ident->setDefault($ident->getDefault());
262: $names[$uid] = $ident->getValue('fullname');
263: if (empty($names[$uid])) {
264: $names[$uid] = $uid;
265: }
266: }
267:
268: return $names[$uid];
269: }
270:
271: 272: 273: 274: 275: 276: 277: 278: 279:
280: protected static function _sortByDesc($a, $b)
281: {
282: return strcoll($a['desc'], $b['desc']);
283: }
284:
285: 286: 287: 288: 289: 290: 291: 292: 293:
294: protected static function _rsortByDesc($a, $b)
295: {
296: return strcoll($b['desc'], $a['desc']);
297: }
298:
299: 300: 301: 302: 303: 304: 305: 306: 307:
308: protected static function _sortByCategory($a, $b)
309: {
310: return strcoll($a['category'] ? $a['category'] : _("Unfiled"),
311: $b['category'] ? $b['category'] : _("Unfiled"));
312: }
313:
314: 315: 316: 317: 318: 319: 320: 321: 322:
323: protected static function _rsortByCategory($a, $b)
324: {
325: return strcoll($b['category'] ? $b['category'] : _("Unfiled"),
326: $a['category'] ? $a['category'] : _("Unfiled"));
327: }
328:
329: 330: 331: 332: 333: 334: 335: 336: 337:
338: protected static function _sortByNotepad($a, $b)
339: {
340: $aowner = $a['memolist_id'];
341: $bowner = $b['memolist_id'];
342:
343: $ashare = $GLOBALS['mnemo_shares']->getShare($aowner);
344: $bshare = $GLOBALS['mnemo_shares']->getShare($bowner);
345:
346: if ($aowner != $ashare->get('owner')) {
347: $aowner = $ashare->get('name');
348: }
349: if ($bowner != $bshare->get('owner')) {
350: $bowner = $bshare->get('name');
351: }
352:
353: return strcoll($aowner, $bowner);
354: }
355:
356: 357: 358: 359: 360: 361: 362: 363: 364:
365: protected static function _rsortByNotepad($a, $b)
366: {
367: $aowner = $a['memolist_id'];
368: $bowner = $b['memolist_id'];
369:
370: $ashare = $GLOBALS['mnemo_shares']->getShare($aowner);
371: $bshare = $GLOBALS['mnemo_shares']->getShare($bowner);
372:
373: if ($aowner != $ashare->get('owner')) {
374: $aowner = $ashare->get('name');
375: }
376: if ($bowner != $bshare->get('owner')) {
377: $bowner = $bshare->get('name');
378: }
379:
380: return strcoll($bowner, $aowner);
381: }
382:
383: 384: 385: 386: 387: 388: 389: 390: 391:
392: protected static function _sortByModDate($a, $b)
393: {
394:
395: $history = $GLOBALS['injector']->getInstance('Horde_History');
396:
397: $guidA = 'mnemo:' . $a['memolist_id'] . ':' . $a['uid'];
398: $guidB = 'mnemo:' . $b['memolist_id'] . ':' . $b['uid'];
399:
400:
401: $modDateA = $history->getActionTimestamp($guidA, 'modify');
402: $modDateB = $history->getActionTimestamp($guidB, 'modify');
403:
404:
405: if ($modDateA == 0) {
406: $modDateA = $history->getActionTimestamp($guidA, 'add');
407: }
408: if ($modDateB == 0) {
409: $modDateB = $history->getActionTimestamp($guidB, 'add');
410: }
411: if ($modDateA == $modDateB) {
412: return 0;
413: }
414:
415: return ($modDateA > $modDateB) ? 1 : -1;
416: }
417:
418: 419: 420: 421: 422: 423: 424: 425: 426:
427: protected static function _rsortByModDate($a, $b)
428: {
429:
430: $history = $GLOBALS['injector']->getInstance('Horde_History');
431:
432: $guidA = 'mnemo:' . $a['memolist_id'] . ':' . $a['uid'];
433: $guidB = 'mnemo:' . $b['memolist_id'] . ':' . $b['uid'];
434:
435:
436: $modDateA = $history->getActionTimestamp($guidA, 'modify');
437: $modDateB = $history->getActionTimestamp($guidB, 'modify');
438:
439:
440: if ($modDateA == 0) {
441: $modDateA = $history->getActionTimestamp($guidA, 'add');
442: }
443: if ($modDateB == 0) {
444: $modDateB = $history->getActionTimestamp($guidB, 'add');
445: }
446:
447: if ($modDateA == $modDateB) {
448: return 0;
449: }
450:
451: return ($modDateA < $modDateB) ? 1 : -1;
452: }
453:
454: 455: 456: 457: 458: 459: 460:
461: public static function hasPermission($permission)
462: {
463: global $perms;
464:
465: if (!$perms->exists('mnemo:' . $permission)) {
466: return true;
467: }
468:
469: $allowed = $perms->getPermissions('mnemo:' . $permission, $GLOBALS['registry']->getAuth());
470: if (is_array($allowed)) {
471: switch ($permission) {
472: case 'max_notes':
473: $allowed = max($allowed);
474: break;
475: }
476: }
477:
478: return $allowed;
479: }
480:
481: 482: 483: 484: 485: 486: 487: 488:
489: public static function getPassphrase($id)
490: {
491: if (!$id) {
492: return;
493: }
494: if ($passphrase = $GLOBALS['session']->get('mnemo', 'passphrase/' . $id)) {
495: $secret = $GLOBALS['injector']->getInstance('Horde_Secret');
496: return $secret->read($secret->getKey('mnemo'), $passphrase);
497: }
498: }
499:
500: 501: 502: 503: 504: 505: 506: 507: 508:
509: public static function storePassphrase($id, $passphrase)
510: {
511: $secret = $GLOBALS['injector']->getInstance('Horde_Secret');
512: $GLOBALS['session']->set('mnemo', 'passphrase/' . $id, $secret->write($secret->getKey('mnemo'), $passphrase));
513: }
514:
515: 516: 517: 518: 519: 520: 521:
522: public static function initialize()
523: {
524: $GLOBALS['mnemo_shares'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
525:
526:
527:
528:
529: $GLOBALS['display_notepads'] = unserialize($GLOBALS['prefs']->getValue('display_notepads'));
530: if (($notepadId = Horde_Util::getFormData('display_notepad')) !== null) {
531: if (is_array($notepadId)) {
532: $GLOBALS['display_notepads'] = $notepadId;
533: } else {
534: if (in_array($notepadId, $GLOBALS['display_notepads'])) {
535: $key = array_search($notepadId, $GLOBALS['display_notepads']);
536: unset($GLOBALS['display_notepads'][$key]);
537: } else {
538: $GLOBALS['display_notepads'][] = $notepadId;
539: }
540: }
541: }
542:
543:
544: $_temp = ($GLOBALS['display_notepads']) ? $GLOBALS['display_notepads'] : array();
545:
546: $_all = self::listNotepads();
547: $GLOBALS['display_notepads'] = array();
548: foreach ($_temp as $id) {
549: if (isset($_all[$id])) {
550: $GLOBALS['display_notepads'][] = $id;
551: }
552: }
553:
554:
555: if (!count($GLOBALS['display_notepads']) &&
556: !$GLOBALS['registry']->getAuth()) {
557: $GLOBALS['display_notepads'] = array_keys($_all);
558: }
559:
560:
561: $notepads = $GLOBALS['injector']->getInstance('Mnemo_Factory_Notepads')
562: ->create();
563: if (($new_default = $notepads->ensureDefaultShare()) !== null) {
564: $GLOBALS['display_notepads'][] = $new_default;
565: }
566:
567: $GLOBALS['prefs']->setValue('display_notepads', serialize($GLOBALS['display_notepads']));
568: }
569:
570: 571:
572: static public function getCssStyle($category, $stickies = false)
573: {
574: $cManager = new Horde_Prefs_CategoryManager();
575: $colors = $cManager->colors();
576: if (!isset($colors[$category])) {
577: return '';
578: }
579: $fgColors = $cManager->fgColors();
580:
581: if (!$stickies) {
582: return 'color:' . (isset($fgColors[$category]) ? $fgColors[$category] : $fgColors['_default_']) . ';' .
583: 'background:' . $colors[$category] . ';';
584: }
585:
586: $hex = str_replace('#', '', $colors[$category]);
587: if (strlen($hex) == 3) {
588: $r = hexdec(substr($hex, 0, 1));
589: $g = hexdec(substr($hex, 1, 1));
590: $b = hexdec(substr($hex, 2, 1));
591: } else {
592: $r = hexdec(substr($hex, 0, 2));
593: $g = hexdec(substr($hex, 2, 2));
594: $b = hexdec(substr($hex, 4, 2));
595: }
596:
597: return "background: rgba($r, $g, $b, 0.5)";
598: }
599:
600: }
601: