Skip to content

Instantly share code, notes, and snippets.

@apfelbox
Created February 13, 2014 10:01
Show Gist options
  • Select an option

  • Save apfelbox/8972606 to your computer and use it in GitHub Desktop.

Select an option

Save apfelbox/8972606 to your computer and use it in GitHub Desktop.
Proof of Concept code for http://dev.apfelbox.net/simple_codepad/ (don't use it in production!)
<?php
class Codepad
{
/**
* Generates a new id and the file behind
*
* @return string
*/
public function generateNewId ()
{
// create new id
do {
$newId = hash("sha256", uniqid(microtime(), true));
}
while ($this->idExists($newId));
// create file
$this->store($newId, "");
return $newId;
}
/**
* Stores the id
*
* @param $id
* @param $content
*/
public function store ($id, $content)
{
file_put_contents($this->getFilePath($id), $content);
}
/**
* Reads the content of an id
*
* @param string $id
*
* @return null|string
*/
public function read ($id)
{
return $this->idExists($id)
? file_get_contents($this->getFilePath($id))
: null;
}
/**
* Returns, whether the stored id exists
*
* @param string $id
*
* @return bool
*/
public function idExists ($id)
{
if (!is_string($id))
{
return false;
}
return is_file($this->getFilePath($id));
}
/**
* Returns the file path to the file
*
* @param string $id
*
* @return string
*/
private function getFilePath ($id)
{
return STORAGE_DIR . "/{$id}.txt";
}
/**
* Returns a list of all ids
*
* @return array
*/
public function getAllIds ()
{
$directoryIterator = new DirectoryIterator(STORAGE_DIR);
$list = [];
foreach ($directoryIterator as $file)
{
if (!$file->isFile()) continue;
$id = $file->getBasename(".txt");
$list[$id] = $this->getLength($id);
}
return $list;
}
/**
* Returns the length of a text file
*
* @param string $id
*
* @return int
*/
public function getLength ($id)
{
return mb_strlen($this->read($id), "UTF-8");
}
/**
* Removes a text file
*
* @param string $id
*/
public function remove ($id)
{
$filePath = $this->getFilePath($id);
if (is_file($filePath))
{
@unlink($filePath);
}
}
}
<?php
define("STORAGE_DIR", __DIR__ . "/storage/");
$appDir = substr(__DIR__, strlen($_SERVER["DOCUMENT_ROOT"]));
define("APP_URL", "http://{$_SERVER['HTTP_HOST']}{$appDir}/");
require_once 'lib/Codepad.php';
$codepad = new Codepad();
// handle new ID
if (isset($_GET["new"]))
{
// create new id
$newId = $codepad->generateNewId();
header("Location: " . APP_URL . "index.php?id={$newId}");
exit;
}
// handle remove id
if (isset($_GET["remove"]) && $codepad->idExists($_GET["remove"]))
{
$codepad->remove($_GET["remove"]);
header("Location: " . APP_URL);
exit;
}
// handle id set
$id = isset($_GET["id"]) && $codepad->idExists($_GET["id"])
? $_GET["id"]
: null;
if (!is_null($id) && isset($_POST["content"]) && is_string($_POST["content"]))
{
$codepad->store($id, $_POST["content"]);
}
?><!doctype html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Simple Codepad</title>
</head>
<body>
<h1>Simple Codepad</h1>
<?php if (!is_null($id)) : ?>
<p><a href="./index.php">&larr; zurück</a></p>
<h2><code><?= $id ?> (<?= $codepad->getLength($id) ?> chars)</code></h2>
<p>Permalink: <code><?= APP_URL . "index.php?id={$id}" ?></code></p>
<form action="./index.php?id=<?= $id ?>" method="post">
<p><textarea name="content" id="" cols="200" rows="20" style="font-family: monospace;"><?= htmlspecialchars($codepad->read($id), ENT_QUOTES, "UTF-8") ?></textarea></p>
<p><button type="submit">speichern</button></p>
</form>
<?php else : ?>
<p><a href="./index.php?new">Create new ID</a></p>
<p>Bestehende IDs:</p>
<ul>
<?php foreach ($codepad->getAllIds() as $id => $length) : ?>
<li>
<a href="./index.php?id=<?= $id ?>"><code><?= $id ?></code></a>
(<?= $length ?> chars)
(<a href="./index.php?remove=<?= $id ?>">remove</a>)
</li>
<?php endforeach ?>
</ul>
<?php endif ?>
</body>
</html><?php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment