<?php

namespace Core;

class Access
{
	public $policy;
	
	public $allow;
	
	public $deny;

	function __construct($policy = "")
	{
		$this->policy = $method;	
	}
	
	/**
	 * Return remote IP4/6 address from headers or default $_SERVER['REMOTE_ADDR']
	 * 
	 * @return string IP address
	 */
	static function getRemoteAddress()
	{
		global $Log;
		
		$array = array(
			'HTTP_CLIENT_IP',
			'HTTP_X_FORWARDED_FOR',
			'HTTP_X_FORWARDED',
			'HTTP_FORWARDED_FOR',
			'HTTP_FORWARDED',
			);

		foreach ($array as $_)
		{
			if (empty($_SERVER[$_])) 
				continue;
			
			if (filter_var($_SERVER[$_], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE))
			{
				if ($_SERVER[$_] == '127.0.0.1' || $_SERVER[$_] == '::1')
					continue;
				return $_SERVER[$_];
			}
			
			if (is_object($Log))
			{
				$Log->write("Invalid address \$_SERVER['$_'] = '{$_SERVER[$_]}'", "access");
			}
		}

		return $_SERVER['REMOTE_ADDR'];
	}
	
	function check($address = ".")
	{
		return true;
	}
}
