ManageServer.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. <?php
  2. /**
  3. * 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.1 Alpha 1
  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. call_integration_hook('integrate_server_settings', array(&$subActions));
  99. // By default we're editing the core settings
  100. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'general';
  101. $context['sub_action'] = $_REQUEST['sa'];
  102. // Warn the user if there's any relevant information regarding Settings.php.
  103. if ($_REQUEST['sa'] != 'cache')
  104. {
  105. // Warn the user if the backup of Settings.php failed.
  106. $settings_not_writable = !is_writable($boarddir . '/Settings.php');
  107. $settings_backup_fail = !@is_writable($boarddir . '/Settings_bak.php') || !@copy($boarddir . '/Settings.php', $boarddir . '/Settings_bak.php');
  108. if ($settings_not_writable)
  109. $context['settings_message'] = '<div class="centertext"><strong>' . $txt['settings_not_writable'] . '</strong></div><br />';
  110. elseif ($settings_backup_fail)
  111. $context['settings_message'] = '<div class="centertext"><strong>' . $txt['admin_backup_fail'] . '</strong></div><br />';
  112. $context['settings_not_writable'] = $settings_not_writable;
  113. }
  114. // Call the right function for this sub-action.
  115. $subActions[$_REQUEST['sa']]();
  116. }
  117. /**
  118. * General forum settings - forum name, maintenance mode, etc.
  119. * Practically, this shows an interface for the settings in Settings.php
  120. * to be changed.
  121. * It uses the rawdata sub template (not theme-able.)
  122. * Requires the admin_forum permission.
  123. * Uses the edit_settings administration area.
  124. * Contains the actual array of settings to show from Settings.php.
  125. * Accessed from ?action=admin;area=serversettings;sa=general.
  126. *
  127. * @param $return_config
  128. */
  129. function ModifyGeneralSettings($return_config = false)
  130. {
  131. global $scripturl, $context, $txt;
  132. /* If you're writing a mod, it's a bad idea to add things here....
  133. For each option:
  134. variable name, description, type (constant), size/possible values, helptext.
  135. OR an empty string for a horizontal rule.
  136. OR a string for a titled section. */
  137. $config_vars = array(
  138. array('mbname', $txt['admin_title'], 'file', 'text', 30),
  139. '',
  140. array('maintenance', $txt['admin_maintain'], 'file', 'check'),
  141. array('mtitle', $txt['maintenance_subject'], 'file', 'text', 36),
  142. array('mmessage', $txt['maintenance_message'], 'file', 'text', 36),
  143. '',
  144. array('webmaster_email', $txt['admin_webmaster_email'], 'file', 'text', 30),
  145. '',
  146. array('enableCompressedOutput', $txt['enableCompressedOutput'], 'db', 'check', null, 'enableCompressedOutput'),
  147. array('disableTemplateEval', $txt['disableTemplateEval'], 'db', 'check', null, 'disableTemplateEval'),
  148. array('disableHostnameLookup', $txt['disableHostnameLookup'], 'db', 'check', null, 'disableHostnameLookup'),
  149. );
  150. call_integration_hook('integrate_general_settings', array(&$config_vars));
  151. if ($return_config)
  152. return $config_vars;
  153. // Setup the template stuff.
  154. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=general;save';
  155. $context['settings_title'] = $txt['general_settings'];
  156. // Saving settings?
  157. if (isset($_REQUEST['save']))
  158. {
  159. call_integration_hook('integrate_save_general_settings');
  160. saveSettings($config_vars);
  161. redirectexit('action=admin;area=serversettings;sa=general;' . $context['session_var'] . '=' . $context['session_id']);
  162. }
  163. // Fill the config array.
  164. prepareServerSettingsContext($config_vars);
  165. }
  166. /**
  167. * Basic database and paths settings - database name, host, etc.
  168. * It shows an interface for the settings in Settings.php to be changed.
  169. * It contains the actual array of settings to show from Settings.php.
  170. * It uses the rawdata sub template (not theme-able.)
  171. * Requires the admin_forum permission.
  172. * Uses the edit_settings administration area.
  173. * Accessed from ?action=admin;area=serversettings;sa=database.
  174. *
  175. * @param $return_config
  176. */
  177. function ModifyDatabaseSettings($return_config = false)
  178. {
  179. global $scripturl, $context, $settings, $txt, $boarddir;
  180. /* If you're writing a mod, it's a bad idea to add things here....
  181. For each option:
  182. variable name, description, type (constant), size/possible values, helptext.
  183. OR an empty string for a horizontal rule.
  184. OR a string for a titled section. */
  185. $config_vars = array(
  186. array('db_server', $txt['database_server'], 'file', 'text'),
  187. array('db_user', $txt['database_user'], 'file', 'text'),
  188. array('db_passwd', $txt['database_password'], 'file', 'password'),
  189. array('db_name', $txt['database_name'], 'file', 'text'),
  190. array('db_prefix', $txt['database_prefix'], 'file', 'text'),
  191. array('db_persist', $txt['db_persist'], 'file', 'check', null, 'db_persist'),
  192. array('db_error_send', $txt['db_error_send'], 'file', 'check'),
  193. array('ssi_db_user', $txt['ssi_db_user'], 'file', 'text', null, 'ssi_db_user'),
  194. array('ssi_db_passwd', $txt['ssi_db_passwd'], 'file', 'password'),
  195. '',
  196. array('autoFixDatabase', $txt['autoFixDatabase'], 'db', 'check', false, 'autoFixDatabase'),
  197. array('autoOptMaxOnline', $txt['autoOptMaxOnline'], 'db', 'int'),
  198. '',
  199. array('boardurl', $txt['admin_url'], 'file', 'text', 36),
  200. array('boarddir', $txt['boarddir'], 'file', 'text', 36),
  201. array('sourcedir', $txt['sourcesdir'], 'file', 'text', 36),
  202. array('cachedir', $txt['cachedir'], 'file', 'text', 36),
  203. );
  204. call_integration_hook('integrate_database_settings', array(&$config_vars));
  205. if ($return_config)
  206. return $config_vars;
  207. // Setup the template stuff.
  208. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=database;save';
  209. $context['settings_title'] = $txt['database_paths_settings'];
  210. $context['save_disabled'] = $context['settings_not_writable'];
  211. // Saving settings?
  212. if (isset($_REQUEST['save']))
  213. {
  214. call_integration_hook('integrate_save_database_settings');
  215. saveSettings($config_vars);
  216. redirectexit('action=admin;area=serversettings;sa=database;' . $context['session_var'] . '=' . $context['session_id']);
  217. }
  218. // Fill the config array.
  219. prepareServerSettingsContext($config_vars);
  220. }
  221. /**
  222. * This function handles cookies settings modifications.
  223. *
  224. * @param bool $return_config = false
  225. */
  226. function ModifyCookieSettings($return_config = false)
  227. {
  228. global $context, $scripturl, $txt, $sourcedir, $modSettings, $cookiename, $user_settings;
  229. // Define the variables we want to edit.
  230. $config_vars = array(
  231. // Cookies...
  232. array('cookiename', $txt['cookie_name'], 'file', 'text', 20),
  233. array('cookieTime', $txt['cookieTime'], 'db', 'int'),
  234. array('localCookies', $txt['localCookies'], 'db', 'check', false, 'localCookies'),
  235. array('globalCookies', $txt['globalCookies'], 'db', 'check', false, 'globalCookies'),
  236. array('secureCookies', $txt['secureCookies'], 'db', 'check', false, 'secureCookies', 'disabled' => !isset($_SERVER['HTTPS']) || !(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == '1')),
  237. array('httponlyCookies', $txt['httponlyCookies'], 'db', 'check', false, 'httponlyCookies'),
  238. '',
  239. // Sessions
  240. array('databaseSession_enable', $txt['databaseSession_enable'], 'db', 'check', false, 'databaseSession_enable'),
  241. array('databaseSession_loose', $txt['databaseSession_loose'], 'db', 'check', false, 'databaseSession_loose'),
  242. array('databaseSession_lifetime', $txt['databaseSession_lifetime'], 'db', 'int', false, 'databaseSession_lifetime'),
  243. );
  244. call_integration_hook('integrate_cookie_settings', array(&$config_vars));
  245. if ($return_config)
  246. return $config_vars;
  247. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cookie;save';
  248. $context['settings_title'] = $txt['cookies_sessions_settings'];
  249. // Saving settings?
  250. if (isset($_REQUEST['save']))
  251. {
  252. call_integration_hook('integrate_save_cookie_settings');
  253. saveSettings($config_vars);
  254. // If the cookie name was changed, reset the cookie.
  255. if ($cookiename != $_POST['cookiename'])
  256. {
  257. $original_session_id = $context['session_id'];
  258. include_once($sourcedir . '/Subs-Auth.php');
  259. // Remove the old cookie.
  260. setLoginCookie(-3600, 0);
  261. // Set the new one.
  262. $cookiename = $_POST['cookiename'];
  263. setLoginCookie(60 * $modSettings['cookieTime'], $user_settings['id_member'], sha1($user_settings['passwd'] . $user_settings['password_salt']));
  264. redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $original_session_id, $context['server']['needs_login_fix']);
  265. }
  266. redirectexit('action=admin;area=serversettings;sa=cookie;' . $context['session_var'] . '=' . $context['session_id']);
  267. }
  268. // Fill the config array.
  269. prepareServerSettingsContext($config_vars);
  270. }
  271. /**
  272. * Simply modifying cache functions
  273. *
  274. * @param bool $return_config = false
  275. */
  276. function ModifyCacheSettings($return_config = false)
  277. {
  278. global $context, $scripturl, $txt, $helptxt, $modSettings;
  279. // Define the variables we want to edit.
  280. $config_vars = array(
  281. // Only a couple of settings, but they are important
  282. array('select', 'cache_enable', array($txt['cache_off'], $txt['cache_level1'], $txt['cache_level2'], $txt['cache_level3'])),
  283. array('text', 'cache_memcached'),
  284. );
  285. call_integration_hook('integrate_modify_cache_settings', array(&$config_vars));
  286. if ($return_config)
  287. return $config_vars;
  288. // Saving again?
  289. if (isset($_GET['save']))
  290. {
  291. call_integration_hook('integrate_save_cache_settings');
  292. saveDBSettings($config_vars);
  293. // We have to manually force the clearing of the cache otherwise the changed settings might not get noticed.
  294. $modSettings['cache_enable'] = 1;
  295. cache_put_data('modSettings', null, 90);
  296. redirectexit('action=admin;area=serversettings;sa=cache;' . $context['session_var'] . '=' . $context['session_id']);
  297. }
  298. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=cache;save';
  299. $context['settings_title'] = $txt['caching_settings'];
  300. $context['settings_message'] = $txt['caching_information'];
  301. // Detect an optimizer?
  302. if (function_exists('eaccelerator_put'))
  303. $detected = 'eAccelerator';
  304. elseif (function_exists('mmcache_put'))
  305. $detected = 'MMCache';
  306. elseif (function_exists('apc_store'))
  307. $detected = 'APC';
  308. elseif (function_exists('output_cache_put'))
  309. $detected = 'Zend';
  310. elseif (function_exists('memcache_set'))
  311. $detected = 'Memcached';
  312. elseif (function_exists('xcache_set'))
  313. $detected = 'XCache';
  314. else
  315. $detected = 'no_caching';
  316. $context['settings_message'] = sprintf($context['settings_message'], $txt['detected_' . $detected]);
  317. // Prepare the template.
  318. prepareDBSettingContext($config_vars);
  319. }
  320. /**
  321. * Allows to edit load balancing settings.
  322. *
  323. * @param bool $return_config = false
  324. */
  325. function ModifyLoadBalancingSettings($return_config = false)
  326. {
  327. global $txt, $scripturl, $context, $settings, $modSettings;
  328. // Setup a warning message, but disabled by default.
  329. $disabled = true;
  330. $context['settings_message'] = $txt['loadavg_disabled_conf'];
  331. if (strpos(strtolower(PHP_OS), 'win') === 0)
  332. $context['settings_message'] = $txt['loadavg_disabled_windows'];
  333. else
  334. {
  335. $modSettings['load_average'] = @file_get_contents('/proc/loadavg');
  336. if (!empty($modSettings['load_average']) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $modSettings['load_average'], $matches) !== 0)
  337. $modSettings['load_average'] = (float) $matches[1];
  338. elseif (($modSettings['load_average'] = @`uptime`) !== null && preg_match('~load averages?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $modSettings['load_average'], $matches) !== 0)
  339. $modSettings['load_average'] = (float) $matches[1];
  340. else
  341. unset($modSettings['load_average']);
  342. if (!empty($modSettings['load_average']))
  343. {
  344. $context['settings_message'] = sprintf($txt['loadavg_warning'], $modSettings['load_average']);
  345. $disabled = false;
  346. }
  347. }
  348. // Start with a simple checkbox.
  349. $config_vars = array(
  350. array('check', 'loadavg_enable'),
  351. );
  352. // Set the default values for each option.
  353. $default_values = array(
  354. 'loadavg_auto_opt' => '1.0',
  355. 'loadavg_search' => '2.5',
  356. 'loadavg_allunread' => '2.0',
  357. 'loadavg_unreadreplies' => '3.5',
  358. 'loadavg_show_posts' => '2.0',
  359. 'loadavg_bbc' => '30.0',
  360. 'loadavg_forum' => '40.0',
  361. );
  362. // Loop through the settings.
  363. foreach ($default_values as $name => $value)
  364. {
  365. // Use the default value if the setting isn't set yet.
  366. $value = !isset($modSettings[$name]) ? $value : $modSettings[$name];
  367. $config_vars[] = array('text', $name, 'value' => $value, 'disabled' => $disabled);
  368. }
  369. call_integration_hook('integrate_loadavg_settings', array(&$config_vars));
  370. if ($return_config)
  371. return $config_vars;
  372. $context['post_url'] = $scripturl . '?action=admin;area=serversettings;sa=loads;save';
  373. $context['settings_title'] = $txt['load_balancing_settings'];
  374. // Saving?
  375. if (isset($_GET['save']))
  376. {
  377. // Stupidity is not allowed.
  378. foreach ($_POST as $key => $value)
  379. {
  380. if (strpos($key, 'loadavg') === 0 || $key === 'loadavg_enable')
  381. continue;
  382. elseif ($key == 'loadavg_auto_opt' && $value <= 1)
  383. $_POST['loadavg_auto_opt'] = '1.0';
  384. elseif ($key == 'loadavg_forum' && $value < 10)
  385. $_POST['loadavg_forum'] = '10.0';
  386. elseif ($value < 2)
  387. $_POST[$key] = '2.0';
  388. }
  389. call_integration_hook('integrate_save_loadavg_settings');
  390. saveDBSettings($config_vars);
  391. redirectexit('action=admin;area=serversettings;sa=loads;' . $context['session_var'] . '=' . $context['session_id']);
  392. }
  393. prepareDBSettingContext($config_vars);
  394. }
  395. /**
  396. * Helper function, it sets up the context for the manage server settings.
  397. *
  398. * @param array $config_vars
  399. */
  400. function prepareServerSettingsContext(&$config_vars)
  401. {
  402. global $context, $modSettings;
  403. $context['config_vars'] = array();
  404. foreach ($config_vars as $identifier => $config_var)
  405. {
  406. if (!is_array($config_var) || !isset($config_var[1]))
  407. $context['config_vars'][] = $config_var;
  408. else
  409. {
  410. $varname = $config_var[0];
  411. global $$varname;
  412. $context['config_vars'][] = array(
  413. 'label' => $config_var[1],
  414. 'help' => isset($config_var[5]) ? $config_var[5] : '',
  415. 'type' => $config_var[3],
  416. 'size' => empty($config_var[4]) ? 0 : $config_var[4],
  417. 'data' => isset($config_var[4]) && is_array($config_var[4]) ? $config_var[4] : array(),
  418. 'name' => $config_var[0],
  419. '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 : '')),
  420. 'disabled' => !empty($context['settings_not_writable']) || !empty($config_var['disabled']),
  421. 'invalid' => false,
  422. 'javascript' => '',
  423. 'preinput' => '',
  424. 'postinput' => '',
  425. );
  426. }
  427. }
  428. createToken('admin-ssc');
  429. createToken('admin-dbsc');
  430. }
  431. /**
  432. * Helper function, it sets up the context for database settings.
  433. * @todo see rev. 10406 from 2.1-requests
  434. *
  435. * @param array $config_vars
  436. */
  437. function prepareDBSettingContext(&$config_vars)
  438. {
  439. global $txt, $helptxt, $context, $modSettings, $sourcedir;
  440. loadLanguage('Help');
  441. $context['config_vars'] = array();
  442. $inlinePermissions = array();
  443. $bbcChoice = array();
  444. foreach ($config_vars as $config_var)
  445. {
  446. // HR?
  447. if (!is_array($config_var))
  448. $context['config_vars'][] = $config_var;
  449. else
  450. {
  451. // If it has no name it doesn't have any purpose!
  452. if (empty($config_var[1]))
  453. continue;
  454. // Special case for inline permissions
  455. if ($config_var[0] == 'permissions' && allowedTo('manage_permissions'))
  456. $inlinePermissions[] = $config_var[1];
  457. elseif ($config_var[0] == 'permissions')
  458. continue;
  459. // Are we showing the BBC selection box?
  460. if ($config_var[0] == 'bbc')
  461. $bbcChoice[] = $config_var[1];
  462. $context['config_vars'][$config_var[1]] = array(
  463. '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] : '')),
  464. 'help' => isset($helptxt[$config_var[1]]) ? $config_var[1] : '',
  465. 'type' => $config_var[0],
  466. 'size' => !empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], array('int', 'float')) ? 6 : 0),
  467. 'data' => array(),
  468. 'name' => $config_var[1],
  469. '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 : ''),
  470. 'disabled' => false,
  471. 'invalid' => !empty($config_var['invalid']),
  472. 'javascript' => '',
  473. 'var_message' => !empty($config_var['message']) && isset($txt[$config_var['message']]) ? $txt[$config_var['message']] : '',
  474. 'preinput' => isset($config_var['preinput']) ? $config_var['preinput'] : '',
  475. 'postinput' => isset($config_var['postinput']) ? $config_var['postinput'] : '',
  476. );
  477. // If this is a select box handle any data.
  478. if (!empty($config_var[2]) && is_array($config_var[2]))
  479. {
  480. // If we allow multiple selections, we need to adjust a few things.
  481. if ($config_var[0] == 'select' && !empty($config_var['multiple']))
  482. {
  483. $context['config_vars'][$config_var[1]]['name'] .= '[]';
  484. $context['config_vars'][$config_var[1]]['value'] = unserialize($context['config_vars'][$config_var[1]]['value']);
  485. }
  486. // If it's associative
  487. if (isset($config_var[2][0]) && is_array($config_var[2][0]))
  488. $context['config_vars'][$config_var[1]]['data'] = $config_var[2];
  489. else
  490. {
  491. foreach ($config_var[2] as $key => $item)
  492. $context['config_vars'][$config_var[1]]['data'][] = array($key, $item);
  493. }
  494. }
  495. // Finally allow overrides - and some final cleanups.
  496. foreach ($config_var as $k => $v)
  497. {
  498. if (!is_numeric($k))
  499. {
  500. if (substr($k, 0, 2) == 'on')
  501. $context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"';
  502. else
  503. $context['config_vars'][$config_var[1]][$k] = $v;
  504. }
  505. // See if there are any other labels that might fit?
  506. if (isset($txt['setting_' . $config_var[1]]))
  507. $context['config_vars'][$config_var[1]]['label'] = $txt['setting_' . $config_var[1]];
  508. elseif (isset($txt['groups_' . $config_var[1]]))
  509. $context['config_vars'][$config_var[1]]['label'] = $txt['groups_' . $config_var[1]];
  510. }
  511. // Set the subtext in case it's part of the label.
  512. // @todo Temporary. Preventing divs inside label tags.
  513. $divPos = strpos($context['config_vars'][$config_var[1]]['label'], '<div');
  514. if ($divPos !== false)
  515. {
  516. $context['config_vars'][$config_var[1]]['subtext'] = preg_replace('~</?div[^>]*>~', '', substr($context['config_vars'][$config_var[1]]['label'], $divPos));
  517. $context['config_vars'][$config_var[1]]['label'] = substr($context['config_vars'][$config_var[1]]['label'], 0, $divPos);
  518. }
  519. }
  520. }
  521. // If we have inline permissions we need to prep them.
  522. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  523. {
  524. require_once($sourcedir . '/ManagePermissions.php');
  525. init_inline_permissions($inlinePermissions, isset($context['permissions_excluded']) ? $context['permissions_excluded'] : array());
  526. }
  527. // What about any BBC selection boxes?
  528. if (!empty($bbcChoice))
  529. {
  530. // What are the options, eh?
  531. $temp = parse_bbc(false);
  532. $bbcTags = array();
  533. foreach ($temp as $tag)
  534. $bbcTags[] = $tag['tag'];
  535. $bbcTags = array_unique($bbcTags);
  536. $totalTags = count($bbcTags);
  537. // The number of columns we want to show the BBC tags in.
  538. $numColumns = isset($context['num_bbc_columns']) ? $context['num_bbc_columns'] : 3;
  539. // Start working out the context stuff.
  540. $context['bbc_columns'] = array();
  541. $tagsPerColumn = ceil($totalTags / $numColumns);
  542. $col = 0; $i = 0;
  543. foreach ($bbcTags as $tag)
  544. {
  545. if ($i % $tagsPerColumn == 0 && $i != 0)
  546. $col++;
  547. $context['bbc_columns'][$col][] = array(
  548. 'tag' => $tag,
  549. // @todo 'tag_' . ?
  550. 'show_help' => isset($helptxt[$tag]),
  551. );
  552. $i++;
  553. }
  554. // Now put whatever BBC options we may have into context too!
  555. $context['bbc_sections'] = array();
  556. foreach ($bbcChoice as $bbc)
  557. {
  558. $context['bbc_sections'][$bbc] = array(
  559. 'title' => isset($txt['bbc_title_' . $bbc]) ? $txt['bbc_title_' . $bbc] : $txt['bbcTagsToUse_select'],
  560. 'disabled' => empty($modSettings['bbc_disabled_' . $bbc]) ? array() : $modSettings['bbc_disabled_' . $bbc],
  561. 'all_selected' => empty($modSettings['bbc_disabled_' . $bbc]),
  562. );
  563. }
  564. }
  565. call_integration_hook('integrate_prepare_server_settings', array(&$config_vars));
  566. createToken('admin-dbsc');
  567. }
  568. /**
  569. * Helper function. Saves settings by putting them in Settings.php or saving them in the settings table.
  570. * Saves those settings set from ?action=admin;area=serversettings.
  571. * Requires the admin_forum permission.
  572. * Contains arrays of the types of data to save into Settings.php.
  573. * @param $config_vars
  574. */
  575. function saveSettings(&$config_vars)
  576. {
  577. global $boarddir, $sc, $cookiename, $modSettings, $user_settings;
  578. global $sourcedir, $context, $cachedir;
  579. validateToken('admin-ssc');
  580. // Fix the darn stupid cookiename! (more may not be allowed, but these for sure!)
  581. if (isset($_POST['cookiename']))
  582. $_POST['cookiename'] = preg_replace('~[,;\s\.$]+~' . ($context['utf8'] ? 'u' : ''), '', $_POST['cookiename']);
  583. // Fix the forum's URL if necessary.
  584. if (isset($_POST['boardurl']))
  585. {
  586. if (substr($_POST['boardurl'], -10) == '/index.php')
  587. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
  588. elseif (substr($_POST['boardurl'], -1) == '/')
  589. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
  590. if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
  591. $_POST['boardurl'] = 'http://' . $_POST['boardurl'];
  592. }
  593. // Any passwords?
  594. $config_passwords = array(
  595. 'db_passwd',
  596. 'ssi_db_passwd',
  597. );
  598. // All the strings to write.
  599. $config_strs = array(
  600. 'mtitle', 'mmessage',
  601. 'language', 'mbname', 'boardurl',
  602. 'cookiename',
  603. 'webmaster_email',
  604. 'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
  605. 'boarddir', 'sourcedir', 'cachedir',
  606. );
  607. // All the numeric variables.
  608. $config_ints = array(
  609. );
  610. // All the checkboxes.
  611. $config_bools = array(
  612. 'db_persist', 'db_error_send',
  613. 'maintenance',
  614. );
  615. // Now sort everything into a big array, and figure out arrays and etc.
  616. $new_settings = array();
  617. foreach ($config_passwords as $config_var)
  618. {
  619. if (isset($_POST[$config_var][1]) && $_POST[$config_var][0] == $_POST[$config_var][1])
  620. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var][0], '\'\\') . '\'';
  621. }
  622. foreach ($config_strs as $config_var)
  623. {
  624. if (isset($_POST[$config_var]))
  625. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var], '\'\\') . '\'';
  626. }
  627. foreach ($config_ints as $config_var)
  628. {
  629. if (isset($_POST[$config_var]))
  630. $new_settings[$config_var] = (int) $_POST[$config_var];
  631. }
  632. foreach ($config_bools as $key)
  633. {
  634. if (!empty($_POST[$key]))
  635. $new_settings[$key] = '1';
  636. else
  637. $new_settings[$key] = '0';
  638. }
  639. // Save the relevant settings in the Settings.php file.
  640. require_once($sourcedir . '/Subs-Admin.php');
  641. updateSettingsFile($new_settings);
  642. // Now loopt through the remaining (database-based) settings.
  643. $new_settings = array();
  644. foreach ($config_vars as $config_var)
  645. {
  646. // We just saved the file-based settings, so skip their definitions.
  647. if (!is_array($config_var) || $config_var[2] == 'file')
  648. continue;
  649. // Rewrite the definition a bit.
  650. $new_settings[] = array($config_var[3], $config_var[0]);
  651. }
  652. // Save the new database-based settings, if any.
  653. if (!empty($new_settings))
  654. saveDBSettings($new_settings);
  655. }
  656. /**
  657. * Helper function for saving database settings.
  658. * @todo see rev. 10406 from 2.1-requests
  659. *
  660. * @param array $config_vars
  661. */
  662. function saveDBSettings(&$config_vars)
  663. {
  664. global $sourcedir, $context;
  665. validateToken('admin-dbsc');
  666. $inlinePermissions = array();
  667. foreach ($config_vars as $var)
  668. {
  669. if (!isset($var[1]) || (!isset($_POST[$var[1]]) && $var[0] != 'check' && $var[0] != 'permissions' && ($var[0] != 'bbc' || !isset($_POST[$var[1] . '_enabledTags']))))
  670. continue;
  671. // Checkboxes!
  672. elseif ($var[0] == 'check')
  673. $setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0';
  674. // Select boxes!
  675. elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2])))
  676. $setArray[$var[1]] = $_POST[$var[1]];
  677. elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != array())
  678. {
  679. // For security purposes we validate this line by line.
  680. $options = array();
  681. foreach ($_POST[$var[1]] as $invar)
  682. if (in_array($invar, array_keys($var[2])))
  683. $options[] = $invar;
  684. $setArray[$var[1]] = serialize($options);
  685. }
  686. // Integers!
  687. elseif ($var[0] == 'int')
  688. $setArray[$var[1]] = (int) $_POST[$var[1]];
  689. // Floating point!
  690. elseif ($var[0] == 'float')
  691. $setArray[$var[1]] = (float) $_POST[$var[1]];
  692. // Text!
  693. elseif ($var[0] == 'text' || $var[0] == 'large_text')
  694. $setArray[$var[1]] = $_POST[$var[1]];
  695. // Passwords!
  696. elseif ($var[0] == 'password')
  697. {
  698. if (isset($_POST[$var[1]][1]) && $_POST[$var[1]][0] == $_POST[$var[1]][1])
  699. $setArray[$var[1]] = $_POST[$var[1]][0];
  700. }
  701. // BBC.
  702. elseif ($var[0] == 'bbc')
  703. {
  704. $bbcTags = array();
  705. foreach (parse_bbc(false) as $tag)
  706. $bbcTags[] = $tag['tag'];
  707. if (!isset($_POST[$var[1] . '_enabledTags']))
  708. $_POST[$var[1] . '_enabledTags'] = array();
  709. elseif (!is_array($_POST[$var[1] . '_enabledTags']))
  710. $_POST[$var[1] . '_enabledTags'] = array($_POST[$var[1] . '_enabledTags']);
  711. $setArray[$var[1]] = implode(',', array_diff($bbcTags, $_POST[$var[1] . '_enabledTags']));
  712. }
  713. // Permissions?
  714. elseif ($var[0] == 'permissions')
  715. $inlinePermissions[] = $var[1];
  716. }
  717. if (!empty($setArray))
  718. updateSettings($setArray);
  719. // If we have inline permissions we need to save them.
  720. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  721. {
  722. require_once($sourcedir . '/ManagePermissions.php');
  723. save_inline_permissions($inlinePermissions);
  724. }
  725. }
  726. ?>