| Total Complexity | 43 |
| Total Lines | 248 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like InstallWizard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use InstallWizard, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 28 | class InstallWizard |
||
| 29 | { |
||
| 30 | public $language = 'english'; |
||
| 31 | public $pages = []; |
||
| 32 | public $currentPage = 'langselect'; |
||
| 33 | public $pageIndex = 0; |
||
| 34 | public $configs = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return bool |
||
| 38 | */ |
||
| 39 | public function xoInit() |
||
| 40 | { |
||
| 41 | if (@empty($_SERVER['REQUEST_URI'])) { |
||
| 42 | $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; |
||
| 43 | } |
||
| 44 | |||
| 45 | // Load the main language file |
||
| 46 | $this->initLanguage(!empty($_COOKIE['xo_install_lang']) ? $_COOKIE['xo_install_lang'] : 'english'); |
||
| 47 | // Setup pages |
||
| 48 | require_once \dirname(__DIR__) . '/include/page.php'; |
||
| 49 | $this->pages = $pages; |
||
|
|
|||
| 50 | |||
| 51 | // Load default configs |
||
| 52 | |||
| 53 | $this->configs = []; |
||
| 54 | /* |
||
| 55 | // Database type |
||
| 56 | $this->db_types = $db_types; |
||
| 57 | |||
| 58 | // setup config site info |
||
| 59 | $this->conf_names = $conf_names; |
||
| 60 | |||
| 61 | // languages config files |
||
| 62 | $this->language_files = $language_files; |
||
| 63 | |||
| 64 | // extension_loaded |
||
| 65 | $this->extensions = $extensions; |
||
| 66 | |||
| 67 | // Modules to be installed by default |
||
| 68 | $this->modules = $modules; |
||
| 69 | |||
| 70 | // xoops_lib, xoops_data directories |
||
| 71 | $this->xoopsPathDefault = $xoopsPathDefault; |
||
| 72 | |||
| 73 | // writable xoops_lib, xoops_data directories |
||
| 74 | $this->dataPath = $dataPath; |
||
| 75 | |||
| 76 | // Protector default trust_path |
||
| 77 | $this->trust_path = isset($trust_path) ? $trust_path : false; |
||
| 78 | |||
| 79 | // Writable files and directories |
||
| 80 | $this->writable = $writable; |
||
| 81 | */ |
||
| 82 | |||
| 83 | if (!$this->checkAccess()) { |
||
| 84 | return false; |
||
| 85 | } |
||
| 86 | |||
| 87 | $pagename = \preg_replace('~(page_)(.*)~', '$2', \basename((string) $_SERVER['SCRIPT_NAME'], '.php')); |
||
| 88 | $this->setPage($pagename); |
||
| 89 | |||
| 90 | // Prevent client caching |
||
| 91 | \header('Cache-Control: no-store, no-cache, must-revalidate', false); |
||
| 92 | \header('Pragma: no-cache'); |
||
| 93 | |||
| 94 | return true; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | public function checkAccess() |
||
| 101 | { |
||
| 102 | if (INSTALL_USER != '' && INSTALL_PASSWORD != '') { |
||
| 103 | if (!isset($_SERVER['PHP_AUTH_USER'])) { |
||
| 104 | \header('WWW-Authenticate: Basic realm="XOOPS Installer"'); |
||
| 105 | \header('HTTP/1.0 401 Unauthorized'); |
||
| 106 | echo 'You can not access this XOOPS installer.'; |
||
| 107 | |||
| 108 | return false; |
||
| 109 | } |
||
| 110 | if (INSTALL_USER != '' && INSTALL_USER != $_SERVER['PHP_AUTH_USER']) { |
||
| 111 | \header('HTTP/1.0 401 Unauthorized'); |
||
| 112 | echo 'You can not access this XOOPS installer.'; |
||
| 113 | |||
| 114 | return false; |
||
| 115 | } |
||
| 116 | if (INSTALL_PASSWORD != $_SERVER['PHP_AUTH_PW']) { |
||
| 117 | \header('HTTP/1.0 401 Unauthorized'); |
||
| 118 | echo 'You can not access this XOOPS installer.'; |
||
| 119 | |||
| 120 | return false; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | if (empty($GLOBALS['xoopsOption']['checkadmin'])) { |
||
| 125 | return true; |
||
| 126 | } |
||
| 127 | |||
| 128 | if (empty($GLOBALS['xoopsUser']) && !empty($_COOKIE['xo_install_user'])) { |
||
| 129 | install_acceptUser($_COOKIE['xo_install_user']); |
||
| 130 | } |
||
| 131 | if (empty($GLOBALS['xoopsUser'])) { |
||
| 132 | \redirect_header('../user.php'); |
||
| 133 | } |
||
| 134 | if (!$GLOBALS['xoopsUser']->isAdmin()) { |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | return true; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $file |
||
| 143 | */ |
||
| 144 | public function loadLangFile($file): void |
||
| 145 | { |
||
| 146 | \xoops_loadLanguage($file, 'moduleinstaller'); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param $language |
||
| 151 | */ |
||
| 152 | public function initLanguage($language): void |
||
| 153 | { |
||
| 154 | $language = \preg_replace('/[^a-z0-9_\-]/i', '', (string) $language); |
||
| 155 | if (!\file_exists("./language/{$language}/install.php")) { |
||
| 156 | $language = 'english'; |
||
| 157 | } |
||
| 158 | $this->language = $language; |
||
| 159 | $this->loadLangFile('install'); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param $page |
||
| 164 | * |
||
| 165 | * @return false|int|string |
||
| 166 | */ |
||
| 167 | public function setPage($page) |
||
| 168 | { |
||
| 169 | $pages = \array_keys($this->pages); |
||
| 170 | if ((int)$page && $page >= 0 && $page < \count($pages)) { |
||
| 171 | $this->pageIndex = $page; |
||
| 172 | $this->currentPage = $pages[$page]; |
||
| 173 | } elseif (isset($this->pages[$page])) { |
||
| 174 | $this->currentPage = $page; |
||
| 175 | $this->pageIndex = \array_search($this->currentPage, $pages, true); |
||
| 176 | } else { |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($this->pageIndex > 0 && !isset($_COOKIE['xo_install_lang'])) { |
||
| 181 | \header('Location: index.php'); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this->pageIndex; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function baseLocation() |
||
| 191 | { |
||
| 192 | $proto = ('on' === @$_SERVER['HTTPS']) ? 'https' : 'http'; |
||
| 193 | $host = $_SERVER['HTTP_HOST']; |
||
| 194 | $base = mb_substr((string) $_SERVER['SCRIPT_NAME'], 0, mb_strrpos((string) $_SERVER['SCRIPT_NAME'], '/')); |
||
| 195 | |||
| 196 | return $proto . '://' . $host . $base; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param $page |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function pageURI($page) |
||
| 205 | { |
||
| 206 | $pages = \array_keys($this->pages); |
||
| 207 | $pageIndex = $this->pageIndex; |
||
| 208 | if (!(int)$page[0]) { |
||
| 209 | if ('+' == $page[0]) { |
||
| 210 | $pageIndex += mb_substr((string) $page, 1); |
||
| 211 | } elseif ('-' == $page[0]) { |
||
| 212 | $pageIndex -= mb_substr((string) $page, 1); |
||
| 213 | } else { |
||
| 214 | $pageIndex = (int)\array_search($page, $pages, true); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | if (!isset($pages[$pageIndex])) { |
||
| 218 | if (\defined('XOOPS_URL')) { |
||
| 219 | return XOOPS_URL; |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this->baseLocation(); |
||
| 223 | } |
||
| 224 | $page = $pages[$pageIndex]; |
||
| 225 | |||
| 226 | return $this->baseLocation() . "/page_{$page}.php"; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param $page |
||
| 231 | * @param int $status |
||
| 232 | * @param string $message |
||
| 233 | */ |
||
| 234 | public function redirectToPage($page, $status = 303, $message = 'See other'): void |
||
| 235 | { |
||
| 236 | $location = $this->pageURI($page); |
||
| 237 | $proto = !@empty($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; |
||
| 238 | \header("{$proto} {$status} {$message}"); |
||
| 239 | //header( "Status: $status $message" ); |
||
| 240 | \header("Location: {$location}"); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function createForm() |
||
| 276 | } |
||
| 277 | } |
||
| 278 |