ManageMaintenance.php 57 KB

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