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
|
<?php
/**
* $Horde: mnemo/lib/Mnemo.php,v 1.52.2.15 2009-01-13 15:47:33 chuck Exp $
*
* Copyright 2001-2009 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.
*
* @package Mnemo
*/
/**
* Sort by memo description.
*/
define('MNEMO_SORT_DESC', 0);
/**
* Sort by memo category.
*/
define('MNEMO_SORT_CATEGORY', 1);
/**
* Sort by notepad.
*/
define('MNEMO_SORT_NOTEPAD', 2);
/**
* Sort in ascending order.
*/
define('MNEMO_SORT_ASCEND', 0);
/**
* Sort in descending order.
*/
define('MNEMO_SORT_DESCEND', 1);
/**
* No passphrase provided.
*/
define('MNEMO_ERR_NO_PASSPHRASE', 100);
/**
* Decrypting failed
*/
define('MNEMO_ERR_DECRYPT', 101);
/**
* Mnemo Base Class.
*
* @author Jon Parise <jon@horde.org>
* @since Mnemo 1.0
* @package Mnemo
*/
class Mnemo {
/**
* Retrieves the current user's note list from storage. This function will
* also sort the resulting list, if requested.
*
* @param constant $sortby The field by which to sort. (MNEMO_SORT_DESC,
* MNEMO_SORT_CATEGORY, MNEMO_SORT_NOTEPAD)
* @param constant $sortdir The direction by which to sort.
* (MNEMO_SORT_ASC, MNEMO_SORT_DESC)
*
* @return array A list of the requested notes.
*
* @see Mnemo_Driver::listMemos()
*/
function listMemos($sortby = MNEMO_SORT_DESC,
$sortdir = MNEMO_SORT_ASCEND)
{
global $conf, $display_notepads;
$memos = array();
/* Sort the memo list. */
$sort_functions = array(
MNEMO_SORT_DESC => 'ByDesc',
MNEMO_SORT_CATEGORY => 'ByCategory',
MNEMO_SORT_NOTEPAD => 'ByNotepad',
);
foreach ($display_notepads as $notepad) {
/* Create a Mnemo storage instance. */
$storage = &Mnemo_Driver::singleton($notepad);
$storage->retrieve();
/* Retrieve the memo list from storage. */
$newmemos = $storage->listMemos();
$memos = array_merge($memos, $newmemos);
}
/* Sort the array if we have a sort function defined for this
* field. */
if (isset($sort_functions[$sortby])) {
$prefix = ($sortdir == MNEMO_SORT_DESCEND) ? '_rsort' : '_sort';
uasort($memos, array('Mnemo', $prefix . $sort_functions[$sortby]));
}
return $memos;
}
/**
* Returns the number of notes in notepads that the current user owns.
*
* @return integer The number of notes that the user owns.
*/
function countMemos()
{
static $count;
if (isset($count)) {
return $count;
}
$notepads = Mnemo::listNotepads(true, PERMS_ALL);
$count = 0;
foreach (array_keys($notepads) as $notepad) {
/* Create a Mnemo storage instance. */
$storage = &Mnemo_Driver::singleton($notepad);
$storage->retrieve();
/* Retrieve the memo list from storage. */
$count += count($storage->listMemos());
}
return $count;
}
/**
* Retrieves a specific note from storage.
*
* @param string $notepad The notepad to retrieve the note from.
* @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 note.
*/
function getMemo($notepad, $noteId, $passphrase = null)
{
$storage = &Mnemo_Driver::singleton($notepad);
return $storage->get($noteId, $passphrase);
}
/**
* Get preview text for a note (the first 20 lines or so).
*
* @param array $note The note array
*
* @return string A few lines of the note for previews or
* tooltips.
*/
function getNotePreview($note)
{
if (is_a($note['body'], 'PEAR_Error')) {
return $note['body']->getMessage();
}
$lines = explode("\n", wordwrap($note['body']));
return implode("\n", array_splice($lines, 0, 20));
}
/**
* Lists all notepads a user has access to.
*
* @param boolean $owneronly Only return memo lists that this user owns?
* Defaults to false.
* @param integer $permission The permission to filter notepads by.
*
* @return array The memo lists.
*/
function listNotepads($owneronly = false, $permission = PERMS_SHOW)
{
// Work around BC break in listShares() parameters (http://bugs.horde.org/ticket/7820)
include_once $GLOBALS['registry']->get('fileroot', 'horde') . '/lib/version.php';
if (version_compare(HORDE_VERSION, '3.2', '<')) {
$notepads = $GLOBALS['mnemo_shares']->listShares(Auth::getAuth(), $permission, $owneronly ? Auth::getAuth() : null, DATATREE_ROOT, true, 0, 0, 'name');
} else {
$notepads = $GLOBALS['mnemo_shares']->listShares(Auth::getAuth(), $permission, $owneronly ? Auth::getAuth() : null, 0, 0, 'name');
}
if (is_a($notepads, 'PEAR_Error')) {
Horde::logMessage($notepads, __FILE__, __LINE__, PEAR_LOG_ERR);
$empty = array();
return $empty;
}
return $notepads;
}
/**
* Returns the default notepad for the current user at the specified
* permissions level.
*/
function getDefaultNotepad($permission = PERMS_SHOW)
{
global $prefs;
$default_notepad = $prefs->getValue('default_notepad');
$notepads = Mnemo::listNotepads(false, $permission);
if (isset($notepads[$default_notepad])) {
return $default_notepad;
} elseif ($prefs->isLocked('default_notepad')) {
return '';
} elseif (count($notepads)) {
return key($notepads);
}
return false;
}
/**
* Returns the real name, if available, of a user.
*
* @since Mnemo 2.2
*/
function getUserName($uid)
{
static $names = array();
if (!isset($names[$uid])) {
require_once 'Horde/Identity.php';
$ident = &Identity::singleton('none', $uid);
$ident->setDefault($ident->getDefault());
$names[$uid] = $ident->getValue('fullname');
if (empty($names[$uid])) {
$names[$uid] = $uid;
}
}
return $names[$uid];
}
/**
* Comparison function for sorting notes by description.
*
* @param array $a Note one.
* @param array $b Note two.
*
* @return integer 1 if memo one is greater, -1 if memo two is greater; 0
* if they are equal.
*/
function _sortByDesc($a, $b)
{
return strcasecmp($a['desc'], $b['desc']);
}
/**
* Comparison function for reverse sorting notes by description.
*
* @param array $a Note one.
* @param array $b Note two.
*
* @return integer -1 if note one is greater, 1 if note two is greater; 0
* if they are equal.
*/
function _rsortByDesc($a, $b)
{
return strcasecmp($b['desc'], $a['desc']);
}
/**
* Comparison function for sorting notes by category.
*
* @param array $a Note one.
* @param array $b Note two.
*
* @return integer 1 if note one is greater, -1 if note two is greater; 0
* if they are equal.
*/
function _sortByCategory($a, $b)
{
return strcasecmp($a['category'] ? $a['category'] : _("Unfiled"),
$b['category'] ? $b['category'] : _("Unfiled"));
}
/**
* Comparison function for reverse sorting notes by category.
*
* @param array $a Note one.
* @param array $b Note two.
*
* @return integer -1 if note one is greater, 1 if note two is greater; 0
* if they are equal.
*/
function _rsortByCategory($a, $b)
{
return strcasecmp($b['category'] ? $b['category'] : _("Unfiled"),
$a['category'] ? $a['category'] : _("Unfiled"));
}
/**
* Comparison function for sorting notes by notepad name.
*
* @param array $a Note one.
* @param array $b Note two.
*
* @return integer 1 if note one is greater, -1 if note two is greater;
* 0 if they are equal.
*/
function _sortByNotepad($a, $b)
{
$aowner = $a['memolist_id'];
$bowner = $b['memolist_id'];
$ashare = $GLOBALS['mnemo_shares']->getShare($aowner);
$bshare = $GLOBALS['mnemo_shares']->getShare($bowner);
if (!is_a($ashare, 'PEAR_Error') && $aowner != $ashare->get('owner')) {
$aowner = $ashare->get('name');
}
if (!is_a($bshare, 'PEAR_Error') && $bowner != $bshare->get('owner')) {
$bowner = $bshare->get('name');
}
return strcasecmp($aowner, $bowner);
}
/**
* Comparison function for reverse sorting notes by notepad name.
*
* @param array $a Note one.
* @param array $b Note two.
*
* @return integer -1 if note one is greater, 1 if note two is greater;
* 0 if they are equal.
*/
function _rsortByNotepad($a, $b)
{
$aowner = $a['memolist_id'];
$bowner = $b['memolist_id'];
$ashare = $GLOBALS['mnemo_shares']->getShare($aowner);
$bshare = $GLOBALS['mnemo_shares']->getShare($bowner);
if (!is_a($ashare, 'PEAR_Error') && $aowner != $ashare->get('owner')) {
$aowner = $ashare->get('name');
}
if (!is_a($bshare, 'PEAR_Error') && $bowner != $bshare->get('owner')) {
$bowner = $bshare->get('name');
}
return strcasecmp($bowner, $aowner);
}
/**
* Returns the specified permission for the current user.
*
* @since Mnemo 2.1
*
* @param string $permission A permission, currently only 'max_notes'.
*
* @return mixed The value of the specified permission.
*/
function hasPermission($permission)
{
global $perms;
if (!$perms->exists('mnemo:' . $permission)) {
return true;
}
$allowed = $perms->getPermissions('mnemo:' . $permission);
if (is_array($allowed)) {
switch ($permission) {
case 'max_notes':
$allowed = max($allowed);
break;
}
}
return $allowed;
}
/**
* Returns a note's passphrase for symmetric encryption from the session
* cache.
*
* @param string $id A note id.
*
* @return string The passphrase, if set.
*/
function getPassphrase($id)
{
if (isset($_SESSION['mnemo'][$id]['passphrase'])) {
return Secret::read(Secret::getKey('mnemo'), $_SESSION['mnemo'][$id]['passphrase']);
}
}
/**
* Stores a note's passphrase for symmetric encryption in the session
* cache.
*
* @param string $id A note id.
* @param string $passphrase The note's passphrase.
*
* @return boolean True
*/
function storePassphrase($id, $passphrase)
{
$_SESSION['mnemo'][$id]['passphrase'] = Secret::write(Secret::getKey('mnemo'), $passphrase);
}
/**
* Initial app setup code.
*/
function initialize()
{
// Update the preference for which notepads to display. If the
// user doesn't have any selected notepads for view then fall
// back to some available notepad.
$GLOBALS['display_notepads'] = unserialize($GLOBALS['prefs']->getValue('display_notepads'));
if (($notepadId = Util::getFormData('display_notepad')) !== null) {
if (is_array($notepadId)) {
$GLOBALS['display_notepads'] = $notepadId;
} else {
if (in_array($notepadId, $GLOBALS['display_notepads'])) {
$key = array_search($notepadId, $GLOBALS['display_notepads']);
unset($GLOBALS['display_notepads'][$key]);
} else {
$GLOBALS['display_notepads'][] = $notepadId;
}
}
}
// Make sure all notepads exist now, to save on checking later.
$_temp = $GLOBALS['display_notepads'];
$_all = Mnemo::listNotepads();
$GLOBALS['display_notepads'] = array();
foreach ($_temp as $id) {
if (isset($_all[$id])) {
$GLOBALS['display_notepads'][] = $id;
}
}
if (count($GLOBALS['display_notepads']) == 0) {
$lists = Mnemo::listNotepads(true);
if (!Auth::getAuth()) {
/* All notepads for guests. */
$GLOBALS['display_notepads'] = array_keys($lists);
} else {
/* Make sure at least the default notepad is visible. */
$default_notepad = Mnemo::getDefaultNotepad(PERMS_READ);
if ($default_notepad) {
$GLOBALS['display_notepads'] = array($default_notepad);
}
/* If the user's personal notepad doesn't exist, then create it. */
if (!$GLOBALS['mnemo_shares']->exists(Auth::getAuth())) {
require_once 'Horde/Identity.php';
$identity = &Identity::singleton();
$name = $identity->getValue('fullname');
if (trim($name) == '') {
$name = Auth::removeHook(Auth::getAuth());
}
$share = &$GLOBALS['mnemo_shares']->newShare(Auth::getAuth());
$share->set('name', sprintf(_("%s's Notepad"), $name));
$GLOBALS['mnemo_shares']->addShare($share);
/* Make sure the personal notepad is displayed by default. */
if (!in_array(Auth::getAuth(), $GLOBALS['display_notepads'])) {
$GLOBALS['display_notepads'][] = Auth::getAuth();
}
}
}
}
$GLOBALS['prefs']->setValue('display_notepads', serialize($GLOBALS['display_notepads']));
}
/**
* Builds Mnemo's list of menu items.
*/
function getMenu($returnType = 'object')
{
global $conf, $registry, $print_link;
require_once 'Horde/Menu.php';
$menu = new Menu();
$menu->add(Horde::applicationUrl('list.php'), _("_List Notes"), 'mnemo.png', null, null, null, basename($_SERVER['PHP_SELF']) == 'index.php' ? 'current' : null);
if (Mnemo::getDefaultNotepad(PERMS_EDIT) &&
(!empty($conf['hooks']['permsdenied']) ||
Mnemo::hasPermission('max_notes') === true ||
Mnemo::hasPermission('max_notes') > Mnemo::countMemos())) {
$menu->add(Horde::applicationUrl('memo.php?actionID=add_memo'), _("_New Note"), 'add.png', null, null, null, Util::getFormData('memo') ? '__noselection' : null);
}
/* Search. */
$menu->add(Horde::applicationUrl('search.php'), _("_Search"), 'search.png', $registry->getImageDir('horde'));
/* Import/Export */
if ($conf['menu']['import_export']) {
$menu->add(Horde::applicationUrl('data.php'), _("_Import/Export"), 'data.png', $registry->getImageDir('horde'));
}
/* Print */
if ($conf['menu']['print'] && isset($print_link)) {
$menu->add($print_link, _("_Print"), 'print.png', $registry->getImageDir('horde'), '_blank', 'popup(this.href); return false;');
}
if ($returnType == 'object') {
return $menu;
} else {
return $menu->render();
}
}
}
|