Added metadata, upload from URL

This commit is contained in:
Jade Levesque 2018-02-03 23:14:25 -06:00
parent 05955672d2
commit 8a5129bb87
4 changed files with 188 additions and 50 deletions

145
beam.php
View File

@ -1,83 +1,134 @@
<?php <?php
include("header.html"); include("lib.php");
echo "<main class=\"text-center\">";
if (empty($_POST["desired_filename"])) include("header.html");
echo("\n<main class=\"text-center\">\n");
function celebrate($dest_file)
{
?>
<p class="uppercase">Thank you for feeding me</p>
<p class="uppercase">Your coin is in safe hands</p>
<p class="uppercase">It will not be de-atomized for at least a year</p>
<title>COIN INSERTED <3</title>
<img alt="A coin lovingly inserted to a coin-slot" src=res/img/coininserted_big.png>
<p class="uppercase">Your coin's <a href="<?php echo($dest_file); ?>">over here</a></p>
<p class="uppercase"><a href="<?php echo($dest_file); ?>">
https://coinsh.red/<?php echo($dest_file); ?></a></p>
<p class="uppercase">It's meta-stuff (or lack thereof) is
<a href="<?php echo($dest_file); ?>.txt\">here</a></p>
<?php
}
function celebrate_death($dest_file, $upload_method)
{
?>
<title>COIN ON FIRE</title>
<img alt="Ouch, that coin's on fire. Literally." src="res/img/coinfire_big.png">
<p>...</p>
<p class="uppercase">That was weird, something went wrong.</p>
<p class="uppercase">Give it another go-- if it happens again, tell me.<p>
<p class="uppercase">Also tell this: "File: <?php echo($dest_file); ?>,
Method: <?php echo($upload_method); ?>."</p>
<?php
}
if (!empty($_POST["upload_url"]))
{
$upload_url = $_POST["upload_url"];
$file_name = url_to_filename($upload_url);
$upload_method = 2;
}
else if (!empty($_POST["desired_filename"]))
{
$file_name = $_POST["desired_filename"];
$upload_method = 1;
}
else if (!empty($_FILES["fileToUpload"]["name"]))
{ {
$file_name = $_FILES["fileToUpload"]["name"]; $file_name = $_FILES["fileToUpload"]["name"];
$upload_method = 1;
} }
else else
{ {
$file_name = $_POST["desired_filename"]; $upload_method = 0;
} }
// check if file-name has any... undesirable characteristics // check if file-name has any... undesirable characteristics
$file_name = sanitize_filename($file_name);
if (strstr($file_name, " "))
{
$file_name = str_replace(" ", "_", $file_name);
}
if (strstr($file_name, ".."))
{
$file_name = str_replace("..", "_", $file_name);
}
if (strstr($file_name, "/"))
{
$file_name = str_replace("/", "_", $file_name);
}
// and now we pretend that never happened // and now we pretend that never happened
$dest_dir = "p/"; $dest_dir = "p/";
$dest_file = $dest_dir . $file_name; $dest_file = $dest_dir . $file_name;
$beaming_permitted = 1; $beaming_permitted = 1;
if (file_exists($dest_file)) if (file_exists($dest_file))
{ {
echo "<p class=\"uppercase\">We're getting some interference</p>"; echo("\t<p class=\"uppercase\">We're getting some interference</p>\n");
echo "<p class=\"uppercase\">Please use a different coin-name</p>"; echo("\t<p class=\"uppercase\">Please use a different coin-name</p>\n\n");
$beaming_permitted = 0; $beaming_permitted = 0;
} }
elseif ($_FILES["fileToUpload"]["size"] > 50000000)
{
echo "<p class=\"uppercase\">This coin will be up for at least three months</p>";
echo "<p class=\"uppercase\">7 moons in the ordinary sabbatical cycle</p>";
echo "<p class=\"uppercase\">After that, it *may* be de-atomized</p>";
}
elseif ($_FILES["fileToUpload"]["size"] < 25000000)
{
echo "<p class=\"uppercase\">Thank you for feeding me</p>";
echo "<p class=\"uppercase\">Your coin is in safe hands</p>";
echo "<p class=\"uppercase\">It will not be de-atomized for at least a year</p>";
}
if ($beaming_permitted == 0)
if ($beaming_permitted == 0) { {
echo "<title>COIN ON FIRE</title>"; echo("\t<title>COIN ON FIRE</title>\n\n");
echo "<img alt=\"A big coin in bloody flames.\" src=../../res/img/coinfire_big.png>"; echo("\t<img alt=\"A big coin in bloody flames.\" src=res/img/coinfire_big.png>\n");
echo "<p>sorry <\\3</p>"; echo("\t<p>sorry <\\3</p>\n");
} }
else { else
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$dest_file)) { {
echo "<title>COIN INSERTED <3</title>"; if ($upload_method == 1)
echo "<img alt=\"A coin lovingly inserted to a coin-slot\" src=../../res/img/coininserted_big.png>"; {
echo "<p class=\"uppercase\">It is <a href=" . $dest_file . ">here</a></p>"; if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],$dest_file))
{
write_metadata($dest_file, $_POST["file_desc"], $_POST["file_source"]);
celebrate($dest_file);
}
else
{
celebrate_death($dest_file, $upload_method);
}
}
else if ($upload_method == 2)
{
$download = file_get_contents($upload_url);
if (file_put_contents($dest_file, $download))
{
write_metadata($dest_file, $_POST["file_desc"], $upload_url . "\n\t" . $_POST["file_source"]);
celebrate($dest_file);
}
else
{
celebrate_death($dest_file, $upload_method);
}
}
else if ($upload_method == 0)
{
echo("\t<p class=\"uppercase\">(upload something next time, my dumb, sweet honey-pie <3)<p>");
celebrate_death("n/a", "smartassery");
} }
else else
{ {
echo "<title>COIN ON FIRE</title>"; celebrate_death($dest_file, $upload_method);
echo "<img alt=\"Ouch, that coin is on fire. Literally.\" src=../../res/img/coinfire_big.png>";
echo "<p>...</p>";
echo "<p class=\"uppercase\">That was weird, something went wrong. Try again.</p>";
} }
} }
?> ?>
</main> </main>

View File

@ -1,4 +1,4 @@
<main class="text-center"> <div class="text-center">
<footer> <footer>
No data will be recorded on who inserts what coin-- all I'll get is a timestamp. No data will be recorded on who inserts what coin-- all I'll get is a timestamp.
</footer> </footer>
@ -6,7 +6,7 @@
<p> <p>
<a href="https://notabug.org/jadedctrl/insert-coin">source juice</a> <a href="https://notabug.org/jadedctrl/insert-coin">source juice</a>
</p> </p>
</main> </div>
</body> </body>
</html> </html>

View File

@ -1,11 +1,24 @@
<?php include("header.html");?> <?php include("header.html");?>
<main class="text-center" style="margin-top: 30px"> <main class="text-center" style="margin-top: 30px">
<form action="beam.php" method="post" enctype="multipart/form-data"> <form action="beamgit.php" method="post" enctype="multipart/form-data">
<p> <p>
<input type="text" placeholder="Filename (Blank for uploaded name)" <input type="text" placeholder="Filename (Blank for uploaded name)"
name="desired_filename" id="desired_filename" style="width: 400px;"> name="desired_filename" id="desired_filename" style="width: 400px;">
</p> </p>
<p>
<input type="text" placeholder="Description (Optional)"
name="file_desc" id="file_desc" style="width: 400px;">
<input type="text" placeholder="Source (Optional)"
name="file_source" id="file_source" style="width: 400px;">
</p>
<!-- <p>
<input type="text" placeholder="Upload from URL"
name="upload_url" id="upload_url" style="width: 400px;">
</p> -->
<input type="file" name="fileToUpload" id="fileToUpload" style="margin: 0 auto"> <input type="file" name="fileToUpload" id="fileToUpload" style="margin: 0 auto">
<input style="margin-top: 10px; margin-bottom: 10px" type="submit" value="INSERT COIN" name="submit"> <input style="margin-top: 10px; margin-bottom: 10px" type="submit" value="INSERT COIN" name="submit">
</form> </form>

74
lib.php Executable file
View File

@ -0,0 +1,74 @@
<?php
function write_metadata($filename, $desc, $source)
{
$metafile = fopen($filename . ".txt", 'w');
fwrite($metafile, "Source:\n\t" . implode('', sanitize_long_input($source, "\t")) . "\n");
fwrite($metafile, "Descri:\n\t" . implode('', sanitize_long_input($desc, "\t")) . "\t");
fclose($metafile);
}
function sanitize_long_input($sanitizee, $preface)
{
$sanitized = [];
$sani_len = strlen($sanitizee) - 1;
if ($sani_len + 1 == 0)
{
$sanitized = "N/A";
return $sanitized;
}
$j = 0;
for($i = 0; $i <= $sani_len; $i++)
{
if ($i % 80 == 0 && $i != 0)
{
if (substr($sanitizee, $i, 1) == ' ')
{
$sanitized[$j] = "\n";
$sanitized[$j + 1] = $preface;
$j++;
}
else
{
$sanitized[$j] = "-";
$sanitized[$j + 1] = "\n";
$sanitized[$j + 2] = $preface;
$sanitized[$j + 3] = $sanitizee[$i];
$j += 3;
}
}
else
{
$sanitized[$j] = $sanitizee[$i];
}
$j++;
}
return $sanitized;
}
function sanitize_filename($sanitizee)
{
$sanitized = str_replace(" ", "_", $sanitizee);
$sanitized = str_replace("..", "_", $sanitized);
$sanitized = str_replace("/", "_", $sanitized);
$sanitized = str_replace("\\", "_", $sanitized);
return $sanitized;
}
function url_to_filename($url)
{
$working = explode('/', $url);
$filename = $working[count($working) - 1];
return $filename;
}
?>