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