├── .gitignore ├── LICENSE ├── Lib └── Routing │ └── Filter │ └── MaintenanceMode.php ├── README.md └── composer.json /.gitignore: -------------------------------------------------------------------------------- 1 | tmp/* 2 | [Cc]onfig/core.php 3 | [Cc]onfig/database.php 4 | app/tmp/* 5 | app/[Cc]onfig/core.php 6 | app/[Cc]onfig/database.php 7 | !empty 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Prathik Shetty 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Lib/Routing/Filter/MaintenanceMode.php: -------------------------------------------------------------------------------- 1 | _getUserIpAddr(); 27 | foreach($ips as $ip){ 28 | if($this->_compareIp($userIP,$ip)){ 29 | return ; 30 | } 31 | } 32 | } 33 | 34 | 35 | $statusCode = 503; 36 | $body = 'Currently undergoing maintenance'; 37 | 38 | if (!empty($MaintenanceMode['code'])) { 39 | $statusCode = $MaintenanceMode['code']; 40 | } 41 | 42 | 43 | if (!empty($MaintenanceMode['view']['template'])) { 44 | $View = $this->_getView(); 45 | $body = $View->render($MaintenanceMode['view']['template'], $MaintenanceMode['view']['layout']); 46 | } 47 | 48 | $event->data['response']->statusCode($statusCode); 49 | $event->data['response']->body($body); 50 | $event->stopPropagation(); 51 | return $event->data['response']; 52 | } 53 | 54 | protected function _getView() { 55 | 56 | $MaintenanceMode = Configure::read('MaintenanceMode'); 57 | 58 | $helpers = array('Html'); 59 | if (!empty($MaintenanceMode['view']['helpers']) && is_array($helpers)) { 60 | $helpers = $MaintenanceMode['view']['helpers']; 61 | } 62 | 63 | $View = new View(null); 64 | $View->viewVars = $MaintenanceMode; 65 | $View->hasRendered = false; 66 | $View->helpers = $helpers; 67 | $View->loadHelpers(); 68 | return $View; 69 | } 70 | 71 | protected function _getUserIpAddr() 72 | { 73 | $ip = '0.0.0.0'; 74 | if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //if from shared 75 | $ip = $_SERVER['HTTP_CLIENT_IP']; 76 | } 77 | else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ //if from a proxy 78 | $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 79 | } 80 | else { 81 | $ip = $_SERVER['REMOTE_ADDR']; 82 | } 83 | return $ip; 84 | } 85 | 86 | 87 | protected function _compareIp($userIp, $compareIp) 88 | { 89 | $compareIpLowerBoundary = str_replace("*", "0", $compareIp); 90 | $compareIpUpperBoundary = str_replace("*", "255", $compareIp); 91 | 92 | if(ip2long($compareIpLowerBoundary) <= ip2long($userIp) && 93 | ip2long($userIp) <= ip2long($compareIpUpperBoundary)) 94 | { 95 | return true; 96 | } 97 | else{ 98 | return false; 99 | } 100 | } 101 | 102 | } 103 | 104 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Cakephp Maintenance Mode 2 | ======================== 3 | 4 | CakePHP Maintenance Mode is to put your website in maintenance mode while you do your upgrade. Most ppl would add a static file on the server and route all request to it during maintenance. The file is shown to everybody including you unless a rewrite rule or expressions is added to exclude yourself which would complicate the matter. 5 | 6 | This filter file would tell CakePHP to put all requests to maintenance mode i.e show a msg but would allow requests from your IPs to be processed normally. 7 | 8 | ##Requirement 9 | CakePHP 2.* 10 | 11 | 12 | ## Installation 13 | 14 | _[Using [Composer](http://getcomposer.org/)]_ 15 | 16 | [View on Packagist](https://packagist.org/packages/awebdeveloper/cakephp-maintenance-mode), and copy 17 | the JSON snippet for the latest version into your project's `composer.json`. Eg, v. 1.2.0 would look like this: 18 | 19 | { 20 | "require": { 21 | "awebdeveloper/cakephp-maintenance-mode": "1.2.0" 22 | } 23 | } 24 | 25 | Because this plugin has the type `cakephp-plugin` set in it's own `composer.json`, composer knows to install it inside your `/Plugin` directory, rather than in the usual vendor directory. 26 | It is recommended that you add `/Plugin/MaintenanceMode` to your .gitignore file. (Why? [Read this](http://getcomposer.org/doc/faqs/should-i-commit-the-dependencies-in-my-vendor-directory.md).) 27 | 28 | _[Manual]_ 29 | 30 | * Download this: [http://github.com/awebdeveloper/cakephp-maintenance-mode/zipball/master](http://github.com/awebdeveloper/cakephp-maintenance-mode/zipball/master) 31 | * Unzip that download. 32 | * Copy the resulting folder to `app/Plugin` 33 | * Rename the folder you just copied to `MaintenanceMode` 34 | 35 | _[GIT Submodule]_ 36 | 37 | In your app directory type: 38 | 39 | git submodule add -b master git://github.com/awebdeveloper/cakephp-maintenance-mode.git Plugin/MaintenanceMode 40 | git submodule init 41 | git submodule update 42 | 43 | _[GIT Clone]_ 44 | 45 | In your `Plugin` directory type: 46 | 47 | git clone -b master git://github.com/awebdeveloper/cakephp-maintenance-mode.git MaintenanceMode 48 | 49 | 50 | ### Usage ### 51 | 52 | In your **Bootstrap.php** add 53 | 54 | ```php 55 | Configure::write('MaintenanceMode', array( 56 | 'enabled' => true, 57 | 'view' => array( 58 | 'layout' => '', 59 | 'template' => 'MaintenanceMode/index' 60 | ), 61 | 'ip_filters' => array('127.0.*.*') 62 | )); 63 | ``` 64 | 65 | Also in the same file find the below code and add this line 66 | 67 | ```php 68 | Configure::write('Dispatcher.filters', array( 69 | 'AssetDispatcher', 70 | 'CacheDispatcher', 71 | 'MaintenanceMode.MaintenanceMode' ## this line 72 | )); 73 | ``` 74 | 75 | ### Parameters ### 76 | It supports Following Parameters 77 | 78 | 1. **enabled** set this to *true* to enable it 79 | 2. **view** this is a array that accepts the template and the layout. If layout key is absent it will use the default layout. 80 | ``` 81 | 'view' => array( 82 | 'template' => 'Pages/index' 83 | ), 84 | ``` 85 | or 86 | 87 | ``` 88 | 'view' => array( 89 | 'layout' => 'main', 90 | 'template' => 'MaintenanceMode/index' 91 | ), 92 | ``` 93 | 3. **ip_filters** this is either a string containing a single IP or an array containing multiple IPs for which maintainance mode will NOT be applied. 94 | 95 | ``` 96 | 'ip_filters' => array('127.0.*.*','201.201.201.1') 97 | ``` 98 | or 99 | 100 | ``` 101 | 'ip_filters' => '201.201.201.1' 102 | ``` 103 | 104 | 105 | 106 | 107 | For info read the docs on dispatch filter 108 | 109 | http://book.cakephp.org/2.0/en/development/dispatch-filters.html 110 | 111 | 112 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | 2 | { 3 | "name": "awebdeveloper/cakephp-maintenance-mode", 4 | "description": "CakePHP Maintenance Mode", 5 | "type": "cakephp-plugin", 6 | "keywords": [ 7 | "cakephp", 8 | "dispatcher", 9 | "maintenance" 10 | ], 11 | "homepage": "http://github.com/awebdeveloper/cakephp-maintenance-mode", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name":"Prathik Shetty", 16 | "role":"Maintainer", 17 | "homepage":"https://github.com/awebdeveloper" 18 | }, 19 | { 20 | "name": "Jose Diaz-Gonzalez", 21 | "email": "email@josediazgonzalez.com", 22 | "homepage": "http://josediazgonzalez.com", 23 | "role": "Contributor" 24 | } 25 | ], 26 | "require": { 27 | "composer/installers": "*" 28 | }, 29 | "support": { 30 | "issues": "https://github.com/awebdeveloper/cakephp-maintenance-mode/issues", 31 | "source": "https://github.com/awebdeveloper/cakephp-maintenance-mode" 32 | }, 33 | "extra": { 34 | "installer-name": "MaintenanceMode" 35 | } 36 | } 37 | --------------------------------------------------------------------------------