Skip to content

Instantly share code, notes, and snippets.

@sachleen
Last active September 29, 2015 13:47
Show Gist options
  • Save sachleen/1609919 to your computer and use it in GitHub Desktop.
Save sachleen/1609919 to your computer and use it in GitHub Desktop.
Notepad
<?php
header('Content-Type: text/html; charset=ISO-8859-15');
date_default_timezone_set('America/Los_Angeles');
if(isset($_POST['action']) && isset($_POST['key']) && $_POST['key'] == "yoursecret")
{
$file = "notes.txt";
switch($_POST['action']) {
case "get":
if(!file_exists($file))
{
$fh = fopen($file, 'w');
fwrite($fh, " ");
fclose($fh);
}
$fh = fopen($file, 'r');
if(filesize($file) > 0)
$notes = utf8_decode(fread($fh, filesize($file)));
else
$notes = "No notes";
fclose($fh);
echo $notes;
break;
case "save":
if(isset($_POST['content']))
{
$fh = fopen($file, 'w');
fwrite($fh, $_POST['content']);
fclose($fh);
echo '» '. Date('g:i:s A');
}
break;
}
}
else
echo "not allowed";
?>
<!DOCTYPE html>
<html>
<head>
<title>Notepad</title>
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>
<style type="text/css">
a {
text-decoration: none;
}
body {
padding: 0 6px 0 0;
margin: 0 auto;
margin-top: 6px;
max-width: 600px;
max-height: 700px;
font-family: arial;
}
textarea {
width: 100%;
height: 100%;
border: 0;
font-size:20px;
margin-bottom: 5px;
background: transparent;
}
#links {
float: right;
}
</style>
<script>
var key = "";
<?php
if(isset($_GET['key']))
echo "key='" . $_GET['key'] . "';";
?>
var prevContent = "";
function getNotes() {
$.ajax({
type: "POST",
url: "ajax.php",
data: "action=get&key=" + key,
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
cache: false,
success: function(message) {
$("#notes").empty().append(message);
prevContent = message;
}
});
}
function autosave(force) {
var t = setTimeout("autosave()", 5000);
var content = $("#notes").val();
if (content != prevContent || force) {
$.ajax({
type: "POST",
url: "ajax.php",
data: "action=save&key="+key+"&content=" + encodeURIComponent(content),
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
cache: false,
success: function(message) {
$("#timestamp").empty().append(message);
}
});
}
prevContent = content;
}
$(document).ready(function() {
$("#notes").height($(document).height()-50)
getNotes();
setTimeout("autosave()", 5000);
$("#reload").click(function() {
getNotes();
});
$("#save").click(function() {
autosave(true);
});
});
</script>
</head>
<body>
<textarea id="notes"></textarea>
<div id="links"><a href="#" id="reload">Reload</a> &bull; <a href="#" id="save">Save</a></div><div id="timestamp"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment