]> git.mxchange.org Git - simple-upload.git/blobdiff - index.php
Renaming (hide/unhide) of files rewritten so failures can be caught.
[simple-upload.git] / index.php
index 02f1f0f2408ced86ec5770e5432c13d8dc820320..5ba2e262ee543659e0bd328a63ef6d7984d52a6b 100644 (file)
--- a/index.php
+++ b/index.php
                // 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
                // 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
        $data = array();
 
        // Name of this file
-       $data['scriptname'] = $settings['url'] . '/' . 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'][] = basename($data['scriptname']);
 
        // Use canonized path
-       $data['uploaddir'] = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . $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');
                                $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']));
                // Do now allow to overwriting files
                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;
                }
        }
 
                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)) {
+                       $sourceFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . $file;
+                       if (!in_array($file, $data['ignores']) && isReadableFile($sourceFile)) {
                                if (substr($file, 0, 1) === '.') {
-                                       rename($fqfn, substr($fqfn, 1));
-                                       echo 'File has been made visible';
+                                       $targetFile = $data['uploaddir'] . DIRECTORY_SEPARATOR . substr($file, 1);
+                                       $successMessage = 'File has been made public';
+                                       $failedMessage = 'File has NOT been made public';
                                } else {
-                                       rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file);
-                                       echo 'File has been hidden';
+                                       $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;
                        }
                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);
+                       foreach ($file_array as $file_data) {
+                               $targetFile = uploadFile($file_data);
+                       }
                } else {
-                       uploadFile($_FILES['file']);
+                       $targetFile = uploadFile($_FILES['file']);
                }
                exit;
        }
        </head>
        <body>
                <h1><?=$settings['title']?></h1>
+               <p><?=$settings['description']?></p>
                <form action="<?= $data['scriptname'] ?>" method="post" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
                        Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
                        <input type="file" name="file[]" id="simpleupload-input" />