This helper class is supposed to validate the user input from a text based wifi login form.
The entire process is basically this:
I use the create_voucher
function of this API passing voucher_duration
to set the amount of time for which the voucher is going to be valid and $this->clean['name']." ".$this->clean['surname']
as a note so I can later identify which device belongs to which user.
The code returned by create_voucher
is then sent to the passed e-mail-adress using php-mailer so the user can login and use the wifi.
I am particularly unhappy with my error handling and would like to know if you see any obvious ways to break the code or inject malicious code.
class sanitizer
{
public $clean;
private $post=null;
private $reg_email ='/^\S+@\S+\.\S+$/'; //Just some basic checking
private $reg_name = '/^[\'\p{L} -]+[\n]?$/im'; //Allowing some wierd names
private $reg_number='/^[[:digit:]]*$/im'; //A single integer no fuzz
public function __construct($post){
$this->post=$post;
}
private function sanitize($key, $regex){
if (preg_match($regex, $this->post[$key])) {
$this->clean[$key]= $this->post[$key];
} else {
$this->clean[$key]=null;
}
}
public function clean_up(){
if (isset($this->post['smt_sent'])) {
if ($this->post['smt_sent']==1) {
$this->sanitize('name', $this->reg_name);
$this->sanitize('surname', $this->reg_name);
$this->sanitize('voucher_duration', $this->reg_number);
if ($this->post['voucher_duration'] > 0 && ($this->post['voucher_duration']/60 > 48)) {
$this->clean['duration']=null;
}
$this->sanitize('email_own', $this->reg_email);
$this->clean['smt_received']=1;
$this->clean['error'] = false; //No errors yet
foreach ($this->clean as $field) { //Loop trough each field
if (!isset($field)) {
$this->clean['error'] = true; //Yup there are errors
}
}
return $this->clean;
}
}
}
}