2
0

ManageServer.php 35 KB

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