Correct link for GH ribbon
[simple-upload.git] / index.php
1 <?php
2         /*
3                 This program is free software: you can redistribute it and/or modify
4                 it under the terms of the GNU General Public License as published by
5                 the Free Software Foundation, either version 3 of the License, or
6                 (at your option) any later version.
7
8                 This program is distributed in the hope that it will be useful,
9                 but WITHOUT ANY WARRANTY; without even the implied warranty of
10                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11                 GNU General Public License for more details.
12
13                 You should have received a copy of the GNU General Public License
14                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
15         */
16
17         // ============== Configuration begin  ==============
18
19         $settings = array(
20
21                 // Directory to store uploaded files
22                 uploaddir => '.',
23
24                 // Display list uploaded files
25                 listfiles => true,
26
27                 // Allow users to delete files that they have uploaded (will enable sessions)
28                 allow_deletion => true,
29
30                 // Display file sizes
31                 listfiles_size => true,
32
33                 // Display file dates
34                 listfiles_date => true,
35
36                 // Display file dates format
37                 listfiles_date_format => 'F d Y H:i:s',
38
39                 // Randomize file names (number of 'false')
40                 random_name_len => 8,
41
42                 // Keep filetype information (if random name is activated)
43                 random_name_keep_type => true,
44
45                 // Random file name letters
46                 random_name_alphabet => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
47
48                 // Display debugging information
49                 debug => false,
50
51                 // Complete URL to your directory (including tracing slash)
52                 url => 'http://strace.club/',
53
54         );
55
56         // ============== Configuration end  ==============
57
58         $data = array();
59
60         // Name of this file
61         $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
62
63         // Use canonized path
64         $data['uploaddir'] = realpath($settings['uploaddir']);
65
66         // Maximum upload size, set by system
67         $data['max_upload_size'] = ini_get('upload_max_filesize');
68
69         if ($settings['allow_deletion']) {
70                 session_start();
71
72                 if (!isset($_SESSION['upload_user_id']))
73                         $_SESSION['upload_user_id'] = rand(1000, 9999);
74
75                 if (!isset($_SESSION['upload_user_files']))
76                         $_SESSION['upload_user_files'] = array();
77         }
78
79         if ($settings['debug']) {
80
81
82                 // Enabling error reporting
83                 error_reporting(E_ALL);
84                 error_reporting(1);
85
86                 // Displaying debug information
87                 echo '<h2>Debugging information: settings</h2>';
88                 echo '<pre>';
89                 print_r($settings);
90                 echo '</pre>';
91
92                 // Displaying debug information
93                 echo '<h2>Debugging information: data</h2>';
94                 echo '<pre>';
95                 print_r($data);
96                 echo '</pre>';
97                 echo '</pre>';
98
99                 // Displaying debug information
100                 echo '<h2>Debugging information: _SESSION</h2>';
101                 echo '<pre>';
102                 print_r($_SESSION);
103                 echo '</pre>';
104         }
105
106         function FormatSize ($bytes) {
107                 $units = array('B', 'KB', 'MB', 'GB', 'TB');
108
109                 $bytes = max($bytes, 0);
110                 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
111                 $pow = min($pow, count($units) - 1);
112
113                 $bytes /= pow(1024, $pow);
114
115                 return ceil($bytes) . ' ' . $units[$pow];
116         }
117
118         function DiverseArray ($vector) {
119                 $result = array();
120                 foreach($vector as $key1 => $value1)
121                         foreach($value1 as $key2 => $value2)
122                                 $result[$key2][$key1] = $value2;
123                 return $result;
124         }
125
126         function UploadFile ($file_data) {
127                 global $settings;
128                 global $data;
129                 global $_SESSION;
130
131                 $data['uploaded_file_name'] = basename($file_data['name']);
132                 $data['target_file_name'] = $file_data['uploaded_file_name'];
133                 if ($settings['random_name_len'] !== false) {
134                         do {
135                                 $data['target_file_name'] = '';
136                                 while (strlen($data['target_file_name']) < $settings['random_name_len'])
137                                         $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
138                                 if ($settings['random_name_keep_type'])
139                                         $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
140                         } while (file_exists($data['target_file_name']));
141                 }
142                 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
143                 $data['tmp_name'] = $file_data['tmp_name'];
144
145                 if (file_exists($data['upload_target_file'])) {
146                         echo 'File name already exists' . "\n";
147                         return;
148                 }
149
150                 if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) {
151                         if ($settings['allow_deletion'])
152                                 $_SESSION['upload_user_files'][] = $data['target_file_name'];
153                         echo $settings['url'] .  $data['target_file_name'] . "\n";
154                 } else {
155                         echo 'Error: unable to upload the file.';
156                 }
157         }
158
159         if (isset($_FILES['file'])) {
160                 if ($settings['debug']) {
161                         // Displaying debug information
162                         echo '<h2>Debugging information: data</h2>';
163                         echo '<pre>';
164                         print_r($data);
165                         echo '</pre>';
166                         // Displaying debug information
167                         echo '<h2>Debugging information: file</h2>';
168                         echo '<pre>';
169                         print_r($_FILES);
170                         echo '</pre>';
171                 }
172
173                 if (is_array($_FILES['file'])) {
174                         $file_array = DiverseArray($_FILES['file']);
175                         foreach ($file_array as $file_data)
176                                 UploadFile($file_data);
177                 } else
178                         UploadFile($_FILES['file']);
179                 exit;
180         }
181
182         if ($settings['allow_deletion'])
183                 if (isset($_POST))
184                         if ($_POST['action'] === 'delete')
185                                 if (in_array($_POST['target'], $_SESSION['upload_user_files']))
186                                         if (file_exists($_POST['target'])) {
187                                                 unlink($_POST['target']);
188                                                 echo 'File has been removed';
189                                                 exit;
190                                         }
191
192         function ListFiles ($dir, $exclude) {
193                 $file_array = array();
194                 $dh = opendir($dir);
195                         while (false !== ($filename = readdir($dh)))
196                                 if (is_file($filename) && !in_array($filename, $exclude))
197                                         $file_array[filemtime($filename)] = $filename;
198                 ksort($file_array);
199                 $file_array = array_reverse($file_array, true);
200                 return $file_array;
201         }
202
203 ?>
204 <html lang="en-GB">
205         <head>
206                 <meta charset="utf-8">
207                 <title>strace.club</title>
208                 <style media="screen">
209                         body {
210                                 background: #111;
211                                 margin: 0;
212                                 color: #ddd;
213                                 font-family: sans-serif;
214                         }
215
216                         h1 {
217                                 display: block;
218                                 background: rgba(255, 255, 255, 0.05);
219                                 padding: 8px 16px;
220                                 text-align: center;
221                                 margin: 0;
222                         }
223
224                         form {
225                                 display: block;
226                                 background: rgba(255, 255, 255, 0.075);
227                                 padding: 16px 16px;
228                                 margin: 0;
229                         }
230
231                         p {
232                                 display: block;
233                                 background: rgba(255, 255, 255, 0.075);
234                                 padding: 4px 16px;
235                                 margin: 16px 0 0 0;
236                                 text-align: center;
237                         }
238
239                         ul {
240                                 display: block;
241                                 margin: 0;
242                                 padding: 0;
243                         }
244
245                         ul > li {
246                                 display: block;
247                                 margin: 0;
248                                 padding: 0;
249                         }
250
251                         ul > li > a {
252                                 display: block;
253                                 margin: 0 0 1px 0;
254                                 list-style: none;
255                                 background: rgba(255, 255, 255, 0.1);
256                                 padding: 8px 16px;
257                                 text-decoration: none;
258                                 color: inherit;
259                                 opacity: 0.5;
260                         }
261
262                         ul > li > a > span {
263                                 float: right;
264                                 font-size: 90%;
265                         }
266
267                         ul > li > a:hover {
268                                 opacity: 1;
269                         }
270                 </style>
271         </head>
272         <body>
273                 <h1>strace.club</h1>
274                 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
275                         Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
276                         <input type="file" name="file[]" multiple required onchange="formname.submit();" />
277                 </form>
278                 <?php if ($settings['listfiles']) { ?>
279                         <p>Uploaded files:</p>
280                         <ul>
281                                 <?php
282                                         $file_array = ListFiles($settings['uploaddir'], array('.', '..', $data['scriptname']));
283                                         foreach ($file_array as $mtime => $filename) {
284                                                 $file_info = array();
285
286                                                 if ($settings['listfiles_size'])
287                                                         $file_info[] = FormatSize(filesize($filename));
288
289                                                 if ($settings['listfiles_size'])
290                                                         $file_info[] = date($settings['listfiles_date_format'], $mtime);
291
292                                                 if ($settings['allow_deletion'])
293                                                         if (in_array($filename, $_SESSION['upload_user_files']))
294                                                                 $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>';
295
296                                                 $file_info = implode(', ', $file_info);
297
298                                                 if (strlen($file_info) > 0)
299                                                         $file_info = ' (' . $file_info . ')';
300
301                                                 echo "<li><a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a></li>";
302                                         }
303                                 ?>
304                         </ul>
305                 <?php } ?>
306                 <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>
307         </body>
308 </html>