]> git.mxchange.org Git - simple-upload.git/blobdiff - index.php
Verious improvements
[simple-upload.git] / index.php
index b94b8fb7d83f700c0d2f2e45309f9f18f0416fb3..461ee3a1054c851670cde4fdee51bfa69a468d81 100644 (file)
--- a/index.php
+++ b/index.php
                along with this program.  If not, see <http://www.gnu.org/licenses/>.
        */
 
-       // ============== Configuration begin  ==============
-
+       // =============={ Configuration Begin }==============
        $settings = array(
 
+               // Website title
+               title => 'strace.club',
+
                // Directory to store uploaded files
                uploaddir => '.',
 
@@ -27,6 +29,9 @@
                // 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,
 
                random_name_alphabet => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
 
                // Display debugging information
-               debug => ($_SERVER['SERVER_NAME'] === 'localhost')
+               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)
+               time_limit => 0,
 
-       // ============== Configuration end  ==============
+               // Files that will be ignored
+               ignores = array('.', '..', 'LICENSE', 'README.md'),
+       );
+       // =============={ Configuration End }==============
 
        $data = array();
 
        // Name of this file
        $data['scriptname'] = 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'][] = $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 ($settings['allow_deletion']) {
+       // 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(10009999);
+                       $_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']) {
 
-
                // Enabling error reporting
                error_reporting(E_ALL);
                error_reporting(1);
 
                // Displaying debug information
-               echo '<h2>Debugging information: settings</h2>';
+               echo '<h2>Settings:</h2>';
                echo '<pre>';
                print_r($settings);
                echo '</pre>';
 
                // Displaying debug information
-               echo '<h2>Debugging information: data</h2>';
+               echo '<h2>Data:</h2>';
                echo '<pre>';
                print_r($data);
                echo '</pre>';
                echo '</pre>';
 
                // Displaying debug information
-               echo '<h2>Debugging information: _SESSION</h2>';
+               echo '<h2>SESSION:</h2>';
                echo '<pre>';
                print_r($_SESSION);
                echo '</pre>';
        }
 
+       // Format file size
        function FormatSize ($bytes) {
                $units = array('B', 'KB', 'MB', 'GB', 'TB');
 
                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)
+               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;
 
                $data['uploaded_file_name'] = basename($file_data['name']);
                $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 (file_exists($data['target_file_name']));
                }
                $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
-               $data['tmp_name'] = $file_data['tmp_name'];
 
+               // Do now allow to overwriting files
                if (file_exists($data['upload_target_file'])) {
                        echo 'File name already exists' . "\n";
                        return;
                }
 
-
-               echo '<pre>';
-               print_r($data);
-               echo '</pre>';
-
-
-               if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) {
-                       if ($settings['allow_deletion'])
+               // Moving uploaded file OK
+               if (move_uploaded_file($file_data['tmp_name'], $data['upload_target_file'])) {
+                       if ($settings['allow_deletion'] || $settings['allow_private'])
                                $_SESSION['upload_user_files'][] = $data['target_file_name'];
-                       echo $data['pageurl'] .  $data['upload_target_file'] . "\n";
-                       // echo 'File: <b>' . $data['uploaded_file_name'] . '</b> successfully uploaded:<br />';
-                       // echo 'Size: <b>'. number_format($_FILES['file']['size'] / 1024, 3, '.', '') .'KB</b><br />';
-                       // echo 'File /URL: <b><a href="http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$data['upload_target_file'].'">http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$data['upload_target_file'].'</a></b>';
+                       echo $settings['url'] .  $data['target_file_name'] . "\n";
                } else {
                        echo 'Error: unable to upload the file.';
                }
        }
 
-       if (isset($_FILES['file'])) {
-               if ($settings['debug']) {
-                       // Displaying debug information
-                       echo '<h2>Debugging information: data</h2>';
-                       echo '<pre>';
-                       print_r($data);
-                       echo '</pre>';
-                       // Displaying debug information
-                       echo '<h2>Debugging information: file</h2>';
-                       echo '<pre>';
-                       print_r($_FILES);
-                       echo '</pre>';
-               }
 
+       // 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)
                exit;
        }
 
-       if ($settings['allow_deletion'])
-               if (isset($_POST))
+       // Other file functions (delete, private).
+       if (isset($_POST)) {
+               if ($settings['allow_deletion'])
                        if ($_POST['action'] === 'delete')
-                               if (in_array($_POST['target'], $_SESSION['upload_user_files']))
+                               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 ($_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;
+       }
+
 ?>
 <html lang="en-GB">
        <head>
                <meta charset="utf-8">
-               <title>Simple PHP Upload</title>
+               <title><?=$settings['title']?></title>
+               <style media="screen">
+                       body {
+                               background: #111;
+                               margin: 0;
+                               color: #ddd;
+                               font-family: sans-serif;
+                       }
+
+                       body > h1 {
+                               display: block;
+                               background: rgba(255, 255, 255, 0.05);
+                               padding: 8px 16px;
+                               text-align: center;
+                               margin: 0;
+                       }
+
+                       body > form {
+                               display: block;
+                               background: rgba(255, 255, 255, 0.075);
+                               padding: 16px 16px;
+                               margin: 0;
+                               text-align: center;
+                       }
+
+                       body > ul {
+                               display: block;
+                               padding: 0;
+                               max-width: 1000px;
+                               margin: 32px auto;
+                       }
+
+                       body > ul > li {
+                               display: block;
+                               margin: 0;
+                               padding: 0;
+                       }
+
+                       body > ul > li > a {
+                               display: block;
+                               margin: 0 0 1px 0;
+                               list-style: none;
+                               background: rgba(255, 255, 255, 0.1);
+                               padding: 8px 16px;
+                               text-decoration: none;
+                               color: inherit;
+                               opacity: 0.5;
+                       }
+
+                       body > ul > li > a:hover {
+                               opacity: 1;
+                       }
+
+                       body > ul > li > a:active {
+                               opacity: 0.5;
+                       }
+
+                       body > ul > li > a > span {
+                               float: right;
+                               font-size: 90%;
+                       }
+
+                       body > ul > li > form {
+                               display: inline-block;
+                               padding: 0;
+                               margin: 0;
+                       }
+
+                       body > ul > li.owned {
+                               margin: 8px;
+                       }
+
+                       body > ul > li > form > button {
+                               opacity: 0.5;
+                               display: inline-block;
+                               padding: 4px 16px;
+                               margin: 0;
+                               border: 0;
+                               background: rgba(255, 255, 255, 0.1);
+                               color: inherit;
+                       }
+
+                       body > ul > li > form > button:hover {
+                               opacity: 1;
+                       }
+
+                       body > ul > li > form > button:active {
+                               opacity: 0.5;
+                       }
+
+                       body > ul > li.uploading {
+                               animation: upanim 2s linear 0s infinite alternate;
+                       }
+
+                       @keyframes upanim {
+                               from {
+                                       opacity: 0.3;
+                               }
+                               to {
+                                       opacity: 0.8;
+                               }
+                       }
+               </style>
        </head>
        <body>
-               <h1>Simple PHP Upload</h1>
-               <p>
-                       Maximum upload size: <?php echo $data['max_upload_size']; ?>
-               </p>
-               <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
-                       <div class="fallback">
-                               Choose File: <input type="file" name="file[]" multiple required /><br />
-                               <input type="submit" value="Upload" />
-                       </div>
+               <h1><?=$settings['title']?></h1>
+               <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[]" multiple required id="simpleupload-input"/>
                </form>
                <?php if ($settings['listfiles']) { ?>
-                       <strong>Uploaded files:</strong><br />
-                       <ul>
+                       <ul id="simpleupload-ul">
                                <?php
-                                       $dh = opendir($settings['uploaddir']);
-                                       while (false !== ($filename = readdir($dh)))
-                                               if (is_file($filename) && !in_array($filename, array('.', '..', $data['scriptname']))) {
-                                                       $file_info = array();
+                                       $file_array = ListFiles($settings['uploaddir'], $data['ignores']);
+                                       foreach ($file_array as $mtime => $filename) {
+                                               $file_info = array();
+                                               $file_owner = false;
+                                               $file_private = $filename[0] === '.';
+
+                                               if ($settings['listfiles_size'])
+                                                       $file_info[] = FormatSize(filesize($filename));
+
+                                               if ($settings['listfiles_size'])
+                                                       $file_info[] = date($settings['listfiles_date_format'], $mtime);
+
+                                               if ($settings['allow_deletion'] || $settings['allow_private'])
+                                                       if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
+                                                               $file_owner = true;
 
-                                                       if ($settings['listfiles_size'])
-                                                               $file_info[] = FormatSize(filesize($filename));
+                                               $file_info = implode(', ', $file_info);
 
-                                                       if ($settings['listfiles_size'])
-                                                               $file_info[] = date($settings['listfiles_date_format'], filemtime($filename));
+                                               if (strlen($file_info) > 0)
+                                                       $file_info = ' (' . $file_info . ')';
 
-                                                       if ($settings['allow_deletion'])
-                                                               if (in_array($filename, $_SESSION['upload_user_files']))
-                                                                       $file_info[] = '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="delete" /><button type="submit">delete</button></form>';
+                                               $class = '';
+                                               if ($file_owner)
+                                                       $class = 'owned';
 
-                                                       $file_info = implode(', ', $file_info);
+                                               if (!$file_private || $file_owner) {
+                                                       echo "<li class=\"' . $class . '\">";
 
-                                                       if (strlen($file_info) > 0)
-                                                               $file_info = ' (' . $file_info . ')';
+                                                       echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
 
-                                                       echo "<li><a href=\"$filename\">$filename</a>$file_info</li>";
+                                                       if ($file_owner) {
+                                                               if ($settings['allow_deletion'])
+                                                                       echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="delete" /><button type="submit">delete</button></form>';
+
+                                                               if ($settings['allow_private'])
+                                                                       if ($file_private)
+                                                                               echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="privatetoggle" /><button type="submit">make public</button></form>';
+                                                                       else
+                                                                               echo '<form action="' . $data['scriptname'] . '" method="POST"><input type="hidden" name="target" value="' . $filename . '" /><input type="hidden" name="action" value="privatetoggle" /><button type="submit">make private</button></form>';
+                                                       }
+
+                                                       echo "</li>";
                                                }
+                                       }
                                ?>
                        </ul>
                <?php } ?>
+               <a href="https://github.com/muchweb/simple-php-upload"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/38ef81f8aca64bb9a64448d0d70f1308ef5341ab/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a>
+               <script charset="utf-8">
+                       var target_form = document.getElementById('simpleupload-form'),
+                               target_ul = document.getElementById('simpleupload-ul'),
+                               target_input = document.getElementById('simpleupload-input');
+
+                       target_form.addEventListener('dragover', function (event) {
+                               event.preventDefault();
+                       }, false);
+
+                       function AddFileLi (name, info) {
+                               target_form.style.display = 'none';
+
+                               var new_li = document.createElement('li');
+                               new_li.className = 'uploading';
+
+                               var new_a = document.createElement('a');
+                               new_a.innerHTML = name;
+                               new_li.appendChild(new_a);
+
+                               var new_span = document.createElement('span');
+                               new_span.innerHTML = info;
+                               new_a.appendChild(new_span);
+
+                               target_ul.insertBefore(new_li, target_ul.firstChild);
+                       }
+
+                       function HandleFiles (event) {
+                               event.preventDefault();
+
+                               var i = 0,
+                                       files = event.dataTransfer.files,
+                                       len = files.length;
+
+                               var form = new FormData();
+
+                               for (; i < len; i++) {
+                                       form.append('file[]', files[i]);
+                                       AddFileLi(files[i].name, files[i].size + ' bytes');
+                               }
+
+                               var xhr = new XMLHttpRequest();
+                               xhr.onload = function() {
+                                       window.location.reload();
+                               };
+
+                               xhr.open('post', '<?php echo $data['scriptname']; ?>', true);
+                               xhr.send(form);
+                       }
+
+                       target_form.addEventListener('drop', HandleFiles, false);
+
+                       document.getElementById('simpleupload-input').onchange = function () {
+                               AddFileLi('Uploading...', '');
+                               target_form.submit();
+                       };
+               </script>
        </body>
 </html>