punbb/admin_prune.php
changeset 6 5e1f1e916419
parent 5 e3d7322305bf
child 7 98bbc533541c
equal deleted inserted replaced
5:e3d7322305bf 6:5e1f1e916419
     1 <?php
       
     2 /***********************************************************************
       
     3 
       
     4   Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
       
     5 
       
     6   This file is part of PunBB.
       
     7 
       
     8   PunBB is free software; you can redistribute it and/or modify it
       
     9   under the terms of the GNU General Public License as published
       
    10   by the Free Software Foundation; either version 2 of the License,
       
    11   or (at your option) any later version.
       
    12 
       
    13   PunBB is distributed in the hope that it will be useful, but
       
    14   WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    16   GNU General Public License for more details.
       
    17 
       
    18   You should have received a copy of the GNU General Public License
       
    19   along with this program; if not, write to the Free Software
       
    20   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
       
    21   MA  02111-1307  USA
       
    22 
       
    23 ************************************************************************/
       
    24 
       
    25 
       
    26 // Tell header.php to use the admin template
       
    27 define('PUN_ADMIN_CONSOLE', 1);
       
    28 
       
    29 //define('PUN_ROOT', './');
       
    30 //require PUN_ROOT.'include/common.php';
       
    31 
       
    32 global $pun_db, $pun_user, $pun_config, $lang_common;
       
    33 
       
    34 require PUN_ROOT.'include/common_admin.php';
       
    35 
       
    36 
       
    37 if ($pun_user['g_id'] < PUN_ADMIN)
       
    38 	message($lang_common['No permission']);
       
    39 
       
    40 
       
    41 if (isset($_GET['action']) || isset($_POST['prune']) || isset($_POST['prune_comply']))
       
    42 {
       
    43 	if (isset($_POST['prune_comply']))
       
    44 	{
       
    45 		confirm_referrer('admin_prune.php');
       
    46 
       
    47 		$prune_from = $_POST['prune_from'];
       
    48 		$prune_days = intval($_POST['prune_days']);
       
    49 		$prune_date = ($prune_days) ? time() - ($prune_days*86400) : -1;
       
    50 
       
    51 		@set_time_limit(0);
       
    52 
       
    53 		if ($prune_from == 'all')
       
    54 		{
       
    55 			$result = $pun_db->query('SELECT id FROM '.$pun_db->prefix.'forums') or error('Unable to fetch forum list', __FILE__, __LINE__, $pun_db->error());
       
    56 			$num_forums = $pun_db->num_rows($result);
       
    57 
       
    58 			for ($i = 0; $i < $num_forums; ++$i)
       
    59 			{
       
    60 				$fid = $pun_db->result($result, $i);
       
    61 
       
    62 				prune($fid, $_POST['prune_sticky'], $prune_date);
       
    63 				update_forum($fid);
       
    64 			}
       
    65 		}
       
    66 		else
       
    67 		{
       
    68 			$prune_from = intval($prune_from);
       
    69 			prune($prune_from, $_POST['prune_sticky'], $prune_date);
       
    70 			update_forum($prune_from);
       
    71 		}
       
    72 
       
    73 		// Locate any "orphaned redirect topics" and delete them
       
    74 		$result = $pun_db->query('SELECT t1.id FROM '.$pun_db->prefix.'topics AS t1 LEFT JOIN '.$pun_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__, $pun_db->error());
       
    75 		$num_orphans = $pun_db->num_rows($result);
       
    76 
       
    77 		if ($num_orphans)
       
    78 		{
       
    79 			for ($i = 0; $i < $num_orphans; ++$i)
       
    80 				$orphans[] = $pun_db->result($result, $i);
       
    81 
       
    82 			$pun_db->query('DELETE FROM '.$pun_db->prefix.'topics WHERE id IN('.implode(',', $orphans).')') or error('Unable to delete redirect topics', __FILE__, __LINE__, $pun_db->error());
       
    83 		}
       
    84 
       
    85 		pun_redirect('admin_prune.php', 'Posts pruned. Redirecting &hellip;');
       
    86 	}
       
    87 
       
    88 
       
    89 	$prune_days = $_POST['req_prune_days'];
       
    90 	if (!@preg_match('#^\d+$#', $prune_days))
       
    91 		message('Days to prune must be a positive integer.');
       
    92 
       
    93 	$prune_date = time() - ($prune_days*86400);
       
    94 	$prune_from = $_POST['prune_from'];
       
    95 
       
    96 	// Concatenate together the query for counting number or topics to prune
       
    97 	$sql = 'SELECT COUNT(id) FROM '.$pun_db->prefix.'topics WHERE last_post<'.$prune_date.' AND moved_to IS NULL';
       
    98 
       
    99 	if ($_POST['prune_sticky'] == '0')
       
   100 		$sql .= ' AND sticky=\'0\'';
       
   101 
       
   102 	if ($prune_from != 'all')
       
   103 	{
       
   104 		$prune_from = intval($prune_from);
       
   105 		$sql .= ' AND forum_id='.$prune_from;
       
   106 
       
   107 		// Fetch the forum name (just for cosmetic reasons)
       
   108 		$result = $pun_db->query('SELECT forum_name FROM '.$pun_db->prefix.'forums WHERE id='.$prune_from) or error('Unable to fetch forum name', __FILE__, __LINE__, $pun_db->error());
       
   109 		$forum = '"'.pun_htmlspecialchars($pun_db->result($result)).'"';
       
   110 	}
       
   111 	else
       
   112 		$forum = 'all forums';
       
   113 
       
   114 	$result = $pun_db->query($sql) or error('Unable to fetch topic prune count', __FILE__, __LINE__, $pun_db->error());
       
   115 	$num_topics = $pun_db->result($result);
       
   116 
       
   117 	if (!$num_topics)
       
   118 		message('There are no topics that are '.$prune_days.' days old. Please decrease the value of "Days old" and try again.');
       
   119 
       
   120 
       
   121 	$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / Admin / Prune';
       
   122 	require PUN_ROOT.'header.php';
       
   123 
       
   124 	generate_admin_menu('prune');
       
   125 
       
   126 ?>
       
   127 	<div class="blockform">
       
   128 		<h2><span>Prune</span></h2>
       
   129 		<div class="box">
       
   130 			<form method="post" action="<?php echo makeUrlNS('Special', 'Forum/Admin_prune', 'action=foo', true); ?>">
       
   131 				<div class="inform">
       
   132 					<input type="hidden" name="prune_days" value="<?php echo $prune_days ?>" />
       
   133 					<input type="hidden" name="prune_sticky" value="<?php echo $_POST['prune_sticky'] ?>" />
       
   134 					<input type="hidden" name="prune_from" value="<?php echo $prune_from ?>" />
       
   135 					<fieldset>
       
   136 						<legend>Confirm prune posts</legend>
       
   137 						<div class="infldset">
       
   138 							<p>Are you sure that you want to prune all topics older than <?php echo $prune_days ?> days from <?php echo $forum ?>? (<?php echo $num_topics ?> topics)</p>
       
   139 							<p>WARNING! Pruning posts deletes them permanently.</p>
       
   140 						</div>
       
   141 					</fieldset>
       
   142 				</div>
       
   143 				<p><input type="submit" name="prune_comply" value="Prune" /><a href="javascript:history.go(-1)">Go back</a></p>
       
   144 			</form>
       
   145 		</div>
       
   146 	</div>
       
   147 	<div class="clearer"></div>
       
   148 </div>
       
   149 <?php
       
   150 
       
   151 	require PUN_ROOT.'footer.php';
       
   152 }
       
   153 
       
   154 
       
   155 else
       
   156 {
       
   157 	$page_title = pun_htmlspecialchars($pun_config['o_board_title']).' / Admin / Prune';
       
   158 	$required_fields = array('req_prune_days' => 'Days old');
       
   159 	$focus_element = array('prune', 'req_prune_days');
       
   160 	require PUN_ROOT.'header.php';
       
   161 
       
   162 	generate_admin_menu('prune');
       
   163 
       
   164 ?>
       
   165 	<div class="blockform">
       
   166 		<h2><span>Prune</span></h2>
       
   167 		<div class="box">
       
   168 			<form id="prune" method="post" action="<?php echo makeUrlNS('Special', 'Forum/Admin_prune', 'action=foo', true); ?>" onsubmit="return process_form(this)">
       
   169 				<div class="inform">
       
   170 				<input type="hidden" name="form_sent" value="1" />
       
   171 					<fieldset>
       
   172 						<legend>Prune old posts</legend>
       
   173 						<div class="infldset">
       
   174 							<table class="aligntop" cellspacing="0">
       
   175 								<tr>
       
   176 									<th scope="row">Days old</th>
       
   177 									<td>
       
   178 										<input type="text" name="req_prune_days" size="3" maxlength="3" tabindex="1" />
       
   179 										<span>The number of days "old" a topic must be to be pruned. E.g. if you were to enter 30, every topic that didn't contain a post dated less than 30 days old would be deleted.</span>
       
   180 									</td>
       
   181 								</tr>
       
   182 								<tr>
       
   183 									<th scope="row">Prune sticky topics</th>
       
   184 									<td>
       
   185 										<input type="radio" name="prune_sticky" value="1" tabindex="2" checked="checked" />&nbsp;<strong>Yes</strong>&nbsp;&nbsp;&nbsp;<input type="radio" name="prune_sticky" value="0" />&nbsp;<strong>No</strong>
       
   186 										<span>When enabled sticky topics will also be pruned.</span>
       
   187 									</td>
       
   188 								</tr>
       
   189 								<tr>
       
   190 									<th scope="row">Prune from forum</th>
       
   191 									<td>
       
   192 										<select name="prune_from" tabindex="3">
       
   193 											<option value="all">All forums</option>
       
   194 <?php
       
   195 
       
   196 	$result = $pun_db->query('SELECT c.id AS cid, c.cat_name, f.id AS fid, f.forum_name FROM '.$pun_db->prefix.'categories AS c INNER JOIN '.$pun_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__, $pun_db->error());
       
   197 
       
   198 	$cur_category = 0;
       
   199 	while ($forum = $pun_db->fetch_assoc($result))
       
   200 	{
       
   201 		if ($forum['cid'] != $cur_category)	// Are we still in the same category?
       
   202 		{
       
   203 			if ($cur_category)
       
   204 				echo "\t\t\t\t\t\t\t\t\t\t\t".'</optgroup>'."\n";
       
   205 
       
   206 			echo "\t\t\t\t\t\t\t\t\t\t\t".'<optgroup label="'.pun_htmlspecialchars($forum['cat_name']).'">'."\n";
       
   207 			$cur_category = $forum['cid'];
       
   208 		}
       
   209 
       
   210 		echo "\t\t\t\t\t\t\t\t\t\t\t\t".'<option value="'.$forum['fid'].'">'.pun_htmlspecialchars($forum['forum_name']).'</option>'."\n";
       
   211 	}
       
   212 
       
   213 ?>
       
   214 											</optgroup>
       
   215 										</select>
       
   216 										<span>The forum from which you want to prune posts.</span>
       
   217 									</td>
       
   218 								</tr>
       
   219 							</table>
       
   220 							<p class="topspace">Use this feature with caution. Pruned posts can <strong>never</strong> be recovered. For best performance you should put the forum in maintenance mode during pruning.</p>
       
   221 							<div class="fsetsubmit"><input type="submit" name="prune" value="Prune" tabindex="5" /></div>
       
   222 						</div>
       
   223 					</fieldset>
       
   224 				</div>
       
   225 			</form>
       
   226 		</div>
       
   227 	</div>
       
   228 	<div class="clearer"></div>
       
   229 </div>
       
   230 <?php
       
   231 
       
   232 	require PUN_ROOT.'footer.php';
       
   233 }