ManageServer.php 35 KB

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