All pastes #146649 Raw Edit

Someone

public text v1 · immutable
#146649 ·published 2006-08-23 19:39 UTC
rendered paste body
<?php
/***************************************************************************
*	Very Simple News System
*	Version: 3.2.2
*	Filename: functions.php
*	Description: General functions for VSNS Lemon
****************************************************************************
*	Build Date: December 31, 2005
*	Author: Tachyon
*	Website: http://tachyondecay.net/
****************************************************************************
*	Copyright (c) 2005,2006 by Tachyon
*
*	This program 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.  A copy of the GPL version 2 is
*	included with this package in the file "COPYING.TXT"
*
*   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, write to the Free Software
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
****************************************************************************/

// Calls VSNS for external pages
function call_vsns($type)
{
	extract ($_SESSION['config'], EXTR_OVERWRITE);

    if ($type != 'id')
    {
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<div class="vsns_view_articles">
		<input type="hidden" name="type" value="archive" />

		<label>Month:
			<select name="month" id="month">
				<option value="01">January</option>
				<option value="02">February</option>
				<option value="03">March</option>
				<option value="04">April</option>
				<option value="05">May</option>
				<option value="06">June</option>
				<option value="07">July</option>
				<option value="08">August</option>
				<option value="09">September</option>
				<option value="10">October</option>
				<option value="11">November</option>
				<option value="12">December</option>
			</select>
		</label>

		<label>Year:
			<select name="year" id="year">
<?php
    	//Grab the minimum and maximum years
    	$minquery = mysql_query("SELECT MIN(year) AS minyear FROM vsns_news LIMIT 1") or die(mysql_error());
    	extract(mysql_fetch_array($minquery));
    
    	$maxquery = mysql_query("SELECT MAX(year) AS maxyear FROM vsns_news LIMIT 1") or die(mysql_error());
    	extract(mysql_fetch_array($maxquery));
    
    	if ($maxyear)
    	{
    		while($maxyear >= $minyear)
    		{
    			echo "\n\t\t\t" . '<option value="' . $maxyear . '">' . $maxyear . '</option>';
    			$maxyear--;
    		}
    	}
?>

			</select>
		</label>

		<input type="submit" value="Go" />
	</div>
</form>

<?php
    	if (0 == $disable_categories)
    	{
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<div class="vsns_view_articles">
		<input type="hidden" name="type" value="cat" />

		<label>Category
			<select name="category">
				<option value="" selected="selected">Choose a category</option>
<?php
    		//Build a list of categories
    		$hats = explode("\n", $categories);
    
    		foreach ($hats as $category)
    		{
    			$category = trim($category);
    			echo "\t\t\t\t" . '<option value="' . $category . '">' . $category . '</option>' . "\n";
    		}
?>
			</select>
		</label>

		<input type="submit" value="Go" />
	</div>
</form>

<?php
    	}
    }
    switch($type)
    {
    	case 'archive':
    	//
    	//An archive list of articles for a particular month/year
    	//
    
    		//Check if we have a year and month
    		if ($_REQUEST['year'] && $_REQUEST['month'])
    		{
    			$year = mysql_real_escape_string ($_REQUEST['year']);
    			$month = mysql_real_escape_string ($_REQUEST['month']);
    		}
    		else
    		{
    			$year = date('Y');
    			$month = date('m');
    		}
    		$query = mysql_query("SELECT * FROM vsns_news WHERE year = '" . $year . "' AND month = '" . $month . "' ORDER BY ID $disp_order");
    
    		parse_news('archive', $query, 'news_container');
    		break;
    
    	case 'cat':
    	//
    	//A single category
    	//
    
    		parse_pinned('cat', $_REQUEST['category']);
    		$query = mysql_query("SELECT * FROM vsns_news WHERE pinned = '0' AND category = '" . mysql_real_escape_string ($_REQUEST['category']) . "' ORDER BY ID $disp_order") or die(mysql_error());
    		parse_news('cat', $query, 'news_container');
    		break;
    
    	case 'id':
    	//
    	//A single article
    	//
    		$query = mysql_query("SELECT * FROM vsns_news WHERE ID = '" . mysql_real_escape_string ($_REQUEST['id']) . "' LIMIT 1") or die(mysql_error());
    		parse_news('id', $query, 'news_container');
    		break;
    
    	default:
    	case 'index':
    	//
    	//A regular index page of articles
    	//
    		parse_pinned('index');
    		$query = mysql_query("SELECT * FROM vsns_news WHERE pinned = '0' ORDER BY ID $disp_order LIMIT $limit") or die(mysql_error());
    		parse_news('index', $query, 'news_container');
    		break;
    }
}

// Clean the input data
function clean_input_data ($input)
{
    //
	// First, clean the input data
	//
	
	// If magic quotes doesn't do work
    if (!get_magic_quotes_gpc())
	{
		foreach($input as $cereal => $kelloggs)
		{
			$kelloggs = mysql_real_escape_string ($kelloggs);

			if ($cereal == 'comment')
    		{
                $kelloggs = strip_tags ($kelloggs, '<a><em><strong><code><blockquote><abbr><acronym>');
    		}

    		else
    		{
                $kelloggs = strip_tags ($kelloggs);
    		}

			$input[$cereal] = $kelloggs;
		}
	}
	
	// Otherwise, we don't need to add slashes
	else
    {
        foreach($input as $cereal => $kelloggs)
		{
			if (!is_array ($cereal))
			{
    			if ($cereal == 'comment')
    			{
                    $kelloggs = strip_tags ($kelloggs, '<a><em><strong><code><blockquote><abbr><acronym>');
    			}
    
    			else
    			{
                    $kelloggs = strip_tags ($kelloggs);
    			}
    
    			$input[$cereal] = $kelloggs;
    		}
		}
	}
	
	return $input;
}

//Prepare the post
//Kudos to Sani for help formatting
function format_post ($string, $me_replace, $multi_line = TRUE)
{
	if ($multi_line)
	{
    	//Convert newlines to <br /> tags and double <br />s to paragraphs
    	$string = nl2br_skip_html($string);
    	$string = preg_replace("/<br \/>([\s]*)<br \/>/i", "</p>\n<p>", $string);
    	$string = '<p>' . $string . '</p>';
	}

	//Replace emoticon codes with images
	$string = replace_emotes($string);

	// The array of entities that we shall be replacing
	$chars = array ();

	$chars[] = array ('&lt;', '&#60;');			// Less than (<)
	$chars[] = array ('&gt;', '&#62;');			// Greater than (>)
	$chars[] = array ('&nbsp;', '&#160;');		// Non-breaking space
	$chars[] = array ('&copy;', '&#169;');		// Copyright sign
	$chars[] = array ('(c)', '&#169;');			//
	$chars[] = array ('&laquo;', '&#171');		// Left French quotation mark
	$chars[] = array ('&raquo;', '&#187;');		// Right French quotation mark
	$chars[] = array ('&shy;', '&#173;');		// Soft hyphen
	$chars[] = array ('&reg;', '&#174;');		// Registered trademark
	$chars[] = array ('(r)', '&#174;');			//
	$chars[] = array ('&deg;', '&#176;');		// Degree sign
	$chars[] = array ('--', '&#8212;');			// Em dash
	$chars[] = array ('&mdash;', '&#8212;');	//
	$chars[] = array ('&#151;', '&#8212;');		//
	$chars[] = array ("'", '&#8217;');			// Single quotation mark
	$chars[] = array ('...', '&#8230;');		// Ellipses
	$chars[] = array ('. . .', '&#8230;');		//
	$chars[] = array ('&trade;', '&#8482;');	// Trademark sign
	$chars[] = array ('&#153;', '&#8482;');		//
	$chars[] = array ('(tm)', '&#8482;');		//

	//List of block-level HTML elements that should not go inside <p>
	//Separate with |
	$html_match = "blockquote|dl|div|form|ol|table|ul";

	$string = stripslashes ($string); //Stripslashes

	//Bye-bye ampersands
	$string = preg_replace("/&/i", "&#038;$1", $string);
	//Encode XML entities
	$string = preg_replace("/&#038;#([\d]+);/i", "&#$1;", $string);
	//Encode double quotation marks
	$string = preg_replace ("/=\"([^\"]+)\"/i", "={{dq}}$1{{dq}}", $string);
	$string = preg_replace ("/\"([^\"]+)\"/i", "&#8220;$1&#8221;", $string);
	//Make sure URIs aren't affected
	$string = preg_replace ("/=&#8220;([^&#8221;]|[^=]+)&#8221;/i", "=\"$1\"", $string);
	$string = str_replace ('{{dq}}', '"', $string);

	for ($i = 0; $i < count ($chars); $i++)
	{
		$string = str_replace ($chars[$i][0], $chars[$i][1], $string);
	}

	//Get rid of paragraphs around block-level elements
	$string = preg_replace("/<br \/>([\s]*)<(" . $html_match . ")([\s\S]*)<\/(" . $html_match . ")>/i", "</p>\n<$2$3</$4>\n<p>", $string);
	$string = preg_replace("/<br \/>([\s]*)<(" . $html_match . ")([\s\S]*)<\/(" . $html_match . ")>/i", "</p>\n<$2$3</$4>\n<p>", $string);
	$string = preg_replace("/<p>([\s]*)<(" . $html_match . ")([\s\S]*)<\/(" . $html_match . ")>([\s]*)<(\/p|br)(\s\/)?>/i", "<$2$3</$4>$5<p>", $string);

	//Make sure HTML comments aren't affected
	$string = str_replace ('<!&#8212;', '<!--', $string);
	$string = str_replace ('&#8212;>', '-->', $string);


	//Replace "/me" with the replacement string (author)
	$string = str_replace("/me ", "<span class=\"slash-me\">" . $me_replace . "</span>", $string);

	return $string;
}

//Display the comments page of the VSNS Lemon Admin CP
function comment_display()
{
	global $path;

	//Get some stats
	$total_comments = mysql_num_rows(mysql_query("SELECT ID FROM vsns_comments"));
	$num_queued = mysql_num_rows(mysql_query("SELECT ID FROM vsns_comments WHERE queue = '1'"));
?>
<div class="vsns_articlestats">
	<h2>Stats at a Glance</h2>

	<ul>
		<li>Total number of comments: <?php echo $total_comments;?>
			<ul>
				<li><?php echo $num_queued;?> queued</li>
			</ul>
		</li>
	</ul>
</div>

<div class="vsns_articleops">
	<h2>Operations</h2>

	<ul>
		<li><a href="admin.php?towel=view_comments">Browse Comments</a>
<form id="jumpit" method="get" action="<?php echo $_SERVER['PHP_SELF'];?>">
	<div class="vsns_view_articles">
		<input type="hidden" name="towel" value="view_comments" />

		<label>Month:
			<select name="month" id="month">
				<option value="01">January</option>
				<option value="02">February</option>
				<option value="03">March</option>
				<option value="04">April</option>
				<option value="05">May</option>
				<option value="06">June</option>
				<option value="07">July</option>
				<option value="08">August</option>
				<option value="09">September</option>
				<option value="10">October</option>
				<option value="11">November</option>
				<option value="12">December</option>
			</select>
		</label>

		<label>Year:
			<select name="year" id="year">
<?php
	//Grab the minimum and maximum years
	$minquery = mysql_query("SELECT MIN(year) AS minyear FROM vsns_news LIMIT 1") or die(mysql_error());
	extract(mysql_fetch_array($minquery));

	$maxquery = mysql_query("SELECT MAX(year) AS maxyear FROM vsns_news LIMIT 1") or die(mysql_error());
	extract(mysql_fetch_array($maxquery));

	if ($maxyear)
	{
		while($maxyear >= $minyear)
		{
			echo "\n\t\t\t" . '<option value="' . $maxyear . '">' . $maxyear . '</option>';
			$maxyear--;
		}
	}
?>

			</select>
		</label>

		<input type="submit" value="Go" />
	</div>
</form>
		</li>
		<li><a href="admin.php?towel=comment_queue">Manage comment queue</a></li>
		<li><a href="admin.php?towel=manage_ip">Manage banned <abbr title="Internet Protocol">IP</abbr> addresses</a></li>
	</ul>
</div>
<?php
}

//Display the index page of the VSNS Lemon Admin CP
function index_display()
{
	extract ($_SESSION['config'], EXTR_OVERWRITE);
?>
	<p>Welcome to the <abbr title="Very Simple News System">VSNS</abbr> Lemon Admin Control Panel.  From here, you can perform tasks to manage your blog as well as add new entries. <abbr title="Very Simple News System">VSNS</abbr> Lemon is designed to be simple and efficient at what it does.</p>
	
	<div class="vsns_index_box" id="stats_box">
		<img src="images/stats.png" style="width: 100px; height: 100px; float: right;" alt="Statistics" />
		
		<h2>Statistics</h2>
		
<?php
	//Get some stats
	$total_articles = mysql_num_rows(mysql_query ("SELECT ID FROM vsns_news"));
	$num_queued = mysql_num_rows(mysql_query ("SELECT ID FROM vsns_news WHERE queue = '1'"));
	$num_pinned = mysql_num_rows(mysql_query ("SELECT ID FROM vsns_news WHERE pinned = '1'"));

	//If the articles aren't pinned or queued, they must be regular
	//So save a query and do some arithmetic
	$num_unpinned = $total_articles - ($num_queued + $num_pinned);
?>
	<ul>
		<li>Total number of articles: <?php echo $total_articles;?>
			<ul>
				<li><?php echo $num_queued;?> queued</li>
				<li><?php echo $num_pinned;?> pinned</li>
				<li><?php echo $num_unpinned;?> regular</li>
			</ul>
		</li>
		<li>Total number of categories:
<?php
	if (0 == $disable_categories)
	{
		//Build a list of categories
		$hats = explode("\n", $categories);
		echo count($hats);
?>

			<ul>

<?php
		foreach ($hats as $category)
		{
			$category = trim($category);
			$hatsquery = mysql_num_rows(mysql_query("SELECT ID FROM vsns_news WHERE category = '" . $category . "'"));
			echo "\t\t\t\t<li><a href=\"" . $path . "archives/" . $category . "/\">$category</a> (" . $hatsquery . ")</li>\n";
		}
?>
			</ul>
		</li>
	</ul>
<?php
	}
?>
	</div>

<div class="vsns_index_box" id="help_box">
	<img src="images/help.png" style="width: 100px; height: 100px; float: right;" alt="Question mark" />

	<h2>Resources</h2>
	
	<p>Do you need help using <abbr title="Very Simple News System">VSNS</abbr> Lemon?  There are a variety of ways to contact me.  I recommend that you fully read the <a href="readme.html">README</a> first, since it includes a lot of helpful information.</p>
	
	<p>If you still need help, try my <a href="http://forums.tachyondecay.net/">message boards</a>.  You can look there to see if others have had the same problem, if it is caused by a bug, or just post for help and I&#8217;ll do my best to help you.</p>
	
	<p>You can also contact me by email, <strong>ben AT tachyondecay.net</strong>.  Please leave a detailled description of your problem and I will try and help you out.</p>
</div>


<?php
}

// Load configuration variables
function load_config()
{
	global $VSNS_id;

	$_SESSION['VSNS_id'] = $VSNS_id;

    //Configuration variables
    $config_data = array("version", "limit", "headline", "disp_order", "on_expiry", "prefixes", "disable_categories", "categories", "show_date", "show_author", "disable_comments", "path", "hlevel", "vsnsemail", "notification", "sitename", "desc", "cright", "website", "serverpath", "queue", "date_format", "friendly", "wordfilter_on", "replacement_word", "filter_list", "cutoff");
    
    foreach ($config_data as $conf)
    {
    	$query = mysql_query("SELECT config_value AS value FROM vsns_config WHERE config_name = '{$conf}'") or die(mysql_error());
    	$querydata = mysql_fetch_array($query);
    	$_SESSION['config']["$conf"] = stripslashes($querydata["value"]);
    	mysql_free_result($query);
    }

    //A multidimensional array of all smilies
    //Format:
    //First element is the text to be replaced
    //Second element is the filename of the image
    //
    $emotes_query = mysql_query ("SELECT * FROM vsns_emoticons") or die (mysql_error());
    
    if ($_SESSION['emotes'])
    {
    	unset ($_SESSION['emotes']);
    }

    while ($emoticon = mysql_fetch_array ($emotes_query))
    {
    	$_SESSION['emotes'][$emoticon['ID']] = array (1 => $emoticon['icon'], 0 => $emoticon['shortcut']);
    }
    mysql_free_result ($emotes_query);
}

// Convert newlines to br's unless there's HTML
// Thanks to thunberg at gmail dot com:
// http://ca3.php.net/manual/en/function.nl2br.php#56737
function nl2br_skip_html($string)
{
	// remove any carriage returns (mysql)
	$string = str_replace("\r", '', $string);
	// replace any newlines that aren't preceded by a > with a <br />
	$string = preg_replace('/(?<!>)\n/', "<br />\n", $string);
	return $string;
}

// Create the RSS feed file
function write_rss ()
{
	extract ($_SESSION['config']);

	$year = date ("Y");
	
	//Get most recent 10 articles
	$query = mysql_query("SELECT ID,heading,content,author,month,day,year,pubDate FROM vsns_news ORDER BY ID DESC LIMIT 10") or die(mysql_error());
    
	if (mysql_num_rows($query) > 0);
    {
    	while ($row = mysql_fetch_array($query))
    	{
            //Start building the proper information
    		//Title, description, link, and date

    		$heading = stripslashes ($row['heading']);
    		$heading = strip_tags ($heading);
    		$heading = htmlentities ($heading, ENT_QUOTES);

    		$description = stripslashes ($row['content']);
    		//$description = strip_tags ($description);
    		//$description = htmlspecialchars ($description, ENT_QUOTES);
    		$description = format_post ($description, $_row['author']);
    		//$description = substr ($description, 0, 300);
    
    		//Kudos to Stefan for this
    		//$description = preg_replace('/\s+\S*$/', '', $description)."...";

			// Check if friendly URIs through .htaccess are enabled
			if (1 == $friendly)
			{
	    		$link = $path."archives/".$row["year"]."/".$row["month"]."/".$row["day"]."/".$row["ID"]."/";
	    	}
	    	else
	    	{
	    		$link = $path . "?towel=archives&#038;type=id&#038;id=" . $row["ID"];
	    	}
    
    		if ($row["pubDate"] == 0)
    		{
    			$timestamp = mktime("00", "00", "00", $row["month"], $row["day"], $row["year"], -1);
    			$pubDate = date("D, d M Y H:i:s T", $timestamp);
    		}
    		else
    		{
    			$pubDate = strftime("%a, %d %b %Y %T %Z", $row["pubDate"]);
    		}
    		
    		$item .= <<<WOOT

		<item>
			<title>$heading</title>
			<link>$link</link>
			<pubDate>$pubDate</pubDate>
			<description><![CDATA[$description]]></description>
			<guid>$link</guid>
		</item>
WOOT;
		}
		mysql_free_result($query);
	}

	$file =  "<?xml version=\"1.0\" encoding=\"utf-8\"?>";

	$file .= <<<BASEBALL

<rss version="2.0">
	<channel>
		<title>$sitename</title>
		<description>$desc</description>
		<link>$website</link>
		<copyright>Copyright $year $cright.  All rights reserved.</copyright>

$item

	</channel>
</rss>
BASEBALL;

	//Write these settings to the file
	$handle = fopen($serverpath . 'rss.xml', 'wb');
	fwrite($handle, $file);
	fclose($handle);
}
?>