Files
selima-perl/lib/perl5/Selima/UserPref.pm
2026-03-10 21:31:43 +08:00

122 lines
4.1 KiB
Perl

# Selima Website Content Management System
# UserPref.pm: The user preference subroutine.
# Copyright (c) 2004-2018 imacat.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Author: imacat <imacat@mail.imacat.idv.tw>
# First written: 2004-09-12
package Selima::UserPref;
use 5.008;
use strict;
use warnings;
use base qw(Exporter);
use vars qw(@EXPORT @EXPORT_OK);
BEGIN {
@EXPORT = qw(userpref);
@EXPORT_OK = @EXPORT;
# Prototype declaration
sub userpref($;$);
}
use Selima::Cache qw(:userpref);
use Selima::DataVars qw($DBH $SESSION :requri :user);
use Selima::Guest;
use Selima::LogIn;
# userpref: Obtain the specified user preference
sub userpref($;$) {
local ($_, %_);
my ($name, $domain, $cache, $sql, $sth, $row);
($name, $domain) = @_;
# Set the default domain
$domain = $REQUEST_PATH if !defined $domain;
# Initialize the cache
$UserPref_userpref{$domain} = {} if !exists $UserPref_userpref{$domain};
$cache = $UserPref_userpref{$domain};
# Return the cache
return $$cache{$name} if exists $$cache{$name};
# User system not available
if (!use_users || !defined $SESSION) {
return ($$cache{$name} = ${$USERPREF{$domain}}{$name})
if exists $USERPREF{$domain}
&& exists ${$USERPREF{$domain}}{$name};
return ($$cache{$name} = undef);
}
# User system is in use
# Check guest preferences in $SESSION
if (is_guest) {
# Check the preference on this domain
return ($$cache{$name} = ${${$$SESSION{"userpref"}}{$domain}}{$name})
if exists $$SESSION{"userpref"}
&& exists ${$$SESSION{"userpref"}}{$domain}
&& exists ${${$$SESSION{"userpref"}}{$domain}}{$name};
# Check the default preference
return ($$cache{$name} = ${${$$SESSION{"userpref"}}{"*"}}{$name})
if exists $$SESSION{"userpref"}
&& exists ${$$SESSION{"userpref"}}{"*"}
&& exists ${${$$SESSION{"userpref"}}{"*"}}{$name};
}
# Already logged in -- check the user preference
if (defined get_login_sn) {
# Check the user preference on this domain
$sql = "SELECT value FROM userpref"
. " WHERE usr=" . get_login_sn
. " AND domain=" . $DBH->quote($domain)
. " AND name=" . $DBH->quote($name) . ";\n";
$sth = $DBH->prepare($sql);
$sth->execute;
return ($$cache{$name} = ${$sth->fetch}[0])
if $sth->rows == 1;
# Check the default preference of this user
$sql = "SELECT value FROM userpref"
. " WHERE usr=" . get_login_sn
. " AND domain IS NULL"
. " AND name=" . $DBH->quote($name) . ";\n";
$sth = $DBH->prepare($sql);
$sth->execute;
return ($$cache{$name} = ${$sth->fetch}[0])
if $sth->rows == 1;
}
# Check the default preference on this domain
$sql = "SELECT value FROM userpref"
. " WHERE usr IS NULL"
. " AND domain=" . $DBH->quote($domain)
. " AND name=" . $DBH->quote($name) . ";\n";
$sth = $DBH->prepare($sql);
$sth->execute;
return ($$cache{$name} = ${$sth->fetch}[0])
if $sth->rows == 1;
# Check the default preference for everything
$sql = "SELECT value FROM userpref"
. " WHERE usr IS NULL"
. " AND domain IS NULL"
. " AND name=" . $DBH->quote($name) . ";\n";
$sth = $DBH->prepare($sql);
$sth->execute;
return ($$cache{$name} = ${$sth->fetch}[0])
if $sth->rows == 1;
return ($$cache{$name} = undef);
}
return 1;