| @@ -1,5 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * BaseControllerTrait.php | |
| 4 | + * | |
| 5 | + * The BaseControllerTrait trait file. | |
| 6 | + * | |
| 7 | + * PHP versions 5 | |
| 8 | + * | |
| 9 | + * @author Alexander Schneider <alexanderschneider85@gmail.com> | |
| 10 | + * @copyright 2008-2017 Alexander Schneider | |
| 11 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 | |
| 12 | + * @version SVN: $id$ | |
| 13 | + * @link http://wordpress.org/extend/plugins/user-access-manager/ | |
| 14 | + */ | |
| 2 | 15 | |
| 3 | 16 | declare(strict_types=1); |
| 4 | 17 | |
| 5 | 18 | namespace UserAccessManager\Controller; |
| @@ -7,41 +20,73 @@ | ||
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Config\WordpressConfig; |
| 9 | 22 | use UserAccessManager\Wrapper\Php; |
| 10 | 23 | |
| 24 | +/** | |
| 25 | + * Trait BaseControllerTrait | |
| 26 | + * | |
| 27 | + * @package UserAccessManager\Controller | |
| 28 | + */ | |
| 11 | 29 | trait BaseControllerTrait |
| 12 | 30 | { |
| 13 | - protected ?string $template = null; | |
| 14 | - | |
| 31 | + /** | |
| 32 | + * @return Php | |
| 33 | + */ | |
| 15 | 34 | abstract protected function getPhp(): Php; |
| 16 | 35 | |
| 36 | + /** | |
| 37 | + * @return WordpressConfig | |
| 38 | + */ | |
| 17 | 39 | abstract protected function getWordpressConfig(): WordpressConfig; |
| 18 | 40 | |
| 41 | + /** | |
| 42 | + * @var string | |
| 43 | + */ | |
| 44 | + protected $template = null; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * Returns the current request url. | |
| 48 | + * @return string | |
| 49 | + */ | |
| 19 | 50 | public function getRequestUrl(): string |
| 20 | 51 | { |
| 21 | - return htmlentities($_SERVER['REQUEST_URI'] ?? '', ENT_QUOTES); | |
| 52 | + return htmlentities($_SERVER['REQUEST_URI']); | |
| 22 | 53 | } |
| 23 | 54 | |
| 24 | - private function sanitizeValue(mixed $value): mixed | |
| 55 | + /** | |
| 56 | + * Sanitize the given value. | |
| 57 | + * @param mixed $value | |
| 58 | + * @return array|string | |
| 59 | + */ | |
| 60 | + private function sanitizeValue($value) | |
| 25 | 61 | { |
| 26 | - if (is_array($value) === true) { | |
| 27 | - $sanitized = []; | |
| 62 | + if (is_object($value) === true) { | |
| 63 | + return $value; | |
| 64 | + } elseif (is_array($value) === true) { | |
| 65 | + $newValue = []; | |
| 28 | 66 | |
| 29 | 67 | foreach ($value as $key => $arrayValue) { |
| 30 | - $sanitized[$this->sanitizeValue($key)] = $this->sanitizeValue($arrayValue); | |
| 68 | + $sanitizedKey = $this->sanitizeValue($key); | |
| 69 | + $newValue[$sanitizedKey] = $this->sanitizeValue($arrayValue); | |
| 31 | 70 | } |
| 32 | 71 | |
| 33 | - return $sanitized; | |
| 72 | + $value = $newValue; | |
| 73 | + } elseif (is_string($value) === true) { | |
| 74 | + $value = preg_replace('/[\\\\]+(["|\'])/', '$1', $value); | |
| 75 | + $value = stripslashes($value); | |
| 76 | + $value = htmlspecialchars($value); | |
| 34 | 77 | } |
| 35 | 78 | |
| 36 | - if (is_string($value) === true) { | |
| 37 | - return htmlspecialchars(stripslashes($value), ENT_QUOTES); | |
| 38 | - } | |
| 39 | - | |
| 40 | 79 | return $value; |
| 41 | 80 | } |
| 42 | 81 | |
| 43 | - public function getRequestParameter(string $name, mixed $default = null): mixed | |
| 82 | + /** | |
| 83 | + * Returns the request parameter. | |
| 84 | + * @param string $name | |
| 85 | + * @param mixed $default | |
| 86 | + * @return mixed | |
| 87 | + */ | |
| 88 | + public function getRequestParameter(string $name, $default = null) | |
| 44 | 89 | { |
| 45 | 90 | $return = (isset($_POST[$name]) === true) ? $this->sanitizeValue($_POST[$name]) : null; |
| 46 | 91 | |
| 47 | 92 | if ($return === null) { |
| @@ -50,31 +95,40 @@ | ||
| 50 | 95 | |
| 51 | 96 | return $return; |
| 52 | 97 | } |
| 53 | 98 | |
| 99 | + /** | |
| 100 | + * Returns the content of the excluded php file. | |
| 101 | + * @param string $fileName The view file name | |
| 102 | + * @return string | |
| 103 | + */ | |
| 54 | 104 | protected function getIncludeContents(string $fileName): string |
| 55 | 105 | { |
| 106 | + $contents = ''; | |
| 56 | 107 | $realPath = rtrim($this->getWordpressConfig()->getRealPath(), DIRECTORY_SEPARATOR); |
| 57 | - $fileWithPath = implode(DIRECTORY_SEPARATOR, [$realPath, 'src', 'View', $fileName]); | |
| 108 | + $path = [$realPath, 'src', 'View']; | |
| 109 | + $path = implode(DIRECTORY_SEPARATOR, $path).DIRECTORY_SEPARATOR; | |
| 110 | + $fileWithPath = $path.$fileName; | |
| 58 | 111 | |
| 59 | - if (is_file($fileWithPath) === false) { | |
| 60 | - return ''; | |
| 112 | + if (is_file($fileWithPath) === true) { | |
| 113 | + try { | |
| 114 | + ob_start(); | |
| 115 | + $this->getPhp()->includeFile($this, $fileWithPath); | |
| 116 | + $contents = ob_get_contents(); | |
| 117 | + ob_end_clean(); | |
| 118 | + } catch (Exception $exception) { | |
| 119 | + $contents = "Error on including content '{$fileWithPath}': {$exception->getMessage()}"; | |
| 120 | + ob_end_clean(); | |
| 121 | + } | |
| 61 | 122 | } |
| 62 | 123 | |
| 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(); | |
| 72 | - | |
| 73 | 124 | return $contents; |
| 74 | 125 | } |
| 75 | 126 | |
| 76 | - public function render(): void | |
| 127 | + /** | |
| 128 | + * Renders the given template | |
| 129 | + */ | |
| 130 | + public function render() | |
| 77 | 131 | { |
| 78 | 132 | if ($this->template !== null) { |
| 79 | 133 | echo $this->getIncludeContents($this->template); |
| 80 | 134 | } |