| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 0.00% | 0 / 68 |  | 0.00% | 0 / 25 | CRAP |  | 0.00% | 0 / 1 | 
| LegacyController |  | 0.00% | 0 / 68 |  | 0.00% | 0 / 25 | 2070 |  | 0.00% | 0 / 1 | 
| getRequest |  | 0.00% | 0 / 2 |  | 0.00% | 0 / 1 | 2 | |||
| getResponse |  | 0.00% | 0 / 2 |  | 0.00% | 0 / 1 | 2 | |||
| getRequestParameters |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 2 | |||
| hasRequestParameter |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 2 | |||
| getRequestParameter |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 2 | |||
| getRawParameter |  | 0.00% | 0 / 4 |  | 0.00% | 0 / 1 | 6 | |||
| getHeaders |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| hasHeader |  | 0.00% | 0 / 4 |  | 0.00% | 0 / 1 | 6 | |||
| getHeader |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| hasCookie |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| getCookie |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| getRequestMethod |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| isRequestGet |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| isRequestPost |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| isRequestPut |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| isRequestDelete |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| isRequestHead |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| getUserAgent |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| getQueryString |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| getRequestURI |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| setCookie |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| setContentHeader |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| getContentType |  | 0.00% | 0 / 3 |  | 0.00% | 0 / 1 | 6 | |||
| isXmlHttpRequest |  | 0.00% | 0 / 4 |  | 0.00% | 0 / 1 | 6 | |||
| logDeprecated |  | 0.00% | 0 / 1 |  | 0.00% | 0 / 1 | 6 | |||
| 1 | <?php | 
| 2 | |
| 3 | /** | 
| 4 | * This program is free software; you can redistribute it and/or | 
| 5 | * modify it under the terms of the GNU General Public License | 
| 6 | * as published by the Free Software Foundation; under version 2 | 
| 7 | * of the License (non-upgradable). | 
| 8 | * | 
| 9 | * This program is distributed in the hope that it will be useful, | 
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 12 | * GNU General Public License for more details. | 
| 13 | * | 
| 14 | * You should have received a copy of the GNU General Public License | 
| 15 | * along with this program; if not, write to the Free Software | 
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
| 17 | * | 
| 18 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA | 
| 19 | * | 
| 20 | */ | 
| 21 | |
| 22 | namespace oat\tao\model\http; | 
| 23 | |
| 24 | use Context; | 
| 25 | |
| 26 | use function GuzzleHttp\Psr7\stream_for; | 
| 27 | |
| 28 | /** | 
| 29 | * Class LegacyController | 
| 30 | * @package oat\tao\model\http | 
| 31 | * | 
| 32 | * @deprecated please use \oat\tao\model\http\Controller instead | 
| 33 | * | 
| 34 | * In order to remove legacy DI & http request handling, we need to remove this controller | 
| 35 | * Any entry point (e.q. controller) should not extend this class anymore | 
| 36 | * To correctly handle PSR7 request and response please inherit directly \oat\tao\model\http\Controller | 
| 37 | * This is a temporary solution to wait all children do not call legacy "Module" method anymore | 
| 38 | * | 
| 39 | */ | 
| 40 | abstract class LegacyController extends Controller | 
| 41 | { | 
| 42 | protected $response; | 
| 43 | protected $request; | 
| 44 | |
| 45 | /** | 
| 46 | * @deprecated Use getPsrRequest() instead | 
| 47 | * | 
| 48 | * @return \Request | 
| 49 | */ | 
| 50 | public function getRequest() | 
| 51 | { | 
| 52 | $this->logDeprecated(); | 
| 53 | return Context::getInstance()->getRequest(); | 
| 54 | } | 
| 55 | |
| 56 | /** | 
| 57 | * @deprecated Use getPsrResponse() instead | 
| 58 | * | 
| 59 | * @return \Response | 
| 60 | */ | 
| 61 | public function getResponse() | 
| 62 | { | 
| 63 | $this->logDeprecated(); | 
| 64 | return Context::getInstance()->getResponse(); | 
| 65 | } | 
| 66 | |
| 67 | /** | 
| 68 | * Get all parameters that includes: | 
| 69 | * - POST parameters | 
| 70 | * - GET parameters | 
| 71 | * - request attributes | 
| 72 | * - http headers | 
| 73 | * For security reasons, this method will be removed. | 
| 74 | * | 
| 75 | * @return array | 
| 76 | * | 
| 77 | * @deprecated Please use relative method to fetch http parameters. | 
| 78 | */ | 
| 79 | public function getRequestParameters() | 
| 80 | { | 
| 81 | return $this->getRequest()->getParameters(); | 
| 82 | } | 
| 83 | |
| 84 | /** | 
| 85 | * Check if $name exists in request parameters. | 
| 86 | * For security reasons, this method will be removed. | 
| 87 | * | 
| 88 | * @return bool | 
| 89 | * | 
| 90 | * @deprecated Please use relative method to check http parameters. | 
| 91 | */ | 
| 92 | public function hasRequestParameter($name) | 
| 93 | { | 
| 94 | return $this->getRequest()->hasParameter($name); | 
| 95 | } | 
| 96 | |
| 97 | /** | 
| 98 | * Get parameter from request parameters. | 
| 99 | * For security reasons, this method will be removed. | 
| 100 | * | 
| 101 | * @return mixed | 
| 102 | * | 
| 103 | * @deprecated Please use relative method to fetch http parameters. | 
| 104 | */ | 
| 105 | public function getRequestParameter($name) | 
| 106 | { | 
| 107 | return $this->getRequest()->getParameter($name); | 
| 108 | } | 
| 109 | |
| 110 | /** | 
| 111 | * Returns a request parameter unencoded | 
| 112 | * | 
| 113 | * @param string $paramName | 
| 114 | * @throws \common_exception_MissingParameter | 
| 115 | * @return string|array | 
| 116 | */ | 
| 117 | protected function getRawParameter($paramName) | 
| 118 | { | 
| 119 | $raw = $this->getRequest()->getRawParameters(); | 
| 120 | if (!isset($raw[$paramName])) { | 
| 121 | throw new \common_exception_MissingParameter($paramName); | 
| 122 | } | 
| 123 | return $raw[$paramName]; | 
| 124 | } | 
| 125 | |
| 126 | /** | 
| 127 | * @see parent::getHeaders() | 
| 128 | * | 
| 129 | * @return array | 
| 130 | */ | 
| 131 | public function getHeaders() | 
| 132 | { | 
| 133 | if (!$this->request) { | 
| 134 | return $this->getRequest()->getHeaders(); | 
| 135 | } | 
| 136 | return parent::getHeaders(); | 
| 137 | } | 
| 138 | |
| 139 | /** | 
| 140 | * @see parent::hasHeader() | 
| 141 | * | 
| 142 | * @return bool | 
| 143 | */ | 
| 144 | public function hasHeader($name) | 
| 145 | { | 
| 146 | $name = strtolower($name); | 
| 147 | if (!$this->request) { | 
| 148 | return $this->getRequest()->hasHeader($name); | 
| 149 | } | 
| 150 | return parent::hasHeader($name); | 
| 151 | } | 
| 152 | |
| 153 | /** | 
| 154 | * @see parent::getHeader() | 
| 155 | * | 
| 156 | * @param $default | 
| 157 | * @return mixed | 
| 158 | */ | 
| 159 | public function getHeader($name, $default = null) | 
| 160 | { | 
| 161 | if (!$this->request) { | 
| 162 | return $this->getRequest()->getHeader($name); | 
| 163 | } | 
| 164 | return parent::getHeader($name, $default); | 
| 165 | } | 
| 166 | |
| 167 | /** | 
| 168 | * @see parent::hasCookie() | 
| 169 | * | 
| 170 | * @return bool | 
| 171 | */ | 
| 172 | public function hasCookie($name) | 
| 173 | { | 
| 174 | if (!$this->request) { | 
| 175 | return $this->getRequest()->hasCookie($name); | 
| 176 | } | 
| 177 | return parent::hasCookie($name); | 
| 178 | } | 
| 179 | |
| 180 | /** | 
| 181 | * @see parent::getCookie() | 
| 182 | * | 
| 183 | * @param $name | 
| 184 | * @param $default | 
| 185 | * @return bool|mixed | 
| 186 | */ | 
| 187 | public function getCookie($name, $default = null) | 
| 188 | { | 
| 189 | if (!$this->request) { | 
| 190 | return $this->getRequest()->getCookie($name); | 
| 191 | } | 
| 192 | return parent::getCookie($name, $default); | 
| 193 | } | 
| 194 | |
| 195 | /** | 
| 196 | * @see parent::getRequestMethod() | 
| 197 | * | 
| 198 | * @return string | 
| 199 | */ | 
| 200 | public function getRequestMethod() | 
| 201 | { | 
| 202 | if (!$this->request) { | 
| 203 | return $this->getRequest()->getMethod(); | 
| 204 | } | 
| 205 | return parent::getRequestMethod(); | 
| 206 | } | 
| 207 | |
| 208 | /** | 
| 209 | * @see parent::isRequestGet() | 
| 210 | * | 
| 211 | * @return bool | 
| 212 | */ | 
| 213 | public function isRequestGet() | 
| 214 | { | 
| 215 | if (!$this->request) { | 
| 216 | return $this->getRequest()->isGet(); | 
| 217 | } | 
| 218 | return parent::isRequestGet(); | 
| 219 | } | 
| 220 | |
| 221 | /** | 
| 222 | * @see parent::isRequestPost() | 
| 223 | * | 
| 224 | * @return bool | 
| 225 | */ | 
| 226 | public function isRequestPost() | 
| 227 | { | 
| 228 | if (!$this->request) { | 
| 229 | return $this->getRequest()->isPost(); | 
| 230 | } | 
| 231 | return parent::isRequestPost(); | 
| 232 | } | 
| 233 | |
| 234 | /** | 
| 235 | * @see parent::isRequestPut() | 
| 236 | * | 
| 237 | * @return bool | 
| 238 | */ | 
| 239 | public function isRequestPut() | 
| 240 | { | 
| 241 | if (!$this->request) { | 
| 242 | return $this->getRequest()->isPut(); | 
| 243 | } | 
| 244 | return parent::isRequestPut(); | 
| 245 | } | 
| 246 | |
| 247 | /** | 
| 248 | * @see parent::isRequestDelete() | 
| 249 | * | 
| 250 | * @return bool | 
| 251 | */ | 
| 252 | public function isRequestDelete() | 
| 253 | { | 
| 254 | if (!$this->request) { | 
| 255 | return $this->getRequest()->isDelete(); | 
| 256 | } | 
| 257 | return parent::isRequestDelete(); | 
| 258 | } | 
| 259 | |
| 260 | /** | 
| 261 | * @see parent::isRequestHead() | 
| 262 | * | 
| 263 | * @return bool | 
| 264 | */ | 
| 265 | public function isRequestHead() | 
| 266 | { | 
| 267 | if (!$this->request) { | 
| 268 | return $this->getRequest()->isHead(); | 
| 269 | } | 
| 270 | return parent::isRequestHead(); | 
| 271 | } | 
| 272 | |
| 273 | /** | 
| 274 | * @see parent::getUserAgent() | 
| 275 | * | 
| 276 | * @return string[] | 
| 277 | */ | 
| 278 | public function getUserAgent() | 
| 279 | { | 
| 280 | if (!$this->request) { | 
| 281 | return $this->getRequest()->getUserAgent(); | 
| 282 | } | 
| 283 | return parent::getUserAgent(); | 
| 284 | } | 
| 285 | |
| 286 | /** | 
| 287 | * @see parent::getQueryString() | 
| 288 | * | 
| 289 | * @return string | 
| 290 | */ | 
| 291 | public function getQueryString() | 
| 292 | { | 
| 293 | if (!$this->request) { | 
| 294 | return $this->getRequest()->getQueryString(); | 
| 295 | } | 
| 296 | return parent::getQueryString(); | 
| 297 | } | 
| 298 | |
| 299 | /** | 
| 300 | * @see parent::getRequestUri() | 
| 301 | * | 
| 302 | * @return string | 
| 303 | */ | 
| 304 | public function getRequestURI() | 
| 305 | { | 
| 306 | if (!$this->request) { | 
| 307 | return $this->getRequest()->getRequestURI(); | 
| 308 | } | 
| 309 | return parent::getRequestURI(); | 
| 310 | } | 
| 311 | |
| 312 | /** | 
| 313 | * @see parent::setCookie() | 
| 314 | * | 
| 315 | * @param $name | 
| 316 | * @param null $value | 
| 317 | * @param null $expire | 
| 318 | * @param null $domainPath | 
| 319 | * @param null $https | 
| 320 | * @param null $httpOnly | 
| 321 | * @return bool|void | 
| 322 | */ | 
| 323 | public function setCookie( | 
| 324 | $name, | 
| 325 | $value = null, | 
| 326 | $expire = null, | 
| 327 | $domainPath = null, | 
| 328 | $https = null, | 
| 329 | $httpOnly = null | 
| 330 | ) { | 
| 331 | if (!$this->response) { | 
| 332 | return $this->getResponse()->setCookie($name, $value, $expire, $domainPath, $https, $httpOnly); | 
| 333 | } | 
| 334 | return parent::setCookie($name, $value, $expire, $domainPath, $https, $httpOnly); | 
| 335 | } | 
| 336 | |
| 337 | /** | 
| 338 | * @see parent::setContentHeader() | 
| 339 | * | 
| 340 | * @param $contentType | 
| 341 | * @param string $charset | 
| 342 | * @return Controller | 
| 343 | */ | 
| 344 | public function setContentHeader($contentType, $charset = 'UTF-8') | 
| 345 | { | 
| 346 | if (!$this->response) { | 
| 347 | return $this->getResponse()->setContentHeader($contentType, $charset); | 
| 348 | } | 
| 349 | return parent::setContentHeader($contentType, $charset); | 
| 350 | } | 
| 351 | |
| 352 | /** | 
| 353 | * @see parent getContentType() | 
| 354 | * | 
| 355 | * @return string|string[] | 
| 356 | */ | 
| 357 | public function getContentType() | 
| 358 | { | 
| 359 | if (!$this->response) { | 
| 360 | return $this->getResponse()->getContentType(); | 
| 361 | } | 
| 362 | return parent::getContentType(); | 
| 363 | } | 
| 364 | |
| 365 | /** | 
| 366 | * Check if the current request is using AJAX | 
| 367 | * | 
| 368 | * @return bool | 
| 369 | */ | 
| 370 | protected function isXmlHttpRequest() | 
| 371 | { | 
| 372 | if (!$this->request) { | 
| 373 | $this->logDeprecated(); | 
| 374 | return \tao_helpers_Request::isAjax(); | 
| 375 | } | 
| 376 | return parent::isXmlHttpRequest(); | 
| 377 | } | 
| 378 | |
| 379 | /** | 
| 380 | * Mark a method as deprecated | 
| 381 | * @param null $function | 
| 382 | */ | 
| 383 | protected function logDeprecated($function = null) | 
| 384 | { | 
| 385 | return; | 
| 386 | $message = '[DEPRECATED] Deprecated call '; | 
| 387 | if (!is_null($function)) { | 
| 388 | $message .= 'of "' . $function . '"'; | 
| 389 | } | 
| 390 | $message .= ' (' . get_called_class() . ')'; | 
| 391 | \common_Logger::i($message); | 
| 392 | } | 
| 393 | } |