ManageMaintenance.php 64 KB

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