"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/cgi-bin/AbsenceInput.pm" (20 Oct 2013, 2822 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbsenceInput.pm" see the Fossies "Dox" file reference documentation.

    1 #======================================================================
    2 #    This file is part of Absence.
    3 #
    4 #    Absence is free software: you can redistribute it and/or modify
    5 #    it under the terms of the GNU General Public License as published by
    6 #    the Free Software Foundation, either version 3 of the License, or
    7 #    (at your option) any later version.
    8 #
    9 #    Absence is distributed in the hope that it will be useful,
   10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 #    GNU General Public License for more details.
   13 #
   14 #    You should have received a copy of the GNU General Public License
   15 #    along with Absence.  If not, see <http://www.gnu.org/licenses/>.
   16 #======================================================================
   17 
   18 # $Id: AbsenceInput.pm 111 2013-10-20 17:57:24Z urban $
   19 # copyright Robert Urban
   20 
   21 package AbsenceInput;
   22 
   23 use Encode qw(decode_utf8);
   24 
   25 require Exporter;
   26 @ISA = qw(Exporter);
   27 @EXPORT_OK = qw(validate_input);
   28 @EXPORT = qw(validate_input);
   29 
   30 my $VERSION = '2.0.1';
   31 
   32 sub validate_input
   33 {
   34     my $ref = shift;
   35 
   36     my $res = undef;
   37     my @out;
   38 
   39     for(my $i = 0; $i < $#{ $ref }; $i += 2) {
   40         my ($name, $type);
   41         my ($tag, $val) = @{ $ref }[$i, $i+1];
   42         if (!defined($val) || length($val) == 0) { next; }
   43         if (ref($val)) {
   44             ($name, $type, $val) = ($tag, $val->[0], $val->[1]);
   45         } else {
   46             ($name, $type) = ($tag, $tag);
   47         }
   48 
   49         my @vals = ref($val) ? @{ $val } : ($val);
   50         foreach my $v (@vals) {
   51             my $r = check_val($tag, $type, $v);
   52             defined($r) && push(@out, $r);
   53         }
   54     }
   55 
   56     return @out ? join("<BR>\n", @out)."<BR>\n" : undef;
   57 }
   58 
   59 sub check_val
   60 {
   61     my ($tag, $type, $val) = @_;
   62 
   63     $val = decode_utf8($val);
   64 
   65     #----------------------------------------------
   66     # validate value
   67     #----------------------------------------------
   68     if ($type eq 'num') {
   69         ($val =~ /^\d+$/) || return "$tag [$val] contains non-digit chars";
   70     }
   71     elsif ($type eq 'username') {
   72         if ($val !~ /^[a-z0-9_.,]+$/i) {
   73             my $tmp = $val;
   74             $tmp =~ s![a-z0-9_.,]!!ig;
   75             return "username [$val] contains illegal chars [$tmp] (allowed: a-z0-9_.,)";
   76         }
   77     }
   78     elsif ($type eq 'acl') {
   79         ($val =~ /^\d+:\d+$/) || return "acl [$val] illegal";
   80     }
   81     elsif ($type eq 'name') {
   82         if ($val =~ m![\000-\037\\`]!) {
   83             return "name [$val] contains illegal chars";
   84         }
   85         #if ($val !~ m!^[-a-z0-9_,./ ]+$!i) {
   86         #   my $tmp = $val;
   87         #   $tmp =~ s![-a-z0-9_,./ ]!!ig;
   88         #   return "name [$val] contains illegal chars [$tmp] (allowed: a-z0-9.,/- and blank)";
   89         #}
   90     }
   91     elsif ($type eq 'description') {
   92         ($val =~ m{^[-a-z0-9_,!./ ]+$}i)
   93             || return "description [$val] contains illegal chars (allowed: a-z0-9!.,/-)";
   94     }
   95     elsif ($type eq 'email') {
   96         ($val =~ /^[-a-z0-9_.]+@([-a-z0-9]+\.?)+$/i)
   97             || return "email [$val] invalid";
   98     }
   99 
  100     return undef;
  101 }
  102 
  103 1;