Ejemplo n.º 1
0
 /**
  * Parses a string url into an array. Parsed urls will result in an automatic
  * redirection
  *
  * @param string $url The url to parse
  * @return boolean False on failure
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!$this->response) {
         $this->response = new CakeResponse();
     }
     $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 += array('named' => $params['named'], 'pass' => $params['pass'], 'url' => array());
         $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(array('Location' => Router::url($redirect, true)));
     $this->response->statusCode($status);
     $this->response->send();
     $this->_stop();
 }
 /**
  * Parses a string url into an array. Parsed urls will result in an automatic
  * redirection
  *
  * @param string $url The url to parse
  * @return boolean False on failure
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if (!$params) {
         return false;
     }
     if (!$this->response) {
         $this->response = new CakeResponse();
     }
     $redirect = $this->defaults;
     if (count($this->defaults) == 1 && !isset($this->defaults['controller'])) {
         $redirect = $this->defaults[0];
     }
     if (isset($this->options['persist']) && is_array($redirect)) {
         $argOptions['context'] = array('action' => $redirect['action'], 'controller' => $redirect['controller']);
         $args = Router::getArgs($params['_args_'], $argOptions);
         $redirect += $args['pass'];
         $redirect += $args['named'];
     }
     $status = 301;
     if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
         $status = $this->options['status'];
     }
     $this->response->header(array('Location' => Router::url($redirect, true)));
     $this->response->statusCode($status);
     $this->response->send();
 }
Ejemplo n.º 3
0
 /**
  * Parses a string url into an array. Parsed urls will result in an automatic
  * redirection
  *
  * @param string $url The url to parse
  * @return boolean False on failure
  */
 public function parse($url)
 {
     $params = parent::parse($url);
     if ($params === false) {
         return false;
     }
     $Domains = new Domains();
     $subdomain = $Domains->getSubdomain();
     $masterDomain = Configure::read('Domain.Master');
     $defaultRoute = Configure::read('Domain.DefaultRoute');
     $Tenant = new Tenant();
     if (!$Tenant->domainExists($subdomain) && $params != $defaultRoute) {
         if (!$this->response) {
             $this->response = new CakeResponse();
         }
         debug($this->response);
         die;
         $status = 307;
         $redirect = $defaultRoute;
         $this->response->header(array('Location' => Router::url($redirect, true)));
         $this->response->statusCode($status);
         $this->response->send();
         $this->_stop();
     }
     return $subdomain;
 }
Ejemplo n.º 4
0
/**
* Tests the statusCode method
*
* @expectedException CakeException
*/
	public function testStatusCode() {
		$response = new CakeResponse();
		$this->assertEquals($response->statusCode(), 200);
		$response->statusCode(404);
		$this->assertEquals($response->statusCode(), 404);
		$this->assertEquals($response->statusCode(500), 500);

		//Throws exception
		$response->statusCode(1001);
	}
 /**
  * Authenticate user
  *
  * @param CakeRequest $request The request object
  * @param CakeResponse $response response object.
  * @return mixed.  False on login failure.  An array of User data on success.
  */
 public function authenticate(CakeRequest $request, CakeResponse $response)
 {
     $user = $this->getUser($request);
     if (!$user) {
         $response->statusCode(401);
         $response->send();
     }
     return $user;
 }
 /**
  * Authenticate a user using basic HTTP auth.  Will use the configured User model and attempt a
  * login using basic HTTP auth.
  *
  * @param CakeRequest $request The request to authenticate with.
  * @param CakeResponse $response The response to add headers to.
  * @return mixed Either false on failure, or an array of user data on success.
  */
 public function authenticate(CakeRequest $request, CakeResponse $response)
 {
     $result = $this->getUser($request);
     if (empty($result)) {
         $response->header($this->loginHeaders());
         $response->statusCode(401);
         $response->send();
         return false;
     }
     return $result;
 }
Ejemplo n.º 7
0
 /**
  * Checks if a requested asset exists and sends it to the browser
  *
  * @param $url string $url Requested URL
  * @return boolean True on success if the asset file was found and sent
  */
 public function asset($url)
 {
     if (strpos($url, '..') !== false || strpos($url, '.') === false) {
         return false;
     }
     $filters = Configure::read('Asset.filter');
     $isCss = strpos($url, 'ccss/') === 0 || preg_match('#^(theme/([^/]+)/ccss/)|(([^/]+)(?<!css)/ccss)/#i', $url);
     $isJs = strpos($url, 'cjs/') === 0 || preg_match('#^/((theme/[^/]+)/cjs/)|(([^/]+)(?<!js)/cjs)/#i', $url);
     if (!$this->response) {
         $this->response = new CakeResponse();
     }
     if ($isCss && empty($filters['css']) || $isJs && empty($filters['js'])) {
         $this->response->statusCode(404);
         $this->response->send();
         return true;
     } elseif ($isCss) {
         include WWW_ROOT . DS . $filters['css'];
         return true;
     } elseif ($isJs) {
         include WWW_ROOT . DS . $filters['js'];
         return true;
     }
     $controller = null;
     $pathSegments = explode('.', $url);
     $ext = array_pop($pathSegments);
     $parts = explode('/', $url);
     $assetFile = null;
     if ($parts[0] === 'theme') {
         $themeName = $parts[1];
         unset($parts[0], $parts[1]);
         $fileFragment = implode(DS, $parts);
         $path = App::themePath($themeName) . 'webroot' . DS;
         if (file_exists($path . $fileFragment)) {
             $assetFile = $path . $fileFragment;
         }
     } else {
         $plugin = $parts[0];
         unset($parts[0]);
         $fileFragment = implode(DS, $parts);
         $pluginWebroot = App::pluginPath($plugin) . 'webroot' . DS;
         if (file_exists($pluginWebroot . $fileFragment)) {
             $assetFile = $pluginWebroot . $fileFragment;
         }
     }
     if ($assetFile !== null) {
         $this->_deliverAsset($assetFile, $ext);
         return true;
     }
     return false;
 }
Ejemplo n.º 8
0
 /**
  * test beforeRender() method
  *
  * @test
  */
 public function beforeRender()
 {
     $this->generateComponent(['mocks' => ['Api' => ['isApiRequest', 'failure', 'setResponse', '_getDbLog', 'getResponse']], 'componentOptions' => ['logDb' => true]]);
     $this->Api->expects($this->once())->method('failure');
     $this->Api->expects($this->once())->method('isApiRequest')->will($this->returnValue(true));
     $this->Api->expects($this->once())->method('_getDbLog')->will($this->returnValue('testDbLog'));
     $this->Api->expects($this->once())->method('setResponse')->with('dbLog', 'testDbLog');
     $this->Api->expects($this->once())->method('getResponse')->will($this->returnValue(['code' => 401]));
     $this->Api->beforeRender($this->controller);
     $this->assertSame(401, $this->response->statusCode());
     $this->assertArrayHasKey('response', $this->controller->viewVars);
     $this->assertSame(['code' => 401], $this->controller->viewVars['response']);
     $this->assertArrayHasKey('_serialize', $this->controller->viewVars);
     $this->assertSame('response', $this->controller->viewVars['_serialize']);
     $this->assertSame('Json', $this->controller->viewClass);
 }
Ejemplo n.º 9
0
/**
 * Handles (fakes) redirects for Ajax requests using requestAction()
 *
 * @param Controller $controller A reference to the controller
 * @param string|array $url A string or array containing the redirect location
 * @param mixed $status HTTP Status for redirect
 * @param boolean $exit
 * @return void
 */
	public function beforeRedirect($controller, $url, $status = null, $exit = true) {
		if (!$this->request->is('ajax')) {
			return;
		}
		foreach ($_POST as $key => $val) {
			unset($_POST[$key]);
		}
		if (is_array($url)) {
			$url = Router::url($url + array('base' => false));
		}
		if (!empty($status)) {
			$statusCode = $this->response->httpCodes($status);
			$code = key($statusCode);
			$this->response->statusCode($code);
		}
		$this->response->body($this->requestAction($url, array('return', 'bare' => false)));
		$this->response->send();
		$this->_stop();
	}
 /**
  * Handles (fakes) redirects for Ajax requests using requestAction()
  * Modifies the $_POST and $_SERVER['REQUEST_METHOD'] to simulate a new GET request.
  *
  * @param Controller   $controller A reference to the controller
  * @param string|array $url        A string or array containing the redirect location
  * @param int|array    $status     HTTP Status for redirect
  * @param bool         $exit       Whether to exit script, defaults to `true`.
  *
  * @return void
  */
 public function beforeRedirect(Controller $controller, $url, $status = NULL, $exit = TRUE)
 {
     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));
     }
     if (!empty($status)) {
         $statusCode = $this->response->httpCodes($status);
         $code = key($statusCode);
         $this->response->statusCode($code);
     }
     $this->response->body($this->requestAction($url, array('return', 'bare' => FALSE)));
     $this->response->send();
     $this->_stop();
 }
Ejemplo n.º 11
0
 /**
  * Redirects to given $url, after turning off $this->autoRender.
  * Script execution is halted after the redirect.
  *
  * @param string|array $url A string or array-based URL pointing to another location within the app,
  *     or an absolute URL
  * @param int|array|null $status HTTP status code (eg: 301). Defaults to 302 when null is passed.
  * @param bool $exit If true, exit() will be called after the redirect
  * @return void
  * @triggers Controller.beforeRedirect $this, array($url, $status, $exit)
  * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect
  */
 public function redirect($url, $status = null, $exit = true)
 {
     $this->autoRender = false;
     if (is_array($status)) {
         extract($status, EXTR_OVERWRITE);
     }
     $event = new CakeEvent('Controller.beforeRedirect', $this, array($url, $status, $exit));
     list($event->break, $event->breakOn, $event->collectReturn) = array(true, false, true);
     $this->getEventManager()->dispatch($event);
     if ($event->isStopped()) {
         return;
     }
     $response = $event->result;
     extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);
     if ($url !== null) {
         $this->response->header('Location', Router::url($url, true));
     }
     if (is_string($status)) {
         $codes = array_flip($this->response->httpCodes());
         if (isset($codes[$status])) {
             $status = $codes[$status];
         }
     }
     if ($status === null) {
         $status = 302;
     }
     $this->response->statusCode($status);
     if ($exit) {
         $this->response->send();
         $this->_stop();
     }
 }
Ejemplo n.º 12
0
 /**
  * Redirects to given $url, after turning off $this->autoRender.
  * Script execution is halted after the redirect.
  *
  * @param mixed $url A string or array-based URL pointing to another location within the app,
  *     or an absolute URL
  * @param integer $status Optional HTTP status code (eg: 404)
  * @param boolean $exit If true, exit() will be called after the redirect
  * @return mixed void if $exit = false. Terminates script if $exit = true
  * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect
  */
 public function redirect($url, $status = null, $exit = true)
 {
     $this->autoRender = false;
     if (is_array($status)) {
         extract($status, EXTR_OVERWRITE);
     }
     $event = new CakeEvent('Controller.beforeRedirect', $this, array($url, $status, $exit));
     //TODO: Remove the following line when the events are fully migrated to the CakeEventManager
     list($event->break, $event->breakOn, $event->collectReturn) = array(true, false, true);
     $this->getEventManager()->dispatch($event);
     if ($event->isStopped()) {
         return;
     }
     $response = $event->result;
     extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);
     if (function_exists('session_write_close')) {
         session_write_close();
     }
     if (!empty($status) && is_string($status)) {
         $codes = array_flip($this->response->httpCodes());
         if (isset($codes[$status])) {
             $status = $codes[$status];
         }
     }
     if ($url !== null) {
         $this->response->header('Location', Router::url($url, true));
     }
     if (!empty($status) && ($status >= 300 && $status < 400)) {
         $this->response->statusCode($status);
     }
     if ($exit) {
         $this->response->send();
         $this->_stop();
     }
 }
Ejemplo n.º 13
0
/**
 * Redirects to given $url, after turning off $this->autoRender.
 * Script execution is halted after the redirect.
 *
 * @param mixed $url A string or array-based URL pointing to another location within the app,
 *     or an absolute URL
 * @param integer $status Optional HTTP status code (eg: 404)
 * @param boolean $exit If true, exit() will be called after the redirect
 * @return mixed void if $exit = false. Terminates script if $exit = true
 * @link http://book.cakephp.org/2.0/en/controllers.html#Controller::redirect
 */
	public function redirect($url, $status = null, $exit = true) {
		$this->autoRender = false;

		if (is_array($status)) {
			extract($status, EXTR_OVERWRITE);
		}
		$response = $this->Components->trigger(
			'beforeRedirect',
			array(&$this, $url, $status, $exit),
			array('break' => true, 'breakOn' => false, 'collectReturn' => true)
		);

		if ($response === false) {
			return;
		}
		extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);

		$response = $this->beforeRedirect($url, $status, $exit);
		if ($response === false) {
			return;
		}
		extract($this->_parseBeforeRedirect($response, $url, $status, $exit), EXTR_OVERWRITE);

		if (function_exists('session_write_close')) {
			session_write_close();
		}

		if (!empty($status) && is_string($status)) {
			$codes = array_flip($this->response->httpCodes());
			if (isset($codes[$status])) {
				$status = $codes[$status];
			}
		}

		if ($url !== null) {
			$this->response->header('Location', Router::url($url, true));
		}

		if (!empty($status) && ($status >= 300 && $status < 400)) {
			$this->response->statusCode($status);
		}

		if ($exit) {
			$this->response->send();
			$this->_stop();
		}
	}
Ejemplo n.º 14
0
 /**
  * Display or download the given file
  *
  * @param string $view Not used
  * @param string $layout Not used
  * @return mixed
  * @throws NotFoundException
  */
 public function render($view = null, $layout = null)
 {
     $name = $download = $extension = $id = $modified = $path = $cache = $mimeType = $compress = null;
     extract($this->viewVars, EXTR_OVERWRITE);
     if (is_dir($path)) {
         $path = $path . $id;
     } else {
         $path = APP . $path . $id;
     }
     if (!is_file($path)) {
         if (Configure::read('debug')) {
             throw new NotFoundException(sprintf('The requested file %s was not found', $path));
         }
         throw new NotFoundException('The requested file was not found');
     }
     if (is_array($mimeType)) {
         $this->response->type($mimeType);
     }
     if (isset($extension) && $this->_isActive()) {
         $extension = strtolower($extension);
         $chunkSize = 8192;
         $buffer = '';
         $fileSize = @filesize($path);
         $handle = fopen($path, 'rb');
         if ($handle === false) {
             return false;
         }
         if (!empty($modified) && !is_numeric($modified)) {
             $modified = strtotime($modified, time());
         } else {
             $modified = time();
         }
         if ($this->response->type($extension) === false) {
             $download = true;
         }
         if ($cache) {
             $this->response->cache($modified, $cache);
         } else {
             $this->response->header(array('Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT', 'Expires' => '0', 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0', 'Pragma' => 'no-cache'));
         }
         if ($download) {
             $agent = env('HTTP_USER_AGENT');
             if (preg_match('%Opera(/| )([0-9].[0-9]{1,2})%', $agent)) {
                 $contentType = 'application/octetstream';
             } else {
                 if (preg_match('/MSIE ([0-9].[0-9]{1,2})/', $agent)) {
                     $contentType = 'application/force-download';
                 }
             }
             if (!empty($contentType)) {
                 $this->response->type($contentType);
             }
             if (is_null($name)) {
                 $name = $id;
             }
             $this->response->download($name);
             $this->response->header(array('Accept-Ranges' => 'bytes'));
             $httpRange = env('HTTP_RANGE');
             if (isset($httpRange)) {
                 list($toss, $range) = explode('=', $httpRange);
                 $size = $fileSize - 1;
                 $length = $fileSize - $range;
                 $this->response->header(array('Content-Length' => $length, 'Content-Range' => 'bytes ' . $range . $size . '/' . $fileSize));
                 $this->response->statusCode(206);
                 fseek($handle, $range);
             } else {
                 $this->response->header('Content-Length', $fileSize);
             }
         } else {
             $this->response->header(array('Content-Length' => $fileSize));
         }
         $this->_clearBuffer();
         if ($compress) {
             $this->_compressionEnabled = $this->response->compress();
         }
         $this->response->send();
         return $this->_sendFile($handle);
     }
     return false;
 }