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 // =============={ Configuration End }==============
77 // Is the local config file there?
78 if (file_exists('config-local.php')) {
80 include('config-local.php');
83 // Enabling error reporting
84 if ($settings['debug']) {
85 error_reporting(E_ALL);
86 ini_set('display_startup_errors',1);
87 ini_set('display_errors',1);
93 $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
95 // Adding current script name to ignore list
96 $data['ignores'] = $settings['ignores'];
97 $data['ignores'][] = $data['scriptname'];
100 $data['uploaddir'] = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . $settings['uploaddir']);
102 // Maximum upload size, set by system
103 $data['max_upload_size'] = ini_get('upload_max_filesize');
105 // If file deletion or private files are allowed, starting a session.
106 // This is required for user authentification
107 if ($settings['allow_deletion'] || $settings['allow_private']) {
111 if (!isset($_SESSION['upload_user_id']))
112 $_SESSION['upload_user_id'] = mt_rand(100000, 999999);
114 // List of filenames that were uploaded by this user
115 if (!isset($_SESSION['upload_user_files']))
116 $_SESSION['upload_user_files'] = array();
119 // If debug is enabled, logging all variables
120 if ($settings['debug']) {
121 // Displaying debug information
122 echo '<h2>Settings:</h2>';
127 // Displaying debug information
128 echo '<h2>Data:</h2>';
134 // Displaying debug information
135 echo '<h2>SESSION:</h2>';
142 function formatSize ($bytes) {
143 $units = array('B', 'KB', 'MB', 'GB', 'TB');
145 $bytes = max($bytes, 0);
146 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
147 $pow = min($pow, count($units) - 1);
149 $bytes /= pow(1024, $pow);
151 return ceil($bytes) . ' ' . $units[$pow];
154 // Rotating a two-dimensional array
155 function diverseArray ($vector) {
157 foreach ($vector as $key1 => $value1)
158 foreach ($value1 as $key2 => $value2)
159 $result[$key2][$key1] = $value2;
163 // Handling file upload
164 function uploadFile ($file_data) {
169 $file_data['uploaded_file_name'] = basename($file_data['name']);
170 $file_data['target_file_name'] = $file_data['uploaded_file_name'];
172 // Generating random file name
173 if ($settings['random_name_len'] !== false) {
175 $file_data['target_file_name'] = '';
176 while (strlen($file_data['target_file_name']) < $settings['random_name_len'])
177 $file_data['target_file_name'] .= $settings['random_name_alphabet'][mt_rand(0, strlen($settings['random_name_alphabet']) - 1)];
178 if ($settings['random_name_keep_type'])
179 $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
180 } while (file_exists($file_data['target_file_name']));
182 $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file_data['target_file_name'];
184 // Do now allow to overwriting files
185 if (file_exists($file_data['upload_target_file'])) {
186 echo 'File name already exists' . "\n";
190 // Moving uploaded file OK
191 if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) {
192 if ($settings['allow_deletion'] || $settings['allow_private'])
193 $_SESSION['upload_user_files'][] = $file_data['target_file_name'];
194 echo $settings['url'] . $file_data['target_file_name'] . "\n";
196 echo 'Error: unable to upload the file.';
201 // Files are being POSEed. Uploading them one by one.
202 if (isset($_FILES['file'])) {
203 header('Content-type: text/plain');
204 if (is_array($_FILES['file'])) {
205 $file_array = diverseArray($_FILES['file']);
206 foreach ($file_array as $file_data)
207 uploadFile($file_data);
209 uploadFile($_FILES['file']);
213 // Other file functions (delete, private).
215 if ($settings['allow_deletion'])
216 if (isset($_POST['action']) && $_POST['action'] === 'delete')
217 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files'])) {
218 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $_POST['target'];
219 if (file_exists($fqfn)) {
221 echo 'File has been removed';
226 if ($settings['allow_private'])
227 if (isset($_POST['action']) && $_POST['action'] === 'privatetoggle')
228 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files'])) {
229 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $_POST['target'];
230 if (file_exists($fqfn)) {
231 if ($_POST['target'][0] === '.') {
232 rename($fqfn, substr($fqfn, 1));
233 echo 'File has been made visible';
235 rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $_POST['target']);
236 echo 'File has been hidden';
243 // List files in a given directory, excluding certain files
244 function listFiles ($dir, $exclude) {
245 $file_array = array();
247 while (false !== ($filename = readdir($dh))) {
248 $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
249 if (is_file($fqfn) && !in_array($filename, $exclude))
250 $file_array[filemtime($fqfn)] = $filename;
254 $file_array = array_reverse($file_array, true);
259 function removeOldFiles ($dir) {
260 global $file_array, $settings;
261 foreach ($file_array as $file) {
262 $fqfn = $dir . DIRECTORY_SEPARATOR . $file;
263 if ($settings['time_limit'] < time() - filemtime($fqfn))
268 // Only read files if the feature is enabled
269 if ($settings['listfiles']) {
270 $file_array = listFiles($data['uploaddir'], $data['ignores']);
272 // Removing old files
273 if ($settings['remove_old_files'])
274 removeOldFiles($data['uploaddir']);
276 $file_array = listFiles($data['uploaddir'], $data['ignores']);
280 <html lang="<?=$settings['lang']?>" dir="<?=$settings['lang_dir']?>">
282 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
283 <meta http-equiv="Content-Script-Type" content="text/javascript">
284 <meta name="robots" content="noindex">
285 <meta name="referrer" content="origin-when-crossorigin">
286 <title><?=$settings['title']?></title>
287 <style media="screen">
293 font-family: sans-serif;
298 background: rgba(255, 255, 255, 0.05);
306 background: rgba(255, 255, 255, 0.075);
329 background: rgba(255, 255, 255, 0.1);
331 text-decoration: none;
336 body > ul > li > a:hover {
340 body > ul > li > a:active {
344 body > ul > li > a > span {
349 body > ul > li > form {
350 display: inline-block;
355 body > ul > li.owned {
359 body > ul > li > form > button {
361 display: inline-block;
365 background: rgba(255, 255, 255, 0.1);
369 body > ul > li > form > button:hover {
373 body > ul > li > form > button:active {
377 body > ul > li.uploading {
378 animation: upanim 2s linear 0s infinite alternate;
393 <h1><?=$settings['title']?></h1>
394 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
395 Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
396 <input type="file" name="file[]" multiple required id="simpleupload-input"/>
398 <?php if ($settings['listfiles']) { ?>
399 <ul id="simpleupload-ul">
401 foreach ($file_array as $mtime => $filename) {
402 $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $filename;
403 $file_info = array();
405 $file_private = $filename[0] === '.';
407 if ($settings['listfiles_size'])
408 $file_info[] = formatSize(filesize($fqfn));
410 if ($settings['listfiles_size'])
411 $file_info[] = date($settings['listfiles_date_format'], $mtime);
413 if ($settings['allow_deletion'] || $settings['allow_private'])
414 if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
417 $file_info = implode(', ', $file_info);
419 if (strlen($file_info) > 0)
420 $file_info = ' (' . $file_info . ')';
426 if (!$file_private || $file_owner) {
427 echo "<li class=\"' . $class . '\">";
429 $url = str_replace('/./', '/', sprintf('%s%s/%s', $settings['url'], $settings['uploaddir'], $filename));
431 echo "<a href=\"$url\" target=\"_blank\">$filename<span>$file_info</span></a>";
434 if ($settings['allow_deletion'])
435 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>';
437 if ($settings['allow_private'])
439 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>';
441 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>';
450 <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>
451 <script type="text/javascript">
453 var target_form = document.getElementById('simpleupload-form'),
454 target_ul = document.getElementById('simpleupload-ul'),
455 target_input = document.getElementById('simpleupload-input');
457 target_form.addEventListener('dragover', function (event) {
458 event.preventDefault();
461 function AddFileLi (name, info) {
462 target_form.style.display = 'none';
464 var new_li = document.createElement('li');
465 new_li.className = 'uploading';
467 var new_a = document.createElement('a');
468 new_a.innerHTML = name;
469 new_li.appendChild(new_a);
471 var new_span = document.createElement('span');
472 new_span.innerHTML = info;
473 new_a.appendChild(new_span);
475 target_ul.insertBefore(new_li, target_ul.firstChild);
478 function HandleFiles (event) {
479 event.preventDefault();
482 files = event.dataTransfer.files,
485 var form = new FormData();
487 for (; i < len; i++) {
488 form.append('file[]', files[i]);
489 AddFileLi(files[i].name, files[i].size + ' bytes');
492 var xhr = new XMLHttpRequest();
493 xhr.onload = function() {
494 window.location.reload();
497 xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
501 target_form.addEventListener('drop', HandleFiles, false);
503 document.getElementById('simpleupload-input').onchange = function () {
504 AddFileLi('Uploading...', '');
505 target_form.submit();