ManageMaintenance.php 57 KB

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