ManageServer.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. <?php
  2. /**
  3. * This file contains all the functionality required to be able to edit the
  4. * core server settings. This includes anything from which an error may
  5. * result in the forum 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. * ie. array('text', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  21. *
  22. * * A text input box. For numerical values.
  23. * ie. array('int', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  24. *
  25. * * A text input box. For floating point values.
  26. * ie. array('float', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  27. *
  28. * * A large text input box. Used for textual values spanning multiple lines.
  29. * ie. array('large_text', 'nameInModSettingsAndSQL', 'OptionalNumberOfRows'),
  30. *
  31. * * A check box. Either one or zero. (boolean)
  32. * ie. array('check', 'nameInModSettingsAndSQL'),
  33. *
  34. * * A selection box. Used for the selection of something from a list.
  35. * ie. array('select', 'nameInModSettingsAndSQL', array('valueForSQL' => $txt['displayedValue'])),
  36. * Note that just saying array('first', 'second') will put 0 in the SQL for 'first'.
  37. *
  38. * * A password input box. Used for passwords, no less!
  39. * ie. array('password', 'nameInModSettingsAndSQL', 'OptionalInputBoxWidth'),
  40. *
  41. * * A permission - for picking groups who have a permission.
  42. * ie. array('permissions', 'manage_groups'),
  43. *
  44. * * A BBC selection box.
  45. * ie. array('bbc', 'sig_bbc'),
  46. *
  47. * For each option:
  48. * type (see above), variable name, size/possible values.
  49. * OR make type '' for an empty string for a horizontal rule.
  50. * SET preinput - to put some HTML prior to the input box.
  51. * SET postinput - to put some HTML following the input box.
  52. * SET invalid - to mark the data as invalid.
  53. * PLUS you can override label and help parameters by forcing their keys in the array, for example:
  54. * array('text', 'invalidlabel', 3, 'label' => 'Actual Label')
  55. *
  56. * Simple Machines Forum (SMF)
  57. *
  58. * @package SMF
  59. * @author Simple Machines http://www.simplemachines.org
  60. * @copyright 2011 Simple Machines
  61. * @license http://www.simplemachines.org/about/smf/license.php BSD
  62. *
  63. * @version 2.0
  64. */
  65. if (!defined('SMF'))
  66. die('Hacking attempt...');
  67. /**
  68. * This is the main dispatcher. Sets up all the available sub-actions, all the tabs and selects
  69. * the appropriate one based on the sub-action.
  70. * Requires the admin_forum permission.
  71. * Redirects to the appropriate function based on the sub-action.
  72. *
  73. * @uses edit_settings adminIndex.
  74. */
  75. function ModifySettings()
  76. {
  77. global $context, $txt, $scripturl, $boarddir;
  78. // This is just to keep the database password more secure.
  79. isAllowedTo('admin_forum');
  80. // Load up all the tabs...
  81. $context[$context['admin_menu_name']]['tab_data'] = array(
  82. 'title' => $txt['admin_server_settings'],
  83. 'help' => 'serversettings',
  84. 'description' => $txt['admin_basic_settings'],
  85. );
  86. checkSession('request');
  87. // The settings are in here, I swear!
  88. loadLanguage('ManageSettings');
  89. $context['page_title'] = $txt['admin_server_settings'];
  90. $context['sub_template'] = 'show_settings';
  91. $subActions = array(
  92. 'general' => 'ModifyGeneralSettings',
  93. 'database' => 'ModifyDatabaseSettings',
  94. 'cookie' => 'ModifyCookieSettings',
  95. 'cache' => 'ModifyCacheSettings',
  96. 'loads' => 'ModifyLoadBalancingSettings',
  97. );
  98. // By default we're editing the core settings
  99. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'general';
  100. $context['sub_action'] = $_REQUEST['sa'];
  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
  119. * to be changed.
  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. if ($return_config)
  150. return $config_vars;
  151. // Setup the template stuff.
  152. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=general;save';
  153. $context['settings_title'] = $txt['general_settings'];
  154. // Saving settings?
  155. if (isset($_REQUEST['save']))
  156. {
  157. saveSettings($config_vars);
  158. redirectexit('action=admin;area=serversettings;sa=general;' . $context['session_var'] . '=' . $context['session_id']);
  159. }
  160. // Fill the config array.
  161. prepareServerSettingsContext($config_vars);
  162. }
  163. /**
  164. * Basic database and paths settings - database name, host, etc.
  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'], '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. if ($return_config)
  202. return $config_vars;
  203. // Setup the template stuff.
  204. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=database;save';
  205. $context['settings_title'] = $txt['database_paths_settings'];
  206. $context['save_disabled'] = $context['settings_not_writable'];
  207. // Saving settings?
  208. if (isset($_REQUEST['save']))
  209. {
  210. saveSettings($config_vars);
  211. redirectexit('action=admin;area=serversettings;sa=database;' . $context['session_var'] . '=' . $context['session_id']);
  212. }
  213. // Fill the config array.
  214. prepareServerSettingsContext($config_vars);
  215. }
  216. /**
  217. * This function handles cookies settings modifications.
  218. *
  219. * @param $return_config
  220. */
  221. function ModifyCookieSettings($return_config = false)
  222. {
  223. global $context, $scripturl, $txt, $sourcedir, $modSettings, $cookiename, $user_settings;
  224. // Define the variables we want to edit.
  225. $config_vars = array(
  226. // Cookies...
  227. array('cookiename', $txt['cookie_name'], 'file', 'text', 20),
  228. array('cookieTime', $txt['cookieTime'], 'db', 'int'),
  229. array('localCookies', $txt['localCookies'], 'db', 'check', false, 'localCookies'),
  230. array('globalCookies', $txt['globalCookies'], 'db', 'check', false, 'globalCookies'),
  231. array('secureCookies', $txt['secureCookies'], 'db', 'check', false, 'secureCookies', 'disabled' => !isset($_SERVER['HTTPS']) || !(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == '1')),
  232. '',
  233. // Sessions
  234. array('databaseSession_enable', $txt['databaseSession_enable'], 'db', 'check', false, 'databaseSession_enable'),
  235. array('databaseSession_loose', $txt['databaseSession_loose'], 'db', 'check', false, 'databaseSession_loose'),
  236. array('databaseSession_lifetime', $txt['databaseSession_lifetime'], 'db', 'int', false, 'databaseSession_lifetime'),
  237. );
  238. if ($return_config)
  239. return $config_vars;
  240. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cookie;save';
  241. $context['settings_title'] = $txt['cookies_sessions_settings'];
  242. // Saving settings?
  243. if (isset($_REQUEST['save']))
  244. {
  245. saveSettings($config_vars);
  246. // If the cookie name was changed, reset the cookie.
  247. if ($cookiename != $_POST['cookiename'])
  248. {
  249. $original_session_id = $context['session_id'];
  250. include_once($sourcedir . '/Subs-Auth.php');
  251. // Remove the old cookie.
  252. setLoginCookie(-3600, 0);
  253. // Set the new one.
  254. $cookiename = $_POST['cookiename'];
  255. setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], sha1($user_settings['passwd'] . $user_settings['password_salt']));
  256. redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $original_session_id, $context['server']['needs_login_fix']);
  257. }
  258. redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $context['session_id']);
  259. }
  260. // Fill the config array.
  261. prepareServerSettingsContext($config_vars);
  262. }
  263. /**
  264. * Simply modifying cache functions
  265. *
  266. * @param bool $return_config = false
  267. */
  268. function ModifyCacheSettings($return_config = false)
  269. {
  270. global $context, $scripturl, $txt, $helptxt, $modSettings;
  271. // Define the variables we want to edit.
  272. $config_vars = array(
  273. // Only a couple of settings, but they are important
  274. array('select', 'cache_enable', array($txt['cache_off'], $txt['cache_level1'], $txt['cache_level2'], $txt['cache_level3'])),
  275. array('text', 'cache_memcached'),
  276. );
  277. if ($return_config)
  278. return $config_vars;
  279. // Saving again?
  280. if (isset($_GET['save']))
  281. {
  282. saveDBSettings($config_vars);
  283. // We have to manually force the clearing of the cache otherwise the changed settings might not get noticed.
  284. $modSettings['cache_enable'] = 1;
  285. cache_put_data('modSettings', null, 90);
  286. redirectexit('action=admin;area=serversettings;sa=cache;' . $context['session_var'] . '=' . $context['session_id']);
  287. }
  288. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cache;save';
  289. $context['settings_title'] = $txt['caching_settings'];
  290. $context['settings_message'] = $txt['caching_information'];
  291. // Detect an optimizer?
  292. if (function_exists('eaccelerator_put'))
  293. $detected = 'eAccelerator';
  294. elseif (function_exists('mmcache_put'))
  295. $detected = 'MMCache';
  296. elseif (function_exists('apc_store'))
  297. $detected = 'APC';
  298. elseif (function_exists('output_cache_put'))
  299. $detected = 'Zend';
  300. elseif (function_exists('memcache_set'))
  301. $detected = 'Memcached';
  302. elseif (function_exists('xcache_set'))
  303. $detected = 'XCache';
  304. else
  305. $detected = 'no_caching';
  306. $context['settings_message'] = sprintf($context['settings_message'], $txt['detected_' . $detected]);
  307. // Prepare the template.
  308. prepareDBSettingContext($config_vars);
  309. }
  310. /**
  311. * Allows to edit load balancing settings.
  312. *
  313. * @param bool $return_config = false
  314. */
  315. function ModifyLoadBalancingSettings($return_config = false)
  316. {
  317. global $txt, $scripturl, $context, $settings, $modSettings;
  318. // Setup a warning message, but disabled by default.
  319. $disabled = true;
  320. $context['settings_message'] = $txt['loadavg_disabled_conf'];
  321. if (strpos(strtolower(PHP_OS), 'win') === 0)
  322. $context['settings_message'] = $txt['loadavg_disabled_windows'];
  323. else
  324. {
  325. $modSettings['load_average'] = @file_get_contents('/proc/loadavg');
  326. if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) !== 0)
  327. $modSettings['load_average'] = (float) $matches[1];
  328. elseif (($modSettings['load_average'] = @`uptime`) !== null && preg_match('~load averages?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) !== 0)
  329. $modSettings['load_average'] = (float) $matches[1];
  330. else
  331. unset($modSettings['load_average']);
  332. if (!empty($modSettings['load_average']))
  333. {
  334. $context['settings_message'] = sprintf($txt['loadavg_warning'], $modSettings['load_average']);
  335. $disabled = false;
  336. }
  337. }
  338. // Start with a simple checkbox.
  339. $config_vars = array(
  340. array('check', 'loadavg_enable'),
  341. );
  342. // Set the default values for each option.
  343. $default_values = array(
  344. 'loadavg_auto_opt' => '1.0',
  345. 'loadavg_search' => '2.5',
  346. 'loadavg_allunread' => '2.0',
  347. 'loadavg_unreadreplies' => '3.5',
  348. 'loadavg_show_posts' => '2.0',
  349. 'loadavg_forum' => '40.0',
  350. );
  351. // Loop through the settings.
  352. foreach ($default_values as $name => $value)
  353. {
  354. // Use the default value if the setting isn't set yet.
  355. $value = !isset($modSettings[$name]) ? $value : $modSettings[$name];
  356. $config_vars[] = array('text', $name, 'value' => $value, 'disabled' => $disabled);
  357. }
  358. if ($return_config)
  359. return $config_vars;
  360. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=loads;save';
  361. $context['settings_title'] = $txt['load_balancing_settings'];
  362. // Saving?
  363. if (isset($_GET['save']))
  364. {
  365. // Stupidity is not allowed.
  366. foreach ($_POST as $key => $value)
  367. {
  368. if (strpos($key, 'loadavg') === 0 || $key === 'loadavg_enable')
  369. continue;
  370. elseif ($key == 'loadavg_auto_opt' && $value <= 1)
  371. $_POST['loadavg_auto_opt'] = '1.0';
  372. elseif ($key == 'loadavg_forum' && $value < 10)
  373. $_POST['loadavg_forum'] = '10.0';
  374. elseif ($value < 2)
  375. $_POST[$key] = '2.0';
  376. }
  377. saveDBSettings($config_vars);
  378. redirectexit('action=admin;area=serversettings;sa=loads;' . $context['session_var'] . '=' . $context['session_id']);
  379. }
  380. prepareDBSettingContext($config_vars);
  381. }
  382. /**
  383. * Helper function, it sets up the context for the manage server settings.
  384. *
  385. * @param array $config_vars
  386. */
  387. function prepareServerSettingsContext(&$config_vars)
  388. {
  389. global $context, $modSettings;
  390. $context['config_vars'] = array();
  391. foreach ($config_vars as $identifier => $config_var)
  392. {
  393. if (!is_array($config_var) || !isset($config_var[1]))
  394. $context['config_vars'][] = $config_var;
  395. else
  396. {
  397. $varname = $config_var[0];
  398. global $$varname;
  399. $context['config_vars'][] = array(
  400. 'label' => $config_var[1],
  401. 'help' => isset($config_var[5]) ? $config_var[5] : '',
  402. 'type' => $config_var[3],
  403. 'size' => empty($config_var[4]) ? 0 : $config_var[4],
  404. 'data' => isset($config_var[4]) && is_array($config_var[4]) ? $config_var[4] : array(),
  405. 'name' => $config_var[0],
  406. '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 : '')),
  407. 'disabled' => !empty($context['settings_not_writable']) || !empty($config_var['disabled']),
  408. 'invalid' => false,
  409. 'javascript' => '',
  410. 'preinput' => '',
  411. 'postinput' => '',
  412. );
  413. }
  414. }
  415. }
  416. /**
  417. * Helper function, it sets up the context for database settings.
  418. * @todo see rev. 10406 from 2.1-requests
  419. *
  420. * @param array $config_vars
  421. */
  422. function prepareDBSettingContext(&$config_vars)
  423. {
  424. global $txt, $helptxt, $context, $modSettings, $sourcedir;
  425. loadLanguage('Help');
  426. $context['config_vars'] = array();
  427. $inlinePermissions = array();
  428. $bbcChoice = array();
  429. foreach ($config_vars as $config_var)
  430. {
  431. // HR?
  432. if (!is_array($config_var))
  433. $context['config_vars'][] = $config_var;
  434. else
  435. {
  436. // If it has no name it doesn't have any purpose!
  437. if (empty($config_var[1]))
  438. continue;
  439. // Special case for inline permissions
  440. if ($config_var[0] == 'permissions' && allowedTo('manage_permissions'))
  441. $inlinePermissions[] = $config_var[1];
  442. elseif ($config_var[0] == 'permissions')
  443. continue;
  444. // Are we showing the BBC selection box?
  445. if ($config_var[0] == 'bbc')
  446. $bbcChoice[] = $config_var[1];
  447. $context['config_vars'][$config_var[1]] = array(
  448. '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] : '')),
  449. 'help' => isset($helptxt[$config_var[1]]) ? $config_var[1] : '',
  450. 'type' => $config_var[0],
  451. 'size' => !empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], array('int', 'float')) ? 6 : 0),
  452. 'data' => array(),
  453. 'name' => $config_var[1],
  454. '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 : ''),
  455. 'disabled' => false,
  456. 'invalid' => !empty($config_var['invalid']),
  457. 'javascript' => '',
  458. 'var_message' => !empty($config_var['message']) && isset($txt[$config_var['message']]) ? $txt[$config_var['message']] : '',
  459. 'preinput' => isset($config_var['preinput']) ? $config_var['preinput'] : '',
  460. 'postinput' => isset($config_var['postinput']) ? $config_var['postinput'] : '',
  461. );
  462. // If this is a select box handle any data.
  463. if (!empty($config_var[2]) && is_array($config_var[2]))
  464. {
  465. // If we allow multiple selections, we need to adjust a few things.
  466. if ($config_var[0] == 'select' && !empty($config_var['multiple']))
  467. {
  468. $context['config_vars'][$config_var[1]]['name'] .= '[]';
  469. $context['config_vars'][$config_var[1]]['value'] = unserialize($context['config_vars'][$config_var[1]]['value']);
  470. }
  471. // If it's associative
  472. if (isset($config_var[2][0]) && is_array($config_var[2][0]))
  473. $context['config_vars'][$config_var[1]]['data'] = $config_var[2];
  474. else
  475. {
  476. foreach ($config_var[2] as $key => $item)
  477. $context['config_vars'][$config_var[1]]['data'][] = array($key, $item);
  478. }
  479. }
  480. // Finally allow overrides - and some final cleanups.
  481. foreach ($config_var as $k => $v)
  482. {
  483. if (!is_numeric($k))
  484. {
  485. if (substr($k, 0, 2) == 'on')
  486. $context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"';
  487. else
  488. $context['config_vars'][$config_var[1]][$k] = $v;
  489. }
  490. // See if there are any other labels that might fit?
  491. if (isset($txt['setting_' . $config_var[1]]))
  492. $context['config_vars'][$config_var[1]]['label'] = $txt['setting_' . $config_var[1]];
  493. elseif (isset($txt['groups_' . $config_var[1]]))
  494. $context['config_vars'][$config_var[1]]['label'] = $txt['groups_' . $config_var[1]];
  495. }
  496. // Set the subtext in case it's part of the label.
  497. /**
  498. * @todo Temporary. Preventing divs inside label tags.
  499. */
  500. $divPos = strpos($context['config_vars'][$config_var[1]]['label'], '<div');
  501. if ($divPos !== false)
  502. {
  503. $context['config_vars'][$config_var[1]]['subtext'] = preg_replace('~</?div[^>]*>~', '', substr($context['config_vars'][$config_var[1]]['label'], $divPos));
  504. $context['config_vars'][$config_var[1]]['label'] = substr($context['config_vars'][$config_var[1]]['label'], 0, $divPos);
  505. }
  506. }
  507. }
  508. // If we have inline permissions we need to prep them.
  509. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  510. {
  511. require_once($sourcedir . '/ManagePermissions.php');
  512. init_inline_permissions($inlinePermissions, isset($context['permissions_excluded']) ? $context['permissions_excluded'] : array());
  513. }
  514. // What about any BBC selection boxes?
  515. if (!empty($bbcChoice))
  516. {
  517. // What are the options, eh?
  518. $temp = parse_bbc(false);
  519. $bbcTags = array();
  520. foreach ($temp as $tag)
  521. $bbcTags[] = $tag['tag'];
  522. $bbcTags = array_unique($bbcTags);
  523. $totalTags = count($bbcTags);
  524. // The number of columns we want to show the BBC tags in.
  525. $numColumns = isset($context['num_bbc_columns']) ? $context['num_bbc_columns'] : 3;
  526. // Start working out the context stuff.
  527. $context['bbc_columns'] = array();
  528. $tagsPerColumn = ceil($totalTags / $numColumns);
  529. $col = 0; $i = 0;
  530. foreach ($bbcTags as $tag)
  531. {
  532. if ($i % $tagsPerColumn == 0 && $i != 0)
  533. $col++;
  534. $context['bbc_columns'][$col][] = array(
  535. 'tag' => $tag,
  536. /**
  537. * @todo 'tag_' . ?
  538. */
  539. 'show_help' => isset($helptxt[$tag]),
  540. );
  541. $i++;
  542. }
  543. // Now put whatever BBC options we may have into context too!
  544. $context['bbc_sections'] = array();
  545. foreach ($bbcChoice as $bbc)
  546. {
  547. $context['bbc_sections'][$bbc] = array(
  548. 'title' => isset($txt['bbc_title_' . $bbc]) ? $txt['bbc_title_' . $bbc] : $txt['bbcTagsToUse_select'],
  549. 'disabled' => empty($modSettings['bbc_disabled_' . $bbc]) ? array() : $modSettings['bbc_disabled_' . $bbc],
  550. 'all_selected' => empty($modSettings['bbc_disabled_' . $bbc]),
  551. );
  552. }
  553. }
  554. }
  555. /**
  556. * Helper function. Saves settings by putting them in Settings.php or saving them in the settings table.
  557. * Saves those settings set from ?action=admin;area=serversettings.
  558. * Requires the admin_forum permission.
  559. * Contains arrays of the types of data to save into Settings.php.
  560. * @param $config_vars
  561. */
  562. function saveSettings(&$config_vars)
  563. {
  564. global $boarddir, $sc, $cookiename, $modSettings, $user_settings;
  565. global $sourcedir, $context, $cachedir;
  566. // Fix the darn stupid cookiename! (more may not be allowed, but these for sure!)
  567. if (isset($_POST['cookiename']))
  568. $_POST['cookiename'] = preg_replace('~[,;\s\.$]+~' . ($context['utf8'] ? 'u' : ''), '', $_POST['cookiename']);
  569. // Fix the forum's URL if necessary.
  570. if (isset($_POST['boardurl']))
  571. {
  572. if (substr($_POST['boardurl'], -10) == '/index.php')
  573. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
  574. elseif (substr($_POST['boardurl'], -1) == '/')
  575. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
  576. if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
  577. $_POST['boardurl'] = 'http://' . $_POST['boardurl'];
  578. }
  579. // Any passwords?
  580. $config_passwords = array(
  581. 'db_passwd',
  582. 'ssi_db_passwd',
  583. );
  584. // All the strings to write.
  585. $config_strs = array(
  586. 'mtitle', 'mmessage',
  587. 'language', 'mbname', 'boardurl',
  588. 'cookiename',
  589. 'webmaster_email',
  590. 'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
  591. 'boarddir', 'sourcedir', 'cachedir',
  592. );
  593. // All the numeric variables.
  594. $config_ints = array(
  595. );
  596. // All the checkboxes.
  597. $config_bools = array(
  598. 'db_persist', 'db_error_send',
  599. 'maintenance',
  600. );
  601. // Now sort everything into a big array, and figure out arrays and etc.
  602. $new_settings = array();
  603. foreach ($config_passwords as $config_var)
  604. {
  605. if (isset($_POST[$config_var][1]) && $_POST[$config_var][0] == $_POST[$config_var][1])
  606. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var][0], '\'\\') . '\'';
  607. }
  608. foreach ($config_strs as $config_var)
  609. {
  610. if (isset($_POST[$config_var]))
  611. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var], '\'\\') . '\'';
  612. }
  613. foreach ($config_ints as $config_var)
  614. {
  615. if (isset($_POST[$config_var]))
  616. $new_settings[$config_var] = (int) $_POST[$config_var];
  617. }
  618. foreach ($config_bools as $key)
  619. {
  620. if (!empty($_POST[$key]))
  621. $new_settings[$key] = '1';
  622. else
  623. $new_settings[$key] = '0';
  624. }
  625. // Save the relevant settings in the Settings.php file.
  626. require_once($sourcedir . '/Subs-Admin.php');
  627. updateSettingsFile($new_settings);
  628. // Now loopt through the remaining (database-based) settings.
  629. $new_settings = array();
  630. foreach ($config_vars as $config_var)
  631. {
  632. // We just saved the file-based settings, so skip their definitions.
  633. if (!is_array($config_var) || $config_var[2] == 'file')
  634. continue;
  635. // Rewrite the definition a bit.
  636. $new_settings[] = array($config_var[3], $config_var[0]);
  637. }
  638. // Save the new database-based settings, if any.
  639. if (!empty($new_settings))
  640. saveDBSettings($new_settings);
  641. }
  642. /**
  643. * Helper function for saving database settings.
  644. * @todo see rev. 10406 from 2.1-requests
  645. *
  646. * @param array $config_vars
  647. */
  648. function saveDBSettings(&$config_vars)
  649. {
  650. global $sourcedir, $context;
  651. $inlinePermissions = array();
  652. foreach ($config_vars as $var)
  653. {
  654. if (!isset($var[1]) || (!isset($_POST[$var[1]]) && $var[0] != 'check' && $var[0] != 'permissions' && ($var[0] != 'bbc' || !isset($_POST[$var[1] . '_enabledTags']))))
  655. continue;
  656. // Checkboxes!
  657. elseif ($var[0] == 'check')
  658. $setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0';
  659. // Select boxes!
  660. elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2])))
  661. $setArray[$var[1]] = $_POST[$var[1]];
  662. elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != array())
  663. {
  664. // For security purposes we validate this line by line.
  665. $options = array();
  666. foreach ($_POST[$var[1]] as $invar)
  667. if (in_array($invar, array_keys($var[2])))
  668. $options[] = $invar;
  669. $setArray[$var[1]] = serialize($options);
  670. }
  671. // Integers!
  672. elseif ($var[0] == 'int')
  673. $setArray[$var[1]] = (int) $_POST[$var[1]];
  674. // Floating point!
  675. elseif ($var[0] == 'float')
  676. $setArray[$var[1]] = (float) $_POST[$var[1]];
  677. // Text!
  678. elseif ($var[0] == 'text' || $var[0] == 'large_text')
  679. $setArray[$var[1]] = $_POST[$var[1]];
  680. // Passwords!
  681. elseif ($var[0] == 'password')
  682. {
  683. if (isset($_POST[$var[1]][1]) && $_POST[$var[1]][0] == $_POST[$var[1]][1])
  684. $setArray[$var[1]] = $_POST[$var[1]][0];
  685. }
  686. // BBC.
  687. elseif ($var[0] == 'bbc')
  688. {
  689. $bbcTags = array();
  690. foreach (parse_bbc(false) as $tag)
  691. $bbcTags[] = $tag['tag'];
  692. if (!isset($_POST[$var[1] . '_enabledTags']))
  693. $_POST[$var[1] . '_enabledTags'] = array();
  694. elseif (!is_array($_POST[$var[1] . '_enabledTags']))
  695. $_POST[$var[1] . '_enabledTags'] = array($_POST[$var[1] . '_enabledTags']);
  696. $setArray[$var[1]] = implode(',', array_diff($bbcTags, $_POST[$var[1] . '_enabledTags']));
  697. }
  698. // Permissions?
  699. elseif ($var[0] == 'permissions')
  700. $inlinePermissions[] = $var[1];
  701. }
  702. if (!empty($setArray))
  703. updateSettings($setArray);
  704. // If we have inline permissions we need to save them.
  705. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  706. {
  707. require_once($sourcedir . '/ManagePermissions.php');
  708. save_inline_permissions($inlinePermissions);
  709. }
  710. }
  711. ?>