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 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
|
<?php
/**
* Preference is administratively locked.
*/
define('_PREF_LOCKED', 1);
/**
* Preference is shared amongst applications.
*/
define('_PREF_SHARED', 2);
/**
* Preference value has been changed.
*/
define('_PREF_DIRTY', 4);
/**
* Preference value is the application default.
*/
define('_PREF_DEFAULT', 8);
/**
* The Prefs:: class provides a common abstracted interface into the
* various preferences storage mediums. It also includes all of the
* functions for retrieving, storing, and checking preference values.
*
* TODO: document the format of the $_prefs hash here
*
* $_prefs[*pref name*] = array(
* 'value' => *Default value*,
* 'locked' => *boolean*,
* 'shared' => *boolean*,
* 'type' => 'checkbox'
* 'text'
* 'password'
* 'textarea'
* 'select'
* 'number'
* 'implicit'
* 'special'
* 'link' - There must be a field named either 'url'
* (internal application link) or 'xurl'
* (external application link) if this type is used.
* 'enum'
* 'enum' => TODO,
* 'desc' => _(*Description string*),
* 'help' => *Name of the entry in the XML help file*
* );
*
* $Horde: framework/Prefs/Prefs.php,v 1.137.10.27 2006/07/03 17:38:11 chuck Exp $
*
* Copyright 1999-2006 Jon Parise <jon@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Jon Parise <jon@horde.org>
* @since Horde 1.3
* @package Horde_Prefs
*/
class Prefs {
/**
* Hash holding all of the user's preferences. Each preference is
* itself a hash, so this will ultimately be multi-dimensional.
*
* [*pref name*] => Array(
* [d] => *default value*
* [m] => *pref mask*
* [v] => *pref value*
* )
*
* @access private
*
* @var array
*/
var $_prefs = array();
/**
* String containing the name of this scope. This is used to
* maintain the application scope between sets of preferences. By
* default, all preferences belong to the "global" (Horde) scope.
*
* @var string
*/
var $_scope = 'horde';
/**
* String containing the current username. This indicates the owner of the
* preferences.
*
* @var string
*/
var $_user = '';
/**
* Boolean indicating whether preference caching should be used.
*
* @var boolean
*/
var $_caching = false;
/**
* Hash holding preferences with hook functions defined.
*
* @var array
*/
var $_hooks = array();
/**
* Have we run hook functions yet?
*
* @var boolean
*/
var $_hooksCalled = null;
/**
* Default constructor (must be called from each extending class in their
* constructors via parent::Prefs()).
*/
function Prefs()
{
$this->_shutdown();
}
/**
* Returns the charset used by the concrete preference backend.
*
* @return string The preference backend's charset.
*/
function getCharset()
{
return NLS::getCharset();
}
/**
* Updates the session-based preferences cache (if available).
*
* @param string $pref The preference to update. If empty, will update
* the entire cache with the current set of prefs.
*/
function cacheUpdate($pref = null)
{
/* Return immediately if caching is disabled. */
if (!$this->_caching) {
return;
}
if (isset($_SESSION['prefs_cache'])) {
if (is_null($pref)) {
$prefs = $this->_prefs;
} else {
$prefs = array($pref => $this->_prefs[$pref]);
}
/* Place each preference in the cache according to its
* scope. */
foreach ($prefs as $name => $pref) {
$_SESSION['prefs_cache'][$this->getScope($name)][$name] = $pref;
}
}
}
/**
* Tries to find the requested preferences in the cache. If they exist,
* update the $prefs hash with the cached values.
*
* @return boolean True on success, false on failure.
*/
function cacheLookup()
{
/* Return immediately if caching is disabled. */
if (!$this->_caching) {
return false;
}
if (isset($_SESSION['prefs_cache']['horde']) &&
isset($_SESSION['prefs_cache'][$this->_scope])) {
/* Restore global preferences. */
$this->_prefs = array_merge($this->_prefs, $_SESSION['prefs_cache']['horde'], $_SESSION['prefs_cache'][$this->_scope]);
return true;
}
return false;
}
/**
* Adds a new preference entry to the $prefs hash.
*
* @param string $pref The name of the preference to add.
* @param string $val The initial value of the preference.
* @param integer $mask The initial bitmask of the preference.
*/
function add($pref, $val = '', $mask = 0)
{
if (is_array($this->_prefs)) {
$this->_prefs[$pref] = array('v' => $val, 'm' => $mask, 'd' => $val);
}
}
/**
* Removes a preference entry from the $prefs hash.
*
* @param string $pref The name of the preference to remove.
*/
function remove($pref)
{
if (is_array($this->_prefs)) {
$scope = $this->getScope($pref);
unset($this->_prefs[$pref]);
unset($_SESSION['prefs_cache'][$scope][$pref]);
}
}
/**
* Sets the given preferences ($pref) to the specified value
* ($val), if the preference is modifiable.
*
* @param string $pref The name of the preference to modify.
* @param string $val The new value for this preference.
* @param boolean $convert If true the preference value gets converted
* from the current charset to the backend's
* charset.
*
* @return boolean True if the value was successfully set, false on a
* failure.
*/
function setValue($pref, $val, $convert = true)
{
/* Exit early if this preference is locked or doesn't exist. */
if (!isset($this->_prefs[$pref]) || $this->isLocked($pref)) {
return false;
}
return $this->_setValue($pref, $val, true, $convert);
}
function __set($name, $value)
{
return $this->setValue($name, $value);
}
/**
* Sets the given preferences ($pref) to the specified value
* ($val), whether or not the preference is user-modifiable, unset
* the default bit, and set the dirty bit.
*
* @access protected
*
* @param string $pref The name of the preference to modify.
* @param string $val The new value for this preference.
* @param boolean $dirty True if we should mark the new value as
* dirty (changed).
* @param boolean $convert If true the preference value gets converted
* from the current charset to the backend's
* charset.
*
* @return boolean True if the value was successfully set, false on a
* failure.
*/
function _setValue($pref, $val, $dirty = true, $convert = true)
{
global $conf;
if ($convert) {
$val = $this->convertToDriver($val, NLS::getCharset());
}
/* If the preference's value is already equal to $val, don't
* bother changing it. Changing it would set the "dirty" bit,
* causing an unnecessary update later on in the storage
* routine. */
if (isset($this->_prefs[$pref]) &&
(($this->_prefs[$pref]['v'] == $val) &&
!$this->isDefault($pref))) {
return true;
}
/* Check to see if the value exceeds the allowable storage
* limit. */
if (isset($conf['prefs']['maxsize'])) {
if (strlen($val) > $conf['prefs']['maxsize']) {
global $notification;
if (isset($notification)) {
$notification->push(sprintf(_("The preference \"%s\" could not be saved because its data exceeded the maximum allowable size"), $pref), 'horde.error');
return false;
}
}
}
/* Assign the new value, unset the "default" bit, and set the
"dirty" bit. */
if (empty($this->_prefs[$pref]['m'])) {
$this->_prefs[$pref]['m'] = 0;
}
$this->_prefs[$pref]['v'] = $val;
$this->setDefault($pref, false);
if ($dirty) {
$this->setDirty($pref, true);
}
$this->cacheUpdate($pref);
return true;
}
/**
* Returns the value of the requested preference.
*
* @param string $pref The name of the preference to retrieve.
* @param boolean $convert If true the preference value gets converted
* from the backend's charset to the current
* charset.
*
* @return string The value of the preference, null if it doesn't exist.
*/
function getValue($pref, $convert = true)
{
static $charset;
if (!isset($charset)) {
$charset = NLS::getCharset();
}
return (isset($this->_prefs[$pref]['v'])) ?
($convert ?
$this->convertFromDriver($this->_prefs[$pref]['v'], $charset) :
$this->_prefs[$pref]['v']) :
null;
}
function __get($name)
{
return $this->getValue($name);
}
/**
* Modifies the "locked" bit for the given preference.
*
* @param string $pref The name of the preference to modify.
* @param boolean $bool The new boolean value for the "locked" bit.
*/
function setLocked($pref, $bool)
{
$this->_setMask($pref, $bool, _PREF_LOCKED);
}
/**
* Returns the state of the "locked" bit for the given preference.
*
* @param string $pref The name of the preference to check.
*
* @return boolean The boolean state of $pref's "locked" bit.
*/
function isLocked($pref)
{
return $this->_getMask($pref, _PREF_LOCKED);
}
/**
* Modifies the "shared" bit for the given preference.
*
* @param string $pref The name of the preference to modify.
* @param boolean $bool The new boolean value for the "shared" bit.
*/
function setShared($pref, $bool)
{
$this->_setMask($pref, $bool, _PREF_SHARED);
}
/**
* Returns the state of the "shared" bit for the given preference.
*
* @param string $pref The name of the preference to check.
*
* @return boolean The boolean state of $pref's "shared" bit.
*/
function isShared($pref)
{
return $this->_getMask($pref, _PREF_SHARED);
}
/**
* Modifies the "dirty" bit for the given preference.
*
* @param string $pref The name of the preference to modify.
* @param boolean $bool The new boolean value for the "dirty" bit.
*/
function setDirty($pref, $bool)
{
$this->_setMask($pref, $bool, _PREF_DIRTY);
}
/**
* Returns the state of the "dirty" bit for the given preference.
*
* @param string $pref The name of the preference to check.
*
* @return boolean The boolean state of $pref's "dirty" bit.
*/
function isDirty($pref)
{
return $this->_getMask($pref, _PREF_DIRTY);
}
/**
* Modifies the "default" bit for the given preference.
*
* @param string $pref The name of the preference to modify.
* @param boolean $bool The new boolean value for the "default" bit.
*/
function setDefault($pref, $bool)
{
$this->_setMask($pref, $bool, _PREF_DEFAULT);
}
/**
* Returns the default value of the given preference.
*
* @param string $pref The name of the preference to get the default for.
*
* @return string The preference's default value.
*/
function getDefault($pref)
{
return !empty($this->_prefs[$pref]['d']) ?
$this->_prefs[$pref]['d'] :
'';
}
/**
* Determines if the current preference value is the default
* value from prefs.php or a user defined value
*
* @param string $pref The name of the preference to check.
*
* @return boolean True if the preference is the application default
* value.
*/
function isDefault($pref)
{
return $this->_getMask($pref, _PREF_DEFAULT);
}
/**
* Sets the value for a given mask.
*
* @access private
*
* @param string $pref The name of the preference to modify.
* @param boolean $bool The new boolean value for the "default" bit.
* @param integer $mask The mask to add.
*/
function _setMask($pref, $bool, $mask)
{
if (isset($this->_prefs[$pref])) {
if ($bool != $this->_getMask($pref, $mask)) {
if ($bool) {
$this->_prefs[$pref]['m'] |= $mask;
} else {
$this->_prefs[$pref]['m'] &= ~$mask;
}
}
}
}
/**
* Gets the boolean state for a given mask.
*
* @access private
*
* @param string $pref The name of the preference to modify.
* @param integer $mask The mask to get.
*
* @return boolean The boolean state for the given mask.
*/
function _getMask($pref, $mask)
{
return isset($this->_prefs[$pref]['m']) ? ($this->_prefs[$pref]['m'] & $mask) : false;
}
/**
* Determines whether the current preference is empty.
*
* @param string $pref The name of the preference to check.
*
* @return boolean True if the preference is empty.
*/
function isEmpty($pref)
{
return empty($this->_prefs[$pref]['v']);
}
/**
* Returns the scope of the given preference.
*
* @param string $pref The name of the preference to examine.
*
* @return string The scope of the $pref.
*/
function getScope($pref)
{
if ($this->isShared($pref)) {
return 'horde';
} else {
return $this->_scope;
}
}
/**
* Return a list of "dirty" preferences.
*
* @access private
*
* @return array The list of "dirty" preferences in $this->_prefs.
*/
function _dirtyPrefs()
{
$dirty_prefs = array();
foreach (array_keys($this->_prefs) as $pref) {
if ($this->isDirty($pref)) {
$dirty_prefs[] = $pref;
}
}
return $dirty_prefs;
}
/**
* Retrieves the default preferences.
*/
function retrieve()
{
/* Load defaults to make sure we have all preferences. */
$this->_setDefaults('horde');
$this->_setDefaults($this->_scope);
return true;
}
/**
* This function will be run at the end of every request as a shutdown
* function (registered by the Prefs:: constructor). All prefs with the
* dirty bit set will be saved to the storage backend at this time; thus,
* there is no need to manually call $prefs->store() every time a
* preference is changed.
*
* @abstract
*/
function store()
{
return true;
}
/**
* This function provides common cleanup functions for all of the driver
* implementations.
*
* @param boolean $all Clean up all Horde preferences.
*/
function cleanup($all = false)
{
/* Remove this scope from the preferences cache, if it
exists. */
if (isset($_SESSION['prefs_cache'][$this->_scope])) {
unset($_SESSION['prefs_cache'][$this->_scope]);
}
/* Perform a Horde-wide cleanup? */
if ($all) {
/* Destroy the contents of the preferences hash. */
$this->_prefs = array();
/* Destroy the contents of the preferences cache. */
if (isset($_SESSION['prefs_cache'])) {
unset($_SESSION['prefs_cache']);
}
}
}
/**
* Clears all preferences from the backend.
*/
function clear()
{
$this->cleanup(true);
}
/**
* Converts a value from the driver's charset to the specified charset.
*
* @param mixed $value A value to convert.
* @param string $charset The charset to convert to.
*
* @return mixed The converted value.
*/
function convertFromDriver($value, $charset)
{
return $value;
}
/**
* Converts a value from the specified charset to the driver's charset.
*
* @param mixed $value A value to convert.
* @param string $charset The charset to convert from.
*
* @return mixed The converted value.
*/
function convertToDriver($value, $charset)
{
return $value;
}
/**
* Populates the $prefs hash with new entries and externally defined
* default values.
*
* @param string $app The application to load defaults for.
*/
function _setDefaults($app)
{
global $registry;
$filename = $registry->get('fileroot', $app) . '/config/prefs.php';
/* Ensure that the defaults from this file are only read once.
Also, make sure we can read this file. */
if (!@is_readable($filename)) {
return;
}
/* Read the configuration file. The $_prefs array, which will be
in local scope, is assumed to hold the default values. */
include $filename;
foreach ($_prefs as $pref => $pvals) {
if (isset($pvals['value']) &&
isset($pvals['locked']) &&
isset($pvals['shared']) &&
($pvals['type'] != 'link') &&
($pvals['type'] != 'special')) {
$pref = str_replace('.', '_', $pref);
$mask = 0;
if ($pvals['locked']) {
$mask |= _PREF_LOCKED;
}
if ($pvals['shared'] || $app == 'horde') {
$mask |= _PREF_SHARED;
}
$mask &= ~_PREF_DIRTY;
$mask |= _PREF_DEFAULT;
$this->add($pref, $pvals['value'], $mask);
if (!empty($pvals['hook'])) {
$this->_setHook($pref);
}
}
}
/* Update the preferences cache with the defaults. */
$this->cacheUpdate();
}
/**
* Performs shutdown activities.
*
* @access private
*/
function _shutdown()
{
register_shutdown_function(array(&$this, 'store'));
}
/**
* Add $pref to the list of preferences with hook functions.
*
* @param string $pref The preference with a hook.
*/
function _setHook($pref)
{
$this->_hooks[] = $pref;
}
/**
* After preferences have been loaded, set any locked or empty
* preferences that have hooks to the result of the hook.
*/
function _callHooks()
{
if (!is_null($this->_hooksCalled)) {
return;
}
$this->_hooksCalled = true;
if (!count($this->_hooks)) {
return;
}
global $registry;
include_once $registry->get('fileroot', 'horde') . '/config/hooks.php';
foreach ($this->_hooks as $pref) {
if ($this->isLocked($pref) ||
!$this->getValue($pref) ||
$this->isDefault($pref)) {
$func = '_prefs_hook_' . $pref;
if (function_exists($func)) {
$this->_setValue($pref, $func($this->_user));
}
}
}
}
/**
* Attempts to return a concrete Prefs instance based on $driver.
*
* @param mixed $driver The type of concrete Prefs subclass to return.
* If $driver is an array, then we will look in
* $driver[0]/lib/Prefs/ for the subclass
* implementation named $driver[1].php.
* @param string $scope The scope for this set of preferences.
* @param string $user The name of the user who owns this set of
* preferences.
* @param string $password The password associated with $user.
* @param array $params A hash containing any additional configuration
* or connection parameters a subclass might need.
* @param boolean $caching Should caching be used?
*
* @return Prefs The newly created concrete Prefs instance, or false on
* error.
*/
function &factory($driver, $scope = 'horde', $user = '', $password = '',
$params = null, $caching = true)
{
if (is_array($driver)) {
$app = $driver[0];
$driver = $driver[1];
}
/* Attempt to register (cache) the $prefs hash in session storage. */
if ($caching) {
if (!isset($_SESSION['prefs_cache'])) {
$_SESSION['prefs_cache'] = array();
}
}
$driver = basename($driver);
if (empty($driver) || $driver == 'none') {
$driver = 'session';
}
if (is_null($params)) {
$params = Horde::getDriverConfig('prefs', $driver);
}
/* If $params['user_hook'] is defined, use it to retrieve the value to
* use for the username ($this->_user). Otherwise, just use the value
* passed in the $user parameter. */
if (!empty($params['user_hook']) &&
function_exists($params['user_hook'])) {
$user = call_user_func($params['user_hook'], $user);
}
if (!empty($app)) {
require_once $GLOBALS['registry']->get('fileroot', $app) . '/lib/Prefs/' . $driver . '.php';
} elseif (file_exists(dirname(__FILE__) . '/Prefs/' . $driver . '.php')) {
require_once dirname(__FILE__) . '/Prefs/' . $driver . '.php';
} else {
include_once 'Horde/Prefs/' . $driver . '.php';
}
$class = 'Prefs_' . $driver;
if (class_exists($class)) {
$prefs = &new $class($user, $password, $scope, $params, $caching);
} else {
$prefs = PEAR::raiseError('Class definition of ' . $class . ' not found.');
}
return $prefs;
}
/**
* Attempts to return a reference to a concrete Prefs instance based on
* $driver. It will only create a new instance if no Prefs instance
* with the same parameters currently exists.
*
* This should be used if multiple preference sources (and, thus,
* multiple Prefs instances) are required.
*
* This method must be invoked as: $var = &Prefs::singleton()
*
* @param mixed $driver The type of concrete Prefs subclass to return.
* If $driver is an array, then we will look in
* $driver[0]/lib/Prefs/ for the subclass
* implementation named $driver[1].php.
* @param string $scope The scope for this set of preferences.
* @param string $user The name of the user who owns this set of
* preferences.
* @param string $password The password associated with $user.
* @param array $params A hash containing any additional configuration
* or connection parameters a subclass might need.
* @param boolean $caching Should caching be used?
*
* @return Prefs The concrete Prefs reference, or false on an error.
*/
function &singleton($driver, $scope = 'horde', $user = '', $password = '',
$params = null, $caching = true)
{
static $instances = array();
if (is_null($params)) {
$params = Horde::getDriverConfig('prefs', $driver);
}
$signature = serialize(array($driver, $user, $params));
if (!isset($instances[$signature])) {
$instances[$signature] = &Prefs::factory($driver, $scope, $user, $password, $params, $caching);
}
return $instances[$signature];
}
}
|