Subs-Admin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /* This file contains functions that are specifically done by administrators.
  15. The most important function in this file for mod makers happens to be the
  16. updateSettingsFile() function, but it shouldn't be used often anyway.
  17. void getServerVersions(array checkFor)
  18. - get a list of versions that are currently installed on the server.
  19. void getFileVersions(array versionOptions)
  20. - get detailed version information about the physical SMF files on the
  21. server.
  22. - the input parameter allows to set whether to include SSI.php and
  23. whether the results should be sorted.
  24. - returns an array containing information on source files, templates
  25. and language files found in the default theme directory (grouped by
  26. language).
  27. void updateSettingsFile(array config_vars)
  28. - updates the Settings.php file with the changes in config_vars.
  29. - expects config_vars to be an associative array, with the keys as the
  30. variable names in Settings.php, and the values the varaible values.
  31. - does not escape or quote values.
  32. - preserves case, formatting, and additional options in file.
  33. - writes nothing if the resulting file would be less than 10 lines
  34. in length (sanity check for read lock.)
  35. void updateAdminPreferences()
  36. - saves the admins current preferences to the database.
  37. void emailAdmins(string $template, array $replacements = array(), additional_recipients = array())
  38. - loads all users who are admins or have the admin forum permission.
  39. - uses the email template and replacements passed in the parameters.
  40. - sends them an email.
  41. */
  42. function getServerVersions($checkFor)
  43. {
  44. global $txt, $db_connection, $_PHPA, $smcFunc, $memcached, $modSettings;
  45. loadLanguage('Admin');
  46. $versions = array();
  47. // Is GD available? If it is, we should show version information for it too.
  48. if (in_array('gd', $checkFor) && function_exists('gd_info'))
  49. {
  50. $temp = gd_info();
  51. $versions['gd'] = array('title' => $txt['support_versions_gd'], 'version' => $temp['GD Version']);
  52. }
  53. // Now lets check for the Database.
  54. if (in_array('db_server', $checkFor))
  55. {
  56. db_extend();
  57. if (!isset($db_connection) || $db_connection === false)
  58. trigger_error('getServerVersions(): you need to be connected to the database in order to get its server version', E_USER_NOTICE);
  59. else
  60. {
  61. $versions['db_server'] = array('title' => sprintf($txt['support_versions_db'], $smcFunc['db_title']), 'version' => '');
  62. $versions['db_server']['version'] = $smcFunc['db_get_version']();
  63. }
  64. }
  65. // If we're using memcache we need the server info.
  66. if (empty($memcached) && function_exists('memcache_get') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '')
  67. get_memcached_server();
  68. // Check to see if we have any accelerators installed...
  69. if (in_array('mmcache', $checkFor) && defined('MMCACHE_VERSION'))
  70. $versions['mmcache'] = array('title' => 'Turck MMCache', 'version' => MMCACHE_VERSION);
  71. if (in_array('eaccelerator', $checkFor) && defined('EACCELERATOR_VERSION'))
  72. $versions['eaccelerator'] = array('title' => 'eAccelerator', 'version' => EACCELERATOR_VERSION);
  73. if (in_array('phpa', $checkFor) && isset($_PHPA))
  74. $versions['phpa'] = array('title' => 'ionCube PHP-Accelerator', 'version' => $_PHPA['VERSION']);
  75. if (in_array('apc', $checkFor) && extension_loaded('apc'))
  76. $versions['apc'] = array('title' => 'Alternative PHP Cache', 'version' => phpversion('apc'));
  77. if (in_array('memcache', $checkFor) && function_exists('memcache_set'))
  78. $versions['memcache'] = array('title' => 'Memcached', 'version' => empty($memcached) ? '???' : memcache_get_version($memcached));
  79. if (in_array('xcache', $checkFor) && function_exists('xcache_set'))
  80. $versions['xcache'] = array('title' => 'XCache', 'version' => XCACHE_VERSION);
  81. if (in_array('php', $checkFor))
  82. $versions['php'] = array('title' => 'PHP', 'version' => PHP_VERSION);
  83. if (in_array('server', $checkFor))
  84. $versions['server'] = array('title' => $txt['support_versions_server'], 'version' => $_SERVER['SERVER_SOFTWARE']);
  85. return $versions;
  86. }
  87. // Search through source, theme and language files to determine their version.
  88. function getFileVersions(&$versionOptions)
  89. {
  90. global $boarddir, $sourcedir, $settings;
  91. // Default place to find the languages would be the default theme dir.
  92. $lang_dir = $settings['default_theme_dir'] . '/languages';
  93. $version_info = array(
  94. 'file_versions' => array(),
  95. 'default_template_versions' => array(),
  96. 'template_versions' => array(),
  97. 'default_language_versions' => array(),
  98. );
  99. // Find the version in SSI.php's file header.
  100. if (!empty($versionOptions['include_ssi']) && file_exists($boarddir . '/SSI.php'))
  101. {
  102. $fp = fopen($boarddir . '/SSI.php', 'rb');
  103. $header = fread($fp, 4096);
  104. fclose($fp);
  105. // The comment looks rougly like... that.
  106. if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
  107. $version_info['file_versions']['SSI.php'] = $match[1];
  108. // Not found! This is bad.
  109. else
  110. $version_info['file_versions']['SSI.php'] = '??';
  111. }
  112. // Do the paid subscriptions handler?
  113. if (!empty($versionOptions['include_subscriptions']) && file_exists($boarddir . '/subscriptions.php'))
  114. {
  115. $fp = fopen($boarddir . '/subscriptions.php', 'rb');
  116. $header = fread($fp, 4096);
  117. fclose($fp);
  118. // Found it?
  119. if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
  120. $version_info['file_versions']['subscriptions.php'] = $match[1];
  121. // If we haven't how do we all get paid?
  122. else
  123. $version_info['file_versions']['subscriptions.php'] = '??';
  124. }
  125. // Load all the files in the Sources directory, except this file and the redirect.
  126. $sources_dir = dir($sourcedir);
  127. while ($entry = $sources_dir->read())
  128. {
  129. if (substr($entry, -4) === '.php' && !is_dir($sourcedir . '/' . $entry) && $entry !== 'index.php')
  130. {
  131. // Read the first 4k from the file.... enough for the header.
  132. $fp = fopen($sourcedir . '/' . $entry, 'rb');
  133. $header = fread($fp, 4096);
  134. fclose($fp);
  135. // Look for the version comment in the file header.
  136. if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
  137. $version_info['file_versions'][$entry] = $match[1];
  138. // It wasn't found, but the file was... show a '??'.
  139. else
  140. $version_info['file_versions'][$entry] = '??';
  141. }
  142. }
  143. $sources_dir->close();
  144. // Load all the files in the default template directory - and the current theme if applicable.
  145. $directories = array('default_template_versions' => $settings['default_theme_dir']);
  146. if ($settings['theme_id'] != 1)
  147. $directories += array('template_versions' => $settings['theme_dir']);
  148. foreach ($directories as $type => $dirname)
  149. {
  150. $this_dir = dir($dirname);
  151. while ($entry = $this_dir->read())
  152. {
  153. if (substr($entry, -12) == 'template.php' && !is_dir($dirname . '/' . $entry))
  154. {
  155. // Read the first 768 bytes from the file.... enough for the header.
  156. $fp = fopen($dirname . '/' . $entry, 'rb');
  157. $header = fread($fp, 768);
  158. fclose($fp);
  159. // Look for the version comment in the file header.
  160. if (preg_match('~\*\s@version\s+(.+)[\s]{2}~i', $header, $match) == 1)
  161. $version_info[$type][$entry] = $match[1];
  162. // It wasn't found, but the file was... show a '??'.
  163. else
  164. $version_info[$type][$entry] = '??';
  165. }
  166. }
  167. $this_dir->close();
  168. }
  169. // Load up all the files in the default language directory and sort by language.
  170. $this_dir = dir($lang_dir);
  171. while ($entry = $this_dir->read())
  172. {
  173. if (substr($entry, -4) == '.php' && $entry != 'index.php' && !is_dir($lang_dir . '/' . $entry))
  174. {
  175. // Read the first 768 bytes from the file.... enough for the header.
  176. $fp = fopen($lang_dir . '/' . $entry, 'rb');
  177. $header = fread($fp, 768);
  178. fclose($fp);
  179. // Split the file name off into useful bits.
  180. list ($name, $language) = explode('.', $entry);
  181. // Look for the version comment in the file header.
  182. if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1)
  183. $version_info['default_language_versions'][$language][$name] = $match[1];
  184. // It wasn't found, but the file was... show a '??'.
  185. else
  186. $version_info['default_language_versions'][$language][$name] = '??';
  187. }
  188. }
  189. $this_dir->close();
  190. // Sort the file versions by filename.
  191. if (!empty($versionOptions['sort_results']))
  192. {
  193. ksort($version_info['file_versions']);
  194. ksort($version_info['default_template_versions']);
  195. ksort($version_info['template_versions']);
  196. ksort($version_info['default_language_versions']);
  197. // For languages sort each language too.
  198. foreach ($version_info['default_language_versions'] as $language => $dummy)
  199. ksort($version_info['default_language_versions'][$language]);
  200. }
  201. return $version_info;
  202. }
  203. // Update the Settings.php file.
  204. function updateSettingsFile($config_vars)
  205. {
  206. global $boarddir, $cachedir;
  207. // When is Settings.php last changed?
  208. $last_settings_change = filemtime($boarddir . '/Settings.php');
  209. // Load the file. Break it up based on \r or \n, and then clean out extra characters.
  210. $settingsArray = trim(file_get_contents($boarddir . '/Settings.php'));
  211. if (strpos($settingsArray, "\n") !== false)
  212. $settingsArray = explode("\n", $settingsArray);
  213. elseif (strpos($settingsArray, "\r") !== false)
  214. $settingsArray = explode("\r", $settingsArray);
  215. else
  216. return;
  217. // Make sure we got a good file.
  218. if (count($config_vars) == 1 && isset($config_vars['db_last_error']))
  219. {
  220. $temp = trim(implode("\n", $settingsArray));
  221. if (substr($temp, 0, 5) != '<?php' || substr($temp, -2) != '?' . '>')
  222. return;
  223. if (strpos($temp, 'sourcedir') === false || strpos($temp, 'boarddir') === false || strpos($temp, 'cookiename') === false)
  224. return;
  225. }
  226. // Presumably, the file has to have stuff in it for this function to be called :P.
  227. if (count($settingsArray) < 10)
  228. return;
  229. foreach ($settingsArray as $k => $dummy)
  230. $settingsArray[$k] = strtr($dummy, array("\r" => '')) . "\n";
  231. for ($i = 0, $n = count($settingsArray); $i < $n; $i++)
  232. {
  233. // Don't trim or bother with it if it's not a variable.
  234. if (substr($settingsArray[$i], 0, 1) != '$')
  235. continue;
  236. $settingsArray[$i] = trim($settingsArray[$i]) . "\n";
  237. // Look through the variables to set....
  238. foreach ($config_vars as $var => $val)
  239. {
  240. if (strncasecmp($settingsArray[$i], '$' . $var, 1 + strlen($var)) == 0)
  241. {
  242. $comment = strstr(substr($settingsArray[$i], strpos($settingsArray[$i], ';')), '#');
  243. $settingsArray[$i] = '$' . $var . ' = ' . $val . ';' . ($comment == '' ? '' : "\t\t" . rtrim($comment)) . "\n";
  244. // This one's been 'used', so to speak.
  245. unset($config_vars[$var]);
  246. }
  247. }
  248. if (substr(trim($settingsArray[$i]), 0, 2) == '?' . '>')
  249. $end = $i;
  250. }
  251. // This should never happen, but apparently it is happening.
  252. if (empty($end) || $end < 10)
  253. $end = count($settingsArray) - 1;
  254. // Still more? Add them at the end.
  255. if (!empty($config_vars))
  256. {
  257. if (trim($settingsArray[$end]) == '?' . '>')
  258. $settingsArray[$end++] = '';
  259. else
  260. $end++;
  261. foreach ($config_vars as $var => $val)
  262. $settingsArray[$end++] = '$' . $var . ' = ' . $val . ';' . "\n";
  263. $settingsArray[$end] = '?' . '>';
  264. }
  265. else
  266. $settingsArray[$end] = trim($settingsArray[$end]);
  267. // Sanity error checking: the file needs to be at least 12 lines.
  268. if (count($settingsArray) < 12)
  269. return;
  270. // Try to avoid a few pitfalls:
  271. // like a possible race condition,
  272. // or a failure to write at low diskspace
  273. // Check before you act: if cache is enabled, we can do a simple test
  274. // Can we even write things on this filesystem?
  275. if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
  276. $cachedir = $boarddir . '/cache';
  277. $test_fp = @fopen($cachedir . '/settings_update.tmp', "w+");
  278. if ($test_fp)
  279. {
  280. fclose($test_fp);
  281. $test_fp = @fopen($cachedir . '/settings_update.tmp', 'r+');
  282. $written_bytes = fwrite($test_fp, "test");
  283. fclose($test_fp);
  284. @unlink($cachedir . '/settings_update.tmp');
  285. if ($written_bytes !== strlen("test"))
  286. {
  287. // Oops. Low disk space, perhaps. Don't mess with Settings.php then.
  288. // No means no. :P
  289. return;
  290. }
  291. }
  292. // Protect me from what I want! :P
  293. clearstatcache();
  294. if (filemtime($boarddir . '/Settings.php') === $last_settings_change)
  295. {
  296. // You asked for it...
  297. // Blank out the file - done to fix a oddity with some servers.
  298. $fp = @fopen($boarddir . '/Settings.php', 'w');
  299. // Is it even writable, though?
  300. if ($fp)
  301. {
  302. fclose($fp);
  303. $fp = fopen($boarddir . '/Settings.php', 'r+');
  304. foreach ($settingsArray as $line)
  305. fwrite($fp, strtr($line, "\r", ''));
  306. fclose($fp);
  307. }
  308. }
  309. }
  310. function updateAdminPreferences()
  311. {
  312. global $options, $context, $smcFunc, $settings, $user_info;
  313. // This must exist!
  314. if (!isset($context['admin_preferences']))
  315. return false;
  316. // This is what we'll be saving.
  317. $options['admin_preferences'] = serialize($context['admin_preferences']);
  318. // Just check we haven't ended up with something theme exclusive somehow.
  319. $smcFunc['db_query']('', '
  320. DELETE FROM {db_prefix}themes
  321. WHERE id_theme != {int:default_theme}
  322. AND variable = {string:admin_preferences}',
  323. array(
  324. 'default_theme' => 1,
  325. 'admin_preferences' => 'admin_preferences',
  326. )
  327. );
  328. // Update the themes table.
  329. $smcFunc['db_insert']('replace',
  330. '{db_prefix}themes',
  331. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  332. array($user_info['id'], 1, 'admin_preferences', $options['admin_preferences']),
  333. array('id_member', 'id_theme', 'variable')
  334. );
  335. // Make sure we invalidate any cache.
  336. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 0);
  337. }
  338. // Send all the administrators a lovely email.
  339. function emailAdmins($template, $replacements = array(), $additional_recipients = array())
  340. {
  341. global $smcFunc, $sourcedir, $language, $modSettings;
  342. // We certainly want this.
  343. require_once($sourcedir . '/Subs-Post.php');
  344. // Load all groups which are effectively admins.
  345. $request = $smcFunc['db_query']('', '
  346. SELECT id_group
  347. FROM {db_prefix}permissions
  348. WHERE permission = {string:admin_forum}
  349. AND add_deny = {int:add_deny}
  350. AND id_group != {int:id_group}',
  351. array(
  352. 'add_deny' => 1,
  353. 'id_group' => 0,
  354. 'admin_forum' => 'admin_forum',
  355. )
  356. );
  357. $groups = array(1);
  358. while ($row = $smcFunc['db_fetch_assoc']($request))
  359. $groups[] = $row['id_group'];
  360. $smcFunc['db_free_result']($request);
  361. $request = $smcFunc['db_query']('', '
  362. SELECT id_member, member_name, real_name, lngfile, email_address
  363. FROM {db_prefix}members
  364. WHERE (id_group IN ({array_int:group_list}) OR FIND_IN_SET({raw:group_array_implode}, additional_groups) != 0)
  365. AND notify_types != {int:notify_types}
  366. ORDER BY lngfile',
  367. array(
  368. 'group_list' => $groups,
  369. 'notify_types' => 4,
  370. 'group_array_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  371. )
  372. );
  373. $emails_sent = array();
  374. while ($row = $smcFunc['db_fetch_assoc']($request))
  375. {
  376. // Stick their particulars in the replacement data.
  377. $replacements['IDMEMBER'] = $row['id_member'];
  378. $replacements['REALNAME'] = $row['member_name'];
  379. $replacements['USERNAME'] = $row['real_name'];
  380. // Load the data from the template.
  381. $emaildata = loadEmailTemplate($template, $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  382. // Then send the actual email.
  383. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 1);
  384. // Track who we emailed so we don't do it twice.
  385. $emails_sent[] = $row['email_address'];
  386. }
  387. $smcFunc['db_free_result']($request);
  388. // Any additional users we must email this to?
  389. if (!empty($additional_recipients))
  390. foreach ($additional_recipients as $recipient)
  391. {
  392. if (in_array($recipient['email'], $emails_sent))
  393. continue;
  394. $replacements['IDMEMBER'] = $recipient['id'];
  395. $replacements['REALNAME'] = $recipient['name'];
  396. $replacements['USERNAME'] = $recipient['name'];
  397. // Load the template again.
  398. $emaildata = loadEmailTemplate($template, $replacements, empty($recipient['lang']) || empty($modSettings['userLanguage']) ? $language : $recipient['lang']);
  399. // Send off the email.
  400. sendmail($recipient['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 1);
  401. }
  402. }
  403. ?>