:lipstick: Max width styles
[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                 // Allow users to mark files as hidden
31                 allow_private => true,
32
33                 // Display file sizes
34                 listfiles_size => true,
35
36                 // Display file dates
37                 listfiles_date => true,
38
39                 // Display file dates format
40                 listfiles_date_format => 'F d Y H:i:s',
41
42                 // Randomize file names (number of 'false')
43                 random_name_len => 8,
44
45                 // Keep filetype information (if random name is activated)
46                 random_name_keep_type => true,
47
48                 // Random file name letters
49                 random_name_alphabet => 'qazwsxedcrfvtgbyhnujmikolp1234567890',
50
51                 // Display debugging information
52                 debug => false,
53
54                 // Complete URL to your directory (including tracing slash)
55                 url => 'http://strace.club/',
56
57         );
58
59         // ============== Configuration end  ==============
60
61         $data = array();
62
63         // Name of this file
64         $data['scriptname'] = pathinfo(__FILE__, PATHINFO_BASENAME);
65
66         // Use canonized path
67         $data['uploaddir'] = realpath($settings['uploaddir']);
68
69         // Maximum upload size, set by system
70         $data['max_upload_size'] = ini_get('upload_max_filesize');
71
72         if ($settings['allow_deletion'] || $settings['allow_private']) {
73                 session_start();
74
75                 if (!isset($_SESSION['upload_user_id']))
76                         $_SESSION['upload_user_id'] = rand(1000, 9999);
77
78                 if (!isset($_SESSION['upload_user_files']))
79                         $_SESSION['upload_user_files'] = array();
80         }
81
82         if ($settings['debug']) {
83
84
85                 // Enabling error reporting
86                 error_reporting(E_ALL);
87                 error_reporting(1);
88
89                 // Displaying debug information
90                 echo '<h2>Debugging information: settings</h2>';
91                 echo '<pre>';
92                 print_r($settings);
93                 echo '</pre>';
94
95                 // Displaying debug information
96                 echo '<h2>Debugging information: data</h2>';
97                 echo '<pre>';
98                 print_r($data);
99                 echo '</pre>';
100                 echo '</pre>';
101
102                 // Displaying debug information
103                 echo '<h2>Debugging information: _SESSION</h2>';
104                 echo '<pre>';
105                 print_r($_SESSION);
106                 echo '</pre>';
107         }
108
109         function FormatSize ($bytes) {
110                 $units = array('B', 'KB', 'MB', 'GB', 'TB');
111
112                 $bytes = max($bytes, 0);
113                 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
114                 $pow = min($pow, count($units) - 1);
115
116                 $bytes /= pow(1024, $pow);
117
118                 return ceil($bytes) . ' ' . $units[$pow];
119         }
120
121         function DiverseArray ($vector) {
122                 $result = array();
123                 foreach($vector as $key1 => $value1)
124                         foreach($value1 as $key2 => $value2)
125                                 $result[$key2][$key1] = $value2;
126                 return $result;
127         }
128
129         function UploadFile ($file_data) {
130                 global $settings;
131                 global $data;
132                 global $_SESSION;
133
134                 $data['uploaded_file_name'] = basename($file_data['name']);
135                 $data['target_file_name'] = $file_data['uploaded_file_name'];
136                 if ($settings['random_name_len'] !== false) {
137                         do {
138                                 $data['target_file_name'] = '';
139                                 while (strlen($data['target_file_name']) < $settings['random_name_len'])
140                                         $data['target_file_name'] .= $settings['random_name_alphabet'][rand(0, strlen($settings['random_name_alphabet']) - 1)];
141                                 if ($settings['random_name_keep_type'])
142                                         $data['target_file_name'] .= '.' . pathinfo($data['uploaded_file_name'], PATHINFO_EXTENSION);
143                         } while (file_exists($data['target_file_name']));
144                 }
145                 $data['upload_target_file'] = $data['uploaddir'] . DIRECTORY_SEPARATOR . $data['target_file_name'];
146                 $data['tmp_name'] = $file_data['tmp_name'];
147
148                 if (file_exists($data['upload_target_file'])) {
149                         echo 'File name already exists' . "\n";
150                         return;
151                 }
152
153                 if (move_uploaded_file($data['tmp_name'], $data['upload_target_file'])) {
154                         if ($settings['allow_deletion'] || $settings['allow_private'])
155                                 $_SESSION['upload_user_files'][] = $data['target_file_name'];
156                         echo $settings['url'] .  $data['target_file_name'] . "\n";
157                 } else {
158                         echo 'Error: unable to upload the file.';
159                 }
160         }
161
162
163
164         if (isset($_FILES['file'])) {
165                 if ($settings['debug']) {
166                         // Displaying debug information
167                         echo '<h2>Debugging information: data</h2>';
168                         echo '<pre>';
169                         print_r($data);
170                         echo '</pre>';
171                         // Displaying debug information
172                         echo '<h2>Debugging information: file</h2>';
173                         echo '<pre>';
174                         print_r($_FILES);
175                         echo '</pre>';
176                 }
177
178                 if (is_array($_FILES['file'])) {
179                         $file_array = DiverseArray($_FILES['file']);
180                         foreach ($file_array as $file_data)
181                                 UploadFile($file_data);
182                 } else
183                         UploadFile($_FILES['file']);
184                 exit;
185         }
186
187         if (isset($_POST)) {
188                 if ($settings['allow_deletion'])
189                         if ($_POST['action'] === 'delete')
190                                 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
191                                         if (file_exists($_POST['target'])) {
192                                                 unlink($_POST['target']);
193                                                 echo 'File has been removed';
194                                                 exit;
195                                         }
196
197                 if ($settings['allow_private'])
198                         if ($_POST['action'] === 'privatetoggle')
199                                 if (in_array(substr($_POST['target'], 1), $_SESSION['upload_user_files']) || in_array($_POST['target'], $_SESSION['upload_user_files']))
200                                         if (file_exists($_POST['target'])) {
201                                                 if ($_POST['target'][0] === '.') {
202                                                         rename($_POST['target'], substr($_POST['target'], 1));
203                                                         echo 'File has been made visible';
204                                                 } else {
205                                                         rename($_POST['target'], '.' . $_POST['target']);
206                                                         echo 'File has been hidden';
207                                                 }
208                                                 exit;
209                                         }
210         }
211
212         function ListFiles ($dir, $exclude) {
213                 $file_array = array();
214                 $dh = opendir($dir);
215                         while (false !== ($filename = readdir($dh)))
216                                 if (is_file($filename) && !in_array($filename, $exclude))
217                                         $file_array[filemtime($filename)] = $filename;
218                 ksort($file_array);
219                 $file_array = array_reverse($file_array, true);
220                 return $file_array;
221         }
222
223 ?>
224 <html lang="en-GB">
225         <head>
226                 <meta charset="utf-8">
227                 <title>strace.club</title>
228                 <style media="screen">
229                         body {
230                                 background: #111;
231                                 margin: 0;
232                                 color: #ddd;
233                                 font-family: sans-serif;
234                         }
235
236                         body > h1 {
237                                 display: block;
238                                 background: rgba(255, 255, 255, 0.05);
239                                 padding: 8px 16px;
240                                 text-align: center;
241                                 margin: 0;
242                         }
243
244                         body > form {
245                                 display: block;
246                                 background: rgba(255, 255, 255, 0.075);
247                                 padding: 16px 16px;
248                                 margin: 0;
249                                 text-align: center;
250                         }
251
252                         body > p {
253                                 display: block;
254                                 background: rgba(255, 255, 255, 0.075);
255                                 padding: 4px 16px;
256                                 text-align: center;
257                                 max-width: 1000px;
258                                 margin: 16px auto 0 auto;
259                         }
260
261                         body > ul {
262                                 display: block;
263                                 padding: 0;
264                                 max-width: 1000px;
265                                 margin: 0 auto;
266                         }
267
268                         body > ul > li {
269                                 display: block;
270                                 margin: 0;
271                                 padding: 0;
272                         }
273
274                         body > ul > li > a {
275                                 display: block;
276                                 margin: 0 0 1px 0;
277                                 list-style: none;
278                                 background: rgba(255, 255, 255, 0.1);
279                                 padding: 8px 16px;
280                                 text-decoration: none;
281                                 color: inherit;
282                                 opacity: 0.5;
283                         }
284
285                         body > ul > li > a:hover {
286                                 opacity: 1;
287                         }
288
289                         body > ul > li > a:active {
290                                 opacity: 0.5;
291                         }
292
293                         body > ul > li > a > span {
294                                 float: right;
295                                 font-size: 90%;
296                         }
297
298                          body > ul > li > form {
299                                 display: inline-block;
300                                 padding: 0;
301                                 margin: 0;
302                         }
303
304                         body > ul > li.owned {
305                                 margin: 8px;
306                         }
307
308                         body > ul > li > form > button {
309                                 opacity: 0.5;
310                                 display: inline-block;
311                                 padding: 4px 16px;
312                                 margin: 0;
313                                 border: 0;
314                                 background: rgba(255, 255, 255, 0.1);
315                                 color: inherit;
316                         }
317
318                         body > ul > li > form > button:hover {
319                                 opacity: 1;
320                         }
321
322                         body > ul > li > form > button:active {
323                                 opacity: 0.5;
324                         }
325                 </style>
326         </head>
327         <body>
328                 <h1>strace.club</h1>
329                 <form action="<?= $data['scriptname'] ?>" method="POST" enctype="multipart/form-data" class="dropzone" id="simpleupload-form">
330                         Maximum upload size: <?php echo $data['max_upload_size']; ?><br />
331                         <input type="file" name="file[]" multiple required id="simpleupload-input"/>
332                 </form>
333                 <?php if ($settings['listfiles']) { ?>
334                         <p>Uploaded files:</p>
335                         <ul>
336                                 <?php
337                                         $file_array = ListFiles($settings['uploaddir'], array('.', '..', $data['scriptname']));
338                                         foreach ($file_array as $mtime => $filename) {
339                                                 $file_info = array();
340                                                 $file_owner = false;
341                                                 $file_private = $filename[0] === '.';
342
343                                                 if ($settings['listfiles_size'])
344                                                         $file_info[] = FormatSize(filesize($filename));
345
346                                                 if ($settings['listfiles_size'])
347                                                         $file_info[] = date($settings['listfiles_date_format'], $mtime);
348
349                                                 if ($settings['allow_deletion'] || $settings['allow_private'])
350                                                         if (in_array(substr($filename, 1), $_SESSION['upload_user_files']) || in_array($filename, $_SESSION['upload_user_files']))
351                                                                 $file_owner = true;
352
353                                                 $file_info = implode(', ', $file_info);
354
355                                                 if (strlen($file_info) > 0)
356                                                         $file_info = ' (' . $file_info . ')';
357
358                                                 $class = '';
359                                                 if ($file_owner)
360                                                         $class = 'owned';
361
362                                                 if (!$file_private || $file_owner) {
363                                                         echo "<li class=\"' . $class . '\">";
364
365                                                         echo "<a href=\"$filename\" target=\"_blank\">$filename<span>$file_info</span></a>";
366
367                                                         if ($file_owner) {
368                                                                 if ($settings['allow_deletion'])
369                                                                         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>';
370
371                                                                 if ($settings['allow_private'])
372                                                                         if ($file_private)
373                                                                                 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>';
374                                                                         else
375                                                                                 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>';
376                                                         }
377
378                                                         echo "</li>";
379                                                 }
380                                         }
381                                 ?>
382                         </ul>
383                 <?php } ?>
384                 <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>
385                 <script charset="utf-8">
386                         document.getElementById('simpleupload-input').onchange = function() {
387                                 document.getElementById('simpleupload-form').submit();
388                         };
389                 </script>
390         </body>
391 </html>