/** * @param \Cake\Http\ServerRequest $request The request. * @param \Cake\Network\Response $response The response. * @param callable $next The next middleware to call. * @return \Psr\Http\Message\ResponseInterface A response. */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next) { $ip = $request->clientIp(); $Maintenance = new Maintenance(); if (!$Maintenance->isMaintenanceMode($ip)) { return $next($request, $response); } $response = $this->build($response); return $response; }
/** * MaintenanceMode::beforeDispatch() * * @param \Cake\Event\Event $event * @return \Cake\Network\Response|null */ public function beforeDispatch(Event $event) { /* @var \Cake\Http\ServerRequest $request */ $request = $event->data['request']; $ip = $request->clientIp(); $Maintenance = new Maintenance(); if (!$Maintenance->isMaintenanceMode($ip)) { return null; } $body = __d('setup', 'Maintenance work'); $body = $this->_body(); $event->data['response']->header('Retry-After', HOUR); $event->data['response']->statusCode(503); $event->data['response']->body($body); $event->stopPropagation(); return $event->data['response']; }
/** * MaintenanceLibTest::testWhitelist() * * @return void */ public function testWhitelist() { $result = $this->Maintenance->whitelist(); $this->assertEmpty($result); $whitelist = ['192.168.0.1']; $result = $this->Maintenance->whitelist($whitelist); $this->assertTrue($result); $result = $this->Maintenance->whitelist(); $this->assertNotEmpty($result); $result = $this->Maintenance->clearWhitelist(['192.111.111.111']); $this->assertTrue($result); $result = $this->Maintenance->whitelist(); $this->assertSame($whitelist, $result); $result = $this->Maintenance->clearWhitelist(); $result = $this->Maintenance->whitelist(); $this->assertEmpty($result); }
/** * Set maintance mode for everybody except for the own IP which will * be whitelisted. * * Alternatively, this can and should be done using the CLI shell. * * URL Options: * - `duration` query string can be used to set a timeout maintenance window * * @param mixed $maintenance * @return bool Success */ public function setMaintenance($maintenance) { $ip = env('REMOTE_ADDR'); // optional length in minutes $length = (int) $this->Controller->request->query('duration'); $Maintenance = new Maintenance(); if (!$Maintenance->setMaintenanceMode($maintenance ? $length : false)) { return false; } if (!$Maintenance->whitelist([$ip])) { return false; } return true; }