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 // Description for this website
26 // Base path (auto-detection)
27 'base_path' => dirname(__FILE__) . DIRECTORY_SEPARATOR,
29 // Directory to store uploaded files (relative to base_path)
32 // Display list uploaded files
35 // Allow users to delete files that they have uploaded (will enable sessions)
36 'allow_deletion' => true,
38 // Allow users to mark files as hidden
39 'allow_private' => true,
42 'listfiles_size' => true,
45 'listfiles_date' => true,
47 // Display file dates format
48 'listfiles_date_format' => 'F d Y H:i:s',
50 // Randomize file names (number of 'false')
51 'random_name_len' => 8,
53 // Keep filetype information (if random name is activated)
54 'random_name_keep_type' => true,
56 // Random file name letters
57 'random_name_alphabet' => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
59 // Add original name (but cleaned) to file name if random file name feature is enabled?
60 'suffix_original_name' => false,
62 // Display debugging information
65 // Complete URL to your directory (including tracing slash, leave for auto-detection)
66 'url' => detectServerUrl(),
68 // Amount of seconds that each file should be stored for (0 for no limit)
70 'time_limit' => 60 * 60 * 24 * 30,
72 // Files that will be ignored
73 'ignores' => array('.', '..', 'LICENSE', 'README.md'),
82 'remove_old_files' => true,
84 // Privacy: Allow external references (the "fork me" ribbon)
85 'allow_external_refs' => true,
87 // =============={ Configuration End }==============
89 // Is the local config file there?
90 if (isReadableFile('config-local.php')) {
92 include('config-local.php');
95 // Enabling error reporting
96 if ($settings['debug']) {
97 error_reporting(E_ALL);
98 ini_set('display_startup_errors',1);
99 ini_set('display_errors',1);
105 $data['scriptname'] = $settings['url'] . pathinfo(__FILE__, PATHINFO_BASENAME);
107 // Adding current script name to ignore list
108 $data['ignores'] = $settings['ignores'];
109 $data['ignores'][] = basename($data['scriptname']);
111 // Use canonized path
112 $data['uploaddir'] = realpath($settings['base_path'] . $settings['uploaddir']);
114 // Maximum upload size, set by system
115 $data['max_upload_size'] = ini_get('upload_max_filesize');
117 // If file deletion or private files are allowed, starting a session.
118 // This is required for user authentification
119 if ($settings['allow_deletion'] || $settings['allow_private']) {
123 if (!isset($_SESSION['upload_user_id']))
124 $_SESSION['upload_user_id'] = mt_rand(100000, 999999);
126 // List of filenames that were uploaded by this user
127 if (!isset($_SESSION['upload_user_files']))
128 $_SESSION['upload_user_files'] = array();
131 // If debug is enabled, logging all variables
132 if ($settings['debug']) {
133 // Displaying debug information
134 echo '<h2>Settings:</h2>';
139 // Displaying debug information
140 echo '<h2>Data:</h2>';
146 // Displaying debug information
147 echo '<h2>SESSION:</h2>';
154 function formatSize ($bytes) {
155 $units = array('B', 'KB', 'MB', 'GB', 'TB');
157 $bytes = max($bytes, 0);
158 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
159 $pow = min($pow, count($units) - 1);
161 $bytes /= pow(1024, $pow);
163 return ceil($bytes) . ' ' . $units[$pow];
166 // Rotating a two-dimensional array
167 function diverseArray ($vector) {
169 foreach ($vector as $key1 => $value1)
170 foreach ($value1 as $key2 => $value2)
171 $result[$key2][$key1] = $value2;
175 // Handling file upload
176 function uploadFile ($file_data) {
177 global $settings, $data;
179 $file_data['uploaded_file_name'] = basename($file_data['name']);
180 $file_data['target_file_name'] = $file_data['uploaded_file_name'];
182 // Generating random file name
183 if ($settings['random_name_len'] !== false) {
185 $file_data['target_file_name'] = '';
186 while (strlen($file_data['target_file_name']) < $settings['random_name_len'])
187 $file_data['target_file_name'] .= $settings['random_name_alphabet'][mt_rand(0, strlen($settings['random_name_alphabet']) - 1)];
188 if ($settings['suffix_original_name'])
189 $file_data['target_file_name'] .= '_' . preg_replace('/[^0-9a-zA-Z_-]/', '', pathinfo($file_data['uploaded_file_name'], PATHINFO_BASENAME));
190 if ($settings['random_name_keep_type'])
191 $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
192 } while (isReadableFile($file_data['target_file_name']));
194 $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file_data['target_file_name'];
196 // Do now allow to overwriting files
197 if (isReadableFile($file_data['upload_target_file'])) {
198 echo 'File name already exists' . "\n";
202 // Moving uploaded file OK
203 if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) {
204 if ($settings['listfiles'] && ($settings['allow_deletion'] || $settings['allow_private']))
205 $_SESSION['upload_user_files'][] = $file_data['target_file_name'];
206 echo $settings['url'] . $file_data['target_file_name'] . "\n";
208 // Return target file name for later handling
209 return $file_data['upload_target_file'];
211 echo 'Error: unable to upload the file.';
217 function deleteFile ($file) {
220 if (in_array(substr($file, 1), $_SESSION['upload_user_files']) || in_array($file, $_SESSION['upload_user_files'])) {
221 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
222 if (!in_array($file, $data['ignores']) && isReadableFile($fqfn)) {
224 echo 'File has been removed';
230 // Mark/unmark file as hidden
231 function markUnmarkHidden ($file) {
234 if (in_array(substr($file, 1), $_SESSION['upload_user_files']) || in_array($file, $_SESSION['upload_user_files'])) {
235 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
236 if (!in_array($file, $data['ignores']) && isReadableFile($fqfn)) {
237 if (substr($file, 0, 1) === '.') {
238 rename($fqfn, substr($fqfn, 1));
239 echo 'File has been made visible';
241 rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file);
242 echo 'File has been hidden';
249 // Checks if the given file is a file and is readable
250 function isReadableFile ($file) {
251 return (is_file($file) && is_readable($file));
254 // Detects full URL of installation
255 function detectServerUrl () {
257 if (!isset($GLOBALS[__FUNCTION__])) {
258 // Default protocol is HTTP
262 if (((isset($_SERVER['HTTPS'])) && (strtolower($_SERVER['HTTPS']) == 'on')) || ((isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'))) {
267 // Construct full URL
268 $GLOBALS[__FUNCTION__] = str_replace("\\", '', sprintf('%s://%s%s/', $protocol, $_SERVER['SERVER_NAME'], dirname($_SERVER['SCRIPT_NAME'])));
271 // Return cached value
272 return $GLOBALS[__FUNCTION__];
275 // Files are being POSEed. Uploading them one by one.
276 if (isset($_FILES['file'])) {
277 header('Content-type: text/plain');
278 if (is_array($_FILES['file'])) {
279 $file_array = diverseArray($_FILES['file']);
280 foreach ($file_array as $file_data) {
281 $targetFile = uploadFile($file_data);
284 $targetFile = uploadFile($_FILES['file']);
289 // Other file functions (delete, private).
291 if ($settings['allow_deletion'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'delete')
292 deleteFile($_POST['target']);
294 if ($settings['allow_private'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'privatetoggle')
295 markUnmarkHidden($_POST['target']);
298 // List files in a given directory, excluding certain files
299 function createArrayFromPath ($dir) {
302 $file_array = array();
306 while ($filename = readdir($dh)) {
307 $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
308 if (isReadableFile($fqfn) && !in_array($filename, $data['ignores']))
309 $file_array[filemtime($fqfn)] = $filename;
314 $file_array = array_reverse($file_array, true);
320 function removeOldFiles ($dir) {
321 global $file_array, $settings;
323 foreach ($file_array as $file) {
324 $fqfn = $dir . DIRECTORY_SEPARATOR . $file;
325 if ($settings['time_limit'] < time() - filemtime($fqfn))
330 // Only read files if the feature is enabled
331 if ($settings['listfiles']) {
332 $file_array = createArrayFromPath($data['uploaddir']);
334 // Removing old files
335 if ($settings['remove_old_files'])
336 removeOldFiles($data['uploaddir']);
338 $file_array = createArrayFromPath($data['uploaddir']);
341 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
342 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$settings['lang']?>" lang="<?=$settings['lang']?>" dir="<?=$settings['lang_dir']?>">
344 <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
345 <meta http-equiv="content-style-type" content="text/css" />
346 <meta http-equiv="content-script-type" content="text/javascript" />
347 <meta http-equiv="language" content="de" />
349 <meta name="robots" content="noindex" />
350 <meta name="referrer" content="origin-when-crossorigin" />
351 <title><?=$settings['title']?></title>
352 <style type="text/css" media="screen">
357 font-family: sans-serif;
362 background: rgba(255, 255, 255, 0.05);
370 background: rgba(255, 255, 255, 0.075);
393 background: rgba(255, 255, 255, 0.1);
395 text-decoration: none;
400 body > ul > li > a:hover {
404 body > ul > li > a:active {
408 body > ul > li > a > span {
413 body > ul > li > form {
414 display: inline-block;
419 body > ul > li.owned {
423 body > ul > li > form > button {
425 display: inline-block;
429 background: rgba(255, 255, 255, 0.1);
433 body > ul > li > form > button:hover {
437 body > ul > li > form > button:active {
441 body > ul > li.uploading {
442 animation: upanim 2s linear 0s infinite alternate;
456 <h1><?=$settings['title']?></h1>
457 <p><?=$settings['description']?></p>
458 <form action="<?= $data['scriptname'] ?>" method="post" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
459 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
460 <input type="file" name="file[]" id="simpleupload-input" />
462 <?php if ($settings['listfiles']) { ?>
463 <ul id="simpleupload-ul">
465 foreach ($file_array as $mtime => $filename) {
466 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $filename;
467 $file_info = array();
469 $file_private = $filename[0] === '.';
471 if ($settings['listfiles_size'])
472 $file_info[] = formatSize(filesize($fqfn));
474 if ($settings['listfiles_size'])
475 $file_info[] = date($settings['listfiles_date_format'], $mtime);
477 if ($settings['allow_deletion'] || $settings['allow_private'])
478 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
481 $file_info = implode(', ', $file_info);
483 if (strlen($file_info) > 0)
484 $file_info = ' (' . $file_info . ')';
490 if (!$file_private || $file_owner) {
491 echo "<li class=\"' . $class . '\">";
493 // Create full-qualified URL and clean it a bit
494 $url = str_replace('/./', '/', sprintf('%s%s/%s', $settings['url'], $settings['uploaddir'], $filename));
496 echo "<a href=\"$url\" target=\"_blank\">$filename<span>$file_info</span></a>";
499 if ($settings['allow_deletion'])
500 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>';
502 if ($settings['allow_private'])
504 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>';
506 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>';
517 if ($settings['allow_external_refs']) {
519 <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>
523 <a href="https://github.com/muchweb/simple-php-upload" target="_blank">Fork me on GitHub</a>
527 <script type="text/javascript">
529 // Init some variales to shorten code
530 var target_form = document.getElementById('simpleupload-form');
531 var target_ul = document.getElementById('simpleupload-ul');
532 var target_input = document.getElementById('simpleupload-input');
533 var settings_listfiles = <?=($settings['listfiles'] ? 'true' : 'false')?>;
536 * Initializes the upload form
539 // Register drag-over event listener
540 target_form.addEventListener('dragover', function (event) {
541 event.preventDefault();
544 // ... and the drop event listener
545 target_form.addEventListener('drop', handleFiles, false);
547 // Register onchange-event function
548 target_input.onchange = function () {
549 addFileLi('Uploading...', '');
550 target_form.submit();
555 * Adds given file in a new li-tag to target_ul list
557 * @param name Name of the file
558 * @param info Some more informations
560 function addFileLi (name, info) {
561 if (settings_listfiles == false) {
565 target_form.style.display = 'none';
567 var new_li = document.createElement('li');
568 new_li.className = 'uploading';
570 var new_a = document.createElement('a');
571 new_a.innerHTML = name;
572 new_li.appendChild(new_a);
574 var new_span = document.createElement('span');
575 new_span.innerHTML = info;
576 new_a.appendChild(new_span);
578 target_ul.insertBefore(new_li, target_ul.firstChild);
582 * Handles given event for file upload
584 * @param event Event to handle file upload for
586 function handleFiles (event) {
587 event.preventDefault();
589 var files = event.dataTransfer.files;
591 var form = new FormData();
593 for (var i = 0; i < files.length; i++) {
594 form.append('file[]', files[i]);
595 addFileLi(files[i].name, files[i].size + ' bytes');
598 var xhr = new XMLHttpRequest();
599 xhr.onload = function() {
600 window.location.reload();
603 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
607 // Initialize upload form