I have an upload form on my site and I'm trying to sanitize and transform input data.
I will be straight forward to you - this is my first object-oriented style code! The reason I don't just want to use regular function, is that I would like to keep my codes divided in logical blocks. I called this one Filter. It has, so far 2 methods in it.
Before I will tell you what I'd like to improve, here is my Filter Class:
class Filter {
public static function Text($data, $tags = 1, $displace = 1, $characters = 1, $numbers = 0, $punctuation = 0, $linespacing = 2, $whitespaces = 1, $transform = 0) {
if ($tags === 1) {
$data = filter_var($data, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
}
if ($displace === 1) {
$data = str_replace('`', '\'', $data);
}
if ($characters === 1) {
$unwanted_characters = array(
'<',
'>',
'{',
'}',
'*',
'|',
'\\',
'%',
'^',
'~',
'‘',
'getURL',
'javascript',
'activex',
'x00',
'x04',
'x08',
'x0d',
'x1b',
'x20',
'x7f',
'%7b',
'%7d',
'%7c',
'%5c',
'%5e',
'%7e',
'%60',
'%25',
'%27'
);
$data = str_replace($unwanted_characters, '', $data);
}
if ($numbers === 1) {
$data = preg_replace('/\d/', '', $data);
}
if ($punctuation === 1) {
$unwanted_punctuation = array(
',',
'.',
':',
';',
'!',
'?',
'#',
'№',
'@',
'$',
'&',
'*',
'=',
'/',
'[',
']'
);
$data = str_replace($unwanted_punctuation, '', $data);
}
if ($linespacing === 0) {
$data = preg_replace("/(\r?\n){0,}\n+/", " ", $data);
}
if ($linespacing === 1) {
$data = preg_replace("/(\r?\n){1,}/", "\n", $data);
}
if ($linespacing === 2) {
$data = preg_replace("/(\r?\n){2,}/", "\n\n", $data);
}
if ($linespacing === 3) {
$data = preg_replace("/(\r?\n){3,}/", "\n\n\n", $data);
}
if ($linespacing === 4) {
$data = preg_replace("/(\r?\n){4,}/", "\n\n\n\n", $data);
}
if ($linespacing === 5) {
$data = preg_replace("/(\r?\n){5,}/", "\n\n\n\n\n", $data);
}
if ($whitespaces === 1) {
$data = preg_replace("/[ ]+/", " ", $data);
$data = join("\n", array_map("trim", explode("\n", $data)));
$data = join("\r", array_map("trim", explode("\r", $data)));
}
if ($transform === 1) {
$data = mb_strtolower($data);
}
if ($transform === 2) {
$data = mb_strtoupper($data);
}
return $data;
}
public static function Links($data) {
$data = preg_replace('%(((f|ht){1}tp://|(f|ht){1}tps://)[-a-zA-^Z0-9@:\%_\+.~#?&//=]+)%i', '<a href="\\1" target="_blank">\\1</a>', $data);
$data = preg_replace('%([[:space:]()[{}])(www.[-a-zA-Z0-9@:\%_\+.~#?&//=]+)%i', '\\1<a href="http://\\2" target="_blank">\\2</a>', $data);
if (preg_match("/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i", $data, $email)) {
$replacement = '<a href="mailto:' . $email[0] . '" target="_blank">' . $email[0] . '</a> ';
$data = preg_replace("/[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}/i", $replacement, $data);
}
return $data;
}
}
I use it as follows:
$description = Filter::Text($_POST['description'], $tags = 1, $displace = 1, $characters = 1, $numbers = 0, $punctuation = 0, $linespacing = 2, $whitespaces = 1, $transform = 0);
and
$description = Filter::Links($description);
If you have anything in mind that you would like to say to improve it, please go ahead.
I personally don't like to pass everytime a huge set of variables, such as $tags = 1, $displace = 1, $characters = 1, $numbers = 0 ...
. What I would like to do is to set default values at my class's methods just once and then change only specific ones in case I have to.
For example, to show what I mean, in order to filter my description field with keeping default values but one (which is $transform
in the example below):
$description = Filter::Text($_POST['description'], $transform = 1);
Now, instead of writing that huge set of default variable, I could only pass the variable that's value is different from default.
It doesn't work this way so far. Not sure what I'm missing?