Skip to content

Commit

Permalink
initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jan 17, 2017
0 parents commit d44ab95
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
composer.lock
vendor
.php_cs.cache
21 changes: 21 additions & 0 deletions .travis.yml
@@ -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
17 changes: 17 additions & 0 deletions CONTRIBUTING.md
@@ -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.
19 changes: 19 additions & 0 deletions LICENSE
@@ -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.
71 changes: 71 additions & 0 deletions README.md
@@ -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)
2 changes: 2 additions & 0 deletions bin/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
2 changes: 2 additions & 0 deletions build/logs/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
42 changes: 42 additions & 0 deletions composer.json
@@ -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"
}
}
}
12 changes: 12 additions & 0 deletions config/force-https-module.local.php.dist
@@ -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
],
],
];
14 changes: 14 additions & 0 deletions config/module.config.php
@@ -0,0 +1,14 @@
<?php

namespace ForceHttpsModule;

return [
'service_manager' => [
'factories' => [
Listener\ForceListener::class => Listener\ForceHttpsFactory::class,
],
],
'listeners' => [
Listener\ForceListener::class,
],
];
5 changes: 5 additions & 0 deletions spec/Listener/ForceHttpsFactorySpec.php
@@ -0,0 +1,5 @@
<?php

describe('ForceHttpsFactory', function () {

});
5 changes: 5 additions & 0 deletions spec/Listener/ForceHttpsSpec.php
@@ -0,0 +1,5 @@
<?php

describe('ForceHttps', function () {

});
5 changes: 5 additions & 0 deletions spec/ModuleSpec.php
@@ -0,0 +1,5 @@
<?php

describe('Module', function () {

});
75 changes: 75 additions & 0 deletions src/Listener/ForceHttps.php
@@ -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();
}
}
11 changes: 11 additions & 0 deletions src/Listener/ForceHttpsFactory.php
@@ -0,0 +1,11 @@
<?php

namespace ForceHttpsModule\Listener;

class ForceHttpsListenerFactory
{
public function __invoke($container)
{
return new ForceHttpsListener($container->get('config')['force-https-module'])
}
}
11 changes: 11 additions & 0 deletions src/Module.php
@@ -0,0 +1,11 @@
<?php

namespace ForceHttpsModule;

class Module
{
public function getConfig()
{
return include __DIR__.'./../module.config.php';
}
}

0 comments on commit d44ab95

Please sign in to comment.