| @@ -9,36 +9,35 @@ | ||
| 9 | 9 | use UserAccessManager\Wrapper\Php; |
| 10 | 10 | |
| 11 | 11 | trait BaseControllerTrait |
| 12 | 12 | { |
| 13 | + protected ?string $template = null; | |
| 14 | + | |
| 13 | 15 | abstract protected function getPhp(): Php; |
| 16 | + | |
| 14 | 17 | abstract protected function getWordpressConfig(): WordpressConfig; |
| 15 | - protected ?string $template = null; | |
| 16 | 18 | |
| 17 | 19 | public function getRequestUrl(): string |
| 18 | 20 | { |
| 19 | - return htmlentities($_SERVER['REQUEST_URI'], ENT_NOQUOTES); | |
| 21 | + return htmlentities($_SERVER['REQUEST_URI'] ?? '', ENT_QUOTES); | |
| 20 | 22 | } |
| 21 | 23 | |
| 22 | 24 | private function sanitizeValue(mixed $value): mixed |
| 23 | 25 | { |
| 24 | - if (is_object($value) === true) { | |
| 25 | - return $value; | |
| 26 | - } elseif (is_array($value) === true) { | |
| 27 | - $newValue = []; | |
| 26 | + if (is_array($value) === true) { | |
| 27 | + $sanitized = []; | |
| 28 | 28 | |
| 29 | 29 | foreach ($value as $key => $arrayValue) { |
| 30 | - $sanitizedKey = $this->sanitizeValue($key); | |
| 31 | - $newValue[$sanitizedKey] = $this->sanitizeValue($arrayValue); | |
| 30 | + $sanitized[$this->sanitizeValue($key)] = $this->sanitizeValue($arrayValue); | |
| 32 | 31 | } |
| 33 | 32 | |
| 34 | - $value = $newValue; | |
| 35 | - } elseif (is_string($value) === true) { | |
| 36 | - $value = preg_replace('/\\+(["|\'])/', '$1', $value); | |
| 37 | - $value = stripslashes($value); | |
| 38 | - $value = htmlspecialchars($value, ENT_NOQUOTES); | |
| 33 | + return $sanitized; | |
| 39 | 34 | } |
| 40 | 35 | |
| 36 | + if (is_string($value) === true) { | |
| 37 | + return htmlspecialchars(stripslashes($value), ENT_QUOTES); | |
| 38 | + } | |
| 39 | + | |
| 41 | 40 | return $value; |
| 42 | 41 | } |
| 43 | 42 | |
| 44 | 43 | public function getRequestParameter(string $name, mixed $default = null): mixed |
| @@ -53,25 +52,24 @@ | ||
| 53 | 52 | } |
| 54 | 53 | |
| 55 | 54 | protected function getIncludeContents(string $fileName): string |
| 56 | 55 | { |
| 57 | - $contents = ''; | |
| 58 | 56 | $realPath = rtrim($this->getWordpressConfig()->getRealPath(), DIRECTORY_SEPARATOR); |
| 59 | - $path = [$realPath, 'src', 'View']; | |
| 60 | - $path = implode(DIRECTORY_SEPARATOR, $path).DIRECTORY_SEPARATOR; | |
| 61 | - $fileWithPath = $path.$fileName; | |
| 57 | + $fileWithPath = implode(DIRECTORY_SEPARATOR, [$realPath, 'src', 'View', $fileName]); | |
| 62 | 58 | |
| 63 | - if (is_file($fileWithPath) === true) { | |
| 64 | - try { | |
| 65 | - ob_start(); | |
| 66 | - $this->getPhp()->includeFile($this, $fileWithPath); | |
| 67 | - $contents = ob_get_contents(); | |
| 68 | - ob_end_clean(); | |
| 69 | - } catch (Exception $exception) { | |
| 70 | - $contents = "Error on including content '$fileWithPath': {$exception->getMessage()}"; | |
| 71 | - ob_end_clean(); | |
| 72 | - } | |
| 59 | + if (is_file($fileWithPath) === false) { | |
| 60 | + return ''; | |
| 73 | 61 | } |
| 62 | + | |
| 63 | + try { | |
| 64 | + ob_start(); | |
| 65 | + $this->getPhp()->includeFile($this, $fileWithPath); | |
| 66 | + $contents = ob_get_contents(); | |
| 67 | + } catch (Exception $exception) { | |
| 68 | + $contents = "Error on including content '$fileWithPath': {$exception->getMessage()}"; | |
| 69 | + } | |
| 70 | + | |
| 71 | + ob_end_clean(); | |
| 74 | 72 | |
| 75 | 73 | return $contents; |
| 76 | 74 | } |
| 77 | 75 | |