ManageServer.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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'], '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'], 'db', 'check', false, 'localCookies'),
  230. array('globalCookies', $txt['globalCookies'], 'db', 'check', false, 'globalCookies'),
  231. array('globalCookiesDomain', $txt['globalCookiesDomain'], 'db', 'text', false, 'globalCookiesDomain'),
  232. array('secureCookies', $txt['secureCookies'], 'db', 'check', false, 'secureCookies', 'disabled' => !isset($_SERVER['HTTPS']) || !(strtolower($_SERVER['HTTPS']) == 'on' || strtolower($_SERVER['HTTPS']) == '1')),
  233. array('httponlyCookies', $txt['httponlyCookies'], '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' => $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. if (isset($config_var[4][0]) && is_array($config_var[4][0]))
  493. $context['config_vars'][$config_var[0]]['data'] = $config_var[4];
  494. else
  495. {
  496. foreach ($config_var[4] as $key => $item)
  497. $context['config_vars'][$config_var[0]]['data'][] = array($key, $item);
  498. }
  499. }
  500. }
  501. }
  502. // Two tokens because saving these settings requires both saveSettings and saveDBSettings
  503. createToken('admin-ssc');
  504. createToken('admin-dbsc');
  505. }
  506. /**
  507. * Helper function, it sets up the context for database settings.
  508. * @todo see rev. 10406 from 2.1-requests
  509. *
  510. * @param array $config_vars
  511. */
  512. function prepareDBSettingContext(&$config_vars)
  513. {
  514. global $txt, $helptxt, $context, $modSettings, $sourcedir;
  515. loadLanguage('Help');
  516. $context['config_vars'] = array();
  517. $inlinePermissions = array();
  518. $bbcChoice = array();
  519. foreach ($config_vars as $config_var)
  520. {
  521. // HR?
  522. if (!is_array($config_var))
  523. $context['config_vars'][] = $config_var;
  524. else
  525. {
  526. // If it has no name it doesn't have any purpose!
  527. if (empty($config_var[1]))
  528. continue;
  529. // Special case for inline permissions
  530. if ($config_var[0] == 'permissions' && allowedTo('manage_permissions'))
  531. $inlinePermissions[] = $config_var[1];
  532. elseif ($config_var[0] == 'permissions')
  533. continue;
  534. // Are we showing the BBC selection box?
  535. if ($config_var[0] == 'bbc')
  536. $bbcChoice[] = $config_var[1];
  537. $context['config_vars'][$config_var[1]] = array(
  538. '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] : '')),
  539. 'help' => isset($helptxt[$config_var[1]]) ? $config_var[1] : '',
  540. 'type' => $config_var[0],
  541. 'size' => !empty($config_var[2]) && !is_array($config_var[2]) ? $config_var[2] : (in_array($config_var[0], array('int', 'float')) ? 6 : 0),
  542. 'data' => array(),
  543. 'name' => $config_var[1],
  544. '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 : ''),
  545. 'disabled' => false,
  546. 'invalid' => !empty($config_var['invalid']),
  547. 'javascript' => '',
  548. 'var_message' => !empty($config_var['message']) && isset($txt[$config_var['message']]) ? $txt[$config_var['message']] : '',
  549. 'preinput' => isset($config_var['preinput']) ? $config_var['preinput'] : '',
  550. 'postinput' => isset($config_var['postinput']) ? $config_var['postinput'] : '',
  551. );
  552. // If this is a select box handle any data.
  553. if (!empty($config_var[2]) && is_array($config_var[2]))
  554. {
  555. // If we allow multiple selections, we need to adjust a few things.
  556. if ($config_var[0] == 'select' && !empty($config_var['multiple']))
  557. {
  558. $context['config_vars'][$config_var[1]]['name'] .= '[]';
  559. $context['config_vars'][$config_var[1]]['value'] = unserialize($context['config_vars'][$config_var[1]]['value']);
  560. }
  561. // If it's associative
  562. if (isset($config_var[2][0]) && is_array($config_var[2][0]))
  563. $context['config_vars'][$config_var[1]]['data'] = $config_var[2];
  564. else
  565. {
  566. foreach ($config_var[2] as $key => $item)
  567. $context['config_vars'][$config_var[1]]['data'][] = array($key, $item);
  568. }
  569. }
  570. // Finally allow overrides - and some final cleanups.
  571. foreach ($config_var as $k => $v)
  572. {
  573. if (!is_numeric($k))
  574. {
  575. if (substr($k, 0, 2) == 'on')
  576. $context['config_vars'][$config_var[1]]['javascript'] .= ' ' . $k . '="' . $v . '"';
  577. else
  578. $context['config_vars'][$config_var[1]][$k] = $v;
  579. }
  580. // See if there are any other labels that might fit?
  581. if (isset($txt['setting_' . $config_var[1]]))
  582. $context['config_vars'][$config_var[1]]['label'] = $txt['setting_' . $config_var[1]];
  583. elseif (isset($txt['groups_' . $config_var[1]]))
  584. $context['config_vars'][$config_var[1]]['label'] = $txt['groups_' . $config_var[1]];
  585. }
  586. // Set the subtext in case it's part of the label.
  587. // @todo Temporary. Preventing divs inside label tags.
  588. $divPos = strpos($context['config_vars'][$config_var[1]]['label'], '<div');
  589. if ($divPos !== false)
  590. {
  591. $context['config_vars'][$config_var[1]]['subtext'] = preg_replace('~</?div[^>]*>~', '', substr($context['config_vars'][$config_var[1]]['label'], $divPos));
  592. $context['config_vars'][$config_var[1]]['label'] = substr($context['config_vars'][$config_var[1]]['label'], 0, $divPos);
  593. }
  594. }
  595. }
  596. // If we have inline permissions we need to prep them.
  597. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  598. {
  599. require_once($sourcedir . '/ManagePermissions.php');
  600. init_inline_permissions($inlinePermissions, isset($context['permissions_excluded']) ? $context['permissions_excluded'] : array());
  601. }
  602. // What about any BBC selection boxes?
  603. if (!empty($bbcChoice))
  604. {
  605. // What are the options, eh?
  606. $temp = parse_bbc(false);
  607. $bbcTags = array();
  608. foreach ($temp as $tag)
  609. $bbcTags[] = $tag['tag'];
  610. $bbcTags = array_unique($bbcTags);
  611. $totalTags = count($bbcTags);
  612. // The number of columns we want to show the BBC tags in.
  613. $numColumns = isset($context['num_bbc_columns']) ? $context['num_bbc_columns'] : 3;
  614. // Start working out the context stuff.
  615. $context['bbc_columns'] = array();
  616. $tagsPerColumn = ceil($totalTags / $numColumns);
  617. $col = 0; $i = 0;
  618. foreach ($bbcTags as $tag)
  619. {
  620. if ($i % $tagsPerColumn == 0 && $i != 0)
  621. $col++;
  622. $context['bbc_columns'][$col][] = array(
  623. 'tag' => $tag,
  624. // @todo 'tag_' . ?
  625. 'show_help' => isset($helptxt[$tag]),
  626. );
  627. $i++;
  628. }
  629. // Now put whatever BBC options we may have into context too!
  630. $context['bbc_sections'] = array();
  631. foreach ($bbcChoice as $bbc)
  632. {
  633. $context['bbc_sections'][$bbc] = array(
  634. 'title' => isset($txt['bbc_title_' . $bbc]) ? $txt['bbc_title_' . $bbc] : $txt['bbcTagsToUse_select'],
  635. 'disabled' => empty($modSettings['bbc_disabled_' . $bbc]) ? array() : $modSettings['bbc_disabled_' . $bbc],
  636. 'all_selected' => empty($modSettings['bbc_disabled_' . $bbc]),
  637. );
  638. }
  639. }
  640. call_integration_hook('integrate_prepare_db_settings', array(&$config_vars));
  641. createToken('admin-dbsc');
  642. }
  643. /**
  644. * Helper function. Saves settings by putting them in Settings.php or saving them in the settings table.
  645. *
  646. * - Saves those settings set from ?action=admin;area=serversettings.
  647. * - Requires the admin_forum permission.
  648. * - Contains arrays of the types of data to save into Settings.php.
  649. *
  650. * @param $config_vars
  651. */
  652. function saveSettings(&$config_vars)
  653. {
  654. global $boarddir, $sc, $cookiename, $modSettings, $user_settings;
  655. global $sourcedir, $context, $cachedir;
  656. validateToken('admin-ssc');
  657. // Fix the darn stupid cookiename! (more may not be allowed, but these for sure!)
  658. if (isset($_POST['cookiename']))
  659. $_POST['cookiename'] = preg_replace('~[,;\s\.$]+~' . ($context['utf8'] ? 'u' : ''), '', $_POST['cookiename']);
  660. // Fix the forum's URL if necessary.
  661. if (isset($_POST['boardurl']))
  662. {
  663. if (substr($_POST['boardurl'], -10) == '/index.php')
  664. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -10);
  665. elseif (substr($_POST['boardurl'], -1) == '/')
  666. $_POST['boardurl'] = substr($_POST['boardurl'], 0, -1);
  667. if (substr($_POST['boardurl'], 0, 7) != 'http://' && substr($_POST['boardurl'], 0, 7) != 'file://' && substr($_POST['boardurl'], 0, 8) != 'https://')
  668. $_POST['boardurl'] = 'http://' . $_POST['boardurl'];
  669. }
  670. // Any passwords?
  671. $config_passwords = array(
  672. 'db_passwd',
  673. 'ssi_db_passwd',
  674. );
  675. // All the strings to write.
  676. $config_strs = array(
  677. 'mtitle', 'mmessage',
  678. 'language', 'mbname', 'boardurl',
  679. 'cookiename',
  680. 'webmaster_email',
  681. 'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
  682. 'boarddir', 'sourcedir',
  683. 'cachedir', 'cache_accelerator', 'cache_memcached',
  684. );
  685. // All the numeric variables.
  686. $config_ints = array(
  687. 'cache_enable',
  688. );
  689. // All the checkboxes.
  690. $config_bools = array(
  691. 'db_persist', 'db_error_send',
  692. 'maintenance',
  693. );
  694. // Now sort everything into a big array, and figure out arrays and etc.
  695. $new_settings = array();
  696. foreach ($config_passwords as $config_var)
  697. {
  698. if (isset($_POST[$config_var][1]) && $_POST[$config_var][0] == $_POST[$config_var][1])
  699. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var][0], '\'\\') . '\'';
  700. }
  701. foreach ($config_strs as $config_var)
  702. {
  703. if (isset($_POST[$config_var]))
  704. $new_settings[$config_var] = '\'' . addcslashes($_POST[$config_var], '\'\\') . '\'';
  705. }
  706. foreach ($config_ints as $config_var)
  707. {
  708. if (isset($_POST[$config_var]))
  709. $new_settings[$config_var] = (int) $_POST[$config_var];
  710. }
  711. foreach ($config_bools as $key)
  712. {
  713. if (!empty($_POST[$key]))
  714. $new_settings[$key] = '1';
  715. else
  716. $new_settings[$key] = '0';
  717. }
  718. // Save the relevant settings in the Settings.php file.
  719. require_once($sourcedir . '/Subs-Admin.php');
  720. updateSettingsFile($new_settings);
  721. // Now loop through the remaining (database-based) settings.
  722. $new_settings = array();
  723. foreach ($config_vars as $config_var)
  724. {
  725. // We just saved the file-based settings, so skip their definitions.
  726. if (!is_array($config_var) || $config_var[2] == 'file')
  727. continue;
  728. // Rewrite the definition a bit.
  729. $new_settings[] = array($config_var[3], $config_var[0]);
  730. }
  731. // Save the new database-based settings, if any.
  732. if (!empty($new_settings))
  733. saveDBSettings($new_settings);
  734. }
  735. /**
  736. * Helper function for saving database settings.
  737. * @todo see rev. 10406 from 2.1-requests
  738. *
  739. * @param array $config_vars
  740. */
  741. function saveDBSettings(&$config_vars)
  742. {
  743. global $sourcedir, $context;
  744. validateToken('admin-dbsc');
  745. $inlinePermissions = array();
  746. foreach ($config_vars as $var)
  747. {
  748. if (!isset($var[1]) || (!isset($_POST[$var[1]]) && $var[0] != 'check' && $var[0] != 'permissions' && ($var[0] != 'bbc' || !isset($_POST[$var[1] . '_enabledTags']))))
  749. continue;
  750. // Checkboxes!
  751. elseif ($var[0] == 'check')
  752. $setArray[$var[1]] = !empty($_POST[$var[1]]) ? '1' : '0';
  753. // Select boxes!
  754. elseif ($var[0] == 'select' && in_array($_POST[$var[1]], array_keys($var[2])))
  755. $setArray[$var[1]] = $_POST[$var[1]];
  756. elseif ($var[0] == 'select' && !empty($var['multiple']) && array_intersect($_POST[$var[1]], array_keys($var[2])) != array())
  757. {
  758. // For security purposes we validate this line by line.
  759. $options = array();
  760. foreach ($_POST[$var[1]] as $invar)
  761. if (in_array($invar, array_keys($var[2])))
  762. $options[] = $invar;
  763. $setArray[$var[1]] = serialize($options);
  764. }
  765. // Integers!
  766. elseif ($var[0] == 'int')
  767. $setArray[$var[1]] = (int) $_POST[$var[1]];
  768. // Floating point!
  769. elseif ($var[0] == 'float')
  770. $setArray[$var[1]] = (float) $_POST[$var[1]];
  771. // Text!
  772. elseif ($var[0] == 'text' || $var[0] == 'large_text')
  773. $setArray[$var[1]] = $_POST[$var[1]];
  774. // Passwords!
  775. elseif ($var[0] == 'password')
  776. {
  777. if (isset($_POST[$var[1]][1]) && $_POST[$var[1]][0] == $_POST[$var[1]][1])
  778. $setArray[$var[1]] = $_POST[$var[1]][0];
  779. }
  780. // BBC.
  781. elseif ($var[0] == 'bbc')
  782. {
  783. $bbcTags = array();
  784. foreach (parse_bbc(false) as $tag)
  785. $bbcTags[] = $tag['tag'];
  786. if (!isset($_POST[$var[1] . '_enabledTags']))
  787. $_POST[$var[1] . '_enabledTags'] = array();
  788. elseif (!is_array($_POST[$var[1] . '_enabledTags']))
  789. $_POST[$var[1] . '_enabledTags'] = array($_POST[$var[1] . '_enabledTags']);
  790. $setArray[$var[1]] = implode(',', array_diff($bbcTags, $_POST[$var[1] . '_enabledTags']));
  791. }
  792. // Permissions?
  793. elseif ($var[0] == 'permissions')
  794. $inlinePermissions[] = $var[1];
  795. }
  796. if (!empty($setArray))
  797. updateSettings($setArray);
  798. // If we have inline permissions we need to save them.
  799. if (!empty($inlinePermissions) && allowedTo('manage_permissions'))
  800. {
  801. require_once($sourcedir . '/ManagePermissions.php');
  802. save_inline_permissions($inlinePermissions);
  803. }
  804. }
  805. /**
  806. * Allows us to see the servers php settings
  807. *
  808. * - loads the settings into an array for display in a template
  809. * - drops cookie values just in case
  810. */
  811. function ShowPHPinfoSettings()
  812. {
  813. global $context, $txt;
  814. $info_lines = array();
  815. $category = $txt['phpinfo_settings'];
  816. // get the data
  817. ob_start();
  818. phpinfo();
  819. // We only want it for its body, pigs that we are
  820. $info_lines = preg_replace('~^.*<body>(.*)</body>.*$~', '$1', ob_get_contents());
  821. $info_lines = explode("\n", strip_tags($info_lines, "<tr><td><h2>"));
  822. ob_end_clean();
  823. // remove things that could be considered sensative
  824. $remove = '_COOKIE|Cookie|_GET|_REQUEST|REQUEST_URI|QUERY_STRING|REQUEST_URL|HTTP_REFERER';
  825. // put all of it into an array
  826. foreach ($info_lines as $line)
  827. {
  828. if (preg_match('~(' . $remove . ')~', $line))
  829. continue;
  830. // new category?
  831. if (strpos($line, '<h2>') !== false)
  832. $category = preg_match('~<h2>(.*)</h2>~', $line, $title) ? $category = $title[1] : $category;
  833. // load it as setting => value or the old setting local master
  834. if (preg_match('~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~', $line, $val))
  835. $pinfo[$category][$val[1]] = $val[2];
  836. elseif (preg_match('~<tr><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td><td[^>]+>([^<]*)</td></tr>~', $line, $val))
  837. $pinfo[$category][$val[1]] = array($txt['phpinfo_localsettings'] => $val[2], $txt['phpinfo_defaultsettings'] => $val[3]);
  838. }
  839. // load it in to context and display it
  840. $context['pinfo'] = $pinfo;
  841. $context['page_title'] = $txt['admin_server_settings'];
  842. $context['sub_template'] = 'php_info';
  843. return;
  844. }
  845. ?>