Remove http_request

This commit is contained in:
Romain Neutron
2013-11-12 17:41:54 +01:00
parent 5a4b9e5f5f
commit d7a68dbec3
4 changed files with 27 additions and 449 deletions

View File

@@ -443,7 +443,6 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
public function apply_rights()
{
$request = \http_request::getInstance();
$ACL = $this->app['acl']->get($this->app['authentication']->getUser());
$base_ids = array_keys($ACL->get_granted_base(array('canadmin')));
@@ -477,7 +476,7 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
}
$rights[$k] = $right . '_' . $base_id;
}
$parm = $request->get_parms_from_serialized_datas($rights, 'values');
$parm = $this->unserializedRequestData($this->app['request'], $rights, 'values');
foreach ($parm as $p => $v) {
if (trim($v) == '')
@@ -518,7 +517,7 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
$rights[$k] = $right . '_' . $databox->get_sbas_id();
}
$parm = $request->get_parms_from_serialized_datas($rights, 'values');
$parm = $this->unserializedRequestData($this->app['request'], $rights, 'values');
foreach ($parm as $p => $v) {
if (trim($v) == '')
@@ -577,8 +576,6 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
return $this;
}
$request = \http_request::getInstance();
$infos = array(
'gender'
, 'first_name'
@@ -594,7 +591,7 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
, 'fax'
);
$parm = $request->get_parms_from_serialized_datas($infos, 'user_infos');
$parm = $this->unserializedRequestData($this->app['request'], $infos, 'user_infos');
if ($parm['email'] && !\Swift_Validate::email($parm['email'])) {
throw new \Exception_InvalidArgument(_('Email addess is not valid'));
@@ -758,4 +755,18 @@ class Edit extends \Alchemy\Phrasea\Helper\Helper
$ACL->revoke_unused_sbas_rights();
}
}
private function unserializedRequestData(Request $request, array $indexes, $requestIndex)
{
$parameters = $data = array();
parse_str($request->get($requestIndex), $data);
if (count($data) > 0) {
foreach ($indexes as $index) {
$parameters[$index] = isset($data[$index]) ? $data[$index] : null;
}
}
return $parameters;
}
}

View File

@@ -1,261 +0,0 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
*
*
* @license http://opensource.org/licenses/gpl-3.0 GPLv3
* @link www.phraseanet.com
*/
class http_request
{
/**
* @todo enable filters
*/
// private static $_FILTER_IMPLEMENTED = extension_loaded;
//
// const VALIDATE_BOOLEAN = FILTER_VALIDATE_BOOLEAN;
// const VALIDATE_EMAIL = FILTER_VALIDATE_EMAIL;
// const VALIDATE_FLOAT = FILTER_VALIDATE_FLOAT;
// const VALIDATE_INT = FILTER_VALIDATE_INT;
// const VALIDATE_IP = FILTER_VALIDATE_IP;
// const VALIDATE_REGEXP = FILTER_VALIDATE_REGEXP;
// const VALIDATE_URL = FILTER_VALIDATE_URL;
//
// const SANITIZE_EMAIL = FILTER_SANITIZE_EMAIL;
// const SANITIZE_ENCODED = FILTER_SANITIZE_ENCODED;
// const SANITIZE_MAGIC_QUOTES = FILTER_SANITIZE_MAGIC_QUOTES;
// const SANITIZE_NUMBER_FLOAT = FILTER_SANITIZE_NUMBER_FLOAT;
const SANITIZE_NUMBER_INT = 'int';
// const SANITIZE_SPECIAL_CHARS = FILTER_SANITIZE_SPECIAL_CHARS;
const SANITIZE_STRING = 'string';
// const SANITIZE_STRIPPED = FILTER_SANITIZE_STRIPPED;
// const SANITIZE_URL = FILTER_SANITIZE_URL;
/**
*
* @var <type>
*/
private static $_instance;
/**
*
* @var boolean
*/
protected static $_cli_usage;
/**
*
* @var <type>
*/
protected $code;
/**
*
* @return http_request
*/
public static function getInstance()
{
if ( ! (self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
*
* @return http_request
*/
public function __construct()
{
return $this;
}
/**
*
* @return boolean
*/
public function is_ajax()
{
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return true;
}
return false;
}
public function is_secure()
{
return (
isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == 1)
);
}
public function comes_from_flash()
{
return (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/\bflash\b/i', $_SERVER['HTTP_USER_AGENT']) > 0);
}
/**
*
* @return int
*/
public function get_code()
{
if (is_null($this->code) && isset($_SERVER['REDIRECT_STATUS'])) {
$this->code = $_SERVER['REDIRECT_STATUS'];
}
return $this->code;
}
/**
*
* @param int $code
* @return http_request
*/
public function set_code($code)
{
$this->code = (int) $code;
return $this;
}
/**
*
* @return Array
*/
public function get_parms()
{
$parm = array();
$nargs = func_num_args();
if ($nargs == 1 && is_array(func_get_arg(0))) {
foreach (func_get_arg(0) as $key => $nom) {
if (is_string($key)) {
$value = isset($_GET[$key]) ?
$_GET[$key] : (isset($_POST[$key]) ? $_POST[$key] : NULL);
switch ($nom) {
case self::SANITIZE_NUMBER_INT:
$value = (int) $value;
break;
case self::SANITIZE_STRING:
$value = trim((string) $value);
break;
default:
break;
}
$parm[$key] = $value;
} else {
$parm[$nom] = isset($_GET[$nom]) ?
$_GET[$nom] : (isset($_POST[$nom]) ? $_POST[$nom] : NULL);
}
}
} else {
for ($i = 0; $i < $nargs; $i ++) {
$nom = func_get_arg($i);
$parm[$nom] = isset($_GET[$nom]) ?
$_GET[$nom] : (isset($_POST[$nom]) ? $_POST[$nom] : NULL);
}
}
return($parm);
}
/**
*
* @param array $indexes
* @param string $serializeds_datas_index
* @return array
*/
public function get_parms_from_serialized_datas(Array $indexes, $serializeds_datas_index)
{
$parm = array();
$tmp_parms = array();
if (isset($_GET[$serializeds_datas_index]))
parse_str($_GET[$serializeds_datas_index], $tmp_parms);
elseif (isset($_POST[$serializeds_datas_index]))
parse_str($_POST[$serializeds_datas_index], $tmp_parms);
if (count($tmp_parms) > 0) {
foreach ($indexes as $nom) {
$parm[$nom] = isset($tmp_parms[$nom]) ? $tmp_parms[$nom] : NULL;
}
}
return $parm;
}
/**
*
* @return boolean
*/
public function has_post_datas()
{
return ! empty($_POST);
}
/**
*
* @return Array
*/
public function get_post_datas()
{
return $_POST;
}
/**
*
* @return boolean
*/
public function has_get_datas()
{
return ! empty($_GET);
}
/**
*
* @return boolean
*/
public function has_datas()
{
return ($this->has_post_datas() || $this->has_get_datas());
}
/**
*
* @param mixed content $data
* @param const $filter
* @return mixed content
*/
public function filter($data, $filter)
{
return filter_var($data, $filter);
}
/**
* Tells wheter or not it's command line script
*
* @return boolean
*/
public static function is_command_line()
{
if (self::$_cli_usage === null) {
$sapi_name = strtolower(substr(php_sapi_name(), 0, 3));
self::$_cli_usage = ($sapi_name == 'cli');
}
return self::$_cli_usage;
}
}

View File

@@ -63,10 +63,12 @@ class ControllerUsersTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
$user = \User_Adapter::create(self::$DI['app'], $username, "test", $username . "@email.com", false);
$base_id = self::$DI['collection']->get_base_id();
$_GET['values'] = 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1';
$_GET['user_infos'] = "user_infos[email]=" . $user->get_email();
self::$DI['client']->request('POST', '/admin/users/rights/apply/', array('users' => $user->get_id()));
self::$DI['client']->request('POST', '/admin/users/rights/apply/', array(
'users' => $user->get_id(),
'values' => 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1',
'user_infos' => "user_infos[email]=" . $user->get_email(),
));
$response = self::$DI['client']->getResponse();
$this->assertTrue($response->isOK());
$this->assertEquals("application/json", $response->headers->get("content-type"));
@@ -87,9 +89,11 @@ class ControllerUsersTest extends \PhraseanetWebTestCaseAuthenticatedAbstract
$username = uniqid('user_');
$user = \User_Adapter::create(self::$DI['app'], $username, "test", $username . "@email.com", false);
$base_id = self::$DI['collection']->get_base_id();
$_GET['values'] = 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1';
$_GET['user_infos'] = "user_infos[email]=" . $user->get_email();
self::$DI['client']->request('POST', '/admin/users/rights/apply/', array('users' => $user->get_id()));
self::$DI['client']->request('POST', '/admin/users/rights/apply/', array(
'users' => $user->get_id(),
'values' => 'canreport_' . $base_id . '=1&manage_' . self::$DI['collection']->get_base_id() . '=1&canpush_' . self::$DI['collection']->get_base_id() . '=1',
'user_infos' => "user_infos[email]=" . $user->get_email(),
));
$response = self::$DI['client']->getResponse();
$this->assertTrue($response->isOK());
$this->assertEquals("application/json", $response->headers->get("content-type"));

View File

@@ -1,176 +0,0 @@
<?php
class http_requestTest extends PHPUnit_Framework_TestCase
{
/**
* @var http_request
*/
protected $object;
protected function setUp()
{
$this->object = new http_request();
}
public function testGetInstance()
{
$this->assertInstanceOf('http_request', http_request::getInstance());
}
public function testIs_ajax()
{
$this->assertFalse($this->object->is_ajax());
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
$this->assertTrue($this->object->is_ajax());
}
public function testComes_from_flash()
{
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
$this->assertFalse($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash Player';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Flash';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Flash Player';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Flash ';
$this->assertTrue($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = 'Flashs ';
$this->assertFalse($this->object->comes_from_flash());
$_SERVER['HTTP_USER_AGENT'] = $user_agent;
$this->assertFalse($this->object->comes_from_flash());
}
public function testGet_code()
{
$this->assertNull($this->object->get_code());
$_SERVER['REDIRECT_STATUS'] = 301;
$this->assertEquals(301, $this->object->get_code());
$this->object->set_code(580);
$this->assertEquals(580, $this->object->get_code());
$this->object->set_code('a');
$this->assertEquals(0, $this->object->get_code());
$this->object->set_code('a');
$this->assertEquals(0, $this->object->get_code());
}
public function testSet_code()
{
$this->object->set_code(302);
$this->assertEquals(302, $this->object->get_code());
}
public function testGet_parms()
{
$_GET = array('lili' => '25', 'popo' => array('tip', 'top'));
$_POST = array('Plili' => '25', 'Gpopo' => array('mtip', 'btop'));
$parm = $this->object->get_parms('lili', 'Plili', 'popo', 'Gpopo', 'notexists');
$this->assertEquals($_GET['lili'], $parm['lili']);
$this->assertEquals($_POST['Plili'], $parm['Plili']);
$this->assertEquals($_GET['popo'], $parm['popo']);
$this->assertEquals($_POST['Gpopo'], $parm['Gpopo']);
$this->assertNull($parm['notexists']);
$parm = $this->object->get_parms(
array(
'lili' => http_request::SANITIZE_NUMBER_INT
, 'Plili'
, 'popo'
, 'notexists' => http_request::SANITIZE_STRING
)
);
$this->assertEquals((int) $_GET['lili'], $parm['lili']);
$this->assertTrue(is_int($parm['lili']));
$this->assertEquals($_POST['Plili'], $parm['Plili']);
$this->assertEquals($_GET['popo'], $parm['popo']);
$this->assertEquals('', $parm['notexists']);
$_GET = $_POST = array();
}
public function testGet_parmsArraToString()
{
$_POST = array('Plili' => '25', 'Gpopo' => array('mtip', 'btop'));
try {
$this->object->get_parms(array('Gpopo' => http_request::SANITIZE_STRING));
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$this->fail('Should raise an error notice');
}
} catch (\PHPUnit_Framework_Error_Notice $e) {
}
$_GET = $_POST = array();
}
public function testGet_parms_from_serialized_datas()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testHas_post_datas()
{
$this->assertFalse($this->object->has_post_datas());
$_POST = array('TOPPy' => null);
$this->assertTrue($this->object->has_post_datas());
}
public function testGet_post_datas()
{
$post = $_POST = array('Plili' => '25', 'Gpopo' => array('mtip', 'btop'));
$this->assertEquals($post, $this->object->get_post_datas());
}
public function testHas_get_datas()
{
$this->assertFalse($this->object->has_get_datas());
$_GET = array('TOPPy' => null);
$this->assertTrue($this->object->has_get_datas());
}
public function testHas_datas()
{
$_POST = $_GET = array();
$this->assertFalse($this->object->has_datas());
$_POST = array('malal' => true);
$this->assertTrue($this->object->has_datas());
$_GET = array('malal' => true);
$_POST = array();
$this->assertTrue($this->object->has_datas());
$_GET = array('malal' => true);
$_POST = array('malal' => true);
$this->assertTrue($this->object->has_datas());
$_POST = $_GET = array();
$this->assertFalse($this->object->has_datas());
}
/**
* @todo Implement testFilter().
*/
public function testFilter()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
public function testIs_command_line()
{
$this->assertTrue($this->object->is_command_line());
}
}