PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/admin_maintenance.php

https://github.com/Dratone/EveBB
PHP | 381 lines | 318 code | 45 blank | 18 comment | 44 complexity | e976f54ad0adf3e8ab54afe4e18410fd MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2010 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. // Tell header.php to use the admin template
  8. define('PUN_ADMIN_CONSOLE', 1);
  9. // Tell common.php that we don't want output buffering
  10. define('PUN_DISABLE_BUFFERING', 1);
  11. define('PUN_ROOT', dirname(__FILE__).'/');
  12. require PUN_ROOT.'include/common.php';
  13. require PUN_ROOT.'include/common_admin.php';
  14. if ($pun_user['g_id'] != PUN_ADMIN)
  15. message($lang_common['No permission']);
  16. // Load the admin_maintenance.php language file
  17. require PUN_ROOT.'lang/'.$admin_language.'/admin_maintenance.php';
  18. $action = isset($_REQUEST['action']) ? trim($_REQUEST['action']) : '';
  19. if ($action == 'sessions') {
  20. $mode = $_GET['mode'];
  21. if ($mode != 'old' && $mode != 'all') {
  22. message($lang_common['Bad request']);
  23. } //End if.
  24. $sql = 'DELETE FROM '.$db->prefix.'session WHERE ';
  25. if ($mode == 'old') {
  26. $now = gmmktime();
  27. $earlier = $now - $pun_config['session_length'];
  28. $much_earlier = $now - 2629743;
  29. $sql .= 'stamp<'.$much_earlier.' OR (stamp<'.$earlier.' AND length='.$pun_config['session_length'].')';
  30. } else {
  31. $sql .= 'user_id!=0';
  32. } //End if - else.
  33. if (!$db->query($sql)) {
  34. message($lang_admin_maintenance['clear_sessions_error']);
  35. } //End if.
  36. redirect('admin_maintenance.php', $lang_admin_maintenance['clear_sessions_ok']);
  37. } else if ($action == 'rebuild')
  38. {
  39. $per_page = isset($_GET['i_per_page']) ? intval($_GET['i_per_page']) : 0;
  40. $start_at = isset($_GET['i_start_at']) ? intval($_GET['i_start_at']) : 0;
  41. // Check per page is > 0
  42. if ($per_page < 1)
  43. message($lang_admin_maintenance['Posts must be integer message']);
  44. @set_time_limit(0);
  45. // If this is the first cycle of posts we empty the search index before we proceed
  46. if (isset($_GET['i_empty_index']))
  47. {
  48. // This is the only potentially "dangerous" thing we can do here, so we check the referer
  49. confirm_referrer('admin_maintenance.php');
  50. $db->truncate_table('search_matches') or error('Unable to empty search index match table', __FILE__, __LINE__, $db->error());
  51. $db->truncate_table('search_words') or error('Unable to empty search index words table', __FILE__, __LINE__, $db->error());
  52. // Reset the sequence for the search words (not needed for SQLite)
  53. switch ($db_type)
  54. {
  55. case 'mysql':
  56. case 'mysqli':
  57. case 'mysql_innodb':
  58. case 'mysqli_innodb':
  59. $result = $db->query('ALTER TABLE '.$db->prefix.'search_words auto_increment=1') or error('Unable to update table auto_increment', __FILE__, __LINE__, $db->error());
  60. break;
  61. case 'pgsql';
  62. $result = $db->query('SELECT setval(\''.$db->prefix.'search_words_id_seq\', 1, false)') or error('Unable to update sequence', __FILE__, __LINE__, $db->error());
  63. }
  64. }
  65. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_admin_maintenance['Rebuilding search index']);
  66. ?>
  67. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  68. <html>
  69. <head>
  70. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  71. <title><?php echo generate_page_title($page_title) ?></title>
  72. <style type="text/css">
  73. body {
  74. font: 12px Verdana, Arial, Helvetica, sans-serif;
  75. color: #333333;
  76. background-color: #FFFFFF
  77. }
  78. h1 {
  79. font-size: 16px;
  80. font-weight: normal;
  81. }
  82. </style>
  83. </head>
  84. <body>
  85. <h1><?php echo $lang_admin_maintenance['Rebuilding index info'] ?></h1>
  86. <hr />
  87. <?php
  88. $query_str = '';
  89. require PUN_ROOT.'include/search_idx.php';
  90. // Fetch posts to process this cycle
  91. $result = $db->query('SELECT p.id, p.message, t.subject, t.first_post_id FROM '.$db->prefix.'posts AS p INNER JOIN '.$db->prefix.'topics AS t ON t.id=p.topic_id WHERE p.id >= '.$start_at.' ORDER BY p.id ASC LIMIT '.$per_page) or error('Unable to fetch posts', __FILE__, __LINE__, $db->error());
  92. $end_at = 0;
  93. while ($cur_item = $db->fetch_assoc($result))
  94. {
  95. echo '<p><span>'.sprintf($lang_admin_maintenance['Processing post'], $cur_item['id']).'</span></p>'."\n";
  96. if ($cur_item['id'] == $cur_item['first_post_id'])
  97. update_search_index('post', $cur_item['id'], $cur_item['message'], $cur_item['subject']);
  98. else
  99. update_search_index('post', $cur_item['id'], $cur_item['message']);
  100. $end_at = $cur_item['id'];
  101. }
  102. // Check if there is more work to do
  103. if ($end_at > 0)
  104. {
  105. $result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE id > '.$end_at.' ORDER BY id ASC LIMIT 1') or error('Unable to fetch next ID', __FILE__, __LINE__, $db->error());
  106. if ($db->num_rows($result) > 0)
  107. $query_str = '?action=rebuild&i_per_page='.$per_page.'&i_start_at='.$db->result($result);
  108. }
  109. $db->end_transaction();
  110. $db->close();
  111. exit('<script type="text/javascript">window.location="admin_maintenance.php'.$query_str.'"</script><hr /><p>'.sprintf($lang_admin_maintenance['Javascript redirect failed'], '<a href="admin_maintenance.php'.$query_str.'">'.$lang_admin_maintenance['Click here'].'</a>').'</p>');
  112. }
  113. if ($action == 'prune')
  114. {
  115. $prune_from = trim($_POST['prune_from']);
  116. $prune_sticky = intval($_POST['prune_sticky']);
  117. if (isset($_POST['prune_comply']))
  118. {
  119. confirm_referrer('admin_maintenance.php');
  120. $prune_days = intval($_POST['prune_days']);
  121. $prune_date = ($prune_days) ? time() - ($prune_days * 86400) : -1;
  122. @set_time_limit(0);
  123. if ($prune_from == 'all')
  124. {
  125. $result = $db->query('SELECT id FROM '.$db->prefix.'forums') or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());
  126. $num_forums = $db->num_rows($result);
  127. for ($i = 0; $i < $num_forums; ++$i)
  128. {
  129. $fid = $db->result($result, $i);
  130. prune($fid, $prune_sticky, $prune_date);
  131. update_forum($fid);
  132. }
  133. }
  134. else
  135. {
  136. $prune_from = intval($prune_from);
  137. prune($prune_from, $prune_sticky, $prune_date);
  138. update_forum($prune_from);
  139. }
  140. // Locate any "orphaned redirect topics" and delete them
  141. $result = $db->query('SELECT t1.id FROM '.$db->prefix.'topics AS t1 LEFT JOIN '.$db->prefix.'topics AS t2 ON t1.moved_to=t2.id WHERE t2.id IS NULL AND t1.moved_to IS NOT NULL') or error('Unable to fetch redirect topics', __FILE__, __LINE__, $db->error());
  142. $num_orphans = $db->num_rows($result);
  143. if ($num_orphans)
  144. {
  145. for ($i = 0; $i < $num_orphans; ++$i)
  146. $orphans[] = $db->result($result, $i);
  147. $db->query('DELETE FROM '.$db->prefix.'topics WHERE id IN('.implode(',', $orphans).')') or error('Unable to delete redirect topics', __FILE__, __LINE__, $db->error());
  148. }
  149. redirect('admin_maintenance.php', $lang_admin_maintenance['Posts pruned redirect']);
  150. }
  151. $prune_days = trim($_POST['req_prune_days']);
  152. if ($prune_days == '' || preg_match('/[^0-9]/', $prune_days))
  153. message($lang_admin_maintenance['Days must be integer message']);
  154. $prune_date = time() - ($prune_days * 86400);
  155. // Concatenate together the query for counting number of topics to prune
  156. $sql = 'SELECT COUNT(id) FROM '.$db->prefix.'topics WHERE last_post<'.$prune_date.' AND moved_to IS NULL';
  157. if ($prune_sticky == '0')
  158. $sql .= ' AND sticky=0';
  159. if ($prune_from != 'all')
  160. {
  161. $prune_from = intval($prune_from);
  162. $sql .= ' AND forum_id='.$prune_from;
  163. // Fetch the forum name (just for cosmetic reasons)
  164. $result = $db->query('SELECT forum_name FROM '.$db->prefix.'forums WHERE id='.$prune_from) or error('Unable to fetch forum name', __FILE__, __LINE__, $db->error());
  165. $forum = '"'.pun_htmlspecialchars($db->result($result)).'"';
  166. }
  167. else
  168. $forum = $lang_admin_maintenance['All forums'];
  169. $result = $db->query($sql) or error('Unable to fetch topic prune count', __FILE__, __LINE__, $db->error());
  170. $num_topics = $db->result($result);
  171. if (!$num_topics)
  172. message(sprintf($lang_admin_maintenance['No old topics message'], $prune_days));
  173. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_admin_common['Admin'], $lang_admin_common['Prune']);
  174. define('PUN_ACTIVE_PAGE', 'admin');
  175. require PUN_ROOT.'header.php';
  176. generate_admin_menu('maintenance');
  177. ?>
  178. <div class="blockform">
  179. <h2><span><?php echo $lang_admin_maintenance['Prune head'] ?></span></h2>
  180. <div class="box">
  181. <form method="post" action="admin_maintenance.php">
  182. <div class="inform">
  183. <input type="hidden" name="action" value="prune" />
  184. <input type="hidden" name="prune_days" value="<?php echo $prune_days ?>" />
  185. <input type="hidden" name="prune_sticky" value="<?php echo $prune_sticky ?>" />
  186. <input type="hidden" name="prune_from" value="<?php echo $prune_from ?>" />
  187. <fieldset>
  188. <legend><?php echo $lang_admin_maintenance['Confirm prune subhead'] ?></legend>
  189. <div class="infldset">
  190. <p><?php printf($lang_admin_maintenance['Confirm prune info'], $prune_days, $forum, forum_number_format($num_topics)) ?></p>
  191. <p class="warntext"><?php echo $lang_admin_maintenance['Confirm prune warn'] ?></p>
  192. </div>
  193. </fieldset>
  194. </div>
  195. <p class="buttons"><input type="submit" name="prune_comply" value="<?php echo $lang_admin_common['Prune'] ?>" /><a href="javascript:history.go(-1)"><?php echo $lang_admin_common['Go back'] ?></a></p>
  196. </form>
  197. </div>
  198. </div>
  199. <div class="clearer"></div>
  200. </div>
  201. <?php
  202. require PUN_ROOT.'footer.php';
  203. exit;
  204. }
  205. // Get the first post ID from the db
  206. $result = $db->query('SELECT id FROM '.$db->prefix.'posts ORDER BY id ASC LIMIT 1') or error('Unable to fetch topic info', __FILE__, __LINE__, $db->error());
  207. if ($db->num_rows($result))
  208. $first_id = $db->result($result);
  209. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_admin_common['Admin'], $lang_admin_common['Maintenance']);
  210. define('PUN_ACTIVE_PAGE', 'admin');
  211. require PUN_ROOT.'header.php';
  212. generate_admin_menu('maintenance');
  213. ?>
  214. <div class="blockform">
  215. <h2><span><?php echo $lang_admin_maintenance['Maintenance head'] ?></span></h2>
  216. <div class="box">
  217. <form method="get" action="admin_maintenance.php">
  218. <div class="inform">
  219. <input type="hidden" name="action" value="sessions" />
  220. <fieldset>
  221. <legend><?php echo $lang_admin_maintenance['clear_sessions'] ?></legend>
  222. <div class="infldset">
  223. <table class="aligntop" cellspacing="0">
  224. <tr>
  225. <th scope="row"><?php echo $lang_admin_maintenance['clear_old_sessions'] ?></th>
  226. <td>
  227. <a href="admin_maintenance.php?action=sessions&amp;mode=old">[<?php echo $lang_admin_maintenance['clear_old_sessions'] ?>]</a><br/>
  228. <span><?php echo $lang_admin_maintenance['clear_old_sessions_info'] ?></span>
  229. </td>
  230. </tr>
  231. <tr>
  232. <th scope="row"><?php echo $lang_admin_maintenance['clear_all_sessions'] ?></th>
  233. <td>
  234. <a href="admin_maintenance.php?action=sessions&amp;mode=all">[<?php echo $lang_admin_maintenance['clear_all_sessions'] ?>]</a><br/>
  235. <span><?php echo $lang_admin_maintenance['clear_all_sessions_info'] ?></span>
  236. </td>
  237. </tr>
  238. </table>
  239. </div>
  240. </fieldset>
  241. </div>
  242. </form>
  243. <form method="get" action="admin_maintenance.php">
  244. <div class="inform">
  245. <input type="hidden" name="action" value="rebuild" />
  246. <fieldset>
  247. <legend><?php echo $lang_admin_maintenance['Rebuild index subhead'] ?></legend>
  248. <div class="infldset">
  249. <p><?php printf($lang_admin_maintenance['Rebuild index info'], '<a href="admin_options.php#maintenance">'.$lang_admin_common['Maintenance mode'].'</a>') ?></p>
  250. <table class="aligntop" cellspacing="0">
  251. <tr>
  252. <th scope="row"><?php echo $lang_admin_maintenance['Posts per cycle label'] ?></th>
  253. <td>
  254. <input type="text" name="i_per_page" size="7" maxlength="7" value="300" tabindex="1" />
  255. <span><?php echo $lang_admin_maintenance['Posts per cycle help'] ?></span>
  256. </td>
  257. </tr>
  258. <tr>
  259. <th scope="row"><?php echo $lang_admin_maintenance['Starting post label'] ?></th>
  260. <td>
  261. <input type="text" name="i_start_at" size="7" maxlength="7" value="<?php echo (isset($first_id)) ? $first_id : 0 ?>" tabindex="2" />
  262. <span><?php echo $lang_admin_maintenance['Starting post help'] ?></span>
  263. </td>
  264. </tr>
  265. <tr>
  266. <th scope="row"><?php echo $lang_admin_maintenance['Empty index label'] ?></th>
  267. <td class="inputadmin">
  268. <span><input type="checkbox" name="i_empty_index" value="1" tabindex="3" checked="checked" />&#160;&#160;<?php echo $lang_admin_maintenance['Empty index help'] ?></span>
  269. </td>
  270. </tr>
  271. </table>
  272. <p class="topspace"><?php echo $lang_admin_maintenance['Rebuild completed info'] ?></p>
  273. <div class="fsetsubmit"><input type="submit" name="rebuild_index" value="<?php echo $lang_admin_maintenance['Rebuild index'] ?>" tabindex="4" /></div>
  274. </div>
  275. </fieldset>
  276. </div>
  277. </form>
  278. <form method="post" action="admin_maintenance.php" onsubmit="return process_form(this)">
  279. <div class="inform">
  280. <input type="hidden" name="action" value="prune" />
  281. <fieldset>
  282. <legend><?php echo $lang_admin_maintenance['Prune subhead'] ?></legend>
  283. <div class="infldset">
  284. <table class="aligntop" cellspacing="0">
  285. <tr>
  286. <th scope="row"><?php echo $lang_admin_maintenance['Days old label'] ?></th>
  287. <td>
  288. <input type="text" name="req_prune_days" size="3" maxlength="3" tabindex="5" />
  289. <span><?php echo $lang_admin_maintenance['Days old help'] ?></span>
  290. </td>
  291. </tr>
  292. <tr>
  293. <th scope="row"><?php echo $lang_admin_maintenance['Prune sticky label'] ?></th>
  294. <td>
  295. <input type="radio" name="prune_sticky" value="1" tabindex="6" checked="checked" />&#160;<strong><?php echo $lang_admin_common['Yes'] ?></strong>&#160;&#160;&#160;<input type="radio" name="prune_sticky" value="0" />&#160;<strong><?php echo $lang_admin_common['No'] ?></strong>
  296. <span><?php echo $lang_admin_maintenance['Prune sticky help'] ?></span>
  297. </td>
  298. </tr>
  299. <tr>
  300. <th scope="row"><?php echo $lang_admin_maintenance['Prune from label'] ?></th>
  301. <td>
  302. <select name="prune_from" tabindex="7">
  303. <option value="all"><?php echo $lang_admin_maintenance['All forums'] ?></option>
  304. <?php
  305. $result = $db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name FROM '.$db->prefix.'categories AS c INNER JOIN '.$db->prefix.'forums AS f ON c.id=f.cat_id WHERE f.redirect_url IS NULL ORDER BY c.disp_position, c.id, f.disp_position') or error('Unable to fetch category/forum list', __FILE__, __LINE__, $db->error());
  306. $cur_category = 0;
  307. while ($forum = $db->fetch_assoc($result))
  308. {
  309. if ($forum['cid'] != $cur_category) // Are we still in the same category?
  310. {
  311. if ($cur_category)
  312. echo "\t\t\t\t\t\t\t\t\t\t\t".'</optgroup>'."\n";
  313. echo "\t\t\t\t\t\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($forum['cat_name']).'">'."\n";
  314. $cur_category = $forum['cid'];
  315. }
  316. echo "\t\t\t\t\t\t\t\t\t\t\t\t".'<option value="'.$forum['fid'].'">'.pun_htmlspecialchars($forum['forum_name']).'</option>'."\n";
  317. }
  318. ?>
  319. </optgroup>
  320. </select>
  321. <span><?php echo $lang_admin_maintenance['Prune from help'] ?></span>
  322. </td>
  323. </tr>
  324. </table>
  325. <p class="topspace"><?php printf($lang_admin_maintenance['Prune info'], '<a href="admin_options.php#maintenance">'.$lang_admin_common['Maintenance mode'].'</a>') ?></p>
  326. <div class="fsetsubmit"><input type="submit" name="prune" value="<?php echo $lang_admin_common['Prune'] ?>" tabindex="8" /></div>
  327. </div>
  328. </fieldset>
  329. </div>
  330. </form>
  331. </div>
  332. </div>
  333. <div class="clearer"></div>
  334. </div>
  335. <?php
  336. require PUN_ROOT.'footer.php';