X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=index.php;h=883405c9a6da38d97e69066d9ed517e2787fe01a;hb=a7a3f3ce5e4ef8a50bb0829f7a000f9af867b322;hp=dd3d9dcf4debd3a9647a39ebd5c18a58e3c92783;hpb=92ddf9065db936332a8b73008322704546d13228;p=simple-upload.git
diff --git a/index.php b/index.php
index dd3d9dc..883405c 100644
--- a/index.php
+++ b/index.php
@@ -14,96 +14,141 @@
along with this program. If not, see
'; print_r($settings); echo ''; // Displaying debug information - echo '
'; print_r($data); echo ''; echo ''; // Displaying debug information - echo '
'; print_r($_SESSION); echo ''; } - function FormatSize ($bytes) { + // Format file size + function formatSize ($bytes) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); @@ -115,97 +160,191 @@ return ceil($bytes) . ' ' . $units[$pow]; } - function DiverseArray ($vector) { + // Rotating a two-dimensional array + function diverseArray ($vector) { $result = array(); - foreach($vector as $key1 => $value1) - foreach($value1 as $key2 => $value2) + foreach ($vector as $key1 => $value1) + foreach ($value1 as $key2 => $value2) $result[$key2][$key1] = $value2; return $result; } - function UploadFile ($file_data) { - global $settings; - global $data; - global $_SESSION; + // Handling file upload + function uploadFile ($file_data) { + global $settings, $data; - $data['uploaded_file_name'] = basename($file_data['name']); - $data['target_file_name'] = $file_data['uploaded_file_name']; + $file_data['uploaded_file_name'] = basename($file_data['name']); + $file_data['target_file_name'] = $file_data['uploaded_file_name']; + + // Generating random file name if ($settings['random_name_len'] !== false) { do { - $data['target_file_name'] = ''; - while (strlen($data['target_file_name']) < $settings['random_name_len']) - $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)]; + $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['random_name_keep_type']) - $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION); - } while (file_exists($data['target_file_name'])); + $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION); + } while (isReadableFile($file_data['target_file_name'])); } - $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name']; - $data['tmp_name'] = $file_data['tmp_name']; + $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file_data['target_file_name']; - if (file_exists($data['upload_target_file'])) { + // Do now allow to overwriting files + if (isReadableFile($file_data['upload_target_file'])) { echo 'File name already exists' . "\n"; - return; + return false; } - if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) { - if ($settings['allow_deletion']) - $_SESSION['upload_user_files'][] = $data['target_file_name']; - echo $settings['url'] . $data['target_file_name'] . "\n"; + // Moving uploaded file OK + if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) { + 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; } } - if (isset($_FILES['file'])) { - if ($settings['debug']) { - // Displaying debug information - echo '
'; - print_r($data); - echo ''; - // Displaying debug information - echo '
'; - print_r($_FILES); - echo ''; + // 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'])) { + $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file; + if (!in_array($file, $data['ignores']) && isReadableFile($fqfn)) { + if (substr($file, 0, 1) === '.') { + rename($fqfn, substr($fqfn, 1)); + echo 'File has been made visible'; + } else { + rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file); + echo 'File has been hidden'; + } + 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; } - if ($settings['allow_deletion']) - if (isset($_POST)) - if ($_POST['action'] === 'delete') - if (in_array($_POST['target'], $_SESSION['upload_user_files'])) - if (file_exists($_POST['target'])) { - unlink($_POST['target']); - echo 'File has been removed'; - exit; - } + // Other file functions (delete, private). + if (isset($_POST)) { + if ($settings['allow_deletion'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'delete') + deleteFile($_POST['target']); + + 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 createArrayFromPath ($dir) { + global $data; - function ListFiles ($dir, $exclude) { $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; } + // 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); + } + } + + // Only read files if the feature is enabled + if ($settings['listfiles']) { + $file_array = createArrayFromPath($data['uploaddir']); + + // Removing old files + if ($settings['remove_old_files']) + removeOldFiles($data['uploaddir']); + + $file_array = createArrayFromPath($data['uploaddir']); + } ?> - + + - -
Uploaded files:
-