ManageServer.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. createToken('admin-ssc');
  416. createToken('admin-dbsc');
  417. }
  418. /**
  419. * Helper function, it sets up the context for database settings.
  420. * @todo see rev. 10406 from 2.1-requests
  421. *
  422. * @param array $config_vars
  423. */
  424. function prepareDBSettingContext(&$config_vars)
  425. {
  426. global $txt, $helptxt, $context, $modSettings, $sourcedir;
  427. loadLanguage('Help');
  428. $context['config_vars'] = array();
  429. $inlinePermissions = array();
  430. $bbcChoice = array();
  431. foreach ($config_vars as $config_var)
  432. {
  433. // HR?
  434. if (!is_array($config_var))
  435. $context['config_vars'][] = $config_var;
  436. else
  437. {
  438. // If it has no name it doesn't have any purpose!
  439. if (empty($config_var[1]))
  440. continue;
  441. // Special case for inline permissions
  442. if ($config_var[0] == 'permissions' && allowedTo('manage_permissions'))
  443. $inlinePermissions[] = $config_var[1];
  444. elseif ($config_var[0] == 'permissions')
  445. continue;
  446. // Are we showing the BBC selection box?
  447. if ($config_var[0] == 'bbc')
  448. $bbcChoice[] = $config_var[1];
  449. $context['config_vars'][$config_var[1]] = array(
  450. '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] : '')),
  451. 'help' => isset($helptxt[$config_var[1]]) ? $config_var[1] : '',
  452. 'type' => $config_var[0],
  453. 'size' => !empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], array('int', 'float')) ? 6 : 0),
  454. 'data' => array(),
  455. 'name' => $config_var[1],
  456. '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 : ''),
  457. 'disabled' => false,
  458. 'invalid' => !empty($config_var['invalid']),
  459. 'javascript' => '',
  460. 'var_message' => !empty($config_var['message']) && isset($txt[$config_var['message']]) ? $txt[$config_var['message']] : '',
  461. 'preinput' => isset($config_var['preinput']) ? $config_var['preinput'] : '',
  462. 'postinput' => isset($config_var['postinput']) ? $config_var['postinput'] : '',
  463. );
  464. // If this is a select box handle any data.
  465. if (!empty($config_var[2]) && is_array($config_var[2]))
  466. {
  467. // If we allow multiple selections, we need to adjust a few things.
  468. if ($config_var[0] == 'select' && !empty($config_var['multiple']))
  469. {
  470. $context['config_vars'][$config_var[1]]['name'] .= '[]';
  471. $context['config_vars'][$config_var[1]]['value'] = unserialize($context['config_vars'][$config_var[1]]['value']);
  472. }
  473. // If it's associative
  474. if (isset($config_var[2][0]) && is_array($config_var[2][0]))
  475. $context['config_vars'][$config_var[1]]['data'] = $config_var[2];
  476. else
  477. {
  478. foreach ($config_var[2] as $key => $item)
  479. $context['config_vars'][$config_var[1]]['data'][] = array($key, $item);
  480. }
  481. }
  482. // Finally allow overrides - and some final cleanups.
  483. foreach ($config_var as $k => $v)
  484. {
  485. if (!is_numeric($k))
  486. {
  487. if (substr($k, 0, 2) == 'on')
  488. $context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"';
  489. else
  490. $context['config_vars'][$config_var[1]][$k] = $v;
  491. }
  492. // See if there are any other labels that might fit?
  493. if (isset($txt['setting_' . $config_var[1]]))
  494. $context['config_vars'][$config_var[1]]['label'] = $txt['setting_' . $config_var[1]];
  495. elseif (isset($txt['groups_' . $config_var[1]]))
  496. $context['config_vars'][$config_var[1]]['label'] = $txt['groups_' . $config_var[1]];
  497. }
  498. // Set the subtext in case it's part of the label.
  499. /**
  500. * @todo Temporary. Preventing divs inside label tags.
  501. */
  502. $divPos = strpos($context['config_vars'][$config_var[1]]['label'], '<div');
  503. if ($divPos !== false)
  504. {
  505. $context['config_vars'][$config_var[1]]['subtext'] = preg_replace('~</?div[^>]*>~', '', substr($context['config_vars'][$config_var[1]]['label'], $divPos));
  506. $context['config_vars'][$config_var[1]]['label'] = substr($context['config_vars'][$config_var[1]]['label'], 0, $divPos);
  507. }
  508. }
  509. }
  510. // If we have inline permissions we need to prep them.
  511. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  512. {
  513. require_once($sourcedir . '/ManagePermissions.php');
  514. init_inline_permissions($inlinePermissions, isset($context['permissions_excluded']) ? $context['permissions_excluded'] : array());
  515. }
  516. // What about any BBC selection boxes?
  517. if (!empty($bbcChoice))
  518. {
  519. // What are the options, eh?
  520. $temp = parse_bbc(false);
  521. $bbcTags = array();
  522. foreach ($temp as $tag)
  523. $bbcTags[] = $tag['tag'];
  524. $bbcTags = array_unique($bbcTags);
  525. $totalTags = count($bbcTags);
  526. // The number of columns we want to show the BBC tags in.
  527. $numColumns = isset($context['num_bbc_columns']) ? $context['num_bbc_columns'] : 3;
  528. // Start working out the context stuff.
  529. $context['bbc_columns'] = array();
  530. $tagsPerColumn = ceil($totalTags / $numColumns);
  531. $col = 0; $i = 0;
  532. foreach ($bbcTags as $tag)
  533. {
  534. if ($i % $tagsPerColumn == 0 && $i != 0)
  535. $col++;
  536. $context['bbc_columns'][$col][] = array(
  537. 'tag' => $tag,
  538. /**
  539. * @todo 'tag_' . ?
  540. */
  541. 'show_help' => isset($helptxt[$tag]),
  542. );
  543. $i++;
  544. }
  545. // Now put whatever BBC options we may have into context too!
  546. $context['bbc_sections'] = array();
  547. foreach ($bbcChoice as $bbc)
  548. {
  549. $context['bbc_sections'][$bbc] = array(
  550. 'title' => isset($txt['bbc_title_' . $bbc]) ? $txt['bbc_title_' . $bbc] : $txt['bbcTagsToUse_select'],
  551. 'disabled' => empty($modSettings['bbc_disabled_' . $bbc]) ? array() : $modSettings['bbc_disabled_' . $bbc],
  552. 'all_selected' => empty($modSettings['bbc_disabled_' . $bbc]),
  553. );
  554. }
  555. }
  556. createToken('admin-dbsc');
  557. }
  558. /**
  559. * Helper function. Saves settings by putting them in Settings.php or saving them in the settings table.
  560. * Saves those settings set from ?action=admin;area=serversettings.
  561. * Requires the admin_forum permission.
  562. * Contains arrays of the types of data to save into Settings.php.
  563. * @param $config_vars
  564. */
  565. function saveSettings(&$config_vars)
  566. {
  567. global $boarddir, $sc, $cookiename, $modSettings, $user_settings;
  568. global $sourcedir, $context, $cachedir;
  569. validateToken('admin-ssc');
  570. // Fix the darn stupid cookiename! (more may not be allowed, but these for sure!)
  571. if (isset($_POST['cookiename']))
  572. $_POST['cookiename'] = preg_replace('~[,;\s\.$]+~' . ($context['utf8'] ? 'u' : ''), '', $_POST['cookiename']);
  573. // Fix the forum's URL if necessary.
  574. if (isset($_POST['boardurl']))
  575. {
  576. if (substr($_POST['boardurl'], -10) == '/index.php')
  577. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
  578. elseif (substr($_POST['boardurl'], -1) == '/')
  579. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
  580. if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
  581. $_POST['boardurl'] = 'http://' . $_POST['boardurl'];
  582. }
  583. // Any passwords?
  584. $config_passwords = array(
  585. 'db_passwd',
  586. 'ssi_db_passwd',
  587. );
  588. // All the strings to write.
  589. $config_strs = array(
  590. 'mtitle', 'mmessage',
  591. 'language', 'mbname', 'boardurl',
  592. 'cookiename',
  593. 'webmaster_email',
  594. 'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
  595. 'boarddir', 'sourcedir', 'cachedir',
  596. );
  597. // All the numeric variables.
  598. $config_ints = array(
  599. );
  600. // All the checkboxes.
  601. $config_bools = array(
  602. 'db_persist', 'db_error_send',
  603. 'maintenance',
  604. );
  605. // Now sort everything into a big array, and figure out arrays and etc.
  606. $new_settings = array();
  607. foreach ($config_passwords as $config_var)
  608. {
  609. if (isset($_POST[$config_var][1]) && $_POST[$config_var][0] == $_POST[$config_var][1])
  610. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var][0], '\'\\') . '\'';
  611. }
  612. foreach ($config_strs as $config_var)
  613. {
  614. if (isset($_POST[$config_var]))
  615. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var], '\'\\') . '\'';
  616. }
  617. foreach ($config_ints as $config_var)
  618. {
  619. if (isset($_POST[$config_var]))
  620. $new_settings[$config_var] = (int) $_POST[$config_var];
  621. }
  622. foreach ($config_bools as $key)
  623. {
  624. if (!empty($_POST[$key]))
  625. $new_settings[$key] = '1';
  626. else
  627. $new_settings[$key] = '0';
  628. }
  629. // Save the relevant settings in the Settings.php file.
  630. require_once($sourcedir . '/Subs-Admin.php');
  631. updateSettingsFile($new_settings);
  632. // Now loopt through the remaining (database-based) settings.
  633. $new_settings = array();
  634. foreach ($config_vars as $config_var)
  635. {
  636. // We just saved the file-based settings, so skip their definitions.
  637. if (!is_array($config_var) || $config_var[2] == 'file')
  638. continue;
  639. // Rewrite the definition a bit.
  640. $new_settings[] = array($config_var[3], $config_var[0]);
  641. }
  642. // Save the new database-based settings, if any.
  643. if (!empty($new_settings))
  644. saveDBSettings($new_settings);
  645. }
  646. /**
  647. * Helper function for saving database settings.
  648. * @todo see rev. 10406 from 2.1-requests
  649. *
  650. * @param array $config_vars
  651. */
  652. function saveDBSettings(&$config_vars)
  653. {
  654. global $sourcedir, $context;
  655. validateToken('admin-dbsc');
  656. $inlinePermissions = array();
  657. foreach ($config_vars as $var)
  658. {
  659. if (!isset($var[1]) || (!isset($_POST[$var[1]]) && $var[0] != 'check' && $var[0] != 'permissions' && ($var[0] != 'bbc' || !isset($_POST[$var[1] . '_enabledTags']))))
  660. continue;
  661. // Checkboxes!
  662. elseif ($var[0] == 'check')
  663. $setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0';
  664. // Select boxes!
  665. elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2])))
  666. $setArray[$var[1]] = $_POST[$var[1]];
  667. elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != array())
  668. {
  669. // For security purposes we validate this line by line.
  670. $options = array();
  671. foreach ($_POST[$var[1]] as $invar)
  672. if (in_array($invar, array_keys($var[2])))
  673. $options[] = $invar;
  674. $setArray[$var[1]] = serialize($options);
  675. }
  676. // Integers!
  677. elseif ($var[0] == 'int')
  678. $setArray[$var[1]] = (int) $_POST[$var[1]];
  679. // Floating point!
  680. elseif ($var[0] == 'float')
  681. $setArray[$var[1]] = (float) $_POST[$var[1]];
  682. // Text!
  683. elseif ($var[0] == 'text' || $var[0] == 'large_text')
  684. $setArray[$var[1]] = $_POST[$var[1]];
  685. // Passwords!
  686. elseif ($var[0] == 'password')
  687. {
  688. if (isset($_POST[$var[1]][1]) && $_POST[$var[1]][0] == $_POST[$var[1]][1])
  689. $setArray[$var[1]] = $_POST[$var[1]][0];
  690. }
  691. // BBC.
  692. elseif ($var[0] == 'bbc')
  693. {
  694. $bbcTags = array();
  695. foreach (parse_bbc(false) as $tag)
  696. $bbcTags[] = $tag['tag'];
  697. if (!isset($_POST[$var[1] . '_enabledTags']))
  698. $_POST[$var[1] . '_enabledTags'] = array();
  699. elseif (!is_array($_POST[$var[1] . '_enabledTags']))
  700. $_POST[$var[1] . '_enabledTags'] = array($_POST[$var[1] . '_enabledTags']);
  701. $setArray[$var[1]] = implode(',', array_diff($bbcTags, $_POST[$var[1] . '_enabledTags']));
  702. }
  703. // Permissions?
  704. elseif ($var[0] == 'permissions')
  705. $inlinePermissions[] = $var[1];
  706. }
  707. if (!empty($setArray))
  708. updateSettings($setArray);
  709. // If we have inline permissions we need to save them.
  710. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  711. {
  712. require_once($sourcedir . '/ManagePermissions.php');
  713. save_inline_permissions($inlinePermissions);
  714. }
  715. }
  716. ?>