1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Services; 21 22use Fisharebest\Webtrees\FlashMessages; 23use Fisharebest\Webtrees\GedcomTag; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Tree; 26use Illuminate\Database\Capsule\Manager as DB; 27use Illuminate\Database\Query\Expression; 28use Illuminate\Support\Collection; 29use InvalidArgumentException; 30use League\Flysystem\FilesystemInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Message\UploadedFileInterface; 33use RuntimeException; 34 35use function array_combine; 36use function array_diff; 37use function array_filter; 38use function array_map; 39use function assert; 40use function dirname; 41use function ini_get; 42use function intdiv; 43use function min; 44use function pathinfo; 45use function preg_replace; 46use function sha1; 47use function sort; 48use function str_replace; 49use function strpos; 50use function strtolower; 51use function strtr; 52use function substr; 53use function trim; 54 55use const PATHINFO_EXTENSION; 56use const UPLOAD_ERR_OK; 57 58/** 59 * Managing media files. 60 */ 61class MediaFileService 62{ 63 public const EDIT_RESTRICTIONS = [ 64 'locked', 65 ]; 66 67 public const PRIVACY_RESTRICTIONS = [ 68 'none', 69 'privacy', 70 'confidential', 71 ]; 72 73 public const EXTENSION_TO_FORM = [ 74 'jpg' => 'jpeg', 75 'tif' => 'tiff', 76 ]; 77 78 /** 79 * What is the largest file a user may upload? 80 */ 81 public function maxUploadFilesize(): string 82 { 83 $sizePostMax = $this->parseIniFileSize(ini_get('post_max_size')); 84 $sizeUploadMax = $this->parseIniFileSize(ini_get('upload_max_filesize')); 85 86 $bytes = min($sizePostMax, $sizeUploadMax); 87 $kb = intdiv($bytes + 1023, 1024); 88 89 return I18N::translate('%s KB', I18N::number($kb)); 90 } 91 92 /** 93 * Returns the given size from an ini value in bytes. 94 * 95 * @param string $size 96 * 97 * @return int 98 */ 99 private function parseIniFileSize(string $size): int 100 { 101 $number = (int) $size; 102 103 switch (substr($size, -1)) { 104 case 'g': 105 case 'G': 106 return $number * 1073741824; 107 case 'm': 108 case 'M': 109 return $number * 1048576; 110 case 'k': 111 case 'K': 112 return $number * 1024; 113 default: 114 return $number; 115 } 116 } 117 118 /** 119 * A list of key/value options for media types. 120 * 121 * @param string $current 122 * 123 * @return array<string,string> 124 */ 125 public function mediaTypes($current = ''): array 126 { 127 $media_types = GedcomTag::getFileFormTypes(); 128 129 $media_types = ['' => ''] + [$current => $current] + $media_types; 130 131 return $media_types; 132 } 133 134 /** 135 * A list of media files not already linked to a media object. 136 * 137 * @param Tree $tree 138 * @param FilesystemInterface $data_filesystem 139 * 140 * @return array<string> 141 */ 142 public function unusedFiles(Tree $tree, FilesystemInterface $data_filesystem): array 143 { 144 $used_files = DB::table('media_file') 145 ->where('m_file', '=', $tree->id()) 146 ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') 147 ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') 148 ->pluck('multimedia_file_refn') 149 ->all(); 150 151 $disk_files = $tree->mediaFilesystem($data_filesystem)->listContents('', true); 152 153 $disk_files = array_filter($disk_files, static function (array $item) { 154 // Older versions of webtrees used a couple of special folders. 155 return 156 $item['type'] === 'file' && 157 strpos($item['path'], '/thumbs/') === false && 158 strpos($item['path'], '/watermarks/') === false; 159 }); 160 161 $disk_files = array_map(static function (array $item): string { 162 return $item['path']; 163 }, $disk_files); 164 165 $unused_files = array_diff($disk_files, $used_files); 166 167 sort($unused_files); 168 169 return array_combine($unused_files, $unused_files); 170 } 171 172 /** 173 * Store an uploaded file (or URL), either to be added to a media object 174 * or to create a media object. 175 * 176 * @param ServerRequestInterface $request 177 * 178 * @return string The value to be stored in the 'FILE' field of the media object. 179 */ 180 public function uploadFile(ServerRequestInterface $request): string 181 { 182 $tree = $request->getAttribute('tree'); 183 assert($tree instanceof Tree); 184 185 $data_filesystem = $request->getAttribute('filesystem.data'); 186 assert($data_filesystem instanceof FilesystemInterface); 187 188 $params = (array) $request->getParsedBody(); 189 $file_location = $params['file_location']; 190 191 switch ($file_location) { 192 case 'url': 193 $remote = $params['remote']; 194 195 if (strpos($remote, '://') !== false) { 196 return $remote; 197 } 198 199 return ''; 200 201 case 'unused': 202 $unused = $params['unused']; 203 204 if ($tree->mediaFilesystem($data_filesystem)->has($unused)) { 205 return $unused; 206 } 207 208 return ''; 209 210 case 'upload': 211 default: 212 $folder = $params['folder']; 213 $auto = $params['auto']; 214 $new_file = $params['new_file']; 215 216 /** @var UploadedFileInterface|null $uploaded_file */ 217 $uploaded_file = $request->getUploadedFiles()['file']; 218 if ($uploaded_file === null || $uploaded_file->getError() !== UPLOAD_ERR_OK) { 219 return ''; 220 } 221 222 // The filename 223 $new_file = str_replace('\\', '/', $new_file); 224 if ($new_file !== '' && strpos($new_file, '/') === false) { 225 $file = $new_file; 226 } else { 227 $file = $uploaded_file->getClientFilename(); 228 } 229 230 // The folder 231 $folder = str_replace('\\', '/', $folder); 232 $folder = trim($folder, '/'); 233 if ($folder !== '') { 234 $folder .= '/'; 235 } 236 237 // Generate a unique name for the file? 238 if ($auto === '1' || $tree->mediaFilesystem($data_filesystem)->has($folder . $file)) { 239 $folder = ''; 240 $extension = pathinfo($uploaded_file->getClientFilename(), PATHINFO_EXTENSION); 241 $file = sha1((string) $uploaded_file->getStream()) . '.' . $extension; 242 } 243 244 try { 245 $tree->mediaFilesystem($data_filesystem)->putStream($folder . $file, $uploaded_file->getStream()->detach()); 246 247 return $folder . $file; 248 } catch (RuntimeException | InvalidArgumentException $ex) { 249 FlashMessages::addMessage(I18N::translate('There was an error uploading your file.')); 250 251 return ''; 252 } 253 } 254 } 255 256 /** 257 * Convert the media file attributes into GEDCOM format. 258 * 259 * @param string $file 260 * @param string $type 261 * @param string $title 262 * @param string $note 263 * 264 * @return string 265 */ 266 public function createMediaFileGedcom(string $file, string $type, string $title, string $note): string 267 { 268 // Tidy non-printing characters 269 $type = trim(preg_replace('/\s+/', ' ', $type)); 270 $title = trim(preg_replace('/\s+/', ' ', $title)); 271 272 $gedcom = '1 FILE ' . $file; 273 274 $format = strtolower(pathinfo($file, PATHINFO_EXTENSION)); 275 $format = self::EXTENSION_TO_FORM[$format] ?? $format; 276 277 if ($format !== '') { 278 $gedcom .= "\n2 FORM " . $format; 279 } elseif ($type !== '') { 280 $gedcom .= "\n2 FORM"; 281 } 282 283 if ($type !== '') { 284 $gedcom .= "\n3 TYPE " . $type; 285 } 286 287 if ($title !== '') { 288 $gedcom .= "\n2 TITL " . $title; 289 } 290 291 if ($note !== '') { 292 // Convert HTML line endings to GEDCOM continuations 293 $gedcom .= "\n1 NOTE " . strtr($note, ["\r\n" => "\n2 CONT "]); 294 } 295 296 return $gedcom; 297 } 298 299 /** 300 * Fetch a list of all files on disk (in folders used by any tree). 301 * 302 * @param FilesystemInterface $data_filesystem Fileystem to search 303 * @param string $media_folder Root folder 304 * @param bool $subfolders Include subfolders 305 * 306 * @return Collection<string> 307 */ 308 public function allFilesOnDisk(FilesystemInterface $data_filesystem, string $media_folder, bool $subfolders): Collection 309 { 310 $array = $data_filesystem->listContents($media_folder, $subfolders); 311 312 return Collection::make($array) 313 ->filter(static function (array $metadata): bool { 314 return 315 $metadata['type'] === 'file' && 316 strpos($metadata['path'], '/thumbs/') === false && 317 strpos($metadata['path'], '/watermark/') === false; 318 }) 319 ->map(static function (array $metadata): string { 320 return $metadata['path']; 321 }); 322 } 323 324 /** 325 * Fetch a list of all files on in the database. 326 * 327 * @param string $media_folder Root folder 328 * @param bool $subfolders Include subfolders 329 * 330 * @return Collection<string> 331 */ 332 public function allFilesInDatabase(string $media_folder, bool $subfolders): Collection 333 { 334 $query = DB::table('media_file') 335 ->join('gedcom_setting', 'gedcom_id', '=', 'm_file') 336 ->where('setting_name', '=', 'MEDIA_DIRECTORY') 337 //->where('multimedia_file_refn', 'LIKE', '%/%') 338 ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') 339 ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') 340 ->where(new Expression('setting_value || multimedia_file_refn'), 'LIKE', $media_folder . '%') 341 ->select(new Expression('setting_value || multimedia_file_refn AS path')) 342 ->orderBy(new Expression('setting_value || multimedia_file_refn')); 343 344 if (!$subfolders) { 345 $query->where(new Expression('setting_value || multimedia_file_refn'), 'NOT LIKE', $media_folder . '%/%'); 346 } 347 348 return $query->pluck('path'); 349 } 350 351 /** 352 * Generate a list of all folders in either the database or the filesystem. 353 * 354 * @param FilesystemInterface $data_filesystem 355 * 356 * @return Collection<string,string> 357 */ 358 public function allMediaFolders(FilesystemInterface $data_filesystem): Collection 359 { 360 $db_folders = DB::table('media_file') 361 ->join('gedcom_setting', 'gedcom_id', '=', 'm_file') 362 ->where('setting_name', '=', 'MEDIA_DIRECTORY') 363 ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') 364 ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') 365 ->select(new Expression('setting_value || multimedia_file_refn AS path')) 366 ->pluck('path') 367 ->map(static function (string $path): string { 368 return dirname($path) . '/'; 369 }); 370 371 $media_roots = DB::table('gedcom_setting') 372 ->where('setting_name', '=', 'MEDIA_DIRECTORY') 373 ->where('gedcom_id', '>', '0') 374 ->pluck('setting_value') 375 ->uniqueStrict(); 376 377 $disk_folders = new Collection($media_roots); 378 379 foreach ($media_roots as $media_folder) { 380 $tmp = Collection::make($data_filesystem->listContents($media_folder, true)) 381 ->filter(static function (array $metadata) { 382 return $metadata['type'] === 'dir'; 383 }) 384 ->map(static function (array $metadata): string { 385 return $metadata['path'] . '/'; 386 }) 387 ->filter(static function (string $dir): bool { 388 return strpos($dir, '/thumbs/') === false && strpos($dir, 'watermarks') === false; 389 }); 390 391 $disk_folders = $disk_folders->concat($tmp); 392 } 393 394 return $disk_folders->concat($db_folders) 395 ->uniqueStrict() 396 ->mapWithKeys(static function (string $folder): array { 397 return [$folder => $folder]; 398 }); 399 } 400} 401