// Website title
'title' => 'strace.club',
- // Directory to store uploaded files
+ // Description for this website
+ 'description' => '',
+
+ // Base path (auto-detection)
+ 'base_path' => dirname(__FILE__) . DIRECTORY_SEPARATOR,
+
+ // Directory to store uploaded files (relative to base_path)
'uploaddir' => '.',
// Display list uploaded files
// Random file name letters
'random_name_alphabet' => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
+ // Add original name (but cleaned) to file name if random file name feature is enabled?
+ 'suffix_original_name' => false,
+
// Display debugging information
'debug' => false,
- // Complete URL to your directory (including tracing slash)
- 'url' => 'http://strace.club/',
+ // Complete URL to your directory (including tracing slash, leave for auto-detection)
+ 'url' => detectServerUrl(),
// Amount of seconds that each file should be stored for (0 for no limit)
// Default 30 days
$data = array();
// Name of this file
- $data['scriptname'] = $settings['url'] . '/' . pathinfo(__FILE__, PATHINFO_BASENAME);
+ $data['scriptname'] = $settings['url'] . pathinfo(__FILE__, PATHINFO_BASENAME);
// Adding current script name to ignore list
$data['ignores'] = $settings['ignores'];
$data['ignores'][] = basename($data['scriptname']);
// Use canonized path
- $data['uploaddir'] = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . $settings['uploaddir']);
+ $data['uploaddir'] = realpath($settings['base_path'] . $settings['uploaddir']);
// Maximum upload size, set by system
$data['max_upload_size'] = ini_get('upload_max_filesize');
$file_data['target_file_name'] = '';
while (strlen($file_data['target_file_name']) < $settings['random_name_len'])
$file_data['target_file_name'] .= $settings['random_name_alphabet'][mt_rand(0, strlen($settings['random_name_alphabet']) - 1)];
+ if ($settings['suffix_original_name'])
+ $file_data['target_file_name'] .= '_' . preg_replace('/[^0-9a-zA-Z_-]/', '', pathinfo($file_data['uploaded_file_name'], PATHINFO_BASENAME));
if ($settings['random_name_keep_type'])
$file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
} while (isReadableFile($file_data['target_file_name']));
// Do now allow to overwriting files
if (isReadableFile($file_data['upload_target_file'])) {
echo 'File name already exists' . "\n";
- return;
+ return false;
}
// Moving uploaded file OK
if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) {
- if ($settings['allow_deletion'] || $settings['allow_private'])
+ if ($settings['listfiles'] && ($settings['allow_deletion'] || $settings['allow_private']))
$_SESSION['upload_user_files'][] = $file_data['target_file_name'];
echo $settings['url'] . $file_data['target_file_name'] . "\n";
+
+ // Return target file name for later handling
+ return $file_data['upload_target_file'];
} else {
echo 'Error: unable to upload the file.';
+ return false;
}
}
global $data;
if (in_array(substr($file, 1), $_SESSION['upload_user_files']) || in_array($file, $_SESSION['upload_user_files'])) {
- $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
- if (!in_array($file, $data['ignores']) && isReadableFile($fqfn)) {
+ $sourceFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
+ if (!in_array($file, $data['ignores']) && isReadableFile($sourceFile)) {
if (substr($file, 0, 1) === '.') {
- rename($fqfn, substr($fqfn, 1));
- echo 'File has been made visible';
+ $targetFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . substr($file, 1);
+ $successMessage = 'File has been made public';
+ $failedMessage = 'File has NOT been made public';
} else {
- rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file);
- echo 'File has been hidden';
+ $targetFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file;
+ $successMessage = 'File has been hidden';
+ $failedMessage = 'File has NOT been hidden';
+ }
+ if (rename($sourceFile, $targetFile)) {
+ echo $successMessage;
+ } else {
+ echo $failedMessage;
}
exit;
}
return (is_file($file) && is_readable($file));
}
+ // Detects full URL of installation
+ function detectServerUrl () {
+ // Is "cache" there?
+ if (!isset($GLOBALS[__FUNCTION__])) {
+ // Default protocol is HTTP
+ $protocol = 'http';
+
+ // Is SSL given?
+ if (((isset($_SERVER['HTTPS'])) && (strtolower($_SERVER['HTTPS']) == 'on')) || ((isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https'))) {
+ // Protocol is HTTPS
+ $protocol = 'https';
+ } // END - if
+
+ // Construct full URL
+ $GLOBALS[__FUNCTION__] = str_replace("\\", '', sprintf('%s://%s%s/', $protocol, $_SERVER['SERVER_NAME'], dirname($_SERVER['SCRIPT_NAME'])));
+ } // END - if
+
+ // Return cached value
+ return $GLOBALS[__FUNCTION__];
+ }
+
// Files are being POSEed. Uploading them one by one.
if (isset($_FILES['file'])) {
header('Content-type: text/plain');
if (is_array($_FILES['file'])) {
$file_array = diverseArray($_FILES['file']);
- foreach ($file_array as $file_data)
- uploadFile($file_data);
- } else
- uploadFile($_FILES['file']);
+ foreach ($file_array as $file_data) {
+ $targetFile = uploadFile($file_data);
+ }
+ } else {
+ $targetFile = uploadFile($_FILES['file']);
+ }
exit;
}
global $data;
$file_array = array();
+
$dh = opendir($dir);
- while ($filename = readdir($dh)) {
- $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
- if (isReadableFile($fqfn) && !in_array($filename, $data['ignores']))
- $file_array[filemtime($fqfn)] = $filename;
- }
+
+ while ($filename = readdir($dh)) {
+ $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
+ if (isReadableFile($fqfn) && !in_array($filename, $data['ignores']))
+ $file_array[filemtime($fqfn)] = $filename;
+ }
ksort($file_array);
+
$file_array = array_reverse($file_array, true);
+
return $file_array;
}
</head>
<body>
<h1><?=$settings['title']?></h1>
+ <p><?=$settings['description']?></p>
<form action="<?= $data['scriptname'] ?>" method="post" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
<input type="file" name="file[]" id="simpleupload-input" />
if (!$file_private || $file_owner) {
echo "<li class=\"' . $class . '\">";
+ // Create full-qualified URL and clean it a bit
$url = str_replace('/./', '/', sprintf('%s%s/%s', $settings['url'], $settings['uploaddir'], $filename));
echo "<a href=\"$url\" target=\"_blank\">$filename<span>$file_info</span></a>";
?>
<script type="text/javascript">
<!--
- var target_form = document.getElementById('simpleupload-form');
- var target_ul = document.getElementById('simpleupload-ul');
- var target_input = document.getElementById('simpleupload-input');
-
- target_form.addEventListener('dragover', function (event) {
- event.preventDefault();
- }, false);
+ // Init some variales to shorten code
+ var target_form = document.getElementById('simpleupload-form');
+ var target_ul = document.getElementById('simpleupload-ul');
+ var target_input = document.getElementById('simpleupload-input');
+ var settings_listfiles = <?=($settings['listfiles'] ? 'true' : 'false')?>;
+
+ /**
+ * Initializes the upload form
+ */
+ function init () {
+ // Register drag-over event listener
+ target_form.addEventListener('dragover', function (event) {
+ event.preventDefault();
+ }, false);
+
+ // ... and the drop event listener
+ target_form.addEventListener('drop', handleFiles, false);
+
+ // Register onchange-event function
+ target_input.onchange = function () {
+ addFileLi('Uploading...', '');
+ target_form.submit();
+ };
+ }
+ /**
+ * Adds given file in a new li-tag to target_ul list
+ *
+ * @param name Name of the file
+ * @param info Some more informations
+ */
function addFileLi (name, info) {
+ if (settings_listfiles == false) {
+ return;
+ }
+
target_form.style.display = 'none';
var new_li = document.createElement('li');
target_ul.insertBefore(new_li, target_ul.firstChild);
}
+ /**
+ * Handles given event for file upload
+ *
+ * @param event Event to handle file upload for
+ */
function handleFiles (event) {
event.preventDefault();
xhr.send(form);
}
- target_form.addEventListener('drop', handleFiles, false);
+ // Initialize upload form
+ init();
- document.getElementById('simpleupload-input').onchange = function () {
- addFileLi('Uploading...', '');
- target_form.submit();
- };
//-->
</script>
</body>