aboutsummaryrefslogtreecommitdiffstats
path: root/public/fileManager.php
blob: bdd52f1a6f857041f61a654914411dd6cb6f5388 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
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;
?>