]> git.mxchange.org Git - simple-upload.git/blob - index.php
4f33d3e7cbef5948f7b3ea2edfedc058d010c87f
[simple-upload.git] / index.php
1 <?php
2         /*
3                 This program is free software: you can redistribute it and/or modify
4                 it under the terms of the GNU General Public License as published by
5                 the Free Software Foundation, either version 3 of the License, or
6                 (at your option) any later version.
7
8                 This program is distributed in the hope that it will be useful,
9                 but WITHOUT ANY WARRANTY; without even the implied warranty of
10                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11                 GNU General Public License for more details.
12
13                 You should have received a copy of the GNU General Public License
14                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
15         */
16
17         // =============={ Configuration Begin }==============
18         $settings = array(
19
20                 // Website title
21                 'title' => 'strace.club',
22
23                 // Directory to store uploaded files
24                 'uploaddir' => '.',
25
26                 // Display list uploaded files
27                 'listfiles' => true,
28
29                 // Allow users to delete files that they have uploaded (will enable sessions)
30                 'allow_deletion' => true,
31
32                 // Allow users to mark files as hidden
33                 'allow_private' => true,
34
35                 // Display file sizes
36                 'listfiles_size' => true,
37
38                 // Display file dates
39                 'listfiles_date' => true,
40
41                 // Display file dates format
42                 'listfiles_date_format' => 'F d Y H:i:s',
43
44                 // Randomize file names (number of 'false')
45                 'random_name_len' => 8,
46
47                 // Keep filetype information (if random name is activated)
48                 'random_name_keep_type' => true,
49
50                 // Random file name letters
51                 'random_name_alphabet' => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
52
53                 // Display debugging information
54                 'debug' => false,
55
56                 // Complete URL to your directory (including tracing slash)
57                 'url' => 'http://strace.club/',
58
59                 // Amount of seconds that each file should be stored for (0 for no limit)
60                 // Default 30 days
61                 'time_limit' => 60 * 60 * 24 * 30,
62
63                 // Files that will be ignored
64                 'ignores' => array('.', '..', 'LICENSE', 'README.md'),
65         );
66         // =============={ Configuration End }==============
67
68         // Enabling error reporting
69         if ($settings['debug']) {
70                 error_reporting(E_ALL);
71                 ini_set('display_startup_errors',1);
72                 ini_set('display_errors',1);
73         }
74
75         $data = array();
76
77         // Name of this file
78         $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
79
80         // Adding current script name to ignore list
81         $data['ignores'] = $settings['ignores'];
82         $data['ignores'][] = $data['scriptname'];
83
84         // Use canonized path
85         $data['uploaddir'] = realpath($settings['uploaddir']);
86
87         // Maximum upload size, set by system
88         $data['max_upload_size'] = ini_get('upload_max_filesize');
89
90         // If file deletion or private files are allowed, starting a session.
91         // This is required for user authentification
92         if ($settings['allow_deletion'] || $settings['allow_private']) {
93                 session_start();
94
95                 // 'User ID'
96                 if (!isset($_SESSION['upload_user_id']))
97                         $_SESSION['upload_user_id'] = rand(100000, 999999);
98
99                 // List of filenames that were uploaded by this user
100                 if (!isset($_SESSION['upload_user_files']))
101                         $_SESSION['upload_user_files'] = array();
102         }
103
104         // If debug is enabled, logging all variables
105         if ($settings['debug']) {
106                 // Displaying debug information
107                 echo '<h2>Settings:</h2>';
108                 echo '<pre>';
109                 print_r($settings);
110                 echo '</pre>';
111
112                 // Displaying debug information
113                 echo '<h2>Data:</h2>';
114                 echo '<pre>';
115                 print_r($data);
116                 echo '</pre>';
117                 echo '</pre>';
118
119                 // Displaying debug information
120                 echo '<h2>SESSION:</h2>';
121                 echo '<pre>';
122                 print_r($_SESSION);
123                 echo '</pre>';
124         }
125
126         // Format file size
127         function FormatSize ($bytes) {
128                 $units = array('B', 'KB', 'MB', 'GB', 'TB');
129
130                 $bytes = max($bytes, 0);
131                 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
132                 $pow = min($pow, count($units) - 1);
133
134                 $bytes /= pow(1024, $pow);
135
136                 return ceil($bytes) . ' ' . $units[$pow];
137         }
138
139         // Rotating a two-dimensional array
140         function DiverseArray ($vector) {
141                 $result = array();
142                 foreach ($vector as $key1 => $value1)
143                         foreach ($value1 as $key2 => $value2)
144                                 $result[$key2][$key1] = $value2;
145                 return $result;
146         }
147
148         // Handling file upload
149         function UploadFile ($file_data) {
150                 global $settings;
151                 global $data;
152                 global $_SESSION;
153
154                 $file_data['uploaded_file_name'] = basename($file_data['name']);
155                 $file_data['target_file_name'] = $file_data['uploaded_file_name'];
156
157                 // Generating random file name
158                 if ($settings['random_name_len'] !== false) {
159                         do {
160                                 $file_data['target_file_name'] = '';
161                                 while (strlen($file_data['target_file_name']) < $settings['random_name_len'])
162                                         $file_data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
163                                 if ($settings['random_name_keep_type'])
164                                         $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
165                         } while (file_exists($file_data['target_file_name']));
166                 }
167                 $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file_data['target_file_name'];
168
169                 // Do now allow to overwriting files
170                 if (file_exists($file_data['upload_target_file'])) {
171                         echo 'File name already exists' . "\n";
172                         return;
173                 }
174
175                 // Moving uploaded file OK
176                 if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) {
177                         if ($settings['allow_deletion'] || $settings['allow_private'])
178                                 $_SESSION['upload_user_files'][] = $file_data['target_file_name'];
179                         echo $settings['url'] .  $file_data['target_file_name'] . "\n";
180                 } else {
181                         echo 'Error: unable to upload the file.';
182                 }
183         }
184
185
186         // Files are being POSEed. Uploading them one by one.
187         if (isset($_FILES['file'])) {
188                 header('Content-type: text/plain');
189                 if (is_array($_FILES['file'])) {
190                         $file_array = DiverseArray($_FILES['file']);
191                         foreach ($file_array as $file_data)
192                                 UploadFile($file_data);
193                 } else
194                         UploadFile($_FILES['file']);
195                 exit;
196         }
197
198         // Other file functions (delete, private).
199         if (isset($_POST)) {
200                 if ($settings['allow_deletion'])
201                         if (isset($_POST['action']) && $_POST['action'] === 'delete')
202                                 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
203                                         if (file_exists($_POST['target'])) {
204                                                 unlink($_POST['target']);
205                                                 echo 'File has been removed';
206                                                 exit;
207                                         }
208
209                 if ($settings['allow_private'])
210                         if (isset($_POST['action']) && $_POST['action'] === 'privatetoggle')
211                                 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
212                                         if (file_exists($_POST['target'])) {
213                                                 if ($_POST['target'][0] === '.') {
214                                                         rename($_POST['target'], substr($_POST['target'], 1));
215                                                         echo 'File has been made visible';
216                                                 } else {
217                                                         rename($_POST['target'], '.' . $_POST['target']);
218                                                         echo 'File has been hidden';
219                                                 }
220                                                 exit;
221                                         }
222         }
223
224         // List files in a given directory, excluding certain files
225         function ListFiles ($dir, $exclude) {
226                 $file_array = array();
227                 $dh = opendir($dir);
228                         while (false !== ($filename = readdir($dh)))
229                                 if (is_file($filename) && !in_array($filename, $exclude))
230                                         $file_array[filemtime($filename)] = $filename;
231                 ksort($file_array);
232                 $file_array = array_reverse($file_array, true);
233                 return $file_array;
234         }
235
236         $file_array = ListFiles($settings['uploaddir'], $data['ignores']);
237
238         // Removing old files
239         foreach ($file_array as $file)
240                 if ($settings['time_limit'] < time() - filemtime($file))
241                         unlink($file);
242
243         $file_array = ListFiles($settings['uploaddir'], $data['ignores']);
244
245 ?>
246 <html lang="en-GB">
247         <head>
248                 <meta charset="utf-8">
249                 <title><?=$settings['title']?></title>
250                 <style media="screen">
251                         body {
252                                 background: #111;
253                                 margin: 0;
254                                 color: #ddd;
255                                 font-family: sans-serif;
256                         }
257
258                         body > h1 {
259                                 display: block;
260                                 background: rgba(255, 255, 255, 0.05);
261                                 padding: 8px 16px;
262                                 text-align: center;
263                                 margin: 0;
264                         }
265
266                         body > form {
267                                 display: block;
268                                 background: rgba(255, 255, 255, 0.075);
269                                 padding: 16px 16px;
270                                 margin: 0;
271                                 text-align: center;
272                         }
273
274                         body > ul {
275                                 display: block;
276                                 padding: 0;
277                                 max-width: 1000px;
278                                 margin: 32px auto;
279                         }
280
281                         body > ul > li {
282                                 display: block;
283                                 margin: 0;
284                                 padding: 0;
285                         }
286
287                         body > ul > li > a {
288                                 display: block;
289                                 margin: 0 0 1px 0;
290                                 list-style: none;
291                                 background: rgba(255, 255, 255, 0.1);
292                                 padding: 8px 16px;
293                                 text-decoration: none;
294                                 color: inherit;
295                                 opacity: 0.5;
296                         }
297
298                         body > ul > li > a:hover {
299                                 opacity: 1;
300                         }
301
302                         body > ul > li > a:active {
303                                 opacity: 0.5;
304                         }
305
306                         body > ul > li > a > span {
307                                 float: right;
308                                 font-size: 90%;
309                         }
310
311                         body > ul > li > form {
312                                 display: inline-block;
313                                 padding: 0;
314                                 margin: 0;
315                         }
316
317                         body > ul > li.owned {
318                                 margin: 8px;
319                         }
320
321                         body > ul > li > form > button {
322                                 opacity: 0.5;
323                                 display: inline-block;
324                                 padding: 4px 16px;
325                                 margin: 0;
326                                 border: 0;
327                                 background: rgba(255, 255, 255, 0.1);
328                                 color: inherit;
329                         }
330
331                         body > ul > li > form > button:hover {
332                                 opacity: 1;
333                         }
334
335                         body > ul > li > form > button:active {
336                                 opacity: 0.5;
337                         }
338
339                         body > ul > li.uploading {
340                                 animation: upanim 2s linear 0s infinite alternate;
341                         }
342
343                         @keyframes upanim {
344                                 from {
345                                         opacity: 0.3;
346                                 }
347                                 to {
348                                         opacity: 0.8;
349                                 }
350                         }
351                 </style>
352         </head>
353         <body>
354                 <h1><?=$settings['title']?></h1>
355                 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
356                         Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
357                         <input type="file" name="file[]" multiple required id="simpleupload-input"/>
358                 </form>
359                 <?php if ($settings['listfiles']) { ?>
360                         <ul id="simpleupload-ul">
361                                 <?php
362                                         foreach ($file_array as $mtime => $filename) {
363                                                 $file_info = array();
364                                                 $file_owner = false;
365                                                 $file_private = $filename[0] === '.';
366
367                                                 if ($settings['listfiles_size'])
368                                                         $file_info[] = FormatSize(filesize($filename));
369
370                                                 if ($settings['listfiles_size'])
371                                                         $file_info[] = date($settings['listfiles_date_format'], $mtime);
372
373                                                 if ($settings['allow_deletion'] || $settings['allow_private'])
374                                                         if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
375                                                                 $file_owner = true;
376
377                                                 $file_info = implode(', ', $file_info);
378
379                                                 if (strlen($file_info) > 0)
380                                                         $file_info = ' (' . $file_info . ')';
381
382                                                 $class = '';
383                                                 if ($file_owner)
384                                                         $class = 'owned';
385
386                                                 if (!$file_private || $file_owner) {
387                                                         echo "<li class=\"' . $class . '\">";
388
389                                                         echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
390
391                                                         if ($file_owner) {
392                                                                 if ($settings['allow_deletion'])
393                                                                         echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="delete" /><button type="submit">delete</button></form>';
394
395                                                                 if ($settings['allow_private'])
396                                                                         if ($file_private)
397                                                                                 echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="privatetoggle" /><button type="submit">make public</button></form>';
398                                                                         else
399                                                                                 echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="privatetoggle" /><button type="submit">make private</button></form>';
400                                                         }
401
402                                                         echo "</li>";
403                                                 }
404                                         }
405                                 ?>
406                         </ul>
407                 <?php } ?>
408                 <a href="https://github.com/muchweb/simple-php-upload"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
409                 <script charset="utf-8">
410                         var target_form = document.getElementById('simpleupload-form'),
411                                 target_ul = document.getElementById('simpleupload-ul'),
412                                 target_input = document.getElementById('simpleupload-input');
413
414                         target_form.addEventListener('dragover', function (event) {
415                                 event.preventDefault();
416                         }, false);
417
418                         function AddFileLi (name, info) {
419                                 target_form.style.display = 'none';
420
421                                 var new_li = document.createElement('li');
422                                 new_li.className = 'uploading';
423
424                                 var new_a = document.createElement('a');
425                                 new_a.innerHTML = name;
426                                 new_li.appendChild(new_a);
427
428                                 var new_span = document.createElement('span');
429                                 new_span.innerHTML = info;
430                                 new_a.appendChild(new_span);
431
432                                 target_ul.insertBefore(new_li, target_ul.firstChild);
433                         }
434
435                         function HandleFiles (event) {
436                                 event.preventDefault();
437
438                                 var i = 0,
439                                         files = event.dataTransfer.files,
440                                         len = files.length;
441
442                                 var form = new FormData();
443
444                                 for (; i < len; i++) {
445                                         form.append('file[]', files[i]);
446                                         AddFileLi(files[i].name, files[i].size + ' bytes');
447                                 }
448
449                                 var xhr = new XMLHttpRequest();
450                                 xhr.onload = function() {
451                                         window.location.reload();
452                                 };
453
454                                 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
455                                 xhr.send(form);
456                         }
457
458                         target_form.addEventListener('drop', HandleFiles, false);
459
460                         document.getElementById('simpleupload-input').onchange = function () {
461                                 AddFileLi('Uploading...', '');
462                                 target_form.submit();
463                         };
464                 </script>
465         </body>
466 </html>