Location: PHPKode > projects > Cadence Guestbook Host > cadence/sources/admin/posts.php
<?php

/************************************
 * Cadence
 * Remotely Hosted Guestbook Script.
 * (c) 2006, Dennis Pedrie
 * www.CadenceBook.com
 * posts.php
 ***********************************
 * Cadence Guestbook is licensed under
 * a Creative Commons License.
 * More information is available by visiting
 * http://creativecommons.org/licenses/by/3.0/
 * or the LICENSE file in the Cadence Root Folder
 ***********************************/

if(!defined('IN_CADENCE')) {
	trigger_error("You may not access this file directly",E_USER_ERROR);
}

echo "<h3>Manage Posts</h3>
<p>Use this interface to manage comments. You can edit or delete comments here</p>";
if($sysmsg == true) {
	$tpl->display("system/admin/sysmsg.tpl");
}

$do = $_GET['do'];
	
// Delete Post
if($do == "delete") {
	$delete = intval($_GET['id']);
	$auth = $post->clean_var($_GET['auth']);
	$db->query($q->delete($delete,$auth));
	if($db->rows_affected > 0) {
		$db->query($q->updatepostcount("down"));
		$tpl->assign("sysmsg","Post ". $delete ." Deleted");
		$tpl->assign("linkback","index.php?book=$book&amp;act=admin&amp;acpact=posts");
		$tpl->display("system/admin/sysmsg.tpl");
	}
}
	
// Edit Post
if($do == "edit") {
	
	$id = intval($_GET['id']);
	if($id == 0) {
		$gbook->kill("<strong>Invalid Access</strong>");
	}
	
	if($_POST['submit']) {
		$pid = intval($_POST['id']);
		$content = $post->clean_var($_POST['content']);
		$author = $post->clean_var($_POST['author']);
		
		$doedit = $db->query($q->editpost($content,$author,$id));
		$tpl->assign("sysmsg","Post ". $id ." Edited");
		$tpl->assign("linkback","index.php?book=$book&amp;act=admin&amp;acpact=posts");
		$tpl->display("system/admin/sysmsg.tpl");
	}
	else {
		$posts = $db->get_results($q->getposttoedit($id));
		foreach($posts as $posts) {
			$tpl->assign("post_id",$posts->post_id);
			$tpl->assign("post_author",$posts->post_author);
			$tpl->assign("post_content",$posts->post_content);
			$tpl->assign("post_date",date("M d, Y g:i A",$posts->post_date));
			$tpl->display("system/admin/edit.tpl");
		}
	}
}
	
// Delete Multiple Posts
if($do == "deletemultiple") {
	$delete = $_POST['dodelete'];
	$total = 0;
	foreach($delete as $delete) {
		$db->query("DELETE FROM ". TABLE_PREFIX ."posts WHERE post_gbook = '$book' AND post_id = '$delete'");
		$total = $total + 1;		
	}
	//echo "UPDATE ". TABLE_PREFIX ."gbooks SET gbook_posts = gbook_posts-$total WHERE gbook_id = '$book'";
	($total > 0) ? $db->query("UPDATE ". TABLE_PREFIX ."gbooks SET gbook_posts = gbook_posts-$total WHERE gbook_id = '$book'") : "";
	$tpl->assign("sysmsg","Posts Deleted");
	$tpl->assign("linkback","index.php?book=$book&amp;act=admin&amp;acpact=posts");
	$tpl->display("system/admin/sysmsg.tpl");
}
	
// Show All Posts
if(!isset($do)) {
	// Pagination Settings
	$page = ($_GET['page']) ? intval($_GET['page']) : 1;
	$start = $page * 10 - 10;
	$orderby = $_GET['orderby'];
	$order = $_GET['order'];
	$results = intval($_GET['results']);

// Clean out our results for Query Use.
$orderby_avail = array('id','author','date','ip');
if(!in_array($orderby,$orderby_avail)) {
$orderby = "id";
}

$order = ($order == "DESC" || $order == "ASC") ? $order : "DESC";

$results = ($results > 0) ? $results : 10;
// How many posts are there?
$db->get_var("SELECT post_id FROM ". TABLE_PREFIX ."posts where post_gbook = '$book'");
$num = $db->num_rows;

// Setup Pagination
$pagination = $gbook->pagination($num, "10", $page, $CONFIG['url'] . "index.php?". $_SERVER['QUERY_STRING']);

// Query For Posts
$posts = $db->get_results("SELECT post_id,post_author,post_email,post_ip,post_date,post_hash,post_admin_approved,post_email_approved FROM ". TABLE_PREFIX ."posts WHERE post_gbook = '$book' ORDER BY post_". $orderby ." ". $order ." LIMIT $start,". $results);

// Display Posts
if($db->num_rows > 0) {
	echo "<br /><form name='del' action='index.php?book=$book&amp;act=admin&amp;acpact=posts&amp;do=deletemultiple' method='post'>
		<div style='background-color:#eee;'>
		<table style='border:0px' width='100%'>";
		
		// Calculate the Page the post is on.
		$index = range(1,ceil($num / 15));
		
		foreach($posts as $posts) {
			echo "		  <tr>
			<td style='border:0px; background-color:#fff;' width='20%'>
				<a href='index.php?book=$book#post". $posts->post_id ."'>". $posts->post_author ."</a>
			</td>
			<td style='border:0px; background-color:#fff; text-align:center;' width='15%'>
				<a href='index.php?book=$book&amp;act=admin&amp;acpact=posts&amp;do=edit&amp;id=". $posts->post_id ."'>Edit</a>
			</td>
			<td style='border:0px; background-color:#fff; text-align:center;' width='15%'>
				<a href='javascript:delete_post(\"index.php?book=$book&amp;act=admin&amp;acpact=posts&amp;do=delete&amp;id=". $posts->post_id ."&amp;auth=". $posts->post_hash ."\")'>Delete</a>
			</td>
			<td style='border:0px; background-color:#fff; text-align:center;' width='20%'>
				". date("M d, Y", $posts->post_date) ."
			</td>
			<td style='border:0px; background-color:#fff; text-align:center;' width='25%'>
				<a href=\"index.php?book=$book&amp;act=admin&amp;acpact=users&amp;do=iplookup&amp;ip=". $posts->post_ip ."\">". $posts->post_ip ."</a>
			</td>
			<td style='border:0px; background-color:#fff; text-align:center;' width='5%'>
				<input type='checkbox' name='dodelete[$posts->post_id]' value='$posts->post_id'/>
			</td>
			</tr>";
			$number ++;
		}
		echo "<tr><td colspan=\"6\">
		<div style=\"float:left;\">". $pagination ."</div>
		<div style='float:right;font-size:9px;'><a href='javascript:delete_posts();'>Delete Selected</a> 
		(<a href=\"javascript:show_add_ban_form('more','less')\">More Options</a>)</div><br /></td></tr></table></div></form>
		<form id=\"more\" action=\"index.php?book=$book&amp;act=admin&amp;acpact=posts\" method=\"get\">
			Display Options:<br />
			<input type='hidden' name='book' value='$book' />
			<input type='hidden' name='act' value='admin' />
			<input type='hidden' name='acpact' value='posts' />
			<input type='hidden' name='page' value='$page' />
			<select name='orderby'>
				<optgroup label='Order By:'>
					<option value='id' selected>Post ID</option>
					<option value='author'>Post Author</option>
					<option value='date'>Post Date</option>
					<option value='ip'>Post ID</option>
				</optgroup>
			</select>
			<select name='order'>
				<optgroup label='Order:'>
					<option value='DESC' selected>Descending</option>
					<option value='ASC'>Ascending</option>
				</optgroup>
			</select>
			<select name='results'>
				<optgroup label='Per Page:'>
					<option value='10' selected>10</option>
					<option value='20'>20</option>
					<option value='30'>30</option>
					<option value='40'>40</option>
					<option value='50'>50</option>
					<option value='60'>60</option>
					<option value='70'>70</option>
					<option value='80'>80</option>
					<option value='90'>90</option>
					<option value='100'>100</option>
				</optgroup>
			</select>
			<input type=\"button\" onclick=\"return submit()\" value=\"Submit\" />
		</form><br />
		<div id=\"less\"></div>";
	}
	else {
		$gbook->kill("Gasp! Nothing Here. Add a post, so your ACP isn't lonely.");
	}
}

?>
Return current item: Cadence Guestbook Host