Location: PHPKode > projects > Blite > blite-0.1-beta6/blite/categories.php
<?php
/*
 *      Copyright 2012 Douglas Robbins <hide@address.com>
 *      
 *      This file is part of Blite, a blogging application, available at
 *      <http://blite.ca/>.
 * 
 *      Blite 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 3 of the License, or
 *      (at your option) any later version.
 *      
 *      This program 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, see <http://www.gnu.org/licenses/>.
 */

require('configure.php');

if (!$authuser) {
	echo $lang['permdenied'];
	exit;
}

// Create an array of categories used by unpublished posts (drafts, retracted).
// Do not allow these categories to be removed if 'empty' (i.e., do not contain published posts).

$cats_unpub = array();

$query_params = '';
$results = db_query("SELECT categories FROM posts WHERE status > '1'");

while ($row = db_getdata($results)) {
	$cats = explode(' ', $row['categories']);
	foreach ($cats as $cat) {
		if (!in_array($cat, $cats_unpub)) {
			$cats_unpub[] = $cat;
		}
	}
}

if (!empty($_POST['save'])) {

	// Check the nonce.
	list($nonce, $noncestamp, $nonceerror) = verify_nonce('cat_admin','The admin page expired.');
	if ( !empty($nonceerror) ) {
		echo $nonceerror;
		exit;
	}
	
	foreach ($_POST as $key => $val) {
		if (stristr($key,'-') && $val) {
			list($action,$catid) = explode('-', $key);
			if ($action == 'rename') {
				if (get_magic_quotes_gpc()) {
					$val = stripslashes($val);
				}
				$query_params = array( 'val' => 'txt', 'catid' => 'int' );
				db_query("UPDATE categories SET catname=? WHERE id=?");
			}
			elseif ($action == 'remove' && !in_array($catid, $cats_unpub) ) {
				$query_params = array( 'catid' => 'int' );
				db_query("DELETE FROM categories WHERE id=?");
			}
		}
	}
	$query_params = array( 'nonce' => 'txt', 'noncestamp' => 'int' );
	db_query("INSERT INTO nonces (nonce, type, stamp) VALUES (?, 'cat_admin', ?)");
	header("Location: " . $_SESSION['lastpage']);
	exit;
}

// Build a nonce.

$noncestamp = time();
$nonce = sha1( $noncestamp . $_SERVER['REMOTE_ADDR'] . $cfg['noncesalt'] );

// Categories list display.

$query_params = '';
$results = db_query("SELECT id, catname, postcount FROM categories");

$cnt = 0;
$cnt_unpub = 0;
$page['list'] = "<tr><th>" . $lang['category'] . "</th><th>" . $lang['rename'] . "</th><th>" . $lang['posts'] . "</th><th class='last'>&nbsp;</th></tr>\n";

while ($row = db_getdata($results)) {
	$catid = $row['id'];
	$catname = $row['catname'];
	$postcount = $row['postcount'];
	$delete = '';
	$flag = '';
	$flagtext = '';
	if (!$postcount && in_array($catid, $cats_unpub)) {
		$flag = ' *';
		$cnt_unpub++;
	}
	$delete = "<span class='dis'><input type='checkbox' name='' value='' disabled> " . $lang['remove'] . "</span>";
	if (!$postcount && !in_array($catid, $cats_unpub)) {
		$delete = "<label><input type='checkbox' name='remove-${catid}' value='1'> " . $lang['remove'] . "</label>";
	}
	$page['list'] .= "<tr><td class='ptitle'>$catname</td><td><input type='text' name='rename-${catid}' value='' style='width:200px'></td><td>${postcount}${flag}</td><td class='last'>${delete}&nbsp;</td></tr>\n";
	$cnt++;
}

if ($cnt == 0) {
	$page['list'] = "<div style='text-align:center;font-weight:bold'>There are no " . $lang['categories'] . "</div>";
}
else {
	$page['list'] = "<form method='post' action='categories.php'>\n<table cellspacing=0 cellpadding=0 border=0 class='list'>\n" . $page['list'] . "</table>\n";
}
$page['list'] .= "<div class='footnote'>\n<div>" . $lang['emptyremove'] . "</div>\n";
if ($cnt_unpub > 0) {
	$page['list'] .= "<div>* " . $lang['hasunpub'] . " (<a href='list.php?s=2' class='admin'>" . $lang['admindrafts'] . "</a> " . $lang['or'] . " <a href='list.php?s=3' class='admin'>" . $lang['adminretracted'] . "</a>).</div>\n";
}
$page['list'] .= "</div>\n";
$page['list'] .= "<div class='buttons'><input type='submit' name='save' value='" . $lang['save'] . "'></div>\n";
$page['list'] .= "<input type='hidden' name='nonce' value='${nonce}'>\n";
$page['list'] .= "<input type='hidden' name='stamp' value='${noncestamp}'>\n";
$page['list'] .= "</form>";

$page['pagetitle'] = $lang['categories'];
$page['cssfile'] = 'themes/' . $cfg['theme'] . '/admin.css';
$page['infobar'] = "<div class='infobar'>" . $page['pagetitle'] . "</div>";
$page['contentclass'] = 'categories';
$page['navbottom'] = "<ul class='adminnav'><li><a href='./'>Home</a></li>\n$adminnav</ul>\n";

$subtemplate = file_get_contents('themes/' . $cfg['theme'] . '/templates/sub_list.tpl');
$template = file_get_contents('themes/' . $cfg['theme'] . '/templates/main.tpl');
$template = str_replace("#maincontent#", $subtemplate, $template);
foreach ($page as $key => $val) {
	$template = str_replace("#${key}#",$val,$template);
}
echo $template;
exit;
?>
Return current item: Blite