1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
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 */
16namespace Fisharebest\Webtrees;
17
18/**
19 * Defined in session.php
20 *
21 * @global Tree $WT_TREE
22 */
23global $WT_TREE;
24
25use Fisharebest\Webtrees\Controller\PageController;
26use Fisharebest\Webtrees\Functions\Functions;
27use Fisharebest\Webtrees\Query\QueryMedia;
28
29define('WT_SCRIPT_NAME', 'admin_media_upload.php');
30require './includes/session.php';
31
32$MEDIA_DIRECTORY = $WT_TREE->getPreference('MEDIA_DIRECTORY');
33
34$controller = new PageController;
35$controller
36    ->restrictAccess(Auth::isManager($WT_TREE))
37    ->setPageTitle(I18N::translate('Upload media files'));
38
39$action = Filter::post('action');
40
41if ($action == "upload") {
42    for ($i = 1; $i < 6; $i++) {
43        if (!empty($_FILES['mediafile' . $i]["name"]) || !empty($_FILES['thumbnail' . $i]["name"])) {
44            $folder = Filter::post('folder' . $i);
45
46            // Validate the media folder
47            $folderName = str_replace('\\', '/', $folder);
48            $folderName = trim($folderName, '/');
49            if ($folderName == '.') {
50                $folderName = '';
51            }
52            if ($folderName) {
53                $folderName .= '/';
54                // Not allowed to use “../”
55                if (strpos('/' . $folderName, '/../') !== false) {
56                    FlashMessages::addMessage('Folder names are not allowed to include “../”');
57                    break;
58                }
59            }
60
61            // Make sure the media folder exists
62            if (!is_dir(WT_DATA_DIR . $MEDIA_DIRECTORY)) {
63                if (File::mkdir(WT_DATA_DIR . $MEDIA_DIRECTORY)) {
64                    FlashMessages::addMessage(I18N::translate('The folder %s has been created.', Html::filename(WT_DATA_DIR . $MEDIA_DIRECTORY)));
65                } else {
66                    FlashMessages::addMessage(I18N::translate('The folder %s does not exist, and it could not be created.', Html::filename(WT_DATA_DIR . $MEDIA_DIRECTORY)), 'danger');
67                    break;
68                }
69            }
70
71            // Managers can create new media paths (subfolders). Users must use existing folders.
72            if ($folderName && !is_dir(WT_DATA_DIR . $MEDIA_DIRECTORY . $folderName)) {
73                if (Auth::isManager($WT_TREE)) {
74                    if (File::mkdir(WT_DATA_DIR . $MEDIA_DIRECTORY . $folderName)) {
75                        FlashMessages::addMessage(I18N::translate('The folder %s has been created.', Html::filename(WT_DATA_DIR . $MEDIA_DIRECTORY . $folderName)));
76                    } else {
77                        FlashMessages::addMessage(I18N::translate('The folder %s does not exist, and it could not be created.', Html::filename(WT_DATA_DIR . $MEDIA_DIRECTORY . $folderName)), 'danger');
78                        break;
79                    }
80                } else {
81                    // Regular users should not have seen this option - so no need for an error message.
82                    break;
83                }
84            }
85
86            // The media folder exists. Now create a thumbnail folder to match it.
87            if (!is_dir(WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $folderName)) {
88                if (!File::mkdir(WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $folderName)) {
89                    FlashMessages::addMessage(I18N::translate('The folder %s does not exist, and it could not be created.', Html::filename(WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $folderName)));
90                    break;
91                }
92            }
93
94            // A thumbnail file with no main image?
95            if (!empty($_FILES['thumbnail' . $i]['name']) && empty($_FILES['mediafile' . $i]['name'])) {
96                // Assume the user used the wrong field, and treat this as a main image
97                $_FILES['mediafile' . $i] = $_FILES['thumbnail' . $i];
98                unset($_FILES['thumbnail' . $i]);
99            }
100
101            // Thumbnail files must contain images.
102            if (!empty($_FILES['thumbnail' . $i]['name']) && !preg_match('/^image\/(png|gif|jpeg)/', $_FILES['thumbnail' . $i]['type'])) {
103                FlashMessages::addMessage(I18N::translate('Thumbnail files must contain images.'));
104                break;
105            }
106
107            // User-specified filename?
108            $filename = Filter::post('filename' . $i);
109            // Use the name of the uploaded file?
110            if (!$filename && !empty($_FILES['mediafile' . $i]['name'])) {
111                $filename = $_FILES['mediafile' . $i]['name'];
112            }
113
114            // Validate the media path and filename
115            if (preg_match('/([\/\\\\<>])/', $filename, $match)) {
116                // Local media files cannot contain certain special characters
117                FlashMessages::addMessage(I18N::translate('Filenames are not allowed to contain the character “%s”.', $match[1]));
118                $filename = '';
119                break;
120            } elseif (preg_match('/(\.(php|pl|cgi|bash|sh|bat|exe|com|htm|html|shtml))$/i', $filename, $match)) {
121                // Do not allow obvious script files.
122                FlashMessages::addMessage(I18N::translate('Filenames are not allowed to have the extension “%s”.', $match[1]));
123                $filename = '';
124                break;
125            } elseif (!$filename) {
126                FlashMessages::addMessage(I18N::translate('No media file was provided.'));
127                break;
128            } else {
129                $fileName = $filename;
130            }
131
132            // Now copy the file to the correct location.
133            if (!empty($_FILES['mediafile' . $i]['name'])) {
134                $serverFileName = WT_DATA_DIR . $MEDIA_DIRECTORY . $folderName . $fileName;
135                if (file_exists($serverFileName)) {
136                    FlashMessages::addMessage(I18N::translate('The file %s already exists. Use another filename.', $folderName . $fileName));
137                    $filename = '';
138                    break;
139                }
140                if (move_uploaded_file($_FILES['mediafile' . $i]['tmp_name'], $serverFileName)) {
141                    FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($serverFileName)));
142                    Log::addMediaLog('Media file ' . $serverFileName . ' uploaded');
143                } else {
144                    FlashMessages::addMessage(
145                        I18N::translate('There was an error uploading your file.') .
146                        '<br>' .
147                        Functions::fileUploadErrorText($_FILES['mediafile' . $i]['error'])
148                    );
149                    $filename = '';
150                    break;
151                }
152
153                // Now copy the (optional thumbnail)
154                if (!empty($_FILES['thumbnail' . $i]['name']) && preg_match('/^image\/(png|gif|jpeg)/', $_FILES['thumbnail' . $i]['type'], $match)) {
155                    $extension      = $match[1];
156                    $thumbFile      = preg_replace('/\.[a-z0-9]{3,5}$/', '.' . $extension, $fileName);
157                    $serverFileName = WT_DATA_DIR . $MEDIA_DIRECTORY . 'thumbs/' . $folderName . $thumbFile;
158                    if (move_uploaded_file($_FILES['thumbnail' . $i]['tmp_name'], $serverFileName)) {
159                        FlashMessages::addMessage(I18N::translate('The file %s has been uploaded.', Html::filename($serverFileName)));
160                        Log::addMediaLog('Thumbnail file ' . $serverFileName . ' uploaded');
161                    }
162                }
163            }
164        }
165    }
166}
167
168$controller->pageHeader();
169
170$mediaFolders = QueryMedia::folderListAll();
171
172// Determine file size limit
173$filesize = ini_get('upload_max_filesize');
174if (empty($filesize)) {
175    $filesize = "2M";
176}
177
178?>
179<ol class="breadcrumb small">
180    <li><a href="admin.php"><?php echo I18N::translate('Control panel'); ?></a></li>
181    <li class="active"><?php echo $controller->getPageTitle(); ?></li>
182</ol>
183
184<h1><?php echo $controller->getPageTitle(); ?></h1>
185
186<p>
187    <?php echo I18N::translate('Upload one or more media files from your local computer. Media files can be pictures, video, audio, or other formats.'); ?>
188    <?php echo I18N::translate('Maximum upload size: '), $filesize; ?>
189</p>
190
191<?php
192
193// Print the form
194echo '<form name="uploadmedia" enctype="multipart/form-data" method="post" action="', WT_SCRIPT_NAME, '">';
195echo '<input type="hidden" name="action" value="upload">';
196
197// Print 5 forms for uploading images
198for ($i = 1; $i < 6; $i++) {
199    echo '<table class="upload_media">';
200    echo '<tr><th>', I18N::translate('Media file'), ' ', $i, '</th></tr>';
201    echo '<tr><td>';
202    echo I18N::translate('Media file to upload');
203    echo '</td>';
204    echo '<td>';
205    echo '<input name="mediafile', $i, '" type="file" size="40">';
206    echo '</td></tr>';
207    echo '<tr><td>';
208    echo I18N::translate('Thumbnail to upload');
209    echo '</td>';
210    echo '<td>';
211    echo '<input name="thumbnail', $i, '" type="file" size="40">';
212    if ($i === 1) {
213        echo '<p class="small text-muted">', I18N::translate('Choose the thumbnail image that you want to upload. Although thumbnails can be generated automatically for images, you may wish to generate your own thumbnail, especially for other media types. For example, you can provide a still image from a video, or a photograph of the individual who made an audio recording.'), '</p>';
214    }
215    echo '</td></tr>';
216
217    if (Auth::isManager($WT_TREE)) {
218        echo '<tr><td>';
219        echo I18N::translate('Filename on server');
220        echo '</td>';
221        echo '<td>';
222        echo '<input name="filename', $i, '" type="text" size="40">';
223        if ($i == 1) {
224            echo '<p class="small text-muted">', I18N::translate('Do not change to keep original filename.'), "</p>";
225            echo '<p class="small text-muted">', I18N::translate('The media file you are uploading can be, and probably should be, named differently on the server than it is on your local computer. This is so because often the local filename has meaning to you but is much less meaningful to others visiting this website. Consider also the possibility that you and someone else both try to upload different files called “granny.jpg“.<br><br>In this field, you specify the new name of the file you are uploading. The name you enter here will also be used to name the thumbnail, which can be uploaded separately or generated automatically. You do not need to enter the filename extension (jpg, gif, pdf, doc, etc.)<br><br>Leave this field blank to keep the original name of the file you have uploaded from your local computer.'), '</p>';
226        }
227        echo '</td></tr>';
228    } else {
229        echo '<tr style="display:none;"><td><input type="hidden" name="filename', $i, '" value=""></td></tr>';
230    }
231
232    if (Auth::isManager($WT_TREE)) {
233        echo '<tr><td>';
234        echo I18N::translate('Folder name on server');
235        echo '</td>';
236        echo '<td>';
237
238        echo '<select name="folder_list', $i, '" onchange="document.uploadmedia.folder', $i, '.value=this.options[this.selectedIndex].value;">';
239        echo '<option';
240        echo ' value="/"> ', I18N::translate('&lt;select&gt;'), ' </option>';
241        if (Auth::isAdmin()) {
242            echo '<option value="other" disabled>', I18N::translate('Other folder… please type in'), "</option>";
243        }
244        foreach ($mediaFolders as $f) {
245            echo '<option value="', Filter::escapeHtml($f), '">', Filter::escapeHtml($f), "</option>";
246        }
247        echo "</select>";
248        if (Auth::isAdmin()) {
249            echo '<br><input name="folder', $i, '" type="text" size="40" value="">';
250        } else {
251            echo '<input name="folder', $i, '" type="hidden" value="">';
252        }
253        if ($i === 1) {
254            echo '<p class="small text-muted">', I18N::translate('If you have a large number of media files, you can organize them into folders and subfolders.'), '</p>';
255        }
256        echo '</td></tr>';
257    } else {
258        echo '<tr style="display:none;"><td><input name="folder', $i, '" type="hidden" value=""></td></tr>';
259    }
260    echo '</table>';
261}
262// Print the Submit button for uploading the media
263echo '<input type="submit" value="', /* I18N: A button label. */ I18N::translate('upload'), '">';
264echo '</form>';
265