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