ManageMaintenance.php 57 KB

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