PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/sources/subs/Admin.subs.php

https://github.com/Arantor/Elkarte
PHP | 536 lines | 325 code | 70 blank | 141 comment | 94 complexity | b0853bb8f88bd671f59ac18efb3f385f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * This file contains functions that are specifically done by administrators.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Get a list of versions that are currently installed on the server.
  22. * @param array $checkFor
  23. */
  24. function getServerVersions($checkFor)
  25. {
  26. global $txt, $db_connection, $_PHPA, $smcFunc, $memcached, $modSettings;
  27. loadLanguage('Admin');
  28. $versions = array();
  29. // Is GD available? If it is, we should show version information for it too.
  30. if (in_array('gd', $checkFor) && function_exists('gd_info'))
  31. {
  32. $temp = gd_info();
  33. $versions['gd'] = array('title' => $txt['support_versions_gd'], 'version' => $temp['GD Version']);
  34. }
  35. // Why not have a look at ImageMagick? If it is, we should show version information for it too.
  36. if (in_array('imagick', $checkFor) && class_exists('Imagick'))
  37. {
  38. $temp = New Imagick;
  39. $temp2 = $temp->getVersion();
  40. $versions['imagick'] = array('title' => $txt['support_versions_imagick'], 'version' => $temp2['versionString']);
  41. }
  42. // Now lets check for the Database.
  43. if (in_array('db_server', $checkFor))
  44. {
  45. db_extend();
  46. if (!isset($db_connection) || $db_connection === false)
  47. trigger_error('getServerVersions(): you need to be connected to the database in order to get its server version', E_USER_NOTICE);
  48. else
  49. {
  50. $versions['db_server'] = array('title' => sprintf($txt['support_versions_db'], $smcFunc['db_title']), 'version' => '');
  51. $versions['db_server']['version'] = $smcFunc['db_get_version']();
  52. }
  53. }
  54. // If we're using memcache we need the server info.
  55. if (empty($memcached) && function_exists('memcache_get') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '')
  56. get_memcached_server();
  57. // Check to see if we have any accelerators installed...
  58. if (in_array('mmcache', $checkFor) && defined('MMCACHE_VERSION'))
  59. $versions['mmcache'] = array('title' => 'Turck MMCache', 'version' => MMCACHE_VERSION);
  60. if (in_array('eaccelerator', $checkFor) && defined('EACCELERATOR_VERSION'))
  61. $versions['eaccelerator'] = array('title' => 'eAccelerator', 'version' => EACCELERATOR_VERSION);
  62. if (in_array('phpa', $checkFor) && isset($_PHPA))
  63. $versions['phpa'] = array('title' => 'ionCube PHP-Accelerator', 'version' => $_PHPA['VERSION']);
  64. if (in_array('apc', $checkFor) && extension_loaded('apc'))
  65. $versions['apc'] = array('title' => 'Alternative PHP Cache', 'version' => phpversion('apc'));
  66. if (in_array('memcache', $checkFor) && function_exists('memcache_set'))
  67. $versions['memcache'] = array('title' => 'Memcached', 'version' => empty($memcached) ? '???' : memcache_get_version($memcached));
  68. if (in_array('xcache', $checkFor) && function_exists('xcache_set'))
  69. $versions['xcache'] = array('title' => 'XCache', 'version' => XCACHE_VERSION);
  70. if (in_array('php', $checkFor))
  71. $versions['php'] = array('title' => 'PHP', 'version' => PHP_VERSION, 'more' => '?action=admin;area=serversettings;sa=phpinfo');
  72. if (in_array('server', $checkFor))
  73. $versions['server'] = array('title' => $txt['support_versions_server'], 'version' => $_SERVER['SERVER_SOFTWARE']);
  74. return $versions;
  75. }
  76. /**
  77. * Search through source, theme and language files to determine their version.
  78. * Get detailed version information about the physical ELKARTE files on the server.
  79. *
  80. * - the input parameter allows to set whether to include SSI.php and whether
  81. * the results should be sorted.
  82. * - returns an array containing information on source files, templates and
  83. * language files found in the default theme directory (grouped by language).
  84. *
  85. * @param array &$versionOptions
  86. */
  87. function getFileVersions(&$versionOptions)
  88. {
  89. global $settings;
  90. // Default place to find the languages would be the default theme dir.
  91. $lang_dir = $settings['default_theme_dir'] . '/languages';
  92. $version_info = array(
  93. 'file_versions' => array(),
  94. 'file_versions_admin' => array(),
  95. 'file_versions_controllers' => array(),
  96. 'file_versions_database' => array(),
  97. 'file_versions_subs' => array(),
  98. 'default_template_versions' => array(),
  99. 'template_versions' => array(),
  100. 'default_language_versions' => array(),
  101. );
  102. // The comment looks rougly like... that.
  103. $version_regex = '~\*\s@version\s+(.+)[\s]{2}~i';
  104. $unknown_version = '??';
  105. // Find the version in SSI.php's file header.
  106. if (!empty($versionOptions['include_ssi']) && file_exists(BOARDDIR . '/SSI.php'))
  107. {
  108. $header = file_get_contents(BOARDDIR . '/SSI.php', NULL, NULL, 0, 768);
  109. if (preg_match($version_regex, $header, $match) == 1)
  110. $version_info['file_versions']['SSI.php'] = $match[1];
  111. // Not found! This is bad.
  112. else
  113. $version_info['file_versions']['SSI.php'] = $unknown_version;
  114. }
  115. // Do the paid subscriptions handler?
  116. if (!empty($versionOptions['include_subscriptions']) && file_exists(BOARDDIR . '/subscriptions.php'))
  117. {
  118. $header = file_get_contents(BOARDDIR . '/subscriptions.php', NULL, NULL, 0, 768);
  119. if (preg_match($version_regex, $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'] = $unknown_version;
  124. }
  125. // Load all the files in the sources and its sub directorys
  126. $directories = array(
  127. 'file_versions' => SOURCEDIR,
  128. 'file_versions_admin' => ADMINDIR,
  129. 'file_versions_controllers' => CONTROLLERDIR,
  130. 'file_versions_database' => SOURCEDIR . '/database',
  131. 'file_versions_subs' => SUBSDIR,
  132. 'file_versions_lib' => EXTDIR
  133. );
  134. foreach ($directories as $area => $dir)
  135. {
  136. $sources_dir = dir($dir);
  137. while ($entry = $sources_dir->read())
  138. {
  139. if (substr($entry, -4) === '.php' && !is_dir($dir . '/' . $entry) && $entry !== 'index.php' && $entry !== 'sphinxapi.php')
  140. {
  141. // Read the first 4k from the file.... enough for the header.
  142. $header = file_get_contents($dir . '/' . $entry, NULL, NULL, 0, 768);
  143. // Look for the version comment in the file header.
  144. if (preg_match($version_regex, $header, $match))
  145. $version_info[$area][$entry] = $match[1];
  146. // It wasn't found, but the file was... show a $unknown_version.
  147. else
  148. $version_info[$area][$entry] = '??';
  149. }
  150. }
  151. $sources_dir->close();
  152. }
  153. // Load all the files in the default template directory - and the current theme if applicable.
  154. $directories = array('default_template_versions' => $settings['default_theme_dir']);
  155. if ($settings['theme_id'] != 1)
  156. $directories += array('template_versions' => $settings['theme_dir']);
  157. foreach ($directories as $type => $dirname)
  158. {
  159. $this_dir = dir($dirname);
  160. while ($entry = $this_dir->read())
  161. {
  162. if (substr($entry, -12) == 'template.php' && !is_dir($dirname . '/' . $entry))
  163. {
  164. // Read the first 768 bytes from the file.... enough for the header.
  165. $header = file_get_contents($dirname . '/' . $entry, NULL, NULL, 0, 768);
  166. // Look for the version comment in the file header.
  167. if (preg_match($version_regex, $header, $match) == 1)
  168. $version_info[$type][$entry] = $match[1];
  169. // It wasn't found, but the file was... show a '??'.
  170. else
  171. $version_info[$type][$entry] = $unknown_version;
  172. }
  173. }
  174. $this_dir->close();
  175. }
  176. // Load up all the files in the default language directory and sort by language.
  177. $this_dir = dir($lang_dir);
  178. while ($entry = $this_dir->read())
  179. {
  180. if (substr($entry, -4) == '.php' && $entry != 'index.php' && !is_dir($lang_dir . '/' . $entry))
  181. {
  182. // Read the first 768 bytes from the file.... enough for the header.
  183. $header = file_get_contents($lang_dir . '/' . $entry, NULL, NULL, 0, 768);
  184. // Split the file name off into useful bits.
  185. list ($name, $language) = explode('.', $entry);
  186. // Look for the version comment in the file header.
  187. if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1)
  188. $version_info['default_language_versions'][$language][$name] = $match[1];
  189. // It wasn't found, but the file was... show a '??'.
  190. else
  191. $version_info['default_language_versions'][$language][$name] = $unknown_version;
  192. }
  193. }
  194. $this_dir->close();
  195. // Sort the file versions by filename.
  196. if (!empty($versionOptions['sort_results']))
  197. {
  198. ksort($version_info['file_versions']);
  199. ksort($version_info['file_versions_admin']);
  200. ksort($version_info['file_versions_controllers']);
  201. ksort($version_info['file_versions_database']);
  202. ksort($version_info['file_versions_subs']);
  203. ksort($version_info['default_template_versions']);
  204. ksort($version_info['template_versions']);
  205. ksort($version_info['default_language_versions']);
  206. // For languages sort each language too.
  207. foreach ($version_info['default_language_versions'] as $language => $dummy)
  208. ksort($version_info['default_language_versions'][$language]);
  209. }
  210. return $version_info;
  211. }
  212. /**
  213. * Update the Settings.php file.
  214. *
  215. * The most important function in this file for mod makers happens to be the
  216. * updateSettingsFile() function, but it shouldn't be used often anyway.
  217. *
  218. * - updates the Settings.php file with the changes supplied in config_vars.
  219. * - expects config_vars to be an associative array, with the keys as the
  220. * variable names in Settings.php, and the values the variable values.
  221. * - does not escape or quote values.
  222. * - preserves case, formatting, and additional options in file.
  223. * - writes nothing if the resulting file would be less than 10 lines
  224. * in length (sanity check for read lock.)
  225. * - check for changes to db_last_error and passes those off to a separate handler
  226. * - attempts to create a backup file and will use it should the writing of the
  227. * new settings file fail
  228. *
  229. * @param array $config_vars
  230. */
  231. function updateSettingsFile($config_vars)
  232. {
  233. global $context;
  234. // Updating the db_last_error, then don't mess around with Settings.php
  235. if (count($config_vars) === 1 && isset($config_vars['db_last_error']))
  236. {
  237. updateDbLastError($config_vars['db_last_error']);
  238. return;
  239. }
  240. // When was Settings.php last changed?
  241. $last_settings_change = filemtime(BOARDDIR . '/Settings.php');
  242. // Load the settings file.
  243. $settingsArray = trim(file_get_contents(BOARDDIR . '/Settings.php'));
  244. // Break it up based on \r or \n, and then clean out extra characters.
  245. if (strpos($settingsArray, "\n") !== false)
  246. $settingsArray = explode("\n", $settingsArray);
  247. elseif (strpos($settingsArray, "\r") !== false)
  248. $settingsArray = explode("\r", $settingsArray);
  249. else
  250. return;
  251. // Presumably, the file has to have stuff in it for this function to be called :P.
  252. if (count($settingsArray) < 10)
  253. return;
  254. // remove any /r's that made there way in here
  255. foreach ($settingsArray as $k => $dummy)
  256. $settingsArray[$k] = strtr($dummy, array("\r" => '')) . "\n";
  257. // go line by line and see whats changing
  258. for ($i = 0, $n = count($settingsArray); $i < $n; $i++)
  259. {
  260. // Don't trim or bother with it if it's not a variable.
  261. if (substr($settingsArray[$i], 0, 1) != '$')
  262. continue;
  263. $settingsArray[$i] = trim($settingsArray[$i]) . "\n";
  264. // Look through the variables to set....
  265. foreach ($config_vars as $var => $val)
  266. {
  267. // be sure someone is not updating db_last_error this with a group
  268. if ($var === 'db_last_error')
  269. {
  270. updateDbLastError($val);
  271. unset($config_vars[$var]);
  272. }
  273. elseif (strncasecmp($settingsArray[$i], '$' . $var, 1 + strlen($var)) == 0)
  274. {
  275. $comment = strstr(substr($settingsArray[$i], strpos($settingsArray[$i], ';')), '#');
  276. $settingsArray[$i] = '$' . $var . ' = ' . $val . ';' . ($comment == '' ? '' : "\t\t" . rtrim($comment)) . "\n";
  277. // This one's been 'used', so to speak.
  278. unset($config_vars[$var]);
  279. }
  280. }
  281. // End of the file ... maybe
  282. if (substr(trim($settingsArray[$i]), 0, 2) == '?' . '>')
  283. $end = $i;
  284. }
  285. // This should never happen, but apparently it is happening.
  286. if (empty($end) || $end < 10)
  287. $end = count($settingsArray) - 1;
  288. // Still more variables to go? Then lets add them at the end.
  289. if (!empty($config_vars))
  290. {
  291. if (trim($settingsArray[$end]) == '?' . '>')
  292. $settingsArray[$end++] = '';
  293. else
  294. $end++;
  295. // Add in any newly defined vars that were passed
  296. foreach ($config_vars as $var => $val)
  297. $settingsArray[$end++] = '$' . $var . ' = ' . $val . ';' . "\n";
  298. }
  299. else
  300. $settingsArray[$end] = trim($settingsArray[$end]);
  301. // Sanity error checking: the file needs to be at least 12 lines.
  302. if (count($settingsArray) < 12)
  303. return;
  304. // Try to avoid a few pitfalls:
  305. // - like a possible race condition,
  306. // - or a failure to write at low diskspace
  307. //
  308. // Check before you act: if cache is enabled, we can do a simple write test
  309. // to validate that we even write things on this filesystem.
  310. if ((!defined('CACHEDIR') || !file_exists(CACHEDIR)) && file_exists(BOARDDIR . '/cache'))
  311. $tmp_cache = BOARDDIR . '/cache';
  312. else
  313. $tmp_cache = CACHEDIR;
  314. $test_fp = @fopen($tmp_cache . '/settings_update.tmp', "w+");
  315. if ($test_fp)
  316. {
  317. fclose($test_fp);
  318. $written_bytes = file_put_contents($tmp_cache . '/settings_update.tmp', 'test', LOCK_EX);
  319. @unlink($tmp_cache . '/settings_update.tmp');
  320. if ($written_bytes !== 4)
  321. {
  322. // Oops. Low disk space, perhaps. Don't mess with Settings.php then.
  323. // No means no. :P
  324. return;
  325. }
  326. }
  327. // Protect me from what I want! :P
  328. clearstatcache();
  329. if (filemtime(BOARDDIR . '/Settings.php') === $last_settings_change)
  330. {
  331. // save the old before we do anything
  332. $file = BOARDDIR . '/Settings.php';
  333. $settings_backup_fail = !@is_writable(BOARDDIR . '/Settings_bak.php') || !@copy(BOARDDIR . '/Settings.php', BOARDDIR . '/Settings_bak.php');
  334. $settings_backup_fail = !$settings_backup_fail ? (!file_exists(BOARDDIR . '/Settings_bak.php') || filesize(BOARDDIR . '/Settings_bak.php') === 0) : $settings_backup_fail;
  335. // write out the new
  336. $write_settings = implode('', $settingsArray);
  337. $written_bytes = file_put_contents(BOARDDIR . '/Settings.php', $write_settings, LOCK_EX);
  338. // survey says ...
  339. if ($written_bytes !== strlen($write_settings) && !$settings_backup_fail)
  340. {
  341. // Well this is not good at all, lets see if we can save this
  342. $context['settings_message'] = 'settings_error';
  343. if (file_exists(BOARDDIR . '/Settings_bak.php'))
  344. @copy(BOARDDIR . '/Settings_bak.php', BOARDDIR . '/Settings.php');
  345. }
  346. }
  347. }
  348. /**
  349. * Saves the time of the last db error for the error log
  350. * - Done separately from updateSettingsFile to avoid race conditions
  351. * which can occur during a db error
  352. * - If it fails Settings.php will assume 0
  353. *
  354. * @param type $time
  355. */
  356. function updateDbLastError($time)
  357. {
  358. // Write out the db_last_error file with the error timestamp
  359. file_put_contents(BOARDDIR . '/db_last_error.php', '<' . '?' . "php\n" . '$db_last_error = ' . $time . ';', LOCK_EX);
  360. @touch(BOARDDIR . '/' . 'Settings.php');
  361. }
  362. /**
  363. * Saves the admins current preferences to the database.
  364. */
  365. function updateAdminPreferences()
  366. {
  367. global $options, $context, $smcFunc, $settings, $user_info;
  368. // This must exist!
  369. if (!isset($context['admin_preferences']))
  370. return false;
  371. // This is what we'll be saving.
  372. $options['admin_preferences'] = serialize($context['admin_preferences']);
  373. // Just check we haven't ended up with something theme exclusive somehow.
  374. $smcFunc['db_query']('', '
  375. DELETE FROM {db_prefix}themes
  376. WHERE id_theme != {int:default_theme}
  377. AND variable = {string:admin_preferences}',
  378. array(
  379. 'default_theme' => 1,
  380. 'admin_preferences' => 'admin_preferences',
  381. )
  382. );
  383. // Update the themes table.
  384. $smcFunc['db_insert']('replace',
  385. '{db_prefix}themes',
  386. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  387. array($user_info['id'], 1, 'admin_preferences', $options['admin_preferences']),
  388. array('id_member', 'id_theme', 'variable')
  389. );
  390. // Make sure we invalidate any cache.
  391. cache_put_data('theme_settings-' . $settings['theme_id'] . ':' . $user_info['id'], null, 0);
  392. }
  393. /**
  394. * Send all the administrators a lovely email.
  395. * - loads all users who are admins or have the admin forum permission.
  396. * - uses the email template and replacements passed in the parameters.
  397. * - sends them an email.
  398. *
  399. * @param string $template
  400. * @param array $replacements
  401. * @param array $additional_recipients
  402. */
  403. function emailAdmins($template, $replacements = array(), $additional_recipients = array())
  404. {
  405. global $smcFunc, $language, $modSettings;
  406. // We certainly want this.
  407. require_once(SUBSDIR . '/Mail.subs.php');
  408. // Load all groups which are effectively admins.
  409. $request = $smcFunc['db_query']('', '
  410. SELECT id_group
  411. FROM {db_prefix}permissions
  412. WHERE permission = {string:admin_forum}
  413. AND add_deny = {int:add_deny}
  414. AND id_group != {int:id_group}',
  415. array(
  416. 'add_deny' => 1,
  417. 'id_group' => 0,
  418. 'admin_forum' => 'admin_forum',
  419. )
  420. );
  421. $groups = array(1);
  422. while ($row = $smcFunc['db_fetch_assoc']($request))
  423. $groups[] = $row['id_group'];
  424. $smcFunc['db_free_result']($request);
  425. $request = $smcFunc['db_query']('', '
  426. SELECT id_member, member_name, real_name, lngfile, email_address
  427. FROM {db_prefix}members
  428. WHERE (id_group IN ({array_int:group_list}) OR FIND_IN_SET({raw:group_array_implode}, additional_groups) != 0)
  429. AND notify_types != {int:notify_types}
  430. ORDER BY lngfile',
  431. array(
  432. 'group_list' => $groups,
  433. 'notify_types' => 4,
  434. 'group_array_implode' => implode(', additional_groups) != 0 OR FIND_IN_SET(', $groups),
  435. )
  436. );
  437. $emails_sent = array();
  438. while ($row = $smcFunc['db_fetch_assoc']($request))
  439. {
  440. // Stick their particulars in the replacement data.
  441. $replacements['IDMEMBER'] = $row['id_member'];
  442. $replacements['REALNAME'] = $row['member_name'];
  443. $replacements['USERNAME'] = $row['real_name'];
  444. // Load the data from the template.
  445. $emaildata = loadEmailTemplate($template, $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
  446. // Then send the actual email.
  447. sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 1);
  448. // Track who we emailed so we don't do it twice.
  449. $emails_sent[] = $row['email_address'];
  450. }
  451. $smcFunc['db_free_result']($request);
  452. // Any additional users we must email this to?
  453. if (!empty($additional_recipients))
  454. foreach ($additional_recipients as $recipient)
  455. {
  456. if (in_array($recipient['email'], $emails_sent))
  457. continue;
  458. $replacements['IDMEMBER'] = $recipient['id'];
  459. $replacements['REALNAME'] = $recipient['name'];
  460. $replacements['USERNAME'] = $recipient['name'];
  461. // Load the template again.
  462. $emaildata = loadEmailTemplate($template, $replacements, empty($recipient['lang']) || empty($modSettings['userLanguage']) ? $language : $recipient['lang']);
  463. // Send off the email.
  464. sendmail($recipient['email'], $emaildata['subject'], $emaildata['body'], null, null, false, 1);
  465. }
  466. }