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

253 lines
8.3 KiB
Perl

# Selima Website Content Management System
# Link.pm: The related-link 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-24
package Selima::Checker::Link;
use 5.008;
use strict;
use warnings;
use base qw(Selima::Checker);
use Email::Valid;
use Selima::ChkFunc;
use Selima::DataVars qw($DBH);
use Selima::ShortCut;
# new: Initialize the checker
sub new : method {
local ($_, %_);
my ($class, $self);
($class, @_) = @_;
$_[1] = "links" if scalar(@_) < 2 || !defined $_[1];
$self = $class->SUPER::new(@_);
${$self->{"minlens"}}{"email"} = 5;
return $self;
}
# _check_addr: Check the address
sub _check_addr : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("addr");
return $error if defined $error;
# Regularize it
$self->_trim("addr");
# Skip if it is not filled
return if $form->param("addr") eq "";
# Check the length
return {"msg"=>N_("This address is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"addr"}]}
if length $form->param("addr") > ${$self->{"maxlens"}}{"addr"};
# OK
return;
}
# _check_cats: Check the categories list
sub _check_cats : method {
local ($_, %_);
my ($self, $form, $error, $val);
$self = $_[0];
$form = $self->{"form"};
# Loop each category
for ($_ = 0, %_ = qw(); !$self->_missing("cat$_"); $_++) {
# Regularize it
$self->_trim("cat$_");
# Skip if it is not filled
next if $form->param("cat$_") eq "";
# Check if this selection is duplicated
return {"msg"=>N_("This category is duplicated. You cannot set duplicated ones.")}
if exists $_{$form->param("cat$_")};
# Check if the category exists
return {"msg"=>N_("This category does not exist anymore. Please select another one.")}
if !check_sn_in ${$form->param_fetch("cat$_")}[0], "linkcat";
$_{$form->param("cat$_")} = 1;
}
# Check if there is any category selected
return {"msg"=>N_("Please select a category.")}
if scalar(keys %_) == 0;
# OK
return;
}
# _check_email: Check the e-mail
sub _check_email : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("email");
return $error if defined $error;
# Regularize it
$self->_trim("email");
# Skip if it is not filled
return if $form->param("email") eq "";
# Check the length
return {"msg"=>N_("This e-mail is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"email"}]}
if length $form->param("email") > ${$self->{"maxlens"}}{"email"};
return {"msg"=>N_("This e-mail is too short. (Min. length [#,_1])"),
"margs"=>[${$self->{"minlens"}}{"email"}]}
if length $form->param("email") < ${$self->{"minlens"}}{"email"};
# Check the e-mail validity
return {"msg"=>N_("Please fill in a valid e-mail address.")}
if !Email::Valid->rfc822($form->param("email"));
return {"msg"=>N_("The domain of this e-mail does not exists. Check if there is any typo in it.")}
if $self->_missing("hid")
&& !Email::Valid->mx($form->param("email"));
# OK
return;
}
# _check_fax: Check the facsimile number
sub _check_fax : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("fax");
return $error if defined $error;
# Regularize it
$self->_trim("fax");
# Skip if it is not filled
return if $form->param("fax") eq "";
# Check the length
return {"msg"=>N_("This facsimile number is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"fax"}]}
if length $form->param("fax") > ${$self->{"maxlens"}}{"fax"};
# OK
return;
}
# _check_icon: Check the link icon
sub _check_icon : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("icon");
return $error if defined $error;
# Regularize it
$self->_trim("icon");
# Skip if it is not filled
return if $form->param("icon") eq "" || $form->param("icon") eq "http://";
# Check the length
return {"msg"=>N_("This link icon URL is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"icon"}]}
if length $form->param("icon") > ${$self->{"maxlens"}}{"icon"};
# Check its format
return {"msg"=>N_("Please fill in a valid link icon URL.")}
if !is_url_wellformed $form->param("icon");
# Check if it is available
return {"msg"=>N_("This link icon URL is not reachable. Check if there is any typo in it.")}
if $self->_missing("hid")
&& !is_url_reachable $form->param("icon");
# OK
return;
}
# _check_tel: Check the telephone number
sub _check_tel : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("tel");
return $error if defined $error;
# Regularize it
$self->_trim("tel");
# Skip if it is not filled
return if $form->param("tel") eq "";
# Check the length
return {"msg"=>N_("This telephone number is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"tel"}]}
if length $form->param("tel") > ${$self->{"maxlens"}}{"tel"};
# OK
return;
}
# _check_title_2ln: The 2nd language title checker
sub _check_title_2ln : method {
local ($_, %_);
my ($self, $form, $error);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("title_2ln");
return $error if defined $error;
# Regularize it
$self->_trim("title_2ln");
# Skip if it is not filled
return if $form->param("title_2ln") eq "";
# Check the length
return {"msg"=>N_("This 2nd language title is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"title_2ln"}]}
if length $form->param("title_2ln") > ${$self->{"maxlens"}}{"title_2ln"};
# OK
return;
}
# _check_url: The URL checker
sub _check_url : method {
local ($_, %_);
my ($self, $form, $error, $sth, $sql);
$self = $_[0];
$form = $self->{"form"};
# Check if it exists
$error = $self->_missing("url");
return $error if defined $error;
# Regularize it
$self->_trim("url");
# Check if it is filled
return {"msg"=>N_("Please fill in the URL.")}
if $form->param("url") eq "" || $form->param("url") eq "http://";
# Check the length
return {"msg"=>N_("This URL is too long. (Max. length [#,_1])"),
"margs"=>[${$self->{"maxlens"}}{"url"}]}
if length $form->param("url") > ${$self->{"maxlens"}}{"url"};
# Check its format
return {"msg"=>N_("Please fill in a valid URL.")}
if !is_url_wellformed $form->param("url");
# Check if this item is duplicated
@_ = qw();
push @_, "url=" . $DBH->quote($form->param("url"));
push @_, "sn!=" . $self->{"sn"} if $self->{"iscur"};
$sql = "SELECT * FROM " . $DBH->quote_identifier($self->{"table"})
. " WHERE " . join(" AND ", @_) . ";\n";
$sth = $DBH->prepare($sql);
$sth->execute;
return {"msg"=>N_("This related link already exists. You cannot create a duplicated one.")}
if $sth->rows > 0;
# Check if it is available
return {"msg"=>N_("This URL is not reachable. Check if there is any typo in it.")}
if $self->_missing("hid")
&& !is_url_reachable $form->param("url");
# OK
return;
}
return 1;