Skip to content

Commit

Permalink
Add Option to Zip Files
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskthomas authored Jun 22, 2024
1 parent e57dbc6 commit fc7a20e
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ RUN set -x \
&& groupadd --system --gid 101 nginx \
&& useradd --system -g nginx --no-create-home --home-dir /nonexistent --comment "nginx user" --shell /bin/false --uid 101 nginx \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y nginx-core php-fpm \
&& apt-get install --no-install-recommends --no-install-suggests -y nginx-core php-fpm php-zip \
# forward request and error logs to docker log collector
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
Expand Down
137 changes: 60 additions & 77 deletions src/api.php
Original file line number Diff line number Diff line change
@@ -1,85 +1,68 @@
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// get the comment from the POST
$description = $_POST["description"];
$links = $_POST["links"];
// Create theme object
if (!empty($_POST["theme"])) {
$theme = json_decode($_POST["theme"], true);
} else {
$theme = array();
}
} else {
<?php

// If the request method is not POST, return a 405 Method Not Allowed response
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
header('allow: POST');
print "You can only access this page by submission of a form.";
exit();
die();
}

if (is_uploaded_file($_FILES["photo"]["tmp_name"])) {
// process image
$imagesize = getimagesize($_FILES["photo"]["tmp_name"]);
if ($imagesize !== false) {
// client uploaded a bona fide image!
$photo_data = base64_encode(file_get_contents($_FILES["photo"]["tmp_name"]));
$user_photo = "data:" . $imagesize["mime"] . ";base64," . $photo_data;
if (!isset($_POST["getzip"])) {
// If the "getzip" box is not checked, set the template to be downloaded as "index.html"

// If the "ispreview" box is not checked, set the template to be downloaded as "index.html"
if (!isset($_POST["ispreview"])) {
header('Content-Disposition: attachment; filename="index.html"');
}
}

if (!isset($_POST["ispreview"])) {
header('Content-Disposition: attachment; filename="index.html"');
// Include the template.php file
include "template.php";

// Exit the script
die();
} else {
// If the "getzip" box is checked, capture the output of template.php and create a zip file

// Start output buffering
ob_start();

// Include the template.php file
include "template.php";

// Get the contents of the output buffer
$buffer = ob_get_clean();

try {
// Create a temporary zip file
$tmpfile = tempnam(sys_get_temp_dir(), "linkfree");

// Register a shutdown function to delete the temporary file
register_shutdown_function('unlink', $tmpfile);

// Create a new ZipArchive object
$zip = new ZipArchive();
$zip->open($tmpfile, ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Add the index.html file to the zip
$zip->addFromString("index.html", $buffer);

$zip->close();
} catch (Exception $e) {
// If an exception is thrown, return a 500 Server Error response
http_response_code(500);
print "An error occurred while creating the zip file.";
die();
}

// Set the headers to download the zip file
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($tmpfile));
header('Content-Disposition: attachment; filename="linkfree.zip"');

// Output the contents of the zip file
readfile($tmpfile);

// Exit the script
die();
}
?>
<!DOCTYPE html>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title><?= $_POST["name"] ?></title>
<?php if (!empty($theme["css"])) { ?>
<link rel="stylesheet" href="<?= "{$_POST["themes-source"]}/{$theme["css"]}" ?>">
<?php } else { ?>
<style>
<?php include "default.css"; ?>
</style>
<?php } ?>
</head>

<body>
<?php if (!empty($user_photo)) { ?>
<img id="userPhoto" src="<?= $user_photo ?>" alt="User Photo">
<?php } ?>

<a href="<?= (!empty($_POST["url"]) ? $_POST["url"] : ".") ?>">
<h1 id="userName"><?= $_POST["name"] ?></h1>
</a>

<?php if (!empty($_POST["description"])) { ?>
<p id="description"><?= $_POST["description"] ?></p>
<?php } ?>

<div id="links">
<?php foreach ($links as $link) {
if (!empty($link["url"])) { ?>
<a class="link" href="<?= $link["url"] ?>" target="_blank">
<?php if (!empty($link["icon"])) { ?>
<ion-icon name="<?= $link["icon"] ?>"></ion-icon>
<?php } ?>
<?= $link["name"] ?>
</a>
<?php }
} ?>
<?php if (!empty($_POST["email"])) { ?>
<!--email_off-->
<a class="link" href="mailto:<?= $_POST["email"] ?>" target="_blank"><ion-icon name="mail"></ion-icon> Email</a>
<!--/email_off-->
<?php } ?>
</div>
<?php if (!empty($theme["js"])) { ?>
<script src="<?= "{$_POST["themes-source"]}/{$theme["js"]}" ?>"></script>
<?php } ?>
<script type="module" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ionicons/ionicons.js"></script>
</body>

</html>
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const localStorageIgnore = ["photo"];

// Elements
const addCustomLinkButton = document.querySelector("a.btn");
const clearButton = document.querySelector("a.btn-danger");
const form = document.querySelector("form");
const checkbox_preview = document.getElementById("ispreview");
const checkbox_zip = document.getElementById("getzip");

let linksIndex = Number(addCustomLinkButton.dataset.index);

Expand Down Expand Up @@ -205,3 +208,13 @@ urlInputs.forEach((input) => {
addProtocolIfMissing(event.target);
});
});

// Disable the zip checkbox if the preview checkbox is checked
checkbox_preview.addEventListener("change", () => {
checkbox_zip.disabled = checkbox_preview.checked;
});

// Disable the preview checkbox if the zip checkbox is checked
checkbox_zip.addEventListener("change", () => {
checkbox_preview.disabled = checkbox_zip.checked;
});
4 changes: 4 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@
<label for="ispreview" class="form-check-label">Preview?</label>
<span class="form-text">Make a preview instead of downloading file.</span>
</div>
<div class="mb-3 form-check">
<input type="checkbox" id="getzip" name="getzip" class="form-check-input">
<label for="getzip" class="form-check-label">Zip File?</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<a id="clear" class="btn btn-danger" role="button">Clear</a>
</form>
Expand Down
77 changes: 77 additions & 0 deletions src/template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
// get the comment from the POST
$description = $_POST["description"];
$links = $_POST["links"];

// Create theme object
if (!empty($_POST["theme"])) {
$theme = json_decode($_POST["theme"], true);
} else {
$theme = array();
}

// Convert uploaded photo to data URI
if (is_uploaded_file($_FILES["photo"]["tmp_name"])) {
// process image
$imagesize = getimagesize($_FILES["photo"]["tmp_name"]);
if ($imagesize !== false) {
// client uploaded a bona fide image!
$photo_data = base64_encode(file_get_contents($_FILES["photo"]["tmp_name"]));
$user_photo = "data:" . $imagesize["mime"] . ";base64," . $photo_data;
}
}
?>
<!DOCTYPE html>
<html>

<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="UTF-8">
<title><?= $_POST["name"] ?></title>
<?php if (!empty($theme["css"])) { ?>
<link rel="stylesheet" href="<?= "{$_POST["themes-source"]}/{$theme["css"]}" ?>">
<?php } else { ?>
<style>
<?php include "default.css"; ?>
</style>
<?php } ?>
</head>

<body>
<?php if (!empty($user_photo)) { ?>
<img id="userPhoto" src="<?= $user_photo ?>" alt="User Photo">
<?php } ?>

<a href="<?= (!empty($_POST["url"]) ? $_POST["url"] : ".") ?>">
<h1 id="userName"><?= $_POST["name"] ?></h1>
</a>

<?php if (!empty($_POST["description"])) { ?>
<p id="description"><?= $_POST["description"] ?></p>
<?php } ?>

<div id="links">
<?php foreach ($links as $link) {
if (!empty($link["url"])) { ?>
<a class="link" href="<?= $link["url"] ?>" target="_blank">
<?php if (!empty($link["icon"])) { ?>
<ion-icon name="<?= $link["icon"] ?>"></ion-icon>
<?php } ?>
<?= $link["name"] ?>
</a>
<?php }
} ?>
<?php if (!empty($_POST["email"])) { ?>
<!--email_off-->
<a class="link" href="mailto:<?= $_POST["email"] ?>" target="_blank"><ion-icon name="mail"></ion-icon> Email</a>
<!--/email_off-->
<?php } ?>
</div>
<?php if (!empty($theme["js"])) { ?>
<script src="<?= "{$_POST["themes-source"]}/{$theme["js"]}" ?>"></script>
<?php } ?>
<script type="module" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ionicons/ionicons.js"></script>
</body>

</html>

0 comments on commit fc7a20e

Please sign in to comment.