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