Move get_ip_address() and get_user_agent() to Request namespace

This commit is contained in:
Frederic Guillot 2016-08-17 21:27:05 -04:00
parent 38b08719da
commit 55e4ab882c
No known key found for this signature in database
GPG Key ID: 92D77191BA7FBC99
8 changed files with 70 additions and 54 deletions

View File

@ -1,3 +1,6 @@
git:
depth: 3
language: php
sudo: false

View File

@ -1,8 +1,8 @@
[![Build Status](https://travis-ci.org/miniflux/miniflux.svg?branch=master)](https://travis-ci.org/miniflux/miniflux)
Miniflux - Minimalist News Reader
=================================
[![Build Status](https://travis-ci.org/miniflux/miniflux.svg?branch=master)](https://travis-ci.org/miniflux/miniflux)
Miniflux is a minimalist and web-based RSS reader.
Features

View File

@ -2,6 +2,12 @@
namespace Request;
function get_server_variable($variable)
{
return isset($_SERVER[$variable]) ? $_SERVER[$variable] : '';
}
function param($name, $default_value = null)
{
return isset($_GET[$name]) ? $_GET[$name] : $default_value;
@ -53,7 +59,6 @@ function file_content($field)
}
function uri()
{
return $_SERVER['REQUEST_URI'];
@ -64,3 +69,35 @@ function is_post()
{
return $_SERVER['REQUEST_METHOD'] === 'POST';
}
function get_user_agent()
{
return get_server_variable('HTTP_USER_AGENT') ?: t('Unknown');
}
function get_ip_address()
{
$keys = array(
'HTTP_X_REAL_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
);
foreach ($keys as $key) {
$value = get_server_variable($key);
if ($value !== '') {
foreach (explode(',', $value) as $ip_address) {
return trim($ip_address);
}
}
}
return t('Unknown');
}

View File

@ -2,36 +2,6 @@
namespace Helper;
// Get the real IP address of the connected user
function get_ip_address()
{
$keys = array(
'HTTP_X_REAL_IP',
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'REMOTE_ADDR'
);
foreach ($keys as $key) {
$value = get_server_variable($key);
if ($value !== '') {
foreach (explode(',', $value) as $ip_address) {
return trim($ip_address);
}
}
}
return t('Unknown');
}
function get_server_variable($variable)
{
return isset($_SERVER[$variable]) ? $_SERVER[$variable] : '';
}
function is_secure_connection()
{

View File

@ -363,9 +363,3 @@ function reload()
$_SESSION['config'] = get_all();
Translator\load(get('language'));
}
// Get the user agent of the connected user
function get_user_agent()
{
return empty($_SERVER['HTTP_USER_AGENT']) ? t('Unknown') : $_SERVER['HTTP_USER_AGENT'];
}

View File

@ -6,7 +6,7 @@ use SimpleValidator\Validator;
use SimpleValidator\Validators;
use PicoDb\Database;
use Session;
use Helper;
use Request;
use Model\Config;
use Model\RememberMe;
use Model\Database as DatabaseModel;
@ -62,7 +62,7 @@ function validate_login(array $values)
// Setup the remember me feature
if (! empty($values['remember_me'])) {
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Helper\get_ip_address(), Config\get_user_agent());
$cookie = RememberMe\create(DatabaseModel\select(), $values['username'], Request\get_ip_address(), Request\get_user_agent());
RememberMe\write_cookie($cookie['token'], $cookie['sequence'], $cookie['expiration']);
}
} else {

View File

@ -1,13 +0,0 @@
<?php
class HelperTest extends PHPUnit_Framework_TestCase
{
public function testGetIpAddress()
{
$_SERVER = array('HTTP_X_REAL_IP' => '127.0.0.1');
$this->assertEquals('127.0.0.1', Helper\get_ip_address());
$_SERVER = array('HTTP_FORWARDED_FOR' => ' 127.0.0.1, 192.168.0.1');
$this->assertEquals('127.0.0.1', Helper\get_ip_address());
}
}

View File

@ -0,0 +1,25 @@
<?php
class RequestTest extends PHPUnit_Framework_TestCase
{
public function testGetIpAddress()
{
$_SERVER = array('HTTP_X_REAL_IP' => '127.0.0.1');
$this->assertEquals('127.0.0.1', Request\get_ip_address());
$_SERVER = array('HTTP_FORWARDED_FOR' => ' 127.0.0.1, 192.168.0.1');
$this->assertEquals('127.0.0.1', Request\get_ip_address());
$_SERVER = array();
$this->assertEquals('Unknown', Request\get_ip_address());
}
public function testGetUserAgent()
{
$_SERVER = array();
$this->assertEquals('Unknown', Request\get_user_agent());
$_SERVER = array('HTTP_USER_AGENT' => 'foobar');
$this->assertEquals('foobar', Request\get_user_agent());
}
}