|
| 1 | +<?php |
| 2 | +namespace Maintenance\Middleware; |
| 3 | + |
| 4 | +use Cake\Core\InstanceConfigTrait; |
| 5 | +use Cake\Utility\Inflector; |
| 6 | +use Cake\View\ViewBuilder; |
| 7 | +use Cake\Network\Request; |
| 8 | + |
| 9 | +class MaintenanceMiddleware |
| 10 | +{ |
| 11 | + use InstanceConfigTrait; |
| 12 | + |
| 13 | + /** |
| 14 | + * Default config. |
| 15 | + * |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + protected $_defaultConfig = [ |
| 19 | + 'allowIp' => [], |
| 20 | + |
| 21 | + 'className' => 'Cake\View\View', |
| 22 | + 'templatePath' => 'Error', |
| 23 | + |
| 24 | + 'statusFilePath' => TMP, |
| 25 | + 'statusFileName' => 'maintenance', |
| 26 | + 'statusCode' => 503, |
| 27 | + |
| 28 | + 'ctpFileName' => 'maintenance', |
| 29 | + 'ctpExtension' => '.ctp', |
| 30 | + |
| 31 | + 'contentType' => 'text/html' |
| 32 | + ]; |
| 33 | + |
| 34 | + public function __construct($config = []) |
| 35 | + { |
| 36 | + $this->config($config); |
| 37 | + } |
| 38 | + |
| 39 | + public function __invoke($request, $response, $next) |
| 40 | + { |
| 41 | + $isActive = $this->isMaintenance($request); |
| 42 | + |
| 43 | + if ($isActive === false) { |
| 44 | + $response = $next($request, $response); |
| 45 | + } else { |
| 46 | + $response = $this->execute($response); |
| 47 | + } |
| 48 | + |
| 49 | + return $response; |
| 50 | + } |
| 51 | + |
| 52 | + private function execute($response) |
| 53 | + { |
| 54 | + $cakeRequest = Request::createFromGlobals(); |
| 55 | + $builder = new ViewBuilder(); |
| 56 | + |
| 57 | + $className = $this->config('className'); |
| 58 | + $templateName = $this->config('ctpFileName'); |
| 59 | + $templatePath = $this->config('templatePath'); |
| 60 | + $ext = $this->config('ctpExtension'); |
| 61 | + $contentType = $this->config('contentType'); |
| 62 | + $statusCode = $this->config('statusCode'); |
| 63 | + |
| 64 | + $view = $builder |
| 65 | + ->className($className) |
| 66 | + ->templatePath(Inflector::camelize($templatePath)) |
| 67 | + ->layout(false) |
| 68 | + ->build([], $cakeRequest); |
| 69 | + $view->_ext = $ext; |
| 70 | + $bodyString = $view->render($templateName); |
| 71 | + |
| 72 | + $body = $response->withHeader('Content-Type', $contentType) |
| 73 | + ->withStatus($statusCode) |
| 74 | + ->getBody(); |
| 75 | + $body->write($bodyString); |
| 76 | + |
| 77 | + return $response; |
| 78 | + } |
| 79 | + |
| 80 | + private function isMaintenance($request) |
| 81 | + { |
| 82 | + $fullPath = $this->config('statusFilePath') . $this->config('statusFileName'); |
| 83 | + $ret = file_exists($fullPath); |
| 84 | + if ($ret === false) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + $ret = $this->isAllowIp($request); |
| 89 | + if ($ret === true) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + private function isAllowIp($request) |
| 97 | + { |
| 98 | + $params = $request->getServerParams(); |
| 99 | + $myIpAddress = $params['REMOTE_ADDR']; |
| 100 | + |
| 101 | + $ipAddressList = $this->config('allowIp'); |
| 102 | + if (empty($ipAddressList)) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + foreach ($ipAddressList as $allowIP) { |
| 107 | + // サブネットマスクが指定されていない場合 /32 を追加 |
| 108 | + if (strpos($allowIP, '/') == 0) { |
| 109 | + $allowIP .= '/32'; |
| 110 | + } |
| 111 | + // IPアドレスの書式チェック |
| 112 | + if (!preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\/([1-9]|1[0-9]|2[0-9]|3[0-2])$/', $allowIP)) { |
| 113 | + // 書式が不正 |
| 114 | + continue; |
| 115 | + } |
| 116 | + list($ip, $maskBit) = explode("/", $allowIP); |
| 117 | + $ipLong = ip2long($ip) >> (32 - $maskBit); |
| 118 | + |
| 119 | + $selfIpLong = ip2long($myIpAddress) >> (32 - $maskBit); |
| 120 | + if ($selfIpLong === $ipLong) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return false; |
| 126 | + } |
| 127 | +} |
0 commit comments