]> git.mxchange.org Git - simple-upload.git/commitdiff
Moved more code to functions to encapsulate a bit more.
authorRoland Häder <roland@mxchange.org>
Tue, 29 Mar 2016 13:25:29 +0000 (15:25 +0200)
committerRoland Häder <roland@mxchange.org>
Tue, 29 Mar 2016 13:25:29 +0000 (15:25 +0200)
index.php

index 4b469a8f1b039b494b9cc206464fadf925e510f4..9429e6f0a4f619e6780e0c4795f1e8cd013ef422 100644 (file)
--- a/index.php
+++ b/index.php
 
        // Handling file upload
        function uploadFile ($file_data) {
-               global $settings;
-               global $data;
-               global $_SESSION;
+               global $settings, $data;
 
                $file_data['uploaded_file_name'] = basename($file_data['name']);
                $file_data['target_file_name'] = $file_data['uploaded_file_name'];
                }
        }
 
+       // Delete file
+       function deleteFile ($file) {
+               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 (file_exists($fqfn)) {
+                               unlink($fqfn);
+                               echo 'File has been removed';
+                               exit;
+                       }
+               }
+       }
+
+       // Mark/unmark file as hidden
+       function markUnmarkHidden ($file) {
+               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 (file_exists($fqfn)) {
+                               if (substr($file, 0, 1) === '.') {
+                                       rename($fqfn, substr($fqfn, 1));
+                                       echo 'File has been made visible';
+                               } else {
+                                       rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $file);
+                                       echo 'File has been hidden';
+                               }
+                               exit;
+                       }
+               }
+       }
 
        // Files are being POSEed. Uploading them one by one.
        if (isset($_FILES['file'])) {
 
        // 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'])) {
-                                       $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $_POST['target'];
-                                       if (file_exists($fqfn)) {
-                                               unlink($fqfn);
-                                               echo 'File has been removed';
-                                               exit;
-                                       }
-                               }
+               if ($settings['allow_deletion'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'delete')
+                       deleteFile($_POST['target']);
 
-               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'])) {
-                                       $fqfn = $data['uploaddir'] . DIRECTORY_SEPARATOR . $_POST['target'];
-                                       if (file_exists($fqfn)) {
-                                               if ($_POST['target'][0] === '.') {
-                                                       rename($fqfn, substr($fqfn, 1));
-                                                       echo 'File has been made visible';
-                                               } else {
-                                                       rename($fqfn, $data['uploaddir'] . DIRECTORY_SEPARATOR . '.' . $_POST['target']);
-                                                       echo 'File has been hidden';
-                                               }
-                                               exit;
-                                       }
-                               }
+               if ($settings['allow_private'] && (isset($_POST['target'])) && isset($_POST['action']) && $_POST['action'] === 'privatetoggle')
+                       markUnmarkHidden($_POST['target']);
        }
 
        // List files in a given directory, excluding certain files
        // Removes old files
        function removeOldFiles ($dir) {
                global $file_array, $settings;
+
                foreach ($file_array as $file) {
                        $fqfn = $dir . DIRECTORY_SEPARATOR . $file;
                        if ($settings['time_limit'] < time() - filemtime($fqfn))