Skip to content

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jamielsharief committed Nov 6, 2019
2 parents 49f07bf + 75bbf22 commit c8d69d8
Show file tree
Hide file tree
Showing 33 changed files with 1,470 additions and 90 deletions.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# Changelog
## [Unreleased]

## [2.2.0] - 2019-11-06

### Fixed
- Fixed issue with group reflection of tests in PHP 7.3 only

### Changed
- exceptions that extends HttpException will now show error message even if status code is 500 or above.

### Added

- PhpFile - class for reading/and writing arrays to files
- AccessLogMiddleware
- FirewallMiddleware
- IdsMiddleware
- ProfilerMiddleware
- ThrottleMiddleware

### Changed
- Controller::redirect adjusted and now no longer stops script execution, it lets the dispatcher handle this.

## [2.1.0] - 2019-11-01
### Added
Expand Down
3 changes: 1 addition & 2 deletions src/Console/ArgumentParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types = 1);
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
Expand All @@ -12,7 +11,7 @@
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

declare(strict_types = 1);
namespace Origin\Console;

use Origin\Console\Exception\ConsoleException;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Dot.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types = 1);
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
Expand All @@ -12,6 +11,7 @@
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types = 1);
namespace Origin\Core;

/**
Expand Down
58 changes: 58 additions & 0 deletions src/Core/PhpFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
*
* Licensed under The MIT License
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* @copyright Copyright (c) Jamiel Sharief
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types = 1);
namespace Origin\Core;

use Origin\Core\Exception\Exception;
use Origin\Core\Exception\InvalidArgumentException;

/**
* A class for reading and writing arrays to file
*/
class PhpFile
{
/**
* Reads a PHPFile (array)
*
* @param string $filename /var/www/config/data.php
* @return array
*/
public function read(string $filename) : array
{
if (!file_exists($filename)) {
throw new InvalidArgumentException(sprintf('File `%` does not exist', $filename));
}
$out = include $filename;
if (is_array($out)) {
return $out;
}
throw new Exception(sprintf('File `%s` did not return an array', $filename));
}

/**
* Writes the array to disk
*
* @param string $filename
* @param array $data
* @param array $options The following options key are supported
* - lock: default false. Wether to lock the file write.
* @return boolean
*/
public function write(string $filename, array $data, array $options = []) : bool
{
$options += ['lock' => false];
$out = '<?php' . "\n" . 'return ' . var_export($data, true) . ';';
return (bool) file_put_contents($filename, $out, $options['lock'] ? LOCK_EX : 0);
}
}
7 changes: 2 additions & 5 deletions src/Http/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
*/
class Controller
{
use ModelTrait, InitializerTrait, CallbackRegistrationTrait;
use HookTrait;
use ModelTrait, InitializerTrait, CallbackRegistrationTrait,HookTrait;
/**
* Controller name.
*
Expand Down Expand Up @@ -607,9 +606,7 @@ public function redirect($url, int $code = 302) : Response

$this->response->statusCode($code);
$this->response->header('Location', Router::url($url));
$this->response->send();
$this->response->stop();


// Return the response object once called
return $this->response;
}
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Dispatcher.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
declare(strict_types = 1);
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
Expand All @@ -12,7 +11,7 @@
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/

declare(strict_types = 1);
namespace Origin\Http;

use Origin\Core\Config;
Expand Down
4 changes: 2 additions & 2 deletions src/Http/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function exceptionHandler($exception) : void
$errorCode = 404;
}

$this->logException($exception, $errorCode);
$this->logException($exception);
$this->cleanBuffer();

/**
Expand All @@ -200,7 +200,7 @@ public function exceptionHandler($exception) : void
/**
* Logs The exception
*
* @param Exception $exception
* @param \Exception $exception
* @return void
*/
protected function logException($exception): void
Expand Down
3 changes: 2 additions & 1 deletion src/Http/ExceptionRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@ protected function getErrorCodeAndMessage($exception) : array
{
$errorCode = ($exception->getCode() === 404) ? 404 : 500;
$errorMessage = ($exception->getCode() === 404) ? 'Not Found' : 'An Internal Error has Occured';
if ($exception instanceof HttpException and $exception->getCode() < 500) {
if ($exception instanceof HttpException) {
$errorCode = $exception->getCode();
$errorMessage = $exception->getMessage(); # used in rendering
}

return [$errorCode,$errorMessage];
}

protected function getFileToRender($exception)
{
$errorCode = ($exception->getCode() === 404) ? 404 : 500;
Expand Down
72 changes: 72 additions & 0 deletions src/Http/Middleware/AccessLogMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
*
* Licensed under The MIT License
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* @copyright Copyright (c) Jamiel Sharief
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Origin\Http\Middleware;

use Origin\Http\Request;
use Origin\Http\Response;
use Origin\Http\Middleware\Middleware;

/**
* Creates an access log using the Apache Common LOG format, with one main difference, the ability to detect users
* logged in.
*
* @see https://httpd.apache.org/docs/2.4/logs.html#accesslog.
*/
class AccessLogMiddleware extends Middleware
{
/**
* Default config
*
* @var array
*/
protected $defaultConfig = [
'file' => LOGS . '/access.log'
];

/**
* This PROCESSES the response. Use this to make changes to the response.
*
* @param \Origin\Http\Request $request
* @param \Origin\Http\Response $response
* @return void
*/
public function process(Request $request, Response $response) : void
{
file_put_contents($this->config['file'], $this->commonFormat($request, $response) . "\n", FILE_APPEND);
}

/**
* Creates an access log using the Apache Common LOG format
* @see https://httpd.apache.org/docs/2.4/logs.html#accesslog.
*
* @param \Origin\Http\Request $request
* @param \Origin\Http\Response $response
* @return string
*/
private function commonFormat(Request $request, Response $response) : string
{
return sprintf(
'%s %s [%s] "%s %s %s" %d %d',
$request->ip(),
$request->session()->read('Auth.User.id') ?: '-',
date('d/M/Y:H:i:s O'),
$request->method(),
$request->env('REQUEST_URI'),
$request->env('SERVER_PROTOCOL'),
$response->statusCode(),
mb_strlen($response->body() ?? '')
);
}
}
93 changes: 93 additions & 0 deletions src/Http/Middleware/FirewallMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* OriginPHP Framework
* Copyright 2018 - 2019 Jamiel Sharief.
*
* Licensed under The MIT License
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* @copyright Copyright (c) Jamiel Sharief
* @link https://www.originphp.com
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Origin\Http\Middleware;

use Origin\Http\Exception\ForbiddenException;
use Origin\Http\Request;
use Origin\Http\Middleware\Middleware;
use Origin\Core\PhpFile;

/**
* FirewallMiddleware is a for blacklisting IPs or restricting access to your web to only certain IPs
*
* Create config/blacklist.php (or whitelist.php)
*
* <?php
*
* return [
* '192.168.176.4'
* ];
*/
class FirewallMiddleware extends Middleware
{
/**
* Array of IPs that are blacklisted
*
* @var array
*/
protected $blacklist = [];

/**
* Array of IPs that are allowed. If this has any values only these IP addresses
* will be allowed.
*
* @var array
*/
protected $whitelist = [];

/**
* This HANDLES the request. Use this to make changes to the request.
*
* @param \Origin\Http\Request $request
* @return void
*/
public function handle(Request $request) : void
{
$ipAddress = $request->ip();
if (!$ipAddress) {
throw new ForbiddenException('Invalid IP address');
}

if (file_exists(CONFIG . '/blacklist.php')) {
$this->blacklist = (new PhpFile())->read(CONFIG . '/blacklist.php');
}

if (file_exists(CONFIG . '/whitelist.php')) {
$this->whitelist = (new PhpFile())->read(CONFIG . '/whitelist.php');
}

$this->checkLists($ipAddress);

// Free mem
$this->blacklist = $this->whitelist = null;
}

/**
* Checks an IP address against blacklist and whitelists
*
* @param string $ip
* @return void
*/
protected function checkLists(string $ip) : void
{
if ($this->whitelist) {
if (!in_array($ip, $this->whitelist)) {
throw new ForbiddenException('IP address is not allowed');
}
} elseif (in_array($ip, $this->blacklist)) {
throw new ForbiddenException('IP address is blacklisted');
}
}
}
Loading

0 comments on commit c8d69d8

Please sign in to comment.