/** * @param \Cake\Network\Response $response The response. * @return \Cake\Network\Response */ protected function build($response) { $cakeRequest = Request::createFromGlobals(); $builder = new ViewBuilder(); $templateName = $this->config('templateFileName'); $templatePath = $this->config('templatePath'); $view = $builder->className($this->config('className'))->templatePath(Inflector::camelize($templatePath))->layout($this->config('templateLayout'))->build([], $cakeRequest); $view->_ext = $this->config('templateExtension'); $bodyString = $view->render($templateName); $response = $response->withHeader('Retry-After', (string) HOUR)->withHeader('Content-Type', $this->config('contentType'))->withStatus($this->config('statusCode')); $body = $response->getBody(); $body->write($bodyString); return $response; }
/** * The Front Controller for handling every request * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @since 0.2.9 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ // for built-in server if (php_sapi_name() === 'cli-server') { $_SERVER['PHP_SELF'] = '/' . basename(__FILE__); $url = parse_url(urldecode($_SERVER['REQUEST_URI'])); $file = __DIR__ . $url['path']; if (strpos($url['path'], '..') === false && strpos($url['path'], '.') !== false && is_file($file)) { return false; } } require dirname(__DIR__) . '/config/bootstrap.php'; use Cake\Network\Request; use Cake\Network\Response; use Cake\Routing\DispatcherFactory; $dispatcher = DispatcherFactory::create(); $dispatcher->dispatch(Request::createFromGlobals(), new Response());
/** * Initialize config data and properties. * * @param array $config The config data. * @return void */ public function initialize(array $config) { if (!$this->_config['key']) { $this->config('key', Security::salt()); } $controller = $this->_registry->getController(); if ($controller && isset($controller->request)) { $this->_request = $controller->request; } else { $this->_request = Request::createFromGlobals(); } if (empty($this->_config['path'])) { $this->config('path', $this->_request->base ?: '/'); } if ($controller && isset($controller->response)) { $this->_response = $controller->response; } else { $this->_response = new Response(); } }
/** * Get the controller instance to handle the exception. * Override this method in subclasses to customize the controller used. * This method returns the built in `ErrorController` normally, or if an error is repeated * a bare controller will be used. * * @return \Cake\Controller\Controller * @triggers Controller.startup $controller */ protected function _getController() { if (!($request = Router::getRequest(true))) { $request = Request::createFromGlobals(); } $response = new Response(); try { $class = App::className('Error', 'Controller', 'Controller'); $controller = new $class($request, $response); $controller->startupProcess(); $startup = true; } catch (Exception $e) { $startup = false; } // Retry RequestHandler, as another aspect of startupProcess() // could have failed. Ignore any exceptions out of startup, as // there could be userland input data parsers. if ($startup === false && !empty($controller) && isset($controller->RequestHandler)) { try { $event = new Event('Controller.startup', $controller); $controller->RequestHandler->startup($event); } catch (Exception $e) { } } if (empty($controller)) { $controller = new Controller($request, $response); } return $controller; }
/** * Test the content type method. * * @return void */ public function testContentType() { $_SERVER['HTTP_CONTENT_TYPE'] = 'application/json'; $request = Request::createFromGlobals(); $this->assertEquals('application/json', $request->contentType()); $_SERVER['CONTENT_TYPE'] = 'application/xml'; $request = Request::createFromGlobals(); $this->assertEquals('application/xml', $request->contentType(), 'prefer non http header.'); }
/** * Checks to see if the given URL can be parsed by this route. * * If the route can be parsed an array of parameters will be returned; if not * false will be returned. String URLs are parsed if they match a routes regular expression. * * @param string $url The URL to attempt to parse. * @return array|false Boolean false on failure, otherwise an array of parameters. */ public function parse($url) { $request = Router::getRequest(true) ?: Request::createFromGlobals(); if (empty($this->_compiledRoute)) { $this->compile(); } list($url, $ext) = $this->_parseExtension($url); if (!preg_match($this->_compiledRoute, urldecode($url), $route)) { return false; } if (isset($this->defaults['_method'])) { $method = $request->env('REQUEST_METHOD'); if (!in_array($method, (array) $this->defaults['_method'], true)) { return false; } } array_shift($route); $count = count($this->keys); for ($i = 0; $i <= $count; $i++) { unset($route[$i]); } $route['pass'] = []; // Assign defaults, set passed args to pass foreach ($this->defaults as $key => $value) { if (isset($route[$key])) { continue; } if (is_int($key)) { $route['pass'][] = $value; continue; } $route[$key] = $value; } if (isset($route['_args_'])) { $pass = $this->_parseArgs($route['_args_'], $route); $route['pass'] = array_merge($route['pass'], $pass); unset($route['_args_']); } if (isset($route['_trailing_'])) { $route['pass'][] = $route['_trailing_']; unset($route['_trailing_']); } if (!empty($ext)) { $route['_ext'] = $ext; } // restructure 'pass' key route params if (isset($this->options['pass'])) { $j = count($this->options['pass']); while ($j--) { if (isset($route[$this->options['pass'][$j]])) { array_unshift($route['pass'], $route[$this->options['pass'][$j]]); } } } return $route; }
/** * Get the controller instance to handle the exception. * Override this method in subclasses to customize the controller used. * This method returns the built in `ErrorController` normally, or if an error is repeated * a bare controller will be used. * * @return \Cake\Controller\Controller */ protected function _getController() { if (!($request = Router::getRequest(true))) { $request = Request::createFromGlobals(); } $response = new Response(); try { $controller = new ErrorController($request, $response); $controller->startupProcess(); } catch (Exception $e) { if (!empty($controller) && isset($controller->RequestHandler)) { $event = new Event('Controller.startup', $controller); $controller->RequestHandler->startup($event); } } if (empty($controller)) { $controller = new Controller($request, $response); $controller->viewPath = 'Error'; } return $controller; }
DispatcherFactory::add('ControllerFactory'); /** * Enable immutable time objects in the ORM. * * You can enable default locale format parsing by adding calls * to `useLocaleParser()`. This enables the automatic conversion of * locale specific date formats. For details see * @link http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#parsing-localized-datetime-data */ Type::build('time')->useImmutable(); Type::build('date')->useImmutable(); Type::build('datetime')->useImmutable(); /** * Bootstrap & load Site plugin */ $Request = Request::createFromGlobals(); $url = explode('/', $Request->url); $website_dir = 'websites'; $website_default = 'default'; $website_name = trim($url[0]) != '' ? trim($url[0]) : $website_default; $website_fullpath = ROOT . DS . $website_dir . DS . $website_name . DS; if (file_exists($website_fullpath)) { $plugins = []; if (Configure::check(['plugins'])) { $plugins = Configure::read(['plugins']); } $plugins['Site'] = $website_fullpath; Configure::write('plugins', $plugins); // Define default Site variables define('APP_WEBROOT', $Request->webroot); define('APP_BASE_URL', $Request->base);
/** * Initialize config data and properties. * * @param array $config The config data. * @return void */ public function initialize(array $config) { if (!$this->_config['key']) { $this->config('key', Security::salt()); } $controller = $this->_registry->getController(); if ($controller !== null) { $this->_response =& $controller->response; } if ($controller === null) { $this->request = Request::createFromGlobals(); $this->_response = new Response(); } if (empty($this->_config['path'])) { $this->config('path', $this->request->webroot); } }
/** * Checks to see if the given URL can be parsed by this route. * * If the route can be parsed an array of parameters will be returned; if not * false will be returned. String URLs are parsed if they match a routes regular expression. * * @param string $url The URL to attempt to parse. * @return mixed Boolean false on failure, otherwise an array or parameters */ public function parse($url) { $request = Router::getRequest(true) ?: Request::createFromGlobals(); if (!$this->compiled()) { $this->compile(); } list($url, $ext) = $this->_parseExtension($url); if (!preg_match($this->_compiledRoute, urldecode($url), $route)) { return false; } foreach ($this->defaults as $key => $val) { $key = (string) $key; if ($key[0] === '[' && preg_match('/^\\[(\\w+)\\]$/', $key, $header)) { if (isset($this->_headerMap[$header[1]])) { $header = $this->_headerMap[$header[1]]; } else { $header = 'http_' . $header[1]; } $header = strtoupper($header); $val = (array) $val; $h = false; foreach ($val as $v) { if ($request->env($header) === $v) { $h = true; } } if (!$h) { return false; } } } array_shift($route); $count = count($this->keys); for ($i = 0; $i <= $count; $i++) { unset($route[$i]); } $route['pass'] = []; // Assign defaults, set passed args to pass foreach ($this->defaults as $key => $value) { if (isset($route[$key])) { continue; } if (is_int($key)) { $route['pass'][] = $value; continue; } $route[$key] = $value; } if (isset($route['_args_'])) { $pass = $this->_parseArgs($route['_args_'], $route); $route['pass'] = array_merge($route['pass'], $pass); unset($route['_args_']); } if (isset($route['_trailing_'])) { $route['pass'][] = $route['_trailing_']; unset($route['_trailing_']); } if (!empty($ext)) { $route['_ext'] = $ext; } // restructure 'pass' key route params if (isset($this->options['pass'])) { $j = count($this->options['pass']); while ($j--) { if (isset($route[$this->options['pass'][$j]])) { array_unshift($route['pass'], $route[$this->options['pass'][$j]]); } } } return $route; }
/** * Build and set all the view properties needed to render the layout and template. * * @return array The rendered template wrapped in layout. */ protected function _render() { $viewClass = $this->viewRender(); $viewClass = App::className($viewClass, 'View', $viewClass == 'View' ? '' : 'View'); $View = new $viewClass(Request::createFromGlobals()); $View->viewVars = $this->_viewVars; $View->theme = $this->_theme; $View->layoutPath = 'pdf'; $View->viewPath = 'Pdf'; $View->view = $this->_template; $View->layout = $this->_layout; $View->helpers = $this->_helpers; $View->loadHelpers(); return $View->render(); }
/** * Constructor * * @param ComponentRegistry $collection A ComponentRegistry for this component * @param array $config Array of config. */ public function __construct(ComponentRegistry $collection, array $config = array()) { parent::__construct($collection, $config); if (!$this->_config['key']) { $this->config('key', Configure::read('Security.salt')); } if ($this->_config['time']) { $this->_expire($this->_config['time']); } $controller = $collection->getController(); if ($controller && isset($controller->request)) { $this->_request = $controller->request; } else { $this->_request = Request::createFromGlobals(); } if ($controller && isset($controller->response)) { $this->_response = $controller->response; } else { $this->_response = new Response(); } }
/** * Test environment detection * * @dataProvider environmentGenerator * @param $name * @param $env * @param $expected * @return void */ public function testEnvironmentDetection($name, $env, $expected) { $_GET = array(); $this->_loadEnvironment($env); $request = Request::createFromGlobals(); $this->assertEquals($expected['url'], $request->url, "url error"); $this->assertEquals($expected['base'], $request->base, "base error"); $this->assertEquals($expected['webroot'], $request->webroot, "webroot error"); if (isset($expected['urlParams'])) { $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch"); } }
/** * Tests getting the sessions from the request * * @return void */ public function testSession() { $session = new Session(); $request = new Request(['session' => $session]); $this->assertSame($session, $request->session()); $request = Request::createFromGlobals(); $this->assertEquals($session, $request->session()); }
public function currentUser() { $request = Request::createFromGlobals(); $session = $request->session(); return ['id' => $session->read('Auth.User.id'), 'ip' => $request->env('REMOTE_ADDR'), 'url' => $request->here(), 'description' => $session->read('Auth.User.username')]; }
/** * Return a static instance of the I18n class * * @return I18n */ public static function getInstance() { static $instance = array(); if (!$instance) { $instance[0] = new I18n(Request::createFromGlobals()); } return $instance[0]; }