2015-09-17 23:11:18 -04:00
|
|
|
<?php
|
2015-11-16 11:40:01 -05:00
|
|
|
/**
|
|
|
|
* Ion
|
|
|
|
*
|
|
|
|
* Building blocks for web development
|
|
|
|
*
|
|
|
|
* @package Ion
|
|
|
|
* @author Timothy J. Warren
|
2016-01-04 16:58:33 -05:00
|
|
|
* @copyright Copyright (c) 2015 - 2016
|
2015-11-16 11:40:01 -05:00
|
|
|
* @license MIT
|
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
|
|
|
|
namespace Aviat\Ion\View;
|
|
|
|
|
2016-02-17 11:36:37 -05:00
|
|
|
use Zend\Diactoros\Response;
|
2016-02-16 16:28:44 -05:00
|
|
|
use Zend\Diactoros\Response\SapiEmitter;
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\View as BaseView;
|
|
|
|
|
2015-10-09 22:29:59 -04:00
|
|
|
/**
|
|
|
|
* Base view class for Http output
|
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
class HttpView extends BaseView {
|
2015-10-09 22:29:59 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
/**
|
|
|
|
* Do a redirect
|
2015-10-09 22:29:59 -04:00
|
|
|
*
|
2016-03-07 14:37:49 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-09-17 23:11:18 -04:00
|
|
|
* @param string $url
|
|
|
|
* @param int $code
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function redirect($url, $code)
|
|
|
|
{
|
2016-02-17 11:36:37 -05:00
|
|
|
ob_start();
|
2016-03-03 16:53:17 -05:00
|
|
|
$message = $this->response->getReasonPhrase($code);
|
|
|
|
$this->setStatusCode($code);
|
|
|
|
$this->response->withHeader('Location', $url);
|
2016-02-17 11:36:37 -05:00
|
|
|
|
2016-03-03 16:53:17 -05:00
|
|
|
if (PHP_SAPI !== 'cli')
|
|
|
|
{
|
|
|
|
header("HTTP/1.1 ${code} ${message}");
|
|
|
|
header("Location: {$url}");
|
|
|
|
}
|
2016-02-17 11:36:37 -05:00
|
|
|
|
|
|
|
$this->hasRendered = TRUE;
|
|
|
|
ob_end_clean();
|
2016-01-08 15:53:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the status code of the request
|
|
|
|
*
|
|
|
|
* @param int $code
|
|
|
|
* @return HttpView
|
|
|
|
*/
|
|
|
|
public function setStatusCode($code)
|
|
|
|
{
|
2016-03-07 14:37:49 -05:00
|
|
|
$this->response = $this->response->withStatus($code)
|
2016-03-29 11:40:27 -04:00
|
|
|
->withProtocolVersion('1.1');
|
2016-01-08 15:53:50 -05:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send output to client
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function send()
|
|
|
|
{
|
|
|
|
$this->hasRendered = TRUE;
|
|
|
|
$this->output();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the appropriate response
|
|
|
|
*
|
2016-01-11 10:42:34 -05:00
|
|
|
* @codeCoverageIgnore
|
2016-01-08 15:53:50 -05:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function output()
|
|
|
|
{
|
2016-02-16 16:28:44 -05:00
|
|
|
$this->response->withHeader('Content-type', "{$this->contentType};charset=utf-8")
|
|
|
|
->withHeader('Content-Security-Policy', "script-src 'self'")
|
|
|
|
->withHeader('X-Content-Type-Options', 'nosniff')
|
|
|
|
->withHeader('X-XSS-Protection', '1;mode=block')
|
|
|
|
->withHeader('X-Frame-Options', 'SAMEORIGIN');
|
2016-01-08 15:53:50 -05:00
|
|
|
|
2016-02-16 16:28:44 -05:00
|
|
|
$sender = new SapiEmitter($this->response);
|
2016-02-17 10:29:05 -05:00
|
|
|
$sender->emit($this->response);
|
2016-01-08 15:53:50 -05:00
|
|
|
}
|
2015-10-09 22:29:59 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
}
|