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

133 lines
4.4 KiB
Perl

# Selima Website Content Management System
# ListPref.pm: The list preference form checker.
# 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-10-14
package Selima::Checker::ListPref;
use 5.008;
use strict;
use warnings;
use base qw(Selima::Checker);
use Selima::CallForm;
use Selima::ChkFunc;
use Selima::DataVars qw($DBH :forms);
use Selima::HTTP;
use Selima::ShortCut;
# new: Initialize the checker
sub new : method {
local ($_, %_);
my ($class, $self);
($class, @_) = @_;
$_[1] = "userpref" if scalar(@_) < 2 || !defined $_[1];
$self = $class->SUPER::new(@_);
${$self->{"maxlens"}}{"listsize"} = 4;
return $self;
}
# _check_domain: Check the preference domain
sub _check_domain : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("domain");
return $error if defined $error;
# Regularize it
$self->_trim("domain");
# Check if it is filled
return {"msg"=>N_("Please fill in the preference domain.")}
if $form->param("domain") eq "";
# Check the length
return {"msg"=>N_("This preference domain is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"domain"}]}
if length $form->param("domain") > ${$self->{"maxlens"}}{"domain"};
# OK
return;
}
# _check_listcols: Check the list columns
sub _check_listcols : method {
local ($_, %_);
my ($self, $listcols, $errmsg);
$self = $_[0];
# No need to check the validility. Invalids are simply ignored.
@_ = grep s/^listcols_//, $self->{"form"}->param;
# Obtain the preference value
$listcols = join " ", @_;
# Skip if it is not filled
return if $listcols eq "";
# Check the length
if (length "listcols" > ${$self->{"maxlens"}}{"name"}) {
$errmsg = sprintf "Maximum preference name length too short (%d for \"%s\" %d)",
${$self->{"maxlens"}}{"name"}, "listcols", length "listcols";
http_500 $errmsg;
}
if (length $listcols > ${$self->{"maxlens"}}{"value"}) {
$errmsg = sprintf "Maximum preference value length too short (%d for \"%s\" %d)",
${$self->{"maxlens"}}{"name"}, "listcols", length $listcols;
http_500 $errmsg;
}
# OK
return;
}
# _check_listsize: Check the list size
sub _check_listsize : method {
local ($_, %_);
my ($self, $form, $error, $errmsg);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("listsize");
return $error if defined $error;
# Regularize it
$self->_trim("listsize");
# Check if it is filled
return {"msg"=>N_("Please fill in the number of rows per page.")}
if $form->param("listsize") eq "";
# If there is any non-digit character
return {"msg"=>N_("Please fill in a positive integer number of rows per page.")}
unless $form->param("listsize") =~ /^[1-9][0-9]*$/;
# Set to an integer
$_ = $form->param("listsize");
$_ += 0;
$form->param("listsize", $_);
# Check the length
return {"msg"=>N_("This number of rows per page is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"listsize"}]}
if length $form->param("listsize") > ${$self->{"maxlens"}}{"listsize"};
# Check the length
if (length "listsize" > ${$self->{"maxlens"}}{"name"}) {
$errmsg = sprintf "Maximum preference name length too short (%d for \"%s\" %d)",
${$self->{"maxlens"}}{"name"}, "listsize", length "listsize";
http_500 $errmsg;
}
if (length $form->param("listsize") > ${$self->{"maxlens"}}{"value"}) {
$errmsg = sprintf "Maximum preference value length too short (%d for \"%s\" %d)",
${$self->{"maxlens"}}{"name"}, "listsize", length $form->param("listsize");
http_500 $errmsg;
}
# OK
return;
}
return 1;