aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaksymilian Jopek <maks@jopek.eu>2023-04-12 16:26:30 +0200
committerMaksymilian Jopek <maks@jopek.eu>2023-04-12 16:26:30 +0200
commitd5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd (patch)
treec547f0145026908e39e33dcaef9c496065c1d7c6
downloadphp-upload-d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd.tar.gz
php-upload-d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd.tar.zst
php-upload-d5cb8fd65d5c86c8627d3728d3aec7e1ae6ed4dd.zip
Initial commit with working app
-rw-r--r--.gitignore2
-rw-r--r--Readme.md3
-rw-r--r--passwords.txt1
-rw-r--r--private/.gitkeep0
-rw-r--r--public/fileManager.php284
-rw-r--r--public/fileManagerFiles/download.php63
-rw-r--r--public/index.php71
-rwxr-xr-xstart3
8 files changed, 427 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..389308f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+private/*
+!private/.gitkeep
diff --git a/Readme.md b/Readme.md
new file mode 100644
index 0000000..9bb15b6
--- /dev/null
+++ b/Readme.md
@@ -0,0 +1,3 @@
+# Upload app in PHP
+Credentials in `passwords.txt` are:
+- Username: maks, password: qwerty
diff --git a/passwords.txt b/passwords.txt
new file mode 100644
index 0000000..d2ce0bb
--- /dev/null
+++ b/passwords.txt
@@ -0,0 +1 @@
+maks:$2y$10$gOx4lHcC04npTafERFdiOelQrIltCPxuTD6TqttQk/drdwdexo/GS
diff --git a/private/.gitkeep b/private/.gitkeep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/private/.gitkeep
diff --git a/public/fileManager.php b/public/fileManager.php
new file mode 100644
index 0000000..bdd52f1
--- /dev/null
+++ b/public/fileManager.php
@@ -0,0 +1,284 @@
+<?php
+session_start();
+
+function random_str(
+ int $length = 64,
+ string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
+): string
+{
+ if ( $length < 1 )
+ throw new RangeException("Length must be a positive integer");
+
+ $pieces = [];
+ $max = mb_strlen($keyspace, '8bit') - 1;
+ for ($i = 0; $i < $length; ++$i)
+ $pieces[] = $keyspace[random_int(0, $max)];
+
+ return implode('', $pieces);
+}
+
+if ( !$_SESSION["logged"] )
+{
+ header("Location: index.php");
+ exit();
+}
+
+if ( isset($_FILES['files']['name'][0]) )
+{
+ if ( isset($_FILES['files']['name'][0]) && ($_FILES['files']['error'][0] == UPLOAD_ERR_OK) )
+ {
+ $uploaddir = '../private/upload/';
+ $uploaddir = realpath(getcwd() . DIRECTORY_SEPARATOR . $uploaddir) . DIRECTORY_SEPARATOR;
+ $success = [];
+ $error = array();
+ $i = 0;
+ foreach ($_FILES['files']['error'] as $k => $v)
+ {
+ if ( $v == 0 )
+ {
+ $id = random_str();
+ $new_name = $uploaddir . $id;
+ var_dump($new_name);
+ $temp_name = $_FILES['files']['tmp_name'][$k];
+ $file_name = $_FILES['files']['name'][$k];
+
+ if ( file_exists($new_name) ) {
+ $_SESSION["communicates"]['fileError'] = "Plik z podaną nazwą $new_name już istnieje na serwerze";
+ }
+ else {
+ if ( move_uploaded_file($temp_name, $new_name) ) {
+ $success[] = $_FILES['files']['name'][$k];
+ $fd = fopen($uploaddir . "../downloads.txt", 'a');
+ $description = $_POST["text$i"];// != "" ? $_POST["text$i"] : "brak";
+ fwrite($fd, "$id;$file_name;$uploaddir;$description\n");
+ fclose($fd);
+ } else {
+ $error[] = $_FILES['files']['name'][$k];
+ }
+ }
+ }
+ $i++;
+ }
+ if ( count($error) )
+ $_SESSION["communicates"]['uploadError'] = "Nie udało się załadować następujących plików: " . implode(", ", $error);
+ else if ( count($success) )
+ $_SESSION["communicates"]['uploadSuccess'] = "Udało się załadować następujące pliki: " . implode(", ", $success);
+ } else
+ {
+ switch ($_FILES['fileName']['error'])
+ {
+ case UPLOAD_ERR_INI_SIZE:
+ case UPLOAD_ERR_FORM_SIZE:
+ $_SESSION["communicates"]["uploadFileError"] = "Przekroczony maksymalny rozmiar pliku!";
+ break;
+ case UPLOAD_ERR_PARTIAL:
+ $_SESSION["communicates"]["uploadFileError"] = "Odebrano tylko część pliku!";
+ break;
+ case UPLOAD_ERR_NO_FILE:
+ $_SESSION["communicates"]["uploadFileError"] = "Plik nie został pobrany!";
+ break;
+ case UPLOAD_ERR_NO_TMP_DIR:
+ $_SESSION["communicates"]["uploadFileError"] = "Brak dostępu do katalogu tymczasowego!";
+ break;
+ case UPLOAD_ERR_CANT_WRITE:
+ $_SESSION["communicates"]["uploadFileError"] = "Nie udało się zapisać pliku na dysku serwera!";
+ break;
+ case UPLOAD_ERR_EXTENSION:
+ $_SESSION["communicates"]["uploadFileError"] = "Ładowanie pliku przerwane przez rozszerzenie PHP!";
+ break;
+ default:
+ $_SESSION["communicates"]["uploadFileError"] = "Nieznany typ błędu!";
+ }
+ }
+ header("Location: fileManager.php");
+ exit();
+}
+?>
+<!DOCTYPE html>
+<html>
+
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport"
+ content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
+
+ <!-- Bootstrap CSS -->
+ <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
+ integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
+
+ <style>
+ body {
+ display: grid;
+ grid-template-columns: repeat(2, 1fr);
+ background-color: black;
+ color: white;
+ overflow: hidden;
+ }
+
+ #form {
+ display: grid;
+ grid-template-rows: repeat(2, 1fr);
+ }
+ #form div {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ }
+
+ input[type="file"] {
+ margin-top: 2rem;
+ background-color: rgba(0, 0, 0, 0);
+ border: 3px solid white;
+ border-radius: 1rem;
+ padding: 1rem;
+ }
+ ::file-selector-button {
+ background-color: #0000;
+ border: 3px solid #0056b3;
+ color: #0056b3;
+ font-size: 1rem;
+ padding: .5rem;
+ border-radius: .75rem;
+ margin-right: 1.5rem;
+ }
+ label {
+ margin-top: 0.75rem;
+ }
+
+ button {
+ background-color: #0000;
+ border: 3px solid #0056b3;
+ color: #0056b3;
+ font-size: 1.5rem;
+ padding: .5rem;
+ border-radius: .75rem;
+ }
+
+ .list-group {
+ display: flex;
+ flex-direction: column;
+ gap: 1rem;
+ font-size: 1.5rem;
+ max-height: 90vh;
+ overflow-y: scroll;
+ margin-top: 2rem;
+ text-align: center;
+ align-items: center;
+ }
+
+ .list-group div {
+ border: 3px solid white;
+ border-radius: 2rem;
+ padding: 1rem;
+ width: 80%;
+ }
+
+ .list-group span {
+ font-size: 1.4rem;
+ }
+
+ #checkIfNotEmpty {
+ position: absolute;
+ font-size: 2rem;
+ color: #E00;
+ left: 10rem;
+ bottom: 10rem;
+ }
+ </style>
+
+ <title>fileManager</title>
+</head>
+
+<body>
+ <form id="form" enctype="multipart/form-data" action="fileManager.php" method="POST">
+ <div>
+ <input type="file" name="files[]" id="file0" onchange="addInput(this)">
+ <!--<input type="file" name="files[]" id="file1" onchange="addInput(this)">-->
+ <!--<input type="file" name="files[]" id="file2" onchange="addInput(this)">-->
+ <br><br>
+ <button type="button" onclick="checkIfNotEmpty(event)" id="button">Send files</button>
+ </div>
+ <p id="checkIfNotEmpty">
+ <?php
+ foreach ($_SESSION["communicates"] as $key => $value)
+ echo "$value";
+ ?>
+ </p>
+ </form>
+ <div class="list-group">
+ <h1>Uploaded files</h1>
+ <?php
+ //$path = "/var/www/private/fileManagerFiles/downloads.txt";
+ $path = "../private/downloads.txt";
+ $fd = fopen($path, 'r');
+ if ( $fd )
+ {
+ while (!feof($fd))
+ {
+ $line = trim(fgets($fd));
+ $arr = explode(";", $line);
+ if ( count($arr) == 4 )
+ {
+ $where = "fileManagerFiles/download.php?fileid=";
+ echo "<div><a href='$where{$arr[0]}'>{$arr[1]}</a>";
+ if($arr[3] !== "")
+ echo "<br><span>{$arr[3]}</span></div>";
+ else
+ echo "</div>";
+ }
+ }
+ fclose($fd);
+ } else
+ echo '<a href="#">Something went wrong</a>';
+ ?>
+ </div>
+ <script>
+ function checkIfNotEmpty(e) {
+ e.preventDefault();
+ let form = document.getElementById("form");
+
+ for (let input of form.querySelectorAll('input[type="file"]')) {
+ if (input.value) {
+ form.submit();
+ return
+ }
+ }
+
+ document.getElementById("checkIfNotEmpty").innerHTML = "No files given!";
+ }
+
+ let number = 1;
+
+ let div = document.getElementById("form").children[0];
+ let br = document.getElementById("br");
+ function addInput(self) {
+ let newInput = document.createElement("input");
+ //let txtInput = document.createElement("input");
+ let label = document.createElement("label");
+
+ newInput.type = "file";
+ newInput.name = "files[]";
+ newInput.id = "file" + number.toString();
+ newInput.onchange = () => addInput(newInput);
+ //txtInput.type = "text";
+ //txtInput.name = "text" + self.id[4];
+ //txtInput.id = txtInput.name;
+ number++;
+ label.innerHTML = `Opis: <input type="text" name="${"text" + self.id[4]}" id="${"text" + self.id[4]}">`;
+ console.log(self.id, self.id[4])
+
+ self.after(label, newInput);
+ //let t = self.nextSibling.nextSibling;
+ ////form.insertBefore(txtInput, t.nodeName === "INPUT" ? t : newInput);
+ //div.insertBefore(label, newInput);
+ }
+ </script>
+</body>
+
+</html>
+<?php
+session_unset();
+$_SESSION["logged"] = true;
+?>
diff --git a/public/fileManagerFiles/download.php b/public/fileManagerFiles/download.php
new file mode 100644
index 0000000..1b7731c
--- /dev/null
+++ b/public/fileManagerFiles/download.php
@@ -0,0 +1,63 @@
+<?php
+session_start();
+
+if ( !$_SESSION["logged"] )
+{
+ header("Location: /index.php");
+ exit();
+}
+
+function send($id)
+{
+ $path = "../../private/downloads.txt";
+ if ( !($fd = fopen($path, "r")) )
+ {
+ $_SESSION["communicates"]["serverError"] = "<span style='color:red'>Server error! Mail admin at admin@{$_SERVER['HTTP_HOST']}</span>";
+ header("Location: ../fileManager.php");
+ return;
+ }
+ $found = false;
+ while (!feof($fd))
+ {
+ $line = trim(fgets($fd));
+ $arr = explode(";", $line);
+ if ( count($arr) == 4 )
+ {
+ if ( $id === $arr[0] )
+ {
+ $found = true;
+ $path = $arr[2];
+ $name = $arr[1];
+ break;
+ }
+ }
+ }
+
+ //var_dump($found);
+ //var_dump($path . $id);
+ //var_dump(!file_exists($path . $id));
+ if ( !$found || !file_exists($path . $id) )
+ {
+ $_SESSION["communicates"]["fileError"] = "<span style='color:red'>No file with given id!</span>";
+ return;
+ }
+ $fd = fopen($path . $id, "r");
+ $size = filesize($path . $id);
+ //$contents = fread($fd, $size);
+ fclose($fd);
+
+ header("Content-Type: application/octet-stream");
+ header("Content-Length: $size;");
+ header('Content-Disposition: attachment; filename="'.$name.'"');
+ echo file_get_contents($path . $id);
+ //echo $contents;
+}
+
+if ( !empty($_GET['fileid']) )
+ send($_GET['fileid']);
+else
+{
+ $_SESSION["communicates"]["fileError"] = "<span style='color:red'>No file id given!</span>";
+ header("Location: ../fileManager.php");
+}
+?>
diff --git a/public/index.php b/public/index.php
new file mode 100644
index 0000000..dab9201
--- /dev/null
+++ b/public/index.php
@@ -0,0 +1,71 @@
+<?php
+session_start();
+if (isset($_SESSION["logged"]) && $_SESSION["logged"] === true) {
+ header("Location: fileManager.php");
+ exit();
+}
+if(isset($_POST["username"]) && isset($_POST["password"])) {
+ $u = $_POST["username"];
+ $p = $_POST["password"];
+ $passes = file("../passwords.txt");
+ foreach ($passes as $pass) {
+ $pass = explode(':', trim($pass));
+ if($pass[0] === $u && password_verify($p, $pass[1])) {
+ $_SESSION['logged'] = true;
+ $_SESSION['username'] = $u;
+ header("Location: fileManager.php");
+ exit();
+ }
+ }
+}
+?>
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <title>sessionLogin</title>
+ <style>
+ body {
+ background-color: black;
+ color: white;
+ }
+
+ #form {
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ }
+ label {
+ margin-top: 0.75rem;
+ }
+ button {
+ background-color: #0000;
+ border: 3px solid #0056b3;
+ color: #0056b3;
+ font-size: 1.5rem;
+ padding: .5rem;
+ border-radius: .75rem;
+ }
+ </style>
+</head>
+
+<body style="display: grid; place-items: center; height: 100vh;">
+ <div style="width: min-content; font-size: 1.7rem; transform: translateY(-20%);">
+ <form method="post" action="index.php">
+ <label>
+ Name:
+ <input type="text" name="username" required><br>
+ </label><br>
+ <label>
+ Password:
+ <input type="password" name="password" required>
+ </label><br><br>
+ <button type="submit">Submit</button>
+ </form>
+ </div>
+</body>
+
+</html>
diff --git a/start b/start
new file mode 100755
index 0000000..117915c
--- /dev/null
+++ b/start
@@ -0,0 +1,3 @@
+#!/bin/sh
+cd public
+php -S 0.0.0.0:8000