index.php: remove the sandbox attribute for the iframe element
[experiments/php-simple-upload.git] / index.php
1 <?php
2 /**
3  * @file
4  * php-simple-upload - simple script to upload files to the web server
5  *
6  * Copyright (C) 2017  Antonio Ospite <ao2@ao2.it>
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 require __DIR__ . '/vendor/autoload.php';
23
24 use Sirius\Upload\Handler as UploadHandler;
25
26 // Path relative to the script dir.
27 const INCOMING_DIR = 'incoming/';
28
29 const MAX_FILE_SIZE = '1G';
30
31 const ALLOWED_EXTENSIONS = [
32   'avi',
33   'bz2',
34   'gz',
35   'htm',
36   'html',
37   'jpg',
38   'mp3',
39   'mpg',
40   'php',
41   'png',
42   'rar',
43   'txt',
44   'zip',
45 ];
46
47 if (isset($_POST['task']) && $_POST['task'] == "upload") {
48   $uploadHandler = new UploadHandler(INCOMING_DIR);
49
50   $uploadHandler->addRule('extension', ['allowed' => ALLOWED_EXTENSIONS], '{label} invalid file type', 'File');
51   $uploadHandler->addRule('size', ['max' => MAX_FILE_SIZE], '{label} should be less than {max}', 'File');
52
53   $result = $uploadHandler->process($_FILES);
54   if ($result->isValid()) {
55     try {
56       $result->confirm();
57     }
58     catch (\Exception $e) {
59       $result->clear();
60       throw $e;
61     }
62   }
63   else {
64     echo "<pre>{$result->getMessages()}</pre>";
65   }
66 }
67
68 $iframe_parent_request_url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . INCOMING_DIR;
69
70 // Avoid iframe recursion.
71 if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $iframe_parent_request_url) {
72   echo "Iframe recursion detected, use the BACK button in the browser";
73   return;
74 }
75 ?>
76
77 <h1>Upload</h1>
78 <form method="POST" enctype="multipart/form-data">
79   <input type="file" name="filefield[]" multiple="true"/>
80   <input type="hidden" name="task" value="upload"/>
81   <input type="submit" value="Upload File"/>
82 </form>
83
84 <iframe src="<?php echo INCOMING_DIR; ?>" height="100%" width="100%" frameborder="0">
85   Your browser does not support iframes <a href="<?php echo INCOMING_DIR; ?>">click here to view the page directly.</a>
86 </iframe>