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