<commit_before><?php

/* !
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2017 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * FormHeader.php
 * Header for all forms that includes the form validation token.
 */

namespace SmoothPHP\Framework\Forms\Containers;

use SmoothPHP\Framework\Flow\Requests\Request;
use SmoothPHP\Framework\Forms\Constraint;
use SmoothPHP\Framework\Forms\Form;

class FormHeader extends Constraint {

	const SESSION_KEY = 'sm_formtokens';

	private $form;
	private $attributes;

	public function __construct(Form $form, array $attributes) {
		$this->form = $form;

		$this->attributes = array_replace_recursive([
			'token' => true,
			'attr'  => [
				'method'  => 'post',
				'class'   => 'smoothform',
				'enctype' => 'multipart/form-data'
			]
		], $attributes);
	}

	public function __toString() {
		$tokenInput = '';
		if ($this->attributes['token']) {
			if (!isset($_SESSION[self::SESSION_KEY]))
				$_SESSION[self::SESSION_KEY] = [];

			$formToken = md5(uniqid(rand(), true));
			$_SESSION[self::SESSION_KEY][] = $formToken;

			$tokenInput = sprintf('<input type="hidden" id="_token" name="_token" value="%s" />', $formToken);
		}

		$htmlAttributes = [];
		$attributes = $this->attributes['attr'];

		$attributes['action'] = $this->form->getAction();

		foreach ($attributes as $key => $attribute)
			if (isset($attribute) && strlen($attribute) > 0)
				$htmlAttributes[] = sprintf('%s="%s"', $key, addcslashes($attribute, '"'));

		return sprintf('<form %s />%s', implode(' ', $htmlAttributes), $tokenInput);
	}

	public function checkConstraint(Request $request, $name, $label, $value, array &$failReasons) {
		if ($this->attributes['token']) {
			$key = array_search($request->post->_token, $_SESSION[self::SESSION_KEY], true);
			if ($key === false) {
				$failReasons[] = 'Form security token mismatch.';
				return;
			}
			unset($_SESSION[self::SESSION_KEY][$key]);
		}<commit_msg>Fixed undefined index error in FormHeader if session is not intact<commit_after><?php

/* !
 * SmoothPHP
 * This file is part of the SmoothPHP project.
 * * * *
 * Copyright (C) 2017 <NAME>
 * License: https://github.com/Ikkerens/SmoothPHP/blob/master/License.md
 * * * *
 * FormHeader.php
 * Header for all forms that includes the form validation token.
 */

namespace SmoothPHP\Framework\Forms\Containers;

use SmoothPHP\Framework\Flow\Requests\Request;
use SmoothPHP\Framework\Forms\Constraint;
use SmoothPHP\Framework\Forms\Form;

class FormHeader extends Constraint {

	const SESSION_KEY = 'sm_formtokens';

	private $form;
	private $attributes;

	public function __construct(Form $form, array $attributes) {
		$this->form = $form;

		$this->attributes = array_replace_recursive([
			'token' => true,
			'attr'  => [
				'method'  => 'post',
				'class'   => 'smoothform',
				'enctype' => 'multipart/form-data'
			]
		], $attributes);
	}

	public function __toString() {
		$tokenInput = '';
		if ($this->attributes['token']) {
			if (!isset($_SESSION[self::SESSION_KEY]))
				$_SESSION[self::SESSION_KEY] = [];

			$formToken = md5(uniqid(rand(), true));
			$_SESSION[self::SESSION_KEY][] = $formToken;

			$tokenInput = sprintf('<input type="hidden" id="_token" name="_token" value="%s" />', $formToken);
		}

		$htmlAttributes = [];
		$attributes = $this->attributes['attr'];

		$attributes['action'] = $this->form->getAction();

		foreach ($attributes as $key => $attribute)
			if (isset($attribute) && strlen($attribute) > 0)
				$htmlAttributes[] = sprintf('%s="%s"', $key, addcslashes($attribute, '"'));

		return sprintf('<form %s />%s', implode(' ', $htmlAttributes), $tokenInput);
	}

	public function checkConstraint(Request $request, $name, $label, $value, array &$failReasons) {
		if ($this->attributes['token']) {
			$tokens = isset($_SESSION[self::SESSION_KEY]) ? $_SESSION[self::SESSION_KEY] : [];
			$key = array_search($request->post->_token, $tokens, true);
			if ($key === false) {
				$failReasons[] = 'Form security token mismatch.';
				return;
			}
			unset($_SESSION[self::SESSION_KEY][$key]);
		}
{'fraction_non_alphanumeric': 0.18157640232108319, 'fraction_numerical': 0.0029013539651837525, 'mean_word_length': 4.508655126498002, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 41, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1202057', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1328, 'n_words': 287}