Tag Archives: php

Regular Expression for analysing ProFTPd’s xferlog

0
Filed under php
Tagged as , , ,
$xferlog_regex  = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun) ";                     //***** 1  Day
$xferlog_regex .= "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) "; //***** 2  Month
$xferlog_regex .= "(( |[0-3])[0-9]) ";                                  //***** 3  Date
$xferlog_regex .= "([0-9][0-9]:[0-5][0-9]:[0-5][0-9]) ";                //***** 5  Time
$xferlog_regex .= "(20[0-9][0-9]) ";                                    //***** 6  Year
$xferlog_regex .= "([^ ]*) ";                                           //***** 7  Transfer time
$xferlog_regex .= "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) ";  //***** 8  IP Address
$xferlog_regex .= "([^ ]*) ";                                           //***** 9  File size
$xferlog_regex .= "([^ ]*) ";                                           //***** 10 File name
$xferlog_regex .= "(a|b) ";                                             //***** 11 ASCII/Binary
$xferlog_regex .= "(C|U|T|_) ";                                         //***** 12 Compressed/Uncompressed/Tar'ed/No action
$xferlog_regex .= "(o|i|d) ";                                           //***** 13 Outgoing/Incoming/Deleted
$xferlog_regex .= "(a|g|r) ";                                           //***** 14 Authenticated/Guest/Real (local) user
$xferlog_regex .= "([^ ]*) ";                                           //***** 15 Username
$xferlog_regex .= "([^ ]*) ";                                           //***** 16 Service name
$xferlog_regex .= "(0|1) ";                                             //***** 17 Authentication: none/RFC931
$xferlog_regex .= "([^ ]*) ";                                           //***** 18 Authenticated User ID
$xferlog_regex .= "(c|i)";                                              //***** 19 Completed/Incomplete

or all in one go if you just want the regex:

/(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (( |[0-3])[0-9]) ([0-9][0-9]:[0-5][0-9]:[0-5][0-9]) (20[0-9][0-9]) ([^ ]*) ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) ([^ ]*) ([^ ]*) (a|b) (C|U|T|_) (o|i|d) (a|g|r) ([^ ]*) ([^ ]*) (0|1) ([^ ]*) (c|i)/

PHP RSS Feed

0
Filed under php
Tagged as ,

I got sick of looking at shit pieces of code that either didn’t work properly, or produced XML so badly formatted you couldn’t read the raw source to debug it, so I decided to write my own, based on the BBC’s RSS source.

<?php
/*
 * @abstract   PHP script to correctly generate an RSS feed
 * @author     Jon Thompson
 * @version    0.1
 * @since      11 Dec 2009
 *
 * Requires an SQL table with the following fields to read:
 * - title
 * - link
 * - description
 * - thumbnail
 *
 * Will generate an on-the fly RSS feed from database-driven content.
 * For best results, amend the following and add to your pages in the <head></head> section:
 *
 * <link rel="alternate" type="application/rss+xml" title="RSS" href="http://www.yoursite.com/link/to/this/script/rss.php" />
 *
 */

//***** Database login details *****
$db_server       = "localhost";
$db_database     = "database";
$db_username     = "username";
$db_password     = "password";

//***** RSS feed details *****
$rss_title       = "Your title here";
$rss_link        = "URL to web page that displays these items";
$rss_description = "An overall description of your feed";
$rss_language    = "en-gb";
$rss_copyright   = "Your copyright notice";
$rss_docs        = "URL to a page explaining distribution and use of these articles";

//***** SQL query to generate list *****
$sql = "SELECT * FROM table limit 15";

//############################################################################################################
//# NOTHING FURTHER TO EDIT FROM THIS POINT ON                                                               #
//############################################################################################################

//***** Connect to database *****
$db = mysql_connect($db_server, $db_username, $db_password) or die("Unable to connect to MySQL server\n\n");
mysql_select_db($db_database, $db) or die("Unable to select database\n\n");

//***** Execute the SQL query *****
$result = mysql_query($sql, $db);

//***** Loop through the results to get the rest of the feed *****
if(mysql_num_rows($result)){
	while($row=mysql_fetch_assoc($result)){
		$output .= "		<item>
			<title>".htmlentities($row["title"])."</title>
			<link>".htmlentities($row["link"])."</link>
			<description>".htmlentities($row["description"])."</description>
			<media:thumbnail url=\"".htmlentities($row["thumbnail"])."\" />
		</item>
";
	}
}else{
	$output .= "		<item>
			<title>No entries</title>
			<link></link>
			<description>Sorry, there are no new entries to view</description>
			<media:thumbnail url=\"\" />
		</item>
";
}

//***** Build the entire feed from our results                                  *****
//***** This will also format it correctly so it's readble in plain text format *****
$output = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
<?xml-stylesheet title=\"XSL_formatting\" type=\"text/xsl\" href=\"/shared/bsp/xsl/rss/nolsol.xsl\"?>
<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss\">
	<channel>
		<title>$rss_title</title>
		<link>$rss_link</link>
		<description>$rss_description</description>
		<language>$rss_language</language>
		<lastBuildDate>".date("D, d M Y H:i:s T")."</lastBuildDate>
		<copyright>$rss_copyright</copyright>
		<docs>$rss_docs</docs>
		<ttl>15</ttl>
".$output."	</channel>
</rss>\n";

//***** Output the feed correctly *****
header("Content-Type: text/xml");
echo $output;
exit(0);
?>