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