diff --git a/src/di.php b/src/di.php index 9b80acbc9..30bc2a862 100644 --- a/src/di.php +++ b/src/di.php @@ -367,7 +367,7 @@ $di['twig'] = $di->factory(function () use ($di) { $di['is_client_logged'] = function () use ($di) { if (!$di['auth']->isClientLoggedIn()) { $api_str = '/api/'; - $url = $di['request']->getQuery('_url'); + $url = $_GET['_url'] ?? null; if (0 === strncasecmp($url, $api_str, strlen($api_str))) { // Throw Exception if api request @@ -395,7 +395,7 @@ $di['is_client_logged'] = function () use ($di) { $di['is_admin_logged'] = function () use ($di) { if (!$di['auth']->isAdminLoggedIn()) { $api_str = '/api/'; - $url = $di['request']->getQuery('_url', null, ''); + $url = $_GET['_url'] ?? ''; if (0 === strncasecmp($url, $api_str, strlen($api_str))) { // Throw Exception if api request throw new Exception('Admin is not logged in'); @@ -739,7 +739,7 @@ $di['translate'] = $di->protect(function ($textDomain = '') use ($di) { /* * Creates a CSV export of data from a specified table and sends it to the browser. - * + * * @param string $table Name of the table to export data from * @param string $outputName Name of the exported CSV file * @param array $headers Optional array of column headers for the CSV file diff --git a/src/index.php b/src/index.php index 133656ac8..5a106420f 100644 --- a/src/index.php +++ b/src/index.php @@ -1,5 +1,4 @@ getQuery('_url') ?? ''; -$http_err_code = $di['request']->getQuery('_errcode') ?? null; +$url = $_GET['_url'] ?? ''; +$http_err_code = $_GET['_errcode'] ?? ''; $admin_prefix = $di['config']['admin_area_prefix']; if (0 === strncasecmp($url, $admin_prefix, strlen($admin_prefix))) { diff --git a/src/library/Box/App.php b/src/library/Box/App.php index c9ceaba3d..1badd90d6 100644 --- a/src/library/Box/App.php +++ b/src/library/Box/App.php @@ -265,7 +265,8 @@ class Box_App */ protected function checkAllowedURLs() { - $REQUEST_URI = $this->di['request']->getServer('REQUEST_URI'); + $REQUEST_URI = $_SERVER['REQUEST_URI'] ?? null; + $allowedURLs = $this->di['config']['maintenance_mode']['allowed_urls']; $rootUrl = $this->di['config']['url']; @@ -323,7 +324,7 @@ class Box_App */ protected function checkAdminPrefix() { - $REQUEST_URI = $this->di['request']->getServer('REQUEST_URI'); + $REQUEST_URI = $_SERVER['REQUEST_URI'] ?? null; $adminPrefix = $this->di['config']['admin_area_prefix']; $rootUrl = $this->di['config']['url']; diff --git a/src/library/Box/Pagination.php b/src/library/Box/Pagination.php index dc628ae4d..e016bb033 100644 --- a/src/library/Box/Pagination.php +++ b/src/library/Box/Pagination.php @@ -41,9 +41,9 @@ class Box_Pagination implements InjectionAwareInterface public function getSimpleResultSet($q, $values, $per_page = 100, $page = null) { if (is_null($page)){ - $page = $this->di['request']->getQuery('page', "int", 1); + $page = $_GET['page'] ?? 1; } - $per_page = $this->di['request']->getQuery('per_page', "int", $per_page); + $per_page = $_GET['per_page'] ?? $per_page; $offset = ($page - 1) * $per_page; @@ -67,8 +67,8 @@ class Box_Pagination implements InjectionAwareInterface public function getAdvancedResultSet($q, $values, $per_page = 100) { - $page = $this->di['request']->getQuery('page', "int", 1); - $per_page = $this->di['request']->getQuery('per_page', "int", $per_page); + $page = $page = $_GET['page'] ?? 1; + $per_page = $_GET['per_page'] ?? $per_page; $offset = ($page - 1) * $per_page; $q = str_replace('SELECT ', 'SELECT SQL_CALC_FOUND_ROWS ', $q); diff --git a/src/library/Box/Request.php b/src/library/Box/Request.php index dfd43004f..483d68f39 100644 --- a/src/library/Box/Request.php +++ b/src/library/Box/Request.php @@ -9,29 +9,12 @@ * Copyright BoxBilling, Inc 2011-2021 * * This source file is subject to the Apache-2.0 License that is bundled - * with this source code in the file LICENSE + * with this source code in the file LICENSE. */ - class Box_Request implements \Box\InjectionAwareInterface { protected $di; - protected $_data = array(); - protected $_header = array(); - protected $_server = array(); - protected $_request = array(); - protected $_post = array(); - protected $_get = array(); - protected $_cookie = array(); - protected $_method = 'post'; - - public function __construct() - { - $this->_get = $_GET; - $this->_post = $_POST; - $this->_request = $_REQUEST; - $this->_server = $_SERVER; - } public function setDi($di) { @@ -43,212 +26,6 @@ class Box_Request implements \Box\InjectionAwareInterface return $this->di; } - public function get($name = null, $filters = null, $defaultValue = null) - { - if (empty($name)) { - return $this->_request; - } - $value = isset($this->_request[$name]) ? $this->_request[$name] : $defaultValue; - return $this->_filterValue($value, $filters); - } - - public function getPost($name = null, $filters = null, $defaultValue = null) - { - if (empty($name)) { - return $this->_post; - } - $value = isset($this->_post[$name]) ? $this->_post[$name] : $defaultValue; - return $this->_filterValue($value, $filters); - } - - public function getQuery($name = null, $filters = null, $defaultValue = null) - { - if (empty($name)) { - return $this->_get; - } - - $value = isset($this->_get[$name]) ? $this->_get[$name] : $defaultValue; - return $this->_filterValue($value, $filters); - } - - /** - * Returns the available headers in the request - * @return array - */ - public function getHeaders() - { - $list = getallheaders(); - return is_array($list) ? $list : array(); - } - - public function getPut($name = null, $filters = null, $defaultValue = null) - { - parse_str($this->getRawBody(), $put); - if(empty($name)) { - return $put; - } - - $value = isset($put[$name]) ? $put[$name] : $defaultValue; - return $this->_filterValue($value, $filters); - } - - /** - * @param string $name - */ - public function getServer($name) - { - return isset($this->_server[$name]) ? $this->_server[$name] : null; - } - - public function has($name) - { - return isset($this->_request[$name]); - } - - public function hasPost($name) - { - return isset($this->_post[$name]); - } - - public function hasPut($name) - { - parse_str($this->getRawBody(), $put); - if(is_array($put) && isset($put[$name])) { - return true; - } - return false; - } - - public function hasQuery($name) - { - return isset($this->_get[$name]); - } - - /** - * @param string $name - */ - public function hasServer($name) - { - return isset($this->_server[$name]); - } - - /** - * Gets HTTP header from request data - * @param string $header - * @return string - */ - public function getHeader($header) - { - $headers = $this->getHeaders(); - return isset($headers[$header]) ? $headers[$header] : null; - } - - /** - * Gets HTTP schema (http/https) - * @return string - */ - public function getScheme() - { - return $this->getServer('HTTPS') ? 'https' : 'http'; - } - - /** - * Checks whether request has been made using ajax. Checks if $_SERVER[‘HTTP_X_REQUESTED_WITH’]==’XMLHttpRequest’ - * @return bool - */ - public function isAjax() - { - return ($this->getServer('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'); - } - - /** - * Checks whether request has been made using SOAP - * @return bool - */ - public function isSoapRequested() - { - if($this->hasServer('HTTP_SOAPACTION')) { - return true; - } - - if($this->getServer('CONTENT_TYPE') == 'application/soap+xml') { - return true; - } - - return false; - } - - /** - * Checks whether request has been made using any secure layer - * @return bool - */ - public function isSecureRequest() - { - return ($this->getScheme() == 'https'); - } - - /** - * Gets HTTP raw request body - * @return string - */ - public function getRawBody() - { - return file_get_contents('php://input'); - } - - /** - * Gets decoded JSON HTTP raw request body - * @return string - */ - public function getJsonRawBody() - { - $result = json_decode($this->getRawBody(), 1); - if($result) { - return $result; - } - - $_messages = array( - JSON_ERROR_NONE => 'No error has occurred', - JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', - JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', - JSON_ERROR_SYNTAX => 'Syntax error', - JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' - ); - $error = json_last_error(); - $msg = isset($_messages[$error]) ? $_messages[$error] : 'Error decoding json'; - throw new RuntimeException($msg); - } - - /** - * Gets active server address IP - * @return string - */ - public function getServerAddress() - { - $address = $this->getServer('SERVER_ADDR'); - return ($address) ? $address : '127.0.0.1'; - } - - /** - * Gets active server name - * @return string - */ - public function getServerName() - { - $address = $this->getServer('SERVER_NAME'); - return ($address) ? $address : 'localhost'; - } - - /** - * Gets information about schema, host and port used by the request - * @return string - */ - public function getHttpHost() - { - return $this->getServer('HTTP_HOST'); - } - /** * Gets most possible client IPv4 Address. This method search in $_SERVER[‘REMOTE_ADDR’] and optionally in $_SERVER[‘HTTP_X_FORWARDED_FOR’] * @param bool $trustForwardedHeader - No by default because this can be changed to anything extremely easy, making it unreliable for tracking and adding a potential source for external data to be executed. @@ -259,10 +36,10 @@ class Box_Request implements \Box\InjectionAwareInterface { $address = null; if($trustForwardedHeader) { - $address = $this->getServer('HTTP_X_FORWARDED_FOR'); + $address = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null; } if(is_null($address)) { - $address = $this->getServer('REMOTE_ADDR'); + $address = $_SERVER['REMOTE_ADDR'] ?? null; } if(is_string($address)) { if(strpos($address, ',') !== false) { @@ -272,115 +49,6 @@ class Box_Request implements \Box\InjectionAwareInterface return $address; } - /** - * Gets HTTP method which request has been made - * @return string - */ - public function getMethod() - { - return $this->getServer('REQUEST_METHOD'); - } - - /** - * Gets HTTP URI which request has been made - * @return string - */ - public function getURI() - { - return $this->getServer('REQUEST_URI'); - } - - /** - * Gets HTTP user agent used to made the request - * @return string - */ - public function getUserAgent() - { - return $this->getServer('HTTP_USER_AGENT'); - } - - /** - * Check if HTTP method match any of the passed methods - * @param string | array - * @return bool - */ - public function isMethod($methods) - { - $current = $this->getMethod(); - if(is_array($methods)) { - foreach($methods as $method) { - if($current == $method) { - return true; - } - } - return false; - } - return ($current == $methods); - } - - /** - * Checks whether HTTP method is POST. if $_SERVER[‘REQUEST_METHOD’]==’POST’ - * @return bool - */ - public function isPost() - { - return ($this->getMethod() == 'POST'); - } - - /** - * Checks whether HTTP method is GET. if $_SERVER[‘REQUEST_METHOD’]==’GET’ - * @return bool - */ - public function isGet() - { - return ($this->getMethod() == 'GET'); - } - - /** - * Checks whether HTTP method is PUT. if $_SERVER[‘REQUEST_METHOD’]==’PUT’ - * @return bool - */ - public function isPut() - { - return ($this->getMethod() == 'PUT'); - } - - /** - * Checks whether HTTP method is PATCH. if $_SERVER[‘REQUEST_METHOD’]==’PATCH’ - * @return bool - */ - public function isPatch() - { - return ($this->getMethod() == 'PATCH'); - } - - /** - * Checks whether HTTP method is HEAD. if $_SERVER[‘REQUEST_METHOD’]==’HEAD’ - * @return bool - */ - public function isHead() - { - return ($this->getMethod() == 'HEAD'); - } - - /** - * Checks whether HTTP method is DELETE. if $_SERVER[‘REQUEST_METHOD’]==’DELETE’ - * @return bool - */ - public function isDelete() - { - return ($this->getMethod() == 'DELETE'); - } - - /** - * Checks whether HTTP method is OPTIONS. if $_SERVER[‘REQUEST_METHOD’]==’OPTIONS’ - * @return bool - */ - public function isOptions() - { - return ($this->getMethod() == 'OPTIONS'); - } - /** * Checks whether request includes attached files * @return int - number of files @@ -417,185 +85,4 @@ class Box_Request implements \Box\InjectionAwareInterface } return $files; } - - /** - * Gets the web page that refers the active request. ie: https://www.google.com - * @return string - */ - public function getHTTPReferer() - { - return $this->getServer('HTTP_REFERER'); - } - - /** - * Gets array with mime/types and their quality accepted by the browser/client from $_SERVER[‘HTTP_ACCEPT’] - * @return array - */ - public function getAcceptableContent() - { - return $this->getServer('HTTP_ACCEPT'); - } - - /** - * Gets best mime/type accepted by the browser/client from $_SERVER[‘HTTP_ACCEPT’] - * @return array - */ - public function getBestAccept() - { - //@todo - } - - /** - * Gets charsets array and their quality accepted by the browser/client from $_SERVER[‘HTTP_ACCEPT_CHARSET’] - * @return array - */ - public function getClientCharsets() - { - //@todo - } - - /** - * Gets best charset accepted by the browser/client from $_SERVER[‘HTTP_ACCEPT_CHARSET’] - * @return string - */ - public function getBestCharset() - { - //@todo - } - - /** - * Gets languages array and their quality accepted by the browser/client from $_SERVER[‘HTTP_ACCEPT_LANGUAGE’] - * @return array - */ - public function getLanguages() - { - $language_header = $this->getServer('HTTP_ACCEPT_LANGUAGE'); - return $this->_getQualityHeader($language_header, 'language'); - } - - /** - * Gets best language accepted by the browser/client from $_SERVER[‘HTTP_ACCEPT_LANGUAGE’] - * @return string - */ - public function getBestLanguage() - { - //@todo - } - - /** - * Gets auth info accepted by the browser/client from $_SERVER[‘PHP_AUTH_USER’] - * @return array - */ - public function getBasicAuth() - { - $username = null; - $password = null; - - if ($this->hasServer('PHP_AUTH_USER')) { - $username = $_SERVER['PHP_AUTH_USER']; - $password = $_SERVER['PHP_AUTH_PW']; - } elseif ($this->hasServer('HTTP_AUTHENTICATION')) { - $auth = $this->getServer('HTTP_AUTHENTICATION'); - if (strpos(strtolower($auth), 'basic') === 0) { - list($username,$password) = explode(':',base64_decode(substr($auth, 6))); - } - } - - return array( - 'username' => $username, - 'password' => $password, - ); - } - - /** - * Gets auth info accepted by the browser/client from $_SERVER[‘PHP_AUTH_DIGEST’] - * @return array - */ - public function getDigestAuth() - { - // mod_php - if ($this->hasServer('PHP_AUTH_DIGEST')) { - $digest = $this->getServer('PHP_AUTH_DIGEST'); - // most other servers - } elseif ($this->hasServer('HTTP_AUTHENTICATION')) { - $auth = $this->getServer('HTTP_AUTHENTICATION'); - if (strpos(strtolower($auth),'digest')===0) { - $digest = substr($auth, 7); - } - } - - // protect against missing data - $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1); - $data = array(); - $keys = implode('|', array_keys($needed_parts)); - - preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $digest, $matches, PREG_SET_ORDER); - - foreach ((array) $matches as $m) { - $data[$m[1]] = $m[3] ? $m[3] : $m[4]; - unset($needed_parts[$m[1]]); - } - - return $needed_parts ? false : $data; - } - - /** - * Process a request header and return an array of values with their qualities - * @param string $q - * @return array - */ - protected function _getQualityHeader($header, $q) - { - $return = array(); - $parts = preg_split('/,\\s*/', $header); - - foreach($parts as $part) { - $headerParts = explode(';', $part); - if(isset($headerParts[1]) === true) { - $quality = substr($headerParts[1], 2); - } else { - $quality = 1; - } - - $return[] = array($q => $headerParts[0], 'quality' => $quality); - } - - return $return; - } - - /** - * Process a request header and return the one with best quality - * @return string - */ - protected function _getBestQuality($header) - { - //@todo - } - - /** - * @todo implement filtering - * @param $value - value to filter - * @param string|array $filters - list of filter to apply on value - */ - private function _filterValue($value, $filters) - { - return $value; - } -} - - -if (!function_exists('getallheaders')) -{ - function getallheaders() - { - $headers = ''; - foreach ($_SERVER as $name => $value) - { - if (substr($name, 0, 5) == 'HTTP_') - { - $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; - } - } - return $headers; - } } diff --git a/src/library/Box/Requirements.php b/src/library/Box/Requirements.php index aed99dd91..5877272d2 100644 --- a/src/library/Box/Requirements.php +++ b/src/library/Box/Requirements.php @@ -65,7 +65,7 @@ class Box_Requirements implements \Box\InjectionAwareInterface public function getInfo() { $data = array(); - $data['ip'] = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null; + $data['ip'] = $_SERVER['SERVER_ADDR'] ?? null; $data['PHP_OS'] = PHP_OS; $data['PHP_VERSION'] = PHP_VERSION; diff --git a/src/load.php b/src/load.php index 29d1c6961..6a1ca0a96 100644 --- a/src/load.php +++ b/src/load.php @@ -345,7 +345,7 @@ ini_set('html_errors', false); ini_set('error_log', PATH_LOG . '/php_error.log'); $isApache = (function_exists('apache_get_version')) ? true : false; -$serverSoftware = (isset($_SERVER['SERVER_SOFTWARE'])) ? $_SERVER['SERVER_SOFTWARE'] : ''; +$serverSoftware = ($_SERVER['SERVER_SOFTWARE']) ?? ''; if ($isApache or (false !== stripos($serverSoftware, 'apache')) or (false !== stripos($serverSoftware, 'litespeed'))) { if (!file_exists(PATH_ROOT . '/.htaccess')) { diff --git a/src/modules/Api/Service.php b/src/modules/Api/Service.php index 37e8f0e7e..592d3c313 100644 --- a/src/modules/Api/Service.php +++ b/src/modules/Api/Service.php @@ -48,7 +48,7 @@ class Service implements \Box\InjectionAwareInterface '; $values = [ 'ip' => $request->getClientAddress(), - 'request' => $request->getURI(), + 'request' => $_SERVER['REQUEST_URI'] ?? null, ]; return $this->di['db']->exec($sql, $values); diff --git a/src/modules/Client/Controller/Admin.php b/src/modules/Client/Controller/Admin.php index fa66e34a9..2898d6419 100644 --- a/src/modules/Client/Controller/Admin.php +++ b/src/modules/Client/Controller/Admin.php @@ -121,8 +121,10 @@ class Admin implements \Box\InjectionAwareInterface $api->client_login(['id' => $id]); $redirect_to = '/'; - if ($this->di['request']->getQuery('r')) { - $r = $this->di['request']->getQuery('r'); + + $query = $_GET['r'] ?? null; + if ($query) { + $r = $query; $redirect_to = '/' . trim($r, '/'); } diff --git a/src/modules/Invoice/Controller/Client.php b/src/modules/Invoice/Controller/Client.php index 55ac87c0b..8e93c4fd6 100644 --- a/src/modules/Invoice/Controller/Client.php +++ b/src/modules/Invoice/Controller/Client.php @@ -93,7 +93,8 @@ class Client implements \Box\InjectionAwareInterface { $api = $this->di['api_guest']; $data = [ - 'subscription' => $this->di['request']->getQuery('subscription'), + + 'subscription' => $_GET['subscription'] ?? null, 'hash' => $hash, 'gateway_id' => $id, 'auto_redirect' => true, diff --git a/src/modules/Order/Controller/Admin.php b/src/modules/Order/Controller/Admin.php index f352ebf25..008e56efa 100644 --- a/src/modules/Order/Controller/Admin.php +++ b/src/modules/Order/Controller/Admin.php @@ -85,8 +85,9 @@ class Admin implements \Box\InjectionAwareInterface public function get_new(\Box_App $app) { $api = $this->di['api_admin']; - $product = $api->product_get(['id' => $this->di['request']->getPost('product_id')]); - $client = $api->client_get(['id' => $this->di['request']->getPost('client_id')]); + + $product = $api->product_get(['id' => $_POST['product_id'] ?? null]); + $client = $api->client_get(['id' => $_POST['client_id'] ?? null]); return $app->render('mod_order_new', ['product' => $product, 'client' => $client]); } diff --git a/src/modules/Servicelicense/Server.php b/src/modules/Servicelicense/Server.php index 17d5de1ae..fdbdd6a2c 100644 --- a/src/modules/Servicelicense/Server.php +++ b/src/modules/Servicelicense/Server.php @@ -50,7 +50,7 @@ class Server implements \Box\InjectionAwareInterface return $_SERVER; } - return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default; + return $_SERVER[$key] ?? $default; } private function getIp($checkProxy = true) diff --git a/src/modules/System/Api/Guest.php b/src/modules/System/Api/Guest.php index 9cdb666f2..5bc7dd57f 100644 --- a/src/modules/System/Api/Guest.php +++ b/src/modules/System/Api/Guest.php @@ -152,7 +152,7 @@ class Guest extends \Api_Abstract */ public function current_url() { - return $this->di['request']->getURI(); + return $_SERVER['REQUEST_URI'] ?? null; } /** diff --git a/src/modules/System/Service.php b/src/modules/System/Service.php index c71024f02..dffdf023e 100644 --- a/src/modules/System/Service.php +++ b/src/modules/System/Service.php @@ -379,22 +379,17 @@ class Service public function getCurrentUrl() { - $request = $this->di['request']; - $pageURL = 'http'; - $https = $request->getScheme(); - if (isset($https) && 'https' == $https) { - $pageURL .= 's'; - } - $pageURL .= '://'; + $pageScheme = $_SERVER['HTTPS'] ? 'https' : 'http'; + $pageURL = $pageScheme . '://'; - $serverPort = $request->getServer('SERVER_PORT'); + $serverPort = $_SERVER['SERVER_PORT'] ?? null; if (isset($serverPort) && '80' != $serverPort && '443' != $serverPort) { - $pageURL .= $request->getServer('SERVER_NAME') . ':' . $request->getServer('SERVER_PORT'); + $pageURL .= $_SERVER['SERVER_NAME'] ?? null . ':' . $serverPort; } else { - $pageURL .= $request->getServer('SERVER_NAME'); + $pageURL .= $_SERVER['SERVER_NAME'] ?? null;; } - $this_page = $request->getURI(); + $this_page = $_SERVER['REQUEST_URI'] ?? ''; if (str_contains($this_page, '?')) { $a = explode('?', $this_page); $this_page = reset($a); diff --git a/tests/integration/bb-library/Box/DiTest.php b/tests/integration/bb-library/Box/DiTest.php index 965766901..4bead7e45 100644 --- a/tests/integration/bb-library/Box/DiTest.php +++ b/tests/integration/bb-library/Box/DiTest.php @@ -23,7 +23,6 @@ class DiTest extends PHPUnit\Framework\TestCase $this->assertInstanceOf('Box_EventManager', $di['events_manager']); $this->assertInstanceOf('Box_Session', $di['session']); - $this->assertInstanceOf('Box_Request', $di['request']); $this->assertInstanceOf('FileCache', $di['cache']); $this->assertInstanceOf('Box_Authorization', $di['auth']); $this->assertInstanceOf('Twig\Environment', $di['twig']); diff --git a/tests/library/Box/Box_RequestTest.php b/tests/library/Box/Box_RequestTest.php deleted file mode 100644 index 3dc1d1877..000000000 --- a/tests/library/Box/Box_RequestTest.php +++ /dev/null @@ -1,446 +0,0 @@ -setDi(2); - $res = $request->getDi(); - $this->assertEquals(2, $res); - } - - public function testGet() - { - $r = array('key'=>'value'); - $_REQUEST = $r; - $request = new Box_Request(); - $full_request = $request->get(); - $this->assertEquals($r, $full_request); - - $res = $request->get('key'); - $this->assertEquals('value', $res); - - $res = $request->get('non_existing', null, 'default'); - $this->assertEquals('default', $res); - } - - public function testPost() - { - $r = array('key'=>'value'); - $_POST = $r; - $request = new Box_Request(); - $full_request = $request->getPost(); - $this->assertEquals($r, $full_request); - - $res = $request->getPost('key'); - $this->assertEquals('value', $res); - - $res = $request->getPost('non_existing', null, 'default'); - $this->assertEquals('default', $res); - } - - public function testQuery() - { - $r = array('key'=>'value'); - $_GET = $r; - $request = new Box_Request(); - $full_request = $request->getQuery(); - $this->assertEquals($r, $full_request); - - $res = $request->getQuery('key'); - $this->assertEquals('value', $res); - - $res = $request->getQuery('non_existing', null, 'default'); - $this->assertEquals('default', $res); - } - - public function testServer() - { - $_SERVER['key'] = 'value'; - $request = new Box_Request(); - - $res = $request->getServer('key'); - $this->assertEquals('value', $res); - - $res = $request->getServer('non_existing'); - $this->assertNull($res); - } - - public function testPut() - { - $input = 'example=1&foo=bar'; - - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getRawBody')) - ->getMock(); - $request->expects($this->atLeastOnce()) - ->method('getRawBody') - ->will($this->returnValue($input)); - - $full_request = $request->getPut(); - $this->assertEquals(array('example'=>'1', 'foo'=>'bar'), $full_request); - - $res = $request->getPut('foo'); - $this->assertEquals('bar', $res); - - $res = $request->getPut('non_existing', null, 'default'); - $this->assertEquals('default', $res); - } - - public function testRawBody() - { - $request = new Box_Request(); - $full_request = $request->getRawBody(); - $this->assertIsString($full_request); - } - - public function testHeaders() - { - $request = new Box_Request(); - $headers = $request->getHeaders(); - $this->assertIsArray($headers); - } - - public function testGetMethod() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - $request->expects($this->atLeastOnce()) - ->method('getServer') - ->will($this->returnValueMap( - array( - array('REQUEST_METHOD', 'POST'), - ) - )); - - $result = $request->getMethod(); - $this->assertEquals('POST', $result); - } - - public function testHeader() - { - $input = array ( - 'Host' => 'www.fossbilling.vm', - 'Connection' => 'keep-alive', - 'Content-Length' => '18', - 'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36', - 'Origin' => 'chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo', - 'Content-Type' => 'application/xml', - 'Accept' => '*/*', - 'DNT' => '1', - 'Accept-Encoding' => 'gzip,deflate,sdch', - 'Accept-Language' => 'en-US,en;q=0.8,fr;q=0.6,id;q=0.4,lt;q=0.2,ms;q=0.2,nl;q=0.2,pl;q=0.2,pt;q=0.2,ru;q=0.2,es;q=0.2,nb;q=0.2,ro;q=0.2', - ); - - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getHeaders')) - ->getMock(); - $request->expects($this->atLeastOnce()) - ->method('getHeaders') - ->will($this->returnValue($input)); - - $result = $request->getHeader('Content-Type'); - $this->assertEquals('application/xml', $result); - - $result = $request->getHeader('DNT'); - $this->assertEquals('1', $result); - } - - public function testIs() - { - $request = $this->getMockBuilder(Box_Request::class) - ->setMethods(['getMethod']) - ->getMock(); - $request->expects($this->exactly(7)) - ->method('getMethod') - ->willReturnOnConsecutiveCalls('POST', 'GET', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS'); - - $this->assertTrue($request->isPost()); - $this->assertTrue($request->isGet()); - $this->assertTrue($request->isPut()); - $this->assertTrue($request->isPatch()); - $this->assertTrue($request->isHead()); - $this->assertTrue($request->isDelete()); - $this->assertTrue($request->isOptions()); - } - - public function testIsMethods() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(['getMethod']) - ->getMock(); - $request->expects($this->exactly(4)) - ->method('getMethod') - ->willReturnOnConsecutiveCalls('POST', 'GET', 'GET', 'PUT'); - - $this->assertTrue($request->isMethod('POST')); - $this->assertFalse($request->isMethod('POST')); - $this->assertTrue($request->isMethod(array('POST', 'GET'))); - $this->assertFalse($request->isMethod(array('POST', 'GET'))); - } - - public function testGetScheme() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->will($this->onConsecutiveCalls($this->returnValueMap( - [ - ['HTTPS', '1'], - ['HTTPS', null] - - ] - ))); - - $this->assertEquals('https', $request->getScheme()); - $this->assertEquals('http', $request->getScheme()); - } - - public function testIsAjax() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->will($this->onConsecutiveCalls($this->returnValueMap( - [ - ['HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'], - ['HTTP_X_REQUESTED_WITH', null] - ]))); - - $this->assertTrue($request->isAjax()); - $this->assertFalse($request->isAjax()); - } - - public function testGetServerAddress() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->will($this->onConsecutiveCalls($this->returnValueMap([ - ['SERVER_ADDR', '8.8.8.8'], - ['SERVER_ADDR', null] - ] - ) - )); - - $this->assertEquals('8.8.8.8', $request->getServerAddress()); - $this->assertEquals('127.0.0.1', $request->getServerAddress()); - } - - public function testGetServerName() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->will($this->onConsecutiveCalls($this->returnValueMap([ - ['SERVER_NAME', 'google.com'], - ['SERVER_NAME', null] - ] - ) - )); - - $this->assertEquals('google.com', $request->getServerName()); - $this->assertEquals('localhost', $request->getServerName()); - } - - public function testGetJsonRawBody() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getRawBody')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getRawBody') - ->will($this->onConsecutiveCalls('{"example":1}', 'ss')); - - $this->assertEquals(array('example'=>1), $request->getJsonRawBody()); - try { - $this->assertTrue($request->getJsonRawBody()); - $this->fail('Should be invalid json'); - } catch(RuntimeException $e) { - //ok invalid json - } - } - - public function testGetClientAddressForwarded() - { - $request = $this->getMockBuilder(Box_Request::class) - ->setMethods(['getServer']) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->withConsecutive(['HTTP_X_FORWARDED_FOR'], ['HTTP_X_FORWARDED_FOR']) - ->will($this->onConsecutiveCalls( - '123.123.123.120', - '123.123.123.121, 123.123.123.122' - ) - ); - - $this->assertEquals('123.123.123.120', $request->getClientAddress(true)); - $this->assertEquals('123.123.123.121', $request->getClientAddress(true)); - } - - public function testGetClientAddress() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->withConsecutive(['REMOTE_ADDR'], ['REMOTE_ADDR']) - ->will($this->onConsecutiveCalls( - '123.123.123.1', - '123.123.123.2, 123.123.123.122' - ) - ); - - $this->assertEquals('123.123.123.1', $request->getClientAddress()); - $this->assertEquals('123.123.123.2', $request->getClientAddress(false)); - } - - public function testGetUserAgent() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->withConsecutive(['HTTP_USER_AGENT'], ['HTTP_USER_AGENT']) - ->willReturnOnConsecutiveCalls('Chrome', null); - - $this->assertEquals('Chrome', $request->getUserAgent()); - $this->assertNull($request->getUserAgent()); - } - - public function testGetURI() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->withConsecutive(['REQUEST_URI'], ['REQUEST_URI']) - ->willReturnOnConsecutiveCalls('/foo/bar', null); - - $this->assertEquals('/foo/bar', $request->getURI()); - $this->assertNull($request->getURI()); - } - - public function testGetHttpHost() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - - $request->expects($this->exactly(2)) - ->method('getServer') - ->withConsecutive(['HTTP_HOST'], ['HTTP_HOST']) - ->willReturnOnConsecutiveCalls('www.google.com', null); - - $this->assertEquals('www.google.com', $request->getHttpHost()); - $this->assertNull($request->getHttpHost()); - } - - - public function testGetLanguages() - { - $request = $this->getMockBuilder('Box_Request') - ->setMethods(array('getServer')) - ->getMock(); - $request->expects($this->once())->method('getServer')->will($this->returnValueMap( - [ - ['HTTP_ACCEPT_LANGUAGE', "en-US,en;q=0.8,fr;q=0.6,id;q=0.4,lt;q=0.2,ms;q=0.2,nl;q=0.2,pl;q=0.2,pt;q=0.2,ru;q=0.2,es;q=0.2,nb;q=0.2,ro;q=0.2"] - ])); - - $result = array ( - 0 => - array ( - 'language' => 'en-US', - 'quality' => 1, - ), - 1 => - array ( - 'language' => 'en', - 'quality' => '0.8', - ), - 2 => - array ( - 'language' => 'fr', - 'quality' => '0.6', - ), - 3 => - array ( - 'language' => 'id', - 'quality' => '0.4', - ), - 4 => - array ( - 'language' => 'lt', - 'quality' => '0.2', - ), - 5 => - array ( - 'language' => 'ms', - 'quality' => '0.2', - ), - 6 => - array ( - 'language' => 'nl', - 'quality' => '0.2', - ), - 7 => - array ( - 'language' => 'pl', - 'quality' => '0.2', - ), - 8 => - array ( - 'language' => 'pt', - 'quality' => '0.2', - ), - 9 => - array ( - 'language' => 'ru', - 'quality' => '0.2', - ), - 10 => - array ( - 'language' => 'es', - 'quality' => '0.2', - ), - 11 => - array ( - 'language' => 'nb', - 'quality' => '0.2', - ), - 12 => - array ( - 'language' => 'ro', - 'quality' => '0.2', - ), - ); - $this->assertEquals($result, $request->getLanguages()); - } - - -} diff --git a/tests/modules/Activity/Api/AdminTest.php b/tests/modules/Activity/Api/AdminTest.php index 6b6fb875f..2c47bd4d8 100644 --- a/tests/modules/Activity/Api/AdminTest.php +++ b/tests/modules/Activity/Api/AdminTest.php @@ -82,28 +82,6 @@ class AdminTest extends \BBTestCase { $activity->log_get_list(array()); } - public function testlog() - { - $databaseMock = $this->getMockBuilder('Box_Database')->getMock(); - $databaseMock->expects($this->atLeastOnce())-> - method('dispense')-> - will($this->returnValue(new \stdClass())); - - $databaseMock->expects($this->atLeastOnce())-> - method('store')-> - will($this->returnValue(1)); - - $di = new \Box_Di(); - $di['db'] = $databaseMock; - $di['request'] = new \Box_Request($di); - - $activity = new \Box\Mod\Activity\Api\Admin(); - $activity->setDi($di); - $result = $activity->log(array('m' => 'hello message')); - - $this->assertEquals(true, $result, 'Log did not returned true'); - } - public function testlogEmptyMParam() { $di = new \Box_Di(); @@ -182,4 +160,3 @@ class AdminTest extends \BBTestCase { $this->assertEquals(true, $result); } } - \ No newline at end of file diff --git a/tests/modules/Activity/ServiceTest.php b/tests/modules/Activity/ServiceTest.php index 785a32f43..22bb8a24e 100644 --- a/tests/modules/Activity/ServiceTest.php +++ b/tests/modules/Activity/ServiceTest.php @@ -45,122 +45,6 @@ class ServiceTest extends \BBTestCase $this->assertTrue(strpos($result[0], $search) != false, $expected, $result); } - /* Disabled - see https://github.com/FOSSBilling/FOSSBilling/pull/433 - public function testonAfterClientLogin() - { - $model = new \Model_ActivityClientHistory(); - $model->loadBean(new \DummyBean()); - $databaseMock = $this->getMockBuilder('Box_Database')->getMock(); - $databaseMock->expects($this->atLeastOnce())-> - method('dispense')-> - will($this->returnValue($model)); - - $databaseMock->expects($this->atLeastOnce())-> - method('store')-> - will($this->returnValue(1)); - - $eventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); - $eventMock->expects($this->atLeastOnce())-> - method('getParameters')->will($this->returnValue(array('ip' => '1.1.1.1', 'id' => 0))); - - $extensionServiceMock = $this->getMockBuilder('\Box\Mod\Extension\Service')->getMock(); - - $extensionServiceMock->expects($this->atLeastOnce()) - ->method('isExtensionActive') - ->will($this->returnValue(true)); - - $di = new \Box_Di(); - $di['db'] = $databaseMock; - $di['mod_service'] = $di->protect(function ($serviceName) use ($extensionServiceMock){ - if ($serviceName == 'Extension'){ - return $extensionServiceMock; - } - }); - - $eventMock->expects($this->atLeastOnce()) - ->method('getDi') - ->will($this->returnValue($di)); - - $service = new \Box\Mod\Activity\Service(); - $service ->setDi($di); - - $service->onAfterClientLogin($eventMock); - } - - public function testonAfterAdminLogin() - { - $model = new \Model_ActivityClientHistory(); - $model->loadBean(new \DummyBean()); - $databaseMock = $this->getMockBuilder('Box_Database')->getMock(); - $databaseMock->expects($this->atLeastOnce())-> - method('dispense')-> - will($this->returnValue($model)); - - $databaseMock->expects($this->atLeastOnce())-> - method('store')-> - will($this->returnValue(1)); - - $eventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock(); - $eventMock->expects($this->atLeastOnce())-> - method('getParameters')->will($this->returnValue(array('ip' => '1.1.1.1', 'id' => 0))); - - $extensionServiceMock = $this->getMockBuilder('\Box\Mod\Extension\Service')->getMock(); - - - $di = new \Box_Di(); - $di['db'] = $databaseMock; - $di['mod_service'] = $di->protect(function ($serviceName) use ($extensionServiceMock){ - if ($serviceName == 'Extension'){ - return $extensionServiceMock; - } - }); - $eventMock->expects($this->atLeastOnce()) - ->method('getDi') - ->will($this->returnValue($di)); - - $service = new \Box\Mod\Activity\Service(); - $service->setDi($di); - - $service->onAfterAdminLogin($eventMock); - } - - - public function testLogEvent() - { - $service = new \Box\Mod\Activity\Service(); - $data = array( - 'message' => 'Logging test message' - ); - - $di = new \Box_Di(); - $db = $this->getMockBuilder('Box_Database')->getMock(); - - $model = new \Model_ActivitySystem(); - $model->loadBean(new \DummyBean()); - $db->expects($this->atLeastOnce()) - ->method('dispense') - ->will($this->returnValue($model)); - $db->expects($this->atLeastOnce()) - ->method('store') - ->will($this->returnValue(array())); - - $extensionServiceMock = $this->getMockBuilder('\Box\Mod\Extension\Service')->getMock(); - - - $di['request'] = $this->getMockBuilder('Box_Request')->getMock();; - $di['db'] = $db; - $di['mod_service'] = $di->protect(function ($serviceName) use ($extensionServiceMock){ - if ($serviceName == 'Extension'){ - return $extensionServiceMock; - } - }); - $service->setDi($di); - - $result = $service->logEvent($data); - $this->assertNull($result); - } - */ - public function testLogEmail() { $service = new \Box\Mod\Activity\Service(); diff --git a/tests/modules/Api/ServiceTest.php b/tests/modules/Api/ServiceTest.php index e5949a5cf..a0ee23add 100644 --- a/tests/modules/Api/ServiceTest.php +++ b/tests/modules/Api/ServiceTest.php @@ -23,32 +23,6 @@ class ServiceTest extends \BBTestCase { $this->assertEquals($di, $getDi); } - public function testlogRequest() - { - $affectedRows = 1; - $dbMock = $this->getMockBuilder('\Box_Database')->getMock(); - $dbMock->expects($this->atLeastOnce()) - ->method('exec') - ->will($this->returnValue($affectedRows)); - - $requestMock = $this->getMockBuilder('\Box_Request')->getMock(); - $requestMock->expects($this->atLeastOnce()) - ->method('getClientAddress') - ->will($this->returnValue('1.1.1.1')); - $requestMock->expects($this->atLeastOnce()) - ->method('getUri') - ->will($this->returnValue('index.html')); - - $di = new \Box_Di(); - $di['request'] = $requestMock; - $di['db'] = $dbMock; - - $this->service->setDi($di); - $result = $this->service->logRequest(); - $this->assertIsInt($result); - $this->assertEquals($affectedRows, $result); - } - public function testgetRequestCount() { $since = 674690401; // timestamp == '1991-05-20 00:00:01'; @@ -72,4 +46,3 @@ class ServiceTest extends \BBTestCase { } } - \ No newline at end of file diff --git a/tests/modules/System/Api/GuestTest.php b/tests/modules/System/Api/GuestTest.php index 4c906b581..8f7a92cb8 100644 --- a/tests/modules/System/Api/GuestTest.php +++ b/tests/modules/System/Api/GuestTest.php @@ -142,21 +142,6 @@ class GuestTest extends \BBTestCase { $this->assertEquals($expected, $result); } - public function testcurrent_url() - { - $requestMock = $this->getMockBuilder('\Box_Request')->getMock(); - $requestMock->expects($this->atLeastOnce()) - ->method('getURI') - ->will($this->returnValue('StringTypeUrl')); - - $di = new \Box_Di(); - $di['request'] = $requestMock; - - $this->api->setDi($di); - $result = $this->api->current_url(); - $this->assertIsString($result); - } - public function testtemplate_exists() { $data = array( @@ -218,4 +203,3 @@ class GuestTest extends \BBTestCase { } } - \ No newline at end of file diff --git a/tests/modules/System/ServiceTest.php b/tests/modules/System/ServiceTest.php index 4f6147949..014a15264 100644 --- a/tests/modules/System/ServiceTest.php +++ b/tests/modules/System/ServiceTest.php @@ -269,40 +269,6 @@ class ServiceTest extends \BBTestCase { $this->assertTrue($result); } - public function testgetCurrentUrl() - { - $requestMock = $this->getMockBuilder('\Box_Request')->getMock(); - $requestMock->expects($this->atLeastOnce()) - ->method('getScheme') - ->will($this->returnValue('https')); - $requestMock->expects($this->atLeastOnce()) - ->method('getURI') - ->will($this->returnValue('?page=1')); - - $requestMock->expects($this->atLeastOnce()) - ->method('getServer') - ->will($this->returnCallback( function () - { - $arg = func_get_arg(0); - if ($arg == 'SERVER_PORT'){ - return '80'; - } - if ($arg == 'SERVER_NAME'){ - return 'localhost'; - } - return false; - })); - - $di = new \Box_Di(); - $di['request'] = $requestMock; - - $this->service->setDi($di); - $result = $this->service->getCurrentUrl(); - $this->assertIsString($result); - } - - - public function testgetPeriod() { $code = '1W';