Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Created March 20, 2017 19:34
Show Gist options
  • Save DanielFGray/aece17e775c7bbeab144b22c88faca0c to your computer and use it in GitHub Desktop.
Save DanielFGray/aece17e775c7bbeab144b22c88faca0c to your computer and use it in GitHub Desktop.
scratchpad thing
<!DOCTYPE html>
<html lang="en">
<head>
<title>scratchpad</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" media="all" />
<link type="text/css" rel="stylesheet" href="style.css" media="all" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script src="marked.min.js"></script>
<script type="text/javascript">
function autoGrow (oField) {
if (oField.scrollHeight != oField.clientHeight) {
oField.style.height = oField.scrollHeight + 'px';
}
}
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: false
});
$(function() {
document.getElementById('scratchpad').innerHTML =
marked(document.getElementById('scratchedit').value);
$('input#edit').click(function() {
if ($('input#edit').attr('value') == 'Edit') {
$('input#edit').attr('value', 'Cancel');
$('textarea#scratchedit').css('display', 'block').focus();
$('input#submit').css('display', 'inline');
$('div#scratchpad').css('display', 'none');
autoGrow(document.getElementById('scratchedit'))
} else {
$('input#edit').attr('value', 'Edit');
$('textarea#scratchedit, input#submit').css('display', 'none');
$('div#scratchpad').css('display', 'block');
}
});
});
</script>
</head>
<body>
<div id="wrapper">
<?php
$file = dirname(__FILE__) . '/secure.php';
$mtime = filemtime($file);
$flatfile = '';
$message = '';
if(isset($_POST['submit']) && $_POST['submit'] == 'Save') {
$text = trim(htmlspecialchars($_POST['scratchedit']));
if($_POST['mtime'] != $mtime) {
$message .= 'Changes were made by someone else while you were editing.';
# $diff = diff(file_get_contents($file), $_POST['scratchedit'], 1);
if (is_string($diff)) {
$message .= '<br/>Here\'s the difference between your version and the newest version:';
$message .= "<div style=\"color:#FFF\; border: 1px solid #000;\">$diff</div>";
} else {
$message .= '<br/>Unfortunately there was an error.';
}
} elseif(! empty($text) && file_put_contents($file, $text)) {
$message .= 'Saved!';
$mtime = filemtime($file);
} else {
$message .= 'There was an error saving your changes!';
}
}
if(! empty($message)) { echo "<div id=\"message\">$message</div><hr/>"; }
$flatfile = file_get_contents($file);
echo '<div id="scratchpad"></div>';
echo '<form method="POST" action="', $_SERVER['REQUEST_URI'], '"><textarea id="scratchedit" name="scratchedit" onkeyup="autoGrow(this);">', $flatfile, '</textarea>';
echo '<input type="hidden" name="mtime" value="', $mtime, '"/>';
echo '<div id="controls"><input type="button" id="edit" value="Edit"/><input type="submit" id="submit" value="Save" name="submit"/></div></form>';
echo '<div id="scratchinfo">Last edited ', date('F d Y H:i:s', $mtime), '</div>';
?>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment