Files
selima-perl/lib/php/monica/userpref.inc.php
2026-03-10 21:31:43 +08:00

124 lines
4.2 KiB
PHP

<?php
// File name: userpref.inc.php
// Description: PHP subroutines to obtain user preferences
// Date: 2002-11-13
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2002-2007 Pristine Communications
// Set the include path
if (!defined("INCPATH_SET")) {
require_once dirname(__FILE__) . "/incpath.inc.php";
}
// Referenced subroutines
require_once "monica/guest.inc.php";
require_once "monica/login.inc.php";
require_once "monica/requri.inc.php";
require_once "monica/sql.inc.php";
// userpref: Obtain the specified user preference
function userpref($name, $domain = null)
{
// Cache the result
static $cache = array();
// Set the default domain
if (is_null($domain)) {
$domain = REQUEST_PATH;
}
// Initialize the cache
if (!array_key_exists($domain, $cache)) {
$cache[$domain] = array();
}
// Return the cache
if (array_key_exists($name, $cache[$domain])) {
return $cache[$domain][$name];
}
// User system not available
if (!use_users() || !isset($_SESSION)) {
if ( array_key_exists("USERPREF", $GLOBALS)
&& array_key_exists($domain, $GLOBALS["USERPREF"])
&& array_key_exists($name, $GLOBALS["USERPREF"][$domain])) {
$cache[$domain][$name] = $GLOBALS["USERPREF"][$domain][$name];
return $cache[$domain][$name];
}
$cache[$domain][$name] = null;
return null;
}
// User system is in use
// Check guest preferences in $_SESSION
if (is_guest()) {
// Check the preference on this domain
if ( array_key_exists("userpref", $_SESSION)
&& array_key_exists($domain, $_SESSION["userpref"])
&& array_key_exists($name, $_SESSION["userpref"][$domain])) {
$cache[$domain][$name] = $_SESSION["userpref"][$domain][$name];
return $cache[$domain][$name];
}
// Check the default preference
if ( array_key_exists("userpref", $_SESSION)
&& array_key_exists("*", $_SESSION["userpref"])
&& array_key_exists($name, $_SESSION["userpref"]["*"])) {
$cache[$domain][$name] = $_SESSION["userpref"]["*"][$name];
return $cache[$domain][$name];
}
}
// Already logged in -- check the user preference
if (!is_null(get_login_sn())) {
// Check the user preference on this domain
$select = "SELECT value FROM userpref"
. " WHERE usr=" . get_login_sn()
. " AND domain='" . sql_esctext($domain) . "'"
. " AND name='" . sql_esctext($name) . "';\n";
$result = sql_query($select);
if (sql_num_rows($result) == 1) {
$row = sql_fetch_assoc($result);
$cache[$domain][$name] = $row["value"];
return $cache[$domain][$name];
}
// Check the default preference of this user
$select = "SELECT value FROM userpref"
. " WHERE usr=" . get_login_sn()
. " AND domain IS NULL"
. " AND name='" . sql_esctext($name) . "';\n";
$result = sql_query($select);
if (sql_num_rows($result) == 1) {
$row = sql_fetch_assoc($result);
$cache[$domain][$name] = $row["value"];
return $cache[$domain][$name];
}
}
// Check the default preference on this domain
$select = "SELECT value FROM userpref"
. " WHERE usr IS NULL"
. " AND domain='" . sql_esctext($domain) . "'"
. " AND name='" . sql_esctext($name) . "';\n";
$result = sql_query($select);
if (sql_num_rows($result) == 1) {
$row = sql_fetch_assoc($result);
$cache[$domain][$name] = $row["value"];
return $cache[$domain][$name];
}
// Check the default preference for everything
$select = "SELECT value FROM userpref"
. " WHERE usr IS NULL"
. " AND domain IS NULL"
. " AND name='" . sql_esctext($name) . "';\n";
$result = sql_query($select);
if (sql_num_rows($result) == 1) {
$row = sql_fetch_assoc($result);
$cache[$domain][$name] = $row["value"];
return $cache[$domain][$name];
}
// Preference not found
$cache[$domain][$name] = null;
return null;
}
?>