Skip to content

Instantly share code, notes, and snippets.

@ameer1234567890
Last active August 23, 2018 17:06
Show Gist options
  • Save ameer1234567890/538501 to your computer and use it in GitHub Desktop.
Save ameer1234567890/538501 to your computer and use it in GitHub Desktop.
Loggy
Change Log of Loggy, the change logger.
===================================================================================================
Version: 2.02.2207133
----------------------
- Minor bug fix.
Version: 2.01.2207132
----------------------
- Bug fix in atom feed.
- Various bug fixes.
Version: 2.00.2207131
----------------------
- Converted to use sqlite3.
Version: 1.76.2406111
----------------------
- Changed php function call htmlspecialchars() to htmlentities().
Version: 1.75.1606111
----------------------
- Changed updater URL to reflect changes in Github.
Version: 1.74.2311101
----------------------
- Changed updater URLs to https, to compensate for Github's move to ssl.
Version: 1.73.1810101
----------------------
- Changed updater to support > PHP 5.3.0.
Version: 1.73.2809101
----------------------
- Removed an additional slash in feed's alternate URL.
- Added "Back to Log" link in single entry show page.
Version: 1.73.2109102
----------------------
- Made actual use of $admin_name and $admin_email in the feed author name and email.
Version: 1.73.2109101
----------------------
- Enhanced update checker.
Version: 1.73.2009101
----------------------
- Added config variables $loggy_title, $admin_name and $admin_email.
- Fixed an incorrect link in atom feed's alternate link.
- Slight change in atom id elements. Changed "changelog" to "loggy".
Version: 1.72.1609101
----------------------
- Added Redirect from "page=1" to base page.
- Added input checking for the page variable from query string.
- Added instruction to consider commenting out the initialize on first load section, once you run the script for the first time. This is useful for better performance, as the script does not have to check if the DB file exists, upon each load.
Version: 1.71.0509101
----------------------
- Added a digit to minor version and a diit to build number.
- Changed update checker to accommodate additional digits in version number.
- Fixed SQL injection bug in show single entry section. It now validates the id provided by the get variable to check if it is a number.
Version: 1.7.050910
----------------------
- Added time zone support.
- Added format for dates displayed.
- Changed name to "Loggy" from simplePHPlog. Refer here for suggestions I got. http://www.thehosthelpers.com/name-for-a-change-log-script-t883.0.html
* Note: Any changes done prior to version 1.7.050910 are not logged here as this change log starts from version 1.7.050910
<?php
/*
Loggy
----------------------------------------------------------------------------------
Version: 2.02.2207133
Author: Ameer Dawood
Email: ameer1234567890@gmail.com
Website: http://ameer1234567890.co.cc/
----------------------------------------------------------------------------------
Loggy Copyright 2010 Ameer Dawood.
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 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/>.
*/
// README
// ----------------------------------------------------------------------------------
// Make sure that you chmod the directory where the db file is, to something higher than 770.
// This script automatically creates a 'Hello world' log entry. You can delete it using your favourite SQLite admin app.
// Be sure to change the variables in config section to your required ones, especially $loggy_title, $admin_name and $admin_email
// Please consider commenting out the initialize on first load section, once you run the script for the first time. This is useful for better performance.
// Global Variables (do not edit/delete these)
// ----------------------------------------------------------------------------------
$self = $_SERVER['SCRIPT_NAME']; // name of this file
$version = '2.02.2207133'; // version number used by update checker
$update_url = 'https://raw.github.com/gist/538501/loggy.php'; // used by update checker
$source_url = 'https://gist.github.com/538501'; // used by update checker
// Config Variables
// ----------------------------------------------------------------------------------
$dbf = 'db/loggy.sqlite3.db'; // path to THE db
$per_page = 10; // max number of entries per page (used in paging)
$show_login = false; // show/hide login link
$feed = true; // enable/disable atom feed
$feed_ent = 10; // number of entries in atom feed
$login_to_read = false; // Self explanatory
date_default_timezone_set('UTC'); // set your own time zone
$date_format = 'M j, Y g:i A T'; // date format to be used for display
$loggy_title = 'Loggy'; // change to your preferred title
$admin_name = 'John Doe'; // change to your full name
$admin_email = 'admin@domain.com'; // change to your email address
// Username, password pairs. Add more as required
$auth = array (
'admin' => 'pass',
'administrator' => 'password',
);
// Session handler
// ----------------------------------------------------------------------------------
session_start();
if (!isset($_SESSION['authed'])) {
$_SESSION['authed'] = false;
}
// Turn Off Magic Quotes
// ----------------------------------------------------------------------------------
if (get_magic_quotes_gpc ()) {
$process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
while (list ($key, $val) = each ($process)) {
foreach ($val as $k => $v) {
unset ($process[$key][$k]);
if (is_array ($v)) {
$process [$key][stripslashes ($k)] = $v;
$process [] = &$process[$key][stripslashes($k)];
} else {
$process [$key][stripslashes ($k)] = stripslashes ($v);
}
}
}
unset($process);
}
// Query Database Function
// ----------------------------------------------------------------------------------
function query_DB($dbf, $sql, $fetch = false) {
if($db = new SQLite3($dbf)) {
if ($fetch == false) {
$res = $db->exec($sql);
if (!$res) die('An error has occured! Error: '.$db->lastErrorCode().' - '.$db->lastErrorMsg());
} else {
$result = $db->query($sql);
if (!$result) die('An error has occured! Error: '.$db->lastErrorCode().' - '.$db->lastErrorMsg());
$rows = array();
$i = 0;
while($res = $result->fetchArray(SQLITE3_ASSOC)){
foreach ($res as $key => $value) {
$rows[$i][$key] = $value;
}
$i++;
}
return $rows;
}
} else {
die('An error has occured! Error: '.$db->lastErrorCode().' - '.$db->lastErrorMsg());
}
}
// Initialize on first load
// ----------------------------------------------------------------------------------
if (!file_exists($dbf)) {
$sql = 'CREATE TABLE log (id INTEGER PRIMARY KEY, entry TEXT, date TEXT)';
query_DB($dbf, $sql, false);
$entry = SQLite3::escapeString(urlencode('Hello world.'));
$date = SQLite3::escapeString(urlencode(date('Y-m-d H:i:s')));
$sql = 'INSERT INTO log (id, entry, date) VALUES(null, "'.$entry.'", "'.$date.'")';
query_DB($dbf, $sql, false);
}
// Atom Feed
// ----------------------------------------------------------------------------------
if ($_GET['action'] == 'feed') {
if (is_numeric($_GET['limit'])) {
$feed_ent = round($_GET['limit']);
}
if ($feed == false || $login_to_read == true) {
$content = <<<HTML
<p>Feeds are disabled!</p>
<p><a href="$self">Back to log</a></p>
HTML;
} else {
header('Content-Type: application/atom+xml; charset=utf-8');
$host = $_SERVER['HTTP_HOST'];
$feedmod = date(DATE_ATOM, filemtime($dbf));
$content = <<<HTML
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>$loggy_title</title>
<link href="http://$host$self" type="text/html" rel="alternate"/>
<link href="http://$host$self?action=feed" rel="self" type="application/atom+xml"/>
<updated>$feedmod</updated>
<author>
<name>$admin_name</name>
<email>$admin_email</email>
</author>
<id>tag:$host,2010:loggy</id>
HTML;
$sql = "SELECT * FROM log";
$rows = query_DB($dbf, $sql, true);
$end = count($rows);
$start = $end-$feed_ent+1;
$sql = "SELECT * FROM log WHERE id >= $start AND id <= $end ORDER BY id DESC";
$rows = query_DB($dbf, $sql, true);
foreach ($rows as $row) {
$id = urldecode($row['id']);
$entry = htmlentities(urldecode($row['entry']), ENT_QUOTES);
$date = date(DATE_ATOM, strtotime(urldecode($row['date'])));
$content .= <<<HTML
<entry>
<title type="html">$entry</title>
<link href="http://$host$self?action=show&amp;id=$id" type="text/html" rel="alternate"/>
<id>tag:$host,2010:loggy-$id</id>
<updated>$date</updated>
<summary type="html">$entry</summary>
</entry>
HTML;
}
$content .= "\n</feed>";
die($content);
}
// Update Checker
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'update') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $update_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$src = curl_exec($ch);
curl_close($ch);
$current = substr(stristr($src, '$version'), 12, 12);
if ($current === $version) {
$content = '<p>Congrats! You are using version '.$version.', which is the latest version.</p><p><a href="'.$self.'">Back to log</a></p>';
} elseif (!$current) {
$content = '<p>Update error! Please check again later.</p><p><a href="'.$self.'">Back to log</a></p>';
} else {
$content = '<p>You are using version '.$version.'. There is a new version ('.$current.') available. Please download it <a href="'.$source_url.'">here</a>.</p><p><a href="'.$self.'">Back to log</a></p>';
}
// Post Log Entry
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'post') {
if ($_SESSION['authed'] == false) {
$content = '<p>Please <a href="'.$self.'?action=login">login</a> to continue.</p>';
} elseif ($_SESSION['authed'] == true) {
if (empty($_POST['entry']) || empty($_POST['date'])) {
$content = '<p>Duh! You missed something. <a href="'.$self.'?action=create">Try again?</a></p>';
} else {
$entry = SQLite3::escapeString(urlencode($_POST['entry']));
$date = SQLite3::escapeString(urlencode($_POST['date']));
$sql = 'INSERT INTO log (id, entry, date) VALUES(null, "'.$entry.'", "'.$date.'")';
query_DB($dbf, $sql, false);
$content = <<<HTML
<p>Your log entry has been posted!</p>
<p><a href="$self">Back to log</a></p>
HTML;
}
}
// Create Form
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'create') {
if ($_SESSION['authed'] == false) {
$content = '<p>Please <a href="'.$self.'?action=login">login</a> to continue.</p>';
} elseif ($_SESSION['authed'] == true) {
$dttm = date('Y-m-d H:i:s T');
$content = <<<HTML
<div id="createform">
<form method="post" action="$self?action=post">
Entry: <textarea name="entry"></textarea><br />
Date / Time: <input type="text" name="date" value="$dttm" /><br />
<input type="submit" value="Post" />
</form>
</div>
<p><a href="$self">Back to log</a></p>
HTML;
}
// Login Form
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'login') {
if ($_SESSION['authed'] == true) {
$content = '<p>You are already logged in. <a href="'.$self.'?action=create">Create Entry</a> | <a href="'.$self.'">Back to log</a>.</p>';
} elseif ($_SESSION['authed'] == false) {
$content = <<<HTML
<div id="loginform">
<form method="post" action="$self?action=logina">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="Login" />
</form>
</div>
<p><a href="$self">Back to log</a></p>
HTML;
}
// Process Login
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'logina') {
if ($_SESSION['authed'] == true) {
$content = '<p>You are already logged in. <a href="'.$self.'?action=create">Create Entry</a> | <a href="'.$self.'">Back to log</a>.</p>';
} elseif ($_SESSION['authed'] == false) {
if (!empty($_POST['username']) && $_POST['password'] == $auth[$_POST['username']]) {
$_SESSION['authed'] = true;
$_SESSION['username'] = $_POST['username'];
$content = '<p>Login sucessful. <a href="'.$self.'?action=create">Create Entry</a> | <a href="'.$self.'">Back to log</a>.</p>';
} else {
$content = '<p>Incorrect username or password. <a href="'.$self.'?action=login">Try again</a> | <a href="'.$self.'">Back to log</a>.</p>';
}
}
// Logout
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'logout') {
if ($_SESSION['authed'] == false) {
$content = '<p>You are not logged in. <a href="'.$self.'?action=login">Login</a> | <a href="'.$self.'">Back to log</a>.</p>';
} elseif ($_SESSION['authed'] == true) {
$_SESSION['authed'] = false;
unset($_SESSION['username']);
session_destroy();
$content = '<p>Sucessfully logged out. <a href="'.$self.'?action=login">Login again</a> | <a href="'.$self.'">Back to log</a>.</p>';
}
// Show (single entry)
// ----------------------------------------------------------------------------------
} elseif ($_GET['action'] == 'show') {
$id = SQLite3::escapeString($_GET['id']);
if (empty($id) || !is_numeric($id)) {
$content = '<p>No valid id specified!</p>';
} else {
$sql = 'SELECT * FROM log WHERE id = "'.$id.'"';
$row = query_DB($dbf, $sql, true);
$content = "<ul>\n";
$content .= '<li><span class="date">'.date($date_format ,strtotime(urldecode($row[0]['date']))).'</span><span class="spacer"> - </span><span class="entry">'.urldecode($row[0]['entry']).'</span></li>';
$content .= "\n</ul>\n";
}
$content .= '<p><a href="'.$self.'">Back to log</a></p>';
// Default, without any actions. Show the log
// ----------------------------------------------------------------------------------
} else {
if ($login_to_read == true && $_SESSION['authed'] == false) {
$content = '<p>Please <a href="'.$self.'?action=login">login</a> to continue.</p>';
} elseif ($login_to_read == false || $_SESSION['authed'] == true) {
$sql = "SELECT * FROM log";
$rows = query_DB($dbf, $sql, true);
$total_entries = count($rows);
$page = $_GET['page'];
if ($_GET['page'] == 1) {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://'.$_SERVER['HTTP_HOST'].$self);
exit();
}
if (empty($page) || !is_numeric($page)) {
$page = 1;
}
$start = $total_entries-($per_page*$page)+1;
$end = $total_entries-($per_page*($page-1));
$sql = "SELECT * FROM log WHERE id >= $start AND id <= $end ORDER BY id DESC";
$rows = query_DB($dbf, $sql, true);
$content = "<ul>\n";
foreach ($rows as $row) {
$content .= '<li><span class="date">'.date($date_format, strtotime(urldecode($row['date']))).'</span><span class="spacer"> - </span><span class="entry">'.urldecode($row['entry']).'</span></li>';
$content .= "\n";
}
$content .= "</ul>\n";
$max_page = ceil($total_entries/$per_page);
$content .= '<p class="pager">';
if ($page == 1) {
$content .= '<span>&lt;&lt;</span> ';
$content .= '<span>&lt;</span> ';
} else {
$content .= '<a href="'.$self.'?page=1">&lt;&lt;</a> ';
$content .= '<a href="'.$self.'?page='.($page-1).'">&lt;</a> ';
}
for ($i = 1; $i <= $max_page; $i++) {
if ($i == $page) {
$content .= '<span class="bold">'.$i.'</span> ';
} else {
$content .= '<a href="'.$self.'?page='.$i.'">'.$i.'</a> ';
}
}
if ($page == $max_page) {
$content .= '<span>&gt;</span> ';
$content .= '<span>&gt;&gt;</span> ';
} else {
$content .= '<a href="'.$self.'?page='.($page+1).'">&gt;</a> ';
$content .= '<a href="'.$self.'?page='.$max_page.'">&gt;&gt;</a> ';
}
$content .= '</p>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $loggy_title; ?></title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<?php
if ($feed == true && $login_to_read == false) {
echo '<link rel="alternate" type="application/atom+xml" href="http://'.$_SERVER['HTTP_HOST'].$self.'?action=feed" title="'.$loggy_title.'"/>';
}
?>
<style type="text/css">
body {
font-family: Verdana;
font-size: 12px;
}
.navbar {
text-align: right;
}
.bold {
font-weight: bold;
}
.pager {
text-align: center;
}
</style>
</head>
<body>
<p class="navbar">
<?php
if ($_SESSION['authed'] == true) {
echo 'Welcome, '.$_SESSION['username'].'. <a href="'.$self.'?action=logout">Logout</a> | <a href="'.$self.'?action=create">Create Entry</a> | <a href="'.$self.'?action=update">Check for Update</a>.';
} elseif ($_SESSION['authed'] == false && $show_login == true) {
echo 'Welcome, guest. <a href="'.$self.'?action=login">Login</a>';
}
?>
</p>
<h2><?php echo $loggy_title; ?></h2>
<div id="content">
<?php echo $content; ?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment