Skip to content
Snippets Groups Projects
Commit 8ee90d24 authored by Nigel Kukard's avatar Nigel Kukard
Browse files

Added some functions to determine data types

parent 265fccd6
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,11 @@ our (@ISA,@EXPORT,@EXPORT_OK); ...@@ -28,6 +28,11 @@ our (@ISA,@EXPORT,@EXPORT_OK);
@EXPORT = qw( @EXPORT = qw(
prettyUndef prettyUndef
toHex toHex
parseFormContent
isVariable
isUsername
isIP
isNumber
); );
@EXPORT_OK = qw( @EXPORT_OK = qw(
); );
...@@ -51,5 +56,123 @@ sub toHex ...@@ -51,5 +56,123 @@ sub toHex
return sprintf('%x',$decimal); return sprintf('%x',$decimal);
} }
# Parse form post data from HTTP content
sub parseFormContent
{
my $data = shift;
my %res;
# Split information into name/value pairs
my @pairs = split(/&/, $data);
foreach my $pair (@pairs)
{
my ($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$res{$name} = $value;
}
return \%res;
}
# Check if variable is normal
sub isVariable
{
my $var = shift;
# A variable cannot be undef?
if (!defined($var)) {
return undef;
}
return (ref($var) eq "");
}
# Check if variable is a username
sub isUsername
{
my $var = shift;
# Make sure we're not a ref
if (!isVariable($var)) {
return undef;
}
# Lowercase it
$var = lc($var);
# Normal username
if ($var =~ /^[a-z0-9_\-\.]+$/) {
return $var;
}
# Username with domain
if ($var =~ /^[a-z0-9_\-\.]+\@[a-z0-9\-\.]+$/) {
return $var;
}
return undef;
}
# Check if variable is an IP
sub isIP
{
my $var = shift;
# Make sure we're not a ref
if (!isVariable($var)) {
return undef;
}
# Lowercase it
$var = lc($var);
# Normal IP
if ($var =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
return $var;
}
return undef;
}
# Check if variable is a number
sub isNumber
{
my $var = shift;
# Make sure we're not a ref
if (!isVariable($var)) {
return undef;
}
# Strip leading 0's
if ($var =~ /^0*([0-9]+)$/) {
my $val = int($1);
# Check we not 0 or negative
if ($val > 0) {
return $val;
}
# Check if we allow 0's
if ($val == 0) {
return $val;
}
}
return undef;
}
1; 1;
# vim: ts=4 # vim: ts=4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment