ManageMaintenance.php 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  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 2014 Simple Machines and individual contributors
  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('No direct access...');
  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, $scripturl, $context;
  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. 'convertentities' => 'ConvertEntities',
  58. 'convertutf8' => 'ConvertUtf8',
  59. 'convertmsgbody' => 'ConvertMsgBody',
  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. 'olddrafts' => 'MaintainRemoveOldDrafts',
  78. ),
  79. ),
  80. 'hooks' => array(
  81. 'function' => 'list_integration_hooks',
  82. ),
  83. 'destroy' => array(
  84. 'function' => 'Destroy',
  85. 'activities' => array(),
  86. ),
  87. );
  88. call_integration_hook('integrate_manage_maintenance', array(&$subActions));
  89. // Yep, sub-action time!
  90. if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
  91. $subAction = $_REQUEST['sa'];
  92. else
  93. $subAction = 'routine';
  94. // Doing something special?
  95. if (isset($_REQUEST['activity']) && isset($subActions[$subAction]['activities'][$_REQUEST['activity']]))
  96. $activity = $_REQUEST['activity'];
  97. // Set a few things.
  98. $context['page_title'] = $txt['maintain_title'];
  99. $context['sub_action'] = $subAction;
  100. $context['sub_template'] = !empty($subActions[$subAction]['template']) ? $subActions[$subAction]['template'] : '';
  101. // Finally fall through to what we are doing.
  102. $subActions[$subAction]['function']();
  103. // Any special activity?
  104. if (isset($activity))
  105. $subActions[$subAction]['activities'][$activity]();
  106. //converted to UTF-8? show a small maintenance info
  107. if (isset($_GET['done']) && $_GET['done'] == 'convertutf8')
  108. $context['maintenance_finished'] = $txt['utf8_title'];
  109. // Create a maintenance token. Kinda hard to do it any other way.
  110. createToken('admin-maint');
  111. }
  112. /**
  113. * Supporting function for the database maintenance area.
  114. */
  115. function MaintainDatabase()
  116. {
  117. global $context, $db_type, $db_character_set, $modSettings, $smcFunc, $txt, $maintenance;
  118. // Show some conversion options?
  119. $context['convert_utf8'] = ($db_type == 'mysql' || $db_type == 'mysqli') && (!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']()), '<=');
  120. $context['convert_entities'] = ($db_type == 'mysql' || $db_type == 'mysqli') && isset($db_character_set, $modSettings['global_character_set']) && $db_character_set === 'utf8' && $modSettings['global_character_set'] === 'UTF-8';
  121. if ($db_type == 'mysql' || $db_type == 'mysqli')
  122. {
  123. db_extend('packages');
  124. $colData = $smcFunc['db_list_columns']('{db_prefix}messages', true);
  125. foreach ($colData as $column)
  126. if ($column['name'] == 'body')
  127. $body_type = $column['type'];
  128. $context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text';
  129. $context['convert_to_suggest'] = ($body_type != 'text' && !empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] < 65536);
  130. }
  131. if (isset($_GET['done']) && $_GET['done'] == 'convertutf8')
  132. $context['maintenance_finished'] = $txt['utf8_title'];
  133. if (isset($_GET['done']) && $_GET['done'] == 'convertentities')
  134. $context['maintenance_finished'] = $txt['entity_convert_title'];
  135. }
  136. /**
  137. * Supporting function for the routine maintenance area.
  138. */
  139. function MaintainRoutine()
  140. {
  141. global $context, $txt;
  142. if (isset($_GET['done']) && $_GET['done'] == 'recount')
  143. $context['maintenance_finished'] = $txt['maintain_recount'];
  144. }
  145. /**
  146. * Supporting function for the members maintenance area.
  147. */
  148. function MaintainMembers()
  149. {
  150. global $context, $smcFunc, $txt;
  151. // Get membergroups - for deleting members and the like.
  152. $result = $smcFunc['db_query']('', '
  153. SELECT id_group, group_name
  154. FROM {db_prefix}membergroups',
  155. array(
  156. )
  157. );
  158. $context['membergroups'] = array(
  159. array(
  160. 'id' => 0,
  161. 'name' => $txt['maintain_members_ungrouped']
  162. ),
  163. );
  164. while ($row = $smcFunc['db_fetch_assoc']($result))
  165. {
  166. $context['membergroups'][] = array(
  167. 'id' => $row['id_group'],
  168. 'name' => $row['group_name']
  169. );
  170. }
  171. $smcFunc['db_free_result']($result);
  172. if (isset($_GET['done']) && $_GET['done'] == 'recountposts')
  173. $context['maintenance_finished'] = $txt['maintain_recountposts'];
  174. loadJavascriptFile('suggest.js', array('default_theme' => true, 'defer' => false), 'smf_suggest');
  175. }
  176. /**
  177. * Supporting function for the topics maintenance area.
  178. */
  179. function MaintainTopics()
  180. {
  181. global $context, $smcFunc, $txt;
  182. // Let's load up the boards in case they are useful.
  183. $result = $smcFunc['db_query']('order_by_board_order', '
  184. SELECT b.id_board, b.name, b.child_level, c.name AS cat_name, c.id_cat
  185. FROM {db_prefix}boards AS b
  186. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  187. WHERE {query_see_board}
  188. AND redirect = {string:blank_redirect}',
  189. array(
  190. 'blank_redirect' => '',
  191. )
  192. );
  193. $context['categories'] = array();
  194. while ($row = $smcFunc['db_fetch_assoc']($result))
  195. {
  196. if (!isset($context['categories'][$row['id_cat']]))
  197. $context['categories'][$row['id_cat']] = array(
  198. 'name' => $row['cat_name'],
  199. 'boards' => array()
  200. );
  201. $context['categories'][$row['id_cat']]['boards'][] = array(
  202. 'id' => $row['id_board'],
  203. 'name' => $row['name'],
  204. 'child_level' => $row['child_level']
  205. );
  206. }
  207. $smcFunc['db_free_result']($result);
  208. if (isset($_GET['done']) && $_GET['done'] == 'purgeold')
  209. $context['maintenance_finished'] = $txt['maintain_old'];
  210. elseif (isset($_GET['done']) && $_GET['done'] == 'massmove')
  211. $context['maintenance_finished'] = $txt['move_topics_maintenance'];
  212. }
  213. /**
  214. * Find and fix all errors on the forum.
  215. */
  216. function MaintainFindFixErrors()
  217. {
  218. global $sourcedir;
  219. // Honestly, this should be done in the sub function.
  220. validateToken('admin-maint');
  221. require_once($sourcedir . '/RepairBoards.php');
  222. RepairBoards();
  223. }
  224. /**
  225. * Wipes the whole cache directory.
  226. * This only applies to SMF's own cache directory, though.
  227. */
  228. function MaintainCleanCache()
  229. {
  230. global $context, $txt;
  231. checkSession();
  232. validateToken('admin-maint');
  233. // Just wipe the whole cache directory!
  234. clean_cache();
  235. $context['maintenance_finished'] = $txt['maintain_cache'];
  236. }
  237. /**
  238. * Empties all uninmportant logs
  239. */
  240. function MaintainEmptyUnimportantLogs()
  241. {
  242. global $context, $smcFunc, $txt;
  243. checkSession();
  244. validateToken('admin-maint');
  245. // No one's online now.... MUHAHAHAHA :P.
  246. $smcFunc['db_query']('', '
  247. DELETE FROM {db_prefix}log_online');
  248. // Dump the banning logs.
  249. $smcFunc['db_query']('', '
  250. DELETE FROM {db_prefix}log_banned');
  251. // Start id_error back at 0 and dump the error log.
  252. $smcFunc['db_query']('truncate_table', '
  253. TRUNCATE {db_prefix}log_errors');
  254. // Clear out the spam log.
  255. $smcFunc['db_query']('', '
  256. DELETE FROM {db_prefix}log_floodcontrol');
  257. // Clear out the karma actions.
  258. $smcFunc['db_query']('', '
  259. DELETE FROM {db_prefix}log_karma');
  260. // Last but not least, the search logs!
  261. $smcFunc['db_query']('truncate_table', '
  262. TRUNCATE {db_prefix}log_search_topics');
  263. $smcFunc['db_query']('truncate_table', '
  264. TRUNCATE {db_prefix}log_search_messages');
  265. $smcFunc['db_query']('truncate_table', '
  266. TRUNCATE {db_prefix}log_search_results');
  267. updateSettings(array('search_pointer' => 0));
  268. $context['maintenance_finished'] = $txt['maintain_logs'];
  269. }
  270. /**
  271. * Oh noes! I'd document this but that would give it away
  272. */
  273. function Destroy()
  274. {
  275. global $context;
  276. echo '<!DOCTYPE html>
  277. <html', $context['right_to_left'] ? ' dir="rtl"' : '', '><head><title>', $context['forum_name_html_safe'], ' deleted!</title></head>
  278. <body style="background-color: orange; font-family: arial, sans-serif; text-align: center;">
  279. <div style="margin-top: 8%; font-size: 400%; color: black;">Oh my, you killed ', $context['forum_name_html_safe'], '!</div>
  280. <div style="margin-top: 7%; font-size: 500%; color: red;"><strong>You lazy bum!</strong></div>
  281. </body></html>';
  282. obExit(false);
  283. }
  284. /**
  285. * Convert both data and database tables to UTF-8 character set.
  286. * It requires the admin_forum permission.
  287. * This only works if UTF-8 is not the global character set.
  288. * It supports all character sets used by SMF's language files.
  289. * It redirects to ?action=admin;area=maintain after finishing.
  290. * This action is linked from the maintenance screen (if it's applicable).
  291. * Accessed by ?action=admin;area=maintain;sa=database;activity=convertutf8.
  292. *
  293. * @uses the convert_utf8 sub template of the Admin template.
  294. */
  295. function ConvertUtf8()
  296. {
  297. global $scripturl, $context, $txt, $language, $db_character_set;
  298. global $modSettings, $user_info, $sourcedir, $smcFunc, $db_prefix;
  299. // Show me your badge!
  300. isAllowedTo('admin_forum');
  301. // The character sets used in SMF's language files with their db equivalent.
  302. $charsets = array(
  303. // Chinese-traditional.
  304. 'big5' => 'big5',
  305. // Chinese-simplified.
  306. 'gbk' => 'gbk',
  307. // West European.
  308. 'ISO-8859-1' => 'latin1',
  309. // Romanian.
  310. 'ISO-8859-2' => 'latin2',
  311. // Turkish.
  312. 'ISO-8859-9' => 'latin5',
  313. // West European with Euro sign.
  314. 'ISO-8859-15' => 'latin9',
  315. // Thai.
  316. 'tis-620' => 'tis620',
  317. // Persian, Chinese, etc.
  318. 'UTF-8' => 'utf8',
  319. // Russian.
  320. 'windows-1251' => 'cp1251',
  321. // Greek.
  322. 'windows-1253' => 'utf8',
  323. // Hebrew.
  324. 'windows-1255' => 'utf8',
  325. // Arabic.
  326. 'windows-1256' => 'cp1256',
  327. );
  328. // Get a list of character sets supported by your MySQL server.
  329. $request = $smcFunc['db_query']('', '
  330. SHOW CHARACTER SET',
  331. array(
  332. )
  333. );
  334. $db_charsets = array();
  335. while ($row = $smcFunc['db_fetch_assoc']($request))
  336. $db_charsets[] = $row['Charset'];
  337. $smcFunc['db_free_result']($request);
  338. // Character sets supported by both MySQL and SMF's language files.
  339. $charsets = array_intersect($charsets, $db_charsets);
  340. // This is for the first screen telling backups is good.
  341. if (!isset($_POST['proceed']))
  342. {
  343. validateToken('admin-maint');
  344. // Character set conversions are only supported as of MySQL 4.1.2.
  345. if (version_compare('4.1.2', preg_replace('~\-.+?$~', '', $smcFunc['db_server_info']()), '>'))
  346. fatal_lang_error('utf8_db_version_too_low');
  347. // Use the messages.body column as indicator for the database charset.
  348. $request = $smcFunc['db_query']('', '
  349. SHOW FULL COLUMNS
  350. FROM {db_prefix}messages
  351. LIKE {string:body_like}',
  352. array(
  353. 'body_like' => 'body',
  354. )
  355. );
  356. $column_info = $smcFunc['db_fetch_assoc']($request);
  357. $smcFunc['db_free_result']($request);
  358. // A collation looks like latin1_swedish. We only need the character set.
  359. list($context['database_charset']) = explode('_', $column_info['Collation']);
  360. $context['database_charset'] = in_array($context['database_charset'], $charsets) ? array_search($context['database_charset'], $charsets) : $context['database_charset'];
  361. // No need to convert to UTF-8 if it already is.
  362. if ($db_character_set === 'utf8' && !empty($modSettings['global_character_set']) && $modSettings['global_character_set'] === 'UTF-8')
  363. fatal_lang_error('utf8_already_utf8');
  364. // Detect whether a fulltext index is set.
  365. db_extend('search');
  366. if ($smcFunc['db_search_support']('fulltext'))
  367. {
  368. require_once($sourcedir . '/ManageSearch.php');
  369. detectFulltextIndex();
  370. }
  371. // Cannot do conversion if using a fulltext index
  372. if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext' || !empty($context['fulltext_index']))
  373. fatal_lang_error('utf8_cannot_convert_fulltext');
  374. // Grab the character set from the default language file.
  375. loadLanguage('index', $language, true);
  376. $context['charset_detected'] = $txt['lang_character_set'];
  377. $context['charset_about_detected'] = sprintf($txt['utf8_detected_charset'], $language, $context['charset_detected']);
  378. // Go back to your own language.
  379. loadLanguage('index', $user_info['language'], true);
  380. // Show a warning if the character set seems not to be supported.
  381. if (!isset($charsets[strtr(strtolower($context['charset_detected']), array('utf' => 'UTF', 'iso' => 'ISO'))]))
  382. {
  383. $context['charset_warning'] = sprintf($txt['utf8_charset_not_supported'], $txt['lang_character_set']);
  384. // Default to ISO-8859-1.
  385. $context['charset_detected'] = 'ISO-8859-1';
  386. }
  387. $context['charset_list'] = array_keys($charsets);
  388. $context['page_title'] = $txt['utf8_title'];
  389. $context['sub_template'] = 'convert_utf8';
  390. createToken('admin-maint');
  391. return;
  392. }
  393. // After this point we're starting the conversion. But first: session check.
  394. checkSession();
  395. validateToken('admin-maint');
  396. createToken('admin-maint');
  397. // Translation table for the character sets not native for MySQL.
  398. $translation_tables = array(
  399. 'windows-1255' => array(
  400. '0x81' => '\'\'', '0x8A' => '\'\'', '0x8C' => '\'\'',
  401. '0x8D' => '\'\'', '0x8E' => '\'\'', '0x8F' => '\'\'',
  402. '0x90' => '\'\'', '0x9A' => '\'\'', '0x9C' => '\'\'',
  403. '0x9D' => '\'\'', '0x9E' => '\'\'', '0x9F' => '\'\'',
  404. '0xCA' => '\'\'', '0xD9' => '\'\'', '0xDA' => '\'\'',
  405. '0xDB' => '\'\'', '0xDC' => '\'\'', '0xDD' => '\'\'',
  406. '0xDE' => '\'\'', '0xDF' => '\'\'', '0xFB' => '\'\'',
  407. '0xFC' => '\'\'', '0xFF' => '\'\'', '0xC2' => '0xFF',
  408. '0x80' => '0xFC', '0xE2' => '0xFB', '0xA0' => '0xC2A0',
  409. '0xA1' => '0xC2A1', '0xA2' => '0xC2A2', '0xA3' => '0xC2A3',
  410. '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7',
  411. '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB',
  412. '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE',
  413. '0xAF' => '0xC2AF', '0xB0' => '0xC2B0', '0xB1' => '0xC2B1',
  414. '0xB2' => '0xC2B2', '0xB3' => '0xC2B3', '0xB4' => '0xC2B4',
  415. '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7',
  416. '0xB8' => '0xC2B8', '0xB9' => '0xC2B9', '0xBB' => '0xC2BB',
  417. '0xBC' => '0xC2BC', '0xBD' => '0xC2BD', '0xBE' => '0xC2BE',
  418. '0xBF' => '0xC2BF', '0xD7' => '0xD7B3', '0xD1' => '0xD781',
  419. '0xD4' => '0xD7B0', '0xD5' => '0xD7B1', '0xD6' => '0xD7B2',
  420. '0xE0' => '0xD790', '0xEA' => '0xD79A', '0xEC' => '0xD79C',
  421. '0xED' => '0xD79D', '0xEE' => '0xD79E', '0xEF' => '0xD79F',
  422. '0xF0' => '0xD7A0', '0xF1' => '0xD7A1', '0xF2' => '0xD7A2',
  423. '0xF3' => '0xD7A3', '0xF5' => '0xD7A5', '0xF6' => '0xD7A6',
  424. '0xF7' => '0xD7A7', '0xF8' => '0xD7A8', '0xF9' => '0xD7A9',
  425. '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6',
  426. '0x86' => '0xE280A0', '0x87' => '0xE280A1', '0x89' => '0xE280B0',
  427. '0x8B' => '0xE280B9', '0x93' => '0xE2809C', '0x94' => '0xE2809D',
  428. '0x95' => '0xE280A2', '0x97' => '0xE28094', '0x99' => '0xE284A2',
  429. '0xC0' => '0xD6B0', '0xC1' => '0xD6B1', '0xC3' => '0xD6B3',
  430. '0xC4' => '0xD6B4', '0xC5' => '0xD6B5', '0xC6' => '0xD6B6',
  431. '0xC7' => '0xD6B7', '0xC8' => '0xD6B8', '0xC9' => '0xD6B9',
  432. '0xCB' => '0xD6BB', '0xCC' => '0xD6BC', '0xCD' => '0xD6BD',
  433. '0xCE' => '0xD6BE', '0xCF' => '0xD6BF', '0xD0' => '0xD780',
  434. '0xD2' => '0xD782', '0xE3' => '0xD793', '0xE4' => '0xD794',
  435. '0xE5' => '0xD795', '0xE7' => '0xD797', '0xE9' => '0xD799',
  436. '0xFD' => '0xE2808E', '0xFE' => '0xE2808F', '0x92' => '0xE28099',
  437. '0x83' => '0xC692', '0xD3' => '0xD783', '0x88' => '0xCB86',
  438. '0x98' => '0xCB9C', '0x91' => '0xE28098', '0x96' => '0xE28093',
  439. '0xBA' => '0xC3B7', '0x9B' => '0xE280BA', '0xAA' => '0xC397',
  440. '0xA4' => '0xE282AA', '0xE1' => '0xD791', '0xE6' => '0xD796',
  441. '0xE8' => '0xD798', '0xEB' => '0xD79B', '0xF4' => '0xD7A4',
  442. '0xFA' => '0xD7AA', '0xFF' => '0xD6B2', '0xFC' => '0xE282AC',
  443. '0xFB' => '0xD792',
  444. ),
  445. 'windows-1253' => array(
  446. '0x81' => '\'\'', '0x88' => '\'\'', '0x8A' => '\'\'',
  447. '0x8C' => '\'\'', '0x8D' => '\'\'', '0x8E' => '\'\'',
  448. '0x8F' => '\'\'', '0x90' => '\'\'', '0x98' => '\'\'',
  449. '0x9A' => '\'\'', '0x9C' => '\'\'', '0x9D' => '\'\'',
  450. '0x9E' => '\'\'', '0x9F' => '\'\'', '0xAA' => '\'\'',
  451. '0xD2' => '\'\'', '0xFF' => '\'\'', '0xCE' => '0xCE9E',
  452. '0xB8' => '0xCE88', '0xBA' => '0xCE8A', '0xBC' => '0xCE8C',
  453. '0xBE' => '0xCE8E', '0xBF' => '0xCE8F', '0xC0' => '0xCE90',
  454. '0xC8' => '0xCE98', '0xCA' => '0xCE9A', '0xCC' => '0xCE9C',
  455. '0xCD' => '0xCE9D', '0xCF' => '0xCE9F', '0xDA' => '0xCEAA',
  456. '0xE8' => '0xCEB8', '0xEA' => '0xCEBA', '0xEC' => '0xCEBC',
  457. '0xEE' => '0xCEBE', '0xEF' => '0xCEBF', '0xC2' => '0xFF',
  458. '0xBD' => '0xC2BD', '0xED' => '0xCEBD', '0xB2' => '0xC2B2',
  459. '0xA0' => '0xC2A0', '0xA3' => '0xC2A3', '0xA4' => '0xC2A4',
  460. '0xA5' => '0xC2A5', '0xA6' => '0xC2A6', '0xA7' => '0xC2A7',
  461. '0xA8' => '0xC2A8', '0xA9' => '0xC2A9', '0xAB' => '0xC2AB',
  462. '0xAC' => '0xC2AC', '0xAD' => '0xC2AD', '0xAE' => '0xC2AE',
  463. '0xB0' => '0xC2B0', '0xB1' => '0xC2B1', '0xB3' => '0xC2B3',
  464. '0xB5' => '0xC2B5', '0xB6' => '0xC2B6', '0xB7' => '0xC2B7',
  465. '0xBB' => '0xC2BB', '0xE2' => '0xCEB2', '0x80' => '0xD2',
  466. '0x82' => '0xE2809A', '0x84' => '0xE2809E', '0x85' => '0xE280A6',
  467. '0x86' => '0xE280A0', '0xA1' => '0xCE85', '0xA2' => '0xCE86',
  468. '0x87' => '0xE280A1', '0x89' => '0xE280B0', '0xB9' => '0xCE89',
  469. '0x8B' => '0xE280B9', '0x91' => '0xE28098', '0x99' => '0xE284A2',
  470. '0x92' => '0xE28099', '0x93' => '0xE2809C', '0x94' => '0xE2809D',
  471. '0x95' => '0xE280A2', '0x96' => '0xE28093', '0x97' => '0xE28094',
  472. '0x9B' => '0xE280BA', '0xAF' => '0xE28095', '0xB4' => '0xCE84',
  473. '0xC1' => '0xCE91', '0xC3' => '0xCE93', '0xC4' => '0xCE94',
  474. '0xC5' => '0xCE95', '0xC6' => '0xCE96', '0x83' => '0xC692',
  475. '0xC7' => '0xCE97', '0xC9' => '0xCE99', '0xCB' => '0xCE9B',
  476. '0xD0' => '0xCEA0', '0xD1' => '0xCEA1', '0xD3' => '0xCEA3',
  477. '0xD4' => '0xCEA4', '0xD5' => '0xCEA5', '0xD6' => '0xCEA6',
  478. '0xD7' => '0xCEA7', '0xD8' => '0xCEA8', '0xD9' => '0xCEA9',
  479. '0xDB' => '0xCEAB', '0xDC' => '0xCEAC', '0xDD' => '0xCEAD',
  480. '0xDE' => '0xCEAE', '0xDF' => '0xCEAF', '0xE0' => '0xCEB0',
  481. '0xE1' => '0xCEB1', '0xE3' => '0xCEB3', '0xE4' => '0xCEB4',
  482. '0xE5' => '0xCEB5', '0xE6' => '0xCEB6', '0xE7' => '0xCEB7',
  483. '0xE9' => '0xCEB9', '0xEB' => '0xCEBB', '0xF0' => '0xCF80',
  484. '0xF1' => '0xCF81', '0xF2' => '0xCF82', '0xF3' => '0xCF83',
  485. '0xF4' => '0xCF84', '0xF5' => '0xCF85', '0xF6' => '0xCF86',
  486. '0xF7' => '0xCF87', '0xF8' => '0xCF88', '0xF9' => '0xCF89',
  487. '0xFA' => '0xCF8A', '0xFB' => '0xCF8B', '0xFC' => '0xCF8C',
  488. '0xFD' => '0xCF8D', '0xFE' => '0xCF8E', '0xFF' => '0xCE92',
  489. '0xD2' => '0xE282AC',
  490. ),
  491. );
  492. // Make some preparations.
  493. if (isset($translation_tables[$_POST['src_charset']]))
  494. {
  495. $replace = '%field%';
  496. foreach ($translation_tables[$_POST['src_charset']] as $from => $to)
  497. $replace = 'REPLACE(' . $replace . ', ' . $from . ', ' . $to . ')';
  498. }
  499. // Grab a list of tables.
  500. if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) === 1)
  501. $queryTables = $smcFunc['db_query']('', '
  502. SHOW TABLE STATUS
  503. FROM `' . strtr($match[1], array('`' => '')) . '`
  504. LIKE {string:table_name}',
  505. array(
  506. 'table_name' => str_replace('_', '\_', $match[2]) . '%',
  507. )
  508. );
  509. else
  510. $queryTables = $smcFunc['db_query']('', '
  511. SHOW TABLE STATUS
  512. LIKE {string:table_name}',
  513. array(
  514. 'table_name' => str_replace('_', '\_', $db_prefix) . '%',
  515. )
  516. );
  517. while ($table_info = $smcFunc['db_fetch_assoc']($queryTables))
  518. {
  519. // Just to make sure it doesn't time out.
  520. if (function_exists('apache_reset_timeout'))
  521. @apache_reset_timeout();
  522. $table_charsets = array();
  523. // Loop through each column.
  524. $queryColumns = $smcFunc['db_query']('', '
  525. SHOW FULL COLUMNS
  526. FROM ' . $table_info['Name'],
  527. array(
  528. )
  529. );
  530. while ($column_info = $smcFunc['db_fetch_assoc']($queryColumns))
  531. {
  532. // Only text'ish columns have a character set and need converting.
  533. if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
  534. {
  535. $collation = empty($column_info['Collation']) || $column_info['Collation'] === 'NULL' ? $table_info['Collation'] : $column_info['Collation'];
  536. if (!empty($collation) && $collation !== 'NULL')
  537. {
  538. list($charset) = explode('_', $collation);
  539. if (!isset($table_charsets[$charset]))
  540. $table_charsets[$charset] = array();
  541. $table_charsets[$charset][] = $column_info;
  542. }
  543. }
  544. }
  545. $smcFunc['db_free_result']($queryColumns);
  546. // Only change the column if the data doesn't match the current charset.
  547. if ((count($table_charsets) === 1 && key($table_charsets) !== $charsets[$_POST['src_charset']]) || count($table_charsets) > 1)
  548. {
  549. $updates_blob = '';
  550. $updates_text = '';
  551. foreach ($table_charsets as $charset => $columns)
  552. {
  553. if ($charset !== $charsets[$_POST['src_charset']])
  554. {
  555. foreach ($columns as $column)
  556. {
  557. $updates_blob .= '
  558. 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'] . '\'') . ',';
  559. $updates_text .= '
  560. 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'] . '\'') . ',';
  561. }
  562. }
  563. }
  564. // Change the columns to binary form.
  565. $smcFunc['db_query']('', '
  566. ALTER TABLE {raw:table_name}{raw:updates_blob}',
  567. array(
  568. 'table_name' => $table_info['Name'],
  569. 'updates_blob' => substr($updates_blob, 0, -1),
  570. )
  571. );
  572. // Convert the character set if MySQL has no native support for it.
  573. if (isset($translation_tables[$_POST['src_charset']]))
  574. {
  575. $update = '';
  576. foreach ($table_charsets as $charset => $columns)
  577. foreach ($columns as $column)
  578. $update .= '
  579. ' . $column['Field'] . ' = ' . strtr($replace, array('%field%' => $column['Field'])) . ',';
  580. $smcFunc['db_query']('', '
  581. UPDATE {raw:table_name}
  582. SET {raw:updates}',
  583. array(
  584. 'table_name' => $table_info['Name'],
  585. 'updates' => substr($update, 0, -1),
  586. )
  587. );
  588. }
  589. // Change the columns back, but with the proper character set.
  590. $smcFunc['db_query']('', '
  591. ALTER TABLE {raw:table_name}{raw:updates_text}',
  592. array(
  593. 'table_name' => $table_info['Name'],
  594. 'updates_text' => substr($updates_text, 0, -1),
  595. )
  596. );
  597. }
  598. // Now do the actual conversion (if still needed).
  599. if ($charsets[$_POST['src_charset']] !== 'utf8')
  600. $smcFunc['db_query']('', '
  601. ALTER TABLE {raw:table_name}
  602. CONVERT TO CHARACTER SET utf8',
  603. array(
  604. 'table_name' => $table_info['Name'],
  605. )
  606. );
  607. }
  608. $smcFunc['db_free_result']($queryTables);
  609. call_integration_hook('integrate_convert_utf8');
  610. // Let the settings know we have a new character set.
  611. updateSettings(array('global_character_set' => 'UTF-8', 'previousCharacterSet' => (empty($translation_tables[$_POST['src_charset']])) ? $charsets[$_POST['src_charset']] : $translation_tables[$_POST['src_charset']]));
  612. // Store it in Settings.php too because it's needed before db connection.
  613. require_once($sourcedir . '/Subs-Admin.php');
  614. updateSettingsFile(array('db_character_set' => '\'utf8\''));
  615. // The conversion might have messed up some serialized strings. Fix them!
  616. require_once($sourcedir . '/Subs-Charset.php');
  617. fix_serialized_columns();
  618. redirectexit('action=admin;area=maintain;done=convertutf8');
  619. }
  620. /**
  621. * Convert the column "body" of the table {db_prefix}messages from TEXT to MEDIUMTEXT and vice versa.
  622. * It requires the admin_forum permission.
  623. * This is needed only for MySQL.
  624. * During the convertion from MEDIUMTEXT to TEXT it check if any of the posts exceed the TEXT length and if so it aborts.
  625. * This action is linked from the maintenance screen (if it's applicable).
  626. * Accessed by ?action=admin;area=maintain;sa=database;activity=convertmsgbody.
  627. *
  628. * @uses the convert_msgbody sub template of the Admin template.
  629. */
  630. function ConvertMsgBody()
  631. {
  632. global $scripturl, $context, $txt, $language, $db_character_set, $db_type;
  633. global $modSettings, $sourcedir, $smcFunc, $time_start;
  634. // Show me your badge!
  635. isAllowedTo('admin_forum');
  636. if ($db_type != 'mysql' && $db_type != 'mysqli')
  637. return;
  638. db_extend('packages');
  639. $colData = $smcFunc['db_list_columns']('{db_prefix}messages', true);
  640. foreach ($colData as $column)
  641. if ($column['name'] == 'body')
  642. $body_type = $column['type'];
  643. $context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text';
  644. if ($body_type == 'text' || ($body_type != 'text' && isset($_POST['do_conversion'])))
  645. {
  646. checkSession();
  647. validateToken('admin-maint');
  648. // Make it longer so we can do their limit.
  649. if ($body_type == 'text')
  650. $smcFunc['db_change_column']('{db_prefix}messages', 'body', array('type' => 'mediumtext'));
  651. // Shorten the column so we can have a bit (literally per record) less space occupied
  652. else
  653. $smcFunc['db_change_column']('{db_prefix}messages', 'body', array('type' => 'text'));
  654. $colData = $smcFunc['db_list_columns']('{db_prefix}messages', true);
  655. foreach ($colData as $column)
  656. if ($column['name'] == 'body')
  657. $body_type = $column['type'];
  658. $context['maintenance_finished'] = $txt[$context['convert_to'] . '_title'];
  659. $context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text';
  660. $context['convert_to_suggest'] = ($body_type != 'text' && !empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] < 65536);
  661. return;
  662. redirectexit('action=admin;area=maintain;sa=database');
  663. }
  664. elseif ($body_type != 'text' && (!isset($_POST['do_conversion']) || isset($_POST['cont'])))
  665. {
  666. checkSession();
  667. if (empty($_REQUEST['start']))
  668. validateToken('admin-maint');
  669. else
  670. validateToken('admin-convertMsg');
  671. $context['page_title'] = $txt['not_done_title'];
  672. $context['continue_post_data'] = '';
  673. $context['continue_countdown'] = 3;
  674. $context['sub_template'] = 'not_done';
  675. $increment = 500;
  676. $id_msg_exceeding = isset($_POST['id_msg_exceeding']) ? explode(',', $_POST['id_msg_exceeding']) : array();
  677. $request = $smcFunc['db_query']('', '
  678. SELECT COUNT(*) as count
  679. FROM {db_prefix}messages',
  680. array()
  681. );
  682. list($max_msgs) = $smcFunc['db_fetch_row']($request);
  683. $smcFunc['db_free_result']($request);
  684. // Try for as much time as possible.
  685. @set_time_limit(600);
  686. while ($_REQUEST['start'] < $max_msgs)
  687. {
  688. $request = $smcFunc['db_query']('', '
  689. SELECT /*!40001 SQL_NO_CACHE */ id_msg
  690. FROM {db_prefix}messages
  691. WHERE id_msg BETWEEN {int:start} AND {int:start} + {int:increment}
  692. AND LENGTH(body) > 65535',
  693. array(
  694. 'start' => $_REQUEST['start'],
  695. 'increment' => $increment - 1,
  696. )
  697. );
  698. while ($row = $smcFunc['db_fetch_assoc']($request))
  699. $id_msg_exceeding[] = $row['id_msg'];
  700. $smcFunc['db_free_result']($request);
  701. $_REQUEST['start'] += $increment;
  702. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  703. {
  704. createToken('admin-convertMsg');
  705. $context['continue_post_data'] = '
  706. <input type="hidden" name="' . $context['admin-convertMsg_token_var'] . '" value="' . $context['admin-convertMsg_token'] . '">
  707. <input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '">
  708. <input type="hidden" name="id_msg_exceeding" value="' . implode(',', $id_msg_exceeding) . '">';
  709. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=convertmsgbody;start=' . $_REQUEST['start'];
  710. $context['continue_percent'] = round(100 * $_REQUEST['start'] / $max_msgs);
  711. return;
  712. }
  713. }
  714. createToken('admin-maint');
  715. $context['page_title'] = $txt[$context['convert_to'] . '_title'];
  716. $context['sub_template'] = 'convert_msgbody';
  717. if (!empty($id_msg_exceeding))
  718. {
  719. if (count($id_msg_exceeding) > 100)
  720. {
  721. $query_msg = array_slice($id_msg_exceeding, 0, 100);
  722. $context['exceeding_messages_morethan'] = sprintf($txt['exceeding_messages_morethan'], count($id_msg_exceeding));
  723. }
  724. else
  725. $query_msg = $id_msg_exceeding;
  726. $context['exceeding_messages'] = array();
  727. $request = $smcFunc['db_query']('', '
  728. SELECT id_msg, id_topic, subject
  729. FROM {db_prefix}messages
  730. WHERE id_msg IN ({array_int:messages})',
  731. array(
  732. 'messages' => $query_msg,
  733. )
  734. );
  735. while ($row = $smcFunc['db_fetch_assoc']($request))
  736. $context['exceeding_messages'][] = '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['id_msg'] . '#msg' . $row['id_msg'] . '">' . $row['subject'] . '</a>';
  737. $smcFunc['db_free_result']($request);
  738. }
  739. }
  740. }
  741. /**
  742. * Converts HTML-entities to their UTF-8 character equivalents.
  743. * This requires the admin_forum permission.
  744. * Pre-condition: UTF-8 has been set as database and global character set.
  745. *
  746. * It is divided in steps of 10 seconds.
  747. * This action is linked from the maintenance screen (if applicable).
  748. * It is accessed by ?action=admin;area=maintain;sa=database;activity=convertentities.
  749. *
  750. * @uses Admin template, convert_entities sub-template.
  751. */
  752. function ConvertEntities()
  753. {
  754. global $db_character_set, $modSettings, $context, $sourcedir, $smcFunc;
  755. isAllowedTo('admin_forum');
  756. // Check to see if UTF-8 is currently the default character set.
  757. if ($modSettings['global_character_set'] !== 'UTF-8' || !isset($db_character_set) || $db_character_set !== 'utf8')
  758. fatal_lang_error('entity_convert_only_utf8');
  759. // Some starting values.
  760. $context['table'] = empty($_REQUEST['table']) ? 0 : (int) $_REQUEST['table'];
  761. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  762. $context['start_time'] = time();
  763. $context['first_step'] = !isset($_REQUEST[$context['session_var']]);
  764. $context['last_step'] = false;
  765. // The first step is just a text screen with some explanation.
  766. if ($context['first_step'])
  767. {
  768. validateToken('admin-maint');
  769. createToken('admin-maint');
  770. $context['sub_template'] = 'convert_entities';
  771. return;
  772. }
  773. // Otherwise use the generic "not done" template.
  774. $context['sub_template'] = 'not_done';
  775. $context['continue_post_data'] = '';
  776. $context['continue_countdown'] = 3;
  777. // Now we're actually going to convert...
  778. checkSession('request');
  779. validateToken('admin-maint');
  780. createToken('admin-maint');
  781. // A list of tables ready for conversion.
  782. $tables = array(
  783. 'ban_groups',
  784. 'ban_items',
  785. 'boards',
  786. 'calendar',
  787. 'calendar_holidays',
  788. 'categories',
  789. 'log_errors',
  790. 'log_search_subjects',
  791. 'membergroups',
  792. 'members',
  793. 'message_icons',
  794. 'messages',
  795. 'package_servers',
  796. 'personal_messages',
  797. 'pm_recipients',
  798. 'polls',
  799. 'poll_choices',
  800. 'smileys',
  801. 'themes',
  802. );
  803. $context['num_tables'] = count($tables);
  804. // Loop through all tables that need converting.
  805. for (; $context['table'] < $context['num_tables']; $context['table']++)
  806. {
  807. $cur_table = $tables[$context['table']];
  808. $primary_key = '';
  809. // Make sure we keep stuff unique!
  810. $primary_keys = array();
  811. if (function_exists('apache_reset_timeout'))
  812. @apache_reset_timeout();
  813. // Get a list of text columns.
  814. $columns = array();
  815. $request = $smcFunc['db_query']('', '
  816. SHOW FULL COLUMNS
  817. FROM {db_prefix}' . $cur_table,
  818. array(
  819. )
  820. );
  821. while ($column_info = $smcFunc['db_fetch_assoc']($request))
  822. if (strpos($column_info['Type'], 'text') !== false || strpos($column_info['Type'], 'char') !== false)
  823. $columns[] = strtolower($column_info['Field']);
  824. // Get the column with the (first) primary key.
  825. $request = $smcFunc['db_query']('', '
  826. SHOW KEYS
  827. FROM {db_prefix}' . $cur_table,
  828. array(
  829. )
  830. );
  831. while ($row = $smcFunc['db_fetch_assoc']($request))
  832. {
  833. if ($row['Key_name'] === 'PRIMARY')
  834. {
  835. if (empty($primary_key) || ($row['Seq_in_index'] == 1 && !in_array(strtolower($row['Column_name']), $columns)))
  836. $primary_key = $row['Column_name'];
  837. $primary_keys[] = $row['Column_name'];
  838. }
  839. }
  840. $smcFunc['db_free_result']($request);
  841. // No primary key, no glory.
  842. // Same for columns. Just to be sure we've work to do!
  843. if (empty($primary_key) || empty($columns))
  844. continue;
  845. // Get the maximum value for the primary key.
  846. $request = $smcFunc['db_query']('', '
  847. SELECT MAX(' . $primary_key . ')
  848. FROM {db_prefix}' . $cur_table,
  849. array(
  850. )
  851. );
  852. list($max_value) = $smcFunc['db_fetch_row']($request);
  853. $smcFunc['db_free_result']($request);
  854. if (empty($max_value))
  855. continue;
  856. while ($context['start'] <= $max_value)
  857. {
  858. // Retrieve a list of rows that has at least one entity to convert.
  859. $request = $smcFunc['db_query']('', '
  860. SELECT {raw:primary_keys}, {raw:columns}
  861. FROM {db_prefix}{raw:cur_table}
  862. WHERE {raw:primary_key} BETWEEN {int:start} AND {int:start} + 499
  863. AND {raw:like_compare}
  864. LIMIT 500',
  865. array(
  866. 'primary_keys' => implode(', ', $primary_keys),
  867. 'columns' => implode(', ', $columns),
  868. 'cur_table' => $cur_table,
  869. 'primary_key' => $primary_key,
  870. 'start' => $context['start'],
  871. 'like_compare' => '(' . implode(' LIKE \'%&#%\' OR ', $columns) . ' LIKE \'%&#%\')',
  872. )
  873. );
  874. while ($row = $smcFunc['db_fetch_assoc']($request))
  875. {
  876. $insertion_variables = array();
  877. $changes = array();
  878. foreach ($row as $column_name => $column_value)
  879. if ($column_name !== $primary_key && strpos($column_value, '&#') !== false)
  880. {
  881. $changes[] = $column_name . ' = {string:changes_' . $column_name . '}';
  882. $insertion_variables['changes_' . $column_name] = preg_replace_callback('~&#(\d{1,7}|x[0-9a-fA-F]{1,6});~', 'fixchar__callback', $column_value);
  883. }
  884. $where = array();
  885. foreach ($primary_keys as $key)
  886. {
  887. $where[] = $key . ' = {string:where_' . $key . '}';
  888. $insertion_variables['where_' . $key] = $row[$key];
  889. }
  890. // Update the row.
  891. if (!empty($changes))
  892. $smcFunc['db_query']('', '
  893. UPDATE {db_prefix}' . $cur_table . '
  894. SET
  895. ' . implode(',
  896. ', $changes) . '
  897. WHERE ' . implode(' AND ', $where),
  898. $insertion_variables
  899. );
  900. }
  901. $smcFunc['db_free_result']($request);
  902. $context['start'] += 500;
  903. // After ten seconds interrupt.
  904. if (time() - $context['start_time'] > 10)
  905. {
  906. // Calculate an approximation of the percentage done.
  907. $context['continue_percent'] = round(100 * ($context['table'] + ($context['start'] / $max_value)) / $context['num_tables'], 1);
  908. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;activity=convertentities;table=' . $context['table'] . ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  909. return;
  910. }
  911. }
  912. $context['start'] = 0;
  913. }
  914. // Make sure all serialized strings are all right.
  915. require_once($sourcedir . '/Subs-Charset.php');
  916. fix_serialized_columns();
  917. // If we're here, we must be done.
  918. $context['continue_percent'] = 100;
  919. $context['continue_get_data'] = '?action=admin;area=maintain;sa=database;done=convertentities';
  920. $context['last_step'] = true;
  921. $context['continue_countdown'] = -1;
  922. }
  923. /**
  924. * Optimizes all tables in the database and lists how much was saved.
  925. * It requires the admin_forum permission.
  926. * It shows as the maintain_forum admin area.
  927. * It is accessed from ?action=admin;area=maintain;sa=database;activity=optimize.
  928. * It also updates the optimize scheduled task such that the tables are not automatically optimized again too soon.
  929. * @uses the rawdata sub template (built in.)
  930. */
  931. function OptimizeTables()
  932. {
  933. global $db_type, $db_name, $db_prefix, $txt, $context, $scripturl, $sourcedir, $smcFunc;
  934. isAllowedTo('admin_forum');
  935. checkSession();
  936. validateToken('admin-maint');
  937. ignore_user_abort(true);
  938. db_extend();
  939. // Start with no tables optimized.
  940. $opttab = 0;
  941. $context['page_title'] = $txt['database_optimize'];
  942. $context['sub_template'] = 'optimize';
  943. // Only optimize the tables related to this smf install, not all the tables in the db
  944. $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
  945. // Get a list of tables, as well as how many there are.
  946. $temp_tables = $smcFunc['db_list_tables'](false, $real_prefix . '%');
  947. $tables = array();
  948. foreach ($temp_tables as $table)
  949. $tables[] = array('table_name' => $table);
  950. // If there aren't any tables then I believe that would mean the world has exploded...
  951. $context['num_tables'] = count($tables);
  952. if ($context['num_tables'] == 0)
  953. fatal_error('You appear to be running SMF in a flat file mode... fantastic!', false);
  954. // For each table....
  955. $context['optimized_tables'] = array();
  956. foreach ($tables as $table)
  957. {
  958. // Optimize the table! We use backticks here because it might be a custom table.
  959. $data_freed = $smcFunc['db_optimize_table']($table['table_name']);
  960. // Optimizing one sqlite table optimizes them all.
  961. if ($db_type == 'sqlite')
  962. break;
  963. if ($data_freed > 0)
  964. $context['optimized_tables'][] = array(
  965. 'name' => $table['table_name'],
  966. 'data_freed' => $data_freed,
  967. );
  968. }
  969. // Number of tables, etc....
  970. $txt['database_numb_tables'] = sprintf($txt['database_numb_tables'], $context['num_tables']);
  971. $context['num_tables_optimized'] = count($context['optimized_tables']);
  972. // Check that we don't auto optimise again too soon!
  973. require_once($sourcedir . '/ScheduledTasks.php');
  974. CalculateNextTrigger('auto_optimize', true);
  975. }
  976. /**
  977. * Recount many forum totals that can be recounted automatically without harm.
  978. * it requires the admin_forum permission.
  979. * It shows the maintain_forum admin area.
  980. *
  981. * Totals recounted:
  982. * - fixes for topics with wrong num_replies.
  983. * - updates for num_posts and num_topics of all boards.
  984. * - recounts instant_messages but not unread_messages.
  985. * - repairs messages pointing to boards with topics pointing to other boards.
  986. * - updates the last message posted in boards and children.
  987. * - updates member count, latest member, topic count, and message count.
  988. *
  989. * The function redirects back to ?action=admin;area=maintain when complete.
  990. * It is accessed via ?action=admin;area=maintain;sa=database;activity=recount.
  991. */
  992. function AdminBoardRecount()
  993. {
  994. global $txt, $context, $scripturl, $modSettings, $sourcedir;
  995. global $time_start, $smcFunc;
  996. isAllowedTo('admin_forum');
  997. checkSession('request');
  998. // validate the request or the loop
  999. if (!isset($_REQUEST['step']))
  1000. validateToken('admin-maint');
  1001. else
  1002. validateToken('admin-boardrecount');
  1003. $context['page_title'] = $txt['not_done_title'];
  1004. $context['continue_post_data'] = '';
  1005. $context['continue_countdown'] = 3;
  1006. $context['sub_template'] = 'not_done';
  1007. // Try for as much time as possible.
  1008. @set_time_limit(600);
  1009. // Step the number of topics at a time so things don't time out...
  1010. $request = $smcFunc['db_query']('', '
  1011. SELECT MAX(id_topic)
  1012. FROM {db_prefix}topics',
  1013. array(
  1014. )
  1015. );
  1016. list ($max_topics) = $smcFunc['db_fetch_row']($request);
  1017. $smcFunc['db_free_result']($request);
  1018. $increment = min(max(50, ceil($max_topics / 4)), 2000);
  1019. if (empty($_REQUEST['start']))
  1020. $_REQUEST['start'] = 0;
  1021. $total_steps = 8;
  1022. // Get each topic with a wrong reply count and fix it - let's just do some at a time, though.
  1023. if (empty($_REQUEST['step']))
  1024. {
  1025. $_REQUEST['step'] = 0;
  1026. while ($_REQUEST['start'] < $max_topics)
  1027. {
  1028. // Recount approved messages
  1029. $request = $smcFunc['db_query']('', '
  1030. SELECT /*!40001 SQL_NO_CACHE */ t.id_topic, MAX(t.num_replies) AS num_replies,
  1031. CASE WHEN COUNT(ma.id_msg) >= 1 THEN COUNT(ma.id_msg) - 1 ELSE 0 END AS real_num_replies
  1032. FROM {db_prefix}topics AS t
  1033. LEFT JOIN {db_prefix}messages AS ma ON (ma.id_topic = t.id_topic AND ma.approved = {int:is_approved})
  1034. WHERE t.id_topic > {int:start}
  1035. AND t.id_topic <= {int:max_id}
  1036. GROUP BY t.id_topic
  1037. HAVING CASE WHEN COUNT(ma.id_msg) >= 1 THEN COUNT(ma.id_msg) - 1 ELSE 0 END != MAX(t.num_replies)',
  1038. array(
  1039. 'is_approved' => 1,
  1040. 'start' => $_REQUEST['start'],
  1041. 'max_id' => $_REQUEST['start'] + $increment,
  1042. )
  1043. );
  1044. while ($row = $smcFunc['db_fetch_assoc']($request))
  1045. $smcFunc['db_query']('', '
  1046. UPDATE {db_prefix}topics
  1047. SET num_replies = {int:num_replies}
  1048. WHERE id_topic = {int:id_topic}',
  1049. array(
  1050. 'num_replies' => $row['real_num_replies'],
  1051. 'id_topic' => $row['id_topic'],
  1052. )
  1053. );
  1054. $smcFunc['db_free_result']($request);
  1055. // Recount unapproved messages
  1056. $request = $smcFunc['db_query']('', '
  1057. SELECT /*!40001 SQL_NO_CACHE */ t.id_topic, MAX(t.unapproved_posts) AS unapproved_posts,
  1058. COUNT(mu.id_msg) AS real_unapproved_posts
  1059. FROM {db_prefix}topics AS t
  1060. LEFT JOIN {db_prefix}messages AS mu ON (mu.id_topic = t.id_topic AND mu.approved = {int:not_approved})
  1061. WHERE t.id_topic > {int:start}
  1062. AND t.id_topic <= {int:max_id}
  1063. GROUP BY t.id_topic
  1064. HAVING COUNT(mu.id_msg) != MAX(t.unapproved_posts)',
  1065. array(
  1066. 'not_approved' => 0,
  1067. 'start' => $_REQUEST['start'],
  1068. 'max_id' => $_REQUEST['start'] + $increment,
  1069. )
  1070. );
  1071. while ($row = $smcFunc['db_fetch_assoc']($request))
  1072. $smcFunc['db_query']('', '
  1073. UPDATE {db_prefix}topics
  1074. SET unapproved_posts = {int:unapproved_posts}
  1075. WHERE id_topic = {int:id_topic}',
  1076. array(
  1077. 'unapproved_posts' => $row['real_unapproved_posts'],
  1078. 'id_topic' => $row['id_topic'],
  1079. )
  1080. );
  1081. $smcFunc['db_free_result']($request);
  1082. $_REQUEST['start'] += $increment;
  1083. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1084. {
  1085. createToken('admin-boardrecount');
  1086. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-boardrecount_token_var'] . '" value="' . $context['admin-boardrecount_token'] . '">';
  1087. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=0;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1088. $context['continue_percent'] = round((100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1089. return;
  1090. }
  1091. }
  1092. $_REQUEST['start'] = 0;
  1093. }
  1094. // Update the post count of each board.
  1095. if ($_REQUEST['step'] <= 1)
  1096. {
  1097. if (empty($_REQUEST['start']))
  1098. $smcFunc['db_query']('', '
  1099. UPDATE {db_prefix}boards
  1100. SET num_posts = {int:num_posts}
  1101. WHERE redirect = {string:redirect}',
  1102. array(
  1103. 'num_posts' => 0,
  1104. 'redirect' => '',
  1105. )
  1106. );
  1107. while ($_REQUEST['start'] < $max_topics)
  1108. {
  1109. $request = $smcFunc['db_query']('', '
  1110. SELECT /*!40001 SQL_NO_CACHE */ m.id_board, COUNT(*) AS real_num_posts
  1111. FROM {db_prefix}messages AS m
  1112. WHERE m.id_topic > {int:id_topic_min}
  1113. AND m.id_topic <= {int:id_topic_max}
  1114. AND m.approved = {int:is_approved}
  1115. GROUP BY m.id_board',
  1116. array(
  1117. 'id_topic_min' => $_REQUEST['start'],
  1118. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1119. 'is_approved' => 1,
  1120. )
  1121. );
  1122. while ($row = $smcFunc['db_fetch_assoc']($request))
  1123. $smcFunc['db_query']('', '
  1124. UPDATE {db_prefix}boards
  1125. SET num_posts = num_posts + {int:real_num_posts}
  1126. WHERE id_board = {int:id_board}',
  1127. array(
  1128. 'id_board' => $row['id_board'],
  1129. 'real_num_posts' => $row['real_num_posts'],
  1130. )
  1131. );
  1132. $smcFunc['db_free_result']($request);
  1133. $_REQUEST['start'] += $increment;
  1134. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1135. {
  1136. createToken('admin-boardrecount');
  1137. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-boardrecount_token_var'] . '" value="' . $context['admin-boardrecount_token'] . '">';
  1138. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=1;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1139. $context['continue_percent'] = round((200 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1140. return;
  1141. }
  1142. }
  1143. $_REQUEST['start'] = 0;
  1144. }
  1145. // Update the topic count of each board.
  1146. if ($_REQUEST['step'] <= 2)
  1147. {
  1148. if (empty($_REQUEST['start']))
  1149. $smcFunc['db_query']('', '
  1150. UPDATE {db_prefix}boards
  1151. SET num_topics = {int:num_topics}',
  1152. array(
  1153. 'num_topics' => 0,
  1154. )
  1155. );
  1156. while ($_REQUEST['start'] < $max_topics)
  1157. {
  1158. $request = $smcFunc['db_query']('', '
  1159. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, COUNT(*) AS real_num_topics
  1160. FROM {db_prefix}topics AS t
  1161. WHERE t.approved = {int:is_approved}
  1162. AND t.id_topic > {int:id_topic_min}
  1163. AND t.id_topic <= {int:id_topic_max}
  1164. GROUP BY t.id_board',
  1165. array(
  1166. 'is_approved' => 1,
  1167. 'id_topic_min' => $_REQUEST['start'],
  1168. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1169. )
  1170. );
  1171. while ($row = $smcFunc['db_fetch_assoc']($request))
  1172. $smcFunc['db_query']('', '
  1173. UPDATE {db_prefix}boards
  1174. SET num_topics = num_topics + {int:real_num_topics}
  1175. WHERE id_board = {int:id_board}',
  1176. array(
  1177. 'id_board' => $row['id_board'],
  1178. 'real_num_topics' => $row['real_num_topics'],
  1179. )
  1180. );
  1181. $smcFunc['db_free_result']($request);
  1182. $_REQUEST['start'] += $increment;
  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=2;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1188. $context['continue_percent'] = round((300 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1189. return;
  1190. }
  1191. }
  1192. $_REQUEST['start'] = 0;
  1193. }
  1194. // Update the unapproved post count of each board.
  1195. if ($_REQUEST['step'] <= 3)
  1196. {
  1197. if (empty($_REQUEST['start']))
  1198. $smcFunc['db_query']('', '
  1199. UPDATE {db_prefix}boards
  1200. SET unapproved_posts = {int:unapproved_posts}',
  1201. array(
  1202. 'unapproved_posts' => 0,
  1203. )
  1204. );
  1205. while ($_REQUEST['start'] < $max_topics)
  1206. {
  1207. $request = $smcFunc['db_query']('', '
  1208. SELECT /*!40001 SQL_NO_CACHE */ m.id_board, COUNT(*) AS real_unapproved_posts
  1209. FROM {db_prefix}messages AS m
  1210. WHERE m.id_topic > {int:id_topic_min}
  1211. AND m.id_topic <= {int:id_topic_max}
  1212. AND m.approved = {int:is_approved}
  1213. GROUP BY m.id_board',
  1214. array(
  1215. 'id_topic_min' => $_REQUEST['start'],
  1216. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1217. 'is_approved' => 0,
  1218. )
  1219. );
  1220. while ($row = $smcFunc['db_fetch_assoc']($request))
  1221. $smcFunc['db_query']('', '
  1222. UPDATE {db_prefix}boards
  1223. SET unapproved_posts = unapproved_posts + {int:unapproved_posts}
  1224. WHERE id_board = {int:id_board}',
  1225. array(
  1226. 'id_board' => $row['id_board'],
  1227. 'unapproved_posts' => $row['real_unapproved_posts'],
  1228. )
  1229. );
  1230. $smcFunc['db_free_result']($request);
  1231. $_REQUEST['start'] += $increment;
  1232. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1233. {
  1234. createToken('admin-boardrecount');
  1235. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-boardrecount_token_var'] . '" value="' . $context['admin-boardrecount_token'] . '">';
  1236. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=3;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1237. $context['continue_percent'] = round((400 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1238. return;
  1239. }
  1240. }
  1241. $_REQUEST['start'] = 0;
  1242. }
  1243. // Update the unapproved topic count of each board.
  1244. if ($_REQUEST['step'] <= 4)
  1245. {
  1246. if (empty($_REQUEST['start']))
  1247. $smcFunc['db_query']('', '
  1248. UPDATE {db_prefix}boards
  1249. SET unapproved_topics = {int:unapproved_topics}',
  1250. array(
  1251. 'unapproved_topics' => 0,
  1252. )
  1253. );
  1254. while ($_REQUEST['start'] < $max_topics)
  1255. {
  1256. $request = $smcFunc['db_query']('', '
  1257. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, COUNT(*) AS real_unapproved_topics
  1258. FROM {db_prefix}topics AS t
  1259. WHERE t.approved = {int:is_approved}
  1260. AND t.id_topic > {int:id_topic_min}
  1261. AND t.id_topic <= {int:id_topic_max}
  1262. GROUP BY t.id_board',
  1263. array(
  1264. 'is_approved' => 0,
  1265. 'id_topic_min' => $_REQUEST['start'],
  1266. 'id_topic_max' => $_REQUEST['start'] + $increment,
  1267. )
  1268. );
  1269. while ($row = $smcFunc['db_fetch_assoc']($request))
  1270. $smcFunc['db_query']('', '
  1271. UPDATE {db_prefix}boards
  1272. SET unapproved_topics = unapproved_topics + {int:real_unapproved_topics}
  1273. WHERE id_board = {int:id_board}',
  1274. array(
  1275. 'id_board' => $row['id_board'],
  1276. 'real_unapproved_topics' => $row['real_unapproved_topics'],
  1277. )
  1278. );
  1279. $smcFunc['db_free_result']($request);
  1280. $_REQUEST['start'] += $increment;
  1281. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1282. {
  1283. createToken('admin-boardrecount');
  1284. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-boardrecount_token_var'] . '" value="' . $context['admin-boardrecount_token'] . '">';
  1285. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=4;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1286. $context['continue_percent'] = round((500 + 100 * $_REQUEST['start'] / $max_topics) / $total_steps);
  1287. return;
  1288. }
  1289. }
  1290. $_REQUEST['start'] = 0;
  1291. }
  1292. // Get all members with wrong number of personal messages.
  1293. if ($_REQUEST['step'] <= 5)
  1294. {
  1295. $request = $smcFunc['db_query']('', '
  1296. SELECT /*!40001 SQL_NO_CACHE */ mem.id_member, COUNT(pmr.id_pm) AS real_num,
  1297. MAX(mem.instant_messages) AS instant_messages
  1298. FROM {db_prefix}members AS mem
  1299. LEFT JOIN {db_prefix}pm_recipients AS pmr ON (mem.id_member = pmr.id_member AND pmr.deleted = {int:is_not_deleted})
  1300. GROUP BY mem.id_member
  1301. HAVING COUNT(pmr.id_pm) != MAX(mem.instant_messages)',
  1302. array(
  1303. 'is_not_deleted' => 0,
  1304. )
  1305. );
  1306. while ($row = $smcFunc['db_fetch_assoc']($request))
  1307. updateMemberData($row['id_member'], array('instant_messages' => $row['real_num']));
  1308. $smcFunc['db_free_result']($request);
  1309. $request = $smcFunc['db_query']('', '
  1310. SELECT /*!40001 SQL_NO_CACHE */ mem.id_member, COUNT(pmr.id_pm) AS real_num,
  1311. MAX(mem.unread_messages) AS unread_messages
  1312. FROM {db_prefix}members AS mem
  1313. 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})
  1314. GROUP BY mem.id_member
  1315. HAVING COUNT(pmr.id_pm) != MAX(mem.unread_messages)',
  1316. array(
  1317. 'is_not_deleted' => 0,
  1318. 'is_not_read' => 0,
  1319. )
  1320. );
  1321. while ($row = $smcFunc['db_fetch_assoc']($request))
  1322. updateMemberData($row['id_member'], array('unread_messages' => $row['real_num']));
  1323. $smcFunc['db_free_result']($request);
  1324. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1325. {
  1326. createToken('admin-boardrecount');
  1327. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-boardrecount_token_var'] . '" value="' . $context['admin-boardrecount_token'] . '">';
  1328. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=0;' . $context['session_var'] . '=' . $context['session_id'];
  1329. $context['continue_percent'] = round(700 / $total_steps);
  1330. return;
  1331. }
  1332. }
  1333. // Any messages pointing to the wrong board?
  1334. if ($_REQUEST['step'] <= 6)
  1335. {
  1336. while ($_REQUEST['start'] < $modSettings['maxMsgID'])
  1337. {
  1338. $request = $smcFunc['db_query']('', '
  1339. SELECT /*!40001 SQL_NO_CACHE */ t.id_board, m.id_msg
  1340. FROM {db_prefix}messages AS m
  1341. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic AND t.id_board != m.id_board)
  1342. WHERE m.id_msg > {int:id_msg_min}
  1343. AND m.id_msg <= {int:id_msg_max}',
  1344. array(
  1345. 'id_msg_min' => $_REQUEST['start'],
  1346. 'id_msg_max' => $_REQUEST['start'] + $increment,
  1347. )
  1348. );
  1349. $boards = array();
  1350. while ($row = $smcFunc['db_fetch_assoc']($request))
  1351. $boards[$row['id_board']][] = $row['id_msg'];
  1352. $smcFunc['db_free_result']($request);
  1353. foreach ($boards as $board_id => $messages)
  1354. $smcFunc['db_query']('', '
  1355. UPDATE {db_prefix}messages
  1356. SET id_board = {int:id_board}
  1357. WHERE id_msg IN ({array_int:id_msg_array})',
  1358. array(
  1359. 'id_msg_array' => $messages,
  1360. 'id_board' => $board_id,
  1361. )
  1362. );
  1363. $_REQUEST['start'] += $increment;
  1364. if (array_sum(explode(' ', microtime())) - array_sum(explode(' ', $time_start)) > 3)
  1365. {
  1366. createToken('admin-boardrecount');
  1367. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-boardrecount_token_var'] . '" value="' . $context['admin-boardrecount_token'] . '">';
  1368. $context['continue_get_data'] = '?action=admin;area=maintain;sa=routine;activity=recount;step=6;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1369. $context['continue_percent'] = round((700 + 100 * $_REQUEST['start'] / $modSettings['maxMsgID']) / $total_steps);
  1370. return;
  1371. }
  1372. }
  1373. $_REQUEST['start'] = 0;
  1374. }
  1375. // Update the latest message of each board.
  1376. $request = $smcFunc['db_query']('', '
  1377. SELECT m.id_board, MAX(m.id_msg) AS local_last_msg
  1378. FROM {db_prefix}messages AS m
  1379. WHERE m.approved = {int:is_approved}
  1380. GROUP BY m.id_board',
  1381. array(
  1382. 'is_approved' => 1,
  1383. )
  1384. );
  1385. $realBoardCounts = array();
  1386. while ($row = $smcFunc['db_fetch_assoc']($request))
  1387. $realBoardCounts[$row['id_board']] = $row['local_last_msg'];
  1388. $smcFunc['db_free_result']($request);
  1389. $request = $smcFunc['db_query']('', '
  1390. SELECT /*!40001 SQL_NO_CACHE */ id_board, id_parent, id_last_msg, child_level, id_msg_updated
  1391. FROM {db_prefix}boards',
  1392. array(
  1393. )
  1394. );
  1395. $resort_me = array();
  1396. while ($row = $smcFunc['db_fetch_assoc']($request))
  1397. {
  1398. $row['local_last_msg'] = isset($realBoardCounts[$row['id_board']]) ? $realBoardCounts[$row['id_board']] : 0;
  1399. $resort_me[$row['child_level']][] = $row;
  1400. }
  1401. $smcFunc['db_free_result']($request);
  1402. krsort($resort_me);
  1403. $lastModifiedMsg = array();
  1404. foreach ($resort_me as $rows)
  1405. foreach ($rows as $row)
  1406. {
  1407. // The latest message is the latest of the current board and its children.
  1408. if (isset($lastModifiedMsg[$row['id_board']]))
  1409. $curLastModifiedMsg = max($row['local_last_msg'], $lastModifiedMsg[$row['id_board']]);
  1410. else
  1411. $curLastModifiedMsg = $row['local_last_msg'];
  1412. // If what is and what should be the latest message differ, an update is necessary.
  1413. if ($row['local_last_msg'] != $row['id_last_msg'] || $curLastModifiedMsg != $row['id_msg_updated'])
  1414. $smcFunc['db_query']('', '
  1415. UPDATE {db_prefix}boards
  1416. SET id_last_msg = {int:id_last_msg}, id_msg_updated = {int:id_msg_updated}
  1417. WHERE id_board = {int:id_board}',
  1418. array(
  1419. 'id_last_msg' => $row['local_last_msg'],
  1420. 'id_msg_updated' => $curLastModifiedMsg,
  1421. 'id_board' => $row['id_board'],
  1422. )
  1423. );
  1424. // Parent boards inherit the latest modified message of their children.
  1425. if (isset($lastModifiedMsg[$row['id_parent']]))
  1426. $lastModifiedMsg[$row['id_parent']] = max($row['local_last_msg'], $lastModifiedMsg[$row['id_parent']]);
  1427. else
  1428. $lastModifiedMsg[$row['id_parent']] = $row['local_last_msg'];
  1429. }
  1430. // Update all the basic statistics.
  1431. updateStats('member');
  1432. updateStats('message');
  1433. updateStats('topic');
  1434. // Finally, update the latest event times.
  1435. require_once($sourcedir . '/ScheduledTasks.php');
  1436. CalculateNextTrigger();
  1437. redirectexit('action=admin;area=maintain;sa=routine;done=recount');
  1438. }
  1439. /**
  1440. * Perform a detailed version check. A very good thing ;).
  1441. * The function parses the comment headers in all files for their version information,
  1442. * and outputs that for some javascript to check with simplemachines.org.
  1443. * It does not connect directly with simplemachines.org, but rather expects the client to.
  1444. *
  1445. * It requires the admin_forum permission.
  1446. * Uses the view_versions admin area.
  1447. * Accessed through ?action=admin;area=maintain;sa=routine;activity=version.
  1448. * @uses Admin template, view_versions sub-template.
  1449. */
  1450. function VersionDetail()
  1451. {
  1452. global $forum_version, $txt, $sourcedir, $context;
  1453. isAllowedTo('admin_forum');
  1454. // Call the function that'll get all the version info we need.
  1455. require_once($sourcedir . '/Subs-Admin.php');
  1456. $versionOptions = array(
  1457. 'include_ssi' => true,
  1458. 'include_subscriptions' => true,
  1459. 'sort_results' => true,
  1460. );
  1461. $version_info = getFileVersions($versionOptions);
  1462. // Add the new info to the template context.
  1463. $context += array(
  1464. 'file_versions' => $version_info['file_versions'],
  1465. 'default_template_versions' => $version_info['default_template_versions'],
  1466. 'template_versions' => $version_info['template_versions'],
  1467. 'default_language_versions' => $version_info['default_language_versions'],
  1468. 'default_known_languages' => array_keys($version_info['default_language_versions']),
  1469. );
  1470. // Make it easier to manage for the template.
  1471. $context['forum_version'] = $forum_version;
  1472. $context['sub_template'] = 'view_versions';
  1473. $context['page_title'] = $txt['admin_version_check'];
  1474. }
  1475. /**
  1476. * Re-attribute posts.
  1477. */
  1478. function MaintainReattributePosts()
  1479. {
  1480. global $sourcedir, $context, $txt;
  1481. checkSession();
  1482. // Find the member.
  1483. require_once($sourcedir . '/Subs-Auth.php');
  1484. $members = findMembers($_POST['to']);
  1485. if (empty($members))
  1486. fatal_lang_error('reattribute_cannot_find_member');
  1487. $memID = array_shift($members);
  1488. $memID = $memID['id'];
  1489. $email = $_POST['type'] == 'email' ? $_POST['from_email'] : '';
  1490. $membername = $_POST['type'] == 'name' ? $_POST['from_name'] : '';
  1491. // Now call the reattribute function.
  1492. require_once($sourcedir . '/Subs-Members.php');
  1493. reattributePosts($memID, $email, $membername, !empty($_POST['posts']));
  1494. $context['maintenance_finished'] = $txt['maintain_reattribute_posts'];
  1495. }
  1496. /**
  1497. * Removing old members. Done and out!
  1498. * @todo refactor
  1499. */
  1500. function MaintainPurgeInactiveMembers()
  1501. {
  1502. global $sourcedir, $context, $smcFunc, $txt;
  1503. $_POST['maxdays'] = empty($_POST['maxdays']) ? 0 : (int) $_POST['maxdays'];
  1504. if (!empty($_POST['groups']) && $_POST['maxdays'] > 0)
  1505. {
  1506. checkSession();
  1507. validateToken('admin-maint');
  1508. $groups = array();
  1509. foreach ($_POST['groups'] as $id => $dummy)
  1510. $groups[] = (int) $id;
  1511. $time_limit = (time() - ($_POST['maxdays'] * 24 * 3600));
  1512. $where_vars = array(
  1513. 'time_limit' => $time_limit,
  1514. );
  1515. if ($_POST['del_type'] == 'activated')
  1516. {
  1517. $where = 'mem.date_registered < {int:time_limit} AND mem.is_activated = {int:is_activated}';
  1518. $where_vars['is_activated'] = 0;
  1519. }
  1520. else
  1521. $where = 'mem.last_login < {int:time_limit} AND (mem.last_login != 0 OR mem.date_registered < {int:time_limit})';
  1522. // Need to get *all* groups then work out which (if any) we avoid.
  1523. $request = $smcFunc['db_query']('', '
  1524. SELECT id_group, group_name, min_posts
  1525. FROM {db_prefix}membergroups',
  1526. array(
  1527. )
  1528. );
  1529. while ($row = $smcFunc['db_fetch_assoc']($request))
  1530. {
  1531. // Avoid this one?
  1532. if (!in_array($row['id_group'], $groups))
  1533. {
  1534. // Post group?
  1535. if ($row['min_posts'] != -1)
  1536. {
  1537. $where .= ' AND mem.id_post_group != {int:id_post_group_' . $row['id_group'] . '}';
  1538. $where_vars['id_post_group_' . $row['id_group']] = $row['id_group'];
  1539. }
  1540. else
  1541. {
  1542. $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';
  1543. $where_vars['id_group_' . $row['id_group']] = $row['id_group'];
  1544. }
  1545. }
  1546. }
  1547. $smcFunc['db_free_result']($request);
  1548. // If we have ungrouped unselected we need to avoid those guys.
  1549. if (!in_array(0, $groups))
  1550. {
  1551. $where .= ' AND (mem.id_group != 0 OR mem.additional_groups != {string:blank_add_groups})';
  1552. $where_vars['blank_add_groups'] = '';
  1553. }
  1554. // Select all the members we're about to murder/remove...
  1555. $request = $smcFunc['db_query']('', '
  1556. SELECT mem.id_member, IFNULL(m.id_member, 0) AS is_mod
  1557. FROM {db_prefix}members AS mem
  1558. LEFT JOIN {db_prefix}moderators AS m ON (m.id_member = mem.id_member)
  1559. WHERE ' . $where,
  1560. $where_vars
  1561. );
  1562. $members = array();
  1563. while ($row = $smcFunc['db_fetch_assoc']($request))
  1564. {
  1565. if (!$row['is_mod'] || !in_array(3, $groups))
  1566. $members[] = $row['id_member'];
  1567. }
  1568. $smcFunc['db_free_result']($request);
  1569. require_once($sourcedir . '/Subs-Members.php');
  1570. deleteMembers($members);
  1571. }
  1572. $context['maintenance_finished'] = $txt['maintain_members'];
  1573. createToken('admin-maint');
  1574. }
  1575. /**
  1576. * Removing old posts doesn't take much as we really pass through.
  1577. */
  1578. function MaintainRemoveOldPosts()
  1579. {
  1580. global $sourcedir, $context, $txt;
  1581. validateToken('admin-maint');
  1582. // Actually do what we're told!
  1583. require_once($sourcedir . '/RemoveTopic.php');
  1584. RemoveOldTopics2();
  1585. }
  1586. /**
  1587. * Removing old drafts
  1588. */
  1589. function MaintainRemoveOldDrafts()
  1590. {
  1591. global $sourcedir, $smcFunc;
  1592. validateToken('admin-maint');
  1593. $drafts = array();
  1594. // Find all of the old drafts
  1595. $request = $smcFunc['db_query']('', '
  1596. SELECT id_draft
  1597. FROM {db_prefix}user_drafts
  1598. WHERE poster_time <= {int:poster_time_old}',
  1599. array(
  1600. 'poster_time_old' => time() - (86400 * $_POST['draftdays']),
  1601. )
  1602. );
  1603. while ($row = $smcFunc['db_fetch_row']($request))
  1604. $drafts[] = (int) $row[0];
  1605. $smcFunc['db_free_result']($request);
  1606. // If we have old drafts, remove them
  1607. if (count($drafts) > 0)
  1608. {
  1609. require_once($sourcedir . '/Drafts.php');
  1610. DeleteDraft($drafts, false);
  1611. }
  1612. }
  1613. /**
  1614. * Moves topics from one board to another.
  1615. *
  1616. * @uses not_done template to pause the process.
  1617. */
  1618. function MaintainMassMoveTopics()
  1619. {
  1620. global $smcFunc, $sourcedir, $context, $txt;
  1621. // Only admins.
  1622. isAllowedTo('admin_forum');
  1623. checkSession('request');
  1624. validateToken('admin-maint');
  1625. // Set up to the context.
  1626. $context['page_title'] = $txt['not_done_title'];
  1627. $context['continue_countdown'] = 3;
  1628. $context['continue_post_data'] = '';
  1629. $context['continue_get_data'] = '';
  1630. $context['sub_template'] = 'not_done';
  1631. $context['start'] = empty($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  1632. $context['start_time'] = time();
  1633. // First time we do this?
  1634. $id_board_from = isset($_REQUEST['id_board_from']) ? (int) $_REQUEST['id_board_from'] : 0;
  1635. $id_board_to = isset($_REQUEST['id_board_to']) ? (int) $_REQUEST['id_board_to'] : 0;
  1636. $max_days = isset($_REQUEST['maxdays']) ? (int) $_REQUEST['maxdays'] : 0;
  1637. $locked = isset($_POST['move_type_locked']) || isset($_GET['locked']);
  1638. $sticky = isset($_POST['move_type_sticky']) || isset($_GET['sticky']);
  1639. // No boards then this is your stop.
  1640. if (empty($id_board_from) || empty($id_board_to))
  1641. return;
  1642. // The big WHERE clause
  1643. $conditions = 'WHERE t.id_board = {int:id_board_from}
  1644. AND m.icon != {string:moved}';
  1645. // DB parameters
  1646. $params = array(
  1647. 'id_board_from' => $id_board_from,
  1648. 'moved' => 'moved',
  1649. );
  1650. // Only moving topics not posted in for x days?
  1651. if (!empty($max_days))
  1652. {
  1653. $conditions .= '
  1654. AND m.poster_time < {int:poster_time}';
  1655. $params['poster_time'] = time() - 3600 * 24 * $max_days;
  1656. }
  1657. // Moving locked topics?
  1658. if ($locked)
  1659. {
  1660. $conditions .= '
  1661. AND t.locked = {int:locked}';
  1662. $params['locked'] = 1;
  1663. }
  1664. // What about sticky topics?
  1665. if ($sticky)
  1666. {
  1667. $conditions .= '
  1668. AND t.is_sticky = {int:sticky}';
  1669. $params['sticky'] = 1;
  1670. }
  1671. // How many topics are we converting?
  1672. if (!isset($_REQUEST['totaltopics']))
  1673. {
  1674. $request = $smcFunc['db_query']('', '
  1675. SELECT COUNT(*)
  1676. FROM {db_prefix}topics AS t
  1677. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)' .
  1678. $conditions,
  1679. $params
  1680. );
  1681. list ($total_topics) = $smcFunc['db_fetch_row']($request);
  1682. $smcFunc['db_free_result']($request);
  1683. }
  1684. else
  1685. $total_topics = (int) $_REQUEST['totaltopics'];
  1686. // Seems like we need this here.
  1687. $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 . ';max_days=' . $max_days;
  1688. if ($locked)
  1689. $context['continue_get_data'] .= ';locked';
  1690. if ($sticky)
  1691. $context['continue_get_data'] .= ';sticky';
  1692. $context['continue_get_data'] .= ';start=' . $context['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1693. // We have topics to move so start the process.
  1694. if (!empty($total_topics))
  1695. {
  1696. while ($context['start'] <= $total_topics)
  1697. {
  1698. // Lets get the topics.
  1699. $request = $smcFunc['db_query']('', '
  1700. SELECT t.id_topic
  1701. FROM {db_prefix}topics AS t
  1702. INNER JOIN {db_prefix}messages AS m ON (m.id_msg = t.id_last_msg)
  1703. ' . $conditions . '
  1704. LIMIT 10',
  1705. $params
  1706. );
  1707. // Get the ids.
  1708. $topics = array();
  1709. while ($row = $smcFunc['db_fetch_assoc']($request))
  1710. $topics[] = $row['id_topic'];
  1711. // Just return if we don't have any topics left to move.
  1712. if (empty($topics))
  1713. {
  1714. cache_put_data('board-' . $id_board_from, null, 120);
  1715. cache_put_data('board-' . $id_board_to, null, 120);
  1716. redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
  1717. }
  1718. // Lets move them.
  1719. require_once($sourcedir . '/MoveTopic.php');
  1720. moveTopics($topics, $id_board_to);
  1721. // We've done at least ten more topics.
  1722. $context['start'] += 10;
  1723. // Lets wait a while.
  1724. if (time() - $context['start_time'] > 3)
  1725. {
  1726. // What's the percent?
  1727. $context['continue_percent'] = round(100 * ($context['start'] / $total_topics), 1);
  1728. $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'];
  1729. // Let the template system do it's thang.
  1730. return;
  1731. }
  1732. }
  1733. }
  1734. // Don't confuse admins by having an out of date cache.
  1735. cache_put_data('board-' . $id_board_from, null, 120);
  1736. cache_put_data('board-' . $id_board_to, null, 120);
  1737. redirectexit('action=admin;area=maintain;sa=topics;done=massmove');
  1738. }
  1739. /**
  1740. * Recalculate all members post counts
  1741. * it requires the admin_forum permission.
  1742. *
  1743. * - recounts all posts for members found in the message table
  1744. * - updates the members post count record in the members talbe
  1745. * - honors the boards post count flag
  1746. * - does not count posts in the recyle bin
  1747. * - zeros post counts for all members with no posts in the message table
  1748. * - runs as a delayed loop to avoid server overload
  1749. * - uses the not_done template in Admin.template
  1750. *
  1751. * The function redirects back to action=admin;area=maintain;sa=members when complete.
  1752. * It is accessed via ?action=admin;area=maintain;sa=members;activity=recountposts
  1753. */
  1754. function MaintainRecountPosts()
  1755. {
  1756. global $txt, $context, $modSettings, $smcFunc;
  1757. // You have to be allowed in here
  1758. isAllowedTo('admin_forum');
  1759. checkSession('request');
  1760. // Set up to the context.
  1761. $context['page_title'] = $txt['not_done_title'];
  1762. $context['continue_countdown'] = 3;
  1763. $context['continue_get_data'] = '';
  1764. $context['sub_template'] = 'not_done';
  1765. // init
  1766. $increment = 200;
  1767. $_REQUEST['start'] = !isset($_REQUEST['start']) ? 0 : (int) $_REQUEST['start'];
  1768. // Ask for some extra time, on big boards this may take a bit
  1769. @set_time_limit(600);
  1770. // Only run this query if we don't have the total number of members that have posted
  1771. if (!isset($_SESSION['total_members']))
  1772. {
  1773. validateToken('admin-maint');
  1774. $request = $smcFunc['db_query']('', '
  1775. SELECT COUNT(DISTINCT m.id_member)
  1776. FROM ({db_prefix}messages AS m, {db_prefix}boards AS b)
  1777. WHERE m.id_member != 0
  1778. AND b.count_posts = 0
  1779. AND m.id_board = b.id_board',
  1780. array(
  1781. )
  1782. );
  1783. // save it so we don't do this again for this task
  1784. list ($_SESSION['total_members']) = $smcFunc['db_fetch_row']($request);
  1785. $smcFunc['db_free_result']($request);
  1786. }
  1787. else
  1788. validateToken('admin-recountposts');
  1789. // Lets get a group of members and determine their post count (from the boards that have post count enabled of course).
  1790. $request = $smcFunc['db_query']('', '
  1791. SELECT /*!40001 SQL_NO_CACHE */ m.id_member, COUNT(m.id_member) AS posts
  1792. FROM ({db_prefix}messages AS m, {db_prefix}boards AS b)
  1793. WHERE m.id_member != {int:zero}
  1794. AND b.count_posts = {int:zero}
  1795. AND m.id_board = b.id_board ' . (!empty($modSettings['recycle_enable']) ? '
  1796. AND b.id_board != {int:recycle}' : '') . '
  1797. GROUP BY m.id_member
  1798. LIMIT {int:start}, {int:number}',
  1799. array(
  1800. 'start' => $_REQUEST['start'],
  1801. 'number' => $increment,
  1802. 'recycle' => $modSettings['recycle_board'],
  1803. 'zero' => 0,
  1804. )
  1805. );
  1806. $total_rows = $smcFunc['db_num_rows']($request);
  1807. // Update the post count for this group
  1808. while ($row = $smcFunc['db_fetch_assoc']($request))
  1809. {
  1810. $smcFunc['db_query']('', '
  1811. UPDATE {db_prefix}members
  1812. SET posts = {int:posts}
  1813. WHERE id_member = {int:row}',
  1814. array(
  1815. 'row' => $row['id_member'],
  1816. 'posts' => $row['posts'],
  1817. )
  1818. );
  1819. }
  1820. $smcFunc['db_free_result']($request);
  1821. // Continue?
  1822. if ($total_rows == $increment)
  1823. {
  1824. $_REQUEST['start'] += $increment;
  1825. $context['continue_get_data'] = '?action=admin;area=maintain;sa=members;activity=recountposts;start=' . $_REQUEST['start'] . ';' . $context['session_var'] . '=' . $context['session_id'];
  1826. $context['continue_percent'] = round(100 * $_REQUEST['start'] / $_SESSION['total_members']);
  1827. createToken('admin-recountposts');
  1828. $context['continue_post_data'] = '<input type="hidden" name="' . $context['admin-recountposts_token_var'] . '" value="' . $context['admin-recountposts_token'] . '">';
  1829. if (function_exists('apache_reset_timeout'))
  1830. apache_reset_timeout();
  1831. return;
  1832. }
  1833. // final steps ... made more difficult since we don't yet support sub-selects on joins
  1834. // place all members who have posts in the message table in a temp table
  1835. $createTemporary = $smcFunc['db_query']('', '
  1836. CREATE TEMPORARY TABLE {db_prefix}tmp_maint_recountposts (
  1837. id_member mediumint(8) unsigned NOT NULL default {string:string_zero},
  1838. PRIMARY KEY (id_member)
  1839. )
  1840. SELECT m.id_member
  1841. FROM ({db_prefix}messages AS m,{db_prefix}boards AS b)
  1842. WHERE m.id_member != {int:zero}
  1843. AND b.count_posts = {int:zero}
  1844. AND m.id_board = b.id_board ' . (!empty($modSettings['recycle_enable']) ? '
  1845. AND b.id_board != {int:recycle}' : '') . '
  1846. GROUP BY m.id_member',
  1847. array(
  1848. 'zero' => 0,
  1849. 'string_zero' => '0',
  1850. 'db_error_skip' => true,
  1851. 'recycle' => !empty($modSettings['recycle_board']) ? $modSettings['recycle_board'] : 0,
  1852. )
  1853. ) !== false;
  1854. if ($createTemporary)
  1855. {
  1856. // outer join the members table on the temporary table finding the members that have a post count but no posts in the message table
  1857. $request = $smcFunc['db_query']('', '
  1858. SELECT mem.id_member, mem.posts
  1859. FROM {db_prefix}members AS mem
  1860. LEFT OUTER JOIN {db_prefix}tmp_maint_recountposts AS res
  1861. ON res.id_member = mem.id_member
  1862. WHERE res.id_member IS null
  1863. AND mem.posts != {int:zero}',
  1864. array(
  1865. 'zero' => 0,
  1866. )
  1867. );
  1868. // set the post count to zero for any delinquents we may have found
  1869. while ($row = $smcFunc['db_fetch_assoc']($request))
  1870. {
  1871. $smcFunc['db_query']('', '
  1872. UPDATE {db_prefix}members
  1873. SET posts = {int:zero}
  1874. WHERE id_member = {int:row}',
  1875. array(
  1876. 'row' => $row['id_member'],
  1877. 'zero' => 0,
  1878. )
  1879. );
  1880. }
  1881. $smcFunc['db_free_result']($request);
  1882. }
  1883. // all done
  1884. unset($_SESSION['total_members']);
  1885. $context['maintenance_finished'] = $txt['maintain_recountposts'];
  1886. redirectexit('action=admin;area=maintain;sa=members;done=recountposts');
  1887. }
  1888. /**
  1889. * Generates a list of integration hooks for display
  1890. * Accessed through ?action=admin;area=maintain;sa=hooks;
  1891. * Allows for removal or disabling of selected hooks
  1892. */
  1893. function list_integration_hooks()
  1894. {
  1895. global $sourcedir, $scripturl, $context, $txt, $settings;
  1896. $context['filter_url'] = '';
  1897. $context['current_filter'] = '';
  1898. $currentHooks = get_integration_hooks();
  1899. if (isset($_GET['filter']) && in_array($_GET['filter'], array_keys($currentHooks)))
  1900. {
  1901. $context['filter_url'] = ';filter=' . $_GET['filter'];
  1902. $context['current_filter'] = $_GET['filter'];
  1903. }
  1904. if (!empty($_REQUEST['do']) && isset($_REQUEST['hook']) && isset($_REQUEST['function']))
  1905. {
  1906. checkSession('request');
  1907. validateToken('admin-hook', 'request');
  1908. if ($_REQUEST['do'] == 'remove')
  1909. remove_integration_function($_REQUEST['hook'], urldecode($_REQUEST['function']));
  1910. else
  1911. {
  1912. if ($_REQUEST['do'] == 'disable')
  1913. {
  1914. // It's a hack I know...but I'm way too lazy!!!
  1915. $function_remove = $_REQUEST['function'];
  1916. $function_add = $_REQUEST['function'] . ']';
  1917. }
  1918. else
  1919. {
  1920. $function_remove = $_REQUEST['function'] . ']';
  1921. $function_add = $_REQUEST['function'];
  1922. }
  1923. $file = !empty($_REQUEST['includedfile']) ? urldecode($_REQUEST['includedfile']) : '';
  1924. remove_integration_function($_REQUEST['hook'], $function_remove, $file);
  1925. add_integration_function($_REQUEST['hook'], $function_add, $file);
  1926. redirectexit('action=admin;area=maintain;sa=hooks' . $context['filter_url']);
  1927. }
  1928. }
  1929. $list_options = array(
  1930. 'id' => 'list_integration_hooks',
  1931. 'title' => $txt['hooks_title_list'],
  1932. 'items_per_page' => 20,
  1933. 'base_href' => $scripturl . '?action=admin;area=maintain;sa=hooks' . $context['filter_url'] . ';' . $context['session_var'] . '=' . $context['session_id'],
  1934. 'default_sort_col' => 'hook_name',
  1935. 'get_items' => array(
  1936. 'function' => 'get_integration_hooks_data',
  1937. ),
  1938. 'get_count' => array(
  1939. 'function' => 'get_integration_hooks_count',
  1940. ),
  1941. 'no_items_label' => $txt['hooks_no_hooks'],
  1942. 'columns' => array(
  1943. 'hook_name' => array(
  1944. 'header' => array(
  1945. 'value' => $txt['hooks_field_hook_name'],
  1946. ),
  1947. 'data' => array(
  1948. 'db' => 'hook_name',
  1949. ),
  1950. 'sort' => array(
  1951. 'default' => 'hook_name',
  1952. 'reverse' => 'hook_name DESC',
  1953. ),
  1954. ),
  1955. 'function_name' => array(
  1956. 'header' => array(
  1957. 'value' => $txt['hooks_field_function_name'],
  1958. ),
  1959. 'data' => array(
  1960. 'function' => create_function('$data', '
  1961. global $txt;
  1962. if (!empty($data[\'included_file\']))
  1963. return $txt[\'hooks_field_function\'] . \': \' . $data[\'real_function\'] . \'<br>\' . $txt[\'hooks_field_included_file\'] . \': \' . $data[\'included_file\'] . (!empty($data[\'instance\']) ? \'<br>\'. $txt[\'hooks_field_function_method\'] : \'\');
  1964. else
  1965. return $data[\'real_function\'];
  1966. '),
  1967. ),
  1968. 'sort' => array(
  1969. 'default' => 'function_name',
  1970. 'reverse' => 'function_name DESC',
  1971. ),
  1972. ),
  1973. 'file_name' => array(
  1974. 'header' => array(
  1975. 'value' => $txt['hooks_field_file_name'],
  1976. ),
  1977. 'data' => array(
  1978. 'db' => 'file_name',
  1979. ),
  1980. 'sort' => array(
  1981. 'default' => 'file_name',
  1982. 'reverse' => 'file_name DESC',
  1983. ),
  1984. ),
  1985. 'status' => array(
  1986. 'header' => array(
  1987. 'value' => $txt['hooks_field_hook_exists'],
  1988. 'style' => 'width:3%;',
  1989. ),
  1990. 'data' => array(
  1991. 'function' => create_function('$data', '
  1992. global $txt, $settings, $scripturl, $context;
  1993. $change_status = array(\'before\' => \'\', \'after\' => \'\');
  1994. if ($data[\'can_be_disabled\'] && $data[\'status\'] != \'deny\')
  1995. {
  1996. $change_status[\'before\'] = \'<a href="\' . $scripturl . \'?action=admin;area=maintain;sa=hooks;do=\' . ($data[\'enabled\'] ? \'disable\' : \'enable\') . \';hook=\' . $data[\'hook_name\'] . \';function=\' . $data[\'real_function\'] . (!empty($data[\'included_file\']) ? \';includedfile=\' . urlencode($data[\'included_file\']) : \'\') . $context[\'filter_url\'] . \';\' . $context[\'admin-hook_token_var\'] . \'=\' . $context[\'admin-hook_token\'] . \';\' . $context[\'session_var\'] . \'=\' . $context[\'session_id\'] . \'" onclick="return confirm(\' . javaScriptEscape($txt[\'quickmod_confirm\']) . \');">\';
  1997. $change_status[\'after\'] = \'</a>\';
  1998. }
  1999. return $change_status[\'before\'] . \'<img src="\' . $settings[\'images_url\'] . \'/admin/post_moderation_\' . $data[\'status\'] . \'.png" alt="\' . $data[\'img_text\'] . \'" title="\' . $data[\'img_text\'] . \'">\' . $change_status[\'after\'];
  2000. '),
  2001. 'class' => 'centertext',
  2002. ),
  2003. 'sort' => array(
  2004. 'default' => 'status',
  2005. 'reverse' => 'status DESC',
  2006. ),
  2007. ),
  2008. ),
  2009. 'additional_rows' => array(
  2010. array(
  2011. 'position' => 'after_title',
  2012. 'value' => $txt['hooks_disable_instructions'] . '<br>
  2013. ' . $txt['hooks_disable_legend'] . ':
  2014. <ul style="list-style: none;">
  2015. <li><img src="' . $settings['images_url'] . '/admin/post_moderation_allow.png" alt="' . $txt['hooks_active'] . '" title="' . $txt['hooks_active'] . '"> ' . $txt['hooks_disable_legend_exists'] . '</li>
  2016. <li><img src="' . $settings['images_url'] . '/admin/post_moderation_moderate.png" alt="' . $txt['hooks_disabled'] . '" title="' . $txt['hooks_disabled'] . '"> ' . $txt['hooks_disable_legend_disabled'] . '</li>
  2017. <li><img src="' . $settings['images_url'] . '/admin/post_moderation_deny.png" alt="' . $txt['hooks_missing'] . '" title="' . $txt['hooks_missing'] . '"> ' . $txt['hooks_disable_legend_missing'] . '</li>
  2018. </ul>'
  2019. ),
  2020. ),
  2021. );
  2022. createToken('admin-hook', 'request');
  2023. $list_options['columns']['remove'] = array(
  2024. 'header' => array(
  2025. 'value' => $txt['hooks_button_remove'],
  2026. 'style' => 'width:3%',
  2027. ),
  2028. 'data' => array(
  2029. 'function' => create_function('$data', '
  2030. global $txt, $scripturl, $context;
  2031. if (!$data[\'hook_exists\'])
  2032. return \'
  2033. <a href="\' . $scripturl . \'?action=admin;area=maintain;sa=hooks;do=remove;hook=\' . $data[\'hook_name\'] . \';function=\' . urlencode($data[\'function_name\']) . $context[\'filter_url\'] . \';\' . $context[\'admin-hook_token_var\'] . \'=\' . $context[\'admin-hook_token\'] . \';\' . $context[\'session_var\'] . \'=\' . $context[\'session_id\'] . \'" onclick="return confirm(\' . JavaScriptEscape($txt[\'quickmod_confirm\']) . \');">
  2034. <span class="generic_icons delete" title="\' . $txt[\'hooks_button_remove\'] . \'"></span>
  2035. </a>\';
  2036. '),
  2037. 'class' => 'centertext',
  2038. ),
  2039. );
  2040. $list_options['form'] = array(
  2041. 'href' => $scripturl . '?action=admin;area=maintain;sa=hooks' . $context['filter_url'] . ';' . $context['session_var'] . '=' . $context['session_id'],
  2042. 'name' => 'list_integration_hooks',
  2043. );
  2044. require_once($sourcedir . '/Subs-List.php');
  2045. createList($list_options);
  2046. $context['page_title'] = $txt['hooks_title_list'];
  2047. $context['sub_template'] = 'show_list';
  2048. $context['default_list'] = 'list_integration_hooks';
  2049. }
  2050. /**
  2051. * Gets all of the files in a directory and its children directories
  2052. *
  2053. * @param type $dir_path
  2054. * @return array
  2055. */
  2056. function get_files_recursive($dir_path)
  2057. {
  2058. $files = array();
  2059. if ($dh = opendir($dir_path))
  2060. {
  2061. while (($file = readdir($dh)) !== false)
  2062. {
  2063. if ($file != '.' && $file != '..')
  2064. {
  2065. if (is_dir($dir_path . '/' . $file))
  2066. $files = array_merge($files, get_files_recursive($dir_path . '/' . $file));
  2067. else
  2068. $files[] = array('dir' => $dir_path, 'name' => $file);
  2069. }
  2070. }
  2071. }
  2072. closedir($dh);
  2073. return $files;
  2074. }
  2075. /**
  2076. * Callback function for the integration hooks list (list_integration_hooks)
  2077. * Gets all of the hooks in the system and their status
  2078. * Would be better documented if Ema was not lazy
  2079. *
  2080. * @param type $start
  2081. * @param type $per_page
  2082. * @param type $sort
  2083. * @return array
  2084. */
  2085. function get_integration_hooks_data($start, $per_page, $sort)
  2086. {
  2087. global $boarddir, $sourcedir, $settings, $txt, $context, $scripturl;
  2088. $hooks = $temp_hooks = get_integration_hooks();
  2089. $hooks_data = $temp_data = $hook_status = array();
  2090. $files = get_files_recursive($sourcedir);
  2091. if (!empty($files))
  2092. {
  2093. foreach ($files as $file)
  2094. {
  2095. if (is_file($file['dir'] . '/' . $file['name']) && substr($file['name'], -4) === '.php')
  2096. {
  2097. $fp = fopen($file['dir'] . '/' . $file['name'], 'rb');
  2098. $fc = fread($fp, filesize($file['dir'] . '/' . $file['name']));
  2099. fclose($fp);
  2100. foreach ($temp_hooks as $hook => $functions)
  2101. {
  2102. foreach ($functions as $function_o)
  2103. {
  2104. $object = false;
  2105. if (strpos($function_o, '#') !== false)
  2106. {
  2107. $function_o = str_replace('#', '', $function_o);
  2108. $object = true;
  2109. }
  2110. $hook_name = str_replace(']', '', $function_o);
  2111. if (strpos($hook_name, '::') !== false)
  2112. {
  2113. $function = explode('::', $hook_name);
  2114. $function = $function[1];
  2115. }
  2116. else
  2117. $function = $hook_name;
  2118. $function = explode(':', $function);
  2119. $function = $function[0];
  2120. if (substr($hook, -8) === '_include')
  2121. {
  2122. $hook_status[$hook][$function]['exists'] = file_exists(strtr(trim($function), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir'])));
  2123. // I need to know if there is at least one function called in this file.
  2124. $temp_data['include'][basename($function)] = array('hook' => $hook, 'function' => $function);
  2125. unset($temp_hooks[$hook][$function_o]);
  2126. }
  2127. elseif (strpos(str_replace(' (', '(', $fc), 'function ' . trim($function) . '(') !== false)
  2128. {
  2129. $hook_status[$hook][$function]['exists'] = true;
  2130. $hook_status[$hook][$function]['in_file'] = $file['name'];
  2131. $hook_status[$hook][$function]['instance'] = $object;
  2132. // I want to remember all the functions called within this file (to check later if they are enabled or disabled and decide if the integrare_*_include of that file can be disabled too)
  2133. $temp_data['function'][$file['name']][] = $function_o;
  2134. unset($temp_hooks[$hook][$function_o]);
  2135. }
  2136. }
  2137. }
  2138. }
  2139. }
  2140. }
  2141. $sort_types = array(
  2142. 'hook_name' => array('hook', SORT_ASC),
  2143. 'hook_name DESC' => array('hook', SORT_DESC),
  2144. 'function_name' => array('function', SORT_ASC),
  2145. 'function_name DESC' => array('function', SORT_DESC),
  2146. 'file_name' => array('file_name', SORT_ASC),
  2147. 'file_name DESC' => array('file_name', SORT_DESC),
  2148. 'status' => array('status', SORT_ASC),
  2149. 'status DESC' => array('status', SORT_DESC),
  2150. );
  2151. $sort_options = $sort_types[$sort];
  2152. $sort = array();
  2153. $hooks_filters = array();
  2154. foreach ($hooks as $hook => $functions)
  2155. {
  2156. $hooks_filters[] = '<option' . ($context['current_filter'] == $hook ? ' selected ' : '') . ' value="' . $hook . '">' . $hook . '</option>';
  2157. foreach ($functions as $function)
  2158. {
  2159. $enabled = strstr($function, ']') === false;
  2160. $function = str_replace(']', '', $function);
  2161. // This is a not an include and the function is included in a certain file (if not it doesn't exists so don't care)
  2162. if (substr($hook, -8) !== '_include' && isset($hook_status[$hook][$function]['in_file']))
  2163. {
  2164. $current_hook = isset($temp_data['include'][$hook_status[$hook][$function]['in_file']]) ? $temp_data['include'][$hook_status[$hook][$function]['in_file']] : '';
  2165. $enabled = false;
  2166. // Checking all the functions within this particular file
  2167. // if any of them is enable then the file *must* be included and the integrate_*_include hook cannot be disabled
  2168. foreach ($temp_data['function'][$hook_status[$hook][$function]['in_file']] as $func)
  2169. $enabled = $enabled || strstr($func, ']') !== false;
  2170. if (!$enabled && !empty($current_hook))
  2171. $hook_status[$current_hook['hook']][$current_hook['function']]['enabled'] = true;
  2172. }
  2173. }
  2174. }
  2175. if (!empty($hooks_filters))
  2176. $context['insert_after_template'] .= '
  2177. <script><!-- // --><![CDATA[
  2178. var hook_name_header = document.getElementById(\'header_list_integration_hooks_hook_name\');
  2179. hook_name_header.innerHTML += ' . JavaScriptEscape('<select style="margin-left:15px;" onchange="window.location=(\'' . $scripturl . '?action=admin;area=maintain;sa=hooks\' + (this.value ? \';filter=\' + this.value : \'\'));"><option value="">' . $txt['hooks_reset_filter'] . '</option>' . implode('', $hooks_filters) . '</select>'). ';
  2180. // ]]></script>';
  2181. $temp_data = array();
  2182. $id = 0;
  2183. foreach ($hooks as $hook => $functions)
  2184. {
  2185. if (empty($context['filter']) || (!empty($context['filter']) && $context['filter'] == $hook))
  2186. {
  2187. foreach ($functions as $function)
  2188. {
  2189. // Gotta remove the # bit.
  2190. if (strpos($function, '#') !== false)
  2191. $function = str_replace('#', '', $function);
  2192. $enabled = strstr($function, ']') === false;
  2193. $function = str_replace(']', '', $function);
  2194. if (strpos($function, '::') !== false)
  2195. {
  2196. $function = explode('::', $function);
  2197. $function = $function[1];
  2198. }
  2199. $exploded = explode(':', $function);
  2200. $hook_exists = !empty($hook_status[$hook][$exploded[0]]['exists']);
  2201. $file_name = isset($hook_status[$hook][$exploded[0]]['in_file']) ? $hook_status[$hook][$exploded[0]]['in_file'] : ((substr($hook, -8) === '_include') ? 'zzzzzzzzz' : 'zzzzzzzza');
  2202. $sort[] = $$sort_options[0];
  2203. $temp_data[] = array(
  2204. 'id' => 'hookid_' . $id++,
  2205. 'hook_name' => $hook,
  2206. 'function_name' => $function,
  2207. 'real_function' => $exploded[0],
  2208. 'included_file' => isset($exploded[1]) ? strtr(trim($exploded[1]), array('$boarddir' => $boarddir, '$sourcedir' => $sourcedir, '$themedir' => $settings['theme_dir'])) : '',
  2209. 'file_name' => (isset($hook_status[$hook][$exploded[0]]['in_file']) ? $hook_status[$hook][$exploded[0]]['in_file'] : ''),
  2210. 'instance' => (isset($hook_status[$hook][$exploded[0]]['instance']) ? $hook_status[$hook][$exploded[0]]['instance'] : ''),
  2211. 'hook_exists' => $hook_exists,
  2212. 'status' => $hook_exists ? ($enabled ? 'allow' : 'moderate') : 'deny',
  2213. 'img_text' => $txt['hooks_' . ($hook_exists ? ($enabled ? 'active' : 'disabled') : 'missing')],
  2214. 'enabled' => $enabled,
  2215. 'can_be_disabled' => !isset($hook_status[$hook][$function]['enabled']),
  2216. );
  2217. }
  2218. }
  2219. }
  2220. array_multisort($sort, $sort_options[1], $temp_data);
  2221. $counter = 0;
  2222. $start++;
  2223. foreach ($temp_data as $data)
  2224. {
  2225. if (++$counter < $start)
  2226. continue;
  2227. elseif ($counter == $start + $per_page)
  2228. break;
  2229. $hooks_data[] = $data;
  2230. }
  2231. return $hooks_data;
  2232. }
  2233. /**
  2234. * Simply returns the total count of integration hooks
  2235. * Used by the integration hooks list function (list_integration_hooks)
  2236. *
  2237. * @global type $context
  2238. * @return int
  2239. */
  2240. function get_integration_hooks_count()
  2241. {
  2242. global $context;
  2243. $hooks = get_integration_hooks();
  2244. $hooks_count = 0;
  2245. $context['filter'] = false;
  2246. if (isset($_GET['filter']))
  2247. $context['filter'] = $_GET['filter'];
  2248. foreach ($hooks as $hook => $functions)
  2249. {
  2250. if (empty($context['filter']) || (!empty($context['filter']) && $context['filter'] == $hook))
  2251. $hooks_count += count($functions);
  2252. }
  2253. return $hooks_count;
  2254. }
  2255. /**
  2256. * Parses modSettings to create integration hook array
  2257. *
  2258. * @staticvar type $integration_hooks
  2259. * @return type
  2260. */
  2261. function get_integration_hooks()
  2262. {
  2263. global $modSettings;
  2264. static $integration_hooks;
  2265. if (!isset($integration_hooks))
  2266. {
  2267. $integration_hooks = array();
  2268. foreach ($modSettings as $key => $value)
  2269. {
  2270. if (!empty($value) && substr($key, 0, 10) === 'integrate_')
  2271. $integration_hooks[$key] = explode(',', $value);
  2272. }
  2273. }
  2274. return $integration_hooks;
  2275. }
  2276. ?>