|
From: <ma...@us...> - 2011-09-19 22:10:53
|
Revision: 421
http://openautomation.svn.sourceforge.net/openautomation/?rev=421&view=rev
Author: mayerch
Date: 2011-09-19 22:10:46 +0000 (Mon, 19 Sep 2011)
Log Message:
-----------
Updated version which can dump its content in HTML as well as delete old content.
Also UTF-8 fixes to work with picky RSS readers.
Modified Paths:
--------------
tools/rsslog/rsslog.php
Modified: tools/rsslog/rsslog.php
===================================================================
--- tools/rsslog/rsslog.php 2011-09-19 15:58:09 UTC (rev 420)
+++ tools/rsslog/rsslog.php 2011-09-19 22:10:46 UTC (rev 421)
@@ -13,72 +13,75 @@
// 2. Recieve the log as RSS:
// URL parameter "f": The (optional) filter, only log lines with a tag
// that fit this string are sent
+// 3. Dump all the content in a HTML page:
+// URL parameter "dump" - no value needed
+// 4. Remove old content:
+// URL parameter "r": the timestamp (seconds since 1970) of the oldest log
+// line to keep
+
+// create database connection
+$db = sqlite_open('rsslog.db', 0666, $error);
+if (!$db) die ($error);
+
+// create table if it doesn't exists
+create( $db );
+
if( isset($_GET['c']) )
{
// store a new log
$store = true;
$log_content = $_GET['c'] ? $_GET['c'] : '<no content>';
+ if( mb_detect_encoding($log_content, 'UTF-8', true) != 'UTF-8' )
+ $log_content = utf8_encode($log_content);
$log_tags = $_GET['t'] ? $_GET['t'] : array();
+
+ insert( $db, $log_content, $log_tags );
+} else if( isset($_GET['dump']) )
+{
+ $result = retrieve( $db, $log_filter );
+ ?>
+<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /></head><body>
+<table border="1">
+ <?php
+ while( sqlite_has_more($result) )
+ {
+ $row = sqlite_fetch_array($result, SQLITE_ASSOC );
+ echo '<tr>';
+ echo '<td>' . date( DATE_RFC822, $row['t'] ) . '</td>';
+ echo '<td>' . $row['content'] . '</td>';
+ echo "</tr>\n";
+ }
+ ?>
+</table>
+</body></html>
+ <?php
+} else if( isset($_GET['r']) )
+{
+ $timestamp = $_GET['r'] ? $_GET['r'] : '';
+ delete( $db, $timestamp );
} else {
// send logs
- $store = false;
$log_filter = $_GET['f'] ? $_GET['f'] : '';
-}
-// create database connection
-$db = sqlite_open('rsslog.db', 0666, $error);
-if (!$db) die ($error);
-
-// create table if it doesn't exists
-$q = "SELECT name FROM sqlite_master WHERE type='table' AND name='Logs';";
-$result = sqlite_query( $db, $q, SQLITE_NUM);
-if (!$result) die("Cannot execute query.");
-if( !sqlite_has_more($result) )
-{
- // no table found - create it
- $q = "CREATE TABLE Logs(" .
- " id INTEGER PRIMARY KEY," .
- " content TEXT NOT NULL," .
- " t TIMESTAMP" .
- ");";
- $ok = sqlite_exec($db, $q, $error);
-
- if (!$ok)
- die("Cannot execute query. $error");
-}
-
-if( $store )
-{
- // store a new log line
- $q = "INSERT INTO Logs(content, t) VALUES( " .
- " '" . sqlite_escape_string( $log_content ) . "'," .
- " datetime('now','localtime')" .
- ")";
-
- $ok = sqlite_exec($db, $q, $error);
-
- if (!$ok)
- die("Cannot execute query. $error");
-} else {
// retrieve data
- $q = "SELECT * FROM Logs";
- $result = sqlite_query( $db, $q, SQLITE_ASSOC);
-// phpinfo();
+ $result = retrieve( $db, $log_filter );
echo '<?xml version="1.0"?>';
?>
<rss version="2.0">
<channel>
<title>RSS supplied logs</title>
<link><?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME']; ?></link>
+ <description>RSS supplied logs</description>
<?php
// echo '<description>foo</description>';
while( sqlite_has_more($result) )
{
- $row = sqlite_fetch_array($result, SQLITE_ASSOC);
+ $row = sqlite_fetch_array($result, SQLITE_ASSOC );
echo '<item>';
echo '<title>' . $row['content'] . '</title>';
- echo '<pubDate>' . $row['t'] . '</pubDate>';
- echo '</item>';
+ echo '<description>' . $row['content'] . '</description>';
+ echo '<pubDate>' . date( DATE_RFC822, $row['t'] ) . '</pubDate>';
+ echo '</item>' . "\n";
}
?>
</channel>
@@ -87,4 +90,58 @@
}
sqlite_close($db);
+
+///////////////////////////////////////////////////////////////////////////////
+// create tables if they don't exist
+function create( $db )
+{
+ $q = "SELECT name FROM sqlite_master WHERE type='table' AND name='Logs';";
+ $result = sqlite_query( $db, $q, SQLITE_NUM );
+ if (!$result) die("Cannot execute query.");
+ if( !sqlite_has_more($result) )
+ {
+ // no table found - create it
+ $q = 'CREATE TABLE Logs(' .
+ ' id INTEGER PRIMARY KEY,' .
+ ' content TEXT NOT NULL,' .
+ ' t TIMESTAMP' .
+ ');';
+ $ok = sqlite_exec($db, $q, $error);
+
+ if (!$ok)
+ die("Cannot execute query. $error");
+ }
+}
+
+// insert a new log line
+function insert( $db, $content, $tags )
+{
+ // store a new log line
+ $q = 'INSERT INTO Logs(content, t) VALUES( ' .
+ " '" . sqlite_escape_string( $content ) . "'," .
+ " datetime('now','localtime')" .
+ ')';
+
+ $ok = sqlite_exec($db, $q, $error);
+
+ if (!$ok)
+ die("Cannot execute query. $error");
+}
+
+// return a handle to all the data
+function retrieve( $db, $filter )
+{
+ $q = "SELECT content, strftime('%s', t, 'localtime') AS t FROM Logs";
+ return sqlite_query( $db, $q, SQLITE_ASSOC );
+}
+
+// delete all log lines older than the timestamp
+function delete( $db, $timestamp )
+{
+ $q = "DELETE from Logs WHERE t < datetime($timestamp, 'unixepoch', 'localtime')";
+ $ok = sqlite_exec($db, $q, $error);
+
+ if (!$ok)
+ die("Cannot execute query. $error");
+}
?>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|