X-Git-Url: https://git.mxchange.org/?p=simple-upload.git;a=blobdiff_plain;f=index.php;h=5ba2e262ee543659e0bd328a63ef6d7984d52a6b;hp=4f33d3e7cbef5948f7b3ea2edfedc058d010c87f;hb=HEAD;hpb=bcb76e71554cbf5ae4d9982300b9e79c0cd563fe diff --git a/index.php b/index.php index 4f33d3e..5ba2e26 100644 --- a/index.php +++ b/index.php @@ -20,7 +20,13 @@ // 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 @@ -50,11 +56,14 @@ // 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 @@ -62,9 +71,27 @@ // Files that will be ignored 'ignores' => array('.', '..', 'LICENSE', 'README.md'), + + // Language code + 'lang' => 'en', + + // Language direction + 'lang_dir' => 'ltr', + + // Remove old files? + 'remove_old_files' => true, + + // Privacy: Allow external references (the "fork me" ribbon) + 'allow_external_refs' => true, ); // =============={ Configuration End }============== + // Is the local config file there? + if (isReadableFile('config-local.php')) { + // Load it then + include('config-local.php'); + } + // Enabling error reporting if ($settings['debug']) { error_reporting(E_ALL); @@ -75,14 +102,14 @@ $data = array(); // Name of this file - $data['scriptname'] = 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'][] = $data['scriptname']; + $data['ignores'][] = basename($data['scriptname']); // Use canonized path - $data['uploaddir'] = realpath($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'); @@ -94,7 +121,7 @@ // 'User ID' if (!isset($_SESSION['upload_user_id'])) - $_SESSION['upload_user_id'] = rand(100000, 999999); + $_SESSION['upload_user_id'] = mt_rand(100000, 999999); // List of filenames that were uploaded by this user if (!isset($_SESSION['upload_user_files'])) @@ -124,7 +151,7 @@ } // Format file size - function FormatSize ($bytes) { + function formatSize ($bytes) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); @@ -137,7 +164,7 @@ } // Rotating a two-dimensional array - function DiverseArray ($vector) { + function diverseArray ($vector) { $result = array(); foreach ($vector as $key1 => $value1) foreach ($value1 as $key2 => $value2) @@ -146,10 +173,8 @@ } // Handling file upload - function UploadFile ($file_data) { - global $settings; - global $data; - global $_SESSION; + function uploadFile ($file_data) { + global $settings, $data; $file_data['uploaded_file_name'] = basename($file_data['name']); $file_data['target_file_name'] = $file_data['uploaded_file_name']; @@ -159,95 +184,179 @@ do { $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'][rand(0, strlen($settings['random_name_alphabet']) - 1)]; + $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 (file_exists($file_data['target_file_name'])); + } while (isReadableFile($file_data['target_file_name'])); } $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file_data['target_file_name']; // Do now allow to overwriting files - if (file_exists($file_data['upload_target_file'])) { + 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; } } + // Delete file + function deleteFile ($file) { + 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)) { + unlink($fqfn); + echo 'File has been removed'; + exit; + } + } + } + + // Mark/unmark file as hidden + function markUnmarkHidden ($file) { + global $data; + + if (in_array(substr($file, 1), $_SESSION['upload_user_files']) || in_array($file, $_SESSION['upload_user_files'])) { + $sourceFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file; + if (!in_array($file, $data['ignores']) && isReadableFile($sourceFile)) { + if (substr($file, 0, 1) === '.') { + $targetFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . substr($file, 1); + $successMessage = 'File has been made public'; + $failedMessage = 'File has NOT been made public'; + } else { + $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; + } + } + } + + // Checks if the given file is a file and is readable + function isReadableFile ($file) { + 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']); + $file_array = diverseArray($_FILES['file']); + foreach ($file_array as $file_data) { + $targetFile = uploadFile($file_data); + } + } else { + $targetFile = uploadFile($_FILES['file']); + } exit; } // Other file functions (delete, private). if (isset($_POST)) { - if ($settings['allow_deletion']) - if (isset($_POST['action']) && $_POST['action'] === 'delete') - if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files'])) - if (file_exists($_POST['target'])) { - unlink($_POST['target']); - echo 'File has been removed'; - exit; - } + if ($settings['allow_deletion'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'delete') + deleteFile($_POST['target']); - if ($settings['allow_private']) - if (isset($_POST['action']) && $_POST['action'] === 'privatetoggle') - if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files'])) - if (file_exists($_POST['target'])) { - if ($_POST['target'][0] === '.') { - rename($_POST['target'], substr($_POST['target'], 1)); - echo 'File has been made visible'; - } else { - rename($_POST['target'], '.' . $_POST['target']); - echo 'File has been hidden'; - } - exit; - } + if ($settings['allow_private'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'privatetoggle') + markUnmarkHidden($_POST['target']); } // List files in a given directory, excluding certain files - function ListFiles ($dir, $exclude) { + function createArrayFromPath ($dir) { + global $data; + $file_array = array(); + $dh = opendir($dir); - while (false !== ($filename = readdir($dh))) - if (is_file($filename) && !in_array($filename, $exclude)) - $file_array[filemtime($filename)] = $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; } - $file_array = ListFiles($settings['uploaddir'], $data['ignores']); + // Removes old files + function removeOldFiles ($dir) { + global $file_array, $settings; + + foreach ($file_array as $file) { + $fqfn = $dir . DIRECTORY_SEPARATOR . $file; + if ($settings['time_limit'] < time() - filemtime($fqfn)) + unlink($fqfn); + } + } - // Removing old files - foreach ($file_array as $file) - if ($settings['time_limit'] < time() - filemtime($file)) - unlink($file); + // Only read files if the feature is enabled + if ($settings['listfiles']) { + $file_array = createArrayFromPath($data['uploaddir']); - $file_array = ListFiles($settings['uploaddir'], $data['ignores']); + // Removing old files + if ($settings['remove_old_files']) + removeOldFiles($data['uploaddir']); + $file_array = createArrayFromPath($data['uploaddir']); + } ?> - + + - + + + + + + + <?=$settings['title']?> -