| @@ -1,26 +1,63 @@ | ||
| 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 | - abstract protected function getPhp(): Php; | |
| 14 | - abstract protected function getWordpressConfig(): WordpressConfig; | |
| 15 | - protected ?string $template = null; | |
| 27 | + /** | |
| 28 | + * @return Php | |
| 29 | + */ | |
| 30 | + abstract protected function getPhp(); | |
| 16 | 31 | |
| 17 | - public function getRequestUrl(): string | |
| 32 | + /** | |
| 33 | + * @return WordpressConfig | |
| 34 | + */ | |
| 35 | + abstract protected function getWordpressConfig(); | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @var string | |
| 39 | + */ | |
| 40 | + protected $template = null; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Returns the current request url. | |
| 44 | + * | |
| 45 | + * @return string | |
| 46 | + */ | |
| 47 | + public function getRequestUrl() | |
| 18 | 48 | { |
| 19 | - return htmlentities($_SERVER['REQUEST_URI'], ENT_NOQUOTES); | |
| 49 | + return htmlentities($_SERVER['REQUEST_URI']); | |
| 20 | 50 | } |
| 21 | 51 | |
| 22 | - 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) | |
| 23 | 60 | { |
| 24 | 61 | if (is_object($value) === true) { |
| 25 | 62 | return $value; |
| 26 | 63 | } elseif (is_array($value) === true) { |
| @@ -32,17 +69,25 @@ | ||
| 32 | 69 | } |
| 33 | 70 | |
| 34 | 71 | $value = $newValue; |
| 35 | 72 | } elseif (is_string($value) === true) { |
| 36 | - $value = preg_replace('/\\+(["|\'])/', '$1', $value); | |
| 73 | + $value = preg_replace('/[\\\\]+(["|\']{1})/', '$1', $value); | |
| 37 | 74 | $value = stripslashes($value); |
| 38 | - $value = htmlspecialchars($value, ENT_NOQUOTES); | |
| 75 | + $value = htmlspecialchars($value); | |
| 39 | 76 | } |
| 40 | 77 | |
| 41 | 78 | return $value; |
| 42 | 79 | } |
| 43 | 80 | |
| 44 | - 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) | |
| 45 | 90 | { |
| 46 | 91 | $return = (isset($_POST[$name]) === true) ? $this->sanitizeValue($_POST[$name]) : null; |
| 47 | 92 | |
| 48 | 93 | if ($return === null) { |
| @@ -51,9 +96,16 @@ | ||
| 51 | 96 | |
| 52 | 97 | return $return; |
| 53 | 98 | } |
| 54 | 99 | |
| 55 | - 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) | |
| 56 | 108 | { |
| 57 | 109 | $contents = ''; |
| 58 | 110 | $realPath = rtrim($this->getWordpressConfig()->getRealPath(), DIRECTORY_SEPARATOR); |
| 59 | 111 | $path = [$realPath, 'src', 'View']; |
| @@ -65,10 +117,10 @@ | ||
| 65 | 117 | ob_start(); |
| 66 | 118 | $this->getPhp()->includeFile($this, $fileWithPath); |
| 67 | 119 | $contents = ob_get_contents(); |
| 68 | 120 | ob_end_clean(); |
| 69 | - } catch (Exception $exception) { | |
| 70 | - $contents = "Error on including content '$fileWithPath': {$exception->getMessage()}"; | |
| 121 | + } catch (\Exception $exception) { | |
| 122 | + $contents = "Error on including content '{$fileWithPath}': {$exception->getMessage()}"; | |
| 71 | 123 | ob_end_clean(); |
| 72 | 124 | } |
| 73 | 125 | } |
| 74 | 126 | |
| @@ -74,9 +126,12 @@ | ||
| 74 | 126 | |
| 75 | 127 | return $contents; |
| 76 | 128 | } |
| 77 | 129 | |
| 78 | - public function render(): void | |
| 130 | + /** | |
| 131 | + * Renders the given template | |
| 132 | + */ | |
| 133 | + public function render() | |
| 79 | 134 | { |
| 80 | 135 | if ($this->template !== null) { |
| 81 | 136 | echo $this->getIncludeContents($this->template); |
| 82 | 137 | } |