Skip to content

Commit

Permalink
Add override option
Browse files Browse the repository at this point in the history
  • Loading branch information
ronisaha committed Mar 22, 2015
1 parent 63ccef3 commit a32455e
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 52 deletions.
4 changes: 2 additions & 2 deletions EzMaintenance/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the ez-maintenance package.
* This file is part of the EzMaintenance package.
*
* (c) Xiidea <http://www.xiidea.net>
*
Expand All @@ -13,5 +13,5 @@

interface AdapterInterface
{
public static function isMaintenanceModeEnabled(array $options = array());
public static function isTrue(array $options = array());
}
8 changes: 3 additions & 5 deletions EzMaintenance/Adapter/BaseVariableAdapter.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
<?php

/*
* This file is part of the ez-maintenance package.
* This file is part of the EzMaintenance package.
*
* (c) Xiidea <http://www.xiidea.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/


namespace EzMaintenance\Adapter;


abstract class BaseVariableAdapter implements AdapterInterface, VariableAdapterInterface
{
public static function isMaintenanceModeEnabled(array $options = array())
public static function isTrue(array $options = array())
{
$options = self::getOptions($options);

Expand Down
4 changes: 2 additions & 2 deletions EzMaintenance/Adapter/Constant.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the ez-maintenance package.
* This file is part of the EzMaintenance package.
*
* (c) Xiidea <http://www.xiidea.net>
*
Expand Down
4 changes: 2 additions & 2 deletions EzMaintenance/Adapter/Environment.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* This file is part of the ez-maintenance package.
* This file is part of the EzMaintenance package.
*
* (c) Xiidea <http://www.xiidea.net>
*
Expand Down
7 changes: 3 additions & 4 deletions EzMaintenance/Adapter/FileAdapter.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php

/*
* This file is part of the ez-maintenance package.
* This file is part of the EzMaintenance package.
*
* (c) Xiidea <http://www.xiidea.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/


namespace EzMaintenance\Adapter;


class FileAdapter implements AdapterInterface
{

public static function isMaintenanceModeEnabled(array $options = array())
public static function isTrue(array $options = array())
{
return file_exists(isset($options['path']) ? $options['path'] : 'down');
}
Expand Down
4 changes: 2 additions & 2 deletions EzMaintenance/Adapter/VariableAdapterInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

/*
* This file is part of the EzMaintenance package.
*
Expand All @@ -8,7 +8,7 @@
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EzMaintenance\Adapter;

interface VariableAdapterInterface
Expand Down
70 changes: 70 additions & 0 deletions EzMaintenance/Helper/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the EzMaintenance package.
*
* (c) Xiidea <http://www.xiidea.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EzMaintenance\Helper;

class Security
{
const ENCRYPTION_KEY = 'FVbC#kx3=GBzuaNveh7x2Km';
const COOKIE_NAME = 'EZ_MAINTENANCE_KEY';

public static function hasValidGatePass($overrideKey)
{
$pass = self::getPassKey();

if($pass) {
return self::storeTheKeyIfValid($overrideKey, $pass);
}

return false;
}

/**
* @return bool
*/
private static function getPassKey()
{
if (isset($_COOKIE[self::COOKIE_NAME])) {
return $_COOKIE[self::COOKIE_NAME];
} elseif (isset($_GET['key'])) {
return self::createPassKey($_GET['key']);
}

return false;
}

private static function createPassKey($str)
{
return substr(md5(self::tick() . $str . self::ENCRYPTION_KEY), -12, 10);
}

private static function tick()
{
return ceil(time() / (12 * 60 * 60));
}

private static function storeTheKeyIfValid($key, $pass)
{
if(self::isValidPass($key, $pass)) {
return setcookie(self::COOKIE_NAME, $pass);
}

setcookie(self::COOKIE_NAME, "", time() - 3600);

return false;
}

private static function isValidPass($str, $key)
{
return ($key == self::createPassKey($str));
}

}
2 changes: 1 addition & 1 deletion EzMaintenance/Resources/templates/clock.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
</style>
</head>
<body>
<time title="<?php echo $title ?>" id="currtime"></time>
<time id="currtime"></time>
<?php echo $javascript ?>
<script>
var currentTime = document.getElementById("currtime");
Expand Down
2 changes: 1 addition & 1 deletion EzMaintenance/Resources/templates/game.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,nofollow"/>
<title><?php echo $title ?></title>
<title> Under maintenance </title>
<style type="text/css">
body {text-align:center;}
canvas { border:5px dotted #ccc; }
Expand Down
18 changes: 3 additions & 15 deletions EzMaintenance/Resources/templates/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="robots" content="noindex,nofollow"/>
<title><?php echo $title ?></title>
<title> Under maintenance </title>
<style type="text/css">
* {
margin: 0;
Expand All @@ -21,35 +21,23 @@
div#ct_site {
margin-left: auto;
margin-right: auto;
margin-top: 5em;
margin-top: 4em;
width: 500px;
}

p {
margin: 1em 0;
font-size: 120%;
}

div.image {
text-align: center;
display: block;
}
div.image img {
width: 30%;
height: 30%;
}
</style>

</head>
<body>
<div id="ct_site">
<div class="image"></div>

<p><?php echo $msg ?></p>

</div>
<div class="image">
<img src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTYuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cud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TTkuMDc1LDEzLjg4NmMyLjA2MiwwLDMuNzUtMi44NzQsNC4xNjUtNS40MjZINC45MDhDNS4zMTcsMTEuMDMyLDcuMDEzLDEzLjg4Niw5LjA3NSwxMy44ODZ6Ii8+CgkJPHBhdGggZD0iTTE0LjcwMyw2LjgzN2gtMC45NzVjLTAuMTYyLTIuNDctMC42NzItNS43NzMtNC42NTktNS43NzNjLTMuOTksMC00LjQ5NywzLjMwMy00LjY2Myw1Ljc3M0gzLjM4NXYwLjcxNmgxMS4zMThWNi44Mzd6Ii8+Cgk8L2c+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPGc+CjwvZz4KPC9zdmc+"/>
</div>
<?php echo $javascript ?>
</body>
</html>
20 changes: 12 additions & 8 deletions EzMaintenance/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
/*
* This file is part of the EzMaintenance package.
*
* Copyright (c) 2015 Roni Saha
* (c) Xiidea <http://www.xiidea.net>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/


namespace EzMaintenance;


use EzMaintenance\Adapter\AdapterInterface;
use EzMaintenance\Helper\Security;
use EzMaintenance\Helper\Template;

class Worker {

class Worker
{
/** @var AdapterInterface[] $adapters */
private static $adapters = array(
'file' => '\EzMaintenance\Adapter\FileAdapter',
Expand All @@ -27,8 +26,13 @@ class Worker {

public static function watch($adapter = 'file', $options = array()){
$adapterInterface = self::getAdapter($adapter);
$options = self::getOptions($options);

if($adapterInterface::isMaintenanceModeEnabled($options))
if($options['override_key'] && Security::hasValidGatePass($options['override_key'])) {
return;
}

if($adapterInterface::isTrue($options) xor $options['inverse'])
{
self::showMaintenancePage($options);
}
Expand All @@ -52,17 +56,17 @@ private static function isImplementedAdapter($adapter)

private static function showMaintenancePage($options)
{
$options = self::getOptions($options);
Template::load($options);
exit;
}

protected static function getOptions(array $options) {
return array_merge(array(
'interval' => 5,
'inverse' => false,
'override_key' => false,
'template' => 'simple',
'msg' => 'Our site is currently undergoing maintenance!',
'title' => 'Under maintenance!',
),$options);
}
}
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ If you're using Composer to manage dependencies, you can include the following
in your composer.json file:

"require": {
"ronisaha/ez-maintenance": "dev-master"
"xiidea/ez-maintenance": "dev-master"
}

Then, after running `composer update` or `php composer.phar update`, you can
Expand All @@ -31,11 +31,13 @@ require_once 'PATH_TO_LIBRARY/autoload.php';

```

Then you can just call `EzMaintenance\Worker::watch()` In your `index.php` (see the [example](https://github.com/ronisaha/ez-maintenance/blob/master/example/index.php))
Then you can just call `EzMaintenance\Worker::watch()` In your `index.php` (see the [example](https://github.com/xiidea/ez-maintenance/blob/master/example/index.php))


## Different Adapters to check maintenance mode

Use adapter to detect if we need to serve the maintenance page or not. 3 Types of adapter provided. You can also define your own adapter.

##### File Adapters

This is the default adapter. it checks for a specific file if exist or not. default path is *down*. For the following implementation
Expand Down Expand Up @@ -94,9 +96,18 @@ Following options are available to customize the behaviour of the library

This option state the interval in second, the system will check for the site status with this interval. default is 5 second.

##### override_key

You can configure a key to override the maintenance mode. This is helpful t test your site normally while others will see maintenance page.
To browse the page in maintenance mode, you just need to pass the override key as query parameter like: `http://yourdomain.com/?key={override_key}`

##### inverse

If you want to enable maintenance mode when adapter return false, you can set `inverse = true`.

##### template

You can provide the builtin template name(simple, game, clock) or path to your own template file. All the options will also be available in your template.
You can provide the built-in template name(simple, game, clock) or path to your own template file. All the options will also be available in your template.
You should `echo $javascript` in your template file to enable auto status check.

```php
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "ronisaha/ez-maintenance",
"name": "xiidea/ez-maintenance",
"type": "library",
"description": "A handy library to handle site maintenance situation",
"keywords": ["Under Maintenance", "Service Unavailable", "Easy"],
"license": "MIT",
"homepage": "http://ronisaha.github.io/ez-maintenance/",
"homepage": "http://xiidea.github.io/ez-maintenance/",
"authors": [
{
"name": "Roni Saha",
Expand All @@ -13,8 +13,8 @@
}
],
"support": {
"issues": "https://github.com/ronisaha/ez-maintenance//issues",
"source": "https://github.com/ronisaha/ez-maintenance/"
"issues": "https://github.com/xiidea/ez-maintenance//issues",
"source": "https://github.com/xiidea/ez-maintenance/"
},
"require": {
"php": ">=5.3"
Expand Down
6 changes: 3 additions & 3 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

include_once("../autoload.php");
define('APPLICATION_ENV', 'down');

EzMaintenance\Worker::watch('const', array(
'template' => 'clock'
EzMaintenance\Worker::watch('env', array(
'template' => 'clock',
'override_key' => 'hard',
));

//Your normal code
Expand Down

0 comments on commit a32455e

Please sign in to comment.