]> git.mxchange.org Git - simple-upload.git/blobdiff - index.php
Introduced detectServerUrl() which makes configuration of static 'url'
[simple-upload.git] / index.php
index 02f1f0f2408ced86ec5770e5432c13d8dc820320..883405c9a6da38d97e69066d9ed517e2787fe01a 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
@@ -53,8 +59,8 @@
                // 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');
                // 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;
                }
        }
 
                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" />