Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add several missing functions and fix file_exists error #29

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions src/CI3Compatible/Core/CI_Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
use Kenjis\CI3Compatible\Exception\NotSupportedException;

class CI_Input
{

Check failure on line 20 in src/CI3Compatible/Core/CI_Input.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Opening brace must not be followed by a blank line

Check failure on line 20 in src/CI3Compatible/Core/CI_Input.php

View workflow job for this annotation

GitHub Actions / Coding Standards

There must be exactly 0 empty lines after class opening brace.

Check failure on line 21 in src/CI3Compatible/Core/CI_Input.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Whitespace found at end of line
/**
* IP address of the current user
*
* @var string

Check failure on line 25 in src/CI3Compatible/Core/CI_Input.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Spaces must be used for alignment; tabs are not allowed
*/
protected $ip_address = FALSE;

Check failure on line 27 in src/CI3Compatible/Core/CI_Input.php

View workflow job for this annotation

GitHub Actions / Coding Standards

TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE"

Check failure on line 28 in src/CI3Compatible/Core/CI_Input.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Whitespace found at end of line
/**
* Fetch an item from the GET array
*
Expand Down Expand Up @@ -62,6 +70,22 @@
);
}
}

/**
* Fetch an item from the COOKIE array
*
* @param mixed $index Index for item to be fetched from $_COOKIE
* @param bool $xss_clean Whether to apply XSS filtering
* @return mixed
*/
public function cookie($index = NULL, $xss_clean = false)
{
$this->checkXssClean($xss_clean);

$request = Services::request();

return $request->getCookie($index);
}

/**
* Fetch an item from the SERVER array
Expand All @@ -79,4 +103,179 @@

return $request->getServer($index);
}

/**
* Fetch the IP Address
*
* Determines and validates the visitor's IP address.
*
* @return string IP address
*/
public function ip_address()
{
if ($this->ip_address !== FALSE)
{
return $this->ip_address;
}

$config = config('App');
$proxy_ips = $config->proxyIPs;
if ( ! empty($proxy_ips) && ! is_array($proxy_ips))
{
$proxy_ips = explode(',', str_replace(' ', '', $proxy_ips));
}

$this->ip_address = $this->server('REMOTE_ADDR');

if ($proxy_ips)
{
foreach (array('HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_X_CLIENT_IP', 'HTTP_X_CLUSTER_CLIENT_IP') as $header)
{
if (($spoof = $this->server($header)) !== NULL)
{
// Some proxies typically list the whole chain of IP
// addresses through which the client has reached us.
// e.g. client_ip, proxy_ip1, proxy_ip2, etc.
sscanf($spoof, '%[^,]', $spoof);

if ( ! $this->valid_ip($spoof))
{
$spoof = NULL;
}
else
{
break;
}
}
}

if ($spoof)
{
for ($i = 0, $c = count($proxy_ips); $i < $c; $i++)
{
// Check if we have an IP address or a subnet
if (strpos($proxy_ips[$i], '/') === FALSE)
{
// An IP address (and not a subnet) is specified.
// We can compare right away.
if ($proxy_ips[$i] === $this->ip_address)
{
$this->ip_address = $spoof;
break;
}

continue;
}

// We have a subnet ... now the heavy lifting begins
isset($separator) OR $separator = $this->valid_ip($this->ip_address, 'ipv6') ? ':' : '.';

// If the proxy entry doesn't match the IP protocol - skip it
if (strpos($proxy_ips[$i], $separator) === FALSE)
{
continue;
}

// Convert the REMOTE_ADDR IP address to binary, if needed
if ( ! isset($ip, $sprintf))
{
if ($separator === ':')
{
// Make sure we're have the "full" IPv6 format
$ip = explode(':',
str_replace('::',
str_repeat(':', 9 - substr_count($this->ip_address, ':')),
$this->ip_address
)
);

for ($j = 0; $j < 8; $j++)
{
$ip[$j] = intval($ip[$j], 16);
}

$sprintf = '%016b%016b%016b%016b%016b%016b%016b%016b';
}
else
{
$ip = explode('.', $this->ip_address);
$sprintf = '%08b%08b%08b%08b';
}

$ip = vsprintf($sprintf, $ip);
}

// Split the netmask length off the network address
sscanf($proxy_ips[$i], '%[^/]/%d', $netaddr, $masklen);

// Again, an IPv6 address is most likely in a compressed form
if ($separator === ':')
{
$netaddr = explode(':', str_replace('::', str_repeat(':', 9 - substr_count($netaddr, ':')), $netaddr));
for ($j = 0; $j < 8; $j++)
{
$netaddr[$j] = intval($netaddr[$j], 16);
}
}
else
{
$netaddr = explode('.', $netaddr);
}

// Convert to binary and finally compare
if (strncmp($ip, vsprintf($sprintf, $netaddr), $masklen) === 0)
{
$this->ip_address = $spoof;
break;
}
}
}
}

if ( ! $this->valid_ip($this->ip_address))
{
return $this->ip_address = '0.0.0.0';
}

return $this->ip_address;
}

// --------------------------------------------------------------------

/**
* Validate IP Address
*
* @param string $ip IP address
* @param string $which IP protocol: 'ipv4' or 'ipv6'
* @return bool
*/
public function valid_ip($ip, $which = '')
{
switch (strtolower($which))
{
case 'ipv4':
$which = FILTER_FLAG_IPV4;
break;
case 'ipv6':
$which = FILTER_FLAG_IPV6;
break;
default:
$which = 0;
break;
}

return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
}

// --------------------------------------------------------------------

/**
* Fetch User Agent string
*
* @return string|null User Agent string or NULL if it doesn't exist
*/
public function user_agent($xss_clean = false)
{
return $this->server('HTTP_USER_AGENT', $xss_clean);
}
}
2 changes: 1 addition & 1 deletion src/CI3Compatible/Core/CI_Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected function _ci_load(array $_ci_data)
// Set the default data variables
$_ci_view = $_ci_data['_ci_view'] ?? false;
$_ci_vars = $_ci_data['_ci_vars'] ?? false;
$_ci_path = $_ci_data['_ci_path'] ?? false;
$_ci_path = $_ci_data['_ci_path'] ?? '';
$_ci_return = $_ci_data['_ci_return'] ?? false;

$file_exists = false;
Expand Down
5 changes: 5 additions & 0 deletions src/CI3Compatible/Core/CI_URI.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@
class CI_URI
{
// @TODO

Check failure on line 19 in src/CI3Compatible/Core/CI_URI.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Whitespace found at end of line
public function segment($segmentId, $no_result = ''){

Check failure on line 20 in src/CI3Compatible/Core/CI_URI.php

View workflow job for this annotation

GitHub Actions / Coding Standards

You must use "/**" style comments for a function comment

Check failure on line 20 in src/CI3Compatible/Core/CI_URI.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Opening brace should be on a new line
$uri = service('uri');
return $uri->getSegment($segmentId, $no_result);

Check failure on line 22 in src/CI3Compatible/Core/CI_URI.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Expected 1 line before "return", found 0.
}
}
Loading