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.
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.
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/>.
17 // =============={ Configuration Begin }==============
21 'title' => 'strace.club',
23 // Directory to store uploaded files
26 // Display list uploaded files
29 // Allow users to delete files that they have uploaded (will enable sessions)
30 'allow_deletion' => true,
32 // Allow users to mark files as hidden
33 'allow_private' => true,
36 'listfiles_size' => true,
39 'listfiles_date' => true,
41 // Display file dates format
42 'listfiles_date_format' => 'F d Y H:i:s',
44 // Randomize file names (number of 'false')
45 'random_name_len' => 8,
47 // Keep filetype information (if random name is activated)
48 'random_name_keep_type' => true,
50 // Random file name letters
51 'random_name_alphabet' => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
53 // Display debugging information
56 // Complete URL to your directory (including tracing slash)
57 'url' => 'http://strace.club/',
59 // Amount of seconds that each file should be stored for (0 for no limit)
61 'time_limit' => 60 * 60 * 24 * 30,
63 // Files that will be ignored
64 'ignores' => array('.', '..', 'LICENSE', 'README.md'),
73 'remove_old_files' => true,
75 // Privacy: Allow external references (the "fork me" ribbon)
76 'allow_external_refs' => true,
78 // =============={ Configuration End }==============
80 // Is the local config file there?
81 if (isReadableFile('config-local.php')) {
83 include('config-local.php');
86 // Enabling error reporting
87 if ($settings['debug']) {
88 error_reporting(E_ALL);
89 ini_set('display_startup_errors',1);
90 ini_set('display_errors',1);
96 $data['scriptname'] = $settings['url'] . '/' . pathinfo(__FILE__, PATHINFO_BASENAME);
98 // Adding current script name to ignore list
99 $data['ignores'] = $settings['ignores'];
100 $data['ignores'][] = basename($data['scriptname']);
102 // Use canonized path
103 $data['uploaddir'] = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . $settings['uploaddir']);
105 // Maximum upload size, set by system
106 $data['max_upload_size'] = ini_get('upload_max_filesize');
108 // If file deletion or private files are allowed, starting a session.
109 // This is required for user authentification
110 if ($settings['allow_deletion'] || $settings['allow_private']) {
114 if (!isset($_SESSION['upload_user_id']))
115 $_SESSION['upload_user_id'] = mt_rand(100000, 999999);
117 // List of filenames that were uploaded by this user
118 if (!isset($_SESSION['upload_user_files']))
119 $_SESSION['upload_user_files'] = array();
122 // If debug is enabled, logging all variables
123 if ($settings['debug']) {
124 // Displaying debug information
125 echo '<h2>Settings:</h2>';
130 // Displaying debug information
131 echo '<h2>Data:</h2>';
137 // Displaying debug information
138 echo '<h2>SESSION:</h2>';
145 function formatSize ($bytes) {
146 $units = array('B', 'KB', 'MB', 'GB', 'TB');
148 $bytes = max($bytes, 0);
149 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
150 $pow = min($pow, count($units) - 1);
152 $bytes /= pow(1024, $pow);
154 return ceil($bytes) . ' ' . $units[$pow];
157 // Rotating a two-dimensional array
158 function diverseArray ($vector) {
160 foreach ($vector as $key1 => $value1)
161 foreach ($value1 as $key2 => $value2)
162 $result[$key2][$key1] = $value2;
166 // Handling file upload
167 function uploadFile ($file_data) {
168 global $settings, $data;
170 $file_data['uploaded_file_name'] = basename($file_data['name']);
171 $file_data['target_file_name'] = $file_data['uploaded_file_name'];
173 // Generating random file name
174 if ($settings['random_name_len'] !== false) {
176 $file_data['target_file_name'] = '';
177 while (strlen($file_data['target_file_name']) < $settings['random_name_len'])
178 $file_data['target_file_name'] .= $settings['random_name_alphabet'][mt_rand(0, strlen($settings['random_name_alphabet']) - 1)];
179 if ($settings['random_name_keep_type'])
180 $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
181 } while (isReadableFile($file_data['target_file_name']));
183 $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file_data['target_file_name'];
185 // Do now allow to overwriting files
186 if (isReadableFile($file_data['upload_target_file'])) {
187 echo 'File name already exists' . "\n";
191 // Moving uploaded file OK
192 if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) {
193 if ($settings['allow_deletion'] || $settings['allow_private'])
194 $_SESSION['upload_user_files'][] = $file_data['target_file_name'];
195 echo $settings['url'] . $file_data['target_file_name'] . "\n";
197 echo 'Error: unable to upload the file.';
202 function deleteFile ($file) {
205 if (in_array(substr($file, 1), $_SESSION['upload_user_files']) || in_array($file, $_SESSION['upload_user_files'])) {
206 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
207 if (!in_array($file, $data['ignores']) && isReadableFile($fqfn)) {
209 echo 'File has been removed';
215 // Mark/unmark file as hidden
216 function markUnmarkHidden ($file) {
219 if (in_array(substr($file, 1), $_SESSION['upload_user_files']) || in_array($file, $_SESSION['upload_user_files'])) {
220 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
221 if (!in_array($file, $data['ignores']) && isReadableFile($fqfn)) {
222 if (substr($file, 0, 1) === '.') {
223 rename($fqfn, substr($fqfn, 1));
224 echo 'File has been made visible';
226 rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file);
227 echo 'File has been hidden';
234 // Checks if the given file is a file and is readable
235 function isReadableFile ($file) {
236 return (is_file($file) && is_readable($file));
239 // Files are being POSEed. Uploading them one by one.
240 if (isset($_FILES['file'])) {
241 header('Content-type: text/plain');
242 if (is_array($_FILES['file'])) {
243 $file_array = diverseArray($_FILES['file']);
244 foreach ($file_array as $file_data)
245 uploadFile($file_data);
247 uploadFile($_FILES['file']);
251 // Other file functions (delete, private).
253 if ($settings['allow_deletion'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'delete')
254 deleteFile($_POST['target']);
256 if ($settings['allow_private'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'privatetoggle')
257 markUnmarkHidden($_POST['target']);
260 // List files in a given directory, excluding certain files
261 function createArrayFromPath ($dir) {
264 $file_array = array();
266 while ($filename = readdir($dh)) {
267 $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
268 if (isReadableFile($fqfn) && !in_array($filename, $data['ignores']))
269 $file_array[filemtime($fqfn)] = $filename;
273 $file_array = array_reverse($file_array, true);
278 function removeOldFiles ($dir) {
279 global $file_array, $settings;
281 foreach ($file_array as $file) {
282 $fqfn = $dir . DIRECTORY_SEPARATOR . $file;
283 if ($settings['time_limit'] < time() - filemtime($fqfn))
288 // Only read files if the feature is enabled
289 if ($settings['listfiles']) {
290 $file_array = createArrayFromPath($data['uploaddir']);
292 // Removing old files
293 if ($settings['remove_old_files'])
294 removeOldFiles($data['uploaddir']);
296 $file_array = createArrayFromPath($data['uploaddir']);
299 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
300 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$settings['lang']?>" lang="<?=$settings['lang']?>" dir="<?=$settings['lang_dir']?>">
302 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
303 <meta http-equiv="content-style-type" content="text/css" />
304 <meta http-equiv="content-script-type" content="text/javascript" />
305 <meta http-equiv="language" content="de" />
307 <meta name="robots" content="noindex" />
308 <meta name="referrer" content="origin-when-crossorigin" />
309 <title><?=$settings['title']?></title>
310 <style type="text/css" media="screen">
315 font-family: sans-serif;
320 background: rgba(255, 255, 255, 0.05);
328 background: rgba(255, 255, 255, 0.075);
351 background: rgba(255, 255, 255, 0.1);
353 text-decoration: none;
358 body > ul > li > a:hover {
362 body > ul > li > a:active {
366 body > ul > li > a > span {
371 body > ul > li > form {
372 display: inline-block;
377 body > ul > li.owned {
381 body > ul > li > form > button {
383 display: inline-block;
387 background: rgba(255, 255, 255, 0.1);
391 body > ul > li > form > button:hover {
395 body > ul > li > form > button:active {
399 body > ul > li.uploading {
400 animation: upanim 2s linear 0s infinite alternate;
414 <h1><?=$settings['title']?></h1>
415 <form action="<?= $data['scriptname'] ?>" method="post" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
416 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
417 <input type="file" name="file[]" id="simpleupload-input" />
419 <?php if ($settings['listfiles']) { ?>
420 <ul id="simpleupload-ul">
422 foreach ($file_array as $mtime => $filename) {
423 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $filename;
424 $file_info = array();
426 $file_private = $filename[0] === '.';
428 if ($settings['listfiles_size'])
429 $file_info[] = formatSize(filesize($fqfn));
431 if ($settings['listfiles_size'])
432 $file_info[] = date($settings['listfiles_date_format'], $mtime);
434 if ($settings['allow_deletion'] || $settings['allow_private'])
435 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
438 $file_info = implode(', ', $file_info);
440 if (strlen($file_info) > 0)
441 $file_info = ' (' . $file_info . ')';
447 if (!$file_private || $file_owner) {
448 echo "<li class=\"' . $class . '\">";
450 $url = str_replace('/./', '/', sprintf('%s%s/%s', $settings['url'], $settings['uploaddir'], $filename));
452 echo "<a href=\"$url\" target=\"_blank\">$filename<span>$file_info</span></a>";
455 if ($settings['allow_deletion'])
456 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>';
458 if ($settings['allow_private'])
460 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>';
462 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>';
473 if ($settings['allow_external_refs']) {
475 <a href="https://github.com/muchweb/simple-php-upload" target="_blank"><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>
479 <a href="https://github.com/muchweb/simple-php-upload" target="_blank">Fork me on GitHub</a>
483 <script type="text/javascript">
485 var target_form = document.getElementById('simpleupload-form');
486 var target_ul = document.getElementById('simpleupload-ul');
487 var target_input = document.getElementById('simpleupload-input');
489 target_form.addEventListener('dragover', function (event) {
490 event.preventDefault();
493 function addFileLi (name, info) {
494 target_form.style.display = 'none';
496 var new_li = document.createElement('li');
497 new_li.className = 'uploading';
499 var new_a = document.createElement('a');
500 new_a.innerHTML = name;
501 new_li.appendChild(new_a);
503 var new_span = document.createElement('span');
504 new_span.innerHTML = info;
505 new_a.appendChild(new_span);
507 target_ul.insertBefore(new_li, target_ul.firstChild);
510 function handleFiles (event) {
511 event.preventDefault();
513 var files = event.dataTransfer.files;
515 var form = new FormData();
517 for (var i = 0; i < files.length; i++) {
518 form.append('file[]', files[i]);
519 addFileLi(files[i].name, files[i].size + ' bytes');
522 var xhr = new XMLHttpRequest();
523 xhr.onload = function() {
524 window.location.reload();
527 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
531 target_form.addEventListener('drop', handleFiles, false);
533 document.getElementById('simpleupload-input').onchange = function () {
534 addFileLi('Uploading...', '');
535 target_form.submit();