-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
1,470 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ?? '') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
Oops, something went wrong.