/** * Main functionality to trigger maintenance mode. * Will automatically set the appropriate headers. * * Tip: Check for non CLI first * * if (php_sapi_name() !== 'cli') { * App::uses('MaintenanceLib', 'Setup.Lib'); * $Maintenance = new MaintenanceLib(); * $Maintenance->checkMaintenance(); * } * * @param string|null $ipAddress * @param bool $exit If Response should be sent and exited. * @return void * @deprecated Use Maintenance DispatcherFilter */ public function checkMaintenance($ipAddress = null, $exit = true) { if ($ipAddress === null) { $ipAddress = env('REMOTE_ADDRESS'); } if (!$this->isMaintenanceMode($ipAddress)) { return; } $Response = new Response(); $Response->statusCode(503); $Response->header('Retry-After', DAY); $body = __d('setup', 'Maintenance work'); $template = APP . 'Template' . DS . 'Error' . DS . $this->template; if (file_exists($template)) { $body = file_get_contents($template); } $Response->body($body); if ($exit) { $Response->send(); exit; } }
public function beforeRedirect(Event $event, $url, Response $response) { // $logged_in = is_int($this->Controller->Auth->user('id')); $login_action = false; if (is_string($url)) { if (stripos($url, 'login') !== false) { $login_action = true; } } elseif (is_array($url)) { if ($url['action'] == 'login') { $login_action = true; } } if ($this->request->is('service') && !$logged_in && $login_action) { $this->data = [self::SUCCESS_KEY => false, self::ENABLE_KEY => true, self::CODE_KEY => 401, self::MESSAGE_KEY => __('You are not logged in!')]; $this->Controller->render(); $response->statusCode(201); $response->send(); $response->stop(); if (session_id()) { session_write_close(); } } }
/** * Handles (fakes) redirects for Ajax requests using requestAction() * * @param Event $event The Controller.beforeRedirect event. * @param string|array $url A string or array containing the redirect location * @param \Cake\Network\Response $response The response object. * @return void */ public function beforeRedirect(Event $event, $url, Response $response) { $request = $this->request; if (!$request->is('ajax')) { return; } if (empty($url)) { return; } if (is_array($url)) { $url = Router::url($url + ['_base' => false]); } $controller = $event->subject(); $response->body($controller->requestAction($url, ['return', 'bare' => false, 'environment' => ['REQUEST_METHOD' => 'GET']])); $response->send(); $response->stop(); }
/** * Sends an asset file to the client * * @param \Cake\Network\Request $request The request object to use. * @param \Cake\Network\Response $response The response object to use. * @param string $assetFile Path to the asset file in the file system * @param string $ext The extension of the file to determine its mime type * @return void */ protected function _deliverAsset(Request $request, Response $response, $assetFile, $ext) { ob_start(); $compressionEnabled = Configure::read('Asset.compress') && $response->compress(); if ($response->type($ext) === $ext) { $contentType = 'application/octet-stream'; $agent = $request->env('HTTP_USER_AGENT'); if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent) || preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) { $contentType = 'application/octetstream'; } $response->type($contentType); } if (!$compressionEnabled) { $response->header('Content-Length', filesize($assetFile)); } $response->cache(filemtime($assetFile)); $response->send(); ob_clean(); readfile($assetFile); if ($compressionEnabled) { ob_end_flush(); } }
/** * Handles (fakes) redirects for Ajax requests using requestAction() * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request. * * @param Event $event The Controller.beforeRedirect event. * @param string|array $url A string or array containing the redirect location * @param \Cake\Network\Response $response The response object. * @return void */ public function beforeRedirect(Event $event, $url, $response) { if (!$this->request->is('ajax')) { return; } if (empty($url)) { return; } $_SERVER['REQUEST_METHOD'] = 'GET'; foreach ($_POST as $key => $val) { unset($_POST[$key]); } if (is_array($url)) { $url = Router::url($url + array('base' => false)); } $controller = $event->subject(); $response->body($controller->requestAction($url, array('return', 'bare' => false))); $response->send(); $response->stop(); }
/** * Tests setting of public/private Cache-Control directives * * @return void */ public function testSharable() { $response = $this->getMock('Cake\\Network\\Response', array('_sendHeader', '_sendContent')); $this->assertNull($response->sharable()); $response->sharable(true); $headers = $response->header(); $this->assertEquals('public', $headers['Cache-Control']); $response->expects($this->at(1))->method('_sendHeader')->with('Cache-Control', 'public'); $response->send(); $response = $this->getMock('Cake\\Network\\Response', array('_sendHeader', '_sendContent')); $response->sharable(false); $headers = $response->header(); $this->assertEquals('private', $headers['Cache-Control']); $response->expects($this->at(1))->method('_sendHeader')->with('Cache-Control', 'private'); $response->send(); $response = $this->getMock('Cake\\Network\\Response', array('_sendHeader', '_sendContent')); $response->sharable(true); $headers = $response->header(); $this->assertEquals('public', $headers['Cache-Control']); $response->sharable(false); $headers = $response->header(); $this->assertEquals('private', $headers['Cache-Control']); $response->expects($this->at(1))->method('_sendHeader')->with('Cache-Control', 'private'); $response->send(); $this->assertFalse($response->sharable()); $response->sharable(true); $this->assertTrue($response->sharable()); $response = new Response(); $response->sharable(true, 3600); $headers = $response->header(); $this->assertEquals('public, s-maxage=3600', $headers['Cache-Control']); $response = new Response(); $response->sharable(false, 3600); $headers = $response->header(); $this->assertEquals('private, max-age=3600', $headers['Cache-Control']); $response->send(); }
/** * Parses a string URL into an array. Parsed URLs will result in an automatic * redirection * * @param string $url The URL to parse * @return bool False on failure */ public function parse($url) { $params = parent::parse($url); if (!$params) { return false; } if (!$this->response) { $this->response = new Response(); } $redirect = $this->redirect; if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) { $redirect = $this->redirect[0]; } if (isset($this->options['persist']) && is_array($redirect)) { $redirect += ['pass' => $params['pass'], 'url' => []]; if (is_array($this->options['persist'])) { foreach ($this->options['persist'] as $elem) { if (isset($params[$elem])) { $redirect[$elem] = $params[$elem]; } } } $redirect = Router::reverse($redirect); } $status = 301; if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) { $status = $this->options['status']; } $this->response->header(['Location' => Router::url($redirect, true)]); $this->response->statusCode($status); $this->response->send(); $this->response->stop(); }