<?php
defined('_JEXEC') or die;
define('OS_INPUT_ALLOWRAW', 2);
define('OS_INPUT_ALLOWHTML', 4);

use Joomla\CMS\Filter\InputFilter;
use Joomla\Input\Input;
/**
 * Extends JInput class to allow getting raw data from Input object. This can be removed when we don't provide support for Joomla 2.5.x
 *
 * @author        Ossolution Team
 * @package       OS
 * @subpackage    Controller
 */
class OSInput extends Input
{
	/**
	 * Constructor.
	 *
	 * @param array $source  Source data (Optional, default is $_REQUEST)
	 * @param array $options Array of configuration parameters (Optional)
	 *
	 */

	protected $input;

	public function __construct($source = null, array $options = [])
	{
		if ($source instanceof Input)
		{
			$reflection = new ReflectionClass($source);
			$property   = $reflection->getProperty('data');
			$property->setAccessible(true);
			$source = $property->getValue($source);
		}
		if (!isset($options['filter']))
		{
			//Set default filter so that getHtml can be returned properly
			if (version_compare(JVERSION, '4.0.0-dev', 'ge'))
			{
				//Set default filter so that getHtml can be returned properly
				$options['filter'] = InputFilter::getInstance([], [], 1, 1);
			}
			else
			{
				$options['filter'] = InputFilter::getInstance(null, null, 1, 1);
			}
		}

		parent::__construct($source, $options);

		//$this->data = self::stripSlashesRecursive($this->data);
	}

	/**
	 * Magic method to get an input object
	 *
	 * @param   mixed  $name  Name of the input object to retrieve.
	 *
	 * @return  JInput  The request input object
	 *
	 * @since   11.1
	 */
	public function __get($name)
	{
		/*
		if (isset($this->inputs[$name]))
		{
			return $this->inputs[$name];
		}

		$className = '\\Joomla\\CMS\\Input\\' . ucfirst($name);

		if (class_exists($className))
		{
			$this->inputs[$name] = new $className(null, $this->options);

			return $this->inputs[$name];
		}
		*/
		$allowedGlobals = ['REQUEST', 'GET', 'POST', 'FILES', 'SERVER', 'ENV'];

		$superGlobal = '_' . strtoupper($name);

		if (\in_array(strtoupper($name), $allowedGlobals, true) && isset($GLOBALS[$superGlobal]))
		{
			$this->inputs[$name] = new OSInput($GLOBALS[$superGlobal], $this->options);

			//return $this->inputs[$name];
		}
		
		return parent::__get($name);

	}

	/**
	 * Check to see if a variable is a vaialble in the input or not
	 *
	 * @param string $name the variable name
	 *
	 * @return boolean
	 */
	public function has($name)
	{
		if (isset($this->data[$name]))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Set data for the input, usually used to set validated data back to input for further processing
	 *
	 * @param $data
	 */
	public function setData($data)
	{
		parent::set($name, $value);

		// Store change back to the original Joomla Input object if provided
		if ($this->input)
		{
			$this->input->set($name, $value);
		}
	}
	/**
	 * Get the row data from input
	 *
	 * @return array
	 */
	public function getData($mask = OS_INPUT_ALLOWHTML)
	{
		if ($mask & 2)
		{
			return $this->data;
		}

		return $this->filter->clean($this->data, '');
	}

	protected static function stripSlashesRecursive($value)
	{
		$value = is_array($value) ? array_map(array('OSInput', 'stripSlashesRecursive'), $value) : stripslashes($value);

		return $value;
	}
}
