ManageServer.php 32 KB

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