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