Topic: AP_Post_Author_Update
My first attempt at a PunBB plugin? it modifies the author (poster and poster_id) for a given post (id).
<?php
/***********************************************************************
Copyright (C) 2006 guardian34 (publicbox@fmguy.com)
This file is part of PunBB.
PunBB is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
PunBB is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
************************************************************************/
// Make sure no one attempts to run this script "directly"
if (!defined('PUN'))
exit;
// Tell admin_loader.php that this is indeed a plugin and that it is loaded
define('PUN_PLUGIN_LOADED', 1);
if (isset($_POST['update']))
{
// Make sure post id was entered
if (trim($_POST['id_post']) == '')
message('You didn\'t enter a post id!');
// Make sure user id was entered
if (trim($_POST['id_user']) == '')
message('You didn\'t enter an user id!');
// Check for valid post id
$result = $db->query('SELECT id FROM '.$db->prefix.'posts WHERE id = '.$_POST['id_post']);
$result = $db->fetch_assoc($result);
if (!$result['id'])
message('Post id '.$_POST['id_user'].' wasn\'t found.');
// Check for valid user id and get new username
$result = $db->query('SELECT username FROM '.$db->prefix.'users WHERE id = '.$_POST['id_user']);
$result = $db->fetch_assoc($result);
if (!$result['username'])
message('User id '.$_POST['id_user'].' wasn\'t found.');
// Update post
$db->query('UPDATE '.$db->prefix.'posts SET '.
'poster=\''.$result['username'].'\', '.
'poster_id='.$_POST['id_user'].' WHERE id = '.$_POST['id_post']);
// Display the admin navigation menu
generate_admin_menu($plugin);
?>
<div class="block">
<h2><span>Post Author Update - v1.0</span></h2>
<div class="box">
<div class="inbox">
<p>This plugin modifies the author (poster and poster_id) for a given post (id).</p>
</div>
</div>
</div>
<div class="block">
<h2 class="block2"><span>Result</span></h2>
<div class="box">
<div class="inbox">
<p>The update was successful.
(Post: <a href="viewtopic.php?pid=<?php echo $_POST['id_post'] ?>"><?php echo $_POST['id_post']; ?></a>;
User: <a href="profile.php?id=<?php echo $_POST['id_user']; ?>"><?php echo $_POST['id_user']; ?></a>)</p>
</div>
</div>
</div>
<?php
}
else
{
// Display the admin navigation menu
generate_admin_menu($plugin);
?>
<div class="block">
<h2><span>Post Author Update - v1.0</span></h2>
<div class="box">
<div class="inbox">
<p>This plugin modifies the author (poster and poster_id) for a given post (id).</p>
</div>
</div>
</div>
<div class="blockform">
<h2 class="block2"><span>Input</span></h2>
<div class="box">
<form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<div class="inform">
<fieldset>
<legend>IDs</legend>
<div class="infldset">
<table class="aligntop" cellspacing="0">
<tr>
<th scope="row">Post ID:</th>
<td><input type="text" name="id_post" size="16" /></td>
</tr>
<tr>
<th scope="row">User ID:</th>
<td><input type="text" name="id_user" size="16" /></td>
</tr>
</table>
</div>
</fieldset>
</div>
<p class="submitend"><input type="submit" name="update" value="Update" /></p>
</form>
</div>
</div>
<?php
}
// Note that the script just ends here. The footer will be included by admin_loader.php.
?>
Being fairly new to both PHP and PunBB, I have a couple of concerns. First, should the input be run through any filtering? Second, can I use the UPDATE query result to determine if the post id is valid?