<?php

/*
#####
PERSONAL PASTEBIN
by Eric O'Callaghan
http://ericoc.com/
eric@ericoc.com
#####
*/

/* Variables */

// MySQL information
$sqluser "paste";
$sqlpass "password";
$dbname "paste";

$tablename "pastes"// Leave this alone if you used pastes.sql

// How many pastes should be listed by default?
$show "15";

// Set the max length of the name field
// 0 for no limit
$namelimit 35;

#############

// Get the pasters IP address
$ip $_SERVER['REMOTE_ADDR'];

// Create a function to make strings safe for MySQL
function safe($value){
    return 
mysql_real_escape_string($value);
}

/* MySQL connect */

$link mysql_connect("localhost"$sqluser$sqlpass) OR die("Cannot connect to $dbname database");
mysql_select_db($dbname$link);

/* Add paste */

// Start adding a paste if a name and content we're posted via the form
if ( (isset($_POST['name'])) && (isset($_POST['content'])) && (!empty($_POST['content'])) ) {

    
// Parse the paste's name
    
$name trim($_POST['name']);
    
$namelength strlen($name);
    
$name htmlspecialchars($name);
    
$thename safe($name);

    
// Parse the paste's content
    
$content htmlspecialchars($_POST['content']);
    
$thecontent safe($content);

    
// Get timestamp
    
$timestamp time();

    
// Set name if it wasn't filled in
    
if (empty($name)) {
        
$name "None";
        
$thename "None";
    }

    
// Put the paste in the database if the name isn't too long
    
$savepaste "INSERT INTO `$tablename` VALUES ('0', '$thename', '$thecontent', '$ip', '$timestamp')";

    if ( (
$namelength $namelimit) && ($namelimit 0) ) {
        
$content "That paste name is too long!";

    } elseif (
mysql_query($savepaste$link)) {
        echo 
"";

    } else {
        
$content "Paste failed!\n";
        
$content .= mysql_error();
    }

/* Retrieve pastes */

// Start retrieving an individual paste if a numeric paste ID was given in the URL
} elseif ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {

    
// Get the requested paste ID based on the URL
    
$id $_GET['id'];
    
$getpaste mysql_query("SELECT * FROM `$tablename` WHERE `id` = '$id'"$link);

    
// Say so if the paste ID doesn't exist in the database
    
if (@mysql_num_rows($getpaste) <= 0) { 
        
$content "That paste doesn't exist!";

    
// Retrieve the paste's information if the ID exists
    
} else {
        while (
$row mysql_fetch_array($getpaste)) {
            
$name $row[name];
            
$content $row[content];
            
$timestamp $row[timestamp];
        }
    }
}

/* Header */

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex,nofollow">
<title>Personal Pastebin</title>
<link rel="stylesheet" type="text/css" href="pastebin.css">
</head>
<body>
<center><big><a href="<?php echo $_SERVER['SCRIPT_NAME']; ?>">Pastebin</a></big></center>
<br><br>
<?php

/* List pastes */

// Count all pastes in the database
$totalpastes mysql_query("SELECT id, name, timestamp FROM `$tablename` ORDER BY `id` DESC"$link);
$totalcount = @mysql_num_rows($totalpastes);

// Say so if there are no pastes
if ($totalcount 1) {
    echo 
"<b>There are currently no pastes.</b><br>\n";

} else {

    
// Check if a certain number of pastes was requested
    
if ( (isset($_GET['show'])) && (!empty($_GET['show'])) && (is_numeric($_GET['show'])) && ($_GET['show'] <= $totalcount) && ($_GET['show'] > 0) ) {

        
// Set our show variable to the URL-supplied one
        
$show $_GET['show'];

    
// Check if all pastes were requested
    
} elseif ($_GET['show'] == "all") {
        
$show $totalcount;
    }

    
// List based on the number of pastes requested; this will take the default number if no set number was requested
    
$listpastes mysql_query("SELECT id, name, timestamp FROM `$tablename` ORDER BY `id` DESC LIMIT $show"$link);

    
// Count pastes being listed
    
$listcount = @mysql_num_rows($listpastes);

    
// Display each paste with its timestamp, in a list
    
echo "<ol>\n";
    while (
$list mysql_fetch_array($listpastes)) {
        echo 
"<li><a href=\"" $_SERVER['SCRIPT_NAME'] . "?id=$list[id]&show=$show\">$list[name]</a> -- " date("Y-m-d"$list[timestamp]) . " / " date("g:i:s A"$list[timestamp]) . "</li>\n";
    }
    echo 
"</ol><br>\n";

    
// Tell how many pastes are being shown and how many there are total
    
if ($listcount != $totalcount) {
        echo 
"<i>Showing $listcount of $totalcount pastes.</i><br>\n";
    }

    
// Give a link to show all pastes if we're not already showing all of them
    
if ($totalcount $listcount) {
        echo 
"<a href=\"" $_SERVER['SCRIPT_NAME'] . "?show=all\">Show all pastes</a><br><br>\n";
    }

    
// Give link to make a new paste
    
echo "<a href=\"" $_SERVER['SCRIPT_NAME'] . "\">New paste</a><br>\n";
}

/* Paste form */

echo "<br><hr><br>\n";
echo 
"<form method=\"post\">\n";

// Set the maxlength field on the name textbox if necessary
echo "<b>Name:</b> <input type=\"text\"";
if (
$namelimit 0) {
    echo 
" maxlength=\"$namelimit\"";
}
echo 
" name=\"name\"";

// Show the name if there is one
if (isset($name)) {

    
// Prepend the name with "Re: " if it hasn't already been
    
if (eregi("^(Re:)(.+)$"$name)) {
        echo 
" value=\"$name\"";
    } else {
        echo 
" value=\"Re: $name\"";
    }
}
echo 
"><br>\n";

// Show a separate line with the timestamp if one is set
if (isset($timestamp)) {
    echo 
"<b>Timestamp:</b> " date("Y-m-d"$timestamp) . " / " date("g:i:s A"$timestamp) . "<br>\n";
}

// Show paste content if there is any
echo "<textarea name=\"content\" cols=\"85\" rows=\"15\">$content</textarea><br>\n";

?>
<input type="submit" value="Paste!">
</form>
</body>
</html>