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
0 parents
commit d44ab95
Showing
16 changed files
with
315 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
vendor | ||
.php_cs.cache |
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,21 @@ | ||
language: php | ||
|
||
php: | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
|
||
matrix: | ||
fast_finish: true | ||
|
||
before_script: | ||
- composer self-update | ||
- composer install --prefer-source --no-interaction | ||
- composer dump-autoload -o | ||
|
||
script: | ||
- bin/kahlan --coverage=4 --reporter=verbose --clover=build/logs/clover.xml | ||
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]]; then bin/coveralls -v --exclude-no-stmt; fi | ||
|
||
notifications: | ||
email: false |
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,17 @@ | ||
CONTRIBUTING | ||
------------ | ||
To contribute, you can send pull requests with : | ||
|
||
- Typo fix. | ||
- Use PSR-2 Coding Standard. | ||
- patch(es) need new/updated test(s). | ||
- new feature(s) need test(s). | ||
|
||
Tests | ||
----- | ||
You can run test with : | ||
```shell | ||
$ composer install | ||
$ bin/kahlan --coverage=4 | ||
``` | ||
and make sure there is no regression. |
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,19 @@ | ||
Copyright (c) 2017 Abdul Malik Ikhsan | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,71 @@ | ||
ForceHttpsModule | ||
================ | ||
|
||
[![Latest Version](https://img.shields.io/github/release/samsonasik/ForceHttpsModule.svg?style=flat-square)](https://github.com/samsonasik/ForceHttpsModule/releases) | ||
[![Build Status](https://travis-ci.org/samsonasik/ForceHttpsModule.svg?branch=master)](https://travis-ci.org/samsonasik/ForceHttpsModule) | ||
[![Coverage Status](https://coveralls.io/repos/github/samsonasik/ForceHttpsModule/badge.svg?branch=master)](https://coveralls.io/github/samsonasik/ForceHttpsModule?branch=master) | ||
[![Downloads](https://img.shields.io/packagist/dt/samsonasik/force-https-module.svg?style=flat-square)](https://packagist.org/packages/samsonasik/force-https-module) | ||
|
||
Introduction | ||
------------ | ||
|
||
ForceHttpsModule is a configurable module for force https in your ZF2/ZF3 Mvc Application. | ||
|
||
Features | ||
-------- | ||
|
||
- [x] enable/disable force https | ||
- [x] Force Https to All routes | ||
- [x] Force Https to specific routes only | ||
|
||
Installation | ||
------------ | ||
|
||
**1. Require this module uses [composer](https://getcomposer.org/).** | ||
|
||
```sh | ||
composer require samsonasik/force-https-module | ||
``` | ||
|
||
**4. Copy `force-https-module.local.php.dist` config to your local's autoload and configure it** | ||
|
||
| source | destination | | ||
|------------------------------------------------------------------------------|---------------------------------------------| | ||
| vendor/samsonasik/force-https-module/config/force-https-module.local.php.dist | config/autoload/force-https-module.local.php | | ||
|
||
Or run copy command: | ||
|
||
```sh | ||
cp vendor/samsonasik/force-https-module/config/force-https-module.local.php.dist config/autoload/force-https-module.local.php | ||
``` | ||
|
||
When done, you can modify `config/autoload/force-https-module.local.php` config in your's local config: | ||
|
||
```php | ||
<?php | ||
// config/autoload/force-https-module.local.php | ||
return [ | ||
'force-https-module' => [ | ||
'enable' => true, | ||
'force_all_routes' => true, | ||
'force_specific_routes' => [ | ||
// a lists of specific routes to be https | ||
// only works if previous config 'force_all_routes' => false | ||
], | ||
], | ||
]; | ||
``` | ||
|
||
**5. Lastly, enable it** | ||
```php | ||
// config/modules.config.php or config/application.config.php | ||
return [ | ||
'Application' | ||
'ForceHttpsModule', // register here | ||
], | ||
``` | ||
|
||
|
||
Contributing | ||
------------ | ||
Contributions are very welcome. Please read [CONTRIBUTING.md](https://github.com/samsonasik/ForceHttpsModule/blob/master/CONTRIBUTING.md) |
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,2 @@ | ||
* | ||
!.gitignore |
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,2 @@ | ||
* | ||
!.gitignore |
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,42 @@ | ||
{ | ||
"name": "samsonasik/force-https-module", | ||
"type": "library", | ||
"description": "Force Https Module", | ||
"keywords": ["zf2", "zf3", "force", "https", "http"], | ||
"homepage": "https://github.com/samsonasik/ForceHttpsModule", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Abdul Malik Ikhsan", | ||
"email": "samsonasik@gmail.com", | ||
"homepage": "http://samsonasik.wordpress.com", | ||
"role": "Developer" | ||
} | ||
], | ||
"require":{ | ||
"php": "^5.6 || ^7.0", | ||
"zendframework/zend-console": "^2.5", | ||
"zendframework/zend-mvc": "^2.5 || ^3.0" | ||
}, | ||
"require-dev": { | ||
"kahlan/kahlan": "^3.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"ForceHttpsModule\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"ForceHttpsModule\\Spec\\": "spec/" | ||
} | ||
}, | ||
"config": { | ||
"bin-dir": "bin" | ||
}, | ||
"extra": { | ||
"zf": { | ||
"module": "ForceHttpsModule" | ||
} | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
return [ | ||
'force-https-module' => [ | ||
'enable' => true, | ||
'force_all_routes' => true, | ||
'force_specific_routes' => [ | ||
// a lists of specific routes to be https | ||
// only works if previous config 'force_all_routes' => false | ||
], | ||
], | ||
]; |
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,14 @@ | ||
<?php | ||
|
||
namespace ForceHttpsModule; | ||
|
||
return [ | ||
'service_manager' => [ | ||
'factories' => [ | ||
Listener\ForceListener::class => Listener\ForceHttpsFactory::class, | ||
], | ||
], | ||
'listeners' => [ | ||
Listener\ForceListener::class, | ||
], | ||
]; |
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,5 @@ | ||
<?php | ||
|
||
describe('ForceHttpsFactory', function () { | ||
|
||
}); |
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,5 @@ | ||
<?php | ||
|
||
describe('ForceHttps', function () { | ||
|
||
}); |
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,5 @@ | ||
<?php | ||
|
||
describe('Module', function () { | ||
|
||
}); |
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,75 @@ | ||
<?php | ||
|
||
namespace ForceHttpsModule\Listener; | ||
|
||
use Zend\Console\Console; | ||
use Zend\Console\Console; | ||
use Zend\EventManager\AbstractListenerAggregate; | ||
use Zend\EventManager\EventManagerInterface; | ||
use Zend\Mvc\MvcEvent; | ||
use Zend\Uri\Uri; | ||
|
||
class ForceHttps extends AbstractListenerAggregate | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @method __construct | ||
* @param array $config | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* @param EventManagerInterface $events | ||
* @param int $priority | ||
*/ | ||
public function attach(EventManagerInterface $events, $priority = 1) | ||
{ | ||
if (Console::isConsole()) { | ||
return; | ||
} | ||
|
||
$this->listeners[] = $events->attach(MvcEvent::EVENT_ROUTE, [$this, 'forceHttps']); | ||
} | ||
|
||
public function forceHttps(MvcEvent $e) | ||
{ | ||
if (! $this->config['enable']) { | ||
return; | ||
} | ||
|
||
$uri = $e->getRequest()->getUri(); | ||
$uriScheme = $uri->getScheme(); | ||
if ($uriScheme === 'https') { | ||
return; | ||
} | ||
|
||
$httpsRequestUri = $uri->setScheme('https')->getRequestUri(); | ||
if ($this->config['force_all_routes']) { | ||
return $this->redirectWithHttps($e); | ||
} | ||
|
||
$routeName = $e->getRouteMatch()->getMatchedRouteName(); | ||
if (! in_array($routeName, $this->config['force_specific_routes'])) { | ||
return; | ||
} | ||
|
||
return $this->redirectWithHttps($e); | ||
} | ||
|
||
private function redirectWithHttps(MvcEvent $e) | ||
{ | ||
$response = $e->getResponse(); | ||
$response->setStatusCode(302); | ||
$response->getHeaders() | ||
->addHeaderLine('Location', $httpsRequestUri); | ||
|
||
return $e->stopPropagation(); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace ForceHttpsModule\Listener; | ||
|
||
class ForceHttpsListenerFactory | ||
{ | ||
public function __invoke($container) | ||
{ | ||
return new ForceHttpsListener($container->get('config')['force-https-module']) | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace ForceHttpsModule; | ||
|
||
class Module | ||
{ | ||
public function getConfig() | ||
{ | ||
return include __DIR__.'./../module.config.php'; | ||
} | ||
} |