Example #1
0
	/**
	 * Render this view and send all appropriate headers to the browser, (if applicable)
	 *
	 * @return void
	 */
	public function render() {

		// Before I go about rendering anything, enable UTF-8 to ensure proper i18n!
		if ($this->contenttype && $this->contenttype == View::CTYPE_HTML) {
			View::AddMeta('http-equiv="Content-Type" content="text/html;charset=UTF-8"');
		}

		$data = $this->fetch();

		// Be sure to send the content type and status to the browser, (if it's a page)
		if (
			!headers_sent() &&
			($this->mode == View::MODE_PAGE || $this->mode == View::MODE_PAGEORAJAX || $this->mode == View::MODE_AJAX || $this->mode == View::MODE_NOOUTPUT || $this->mode == View::MODE_EMAILORPRINT)
		) {
			switch ($this->error) {
				case View::ERROR_NOERROR:
					header('Status: 200 OK', true, $this->error);
					break;
				case View::ERROR_BADREQUEST:
					header('Status: 400 Bad Request', true, $this->error);
					break;
				case View::ERROR_UNAUTHORIZED:
					header('Status: 401 Unauthorized', true, $this->error);
					break;
				case View::ERROR_PAYMENTREQUIRED:
					header('Status: 402 Payment Required', true, $this->error);
					break;
				case View::ERROR_ACCESSDENIED:
					header('Status: 403 Forbidden', true, $this->error);
					break;
				case View::ERROR_NOTFOUND:
					header('Status: 404 Not Found', true, $this->error);
					break;
				case View::ERROR_METHODNOTALLOWED:
					header('Status: 405 Method Not Allowed', true, $this->error);
					break;
				case View::ERROR_NOTACCEPTABLE:
					header('Status: 406 Not Acceptable', true, $this->error);
					break;
				case View::ERROR_PROXYAUTHENTICATIONREQUIRED:
					header('Status: 407 Proxy Authentication Required', true, $this->error);
					break;
				case View::ERROR_REQUESTTIMEOUT:
					header('Status: 408 Request Time-out', true, $this->error);
					break;
				case View::ERROR_CONFLICT:
					header('Status: 409 Conflict', true, $this->error);
					break;
				case View::ERROR_GONE:
					header('Status: 410 Gone', true, $this->error);
					break;
				case View::ERROR_LENGTHREQUIRED:
					header('Status: 411 Length Required', true, $this->error);
					break;
				case View::ERROR_PRECONDITIONFAILED:
					header('Status: 412 Precondition Failed', true, $this->error);
					break;
				case View::ERROR_ENTITYTOOLARGE:
					header('Status: 413 Request Entity Too Large', true, $this->error);
					break;
				case View::ERROR_URITOOLARGE:
					header('Status: 414 Request-URI Too Large', true, $this->error);
					break;
				case View::ERROR_UNSUPPORTEDMEDIATYPE:
					header('Status: 415 Unsupported Media Type', true, $this->error);
					break;
				case View::ERROR_RANGENOTSATISFIABLE:
					header('Status: 416 Requested range not satisfiable', true, $this->error);
					break;
				case View::ERROR_EXPECTATIONFAILED:
					header('Status: 417 Expectation Failed', true, $this->error);
					break;
				case View::ERROR_SERVERERROR:
					header('Status: 500 Internal Server Error', true, $this->error);
					break;
				default:
					header('Status: 500 Internal Server Error', true, $this->error);
					break; // I don't know WTF happened...
			}

			if ($this->contenttype) {
				if ($this->contenttype == View::CTYPE_HTML) header('Content-Type: text/html; charset=UTF-8');
				else header('Content-Type: ' . $this->contenttype);
			}
			//mb_internal_encoding('utf-8');

			header('X-Content-Encoded-By: Core Plus' . (DEVELOPMENT_MODE ? ' ' . Core::GetComponent()->getVersion() : ''));
			if(\ConfigHandler::Get('/core/security/x-frame-options')){
				header('X-Frame-Options: ' . \ConfigHandler::Get('/core/security/x-frame-options'));
			}

			if(\ConfigHandler::Get('/core/security/csp-frame-ancestors')){
				header('Content-Security-Policy: frame-ancestors \'self\' ' . \ConfigHandler::Get('/core/security/content-security-policy'));
			}

			if($this->updated !== null){
				header('Last-Modified: ' . Time::FormatGMT($this->updated, Time::TIMEZONE_USER, Time::FORMAT_RFC2822));
				//header('Last-Modified: ' . Time::FormatGMT($this->updated, Time::TIMEZONE_USER, Time::FORMAT_ISO8601));
			}

			// Are there any custom headers to send also?
			foreach($this->headers as $k => $v){
				header($k . ': ' . $v);
			}
		}

		// No SSL, skip all this!
		if(SSL_MODE != SSL_MODE_DISABLED){
			// If SSL is required by the controller and it's available, redirect there!
			if($this->ssl && !SSL){
				header('Location: ' . ROOT_URL_SSL . substr(REL_REQUEST_PATH, 1));
				die('This page requires SSL, if it does not redirect you automatically, please <a href="' . ROOT_URL_SSL . substr(REL_REQUEST_PATH, 1) . '">Click Here</a>.');
			}
			// If SSL is set to be ondemand and the page does not have it set but it's enabled, redirect to the non-SSL version.
			elseif(!$this->ssl && SSL && SSL_MODE == SSL_MODE_ONDEMAND){
				header('Location: ' . ROOT_URL_NOSSL . substr(REL_REQUEST_PATH, 1));
				die('This page does not require SSL, if it does not redirect you automatically, please <a href="' . ROOT_URL_NOSSL . substr(REL_REQUEST_PATH, 1) . '">Click Here</a>.');
			}
			// Else, SSL_MODE_ALLOWED doesn't care if SSL is enabled or not!
		}

		//echo mb_convert_encoding($data, 'UTF-8', 'auto');
		//echo mb_convert_encoding($data, 'HTML-ENTITIES', 'auto');
		echo $data;
	}