]> git.mxchange.org Git - simple-upload.git/blobdiff - index.php
Opps, no more standard, using comment block.
[simple-upload.git] / index.php
index 0746448ce6c242f9647b0d2c9743d2dcf50f46ec..3bbc62558575ec8805964dd4f22a86abdc8612f1 100644 (file)
--- a/index.php
+++ b/index.php
 
                // Remove old files?
                'remove_old_files' => true,
+
+               // Privacy: Allow external references (the "fork me" ribbon)
+               'allow_external_refs' => true,
        );
        // =============={ Configuration End }==============
 
        // Is the local config file there?
-       if (file_exists('config-local.php')) {
+       if (isReadableFile('config-local.php')) {
                // Load it then
                include('config-local.php');
        }
        $data = array();
 
        // Name of this file
-       $data['scriptname'] = 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'][] = $data['scriptname'];
+       $data['ignores'][] = basename($data['scriptname']);
 
        // Use canonized path
        $data['uploaddir'] = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . $settings['uploaddir']);
 
        // 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'];
                                        $file_data['target_file_name'] .= $settings['random_name_alphabet'][mt_rand(0, strlen($settings['random_name_alphabet']) - 1)];
                                if ($settings['random_name_keep_type'])
                                        $file_data['target_file_name'] .= '.' . pathinfo($file_data['uploaded_file_name'], PATHINFO_EXTENSION);
-                       } while (file_exists($file_data['target_file_name']));
+                       } 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 (file_exists($file_data['upload_target_file'])) {
+               if (isReadableFile($file_data['upload_target_file'])) {
                        echo 'File name already exists' . "\n";
                        return;
                }
                }
        }
 
+       // 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 (!in_array($file, $data['ignores']) && isReadableFile($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 (!in_array($file, $data['ignores']) && isReadableFile($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;
+                       }
+               }
+       }
+
+       // Checks if the given file is a file and is readable
+       function isReadableFile ($file) {
+               return (is_file($file) && is_readable($file));
+       }
 
        // 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']))
-                                       if (file_exists($_POST['target'])) {
-                                               unlink($_POST['target']);
-                                               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']))
-                                       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;
-                                       }
+               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
-       function listFiles ($dir, $exclude) {
+       function createArrayFromPath ($dir) {
+               global $data;
+
                $file_array = array();
                $dh = opendir($dir);
-                       while (false !== ($filename = readdir($dh))) {
+                       while ($filename = readdir($dh)) {
                                $fqfn = $dir . DIRECTORY_SEPARATOR . $filename;
-                               if (is_file($fqfn) && !in_array($filename, $exclude))
+                               if (isReadableFile($fqfn) && !in_array($filename, $data['ignores']))
                                        $file_array[filemtime($fqfn)] = $filename;
                        }
 
        // 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))
 
        // Only read files if the feature is enabled
        if ($settings['listfiles']) {
-               $file_array = listFiles($data['uploaddir'], $data['ignores']);
+               $file_array = createArrayFromPath($data['uploaddir']);
 
                // Removing old files
                if ($settings['remove_old_files'])
                        removeOldFiles($data['uploaddir']);
 
-               $file_array = listFiles($data['uploaddir'], $data['ignores']);
+               $file_array = createArrayFromPath($data['uploaddir']);
        }
 ?>
-<!DOCTYPE html>
-<html lang="<?=$settings['lang']?>" dir="<?=$settings['lang_dir']?>">
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?=$settings['lang']?>" lang="<?=$settings['lang']?>" dir="<?=$settings['lang_dir']?>">
        <head>
-               <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-               <meta http-equiv="Content-Script-Type" content="text/javascript">
-               <meta name="robots" content="noindex">
-               <meta name="referrer" content="origin-when-crossorigin">
+               <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
+               <meta http-equiv="content-style-type" content="text/css" />
+               <meta http-equiv="content-script-type" content="text/javascript" />
+               <meta http-equiv="language" content="de" />
+
+               <meta name="robots" content="noindex" />
+               <meta name="referrer" content="origin-when-crossorigin" />
                <title><?=$settings['title']?></title>
-               <style media="screen">
-               <!--
+               <style type="text/css" media="screen">
                        body {
                                background: #111;
                                margin: 0;
                                        opacity: 0.8;
                                }
                        }
-               //-->
                </style>
        </head>
        <body>
                <h1><?=$settings['title']?></h1>
-               <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
+               <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"/>
+                       <input type="file" name="file[]" id="simpleupload-input" />
                </form>
                <?php if ($settings['listfiles']) { ?>
                        <ul id="simpleupload-ul">
 
                                                        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>';
+                                                                       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>';
+                                                                               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 '<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>
+               <?php
+               }
+
+               if ($settings['allow_external_refs']) {
+               ?>
+                       <a href="https://github.com/muchweb/simple-php-upload" target="_blank"><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>
+               <?php
+               } else {
+               ?>
+                       <a href="https://github.com/muchweb/simple-php-upload" target="_blank">Fork me on GitHub</a>
+               <?php
+               }
+               ?>
                <script type="text/javascript">
                <!--
                        var target_form = document.getElementById('simpleupload-form'),