input("csrf")->type("hidden")->value(""); } /** * Use our own template */ public function render($template="form.html", $custom=false) { $session = Session::instance(); $csrf = $session->get("csrf"); if (empty($csrf)) { $csrf = md5(rand()); $session->set("csrf", $csrf); } $this->inputs["csrf"]->value($csrf); return parent::render($template, $custom); } /** * Associate validation rules defined in the model with this form. */ public function add_rules_from($model) { foreach ($this->inputs as $name => $input) { if (isset($input->inputs)) { $input->add_rules_from($model); } if (isset($model->rules[$name])) { $input->rules($model->rules[$name]); } } } /** * Validate our CSRF value as a mandatory part of all form validation. */ public function validate() { $status = parent::validate(); $type = $this->type; if (empty($type)) { $csrf_value = $this->csrf->value; if (empty($csrf_value) || $csrf_value !== Session::instance()->get("csrf")) { throw new Exception("@todo SECURITY_INVALID_CSRF_TOKEN"); } } return $status; } }