<?php
/**
OOB/N1 Framework [©2004, 2005 - Nutus]
@license: BSD
@author: Pablo Micolini / Nutus 2005
OOB/N1 Framework:: Advanced Resources Integration (ARI)
Provides safe-forms by checking that a form can't be sent twise avoiding duplicate data
*/
class OOB_safepost {
private $llave = array ();
private $hayllave = FALSE;
private $form_name;
/** The form name must be provided to the constructor */
public function __construct ($form = 'form')
{
$this->form_name = $form;
}
/** Start the safe form */
public function Iniciar() {
if ($this->hayllave == FALSE) {
$this->llave[0] = md5(microtime());
$this->llave[1] = md5(rand());
$_SESSION[$this->form_name .'_'.$this->llave[1]] = $this->llave[0];
}
$this->hayllave = TRUE;
return $this->llave;
}
/** Provides the form code that must be passed to the output */
public function FormElement() {
if ($this->hayllave === FALSE)
$this->Iniciar();
return '<input name="' . $this->form_name . '-0" type="hidden" value="' . $this->llave[0] . '" />' .
'<input name="' . $this->form_name . '-1" type="hidden" value="' . $this->llave[1] . '" />';
}
/** Validates the form, and returns false if it has been already posted */
public function Validar() {
if (isset($_POST[$this->form_name . "-0"]) && isset($_POST[$this->form_name . "-1"]) && isset($_SESSION[$this->form_name . '_' .$_POST[$this->form_name . "-1"]]) && $_SESSION[$this->form_name . '_' .$_POST[$this->form_name . "-1"]] === $_POST[$this->form_name . "-0"])
{
unset ($_SESSION[$this->form_name . '_' .$_POST[$this->form_name . "-1"]]);
return TRUE;
}
else
return FALSE;
}
}
?>