<commit_before><?php

/**
 * Http Request class for providing global vars to class for improved
 * encapsulation
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.1 dev
 *
 */

/**
 * Class used to interact with super globals, POST, GET, SERVER, COOKIES, SESSION
 *
 * - Currently only a 'getter' of values
 * - Can be passed DataValidation sanitation values to return sanitized values
 * - Fetch raw values as $instance->post->keyname
 *     - $this->-req->post->filename
 * - Fetch cleaned values with $instance->getPost('keyname', 'sanitation needs', 'default value')
 *     - $this->-req->getPost('filename', 'trim|strval', '');
 */
class HttpReq
{
	/**
	 * The returned POST values
	 * @var object
	 */
	public $post;

	/**
	 * The compiled post values (json and cleaned from request)
	 * @var array
	 */
	private $_derived_post;

	/**
	 * The returned GET values
	 * @var object
	 */
	public $query;

	/**
	 * The returned COOKIE values
	 * @var object
	 */
	public $cookie;

	/**
	 * The returned SESSION values
	 * @var object
	 */
	public $session;

	/**
	 * The returned SERVER values
	 * @var object
	 */
	public $server;

	/**
	 * Sole private HttpReq instance
	 * @var HttpReq
	 */
	private static $_req = null;

	/**
	 * Used to hold processed (sanitised) values
	 * @var array
	 */
	private $_param;

	/**
	 * holds instance of the validator
	 * @var Data_Validator
	 */
	protected $_dataValidator;

	/**
	 * Class constructor, sets PHP globals to class members
	 *
	 * @param $dataValidator Data_Validator|null Instance of the data validator
	 */
	public function __construct($dataValidator = null)
	{
		// Make sure the validator is initiated
		$this->_dataValidator = $dataValidator === null ? new Data_Validator : $dataValidator;

		// Make the superglobals available as R/W properties
		$this->cookie = new ArrayObject($_COOKIE, ArrayObject::ARRAY_AS_PROPS);
		$this->session = new ArrayObject($_SESSION, ArrayObject::ARRAY_AS_PROPS);
		$this->server = new ArrayObject($_SERVER, ArrayObject::ARRAY_AS_PROPS);

		$this->_loadParsed();
	}

	/**
	 * Certain variables are born in Request, others are sanitized, and stored in
	 * $_REQUEST, its a awful mess really.
	 * Once that mess is cleaned up this should not be needed.  But the basis is due to
	 * what function cleanRequest() does.
	 *
	 * What it does:
	 * - finds items added by cleanRequest to $_REQUEST
	 * - adds the above to both $_POST and $_GET
	 * - looks for duplicate items in $_REQUEST and $_POST and used the $_REQUEST
	 *   values, being they are "sanitized"  $_GET ones are re-stuffed by cleanRequest
	 */
	private function _loadParsed()
	{
		// Any that were born in cleanRequest, like start from topic=xyz.START
		// are added to the other supers
		$derived = array_diff_key($_REQUEST, $_POST, $_GET);
		$derived_get = array_merge($_GET, $derived);
		$this->_derived_post = array_merge($_POST, $derived);

		// Others may have been "sanitized" from either get or post and saved in request
		// these values replace the existing ones in $_POST
		$cleaned = array_intersect_key($_REQUEST, $this->_derived_post);
		$this->_derived_post = array_merge($this->_derived_post, $cleaned);

		// Load any json into _derived_post
		$this->_loadJson();

		// Make the $_GET $_POST super globals available as R/W properties
		$this->post = new ArrayObject($this->_derived_post, ArrayObject::ARRAY_AS_PROPS);
		$this->query = new ArrayObject($derived_get, ArrayObject::ARRAY_AS_PROPS);
	}

	/**
	 * Looks for the post value jsonString and expands it to POST
	 *
	 * What it does:
	 * - Looks for jsonString passed in post
	 * - json decodes the string and loads its values in to POST
	 * - Does not overwrite any existing keys
	 */
	private function _loadJson()
	{
		// Was the magic json value posted?
		if (!empty($this->_derived_post['jsonString']))
		{
			$json = json_decode($this->_derived_post['jsonString'], true);

			// Valid decode
			if (!empty($json))
			{
				// Maintain the original keys only add new ones
				$json = array_diff_key($json, $this->_derived_post);
				$this->_derived_post = array_merge($this->_derived_post, $json);
			}

			unset($this->_derived_post['jsonString']);
		}
	}

	/**
	 * Generic fetch access for values contained in the super globals
	 * - gets in order of param, get and post
	 *
	 * - $instance->keyanme will check cleaned params, get then post for values
	 *     - $_POST['foo'] = 'bar', $_GET['bar'] = 'foo'
	 *     - $this->req->post->foo is explicit and returns bar
	 *     - $this->req->foo is loose and will trigger this method, return foo as its a found key in GET
	 *
	 * @param string $key
	 */
	public function __get($key)
	{
		switch (true)
		{
			case isset($this->_param[$key]):
				return $this->_param[$key];
			case isset($this->query->{$key}):
				return $this->query->{$key};
			case isset($this->post->{$key}):
				return $this->post->{$key};
			default:<commit_msg>! tweak the comments a touch<commit_after><?php

/**
 * Http Request class for providing global vars to class for improved
 * encapsulation
 *
 * @name      ElkArte Forum
 * @copyright ElkArte Forum contributors
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
 *
 * @version 1.1 dev
 *
 */

/**
 * Class used to interact with super globals, POST, GET, SERVER, COOKIES, SESSION
 *
 * - Currently only a 'getter' of values
 * - Can be passed DataValidation sanitation values to return sanitized values
 * - Fetch raw values as $instance->post->keyname
 *     - $this->-req->post->filename
 *     - $this->_req->query->sa
 * - Fetch cleaned values with $instance->getPost('keyname', 'sanitation needs', 'default value')
 *     - $this->-req->getPost('filename', 'trim|strval', '');
 *     - $this->-req->getQuery('filename', 'intval', 0);
 */
class HttpReq
{
	/**
	 * The returned POST values
	 * @var object
	 */
	public $post;

	/**
	 * The compiled post values (json and cleaned from request)
	 * @var array
	 */
	private $_derived_post;

	/**
	 * The returned GET values
	 * @var object
	 */
	public $query;

	/**
	 * The returned COOKIE values
	 * @var object
	 */
	public $cookie;

	/**
	 * The returned SESSION values
	 * @var object
	 */
	public $session;

	/**
	 * The returned SERVER values
	 * @var object
	 */
	public $server;

	/**
	 * Sole private HttpReq instance
	 * @var HttpReq
	 */
	private static $_req = null;

	/**
	 * Used to hold processed (sanitised) values
	 * @var array
	 */
	private $_param;

	/**
	 * holds instance of the validator
	 * @var Data_Validator
	 */
	protected $_dataValidator;

	/**
	 * Class constructor, sets PHP globals to class members
	 *
	 * @param $dataValidator Data_Validator|null Instance of the data validator
	 */
	public function __construct($dataValidator = null)
	{
		// Make sure the validator is initiated
		$this->_dataValidator = $dataValidator === null ? new Data_Validator : $dataValidator;

		// Make the superglobals available as R/W properties
		$this->cookie = new ArrayObject($_COOKIE, ArrayObject::ARRAY_AS_PROPS);
		$this->session = new ArrayObject($_SESSION, ArrayObject::ARRAY_AS_PROPS);
		$this->server = new ArrayObject($_SERVER, ArrayObject::ARRAY_AS_PROPS);

		// Get will be in ->query, Post in ->post
		$this->_loadParsed();
	}

	/**
	 * Certain variables are born in Request, others are sanitized, and stored in
	 * $_REQUEST, its a awful mess really.
	 *
	 * Once that mess is cleaned up this should not be needed.  But the basis is due to
	 * what function cleanRequest() does.
	 *
	 * What it does:
	 * - Finds items added by cleanRequest to $_REQUEST
	 * - Adds the above to both $_POST and $_GET
	 * - Looks for duplicate items in $_REQUEST and $_POST and uses the $_REQUEST
	 *   values, being they are "sanitized"
	 * - $_GET ones are already re-stuffed by cleanRequest
	 */
	private function _loadParsed()
	{
		// Any that were born in cleanRequest, like start from topic=xyz.START
		// are added to the other supers
		$derived = array_diff_key($_REQUEST, $_POST, $_GET);
		$derived_get = array_merge($_GET, $derived);
		$this->_derived_post = array_merge($_POST, $derived);

		// Others may have been "sanitized" from either get or post and saved in request
		// these values replace the existing ones in $_POST
		$cleaned = array_intersect_key($_REQUEST, $this->_derived_post);
		$this->_derived_post = array_merge($this->_derived_post, $cleaned);

		// Load any json into _derived_post
		$this->_loadJson();

		// Make the $_GET $_POST super globals available as R/W properties
		$this->post = new ArrayObject($this->_derived_post, ArrayObject::ARRAY_AS_PROPS);
		$this->query = new ArrayObject($derived_get, ArrayObject::ARRAY_AS_PROPS);
	}

	/**
	 * Looks for the post value "jsonString" and expands values to POST
	 *
	 * What it does:
	 * - Looks for jsonString passed in post
	 * - json decodes the string and loads its values in to POST
	 * - Does *not* overwrite any existing keys
	 */
	private function _loadJson()
	{
		// Was the magic json value posted?
		if (!empty($this->_derived_post['jsonString']))
		{
			$json = json_decode($this->_derived_post['jsonString'], true);

			// Valid decode
			if (!empty($json))
			{
				// Maintain the original keys only add new ones
				$json = array_diff_key($json, $this->_derived_post);
				$this->_derived_post = array_merge($this->_derived_post, $json);
			}

			unset($this->_derived_post['jsonString']);
		}
	}

	/**
	 * Generic fetch access for values contained in the super globals
	 *
	 * - gets in order of param, get and post
	 * - $instance->keyanme will check cleaned params, get then post for values
	 *     - $_POST['foo'] = 'bar', $_GET['bar'] = 'foo'
	 *     - $this->req->post->foo is explicit and returns bar
	 *     - $this->req->foo is loose and will trigger this method, return foo as its a found key in GET
	 *
	 * @param string $key
	 */
	public function __get($key)
	{
		switch (true)
		{
			case isset($this->_param[$key]):
				return $this->_param[$key];
			case isset($this->query->{$key}):
				return $this->query->{$key};
			case isset($this->post->{$key}):
				return $this->post->{$key};
			default:
{'fraction_non_alphanumeric': 0.12741539970573812, 'fraction_numerical': 0.0006866110838646396, 'mean_word_length': 3.4119428818693205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 96, 'https://': 0, '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': '6515421', 'n_tokens_mistral': 3363, 'n_tokens_neox': 3023, 'n_words': 1045}