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