PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/admin/ManageServer.php

https://github.com/Arantor/Elkarte
PHP | 972 lines | 590 code | 134 blank | 248 comment | 144 complexity | 1fbd1e4d083971d4da5821faee901673 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * Contains all the functionality required to be able to edit the core server settings.
  16. * This includes anything from which an error may result in the forum destroying itself in a firey fury.
  17. *
  18. * Adding options to one of the setting screens isn't hard. Call prepareDBSettingsContext;
  19. * The basic format for a checkbox is:
  20. * array('check', 'nameInModSettingsAndSQL'),
  21. * And for a text box:
  22. * array('text', 'nameInModSettingsAndSQL')
  23. * (NOTE: You have to add an entry for this at the bottom!)
  24. *
  25. * In these cases, it will look for $txt['nameInModSettingsAndSQL'] as the description,
  26. * and $helptxt['nameInModSettingsAndSQL'] as the help popup description.
  27. *
  28. * Here's a quick explanation of how to add a new item:
  29. *
  30. * - A text input box. For textual values.
  31. * array('text', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  32. * - A text input box. For numerical values.
  33. * array('int', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  34. * - A text input box. For floating point values.
  35. * array('float', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  36. * - A large text input box. Used for textual values spanning multiple lines.
  37. * array('large_text', 'nameInModSettingsAndSQL', 'OptionalNumberOfRows'),
  38. * - A check box. Either one or zero. (boolean)
  39. * array('check', 'nameInModSettingsAndSQL'),
  40. * - A selection box. Used for the selection of something from a list.
  41. * array('select', 'nameInModSettingsAndSQL', array('valueForSQL' => $txt['displayedValue'])),
  42. * Note that just saying array('first', 'second') will put 0 in the SQL for 'first'.
  43. * - A password input box. Used for passwords, no less!
  44. * array('password', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  45. * - A permission - for picking groups who have a permission.
  46. * array('permissions', 'manage_groups'),
  47. * - A BBC selection box.
  48. * array('bbc', 'sig_bbc'),
  49. *
  50. * For each option:
  51. * - type (see above), variable name, size/possible values.
  52. * OR make type '' for an empty string for a horizontal rule.
  53. * - SET preinput - to put some HTML prior to the input box.
  54. * - SET postinput - to put some HTML following the input box.
  55. * - SET invalid - to mark the data as invalid.
  56. * - PLUS you can override label and help parameters by forcing their keys in the array, for example:
  57. * array('text', 'invalidlabel', 3, 'label' => 'Actual Label')
  58. *
  59. */
  60. if (!defined('ELKARTE'))
  61. die('No access...');
  62. /**
  63. * This is the main dispatcher. Sets up all the available sub-actions, all the tabs and selects
  64. * the appropriate one based on the sub-action.
  65. *
  66. * Requires the admin_forum permission.
  67. * Redirects to the appropriate function based on the sub-action.
  68. *
  69. * @uses edit_settings adminIndex.
  70. */
  71. function ModifySettings()
  72. {
  73. global $context, $txt, $scripturl;
  74. // This is just to keep the database password more secure.
  75. isAllowedTo('admin_forum');
  76. // Load up all the tabs...
  77. $context[$context['admin_menu_name']]['tab_data'] = array(
  78. 'title' => $txt['admin_server_settings'],
  79. 'help' => 'serversettings',
  80. 'description' => $txt['admin_basic_settings'],
  81. );
  82. checkSession('request');
  83. // The settings are in here, I swear!
  84. loadLanguage('ManageSettings');
  85. $context['page_title'] = $txt['admin_server_settings'];
  86. $context['sub_template'] = 'show_settings';
  87. $subActions = array(
  88. 'general' => 'ModifyGeneralSettings',
  89. 'database' => 'ModifyDatabaseSettings',
  90. 'cookie' => 'ModifyCookieSettings',
  91. 'cache' => 'ModifyCacheSettings',
  92. 'loads' => 'ModifyLoadBalancingSettings',
  93. 'phpinfo' => 'ShowPHPinfoSettings',
  94. );
  95. call_integration_hook('integrate_server_settings', array($subActions));
  96. // By default we're editing the core settings
  97. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'general';
  98. $context['sub_action'] = $_REQUEST['sa'];
  99. // Any messages to speak of?
  100. $context['settings_message'] = (isset($_REQUEST['msg']) && isset($txt[$_REQUEST['msg']])) ? $txt[$_REQUEST['msg']] : '';
  101. // Warn the user if there's any relevant information regarding Settings.php.
  102. if ($_REQUEST['sa'] != 'cache')
  103. {
  104. // Warn the user if the backup of Settings.php failed.
  105. $settings_not_writable = !is_writable(BOARDDIR . '/Settings.php');
  106. $settings_backup_fail = !@is_writable(BOARDDIR . '/Settings_bak.php') || !@copy(BOARDDIR . '/Settings.php', BOARDDIR . '/Settings_bak.php');
  107. if ($settings_not_writable)
  108. $context['settings_message'] = '<div class="centertext"><strong>' . $txt['settings_not_writable'] . '</strong></div><br />';
  109. elseif ($settings_backup_fail)
  110. $context['settings_message'] = '<div class="centertext"><strong>' . $txt['admin_backup_fail'] . '</strong></div><br />';
  111. $context['settings_not_writable'] = $settings_not_writable;
  112. }
  113. // Call the right function for this sub-action.
  114. $subActions[$_REQUEST['sa']]();
  115. }
  116. /**
  117. * General forum settings - forum name, maintenance mode, etc.
  118. * Practically, this shows an interface for the settings in Settings.php to be changed.
  119. *
  120. * - It uses the rawdata sub template (not theme-able.)
  121. * - Requires the admin_forum permission.
  122. * - Uses the edit_settings administration area.
  123. * - Contains the actual array of settings to show from Settings.php.
  124. * - Accessed from ?action=admin;area=serversettings;sa=general.
  125. *
  126. * @param $return_config
  127. */
  128. function ModifyGeneralSettings($return_config = false)
  129. {
  130. global $scripturl, $context, $txt;
  131. /* If you're writing a mod, it's a bad idea to add things here....
  132. For each option:
  133. variable name, description, type (constant), size/possible values, helptext.
  134. OR an empty string for a horizontal rule.
  135. OR a string for a titled section. */
  136. $config_vars = array(
  137. array('mbname', $txt['admin_title'], 'file', 'text', 30),
  138. '',
  139. array('maintenance', $txt['admin_maintain'], 'file', 'check'),
  140. array('mtitle', $txt['maintenance_subject'], 'file', 'text', 36),
  141. array('mmessage', $txt['maintenance_message'], 'file', 'text', 36),
  142. '',
  143. array('webmaster_email', $txt['admin_webmaster_email'], 'file', 'text', 30),
  144. '',
  145. array('enableCompressedOutput', $txt['enableCompressedOutput'], 'db', 'check', null, 'enableCompressedOutput'),
  146. array('disableTemplateEval', $txt['disableTemplateEval'], 'db', 'check', null, 'disableTemplateEval'),
  147. array('disableHostnameLookup', $txt['disableHostnameLookup'], 'db', 'check', null, 'disableHostnameLookup'),
  148. );
  149. call_integration_hook('integrate_general_settings', array($config_vars));
  150. if ($return_config)
  151. return $config_vars;
  152. // Setup the template stuff.
  153. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=general;save';
  154. $context['settings_title'] = $txt['general_settings'];
  155. // Saving settings?
  156. if (isset($_REQUEST['save']))
  157. {
  158. call_integration_hook('integrate_save_general_settings');
  159. saveSettings($config_vars);
  160. redirectexit('action=admin;area=serversettings;sa=general;' . $context['session_var'] . '=' . $context['session_id']. ';msg=' . (!empty($context['settings_message']) ? $context['settings_message'] : 'core_settings_saved'));
  161. }
  162. // Fill the config array.
  163. prepareServerSettingsContext($config_vars);
  164. }
  165. /**
  166. * Basic database and paths settings - database name, host, etc.
  167. *
  168. * - It shows an interface for the settings in Settings.php to be changed.
  169. * - It contains the actual array of settings to show from Settings.php.
  170. * - It uses the rawdata sub template (not theme-able.)
  171. * - Requires the admin_forum permission.
  172. * - Uses the edit_settings administration area.
  173. * - Accessed from ?action=admin;area=serversettings;sa=database.
  174. *
  175. * @param $return_config
  176. */
  177. function ModifyDatabaseSettings($return_config = false)
  178. {
  179. global $scripturl, $context, $settings, $txt;
  180. /* If you're writing a mod, it's a bad idea to add things here....
  181. For each option:
  182. variable name, description, type (constant), size/possible values, helptext.
  183. OR an empty string for a horizontal rule.
  184. OR a string for a titled section. */
  185. $config_vars = array(
  186. array('db_server', $txt['database_server'], 'file', 'text'),
  187. array('db_user', $txt['database_user'], 'file', 'text'),
  188. array('db_passwd', $txt['database_password'], 'file', 'password'),
  189. array('db_name', $txt['database_name'], 'file', 'text'),
  190. array('db_prefix', $txt['database_prefix'], 'file', 'text'),
  191. array('db_persist', $txt['db_persist'], 'file', 'check', null, 'db_persist'),
  192. array('db_error_send', $txt['db_error_send'], 'file', 'check'),
  193. array('ssi_db_user', $txt['ssi_db_user'], 'file', 'text', null, 'ssi_db_user'),
  194. array('ssi_db_passwd', $txt['ssi_db_passwd'], 'file', 'password'),
  195. '',
  196. array('autoFixDatabase', $txt['autoFixDatabase'], 'db', 'check', false, 'autoFixDatabase'),
  197. array('autoOptMaxOnline', $txt['autoOptMaxOnline'], 'subtext' => $txt['zero_for_no_limit'], 'db', 'int'),
  198. '',
  199. array('boardurl', $txt['admin_url'], 'file', 'text', 36),
  200. array('boarddir', $txt['boarddir'], 'file', 'text', 36),
  201. array('sourcedir', $txt['sourcesdir'], 'file', 'text', 36),
  202. array('cachedir', $txt['cachedir'], 'file', 'text', 36),
  203. );
  204. call_integration_hook('integrate_database_settings', array($config_vars));
  205. if ($return_config)
  206. return $config_vars;
  207. // Setup the template stuff.
  208. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=database;save';
  209. $context['settings_title'] = $txt['database_paths_settings'];
  210. $context['save_disabled'] = $context['settings_not_writable'];
  211. // Saving settings?
  212. if (isset($_REQUEST['save']))
  213. {
  214. call_integration_hook('integrate_save_database_settings');
  215. saveSettings($config_vars);
  216. redirectexit('action=admin;area=serversettings;sa=database;' . $context['session_var'] . '=' . $context['session_id'] . ';msg=' . (!empty($context['settings_message']) ? $context['settings_message'] : 'core_settings_saved'));
  217. }
  218. // Fill the config array.
  219. prepareServerSettingsContext($config_vars);
  220. }
  221. /**
  222. * This function handles cookies settings modifications.
  223. *
  224. * @param bool $return_config = false
  225. */
  226. function ModifyCookieSettings($return_config = false)
  227. {
  228. global $context, $scripturl, $txt, $modSettings, $cookiename, $user_settings, $boardurl;
  229. // Define the variables we want to edit.
  230. $config_vars = array(
  231. // Cookies...
  232. array('cookiename', $txt['cookie_name'], 'file', 'text', 20),
  233. array('cookieTime', $txt['cookieTime'], 'db', 'int', 'postinput' => $txt['minutes']),
  234. array('localCookies', $txt['localCookies'], 'subtext' => $txt['localCookies_note'], 'db', 'check', false, 'localCookies'),
  235. array('globalCookies', $txt['globalCookies'], 'subtext' => $txt['globalCookies_note'], 'db', 'check', false, 'globalCookies'),
  236. array('globalCookiesDomain', $txt['globalCookiesDomain'], 'subtext' => $txt['globalCookiesDomain_note'], 'db', 'text', false, 'globalCookiesDomain'),
  237. array('secureCookies', $txt['secureCookies'], 'subtext' => $txt['secureCookies_note'], 'db', 'check', false, 'secureCookies', 'disabled' => !isset($_SERVER['HTTPS']) || !(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == '1')),
  238. array('httponlyCookies', $txt['httponlyCookies'], 'subtext' => $txt['httponlyCookies_note'], 'db', 'check', false, 'httponlyCookies'),
  239. '',
  240. // Sessions
  241. array('databaseSession_enable', $txt['databaseSession_enable'], 'db', 'check', false, 'databaseSession_enable'),
  242. array('databaseSession_loose', $txt['databaseSession_loose'], 'db', 'check', false, 'databaseSession_loose'),
  243. array('databaseSession_lifetime', $txt['databaseSession_lifetime'], 'db', 'int', false, 'databaseSession_lifetime', 'postinput' => $txt['seconds']),
  244. );
  245. call_integration_hook('integrate_cookie_settings', array($config_vars));
  246. if ($return_config)
  247. return $config_vars;
  248. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cookie;save';
  249. $context['settings_title'] = $txt['cookies_sessions_settings'];
  250. // Saving settings?
  251. if (isset($_REQUEST['save']))
  252. {
  253. call_integration_hook('integrate_save_cookie_settings');
  254. if (!empty($_POST['globalCookiesDomain']) && strpos($boardurl, $_POST['globalCookiesDomain']) === false)
  255. fatal_lang_error('invalid_cookie_domain', false);
  256. saveSettings($config_vars);
  257. // If the cookie name was changed, reset the cookie.
  258. if ($cookiename != $_POST['cookiename'])
  259. {
  260. $original_session_id = $context['session_id'];
  261. include_once(SUBSDIR . '/Auth.subs.php');
  262. // Remove the old cookie.
  263. setLoginCookie(-3600, 0);
  264. // Set the new one.
  265. $cookiename = $_POST['cookiename'];
  266. setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], sha1($user_settings['passwd'] . $user_settings['password_salt']));
  267. redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $original_session_id, $context['server']['needs_login_fix']);
  268. }
  269. redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $context['session_id']. ';msg=' . (!empty($context['settings_message']) ? $context['settings_message'] : 'core_settings_saved'));
  270. }
  271. // Fill the config array.
  272. prepareServerSettingsContext($config_vars);
  273. }
  274. /**
  275. * Simply modifying cache functions
  276. *
  277. * @param bool $return_config = false
  278. */
  279. function ModifyCacheSettings($return_config = false)
  280. {
  281. global $context, $scripturl, $txt, $helptxt, $cache_enable;
  282. // Detect all available optimizers
  283. $detected = array();
  284. if (function_exists('eaccelerator_put'))
  285. $detected['eaccelerator'] = $txt['eAccelerator_cache'];
  286. if (function_exists('mmcache_put'))
  287. $detected['mmcache'] = $txt['mmcache_cache'];
  288. if (function_exists('apc_store'))
  289. $detected['apc'] = $txt['apc_cache'];
  290. if (function_exists('output_cache_put') || function_exists('zend_shm_cache_store'))
  291. $detected['zend'] = $txt['zend_cache'];
  292. if (function_exists('memcache_set') || function_exists('memcached_set'))
  293. $detected['memcached'] = $txt['memcached_cache'];
  294. if (function_exists('xcache_set'))
  295. $detected['xcache'] = $txt['xcache_cache'];
  296. if (function_exists('file_put_contents'))
  297. $detected['filebased'] = $txt['default_cache'];
  298. // set our values to show what, if anything, we found
  299. if (empty($detected))
  300. {
  301. $txt['cache_settings_message'] = $txt['detected_no_caching'];
  302. $cache_level = array($txt['cache_off']);
  303. $detected['none'] = $txt['cache_off'];
  304. }
  305. else
  306. {
  307. $txt['cache_settings_message'] = sprintf($txt['detected_accelerators'], implode(', ', $detected));
  308. $cache_level = array($txt['cache_off'], $txt['cache_level1'], $txt['cache_level2'], $txt['cache_level3']);
  309. }
  310. // Define the variables we want to edit.
  311. $config_vars = array(
  312. // Only a few settings, but they are important
  313. array('', $txt['cache_settings_message'], '', 'desc'),
  314. array('cache_enable', $txt['cache_enable'], 'file', 'select', $cache_level, 'cache_enable'),
  315. array('cache_accelerator', $txt['cache_accelerator'], 'file', 'select', $detected),
  316. array('cache_memcached', $txt['cache_memcached'], 'file', 'text', $txt['cache_memcached'], 'cache_memcached'),
  317. array('cachedir', $txt['cachedir'], 'file', 'text', 36, 'cache_cachedir'),
  318. );
  319. // some javascript to enable / disable certain settings if the option is not selected
  320. $context['settings_post_javascript'] = '
  321. var cache_type = document.getElementById(\'cache_accelerator\');
  322. createEventListener(cache_type);
  323. cache_type.addEventListener("change", toggleCache);
  324. toggleCache();';
  325. call_integration_hook('integrate_modify_cache_settings', array($config_vars));
  326. if ($return_config)
  327. return $config_vars;
  328. // Saving again?
  329. if (isset($_GET['save']))
  330. {
  331. call_integration_hook('integrate_save_cache_settings');
  332. saveSettings($config_vars);
  333. // we need to save the $cache_enable to $modSettings as well
  334. updatesettings(array('cache_enable' => (int) $_POST['cache_enable']));
  335. // exit so we reload our new settings on the page
  336. redirectexit('action=admin;area=serversettings;sa=cache;' . $context['session_var'] . '=' . $context['session_id']);
  337. }
  338. loadLanguage('ManageMaintenance');
  339. createToken('admin-maint');
  340. $context['template_layers'][] = 'clean_cache_button';
  341. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cache;save';
  342. $context['settings_title'] = $txt['caching_settings'];
  343. $context['settings_message'] = $txt['caching_information'];
  344. // Prepare the template.
  345. createToken('admin-ssc');
  346. prepareServerSettingsContext($config_vars);
  347. }
  348. /**
  349. * Allows to edit load balancing settings.
  350. *
  351. * @param bool $return_config = false
  352. */
  353. function ModifyLoadBalancingSettings($return_config = false)
  354. {
  355. global $txt, $scripturl, $context, $settings, $modSettings;
  356. // Setup a warning message, but disabled by default.
  357. $disabled = true;
  358. $context['settings_message'] = $txt['loadavg_disabled_conf'];
  359. if (stripos(PHP_OS, 'win') === 0)
  360. $context['settings_message'] = $txt['loadavg_disabled_windows'];
  361. else
  362. {
  363. $modSettings['load_average'] = @file_get_contents('/proc/loadavg');
  364. if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) !== 0)
  365. $modSettings['load_average'] = (float) $matches[1];
  366. elseif (($modSettings['load_average'] = @`uptime`) !== null && preg_match('~load averages?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) !== 0)
  367. $modSettings['load_average'] = (float) $matches[1];
  368. else
  369. unset($modSettings['load_average']);
  370. if (!empty($modSettings['load_average']))
  371. {
  372. $context['settings_message'] = sprintf($txt['loadavg_warning'], $modSettings['load_average']);
  373. $disabled = false;
  374. }
  375. }
  376. // Start with a simple checkbox.
  377. $config_vars = array(
  378. array('check', 'loadavg_enable', 'disabled' => $disabled),
  379. );
  380. // Set the default values for each option.
  381. $default_values = array(
  382. 'loadavg_auto_opt' => '1.0',
  383. 'loadavg_search' => '2.5',
  384. 'loadavg_allunread' => '2.0',
  385. 'loadavg_unreadreplies' => '3.5',
  386. 'loadavg_show_posts' => '2.0',
  387. 'loadavg_userstats' => '10.0',
  388. 'loadavg_bbc' => '30.0',
  389. 'loadavg_forum' => '40.0',
  390. );
  391. // Loop through the settings.
  392. foreach ($default_values as $name => $value)
  393. {
  394. // Use the default value if the setting isn't set yet.
  395. $value = !isset($modSettings[$name]) ? $value : $modSettings[$name];
  396. $config_vars[] = array('text', $name, 'value' => $value, 'disabled' => $disabled);
  397. }
  398. call_integration_hook('integrate_loadavg_settings', array($config_vars));
  399. if ($return_config)
  400. return $config_vars;
  401. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=loads;save';
  402. $context['settings_title'] = $txt['load_balancing_settings'];
  403. // Saving?
  404. if (isset($_GET['save']))
  405. {
  406. // Stupidity is not allowed.
  407. foreach ($_POST as $key => $value)
  408. {
  409. if (strpos($key, 'loadavg') === 0 || $key === 'loadavg_enable')
  410. continue;
  411. elseif ($key == 'loadavg_auto_opt' && $value <= 1)
  412. $_POST['loadavg_auto_opt'] = '1.0';
  413. elseif ($key == 'loadavg_forum' && $value < 10)
  414. $_POST['loadavg_forum'] = '10.0';
  415. elseif ($value < 2)
  416. $_POST[$key] = '2.0';
  417. }
  418. call_integration_hook('integrate_save_loadavg_settings');
  419. saveDBSettings($config_vars);
  420. redirectexit('action=admin;area=serversettings;sa=loads;' . $context['session_var'] . '=' . $context['session_id']);
  421. }
  422. createToken('admin-ssc');
  423. createToken('admin-dbsc');
  424. prepareDBSettingContext($config_vars);
  425. }
  426. /**
  427. * Helper function, it sets up the context for the manage server settings.
  428. * - The basic usage of the six numbered key fields are
  429. * - array (0 ,1, 2, 3, 4, 5
  430. * 0 variable name - the name of the saved variable
  431. * 1 label - the text to show on the settings page
  432. * 2 saveto - file or db, where to save the variable name - value pair
  433. * 3 type - type of data to save, int, float, text, check
  434. * 4 size - false or field size
  435. * 5 help - '' or helptxt variable name
  436. * )
  437. *
  438. * the following named keys are also permitted
  439. * 'disabled' => 'postinput' => 'preinput' =>
  440. *
  441. * @param array $config_vars
  442. */
  443. function prepareServerSettingsContext(&$config_vars)
  444. {
  445. global $context, $modSettings;
  446. $context['config_vars'] = array();
  447. $defines = array(
  448. 'boarddir',
  449. 'sourcedir',
  450. 'cachedir',
  451. );
  452. foreach ($config_vars as $identifier => $config_var)
  453. {
  454. if (!is_array($config_var) || !isset($config_var[1]))
  455. $context['config_vars'][] = $config_var;
  456. else
  457. {
  458. $varname = $config_var[0];
  459. global $$varname;
  460. // Set the subtext in case it's part of the label.
  461. // @todo Temporary. Preventing divs inside label tags.
  462. $divPos = strpos($config_var[1], '<div');
  463. $subtext = '';
  464. if ($divPos !== false)
  465. {
  466. $subtext = preg_replace('~</?div[^>]*>~', '', substr($config_var[1], $divPos));
  467. $config_var[1] = substr($config_var[1], 0, $divPos);
  468. }
  469. $context['config_vars'][$config_var[0]] = array(
  470. 'label' => $config_var[1],
  471. 'help' => isset($config_var[5]) ? $config_var[5] : '',
  472. 'type' => $config_var[3],
  473. 'size' => empty($config_var[4]) ? 0 : $config_var[4],
  474. 'data' => isset($config_var[4]) && is_array($config_var[4]) && $config_var[3] != 'select' ? $config_var[4] : array(),
  475. 'name' => $config_var[0],
  476. 'value' => $config_var[2] == 'file' ? (in_array($varname, $defines) ? constant(strtoupper($varname)): htmlspecialchars($$varname)) : (isset($modSettings[$config_var[0]]) ? htmlspecialchars($modSettings[$config_var[0]]) : (in_array($config_var[3], array('int', 'float')) ? 0 : '')),
  477. 'disabled' => !empty($context['settings_not_writable']) || !empty($config_var['disabled']),
  478. 'invalid' => false,
  479. 'subtext' => !empty($config_var['subtext']) ? $config_var['subtext'] : $subtext,
  480. 'javascript' => '',
  481. 'preinput' => !empty($config_var['preinput']) ? $config_var['preinput'] : '',
  482. 'postinput' => !empty($config_var['postinput']) ? $config_var['postinput'] : '',
  483. );
  484. // If this is a select box handle any data.
  485. if (!empty($config_var[4]) && is_array($config_var[4]))
  486. {
  487. // If it's associative
  488. $config_values = array_values($config_var[4]);
  489. if (isset($config_values[0]) && is_array($config_values[0]))
  490. $context['config_vars'][$config_var[0]]['data'] = $config_var[4];
  491. else
  492. {
  493. foreach ($config_var[4] as $key => $item)
  494. $context['config_vars'][$config_var[0]]['data'][] = array($key, $item);
  495. }
  496. }
  497. }
  498. }
  499. // Two tokens because saving these settings requires both saveSettings and saveDBSettings
  500. createToken('admin-ssc');
  501. createToken('admin-dbsc');
  502. }
  503. /**
  504. * Helper function, it sets up the context for database settings.
  505. *
  506. * @param array $config_vars
  507. */
  508. function prepareDBSettingContext(&$config_vars)
  509. {
  510. global $txt, $helptxt, $context, $modSettings;
  511. loadLanguage('Help');
  512. $context['config_vars'] = array();
  513. $inlinePermissions = array();
  514. $bbcChoice = array();
  515. foreach ($config_vars as $config_var)
  516. {
  517. // HR?
  518. if (!is_array($config_var))
  519. $context['config_vars'][] = $config_var;
  520. else
  521. {
  522. // If it has no name it doesn't have any purpose!
  523. if (empty($config_var[1]))
  524. continue;
  525. // Special case for inline permissions
  526. if ($config_var[0] == 'permissions' && allowedTo('manage_permissions'))
  527. $inlinePermissions[] = $config_var[1];
  528. elseif ($config_var[0] == 'permissions')
  529. continue;
  530. // Are we showing the BBC selection box?
  531. if ($config_var[0] == 'bbc')
  532. $bbcChoice[] = $config_var[1];
  533. $context['config_vars'][$config_var[1]] = array(
  534. 'label' => isset($config_var['text_label']) ? $config_var['text_label'] : (isset($txt[$config_var[1]]) ? $txt[$config_var[1]] : (isset($config_var[3]) && !is_array($config_var[3]) ? $config_var[3] : '')),
  535. 'help' => isset($helptxt[$config_var[1]]) ? $config_var[1] : '',
  536. 'type' => $config_var[0],
  537. 'size' => !empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], array('int', 'float')) ? 6 : 0),
  538. 'data' => array(),
  539. 'name' => $config_var[1],
  540. 'value' => isset($modSettings[$config_var[1]]) ? ($config_var[0] == 'select' ? $modSettings[$config_var[1]] : htmlspecialchars($modSettings[$config_var[1]])) : (in_array($config_var[0], array('int', 'float')) ? 0 : ''),
  541. 'disabled' => false,
  542. 'invalid' => !empty($config_var['invalid']),
  543. 'javascript' => '',
  544. 'var_message' => !empty($config_var['message']) && isset($txt[$config_var['message']]) ? $txt[$config_var['message']] : '',
  545. 'preinput' => isset($config_var['preinput']) ? $config_var['preinput'] : '',
  546. 'postinput' => isset($config_var['postinput']) ? $config_var['postinput'] : '',
  547. );
  548. // If this is a select box handle any data.
  549. if (!empty($config_var[2]) && is_array($config_var[2]))
  550. {
  551. // If we allow multiple selections, we need to adjust a few things.
  552. if ($config_var[0] == 'select' && !empty($config_var['multiple']))
  553. {
  554. $context['config_vars'][$config_var[1]]['name'] .= '[]';
  555. $context['config_vars'][$config_var[1]]['value'] = unserialize($context['config_vars'][$config_var[1]]['value']);
  556. }
  557. // If it's associative
  558. if (isset($config_var[2][0]) && is_array($config_var[2][0]))
  559. $context['config_vars'][$config_var[1]]['data'] = $config_var[2];
  560. else
  561. {
  562. foreach ($config_var[2] as $key => $item)
  563. $context['config_vars'][$config_var[1]]['data'][] = array($key, $item);
  564. }
  565. }
  566. // Finally allow overrides - and some final cleanups.
  567. foreach ($config_var as $k => $v)
  568. {
  569. if (!is_numeric($k))
  570. {
  571. if (substr($k, 0, 2) == 'on')
  572. $context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"';
  573. else
  574. $context['config_vars'][$config_var[1]][$k] = $v;
  575. }
  576. // See if there are any other labels that might fit?
  577. if (isset($txt['setting_' . $config_var[1]]))
  578. $context['config_vars'][$config_var[1]]['label'] = $txt['setting_' . $config_var[1]];
  579. elseif (isset($txt['groups_' . $config_var[1]]))
  580. $context['config_vars'][$config_var[1]]['label'] = $txt['groups_' . $config_var[1]];
  581. }
  582. // Set the subtext in case it's part of the label.
  583. // @todo Temporary. Preventing divs inside label tags.
  584. $divPos = strpos($context['config_vars'][$config_var[1]]['label'], '<div');
  585. if ($divPos !== false)
  586. {
  587. $context['config_vars'][$config_var[1]]['subtext'] = preg_replace('~</?div[^>]*>~', '', substr($context['config_vars'][$config_var[1]]['label'], $divPos));
  588. $context['config_vars'][$config_var[1]]['label'] = substr($context['config_vars'][$config_var[1]]['label'], 0, $divPos);
  589. }
  590. }
  591. }
  592. // If we have inline permissions we need to prep them.
  593. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  594. {
  595. require_once(ADMINDIR . '/ManagePermissions.php');
  596. init_inline_permissions($inlinePermissions, isset($context['permissions_excluded']) ? $context['permissions_excluded'] : array());
  597. }
  598. // What about any BBC selection boxes?
  599. if (!empty($bbcChoice))
  600. {
  601. // What are the options, eh?
  602. $temp = parse_bbc(false);
  603. $bbcTags = array();
  604. foreach ($temp as $tag)
  605. $bbcTags[] = $tag['tag'];
  606. $bbcTags = array_unique($bbcTags);
  607. $totalTags = count($bbcTags);
  608. // The number of columns we want to show the BBC tags in.
  609. $numColumns = isset($context['num_bbc_columns']) ? $context['num_bbc_columns'] : 3;
  610. // Start working out the context stuff.
  611. $context['bbc_columns'] = array();
  612. $tagsPerColumn = ceil($totalTags / $numColumns);
  613. $col = 0; $i = 0;
  614. foreach ($bbcTags as $tag)
  615. {
  616. if ($i % $tagsPerColumn == 0 && $i != 0)
  617. $col++;
  618. $context['bbc_columns'][$col][] = array(
  619. 'tag' => $tag,
  620. // @todo 'tag_' . ?
  621. 'show_help' => isset($helptxt[$tag]),
  622. );
  623. $i++;
  624. }
  625. // Now put whatever BBC options we may have into context too!
  626. $context['bbc_sections'] = array();
  627. foreach ($bbcChoice as $bbc)
  628. {
  629. $context['bbc_sections'][$bbc] = array(
  630. 'title' => isset($txt['bbc_title_' . $bbc]) ? $txt['bbc_title_' . $bbc] : $txt['bbcTagsToUse_select'],
  631. 'disabled' => empty($modSettings['bbc_disabled_' . $bbc]) ? array() : $modSettings['bbc_disabled_' . $bbc],
  632. 'all_selected' => empty($modSettings['bbc_disabled_' . $bbc]),
  633. );
  634. }
  635. }
  636. call_integration_hook('integrate_prepare_db_settings', array($config_vars));
  637. createToken('admin-dbsc');
  638. }
  639. /**
  640. * Helper function. Saves settings by putting them in Settings.php or saving them in the settings table.
  641. *
  642. * - Saves those settings set from ?action=admin;area=serversettings.
  643. * - Requires the admin_forum permission.
  644. * - Contains arrays of the types of data to save into Settings.php.
  645. *
  646. * @param $config_vars
  647. */
  648. function saveSettings(&$config_vars)
  649. {
  650. global $sc, $cookiename, $modSettings, $user_settings;
  651. global $context;
  652. validateToken('admin-ssc');
  653. // Fix the darn stupid cookiename! (more may not be allowed, but these for sure!)
  654. if (isset($_POST['cookiename']))
  655. $_POST['cookiename'] = preg_replace('~[,;\s\.$]+~u', '', $_POST['cookiename']);
  656. // Fix the forum's URL if necessary.
  657. if (isset($_POST['boardurl']))
  658. {
  659. if (substr($_POST['boardurl'], -10) == '/index.php')
  660. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
  661. elseif (substr($_POST['boardurl'], -1) == '/')
  662. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
  663. if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
  664. $_POST['boardurl'] = 'http://' . $_POST['boardurl'];
  665. }
  666. // Any passwords?
  667. $config_passwords = array(
  668. 'db_passwd',
  669. 'ssi_db_passwd',
  670. );
  671. // All the strings to write.
  672. $config_strs = array(
  673. 'mtitle', 'mmessage',
  674. 'language', 'mbname', 'boardurl',
  675. 'cookiename',
  676. 'webmaster_email',
  677. 'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
  678. 'cache_accelerator', 'cache_memcached',
  679. );
  680. // All the numeric variables.
  681. $config_ints = array(
  682. 'cache_enable',
  683. );
  684. // All the checkboxes.
  685. $config_bools = array(
  686. 'db_persist', 'db_error_send',
  687. 'maintenance',
  688. );
  689. // Now sort everything into a big array, and figure out arrays and etc.
  690. $new_settings = array();
  691. foreach ($config_passwords as $config_var)
  692. {
  693. if (isset($_POST[$config_var][1]) && $_POST[$config_var][0] == $_POST[$config_var][1])
  694. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var][0], '\'\\') . '\'';
  695. }
  696. foreach ($config_strs as $config_var)
  697. {
  698. if (isset($_POST[$config_var]))
  699. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var], '\'\\') . '\'';
  700. }
  701. foreach ($config_ints as $config_var)
  702. {
  703. if (isset($_POST[$config_var]))
  704. $new_settings[$config_var] = (int) $_POST[$config_var];
  705. }
  706. foreach ($config_bools as $key)
  707. {
  708. if (!empty($_POST[$key]))
  709. $new_settings[$key] = '1';
  710. else
  711. $new_settings[$key] = '0';
  712. }
  713. // Save the relevant settings in the Settings.php file.
  714. require_once(SUBSDIR . '/Admin.subs.php');
  715. updateSettingsFile($new_settings);
  716. // Now loop through the remaining (database-based) settings.
  717. $new_settings = array();
  718. foreach ($config_vars as $config_var)
  719. {
  720. // We just saved the file-based settings, so skip their definitions.
  721. if (!is_array($config_var) || $config_var[2] == 'file')
  722. continue;
  723. // Rewrite the definition a bit.
  724. $new_settings[] = array($config_var[3], $config_var[0]);
  725. }
  726. // Save the new database-based settings, if any.
  727. if (!empty($new_settings))
  728. saveDBSettings($new_settings);
  729. }
  730. /**
  731. * Helper function for saving database settings.
  732. *
  733. * @param array $config_vars
  734. */
  735. function saveDBSettings(&$config_vars)
  736. {
  737. global $context;
  738. validateToken('admin-dbsc');
  739. $inlinePermissions = array();
  740. foreach ($config_vars as $var)
  741. {
  742. if (!isset($var[1]) || (!isset($_POST[$var[1]]) && $var[0] != 'check' && $var[0] != 'permissions' && ($var[0] != 'bbc' || !isset($_POST[$var[1] . '_enabledTags']))))
  743. continue;
  744. // Checkboxes!
  745. elseif ($var[0] == 'check')
  746. $setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0';
  747. // Select boxes!
  748. elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2])))
  749. $setArray[$var[1]] = $_POST[$var[1]];
  750. elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != array())
  751. {
  752. // For security purposes we validate this line by line.
  753. $options = array();
  754. foreach ($_POST[$var[1]] as $invar)
  755. if (in_array($invar, array_keys($var[2])))
  756. $options[] = $invar;
  757. $setArray[$var[1]] = serialize($options);
  758. }
  759. // Integers!
  760. elseif ($var[0] == 'int')
  761. $setArray[$var[1]] = (int) $_POST[$var[1]];
  762. // Floating point!
  763. elseif ($var[0] == 'float')
  764. $setArray[$var[1]] = (float) $_POST[$var[1]];
  765. // Text!
  766. elseif ($var[0] == 'text' || $var[0] == 'large_text')
  767. $setArray[$var[1]] = $_POST[$var[1]];
  768. // Passwords!
  769. elseif ($var[0] == 'password')
  770. {
  771. if (isset($_POST[$var[1]][1]) && $_POST[$var[1]][0] == $_POST[$var[1]][1])
  772. $setArray[$var[1]] = $_POST[$var[1]][0];
  773. }
  774. // BBC.
  775. elseif ($var[0] == 'bbc')
  776. {
  777. $bbcTags = array();
  778. foreach (parse_bbc(false) as $tag)
  779. $bbcTags[] = $tag['tag'];
  780. if (!isset($_POST[$var[1] . '_enabledTags']))
  781. $_POST[$var[1] . '_enabledTags'] = array();
  782. elseif (!is_array($_POST[$var[1] . '_enabledTags']))
  783. $_POST[$var[1] . '_enabledTags'] = array($_POST[$var[1] . '_enabledTags']);
  784. $setArray[$var[1]] = implode(',', array_diff($bbcTags, $_POST[$var[1] . '_enabledTags']));
  785. }
  786. // Permissions?
  787. elseif ($var[0] == 'permissions')
  788. $inlinePermissions[] = $var[1];
  789. }
  790. if (!empty($setArray))
  791. updateSettings($setArray);
  792. // If we have inline permissions we need to save them.
  793. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  794. {
  795. require_once(ADMINDIR . '/ManagePermissions.php');
  796. save_inline_permissions($inlinePermissions);
  797. }
  798. }
  799. /**
  800. * Allows us to see the servers php settings
  801. *
  802. * - loads the settings into an array for display in a template
  803. * - drops cookie values just in case
  804. */
  805. function ShowPHPinfoSettings()
  806. {
  807. global $context, $txt;
  808. $info_lines = array();
  809. $category = $txt['phpinfo_settings'];
  810. // get the data
  811. ob_start();
  812. phpinfo();
  813. // We only want it for its body, pigs that we are
  814. $info_lines = preg_replace('~^.*<body>(.*)</body>.*$~', '$1', ob_get_contents());
  815. $info_lines = explode("\n", strip_tags($info_lines, "<tr><td><h2>"));
  816. ob_end_clean();
  817. // remove things that could be considered sensitive
  818. $remove = '_COOKIE|Cookie|_GET|_REQUEST|REQUEST_URI|QUERY_STRING|REQUEST_URL|HTTP_REFERER';
  819. // put all of it into an array
  820. foreach ($info_lines as $line)
  821. {
  822. if (preg_match('~(' . $remove . ')~', $line))
  823. continue;
  824. // new category?
  825. if (strpos($line, '<h2>') !== false)
  826. $category = preg_match('~<h2>(.*)</h2>~', $line, $title) ? $category = $title[1] : $category;
  827. // load it as setting => value or the old setting local master
  828. if (preg_match('~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~', $line, $val))
  829. $pinfo[$category][$val[1]] = $val[2];
  830. elseif (preg_match('~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~', $line, $val))
  831. $pinfo[$category][$val[1]] = array($txt['phpinfo_localsettings'] => $val[2], $txt['phpinfo_defaultsettings'] => $val[3]);
  832. }
  833. // load it in to context and display it
  834. $context['pinfo'] = $pinfo;
  835. $context['page_title'] = $txt['admin_server_settings'];
  836. $context['sub_template'] = 'php_info';
  837. return;
  838. }