X-Git-Url: https://git.mxchange.org/?p=simple-upload.git;a=blobdiff_plain;f=index.php;h=5ba2e262ee543659e0bd328a63ef6d7984d52a6b;hp=2952e39b0c1744f729e853b3b9b5f6f5c43a5101;hb=HEAD;hpb=53ce79d11c8f09ea6863830afd9b62b8e6401d7f diff --git a/index.php b/index.php index 2952e39..5ba2e26 100644 --- a/index.php +++ b/index.php @@ -14,69 +14,144 @@ along with this program. If not, see . */ - // ============== Configuration begin ============== - + // =============={ Configuration Begin }============== $settings = array( + // Website title + 'title' => 'strace.club', + + // Description for this website + 'description' => '', - // Directory to store uploaded files - uploaddir => '.', + // Base path (auto-detection) + 'base_path' => dirname(__FILE__) . DIRECTORY_SEPARATOR, + // Directory to store uploaded files (relative to base_path) + 'uploaddir' => '.', // Display list uploaded files - listfiles => true, + '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, + 'listfiles_size' => true, + // Display file dates + 'listfiles_date' => true, - // Randomize file names (number of 'false') - random_name_len => 10, + // 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_name_keep_type' => true, // Random file name letters - random_name_alphabet => 'qwertyuiodfgjkcvbnm', + '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 => true + 'debug' => false, + + // 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 + 'time_limit' => 60 * 60 * 24 * 30, + // 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 }============== - // ============== 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); + ini_set('display_startup_errors',1); + ini_set('display_errors',1); + } - $data = array(); + $data = array(); // Name of this file - $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME); + $data['scriptname'] = $settings['url'] . pathinfo(__FILE__, PATHINFO_BASENAME); - // URL to upload page - $data['pageurl'] = "http" . (($_SERVER['SERVER_PORT']==443) ? "s://" : "://") . $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) . '/'; + // Adding current script name to ignore list + $data['ignores'] = $settings['ignores']; + $data['ignores'][] = basename($data['scriptname']); - if ($settings['debug']) { - // Enabling error reporting - error_reporting(E_ALL); - error_reporting(1); + // Use canonized path + $data['uploaddir'] = realpath($settings['base_path'] . $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'] = mt_rand(100000, 999999); - // Displaying debug information - echo '

Debugging information: settings

'; + // 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 '

Debugging information: data

'; + // Displaying debug information + echo '

Data:

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

SESSION:

'; + echo '
';
+		print_r($_SESSION);
+		echo '
'; } - function FormatSize ($bytes, $precision = 2) { + // Format file size + function formatSize ($bytes) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); @@ -85,240 +160,461 @@ $bytes /= pow(1024, $pow); - return round($bytes, $precision) . ' ' . $units[$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, $data; + + $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'][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'])); + } + $file_data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $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 false; + } + + // 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"; - if (isset($_FILES['file']) && strlen($_FILES['file']['name']) > 1) { - $data['uploaded_file_name'] = basename($_FILES['file']['name']); - $data['target_file_name'] = $data['uploaded_file_name']; - if ($settings['random_name_len'] !== false) { - $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)]; - if ($settings['random_name_keep_type']) - $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION); - } - $data['upload_target_file'] = $settings['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name']; - $data['tmp_name'] = $_FILES['file']['tmp_name']; - - if ($settings['debug']) { - // Displaying debug information - echo '

Debugging information: data

'; - echo '
';
-    		print_r($data);
-    		echo '
'; - } - - if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) { - echo $data['pageurl'] . $data['upload_target_file']; - exit; - // echo 'File: ' . $data['uploaded_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']), '\\/').'/'.$data['upload_target_file'].''; + // Return target file name for later handling + return $file_data['upload_target_file']; } else { echo 'Error: unable to upload the file.'; - exit; + return false; } } -?> - - - Upload files - - - -
-
- Choose File:
- -
+

+

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