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