X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=index.php;h=a75b12c07f1e663ac176f79277e1b19f7b4c8910;hb=4a6652e1e5caeba91057fa16be82ac2d06418347;hp=133bfcfc740c2171276561758a2bba10ecd1f832;hpb=c4d7b5750205935cee573fbf7f497d8c8fd7156d;p=simple-upload.git diff --git a/index.php b/index.php index 133bfcf..a75b12c 100644 --- a/index.php +++ b/index.php @@ -1,217 +1,484 @@ . + */ - // Relative path to this file (don't edit) - $settings['scriptpath'] = $_SERVER['PHP_SELF']; + // =============={ Configuration Begin }============== + $settings = array( - // Name of this file (don't edit) - $settings['scriptname'] = pathinfo(__FILE__, PATHINFO_FILENAME) . '.php'; + // Website title + 'title' => 'strace.club', - if (isset($_FILES['file']) && strlen($_FILES['file']['name']) > 1) { - $upload_file_name = basename($_FILES['file']['name']); - $uploadpath = $settings['uploaddir'] . DIRECTORY_SEPARATOR . $upload_file_name; - $page_url = $_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/'); + // Directory to store uploaded files + 'uploaddir' => '.', - if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadpath)) { - echo 'http://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['REQUEST_URI']), '\\/') . '/' . $uploadpath; - exit; - // echo 'File: ' . $upload_file_name . ' successfully uploaded:
'; - // echo 'Size: '. number_format($_FILES['file']['size'] / 1024, 3, '.', '') .'KB
'; - // echo 'File /URL: http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.''; + // Display list uploaded files + 'listfiles' => true, + + // Allow users to delete files that they have uploaded (will enable sessions) + 'allow_deletion' => true, + + // Allow users to mark files as hidden + 'allow_private' => true, + + // Display file sizes + 'listfiles_size' => true, + + // Display file dates + 'listfiles_date' => true, + + // Display file dates format + 'listfiles_date_format' => 'F d Y H:i:s', + + // Randomize file names (number of 'false') + 'random_name_len' => 8, + + // Keep filetype information (if random name is activated) + 'random_name_keep_type' => true, + + // Random file name letters + 'random_name_alphabet' => 'qazwsxedcrfvtgbyhnujmikolp1234567890', + + // Display debugging information + 'debug' => false, + + // Complete URL to your directory (including tracing slash) + 'url' => 'http://strace.club/', + + // Amount of seconds that each file should be stored for (0 for no limit) + // Default 30 days + 'time_limit' => 60 * 60 * 24 * 30, + + // Files that will be ignored + 'ignores' => array('.', '..', 'LICENSE', 'README.md'), + + // Language code + 'lang' => 'en_GB', + + // Language direction + 'lang_dir' => 'ltr', + ); + // =============={ Configuration End }============== + + // Is the local config file there? + if (file_exists('config-local.php')) { + // Load it then + include('config-local.php'); + } + + // Enabling error reporting + if ($settings['debug']) { + error_reporting(E_ALL); + ini_set('display_startup_errors',1); + ini_set('display_errors',1); + } + + $data = array(); + + // Name of this file + $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME); + + // Adding current script name to ignore list + $data['ignores'] = $settings['ignores']; + $data['ignores'][] = $data['scriptname']; + + // Use canonized path + $data['uploaddir'] = realpath($settings['uploaddir']); + + // Maximum upload size, set by system + $data['max_upload_size'] = ini_get('upload_max_filesize'); + + // If file deletion or private files are allowed, starting a session. + // This is required for user authentification + if ($settings['allow_deletion'] || $settings['allow_private']) { + session_start(); + + // 'User ID' + if (!isset($_SESSION['upload_user_id'])) + $_SESSION['upload_user_id'] = rand(100000, 999999); + + // List of filenames that were uploaded by this user + if (!isset($_SESSION['upload_user_files'])) + $_SESSION['upload_user_files'] = array(); + } + + // If debug is enabled, logging all variables + if ($settings['debug']) { + // Displaying debug information + echo '

Settings:

'; + echo '
';
+		print_r($settings);
+		echo '
'; + + // Displaying debug information + echo '

Data:

'; + echo '
';
+		print_r($data);
+		echo '
'; + echo ''; + + // Displaying debug information + echo '

SESSION:

'; + echo '
';
+		print_r($_SESSION);
+		echo '
'; + } + + // Format file size + function formatSize ($bytes) { + $units = array('B', 'KB', 'MB', 'GB', 'TB'); + + $bytes = max($bytes, 0); + $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); + $pow = min($pow, count($units) - 1); + + $bytes /= pow(1024, $pow); + + return ceil($bytes) . ' ' . $units[$pow]; + } + + // Rotating a two-dimensional array + function diverseArray ($vector) { + $result = array(); + foreach ($vector as $key1 => $value1) + foreach ($value1 as $key2 => $value2) + $result[$key2][$key1] = $value2; + return $result; + } + + // Handling file upload + function uploadFile ($file_data) { + global $settings; + global $data; + global $_SESSION; + + $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 { + $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)]; + 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'])); + } + $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'])) { + echo 'File name already exists' . "\n"; + return; + } + + // Moving uploaded file OK + if (move_uploaded_file($file_data['tmp_name'], $file_data['upload_target_file'])) { + if ($settings['allow_deletion'] || $settings['allow_private']) + $_SESSION['upload_user_files'][] = $file_data['target_file_name']; + echo $settings['url'] . $file_data['target_file_name'] . "\n"; } else { echo 'Error: unable to upload the file.'; - exit; } } + + + // 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']); + 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_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; + } + } + + // List files in a given directory, excluding certain files + 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; + ksort($file_array); + $file_array = array_reverse($file_array, true); + return $file_array; + } + + $file_array = listFiles($settings['uploaddir'], $data['ignores']); + + // Removing old files + foreach ($file_array as $file) + if ($settings['time_limit'] < time() - filemtime($file)) + unlink($file); + + $file_array = listFiles($settings['uploaddir'], $data['ignores']); + ?> - + + - Upload <?=$settings['scriptname']?> - + + + + + <?=$settings['title']?> -
-
- Choose File:
- -
+

+ + Maximum upload size:
+
- - Uploaded files:
-