ManageMaintenance.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. <?php
  2. /**
  3. * Forum maintenance. Important stuff.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2011 Simple Machines
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.0
  13. */
  14. if (!defined('SMF'))
  15. die('Hacking attempt...');
  16. /**
  17. * Main dispatcher, the maintenance access point.
  18. * This, as usual, checks permissions, loads language files, and forwards to the actual workers.
  19. */
  20. function ManageMaintenance()
  21. {
  22. global $txt, $modSettings, $scripturl, $context, $options;
  23. // You absolutely must be an admin by here!
  24. isAllowedTo('admin_forum');
  25. // Need something to talk about?
  26. loadLanguage('ManageMaintenance');
  27. loadTemplate('ManageMaintenance');
  28. // This uses admin tabs - as it should!
  29. $context[$context['admin_menu_name']]['tab_data'] = array(
  30. 'title' => $txt['maintain_title'],
  31. 'description' => $txt['maintain_info'],
  32. 'tabs' => array(
  33. 'routine' => array(),
  34. 'database' => array(),
  35. 'members' => array(),
  36. 'topics' => array(),
  37. ),
  38. );
  39. // So many things you can do - but frankly I won't let you - just these!
  40. $subActions = array(
  41. 'routine' => array(
  42. 'function' => 'MaintainRoutine',
  43. 'template' => 'maintain_routine',
  44. 'activities' => array(
  45. 'version' => 'VersionDetail',
  46. 'repair' => 'MaintainFindFixErrors',
  47. 'recount' => 'AdminBoardRecount',
  48. 'logs' => 'MaintainEmptyUnimportantLogs',
  49. 'cleancache' => 'MaintainCleanCache',
  50. ),
  51. ),
  52. 'database' => array(
  53. 'function' => 'MaintainDatabase',
  54. 'template' => 'maintain_database',
  55. 'activities' => array(
  56. 'optimize' => 'OptimizeTables',
  57. 'backup' => 'MaintainDownloadBackup',
  58. 'convertentities' => 'ConvertEntities',
  59. 'convertutf8' => 'ConvertUtf8',
  60. ),
  61. ),
  62. 'members' => array(
  63. 'function' => 'MaintainMembers',
  64. 'template' => 'maintain_members',
  65. 'activities' => array(
  66. 'reattribute' => 'MaintainReattributePosts',
  67. 'purgeinactive' => 'MaintainPurgeInactiveMembers',
  68. ),
  69. ),
  70. 'topics' => array(
  71. 'function' => 'MaintainTopics',
  72. 'template' => 'maintain_topics',
  73. 'activities' => array(
  74. 'massmove' => 'MaintainMassMoveTopics',
  75. 'pruneold' => 'MaintainRemoveOldPosts',
  76. ),
  77. ),
  78. 'destroy' => array(
  79. 'function' => 'Destroy',
  80. 'activities' => array(),
  81. ),
  82. );
  83. // Yep, sub-action time!
  84. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  85. $subAction = $_REQUEST['sa'];
  86. else
  87. $subAction = 'routine';
  88. // Doing something special?
  89. if (isset($_REQUEST['activity']) && isset($subActions[$subAction]['activities'][$_REQUEST['activity']]))
  90. $activity = $_REQUEST['activity'];
  91. // Set a few things.
  92. $context['page_title'] = $txt['maintain_title'];
  93. $context['sub_action'] = $subAction;
  94. $context['sub_template'] = !empty($subActions[$subAction]['template']) ? $subActions[$subAction]['template'] : '';
  95. // Finally fall through to what we are doing.
  96. $subActions[$subAction]['function']();
  97. // Any special activity?
  98. if (isset($activity))
  99. $subActions[$subAction]['activities'][$activity]();
  100. //converted to UTF-8? show a small maintenance info
  101. if (isset($_GET['done']) && $_GET['done'] == 'convertutf8')
  102. $context['maintenance_finished'] = $txt['utf8_title'];
  103. }
  104. /**
  105. * Supporting function for the database maintenance area.
  106. */
  107. function MaintainDatabase()
  108. {
  109. global $context, $db_type, $db_character_set, $modSettings, $smcFunc, $txt;
  110. // Show some conversion options?
  111. $context['convert_utf8'] = $db_type == 'mysql' && (!isset($db_character_set) || $db_character_set !== 'utf8' || empty($modSettings['global_character_set']) || $modSettings['global_character_set'] !== 'UTF-8') && version_compare('4.1.2', preg_replace('~\-.+?$~', '', $smcFunc['db_server_info']())) <= 0;
  112. $context['convert_entities'] = $db_type == 'mysql' && isset($db_character_set, $modSettings['global_character_set']) && $db_character_set === 'utf8' && $modSettings['global_character_set'] === 'UTF-8';
  113. if (isset($_GET['done']) && $_GET['done'] == 'convertutf8')
  114. $context['maintenance_finished'] = $txt['utf8_title'];
  115. if (isset($_GET['done']) && $_GET['done'] == 'convertentities')
  116. $context['maintenance_finished'] = $txt['entity_convert_title'];
  117. }
  118. /**
  119. * Supporting function for the routine maintenance area.
  120. */
  121. function MaintainRoutine()
  122. {
  123. global $context, $txt;
  124. if (isset($_GET['done']) && $_GET['done'] == 'recount')
  125. $context['maintenance_finished'] = $txt['maintain_recount'];
  126. }
  127. /**
  128. * Supporting function for the members maintenance area.
  129. */
  130. function MaintainMembers()
  131. {
  132. global $context, $smcFunc, $txt;
  133. // Get membergroups - for deleting members and the like.
  134. $result = $smcFunc['db_query']('', '
  135. SELECT id_group, group_name
  136. FROM {db_prefix}membergroups',
  137. array(
  138. )
  139. );
  140. $context['membergroups'] = array(
  141. array(
  142. 'id' => 0,
  143. 'name' => $txt['maintain_members_ungrouped']
  144. ),
  145. );
  146. while ($row = $smcFunc['db_fetch_assoc']($result))
  147. {
  148. $context['membergroups'][] = array(
  149. 'id' => $row['id_group'],
  150. 'name' => $row['group_name']
  151. );
  152. }
  153. $smcFunc['db_free_result']($result);
  154. }
  155. /**
  156. * Supporting function for the topics maintenance area.
  157. */
  158. function MaintainTopics()
  159. {
  160. global $context, $smcFunc, $txt;
  161. // Let's load up the boards in case they are useful.
  162. $result = $smcFunc['db_query']('order_by_board_order', '
  163. SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat
  164. FROM {db_prefix}boards AS b
  165. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  166. WHERE {query_see_board}
  167. AND redirect = {string:blank_redirect}',
  168. array(
  169. 'blank_redirect' => '',
  170. )
  171. );
  172. $context['categories'] = array();
  173. while ($row = $smcFunc['db_fetch_assoc']($result))
  174. {
  175. if (!isset($context['categories'][$row['id_cat']]))
  176. $context['categories'][$row['id_cat']] = array(
  177. 'name' => $row['cat_name'],
  178. 'boards' => array()
  179. );
  180. $context['categories'][$row['id_cat']]['boards'][] = array(
  181. 'id' => $row['id_board'],
  182. 'name' => $row['name'],
  183. 'child_level' => $row['child_level']
  184. );
  185. }
  186. $smcFunc['db_free_result']($result);
  187. if (isset($_GET['done']) && $_GET['done'] == 'purgeold')
  188. $context['maintenance_finished'] = $txt['maintain_old'];
  189. elseif (isset($_GET['done']) && $_GET['done'] == 'massmove')
  190. $context['maintenance_finished'] = $txt['move_topics_maintenance'];
  191. }
  192. /**
  193. * Find and fix all errors on the forum.
  194. */
  195. function MaintainFindFixErrors()
  196. {
  197. global $sourcedir;
  198. require_once($sourcedir . '/RepairBoards.php');
  199. RepairBoards();
  200. }
  201. /**
  202. * Wipes the whole cache directory.
  203. * This only applies to SMF's own cache directory, though.
  204. */
  205. function MaintainCleanCache()
  206. {
  207. global $context, $txt;
  208. // Just wipe the whole cache directory!
  209. clean_cache();
  210. $context['maintenance_finished'] = $txt['maintain_cache'];
  211. }
  212. /**
  213. * Empties all uninmportant logs
  214. */
  215. function MaintainEmptyUnimportantLogs()
  216. {
  217. global $context, $smcFunc, $txt;
  218. checkSession();
  219. // No one's online now.... MUHAHAHAHA :P.
  220. $smcFunc['db_query']('', '
  221. DELETE FROM {db_prefix}log_online');
  222. // Dump the banning logs.
  223. $smcFunc['db_query']('', '
  224. DELETE FROM {db_prefix}log_banned');
  225. // Start id_error back at 0 and dump the error log.
  226. $smcFunc['db_query']('truncate_table', '
  227. TRUNCATE {db_prefix}log_errors');
  228. // Clear out the spam log.
  229. $smcFunc['db_query']('', '
  230. DELETE FROM {db_prefix}log_floodcontrol');
  231. // Clear out the karma actions.
  232. $smcFunc['db_query']('', '
  233. DELETE FROM {db_prefix}log_karma');
  234. // Last but not least, the search logs!
  235. $smcFunc['db_query']('truncate_table', '
  236. TRUNCATE {db_prefix}log_search_topics');
  237. $smcFunc['db_query']('truncate_table', '
  238. TRUNCATE {db_prefix}log_search_messages');
  239. $smcFunc['db_query']('truncate_table', '
  240. TRUNCATE {db_prefix}log_search_results');
  241. updateSettings(array('search_pointer' => 0));
  242. $context['maintenance_finished'] = $txt['maintain_logs'];
  243. }
  244. // Oh noes!
  245. function Destroy()
  246. {
  247. global $context;
  248. echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  249. <html xmlns="http://www.w3.org/1999/xhtml"', $context['right_to_left'] ? ' dir="rtl"' : '', '><head><title>', $context['forum_name_html_safe'], ' deleted!</title></head>
  250. <body style="background-color: orange; font-family: arial, sans-serif; text-align: center;">
  251. <div style="margin-top: 8%; font-size: 400%; color: black;">Oh my, you killed ', $context['forum_name_html_safe'], '!</div>
  252. <div style="margin-top: 7%; font-size: 500%; color: red;"><strong>You lazy bum!</strong></div>
  253. </body></html>';
  254. obExit(false);
  255. }
  256. /**
  257. * Convert both data and database tables to UTF-8 character set.
  258. * It requires the admin_forum permission.
  259. * This only works if UTF-8 is not the global character set.
  260. * It supports all character sets used by SMF's language files.
  261. * It redirects to ?action=admin;area=maintain after finishing.
  262. * This action is linked from the maintenance screen (if it's applicable).
  263. * Accessed by ?action=admin;area=maintain;sa=database;activity=convertutf8.
  264. *
  265. * @uses the convert_utf8 sub template of the Admin template.
  266. */
  267. function ConvertUtf8()
  268. {
  269. global $scripturl, $context, $txt, $language, $db_character_set;
  270. global $modSettings, $user_info, $sourcedir, $smcFunc, $db_prefix;
  271. // Show me your badge!
  272. isAllowedTo('admin_forum');
  273. // The character sets used in SMF's language files with their db equivalent.
  274. $charsets = array(
  275. // Chinese-traditional.
  276. 'big5' => 'big5',
  277. // Chinese-simplified.
  278. 'gbk' => 'gbk',
  279. // West European.
  280. 'ISO-8859-1' => 'latin1',
  281. // Romanian.
  282. 'ISO-8859-2' => 'latin2',
  283. // Turkish.
  284. 'ISO-8859-9' => 'latin5',
  285. // West European with Euro sign.
  286. 'ISO-8859-15' => 'latin9',
  287. // Thai.
  288. 'tis-620' => 'tis620',
  289. // Persian, Chinese, etc.
  290. 'UTF-8' => 'utf8',
  291. // Russian.
  292. 'windows-1251' => 'cp1251',
  293. // Greek.
  294. 'windows-1253' => 'utf8',
  295. // Hebrew.
  296. 'windows-1255' => 'utf8',
  297. // Arabic.
  298. 'windows-1256' => 'cp1256',
  299. );
  300. // Get a list of character sets supported by your MySQL server.
  301. $request = $smcFunc['db_query']('', '
  302. SHOW CHARACTER SET',
  303. array(
  304. )
  305. );
  306. $db_charsets = array();
  307. while ($row = $smcFunc['db_fetch_assoc']($request))
  308. $db_charsets[] = $row['Charset'];
  309. $smcFunc['db_free_result']($request);
  310. // Character sets supported by both MySQL and SMF's language files.
  311. $charsets = array_intersect($charsets, $db_charsets);
  312. // This is for the first screen telling backups is good.
  313. if (!isset($_POST['proceed']))
  314. {
  315. // Character set conversions are only supported as of MySQL 4.1.2.
  316. if (version_compare('4.1.2', preg_replace('~\-.+?$~', '', $smcFunc['db_server_info']())) > 0)
  317. fatal_lang_error('utf8_db_version_too_low');
  318. // Use the messages.body column as indicator for the database charset.
  319. $request = $smcFunc['db_query']('', '
  320. SHOW FULL COLUMNS
  321. FROM {db_prefix}messages
  322. LIKE {string:body_like}',
  323. array(
  324. 'body_like' => 'body',
  325. )
  326. );
  327. $column_info = $smcFunc['db_fetch_assoc']($request);
  328. $smcFunc['db_free_result']($request);
  329. // A collation looks like latin1_swedish. We only need the character set.
  330. list($context['database_charset']) = explode('_', $column_info['Collation']);
  331. $context['database_charset'] = in_array($context['database_charset'], $charsets) ? array_search($context['database_charset'], $charsets) : $context['database_charset'];
  332. // No need to convert to UTF-8 if it already is.
  333. if ($db_character_set === 'utf8' && !empty($modSettings['global_character_set']) && $modSettings['global_character_set'] === 'UTF-8')
  334. fatal_lang_error('utf8_already_utf8');
  335. // Cannot do conversion if using a fulltext index
  336. if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext')
  337. fatal_lang_error('utf8_cannot_convert_fulltext');
  338. // Grab the character set from the default language file.
  339. loadLanguage('index', $language, true);
  340. $context['charset_detected'] = $txt['lang_character_set'];
  341. $context['charset_about_detected'] = sprintf($txt['utf8_detected_charset'], $language, $context['charset_detected']);
  342. // Go back to your own language.
  343. loadLanguage('index', $user_info['language'], true);
  344. // Show a warning if the character set seems not to be supported.
  345. if (!isset($charsets[strtr(strtolower($context['charset_detected']), array('utf' => 'UTF', 'iso' => 'ISO'))]))
  346. {
  347. $context['charset_warning'] = sprintf($txt['utf8_charset_not_supported'], $txt['lang_character_set']);
  348. // Default to ISO-8859-1.
  349. $context['charset_detected'] = 'ISO-8859-1';
  350. }
  351. $context['charset_list'] = array_keys($charsets);
  352. $context['page_title'] = $txt['utf8_title'];
  353. $context['sub_template'] = 'convert_utf8';
  354. return;
  355. }
  356. // After this point we're starting the conversion. But first: session check.
  357. checkSession();
  358. // Translation table for the character sets not native for MySQL.
  359. $translation_tables = array(
  360. 'windows-1255' => array(
  361. '0x81' => '\'\'', '0x8A' => '\'\'', '0x8C' => '\'\'',
  362. '0x8D' => '\'\'', '0x8E' => '\'\'', '0x8F' => '\'\'',
  363. '0x90' => '\'\'', '0x9A' => '\'\'', '0x9C' => '\'\'',
  364. '0x9D' => '\'\'', '0x9E' => '\'\'', '0x9F' => '\'\'',
  365. '0xCA' => '\'\'', '0xD9' => '\'\'', '0xDA' => '\'\'',
  366. '0xDB' => '\'\'', '0xDC' => '\'\'', '0xDD' => '\'\'',
  367. '0xDE' => '\'\'', '0xDF' => '\'\'', '0xFB' => '\'\'',
  368. '0xFC' => '\'\'', '0xFF' => '\'\'', '0xC2' => '0xFF',
  369. '0x80' => '0xFC', '0xE2' => '0xFB', '0xA0' => '0xC2A0',
  370. '0xA1' => '0xC2A1', '0xA2' => '0xC2A2', '0xA3' => '0xC2A3',
  371. '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7',
  372. '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB',
  373. '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE',
  374. '0xAF' => '0xC2AF', '0xB0' => '0xC2B0', '0xB1' => '0xC2B1',
  375. '0xB2' => '0xC2B2', '0xB3' => '0xC2B3', '0xB4' => '0xC2B4',
  376. '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7',
  377. '0xB8' => '0xC2B8', '0xB9' => '0xC2B9', '0xBB' => '0xC2BB',
  378. '0xBC' => '0xC2BC', '0xBD' => '0xC2BD', '0xBE' => '0xC2BE',
  379. '0xBF' => '0xC2BF', '0xD7' => '0xD7B3', '0xD1' => '0xD781',
  380. '0xD4' => '0xD7B0', '0xD5' => '0xD7B1', '0xD6' => '0xD7B2',
  381. '0xE0' => '0xD790', '0xEA' => '0xD79A', '0xEC' => '0xD79C',
  382. '0xED' => '0xD79D', '0xEE' => '0xD79E', '0xEF' => '0xD79F',
  383. '0xF0' => '0xD7A0', '0xF1' => '0xD7A1', '0xF2' => '0xD7A2',
  384. '0xF3' => '0xD7A3', '0xF5' => '0xD7A5', '0xF6' => '0xD7A6',
  385. '0xF7' => '0xD7A7', '0xF8' => '0xD7A8', '0xF9' => '0xD7A9',
  386. '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6',
  387. '0x86' => '0xE280A0', '0x87' => '0xE280A1', '0x89' => '0xE280B0',
  388. '0x8B' => '0xE280B9', '0x93' => '0xE2809C', '0x94' => '0xE2809D',
  389. '0x95' => '0xE280A2', '0x97' => '0xE28094', '0x99' => '0xE284A2',
  390. '0xC0' => '0xD6B0', '0xC1' => '0xD6B1', '0xC3' => '0xD6B3',
  391. '0xC4' => '0xD6B4', '0xC5' => '0xD6B5', '0xC6' => '0xD6B6',
  392. '0xC7' => '0xD6B7', '0xC8' => '0xD6B8', '0xC9' => '0xD6B9',
  393. '0xCB' => '0xD6BB', '0xCC' => '0xD6BC', '0xCD' => '0xD6BD',
  394. '0xCE' => '0xD6BE', '0xCF' => '0xD6BF', '0xD0' => '0xD780',
  395. '0xD2' => '0xD782', '0xE3' => '0xD793', '0xE4' => '0xD794',
  396. '0xE5' => '0xD795', '0xE7' => '0xD797', '0xE9' => '0xD799',
  397. '0xFD' => '0xE2808E', '0xFE' => '0xE2808F', '0x92' => '0xE28099',
  398. '0x83' => '0xC692', '0xD3' => '0xD783', '0x88' => '0xCB86',
  399. '0x98' => '0xCB9C', '0x91' => '0xE28098', '0x96' => '0xE28093',
  400. '0xBA' => '0xC3B7', '0x9B' => '0xE280BA', '0xAA' => '0xC397',
  401. '0xA4' => '0xE282AA', '0xE1' => '0xD791', '0xE6' => '0xD796',
  402. '0xE8' => '0xD798', '0xEB' => '0xD79B', '0xF4' => '0xD7A4',
  403. '0xFA' => '0xD7AA', '0xFF' => '0xD6B2', '0xFC' => '0xE282AC',
  404. '0xFB' => '0xD792',
  405. ),
  406. 'windows-1253' => array(
  407. '0x81' => '\'\'', '0x88' => '\'\'', '0x8A' => '\'\'',
  408. '0x8C' => '\'\'', '0x8D' => '\'\'', '0x8E' => '\'\'',
  409. '0x8F' => '\'\'', '0x90' => '\'\'', '0x98' => '\'\'',
  410. '0x9A' => '\'\'', '0x9C' => '\'\'', '0x9D' => '\'\'',
  411. '0x9E' => '\'\'', '0x9F' => '\'\'', '0xAA' => '\'\'',
  412. '0xD2' => '\'\'', '0xFF' => '\'\'', '0xCE' => '0xCE9E',
  413. '0xB8' => '0xCE88', '0xBA' => '0xCE8A', '0xBC' => '0xCE8C',
  414. '0xBE' => '0xCE8E', '0xBF' => '0xCE8F', '0xC0' => '0xCE90',
  415. '0xC8' => '0xCE98', '0xCA' => '0xCE9A', '0xCC' => '0xCE9C',
  416. '0xCD' => '0xCE9D', '0xCF' => '0xCE9F', '0xDA' => '0xCEAA',
  417. '0xE8' => '0xCEB8', '0xEA' => '0xCEBA', '0xEC' => '0xCEBC',
  418. '0xEE' => '0xCEBE', '0xEF' => '0xCEBF', '0xC2' => '0xFF',
  419. '0xBD' => '0xC2BD', '0xED' => '0xCEBD', '0xB2' => '0xC2B2',
  420. '0xA0' => '0xC2A0', '0xA3' => '0xC2A3', '0xA4' => '0xC2A4',
  421. '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7',
  422. '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB',
  423. '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE',
  424. '0xB0' => '0xC2B0', '0xB1' => '0xC2B1', '0xB3' => '0xC2B3',
  425. '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7',
  426. '0xBB' => '0xC2BB', '0xE2' => '0xCEB2', '0x80' => '0xD2',
  427. '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6',
  428. '0x86' => '0xE280A0', '0xA1' => '0xCE85', '0xA2' => '0xCE86',
  429. '0x87' => '0xE280A1', '0x89' => '0xE280B0', '0xB9' => '0xCE89',
  430. '0x8B' => '0xE280B9', '0x91' => '0xE28098', '0x99' => '0xE284A2',
  431. '0x92' => '0xE28099', '0x93' => '0xE2809C', '0x94' => '0xE2809D',
  432. '0x95' => '0xE280A2', '0x96' => '0xE28093', '0x97' => '0xE28094',
  433. '0x9B' => '0xE280BA', '0xAF' => '0xE28095', '0xB4' => '0xCE84',
  434. '0xC1' => '0xCE91', '0xC3' => '0xCE93', '0xC4' => '0xCE94',
  435. '0xC5' => '0xCE95', '0xC6' => '0xCE96', '0x83' => '0xC692',
  436. '0xC7' => '0xCE97', '0xC9' => '0xCE99', '0xCB' => '0xCE9B',
  437. '0xD0' => '0xCEA0', '0xD1' => '0xCEA1', '0xD3' => '0xCEA3',
  438. '0xD4' => '0xCEA4', '0xD5' => '0xCEA5', '0xD6' => '0xCEA6',
  439. '0xD7' => '0xCEA7', '0xD8' => '0xCEA8', '0xD9' => '0xCEA9',
  440. '0xDB' => '0xCEAB', '0xDC' => '0xCEAC', '0xDD' => '0xCEAD',
  441. '0xDE' => '0xCEAE', '0xDF' => '0xCEAF', '0xE0' => '0xCEB0',
  442. '0xE1' => '0xCEB1', '0xE3' => '0xCEB3', '0xE4' => '0xCEB4',
  443. '0xE5' => '0xCEB5', '0xE6' => '0xCEB6', '0xE7' => '0xCEB7',
  444. '0xE9' => '0xCEB9', '0xEB' => '0xCEBB', '0xF0' => '0xCF80',
  445. '0xF1' => '0xCF81', '0xF2' => '0xCF82', '0xF3' => '0xCF83',
  446. '0xF4' => '0xCF84', '0xF5' => '0xCF85', '0xF6' => '0xCF86',
  447. '0xF7' => '0xCF87', '0xF8' => '0xCF88', '0xF9' => '0xCF89',
  448. '0xFA' => '0xCF8A', '0xFB' => '0xCF8B', '0xFC' => '0xCF8C',
  449. '0xFD' => '0xCF8D', '0xFE' => '0xCF8E', '0xFF' => '0xCE92',
  450. '0xD2' => '0xE282AC',
  451. ),
  452. );
  453. // Make some preparations.
  454. if (isset($translation_tables[$_POST['src_charset']]))
  455. {
  456. $replace = '%field%';
  457. foreach ($translation_tables[$_POST['src_charset']] as $from => $to)
  458. $replace = 'REPLACE(' . $replace . ', ' . $from . ', ' . $to . ')';
  459. }
  460. // Grab a list of tables.
  461. if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) === 1)
  462. $queryTables = $smcFunc['db_query']('', '
  463. SHOW TABLE STATUS
  464. FROM `' . strtr($match[1], array('`' => '')) . '`
  465. LIKE {string:table_name}',
  466. array(
  467. 'table_name' => str_replace('_', '\_', $match[2]) . '%',
  468. )
  469. );
  470. else
  471. $queryTables = $smcFunc['db_query']('', '
  472. SHOW TABLE STATUS
  473. LIKE {string:table_name}',
  474. array(
  475. 'table_name' => str_replace('_', '\_', $db_prefix) . '%',
  476. )
  477. );
  478. while ($table_info = $smcFunc['db_fetch_assoc']($queryTables))
  479. {
  480. // Just to make sure it doesn't time out.
  481. if (function_exists('apache_reset_timeout'))
  482. @apache_reset_timeout();
  483. $table_charsets = array();
  484. // Loop through each column.
  485. $queryColumns = $smcFunc['db_query']('', '
  486. SHOW FULL COLUMNS
  487. FROM ' . $table_info['Name'],
  488. array(
  489. )
  490. );
  491. while ($column_info = $smcFunc['db_fetch_assoc']($queryColumns))
  492. {
  493. // Only text'ish columns have a character set and need converting.
  494. if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
  495. {
  496. $collation = empty($column_info['Collation']) || $column_info['Collation'] === 'NULL' ? $table_info['Collation'] : $column_info['Collation'];
  497. if (!empty($collation) && $collation !== 'NULL')
  498. {
  499. list($charset) = explode('_', $collation);
  500. if (!isset($table_charsets[$charset]))
  501. $table_charsets[$charset] = array();
  502. $table_charsets[$charset][] = $column_info;
  503. }
  504. }
  505. }
  506. $smcFunc['db_free_result']($queryColumns);
  507. // Only change the column if the data doesn't match the current charset.
  508. if ((count($table_charsets) === 1 && key($table_charsets) !== $charsets[$_POST['src_charset']]) || count($table_charsets) > 1)
  509. {
  510. $updates_blob = '';
  511. $updates_text = '';
  512. foreach ($table_charsets as $charset => $columns)
  513. {
  514. if ($charset !== $charsets[$_POST['src_charset']])
  515. {
  516. foreach ($columns as $column)
  517. {
  518. $updates_blob .= '
  519. CHANGE COLUMN ' . $column['Field'] . ' ' . $column['Field'] . ' ' . strtr($column['Type'], array('text' => 'blob', 'char' => 'binary')) . ($column['Null'] === 'YES' ? ' NULL' : ' NOT NULL') . (strpos($column['Type'], 'char') === false ? '' : ' default \'' . $column['Default'] . '\'') . ',';
  520. $updates_text .= '
  521. CHANGE COLUMN ' . $column['Field'] . ' ' . $column['Field'] . ' ' . $column['Type'] . ' CHARACTER SET ' . $charsets[$_POST['src_charset']] . ($column['Null'] === 'YES' ? '' : ' NOT NULL') . (strpos($column['Type'], 'char') === false ? '' : ' default \'' . $column['Default'] . '\'') . ',';
  522. }
  523. }
  524. }
  525. // Change the columns to binary form.
  526. $smcFunc['db_query']('', '
  527. ALTER TABLE {raw:table_name}{raw:updates_blob}',
  528. array(
  529. 'table_name' => $table_info['Name'],
  530. 'updates_blob' => substr($updates_blob, 0, -1),
  531. )
  532. );
  533. // Convert the character set if MySQL has no native support for it.
  534. if (isset($translation_tables[$_POST['src_charset']]))
  535. {
  536. $update = '';
  537. foreach ($table_charsets as $charset => $columns)
  538. foreach ($columns as $column)
  539. $update .= '
  540. ' . $column['Field'] . ' = ' . strtr($replace, array('%field%' => $column['Field'])) . ',';
  541. $smcFunc['db_query']('', '
  542. UPDATE {raw:table_name}
  543. SET {raw:updates}',
  544. array(
  545. 'table_name' => $table_info['Name'],
  546. 'updates' => substr($update, 0, -1),
  547. )
  548. );
  549. }
  550. // Change the columns back, but with the proper character set.
  551. $smcFunc['db_query']('', '
  552. ALTER TABLE {raw:table_name}{raw:updates_text}',
  553. array(
  554. 'table_name' => $table_info['Name'],
  555. 'updates_text' => substr($updates_text, 0, -1),
  556. )
  557. );
  558. }
  559. // Now do the actual conversion (if still needed).
  560. if ($charsets[$_POST['src_charset']] !== 'utf8')
  561. $smcFunc['db_query']('', '
  562. ALTER TABLE {raw:table_name}
  563. CONVERT TO CHARACTER SET utf8',
  564. array(
  565. 'table_name' => $table_info['Name'],
  566. )
  567. );
  568. }
  569. $smcFunc['db_free_result']($queryTables);
  570. // Let the settings know we have a new character set.
  571. updateSettings(array('global_character_set' => 'UTF-8', 'previousCharacterSet' => (empty($translation_tables[$_POST['src_charset']])) ? $charsets[$_POST['src_charset']] : $translation_tables[$_POST['src_charset']]));
  572. // Store it in Settings.php too because it's needed before db connection.
  573. require_once($sourcedir . '/Subs-Admin.php');
  574. updateSettingsFile(array('db_character_set' => '\'utf8\''));
  575. // The conversion might have messed up some serialized strings. Fix them!
  576. require_once($sourcedir . '/Subs-Charset.php');
  577. fix_serialized_columns();
  578. redirectexit('action=admin;area=maintain;done=convertutf8');
  579. }
  580. /**
  581. * Converts HTML-entities to their UTF-8 character equivalents.
  582. * This requires the admin_forum permission.
  583. * Pre-condition: UTF-8 has been set as database and global character set.
  584. *
  585. * It is divided in steps of 10 seconds.
  586. * This action is linked from the maintenance screen (if applicable).
  587. * It is accessed by ?action=admin;area=maintain;sa=database;activity=convertentities.
  588. *
  589. * @uses Admin template, convert_entities sub-template.
  590. */
  591. function ConvertEntities()
  592. {
  593. global $db_character_set, $modSettings, $context, $sourcedir, $smcFunc;
  594. isAllowedTo('admin_forum');
  595. // Check to see if UTF-8 is currently the default character set.
  596. if ($modSettings['global_character_set'] !== 'UTF-8' || !isset($db_character_set) || $db_character_set !== 'utf8')
  597. fatal_lang_error('entity_convert_only_utf8');
  598. // Some starting values.
  599. $context['table'] = empty($_REQUEST['table']) ? 0 : (int) $_REQUEST['table'];
  600. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  601. $context['start_time'] = time();
  602. $context['first_step'] = !isset($_REQUEST[$context['session_var']]);
  603. $context['last_step'] = false;
  604. // The first step is just a text screen with some explanation.
  605. if ($context['first_step'])
  606. {
  607. $context['sub_template'] = 'convert_entities';
  608. return;
  609. }
  610. // Otherwise use the generic "not done" template.
  611. $context['sub_template'] = 'not_done';
  612. $context['continue_post_data'] = '';
  613. $context['continue_countdown'] = 3;
  614. // Now we're actually going to convert...
  615. checkSession('request');
  616. // A list of tables ready for conversion.
  617. $tables = array(
  618. 'ban_groups',
  619. 'ban_items',
  620. 'boards',
  621. 'calendar',
  622. 'calendar_holidays',
  623. 'categories',
  624. 'log_errors',
  625. 'log_search_subjects',
  626. 'membergroups',
  627. 'members',
  628. 'message_icons',
  629. 'messages',
  630. 'package_servers',
  631. 'personal_messages',
  632. 'pm_recipients',
  633. 'polls',
  634. 'poll_choices',
  635. 'smileys',
  636. 'themes',
  637. );
  638. $context['num_tables'] = count($tables);
  639. // This function will do the conversion later on.
  640. $entity_replace = create_function('$string', '
  641. $num = substr($string, 0, 1) === \'x\' ? hexdec(substr($string, 1)) : (int) $string;
  642. return $num < 0x20 || $num > 0x10FFFF || ($num >= 0xD800 && $num <= 0xDFFF) ? \'\' : ($num < 0x80 ? \'&#\' . $num . \';\' : ($num < 0x800 ? chr(192 | $num >> 6) . chr(128 | $num & 63) : ($num < 0x10000 ? chr(224 | $num >> 12) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63) : chr(240 | $num >> 18) . chr(128 | $num >> 12 & 63) . chr(128 | $num >> 6 & 63) . chr(128 | $num & 63))));');
  643. // Loop through all tables that need converting.
  644. for (; $context['table'] < $context['num_tables']; $context['table']++)
  645. {
  646. $cur_table = $tables[$context['table']];
  647. $primary_key = '';
  648. // Make sure we keep stuff unique!
  649. $primary_keys = array();
  650. if (function_exists('apache_reset_timeout'))
  651. @apache_reset_timeout();
  652. // Get a list of text columns.
  653. $columns = array();
  654. $request = $smcFunc['db_query']('', '
  655. SHOW FULL COLUMNS
  656. FROM {db_prefix}' . $cur_table,
  657. array(
  658. )
  659. );
  660. while ($column_info = $smcFunc['db_fetch_assoc']($request))
  661. if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
  662. $columns[] = strtolower($column_info['Field']);
  663. // Get the column with the (first) primary key.
  664. $request = $smcFunc['db_query']('', '
  665. SHOW KEYS
  666. FROM {db_prefix}' . $cur_table,
  667. array(
  668. )
  669. );
  670. while ($row = $smcFunc['db_fetch_assoc']($request))
  671. {
  672. if ($row['Key_name'] === 'PRIMARY')
  673. {
  674. if (empty($primary_key) || ($row['Seq_in_index'] == 1 && !in_array(strtolower($row['Column_name']), $columns)))
  675. $primary_key = $row['Column_name'];
  676. $primary_keys[] = $row['Column_name'];
  677. }
  678. }
  679. $smcFunc['db_free_result']($request);
  680. // No primary key, no glory.
  681. // Same for columns. Just to be sure we've work to do!
  682. if (empty($primary_key) || empty($columns))
  683. continue;
  684. // Get the maximum value for the primary key.
  685. $request = $smcFunc['db_query']('', '
  686. SELECT MAX(' . $primary_key . ')
  687. FROM {db_prefix}' . $cur_table,
  688. array(
  689. )
  690. );
  691. list($max_value) = $smcFunc['db_fetch_row']($request);
  692. $smcFunc['db_free_result']($request);
  693. if (empty($max_value))
  694. continue;
  695. while ($context['start'] <= $max_value)
  696. {
  697. // Retrieve a list of rows that has at least one entity to convert.
  698. $request = $smcFunc['db_query']('', '
  699. SELECT {raw:primary_keys}, {raw:columns}
  700. FROM {db_prefix}{raw:cur_table}
  701. WHERE {raw:primary_key} BETWEEN {int:start} AND {int:start} + 499
  702. AND {raw:like_compare}
  703. LIMIT 500',
  704. array(
  705. 'primary_keys' => implode(', ', $primary_keys),
  706. 'columns' => implode(', ', $columns),
  707. 'cur_table' => $cur_table,
  708. 'primary_key' => $primary_key,
  709. 'start' => $context['start'],
  710. 'like_compare' => '(' . implode(' LIKE \'%&#%\' OR ', $columns) . ' LIKE \'%&#%\')',
  711. )
  712. );
  713. while ($row = $smcFunc['db_fetch_assoc']($request))
  714. {
  715. $insertion_variables = array();
  716. $changes = array();
  717. foreach ($row as $column_name => $column_value)
  718. if ($column_name !== $primary_key && strpos($column_value, '&#') !== false)
  719. {
  720. $changes[] = $column_name . ' = {string:changes_' . $column_name . '}';
  721. $insertion_variables['changes_' . $column_name] = preg_replace('~(&#(\d{1,7}|x[0-9a-fA-F]{1,6});)~e', '$entity_replace(\'\\2\')', $column_value);
  722. }
  723. $where = array();
  724. foreach ($primary_keys as $key)
  725. {
  726. $where[] = $key . ' = {string:where_' . $key . '}';
  727. $insertion_variables['where_' . $key] = $row[$key];
  728. }
  729. // Update the row.
  730. if (!empty($changes))
  731. $smcFunc['db_query']('', '
  732. UPDATE {db_prefix}' . $cur_table . '
  733. SET
  734. ' . implode(',
  735. ', $changes) . '
  736. WHERE ' . implode(' AND ', $where),
  737. $insertion_variables
  738. );
  739. }
  740. $smcFunc['db_free_result']($request);
  741. $context['start'] += 500;
  742. // After ten seconds interrupt.
  743. if (time() - $context['start_time'] > 10)
  744. {
  745. // Calculate an approximation of the percentage done.
  746. $context['continue_percent'] = round(100 * ($context['table'] + ($context['start'] / $max_value)) / $context['num_tables'], 1);
  747. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=convertentities;table=' . $context['table'] . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  748. return;
  749. }
  750. }
  751. $context['start'] = 0;
  752. }
  753. // Make sure all serialized strings are all right.
  754. require_once($sourcedir . '/Subs-Charset.php');
  755. fix_serialized_columns();
  756. // If we're here, we must be done.
  757. $context['continue_percent'] = 100;
  758. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;done=convertentities';
  759. $context['last_step'] = true;
  760. $context['continue_countdown'] = -1;
  761. }
  762. /**
  763. * Optimizes all tables in the database and lists how much was saved.
  764. * It requires the admin_forum permission.
  765. * It shows as the maintain_forum admin area.
  766. * It is accessed from ?action=admin;area=maintain;sa=database;activity=optimize.
  767. * It also updates the optimize scheduled task such that the tables are not automatically optimized again too soon.
  768. * @uses the rawdata sub template (built in.)
  769. */
  770. function OptimizeTables()
  771. {
  772. global $db_type, $db_name, $db_prefix, $txt, $context, $scripturl, $sourcedir, $smcFunc;
  773. isAllowedTo('admin_forum');
  774. checkSession('post');
  775. ignore_user_abort(true);
  776. db_extend();
  777. // Start with no tables optimized.
  778. $opttab = 0;
  779. $context['page_title'] = $txt['database_optimize'];
  780. $context['sub_template'] = 'optimize';
  781. // Only optimize the tables related to this smf install, not all the tables in the db
  782. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  783. // Get a list of tables, as well as how many there are.
  784. $temp_tables = $smcFunc['db_list_tables'](false, $real_prefix . '%');
  785. $tables = array();
  786. foreach ($temp_tables as $table)
  787. $tables[] = array('table_name' => $table);
  788. // If there aren't any tables then I believe that would mean the world has exploded...
  789. $context['num_tables'] = count($tables);
  790. if ($context['num_tables'] == 0)
  791. fatal_error('You appear to be running SMF in a flat file mode... fantastic!', false);
  792. // For each table....
  793. $context['optimized_tables'] = array();
  794. foreach ($tables as $table)
  795. {
  796. // Optimize the table! We use backticks here because it might be a custom table.
  797. $data_freed = $smcFunc['db_optimize_table']($table['table_name']);
  798. // Optimizing one sqlite table optimizes them all.
  799. if ($db_type == 'sqlite')
  800. break;
  801. if ($data_freed > 0)
  802. $context['optimized_tables'][] = array(
  803. 'name' => $table['table_name'],
  804. 'data_freed' => $data_freed,
  805. );
  806. }
  807. // Number of tables, etc....
  808. $txt['database_numb_tables'] = sprintf($txt['database_numb_tables'], $context['num_tables']);
  809. $context['num_tables_optimized'] = count($context['optimized_tables']);
  810. // Check that we don't auto optimise again too soon!
  811. require_once($sourcedir . '/ScheduledTasks.php');
  812. CalculateNextTrigger('auto_optimize', true);
  813. }
  814. /**
  815. * Recount many forum totals that can be recounted automatically without harm.
  816. * it requires the admin_forum permission.
  817. * It shows the maintain_forum admin area.
  818. * Totals recounted:
  819. * - fixes for topics with wrong num_replies.
  820. * - updates for num_posts and num_topics of all boards.
  821. * - recounts instant_messages but not unread_messages.
  822. * - repairs messages pointing to boards with topics pointing to other boards.
  823. * - updates the last message posted in boards and children.
  824. * - updates member count, latest member, topic count, and message count.
  825. *
  826. * The function redirects back to ?action=admin;area=maintain when complete.
  827. * It is accessed via ?action=admin;area=maintain;sa=database;activity=recount.
  828. */
  829. function AdminBoardRecount()
  830. {
  831. global $txt, $context, $scripturl, $modSettings, $sourcedir;
  832. global $time_start, $smcFunc;
  833. isAllowedTo('admin_forum');
  834. checkSession('request');
  835. $context['page_title'] = $txt['not_done_title'];
  836. $context['continue_post_data'] = '';
  837. $context['continue_countdown'] = '3';
  838. $context['sub_template'] = 'not_done';
  839. // Try for as much time as possible.
  840. @set_time_limit(600);
  841. // Step the number of topics at a time so things don't time out...
  842. $request = $smcFunc['db_query']('', '
  843. SELECT MAX(id_topic)
  844. FROM {db_prefix}topics',
  845. array(
  846. )
  847. );
  848. list ($max_topics) = $smcFunc['db_fetch_row']($request);
  849. $smcFunc['db_free_result']($request);
  850. $increment = min(max(50, ceil($max_topics / 4)), 2000);
  851. if (empty($_REQUEST['start']))
  852. $_REQUEST['start'] = 0;
  853. $total_steps = 8;
  854. // Get each topic with a wrong reply count and fix it - let's just do some at a time, though.
  855. if (empty($_REQUEST['step']))
  856. {
  857. $_REQUEST['step'] = 0;
  858. while ($_REQUEST['start'] < $max_topics)
  859. {
  860. // Recount approved messages
  861. $request = $smcFunc['db_query']('', '
  862. SELECT /*!40001 SQL_NO_CACHE */ t.id_topic, MAX(t.num_replies) AS num_replies,
  863. CASE WHEN COUNT(ma.id_msg) >= 1 THEN COUNT(ma.id_msg) - 1 ELSE 0 END AS real_num_replies
  864. FROM {db_prefix}topics AS t
  865. LEFT JOIN {db_prefix}messages AS ma ON (ma.id_topic = t.id_topic AND ma.approved = {int:is_approved})
  866. WHERE t.id_topic > {int:start}
  867. AND t.id_topic <= {int:max_id}
  868. GROUP BY t.id_topic
  869. HAVING CASE WHEN COUNT(ma.id_msg) >= 1 THEN COUNT(ma.id_msg) - 1 ELSE 0 END != MAX(t.num_replies)',
  870. array(
  871. 'is_approved' => 1,
  872. 'start' => $_REQUEST['start'],
  873. 'max_id' => $_REQUEST['start'] + $increment,
  874. )
  875. );
  876. while ($row = $smcFunc['db_fetch_assoc']($request))
  877. $smcFunc['db_query']('', '
  878. UPDATE {db_prefix}topics
  879. SET num_replies = {int:num_replies}
  880. WHERE id_topic = {int:id_topic}',
  881. array(
  882. 'num_replies' => $row['real_num_replies'],
  883. 'id_topic' => $row['id_topic'],
  884. )
  885. );
  886. $smcFunc['db_free_result']($request);
  887. // Recount unapproved messages
  888. $request = $smcFunc['db_query']('', '
  889. SELECT /*!40001 SQL_NO_CACHE */ t.id_topic, MAX(t.unapproved_posts) AS unapproved_posts,
  890. COUNT(mu.id_msg) AS real_unapproved_posts
  891. FROM {db_prefix}topics AS t
  892. LEFT JOIN {db_prefix}messages AS mu ON (mu.id_topic = t.id_topic AND mu.approved = {int:not_approved})
  893. WHERE t.id_topic > {int:start}
  894. AND t.id_topic <= {int:max_id}
  895. GROUP BY t.id_topic
  896. HAVING COUNT(mu.id_msg) != MAX(t.unapproved_posts)',
  897. array(
  898. 'not_approved' => 0,
  899. 'start' => $_REQUEST['start'],
  900. 'max_id' => $_REQUEST['start'] + $increment,
  901. )
  902. );
  903. while ($row = $smcFunc['db_fetch_assoc']($request))
  904. $smcFunc['db_query']('', '
  905. UPDATE {db_prefix}topics
  906. SET unapproved_posts = {int:unapproved_posts}
  907. WHERE id_topic = {int:id_topic}',
  908. array(
  909. 'unapproved_posts' => $row['real_unapproved_posts'],
  910. 'id_topic' => $row['id_topic'],
  911. )
  912. );
  913. $smcFunc['db_free_result']($request);
  914. $_REQUEST['start'] += $increment;
  915. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  916. {
  917. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=0;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  918. $context['continue_percent'] = round((100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  919. return;
  920. }
  921. }
  922. $_REQUEST['start'] = 0;
  923. }
  924. // Update the post count of each board.
  925. if ($_REQUEST['step'] <= 1)
  926. {
  927. if (empty($_REQUEST['start']))
  928. $smcFunc['db_query']('', '
  929. UPDATE {db_prefix}boards
  930. SET num_posts = {int:num_posts}
  931. WHERE redirect = {string:redirect}',
  932. array(
  933. 'num_posts' => 0,
  934. 'redirect' => '',
  935. )
  936. );
  937. while ($_REQUEST['start'] < $max_topics)
  938. {
  939. $request = $smcFunc['db_query']('', '
  940. SELECT /*!40001 SQL_NO_CACHE */ m.id_board, COUNT(*) AS real_num_posts
  941. FROM {db_prefix}messages AS m
  942. WHERE m.id_topic > {int:id_topic_min}
  943. AND m.id_topic <= {int:id_topic_max}
  944. AND m.approved = {int:is_approved}
  945. GROUP BY m.id_board',
  946. array(
  947. 'id_topic_min' => $_REQUEST['start'],
  948. 'id_topic_max' => $_REQUEST['start'] + $increment,
  949. 'is_approved' => 1,
  950. )
  951. );
  952. while ($row = $smcFunc['db_fetch_assoc']($request))
  953. $smcFunc['db_query']('', '
  954. UPDATE {db_prefix}boards
  955. SET num_posts = num_posts + {int:real_num_posts}
  956. WHERE id_board = {int:id_board}',
  957. array(
  958. 'id_board' => $row['id_board'],
  959. 'real_num_posts' => $row['real_num_posts'],
  960. )
  961. );
  962. $smcFunc['db_free_result']($request);
  963. $_REQUEST['start'] += $increment;
  964. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  965. {
  966. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=1;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  967. $context['continue_percent'] = round((200 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  968. return;
  969. }
  970. }
  971. $_REQUEST['start'] = 0;
  972. }
  973. // Update the topic count of each board.
  974. if ($_REQUEST['step'] <= 2)
  975. {
  976. if (empty($_REQUEST['start']))
  977. $smcFunc['db_query']('', '
  978. UPDATE {db_prefix}boards
  979. SET num_topics = {int:num_topics}',
  980. array(
  981. 'num_topics' => 0,
  982. )
  983. );
  984. while ($_REQUEST['start'] < $max_topics)
  985. {
  986. $request = $smcFunc['db_query']('', '
  987. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, COUNT(*) AS real_num_topics
  988. FROM {db_prefix}topics AS t
  989. WHERE t.approved = {int:is_approved}
  990. AND t.id_topic > {int:id_topic_min}
  991. AND t.id_topic <= {int:id_topic_max}
  992. GROUP BY t.id_board',
  993. array(
  994. 'is_approved' => 1,
  995. 'id_topic_min' => $_REQUEST['start'],
  996. 'id_topic_max' => $_REQUEST['start'] + $increment,
  997. )
  998. );
  999. while ($row = $smcFunc['db_fetch_assoc']($request))
  1000. $smcFunc['db_query']('', '
  1001. UPDATE {db_prefix}boards
  1002. SET num_topics = num_topics + {int:real_num_topics}
  1003. WHERE id_board = {int:id_board}',
  1004. array(
  1005. 'id_board' => $row['id_board'],
  1006. 'real_num_topics' => $row['real_num_topics'],
  1007. )
  1008. );
  1009. $smcFunc['db_free_result']($request);
  1010. $_REQUEST['start'] += $increment;
  1011. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1012. {
  1013. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=2;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1014. $context['continue_percent'] = round((300 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1015. return;
  1016. }
  1017. }
  1018. $_REQUEST['start'] = 0;
  1019. }
  1020. // Update the unapproved post count of each board.
  1021. if ($_REQUEST['step'] <= 3)
  1022. {
  1023. if (empty($_REQUEST['start']))
  1024. $smcFunc['db_query']('', '
  1025. UPDATE {db_prefix}boards
  1026. SET unapproved_posts = {int:unapproved_posts}',
  1027. array(
  1028. 'unapproved_posts' => 0,
  1029. )
  1030. );
  1031. while ($_REQUEST['start'] < $max_topics)
  1032. {
  1033. $request = $smcFunc['db_query']('', '
  1034. SELECT /*!40001 SQL_NO_CACHE */ m.id_board, COUNT(*) AS real_unapproved_posts
  1035. FROM {db_prefix}messages AS m
  1036. WHERE m.id_topic > {int:id_topic_min}
  1037. AND m.id_topic <= {int:id_topic_max}
  1038. AND m.approved = {int:is_approved}
  1039. GROUP BY m.id_board',
  1040. array(
  1041. 'id_topic_min' => $_REQUEST['start'],
  1042. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1043. 'is_approved' => 0,
  1044. )
  1045. );
  1046. while ($row = $smcFunc['db_fetch_assoc']($request))
  1047. $smcFunc['db_query']('', '
  1048. UPDATE {db_prefix}boards
  1049. SET unapproved_posts = unapproved_posts + {int:unapproved_posts}
  1050. WHERE id_board = {int:id_board}',
  1051. array(
  1052. 'id_board' => $row['id_board'],
  1053. 'unapproved_posts' => $row['real_unapproved_posts'],
  1054. )
  1055. );
  1056. $smcFunc['db_free_result']($request);
  1057. $_REQUEST['start'] += $increment;
  1058. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1059. {
  1060. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=3;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1061. $context['continue_percent'] = round((400 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1062. return;
  1063. }
  1064. }
  1065. $_REQUEST['start'] = 0;
  1066. }
  1067. // Update the unapproved topic count of each board.
  1068. if ($_REQUEST['step'] <= 4)
  1069. {
  1070. if (empty($_REQUEST['start']))
  1071. $smcFunc['db_query']('', '
  1072. UPDATE {db_prefix}boards
  1073. SET unapproved_topics = {int:unapproved_topics}',
  1074. array(
  1075. 'unapproved_topics' => 0,
  1076. )
  1077. );
  1078. while ($_REQUEST['start'] < $max_topics)
  1079. {
  1080. $request = $smcFunc['db_query']('', '
  1081. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, COUNT(*) AS real_unapproved_topics
  1082. FROM {db_prefix}topics AS t
  1083. WHERE t.approved = {int:is_approved}
  1084. AND t.id_topic > {int:id_topic_min}
  1085. AND t.id_topic <= {int:id_topic_max}
  1086. GROUP BY t.id_board',
  1087. array(
  1088. 'is_approved' => 0,
  1089. 'id_topic_min' => $_REQUEST['start'],
  1090. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1091. )
  1092. );
  1093. while ($row = $smcFunc['db_fetch_assoc']($request))
  1094. $smcFunc['db_query']('', '
  1095. UPDATE {db_prefix}boards
  1096. SET unapproved_topics = unapproved_topics + {int:real_unapproved_topics}
  1097. WHERE id_board = {int:id_board}',
  1098. array(
  1099. 'id_board' => $row['id_board'],
  1100. 'real_unapproved_topics' => $row['real_unapproved_topics'],
  1101. )
  1102. );
  1103. $smcFunc['db_free_result']($request);
  1104. $_REQUEST['start'] += $increment;
  1105. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1106. {
  1107. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=4;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1108. $context['continue_percent'] = round((500 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1109. return;
  1110. }
  1111. }
  1112. $_REQUEST['start'] = 0;
  1113. }
  1114. // Get all members with wrong number of personal messages.
  1115. if ($_REQUEST['step'] <= 5)
  1116. {
  1117. $request = $smcFunc['db_query']('', '
  1118. SELECT /*!40001 SQL_NO_CACHE */ mem.id_member, COUNT(pmr.id_pm) AS real_num,
  1119. MAX(mem.instant_messages) AS instant_messages
  1120. FROM {db_prefix}members AS mem
  1121. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted})
  1122. GROUP BY mem.id_member
  1123. HAVING COUNT(pmr.id_pm) != MAX(mem.instant_messages)',
  1124. array(
  1125. 'is_not_deleted' => 0,
  1126. )
  1127. );
  1128. while ($row = $smcFunc['db_fetch_assoc']($request))
  1129. updateMemberData($row['id_member'], array('instant_messages' => $row['real_num']));
  1130. $smcFunc['db_free_result']($request);
  1131. $request = $smcFunc['db_query']('', '
  1132. SELECT /*!40001 SQL_NO_CACHE */ mem.id_member, COUNT(pmr.id_pm) AS real_num,
  1133. MAX(mem.unread_messages) AS unread_messages
  1134. FROM {db_prefix}members AS mem
  1135. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted} AND pmr.is_read = {int:is_not_read})
  1136. GROUP BY mem.id_member
  1137. HAVING COUNT(pmr.id_pm) != MAX(mem.unread_messages)',
  1138. array(
  1139. 'is_not_deleted' => 0,
  1140. 'is_not_read' => 0,
  1141. )
  1142. );
  1143. while ($row = $smcFunc['db_fetch_assoc']($request))
  1144. updateMemberData($row['id_member'], array('unread_messages' => $row['real_num']));
  1145. $smcFunc['db_free_result']($request);
  1146. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1147. {
  1148. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=0;' . $context['session_var'] . '=' . $context['session_id'];
  1149. $context['continue_percent'] = round(700 / $total_steps);
  1150. return;
  1151. }
  1152. }
  1153. // Any messages pointing to the wrong board?
  1154. if ($_REQUEST['step'] <= 6)
  1155. {
  1156. while ($_REQUEST['start'] < $modSettings['maxMsgID'])
  1157. {
  1158. $request = $smcFunc['db_query']('', '
  1159. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, m.id_msg
  1160. FROM {db_prefix}messages AS m
  1161. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_board != m.id_board)
  1162. WHERE m.id_msg > {int:id_msg_min}
  1163. AND m.id_msg <= {int:id_msg_max}',
  1164. array(
  1165. 'id_msg_min' => $_REQUEST['start'],
  1166. 'id_msg_max' => $_REQUEST['start'] + $increment,
  1167. )
  1168. );
  1169. $boards = array();
  1170. while ($row = $smcFunc['db_fetch_assoc']($request))
  1171. $boards[$row['id_board']][] = $row['id_msg'];
  1172. $smcFunc['db_free_result']($request);
  1173. foreach ($boards as $board_id => $messages)
  1174. $smcFunc['db_query']('', '
  1175. UPDATE {db_prefix}messages
  1176. SET id_board = {int:id_board}
  1177. WHERE id_msg IN ({array_int:id_msg_array})',
  1178. array(
  1179. 'id_msg_array' => $messages,
  1180. 'id_board' => $board_id,
  1181. )
  1182. );
  1183. $_REQUEST['start'] += $increment;
  1184. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1185. {
  1186. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1187. $context['continue_percent'] = round((700 + 100 * $_REQUEST['start'] / $modSettings['maxMsgID']) / $total_steps);
  1188. return;
  1189. }
  1190. }
  1191. $_REQUEST['start'] = 0;
  1192. }
  1193. // Update the latest message of each board.
  1194. $request = $smcFunc['db_query']('', '
  1195. SELECT m.id_board, MAX(m.id_msg) AS local_last_msg
  1196. FROM {db_prefix}messages AS m
  1197. WHERE m.approved = {int:is_approved}
  1198. GROUP BY m.id_board',
  1199. array(
  1200. 'is_approved' => 1,
  1201. )
  1202. );
  1203. $realBoardCounts = array();
  1204. while ($row = $smcFunc['db_fetch_assoc']($request))
  1205. $realBoardCounts[$row['id_board']] = $row['local_last_msg'];
  1206. $smcFunc['db_free_result']($request);
  1207. $request = $smcFunc['db_query']('', '
  1208. SELECT /*!40001 SQL_NO_CACHE */ id_board, id_parent, id_last_msg, child_level, id_msg_updated
  1209. FROM {db_prefix}boards',
  1210. array(
  1211. )
  1212. );
  1213. $resort_me = array();
  1214. while ($row = $smcFunc['db_fetch_assoc']($request))
  1215. {
  1216. $row['local_last_msg'] = isset($realBoardCounts[$row['id_board']]) ? $realBoardCounts[$row['id_board']] : 0;
  1217. $resort_me[$row['child_level']][] = $row;
  1218. }
  1219. $smcFunc['db_free_result']($request);
  1220. krsort($resort_me);
  1221. $lastModifiedMsg = array();
  1222. foreach ($resort_me as $rows)
  1223. foreach ($rows as $row)
  1224. {
  1225. // The latest message is the latest of the current board and its children.
  1226. if (isset($lastModifiedMsg[$row['id_board']]))
  1227. $curLastModifiedMsg = max($row['local_last_msg'], $lastModifiedMsg[$row['id_board']]);
  1228. else
  1229. $curLastModifiedMsg = $row['local_last_msg'];
  1230. // If what is and what should be the latest message differ, an update is necessary.
  1231. if ($row['local_last_msg'] != $row['id_last_msg'] || $curLastModifiedMsg != $row['id_msg_updated'])
  1232. $smcFunc['db_query']('', '
  1233. UPDATE {db_prefix}boards
  1234. SET id_last_msg = {int:id_last_msg}, id_msg_updated = {int:id_msg_updated}
  1235. WHERE id_board = {int:id_board}',
  1236. array(
  1237. 'id_last_msg' => $row['local_last_msg'],
  1238. 'id_msg_updated' => $curLastModifiedMsg,
  1239. 'id_board' => $row['id_board'],
  1240. )
  1241. );
  1242. // Parent boards inherit the latest modified message of their children.
  1243. if (isset($lastModifiedMsg[$row['id_parent']]))
  1244. $lastModifiedMsg[$row['id_parent']] = max($row['local_last_msg'], $lastModifiedMsg[$row['id_parent']]);
  1245. else
  1246. $lastModifiedMsg[$row['id_parent']] = $row['local_last_msg'];
  1247. }
  1248. // Update all the basic statistics.
  1249. updateStats('member');
  1250. updateStats('message');
  1251. updateStats('topic');
  1252. // Finally, update the latest event times.
  1253. require_once($sourcedir . '/ScheduledTasks.php');
  1254. CalculateNextTrigger();
  1255. redirectexit('action=admin;area=maintain;sa=routine;done=recount');
  1256. }
  1257. /**
  1258. * Perform a detailed version check. A very good thing ;).
  1259. * The function parses the comment headers in all files for their version information,
  1260. * and outputs that for some javascript to check with simplemachines.org.
  1261. * It does not connect directly with simplemachines.org, but rather expects the client to.
  1262. *
  1263. * It requires the admin_forum permission.
  1264. * Uses the view_versions admin area.
  1265. * Accessed through ?action=admin;area=maintain;sa=routine;activity=version.
  1266. * @uses Admin template, view_versions sub-template.
  1267. */
  1268. function VersionDetail()
  1269. {
  1270. global $forum_version, $txt, $sourcedir, $context;
  1271. isAllowedTo('admin_forum');
  1272. // Call the function that'll get all the version info we need.
  1273. require_once($sourcedir . '/Subs-Admin.php');
  1274. $versionOptions = array(
  1275. 'include_ssi' => true,
  1276. 'include_subscriptions' => true,
  1277. 'sort_results' => true,
  1278. );
  1279. $version_info = getFileVersions($versionOptions);
  1280. // Add the new info to the template context.
  1281. $context += array(
  1282. 'file_versions' => $version_info['file_versions'],
  1283. 'default_template_versions' => $version_info['default_template_versions'],
  1284. 'template_versions' => $version_info['template_versions'],
  1285. 'default_language_versions' => $version_info['default_language_versions'],
  1286. 'default_known_languages' => array_keys($version_info['default_language_versions']),
  1287. );
  1288. // Make it easier to manage for the template.
  1289. $context['forum_version'] = $forum_version;
  1290. $context['sub_template'] = 'view_versions';
  1291. $context['page_title'] = $txt['admin_version_check'];
  1292. }
  1293. /**
  1294. * Re-attribute posts.
  1295. */
  1296. function MaintainReattributePosts()
  1297. {
  1298. global $sourcedir, $context, $txt;
  1299. checkSession();
  1300. // Find the member.
  1301. require_once($sourcedir . '/Subs-Auth.php');
  1302. $members = findMembers($_POST['to']);
  1303. if (empty($members))
  1304. fatal_lang_error('reattribute_cannot_find_member');
  1305. $memID = array_shift($members);
  1306. $memID = $memID['id'];
  1307. $email = $_POST['type'] == 'email' ? $_POST['from_email'] : '';
  1308. $membername = $_POST['type'] == 'name' ? $_POST['from_name'] : '';
  1309. // Now call the reattribute function.
  1310. require_once($sourcedir . '/Subs-Members.php');
  1311. reattributePosts($memID, $email, $membername, !empty($_POST['posts']));
  1312. $context['maintenance_finished'] = $txt['maintain_reattribute_posts'];
  1313. }
  1314. /**
  1315. * Handling function for the backup stuff.
  1316. */
  1317. function MaintainDownloadBackup()
  1318. {
  1319. global $sourcedir;
  1320. require_once($sourcedir . '/DumpDatabase.php');
  1321. DumpDatabase2();
  1322. }
  1323. /**
  1324. * Removing old members. Done and out!
  1325. * @todo refactor
  1326. */
  1327. function MaintainPurgeInactiveMembers()
  1328. {
  1329. global $sourcedir, $context, $smcFunc, $txt;
  1330. $_POST['maxdays'] = (int) $_POST['maxdays'];
  1331. if (!empty($_POST['groups']) && $_POST['maxdays'])
  1332. {
  1333. checkSession();
  1334. $groups = array();
  1335. foreach ($_POST['groups'] as $id => $dummy)
  1336. $groups[] = (int) $id;
  1337. $time_limit = (time() - ($_POST['maxdays'] * 24 * 3600));
  1338. $where_vars = array(
  1339. 'time_limit' => $time_limit,
  1340. );
  1341. if ($_POST['del_type'] == 'activated')
  1342. {
  1343. $where = 'mem.date_registered < {int:time_limit} AND mem.is_activated = {int:is_activated}';
  1344. $where_vars['is_activated'] = 0;
  1345. }
  1346. else
  1347. $where = 'mem.last_login < {int:time_limit}';
  1348. // Need to get *all* groups then work out which (if any) we avoid.
  1349. $request = $smcFunc['db_query']('', '
  1350. SELECT id_group, group_name, min_posts
  1351. FROM {db_prefix}membergroups',
  1352. array(
  1353. )
  1354. );
  1355. while ($row = $smcFunc['db_fetch_assoc']($request))
  1356. {
  1357. // Avoid this one?
  1358. if (!in_array($row['id_group'], $groups))
  1359. {
  1360. // Post group?
  1361. if ($row['min_posts'] != -1)
  1362. {
  1363. $where .= ' AND mem.id_post_group != {int:id_post_group_' . $row['id_group'] . '}';
  1364. $where_vars['id_post_group_' . $row['id_group']] = $row['id_group'];
  1365. }
  1366. else
  1367. {
  1368. $where .= ' AND mem.id_group != {int:id_group_' . $row['id_group'] . '} AND FIND_IN_SET({int:id_group_' . $row['id_group'] . '}, mem.additional_groups) = 0';
  1369. $where_vars['id_group_' . $row['id_group']] = $row['id_group'];
  1370. }
  1371. }
  1372. }
  1373. $smcFunc['db_free_result']($request);
  1374. // If we have ungrouped unselected we need to avoid those guys.
  1375. if (!in_array(0, $groups))
  1376. {
  1377. $where .= ' AND (mem.id_group != 0 OR mem.additional_groups != {string:blank_add_groups})';
  1378. $where_vars['blank_add_groups'] = '';
  1379. }
  1380. // Select all the members we're about to murder/remove...
  1381. $request = $smcFunc['db_query']('', '
  1382. SELECT mem.id_member, IFNULL(m.id_member, 0) AS is_mod
  1383. FROM {db_prefix}members AS mem
  1384. LEFT JOIN {db_prefix}moderators AS m ON (m.id_member = mem.id_member)
  1385. WHERE ' . $where,
  1386. $where_vars
  1387. );
  1388. $members = array();
  1389. while ($row = $smcFunc['db_fetch_assoc']($request))
  1390. {
  1391. if (!$row['is_mod'] || !in_array(3, $groups))
  1392. $members[] = $row['id_member'];
  1393. }
  1394. $smcFunc['db_free_result']($request);
  1395. require_once($sourcedir . '/Subs-Members.php');
  1396. deleteMembers($members);
  1397. }
  1398. $context['maintenance_finished'] = $txt['maintain_members'];
  1399. }
  1400. /**
  1401. * Removing old posts doesn't take much as we really pass through.
  1402. */
  1403. function MaintainRemoveOldPosts()
  1404. {
  1405. global $sourcedir, $context, $txt;
  1406. // Actually do what we're told!
  1407. require_once($sourcedir . '/RemoveTopic.php');
  1408. RemoveOldTopics2();
  1409. }
  1410. /**
  1411. * Moves topics from one board to another.
  1412. *
  1413. * @uses not_done template to pause the process.
  1414. */
  1415. function MaintainMassMoveTopics()
  1416. {
  1417. global $smcFunc, $sourcedir, $context, $txt;
  1418. // Only admins.
  1419. isAllowedTo('admin_forum');
  1420. checkSession('request');
  1421. // Set up to the context.
  1422. $context['page_title'] = $txt['not_done_title'];
  1423. $context['continue_countdown'] = '3';
  1424. $context['continue_post_data'] = '';
  1425. $context['continue_get_data'] = '';
  1426. $context['sub_template'] = 'not_done';
  1427. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  1428. $context['start_time'] = time();
  1429. // First time we do this?
  1430. $id_board_from = isset($_POST['id_board_from']) ? (int) $_POST['id_board_from'] : (int) $_REQUEST['id_board_from'];
  1431. $id_board_to = isset($_POST['id_board_to']) ? (int) $_POST['id_board_to'] : (int) $_REQUEST['id_board_to'];
  1432. // No boards then this is your stop.
  1433. if (empty($id_board_from) || empty($id_board_to))
  1434. return;
  1435. // How many topics are we converting?
  1436. if (!isset($_REQUEST['totaltopics']))
  1437. {
  1438. $request = $smcFunc['db_query']('', '
  1439. SELECT COUNT(*)
  1440. FROM {db_prefix}topics
  1441. WHERE id_board = {int:id_board_from}',
  1442. array(
  1443. 'id_board_from' => $id_board_from,
  1444. )
  1445. );
  1446. list ($total_topics) = $smcFunc['db_fetch_row']($request);
  1447. $smcFunc['db_free_result']($request);
  1448. }
  1449. else
  1450. $total_topics = (int) $_REQUEST['totaltopics'];
  1451. // Seems like we need this here.
  1452. $context['continue_get_data'] = '?action=admin;area=maintain;sa=topics;activity=massmove;id_board_from=' . $id_board_from . ';id_board_to=' . $id_board_to . ';totaltopics=' . $total_topics . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1453. // We have topics to move so start the process.
  1454. if (!empty($total_topics))
  1455. {
  1456. while ($context['start'] <= $total_topics)
  1457. {
  1458. // Lets get the topics.
  1459. $request = $smcFunc['db_query']('', '
  1460. SELECT id_topic
  1461. FROM {db_prefix}topics
  1462. WHERE id_board = {int:id_board_from}
  1463. LIMIT 10',
  1464. array(
  1465. 'id_board_from' => $id_board_from,
  1466. )
  1467. );
  1468. // Get the ids.
  1469. $topics = array();
  1470. while ($row = $smcFunc['db_fetch_assoc']($request))
  1471. $topics[] = $row['id_topic'];
  1472. // Just return if we don't have any topics left to move.
  1473. if (empty($topics))
  1474. {
  1475. cache_put_data('board-' . $id_board_from, null, 120);
  1476. cache_put_data('board-' . $id_board_to, null, 120);
  1477. redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
  1478. }
  1479. // Lets move them.
  1480. require_once($sourcedir . '/MoveTopic.php');
  1481. moveTopics($topics, $id_board_to);
  1482. // We've done at least ten more topics.
  1483. $context['start'] += 10;
  1484. // Lets wait a while.
  1485. if (time() - $context['start_time'] > 3)
  1486. {
  1487. // What's the percent?
  1488. $context['continue_percent'] = round(100 * ($context['start'] / $total_topics), 1);
  1489. $context['continue_get_data'] = '?action=admin;area=maintain;sa=topics;activity=massmove;id_board_from=' . $id_board_from . ';id_board_to=' . $id_board_to . ';totaltopics=' . $total_topics . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1490. // Let the template system do it's thang.
  1491. return;
  1492. }
  1493. }
  1494. }
  1495. // Don't confuse admins by having an out of date cache.
  1496. cache_put_data('board-' . $id_board_from, null, 120);
  1497. cache_put_data('board-' . $id_board_to, null, 120);
  1498. redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
  1499. }
  1500. ?>