1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
<?php
/* --------------------------------------------------------------
HttpViewController.inc.php 2016-10-13
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
MainFactory::load_class('HttpViewControllerInterface');
/**
* Class HttpViewController
*
* This class contains some helper methods for handling view requests. Be careful
* always when outputting raw user data to HTML or when handling POST requests because
* insufficient protection will lead to XSS and CSRF vulnerabilities.
*
* @link http://en.wikipedia.org/wiki/Cross-site_scripting
* @link http://en.wikipedia.org/wiki/Cross-site_request_forgery
*
* @category System
* @package Http
* @implements HttpViewControllerInterface
*/
class HttpViewController implements HttpViewControllerInterface
{
/**
* @var HttpContextReaderInterface
*/
protected $httpContextReader;
/**
* @var HttpResponseProcessorInterface
*/
protected $httpResponseProcessor;
/**
* @var ContentViewInterface
*/
protected $contentView;
/**
* @var array
*/
protected $queryParametersArray;
/**
* @var array
*/
protected $postDataArray;
/**
* @var AssetCollectionInterface Contain the assets needed to be included in the view HTML.
*/
protected $assets;
/**
* @param HttpContextReaderInterface $httpContextReader
* @param HttpResponseProcessorInterface $httpResponseProcessor
* @param ContentViewInterface $defaultContentView
*/
public function __construct(HttpContextReaderInterface $httpContextReader,
HttpResponseProcessorInterface $httpResponseProcessor,
ContentViewInterface $defaultContentView)
{
$this->httpContextReader = $httpContextReader;
$this->httpResponseProcessor = $httpResponseProcessor;
$this->contentView = $defaultContentView;
$this->assets = MainFactory::create('AssetCollection');
if(method_exists($this, 'init'))
{
$this->init(); // Initialization method for child controllers.
}
}
/**
* Processes a http response object which is get by invoking an action method.
* The action method is determined by the http context reader instance and the current request context.
* Re-implement this method in child classes to enable XSS and CSRF protection on demand.
*
* @param HttpContextInterface $httpContext Http context object which hold the request variables.
*
* @see HttpResponseProcessorInterface::proceed
* @see HttpContextReaderInterface::getActionName
*
* @throws LogicException When no action method is found by the http context reader.
*/
public function proceed(HttpContextInterface $httpContext)
{
$this->queryParametersArray = $this->httpContextReader->getQueryParameters($httpContext);
$this->postDataArray = $this->httpContextReader->getPostData($httpContext);
$actionName = $this->httpContextReader->getActionName($httpContext);
$response = $this->_callActionMethod($actionName);
$this->httpResponseProcessor->proceed($response);
}
/**
* Default action method.
* Every controller child class requires at least the default action method, which is invoked when
* the ::_getQueryParameterData('do') value is not separated by a trailing slash.
*
* Every action method have to return an instance which implements the http controller response interface.
*
* @return \HttpControllerResponseInterface
*/
public function actionDefault()
{
return MainFactory::create('HttpControllerResponse', '');
}
/**
* Invokes an action method by the given action name.
*
* @param string $actionName Name of action method to call, without 'action'-Suffix.
*
* @throws LogicException If no action method of the given name exists.
* @return HttpControllerResponseInterface Response message.
*/
protected function _callActionMethod($actionName)
{
if(empty($actionName))
{
$methodName = 'actionDefault';
}
else
{
$methodName = 'action' . $actionName;
}
if(method_exists($this, $methodName) === false)
{
throw new LogicException('Action method not found for: ' . htmlspecialchars($actionName));
}
$response = call_user_func(array($this, $methodName));
return $response;
}
/**
* Renders and returns a template file.
*
* @param string $templateFile Template file to render.
* @param array $contentArray Content array which represent the variables of the template.
*
* @return string Rendered template.
*/
protected function _render($templateFile, array $contentArray)
{
$this->contentView->set_content_template($templateFile);
foreach($contentArray as $contentItemKey => $contentItemValue)
{
$this->contentView->set_content_data($contentItemKey, $contentItemValue);
}
return $this->contentView->get_html();
}
/**
* Creates and returns a key value collection which represent the global $_GET array.
*
* @return KeyValueCollection
*/
protected function _getQueryParametersCollection()
{
return MainFactory::create('KeyValueCollection', $this->queryParametersArray);
}
/**
* Creates and returns a key value collection which represent the global $_POST array.
*
* @return KeyValueCollection
*/
protected function _getPostDataCollection()
{
return MainFactory::create('KeyValueCollection', $this->postDataArray);
}
/**
* Returns the expected $_GET value by the given key name.
* This method is the object oriented layer for $_GET[$keyName].
*
* @param string $keyName Expected key of query parameter.
*
* @return mixed|null Either the expected value or null, of not found.
*/
protected function _getQueryParameter($keyName)
{
if(!array_key_exists($keyName, $this->queryParametersArray))
{
return null;
}
return $this->queryParametersArray[$keyName];
}
/**
* Returns the expected $_POST value by the given key name.
* This method is the object oriented layer for $_POST[$keyName].
*
* @param string $keyName Expected key of post parameter.
*
* @return string|null Either the expected value or null, of not found.
*/
protected function _getPostData($keyName)
{
if(!array_key_exists($keyName, $this->postDataArray))
{
return null;
}
return $this->postDataArray[$keyName];
}
/**
* Check if the $_POST['pageToken'] or $_GET['pageToken'] variable is provided and if it's valid.
*
* Example:
* public function proceed(HttpContextInterface $httpContext)
* {
* parent::proceed($httpContext); // proceed http context from parent class
* if($_SERVER['REQUEST_METHOD'] === 'POST')
* {
* $this->_validatePageToken(); // CSRF Protection
* }
* }
*
* @param string $customExceptionMessage (optional) You can specify a custom exception message.
*
* @throws Exception If the validation fails.
*/
protected function _validatePageToken($customExceptionMessage = null)
{
$pageToken = $_REQUEST['pageToken'];
if($pageToken === null)
{
throw new Exception($customExceptionMessage ? : '$_POST["pageToken"] variable was not provided with the POST request.');
}
if(!$_SESSION['coo_page_token']->is_valid($pageToken))
{
throw new Exception($customExceptionMessage ? : 'Provided $_POST["pageToken"] variable is not valid.');
}
}
}