Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var \HaaseIT\HCSF\Page |
||
| 30 | */ |
||
| 31 | protected $P; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ServiceManager |
||
| 35 | */ |
||
| 36 | protected $serviceManager; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $requireAdminAuth = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $requireAdminAuthAdminHome = false; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var bool |
||
| 50 | */ |
||
| 51 | protected $requireModuleCustomer = false; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $requireModuleShop = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \HaaseIT\HCSF\HelperConfig |
||
| 60 | */ |
||
| 61 | protected $config; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \HaaseIT\HCSF\Helper |
||
| 65 | */ |
||
| 66 | protected $helper; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Base constructor. |
||
| 70 | * @param ServiceManager $serviceManager |
||
| 71 | */ |
||
| 72 | public function __construct(ServiceManager $serviceManager) |
||
| 73 | { |
||
| 74 | $this->serviceManager = $serviceManager; |
||
| 75 | $this->config = $serviceManager->get('config'); |
||
| 76 | $this->helper = $serviceManager->get('helper'); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return \HaaseIT\HCSF\Page |
||
| 81 | * @throws \Exception |
||
| 82 | */ |
||
| 83 | public function getPage() |
||
| 84 | { |
||
| 85 | if ($this->requireAdminAuth) { |
||
| 86 | $this->requireAdminAuth(); |
||
| 87 | } |
||
| 88 | if ($this->requireModuleCustomer && !$this->config->getCore('enable_module_customer')) { |
||
| 89 | throw new \Exception(404); |
||
| 90 | } |
||
| 91 | if ($this->requireModuleShop && !$this->config->getCore('enable_module_shop')) { |
||
| 92 | throw new \Exception(404); |
||
| 93 | } |
||
| 94 | $this->preparePage(); |
||
| 95 | return $this->P; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function preparePage() |
||
| 99 | { |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | private function requireAdminAuth() { |
||
| 107 | $adminusers = $this->config->getSecret('admin_users'); |
||
| 108 | if ($this->requireAdminAuthAdminHome && (empty($adminusers) || !count($adminusers))) { |
||
| 109 | return true; |
||
| 110 | } elseif (count($adminusers) { |
||
|
|
|||
| 111 | $user = filter_input(INPUT_SERVER, 'PHP_AUTH_USER'); |
||
| 112 | $pass = filter_input(INPUT_SERVER, 'PHP_AUTH_PW'); |
||
| 113 | if (filter_input(INPUT_SERVER, 'REDIRECT_HTTP_AUTHORIZATION') !== null) { // fix for php cgi mode |
||
| 114 | list($user, $pass) = explode(':' , base64_decode(substr(filter_input(INPUT_SERVER, 'REDIRECT_HTTP_AUTHORIZATION'), 6))); |
||
| 115 | } |
||
| 116 | |||
| 117 | if (!empty($user) && !empty($pass)) { |
||
| 118 | $validated = !empty( |
||
| 119 | $adminusers[$user]) |
||
| 120 | && password_verify($pass, $adminusers[$user] |
||
| 121 | ); |
||
| 122 | } else { |
||
| 123 | $validated = false; |
||
| 124 | } |
||
| 125 | |||
| 126 | if (!$validated) { |
||
| 127 | header('WWW-Authenticate: Basic realm="'.$this->config->getSecret('admin_authrealm').'"'); |
||
| 128 | header('HTTP/1.0 401 Unauthorized'); |
||
| 129 | $this->helper->terminateScript('Not authorized'); |
||
| 130 | } |
||
| 131 | |||
| 132 | } else { |
||
| 133 | header('WWW-Authenticate: Basic realm="'.$this->config->getSecret('admin_authrealm').'"'); |
||
| 134 | header('HTTP/1.0 401 Unauthorized'); |
||
| 135 | $this->helper->terminateScript('Not authorized'); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 |