| Total Complexity | 60 |
| Total Lines | 650 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ManageServer_Controller 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 ManageServer_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class ManageServer_Controller extends Action_Controller |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * This is the main dispatcher. Sets up all the available sub-actions, all the tabs and selects |
||
| 31 | * the appropriate one based on the sub-action. |
||
| 32 | * |
||
| 33 | * What it does: |
||
| 34 | * |
||
| 35 | * - Requires the admin_forum permission. |
||
| 36 | * - Redirects to the appropriate function based on the sub-action. |
||
| 37 | * |
||
| 38 | * @event integrate_sa_server_settings |
||
| 39 | * @uses edit_settings adminIndex. |
||
| 40 | * @see Action_Controller::action_index() |
||
| 41 | */ |
||
| 42 | public function action_index() |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * General forum settings - forum name, maintenance mode, etc. |
||
| 107 | * |
||
| 108 | * Practically, this shows an interface for the settings in Settings.php to |
||
| 109 | * be changed. The method handles the display, allows to edit, and saves |
||
| 110 | * the result for generalSettings form. |
||
| 111 | * |
||
| 112 | * What it does: |
||
| 113 | * |
||
| 114 | * - Requires the admin_forum permission. |
||
| 115 | * - Uses the edit_settings administration area. |
||
| 116 | * - Contains the actual array of settings to show from Settings.php. |
||
| 117 | * - Accessed from ?action=admin;area=serversettings;sa=general. |
||
| 118 | * |
||
| 119 | * @event integrate_save_general_settings |
||
| 120 | */ |
||
| 121 | public function action_generalSettings_display() |
||
| 122 | { |
||
| 123 | global $scripturl, $context, $txt; |
||
| 124 | |||
| 125 | // Initialize the form |
||
| 126 | $settingsForm = new Settings_Form(Settings_Form::FILE_ADAPTER); |
||
| 127 | |||
| 128 | // Initialize it with our settings |
||
| 129 | $settingsForm->setConfigVars($this->_generalSettings()); |
||
| 130 | |||
| 131 | // Setup the template stuff. |
||
| 132 | $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=general;save'; |
||
| 133 | $context['settings_title'] = $txt['general_settings']; |
||
| 134 | |||
| 135 | // Saving settings? |
||
| 136 | if (isset($this->_req->query->save)) |
||
| 137 | { |
||
| 138 | call_integration_hook('integrate_save_general_settings'); |
||
| 139 | |||
| 140 | // Reset this in the event the server has changed, it will get set again if needed. |
||
| 141 | updateSettings(array('host_to_dis' => 0)); |
||
| 142 | |||
| 143 | $settingsForm->setConfigValues((array) $this->_req->post); |
||
| 144 | $settingsForm->save(); |
||
| 145 | redirectexit('action=admin;area=serversettings;sa=general;' . $context['session_var'] . '=' . $context['session_id'] . ';msg=' . (!empty($context['settings_message']) ? $context['settings_message'] : 'core_settings_saved')); |
||
| 146 | } |
||
| 147 | |||
| 148 | // Fill the config array for the template and all that. |
||
| 149 | $settingsForm->prepare(); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Basic database and paths settings - database name, host, etc. |
||
| 154 | * |
||
| 155 | * This method handles the display, allows to edit, and saves the results |
||
| 156 | * for _databaseSettings. |
||
| 157 | * |
||
| 158 | * What it does: |
||
| 159 | * |
||
| 160 | * - It shows an interface for the settings in Settings.php to be changed. |
||
| 161 | * - It contains the actual array of settings to show from Settings.php. |
||
| 162 | * - Requires the admin_forum permission. |
||
| 163 | * - Uses the edit_settings administration area. |
||
| 164 | * - Accessed from ?action=admin;area=serversettings;sa=database. |
||
| 165 | * |
||
| 166 | * @event integrate_save_database_settings |
||
| 167 | */ |
||
| 168 | public function action_databaseSettings_display() |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Modify cookies settings. |
||
| 199 | * |
||
| 200 | * This method handles the display, allows to edit, and saves the result |
||
| 201 | * for the _cookieSettings form. |
||
| 202 | * |
||
| 203 | * @event integrate_save_cookie_settings |
||
| 204 | */ |
||
| 205 | public function action_cookieSettings_display() |
||
| 206 | { |
||
| 207 | global $context, $scripturl, $txt, $modSettings, $cookiename, $user_settings, $boardurl; |
||
| 208 | |||
| 209 | // Initialize the form |
||
| 210 | $settingsForm = new Settings_Form(Settings_Form::FILE_ADAPTER); |
||
| 211 | |||
| 212 | // Initialize it with our settings |
||
| 213 | $settingsForm->setConfigVars($this->_cookieSettings()); |
||
| 214 | |||
| 215 | $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cookie;save'; |
||
| 216 | $context['settings_title'] = $txt['cookies_sessions_settings']; |
||
| 217 | |||
| 218 | // Saving settings? |
||
| 219 | if (isset($this->_req->query->save)) |
||
| 220 | { |
||
| 221 | call_integration_hook('integrate_save_cookie_settings'); |
||
| 222 | |||
| 223 | // Its either local or global cookies |
||
| 224 | if (!empty($this->_req->post->localCookies) && !empty($this->_req->post->globalCookies)) |
||
| 225 | unset($this->_req->post->globalCookies); |
||
| 226 | |||
| 227 | if (!empty($this->_req->post->globalCookiesDomain) && strpos($boardurl, $this->_req->post->globalCookiesDomain) === false) |
||
| 228 | throw new Elk_Exception('invalid_cookie_domain', false); |
||
| 229 | |||
| 230 | if ($this->_req->getPost('cookiename', 'trim', '') === '') |
||
| 231 | $this->_req->post->cookiename = $cookiename; |
||
| 232 | |||
| 233 | $settingsForm->setConfigValues((array) $this->_req->post); |
||
| 234 | $settingsForm->save(); |
||
| 235 | |||
| 236 | // If the cookie name was changed, reset the cookie. |
||
| 237 | if ($cookiename != $this->_req->post->cookiename) |
||
| 238 | { |
||
| 239 | require_once(SUBSDIR . '/Auth.subs.php'); |
||
| 240 | |||
| 241 | $original_session_id = $context['session_id']; |
||
| 242 | |||
| 243 | // Remove the old cookie, nom nom nom |
||
| 244 | setLoginCookie(-3600, 0); |
||
| 245 | |||
| 246 | // Set the new one. |
||
| 247 | $cookiename = $this->_req->post->cookiename; |
||
| 248 | setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], hash('sha256', $user_settings['passwd'] . $user_settings['password_salt'])); |
||
| 249 | |||
| 250 | redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $original_session_id, detectServer()->is('needs_login_fix')); |
||
| 251 | } |
||
| 252 | |||
| 253 | redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $context['session_id'] . ';msg=' . (!empty($context['settings_message']) ? $context['settings_message'] : 'core_settings_saved')); |
||
| 254 | } |
||
| 255 | |||
| 256 | addInlineJavascript(' |
||
| 257 | // Initial state |
||
| 258 | hideGlobalCookies(); |
||
| 259 | |||
| 260 | // Update when clicked |
||
| 261 | $("#localCookies, #globalCookies").click(function () { |
||
| 262 | hideGlobalCookies(); |
||
| 263 | });', true); |
||
| 264 | |||
| 265 | // Fill the config array. |
||
| 266 | $settingsForm->prepare(); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Cache settings editing and submission. |
||
| 271 | * |
||
| 272 | * This method handles the display, allows to edit, and saves the result |
||
| 273 | * for _cacheSettings form. |
||
| 274 | * |
||
| 275 | * @event integrate_save_cache_settings |
||
| 276 | */ |
||
| 277 | public function action_cacheSettings_display() |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Allows to edit load management settings. |
||
| 327 | * |
||
| 328 | * This method handles the display, allows to edit, and saves the result |
||
| 329 | * for the _loadavgSettings form. |
||
| 330 | * |
||
| 331 | * @event integrate_loadavg_settings |
||
| 332 | * @event integrate_save_loadavg_settings |
||
| 333 | */ |
||
| 334 | public function action_loadavgSettings_display() |
||
| 335 | { |
||
| 336 | global $txt, $scripturl, $context; |
||
| 337 | |||
| 338 | // Initialize the form |
||
| 339 | $settingsForm = new Settings_Form(Settings_Form::DB_ADAPTER); |
||
| 340 | |||
| 341 | // Initialize it with our settings |
||
| 342 | $settingsForm->setConfigVars($this->_loadavgSettings()); |
||
| 343 | |||
| 344 | call_integration_hook('integrate_loadavg_settings'); |
||
| 345 | |||
| 346 | $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=loads;save'; |
||
| 347 | $context['settings_title'] = $txt['loadavg_settings']; |
||
| 348 | |||
| 349 | // Saving? |
||
| 350 | if (isset($this->_req->query->save)) |
||
| 351 | { |
||
| 352 | // Stupidity is not allowed. |
||
| 353 | foreach ($this->_req->post as $key => $value) |
||
| 354 | { |
||
| 355 | if (strpos($key, 'loadavg') === 0 || $key === 'loadavg_enable') |
||
| 356 | continue; |
||
| 357 | elseif ($key === 'loadavg_auto_opt' && $value <= 1) |
||
| 358 | $this->_req->post->loadavg_auto_opt = '1.0'; |
||
| 359 | elseif ($key === 'loadavg_forum' && $value < 10) |
||
| 360 | $this->_req->post->loadavg_forum = '10.0'; |
||
| 361 | elseif ($value < 2) |
||
| 362 | $this->_req->{$key} = '2.0'; |
||
| 363 | } |
||
| 364 | |||
| 365 | call_integration_hook('integrate_save_loadavg_settings'); |
||
| 366 | |||
| 367 | $settingsForm->setConfigValues((array) $this->_req->post); |
||
| 368 | $settingsForm->save(); |
||
| 369 | redirectexit('action=admin;area=serversettings;sa=loads;' . $context['session_var'] . '=' . $context['session_id']); |
||
| 370 | } |
||
| 371 | |||
| 372 | createToken('admin-ssc'); |
||
| 373 | createToken('admin-dbsc'); |
||
| 374 | $settingsForm->prepare(); |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Allows us to see the servers php settings |
||
| 379 | * |
||
| 380 | * What it does: |
||
| 381 | * |
||
| 382 | * - loads the settings into an array for display in a template |
||
| 383 | * - drops cookie values just in case |
||
| 384 | * |
||
| 385 | * @uses sub-template php_info |
||
| 386 | */ |
||
| 387 | public function action_phpinfo() |
||
| 388 | { |
||
| 389 | global $context, $txt; |
||
| 390 | |||
| 391 | $category = $txt['phpinfo_settings']; |
||
| 392 | $pinfo = array(); |
||
| 393 | |||
| 394 | // Get the data |
||
| 395 | ob_start(); |
||
| 396 | phpinfo(); |
||
| 397 | |||
| 398 | // We only want it for its body, pigs that we are |
||
| 399 | $info_lines = preg_replace('~^.*<body>(.*)</body>.*$~', '$1', ob_get_contents()); |
||
| 400 | $info_lines = explode("\n", strip_tags($info_lines, '<tr><td><h2>')); |
||
| 401 | @ob_end_clean(); |
||
|
|
|||
| 402 | |||
| 403 | // Remove things that could be considered sensitive |
||
| 404 | $remove = '_COOKIE|Cookie|_GET|_REQUEST|REQUEST_URI|QUERY_STRING|REQUEST_URL|HTTP_REFERER'; |
||
| 405 | |||
| 406 | // Put all of it into an array |
||
| 407 | foreach ($info_lines as $line) |
||
| 408 | { |
||
| 409 | if (preg_match('~(' . $remove . ')~', $line)) |
||
| 410 | continue; |
||
| 411 | |||
| 412 | // New category? |
||
| 413 | if (strpos($line, '<h2>') !== false) |
||
| 414 | $category = preg_match('~<h2>(.*)</h2>~', $line, $title) ? $category = $title[1] : $category; |
||
| 415 | |||
| 416 | // Load it as setting => value or the old setting local master |
||
| 417 | if (preg_match('~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~', $line, $val)) |
||
| 418 | $pinfo[$category][$val[1]] = $val[2]; |
||
| 419 | elseif (preg_match('~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~', $line, $val)) |
||
| 420 | $pinfo[$category][$val[1]] = array($txt['phpinfo_localsettings'] => $val[2], $txt['phpinfo_defaultsettings'] => $val[3]); |
||
| 421 | } |
||
| 422 | |||
| 423 | // Load it in to context and display it |
||
| 424 | $context['pinfo'] = $pinfo; |
||
| 425 | $context['page_title'] = $txt['admin_server_settings']; |
||
| 426 | $context['sub_template'] = 'php_info'; |
||
| 427 | |||
| 428 | return; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * This function returns all general settings. |
||
| 433 | * |
||
| 434 | * @event integrate_modify_general_settings |
||
| 435 | */ |
||
| 436 | private function _generalSettings() |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Return the search settings for use in admin search |
||
| 462 | */ |
||
| 463 | public function generalSettings_search() |
||
| 464 | { |
||
| 465 | return $this->_generalSettings(); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * This function returns database settings. |
||
| 470 | * |
||
| 471 | * @event integrate_modify_database_settings |
||
| 472 | */ |
||
| 473 | private function _databaseSettings() |
||
| 474 | { |
||
| 475 | global $txt; |
||
| 476 | |||
| 477 | // initialize settings |
||
| 478 | $config_vars = array( |
||
| 479 | array('db_server', $txt['database_server'], 'file', 'text'), |
||
| 480 | array('db_user', $txt['database_user'], 'file', 'text'), |
||
| 481 | array('db_passwd', $txt['database_password'], 'file', 'password'), |
||
| 482 | array('db_name', $txt['database_name'], 'file', 'text'), |
||
| 483 | array('db_prefix', $txt['database_prefix'], 'file', 'text'), |
||
| 484 | array('db_persist', $txt['db_persist'], 'file', 'check', null, 'db_persist'), |
||
| 485 | array('db_error_send', $txt['db_error_send'], 'file', 'check'), |
||
| 486 | array('ssi_db_user', $txt['ssi_db_user'], 'file', 'text', null, 'ssi_db_user'), |
||
| 487 | array('ssi_db_passwd', $txt['ssi_db_passwd'], 'file', 'password'), |
||
| 488 | '', |
||
| 489 | array('autoFixDatabase', $txt['autoFixDatabase'], 'db', 'check', false, 'autoFixDatabase'), |
||
| 490 | array('autoOptMaxOnline', $txt['autoOptMaxOnline'], 'subtext' => $txt['zero_for_no_limit'], 'db', 'int'), |
||
| 491 | '', |
||
| 492 | array('boardurl', $txt['admin_url'], 'file', 'text', 36), |
||
| 493 | array('boarddir', $txt['boarddir'], 'file', 'text', 36), |
||
| 494 | array('sourcedir', $txt['sourcesdir'], 'file', 'text', 36), |
||
| 495 | array('cachedir', $txt['cachedir'], 'file', 'text', 36), |
||
| 496 | ); |
||
| 497 | |||
| 498 | // Notify the integration |
||
| 499 | call_integration_hook('integrate_modify_database_settings', array(&$config_vars)); |
||
| 500 | |||
| 501 | return $config_vars; |
||
| 502 | } |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Return the search settings for use in admin search |
||
| 506 | */ |
||
| 507 | public function databaseSettings_search() |
||
| 508 | { |
||
| 509 | return $this->_databaseSettings(); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * This little function returns all cookie settings. |
||
| 514 | * |
||
| 515 | * @event integrate_modify_cookie_settings |
||
| 516 | */ |
||
| 517 | private function _cookieSettings() |
||
| 518 | { |
||
| 519 | global $txt; |
||
| 520 | |||
| 521 | // Define the variables we want to edit or show in the cookie form. |
||
| 522 | $config_vars = array( |
||
| 523 | // Cookies... |
||
| 524 | array('cookiename', $txt['cookie_name'], 'file', 'text', 20), |
||
| 525 | array('cookieTime', $txt['cookieTime'], 'db', 'int', 'postinput' => $txt['minutes']), |
||
| 526 | array('localCookies', $txt['localCookies'], 'subtext' => $txt['localCookies_note'], 'db', 'check', false, 'localCookies'), |
||
| 527 | array('globalCookies', $txt['globalCookies'], 'subtext' => $txt['globalCookies_note'], 'db', 'check', false, 'globalCookies'), |
||
| 528 | array('globalCookiesDomain', $txt['globalCookiesDomain'], 'subtext' => $txt['globalCookiesDomain_note'], 'db', 'text', false, 'globalCookiesDomain'), |
||
| 529 | array('secureCookies', $txt['secureCookies'], 'subtext' => $txt['secureCookies_note'], 'db', 'check', false, 'secureCookies', 'disabled' => !isset($_SERVER['HTTPS']) || !(strtolower($_SERVER['HTTPS']) === 'on' || strtolower($_SERVER['HTTPS']) == '1')), |
||
| 530 | array('httponlyCookies', $txt['httponlyCookies'], 'subtext' => $txt['httponlyCookies_note'], 'db', 'check', false, 'httponlyCookies'), |
||
| 531 | '', |
||
| 532 | // Sessions |
||
| 533 | array('databaseSession_enable', $txt['databaseSession_enable'], 'db', 'check', false, 'databaseSession_enable'), |
||
| 534 | array('databaseSession_loose', $txt['databaseSession_loose'], 'db', 'check', false, 'databaseSession_loose'), |
||
| 535 | array('databaseSession_lifetime', $txt['databaseSession_lifetime'], 'db', 'int', false, 'databaseSession_lifetime', 'postinput' => $txt['seconds']), |
||
| 536 | ); |
||
| 537 | |||
| 538 | // Notify the integration |
||
| 539 | call_integration_hook('integrate_modify_cookie_settings', array(&$config_vars)); |
||
| 540 | |||
| 541 | // Set them vars for our settings form |
||
| 542 | return $config_vars; |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Return the search settings for use in admin search |
||
| 547 | */ |
||
| 548 | public function cookieSettings_search() |
||
| 549 | { |
||
| 550 | return $this->_cookieSettings(); |
||
| 551 | } |
||
| 552 | |||
| 553 | /** |
||
| 554 | * This little function returns all cache settings. |
||
| 555 | * |
||
| 556 | * @event integrate_modify_cache_settings |
||
| 557 | */ |
||
| 558 | private function _cacheSettings() |
||
| 559 | { |
||
| 560 | global $txt; |
||
| 561 | |||
| 562 | // Detect all available optimizers |
||
| 563 | $detected = loadCacheEngines(false); |
||
| 564 | $detected_names = array(); |
||
| 565 | $detected_supported = array(); |
||
| 566 | |||
| 567 | foreach ($detected as $key => $value) |
||
| 568 | { |
||
| 569 | $detected_names[] = $value->title(); |
||
| 570 | $supported = $value->isAvailable(); |
||
| 571 | |||
| 572 | if (!empty($supported)) |
||
| 573 | $detected_supported[$key] = $value->title(); |
||
| 574 | } |
||
| 575 | |||
| 576 | $txt['caching_information'] = str_replace('{supported_accelerators}', '<i>' . implode(', ', $detected_names) . '</i><br />', $txt['caching_information']); |
||
| 577 | |||
| 578 | // Set our values to show what, if anything, we found |
||
| 579 | $txt['cache_settings_message'] = sprintf($txt['detected_accelerators'], implode(', ', $detected_supported)); |
||
| 580 | $cache_level = array($txt['cache_off'], $txt['cache_level1'], $txt['cache_level2'], $txt['cache_level3']); |
||
| 581 | |||
| 582 | // Define the variables we want to edit. |
||
| 583 | $config_vars = array( |
||
| 584 | // Only a few settings, but they are important |
||
| 585 | array('cache_enable', $txt['cache_enable'], 'file', 'select', $cache_level, 'cache_enable'), |
||
| 586 | array('cache_accelerator', $txt['cache_accelerator'], 'file', 'select', $detected_supported), |
||
| 587 | ); |
||
| 588 | |||
| 589 | // If the cache engine has specific settings, add them in |
||
| 590 | foreach ($detected as $key => $value) |
||
| 591 | { |
||
| 592 | if ($value->isAvailable()) |
||
| 593 | { |
||
| 594 | $value->settings($config_vars); |
||
| 595 | } |
||
| 596 | } |
||
| 597 | |||
| 598 | // Notify the integration that we're preparing to mess up with cache settings... |
||
| 599 | call_integration_hook('integrate_modify_cache_settings', array(&$config_vars)); |
||
| 600 | |||
| 601 | return $config_vars; |
||
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Return the search settings for use in admin search |
||
| 606 | */ |
||
| 607 | public function cacheSettings_search() |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * This little function returns load management settings. |
||
| 614 | * |
||
| 615 | * @event integrate_modify_loadavg_settings |
||
| 616 | */ |
||
| 617 | private function _loadavgSettings() |
||
| 669 | } |
||
| 670 | |||
| 671 | /** |
||
| 672 | * Return the search settings for use in admin search |
||
| 673 | */ |
||
| 674 | public function balancingSettings_search() |
||
| 677 | } |
||
| 678 | } |
||
| 679 |
If you suppress an error, we recommend checking for the error condition explicitly: