1d4265d07SGreg Roach<?php 2d4265d07SGreg Roach 3d4265d07SGreg Roach/** 4d4265d07SGreg Roach * webtrees: online genealogy 5*dec352c1SGreg Roach * Copyright (C) 2020 webtrees development team 6d4265d07SGreg Roach * This program is free software: you can redistribute it and/or modify 7d4265d07SGreg Roach * it under the terms of the GNU General Public License as published by 8d4265d07SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9d4265d07SGreg Roach * (at your option) any later version. 10d4265d07SGreg Roach * This program is distributed in the hope that it will be useful, 11d4265d07SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12d4265d07SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13d4265d07SGreg Roach * GNU General Public License for more details. 14d4265d07SGreg Roach * You should have received a copy of the GNU General Public License 15d4265d07SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16d4265d07SGreg Roach */ 17d4265d07SGreg Roach 18d4265d07SGreg Roachdeclare(strict_types=1); 19d4265d07SGreg Roach 20d4265d07SGreg Roachnamespace Fisharebest\Webtrees\Services; 21d4265d07SGreg Roach 22d4265d07SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 23d4265d07SGreg Roachuse Fisharebest\Webtrees\GedcomTag; 24d4265d07SGreg Roachuse Fisharebest\Webtrees\I18N; 25d4265d07SGreg Roachuse Fisharebest\Webtrees\Tree; 26d4265d07SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 2713aa75d8SGreg Roachuse Illuminate\Database\Query\Expression; 2813aa75d8SGreg Roachuse Illuminate\Support\Collection; 29d4265d07SGreg Roachuse InvalidArgumentException; 30a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 31d4265d07SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 32d4265d07SGreg Roachuse Psr\Http\Message\UploadedFileInterface; 33d4265d07SGreg Roachuse RuntimeException; 34d4265d07SGreg Roach 35d4265d07SGreg Roachuse function array_combine; 36d4265d07SGreg Roachuse function array_diff; 37d4265d07SGreg Roachuse function array_filter; 38d4265d07SGreg Roachuse function array_map; 39d4265d07SGreg Roachuse function assert; 4013aa75d8SGreg Roachuse function dirname; 41d501c45dSGreg Roachuse function ini_get; 42d4265d07SGreg Roachuse function intdiv; 43d501c45dSGreg Roachuse function min; 44d4265d07SGreg Roachuse function pathinfo; 4545fc2659SGreg Roachuse function preg_replace; 46d4265d07SGreg Roachuse function sha1; 47d4265d07SGreg Roachuse function sort; 48*dec352c1SGreg Roachuse function str_contains; 49d4265d07SGreg Roachuse function strtolower; 5045fc2659SGreg Roachuse function strtr; 51d501c45dSGreg Roachuse function substr; 52d4265d07SGreg Roachuse function trim; 53d4265d07SGreg Roach 54d4265d07SGreg Roachuse const PATHINFO_EXTENSION; 55d4265d07SGreg Roachuse const UPLOAD_ERR_OK; 56d4265d07SGreg Roach 57d4265d07SGreg Roach/** 58d4265d07SGreg Roach * Managing media files. 59d4265d07SGreg Roach */ 60d4265d07SGreg Roachclass MediaFileService 61d4265d07SGreg Roach{ 62d4265d07SGreg Roach public const EDIT_RESTRICTIONS = [ 63d4265d07SGreg Roach 'locked', 64d4265d07SGreg Roach ]; 65d4265d07SGreg Roach 66d4265d07SGreg Roach public const PRIVACY_RESTRICTIONS = [ 67d4265d07SGreg Roach 'none', 68d4265d07SGreg Roach 'privacy', 69d4265d07SGreg Roach 'confidential', 70d4265d07SGreg Roach ]; 71d4265d07SGreg Roach 7245fc2659SGreg Roach public const EXTENSION_TO_FORM = [ 7345fc2659SGreg Roach 'jpg' => 'jpeg', 7445fc2659SGreg Roach 'tif' => 'tiff', 7545fc2659SGreg Roach ]; 7645fc2659SGreg Roach 77d4265d07SGreg Roach /** 78d4265d07SGreg Roach * What is the largest file a user may upload? 79d4265d07SGreg Roach */ 80d4265d07SGreg Roach public function maxUploadFilesize(): string 81d4265d07SGreg Roach { 82d501c45dSGreg Roach $sizePostMax = $this->parseIniFileSize(ini_get('post_max_size')); 83d501c45dSGreg Roach $sizeUploadMax = $this->parseIniFileSize(ini_get('upload_max_filesize')); 84d501c45dSGreg Roach 85d501c45dSGreg Roach $bytes = min($sizePostMax, $sizeUploadMax); 86d4265d07SGreg Roach $kb = intdiv($bytes + 1023, 1024); 87d4265d07SGreg Roach 88d4265d07SGreg Roach return I18N::translate('%s KB', I18N::number($kb)); 89d4265d07SGreg Roach } 90d4265d07SGreg Roach 91d4265d07SGreg Roach /** 92d501c45dSGreg Roach * Returns the given size from an ini value in bytes. 93d501c45dSGreg Roach * 94284014f8SGreg Roach * @param string $size 95d501c45dSGreg Roach * 96d501c45dSGreg Roach * @return int 97d501c45dSGreg Roach */ 98284014f8SGreg Roach private function parseIniFileSize(string $size): int 99d501c45dSGreg Roach { 100d501c45dSGreg Roach $number = (int) $size; 101d501c45dSGreg Roach 102d501c45dSGreg Roach switch (substr($size, -1)) { 103d501c45dSGreg Roach case 'g': 104d501c45dSGreg Roach case 'G': 105fc26b4f6SGreg Roach return $number * 1073741824; 106d501c45dSGreg Roach case 'm': 107d501c45dSGreg Roach case 'M': 108fc26b4f6SGreg Roach return $number * 1048576; 109d501c45dSGreg Roach case 'k': 110d501c45dSGreg Roach case 'K': 111d501c45dSGreg Roach return $number * 1024; 112d501c45dSGreg Roach default: 113d501c45dSGreg Roach return $number; 114d501c45dSGreg Roach } 115d501c45dSGreg Roach } 116d501c45dSGreg Roach 117d501c45dSGreg Roach /** 118d4265d07SGreg Roach * A list of key/value options for media types. 119d4265d07SGreg Roach * 120d4265d07SGreg Roach * @param string $current 121d4265d07SGreg Roach * 122bfe98399SGreg Roach * @return array<string,string> 123d4265d07SGreg Roach */ 124d4265d07SGreg Roach public function mediaTypes($current = ''): array 125d4265d07SGreg Roach { 126d4265d07SGreg Roach $media_types = GedcomTag::getFileFormTypes(); 127d4265d07SGreg Roach 128d4265d07SGreg Roach $media_types = ['' => ''] + [$current => $current] + $media_types; 129d4265d07SGreg Roach 130d4265d07SGreg Roach return $media_types; 131d4265d07SGreg Roach } 132d4265d07SGreg Roach 133d4265d07SGreg Roach /** 134d4265d07SGreg Roach * A list of media files not already linked to a media object. 135d4265d07SGreg Roach * 136d4265d07SGreg Roach * @param Tree $tree 137a04bb9a2SGreg Roach * @param FilesystemInterface $data_filesystem 138d4265d07SGreg Roach * 139bfe98399SGreg Roach * @return array<string> 140d4265d07SGreg Roach */ 141a04bb9a2SGreg Roach public function unusedFiles(Tree $tree, FilesystemInterface $data_filesystem): array 142d4265d07SGreg Roach { 143d4265d07SGreg Roach $used_files = DB::table('media_file') 144d4265d07SGreg Roach ->where('m_file', '=', $tree->id()) 145d4265d07SGreg Roach ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') 146d4265d07SGreg Roach ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') 147d4265d07SGreg Roach ->pluck('multimedia_file_refn') 148d4265d07SGreg Roach ->all(); 149d4265d07SGreg Roach 150a04bb9a2SGreg Roach $disk_files = $tree->mediaFilesystem($data_filesystem)->listContents('', true); 151d4265d07SGreg Roach 152d4265d07SGreg Roach $disk_files = array_filter($disk_files, static function (array $item) { 153d4265d07SGreg Roach // Older versions of webtrees used a couple of special folders. 154d4265d07SGreg Roach return 155d4265d07SGreg Roach $item['type'] === 'file' && 156*dec352c1SGreg Roach !str_contains($item['path'], '/thumbs/') && 157*dec352c1SGreg Roach !str_contains($item['path'], '/watermarks/'); 158d4265d07SGreg Roach }); 159d4265d07SGreg Roach 160d4265d07SGreg Roach $disk_files = array_map(static function (array $item): string { 161d4265d07SGreg Roach return $item['path']; 162d4265d07SGreg Roach }, $disk_files); 163d4265d07SGreg Roach 164d4265d07SGreg Roach $unused_files = array_diff($disk_files, $used_files); 165d4265d07SGreg Roach 166d4265d07SGreg Roach sort($unused_files); 167d4265d07SGreg Roach 168d4265d07SGreg Roach return array_combine($unused_files, $unused_files); 169d4265d07SGreg Roach } 170d4265d07SGreg Roach 171d4265d07SGreg Roach /** 172d4265d07SGreg Roach * Store an uploaded file (or URL), either to be added to a media object 173d4265d07SGreg Roach * or to create a media object. 174d4265d07SGreg Roach * 175d4265d07SGreg Roach * @param ServerRequestInterface $request 176d4265d07SGreg Roach * 177d4265d07SGreg Roach * @return string The value to be stored in the 'FILE' field of the media object. 178d4265d07SGreg Roach */ 179d4265d07SGreg Roach public function uploadFile(ServerRequestInterface $request): string 180d4265d07SGreg Roach { 181d4265d07SGreg Roach $tree = $request->getAttribute('tree'); 182d4265d07SGreg Roach assert($tree instanceof Tree); 183d4265d07SGreg Roach 184a04bb9a2SGreg Roach $data_filesystem = $request->getAttribute('filesystem.data'); 185a04bb9a2SGreg Roach assert($data_filesystem instanceof FilesystemInterface); 186a04bb9a2SGreg Roach 187b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 188d4265d07SGreg Roach $file_location = $params['file_location']; 189d4265d07SGreg Roach 190d4265d07SGreg Roach switch ($file_location) { 191d4265d07SGreg Roach case 'url': 192d4265d07SGreg Roach $remote = $params['remote']; 193d4265d07SGreg Roach 194*dec352c1SGreg Roach if (str_contains($remote, '://')) { 195d4265d07SGreg Roach return $remote; 196d4265d07SGreg Roach } 197d4265d07SGreg Roach 198d4265d07SGreg Roach return ''; 199d4265d07SGreg Roach 200d4265d07SGreg Roach case 'unused': 201d4265d07SGreg Roach $unused = $params['unused']; 202d4265d07SGreg Roach 203a04bb9a2SGreg Roach if ($tree->mediaFilesystem($data_filesystem)->has($unused)) { 204d4265d07SGreg Roach return $unused; 205d4265d07SGreg Roach } 206d4265d07SGreg Roach 207d4265d07SGreg Roach return ''; 208d4265d07SGreg Roach 209d4265d07SGreg Roach case 'upload': 210d4265d07SGreg Roach default: 211d4265d07SGreg Roach $folder = $params['folder']; 212d4265d07SGreg Roach $auto = $params['auto']; 213d4265d07SGreg Roach $new_file = $params['new_file']; 214d4265d07SGreg Roach 215d4265d07SGreg Roach /** @var UploadedFileInterface|null $uploaded_file */ 216d4265d07SGreg Roach $uploaded_file = $request->getUploadedFiles()['file']; 217d4265d07SGreg Roach if ($uploaded_file === null || $uploaded_file->getError() !== UPLOAD_ERR_OK) { 218d4265d07SGreg Roach return ''; 219d4265d07SGreg Roach } 220d4265d07SGreg Roach 221d4265d07SGreg Roach // The filename 222*dec352c1SGreg Roach $new_file = strtr($new_file, ['\\' => '/']); 223*dec352c1SGreg Roach if ($new_file !== '' && !str_contains($new_file, '/')) { 224d4265d07SGreg Roach $file = $new_file; 225d4265d07SGreg Roach } else { 226d4265d07SGreg Roach $file = $uploaded_file->getClientFilename(); 227d4265d07SGreg Roach } 228d4265d07SGreg Roach 229d4265d07SGreg Roach // The folder 230*dec352c1SGreg Roach $folder = strtr($folder, ['\\' => '/']); 231d4265d07SGreg Roach $folder = trim($folder, '/'); 232d4265d07SGreg Roach if ($folder !== '') { 233d4265d07SGreg Roach $folder .= '/'; 234d4265d07SGreg Roach } 235d4265d07SGreg Roach 236d4265d07SGreg Roach // Generate a unique name for the file? 237a04bb9a2SGreg Roach if ($auto === '1' || $tree->mediaFilesystem($data_filesystem)->has($folder . $file)) { 238d4265d07SGreg Roach $folder = ''; 239d4265d07SGreg Roach $extension = pathinfo($uploaded_file->getClientFilename(), PATHINFO_EXTENSION); 240d4265d07SGreg Roach $file = sha1((string) $uploaded_file->getStream()) . '.' . $extension; 241d4265d07SGreg Roach } 242d4265d07SGreg Roach 243d4265d07SGreg Roach try { 2449ddec9bcSGreg Roach $tree->mediaFilesystem($data_filesystem)->putStream($folder . $file, $uploaded_file->getStream()->detach()); 245d4265d07SGreg Roach 246d4265d07SGreg Roach return $folder . $file; 247d4265d07SGreg Roach } catch (RuntimeException | InvalidArgumentException $ex) { 248d4265d07SGreg Roach FlashMessages::addMessage(I18N::translate('There was an error uploading your file.')); 249d4265d07SGreg Roach 250d4265d07SGreg Roach return ''; 251d4265d07SGreg Roach } 252d4265d07SGreg Roach } 253d4265d07SGreg Roach } 254d4265d07SGreg Roach 255d4265d07SGreg Roach /** 256d4265d07SGreg Roach * Convert the media file attributes into GEDCOM format. 257d4265d07SGreg Roach * 258d4265d07SGreg Roach * @param string $file 259d4265d07SGreg Roach * @param string $type 260d4265d07SGreg Roach * @param string $title 26145fc2659SGreg Roach * @param string $note 262d4265d07SGreg Roach * 263d4265d07SGreg Roach * @return string 264d4265d07SGreg Roach */ 26545fc2659SGreg Roach public function createMediaFileGedcom(string $file, string $type, string $title, string $note): string 266d4265d07SGreg Roach { 2672b44f6fbSGreg Roach // Tidy non-printing characters 26845fc2659SGreg Roach $type = trim(preg_replace('/\s+/', ' ', $type)); 26945fc2659SGreg Roach $title = trim(preg_replace('/\s+/', ' ', $title)); 270d4265d07SGreg Roach 271d4265d07SGreg Roach $gedcom = '1 FILE ' . $file; 27245fc2659SGreg Roach 27345fc2659SGreg Roach $format = strtolower(pathinfo($file, PATHINFO_EXTENSION)); 27445fc2659SGreg Roach $format = self::EXTENSION_TO_FORM[$format] ?? $format; 27545fc2659SGreg Roach 27645fc2659SGreg Roach if ($format !== '') { 27745fc2659SGreg Roach $gedcom .= "\n2 FORM " . $format; 27845fc2659SGreg Roach } elseif ($type !== '') { 27945fc2659SGreg Roach $gedcom .= "\n2 FORM"; 280d4265d07SGreg Roach } 28145fc2659SGreg Roach 28245fc2659SGreg Roach if ($type !== '') { 28345fc2659SGreg Roach $gedcom .= "\n3 TYPE " . $type; 28445fc2659SGreg Roach } 28545fc2659SGreg Roach 286d4265d07SGreg Roach if ($title !== '') { 287d4265d07SGreg Roach $gedcom .= "\n2 TITL " . $title; 288d4265d07SGreg Roach } 289d4265d07SGreg Roach 29045fc2659SGreg Roach if ($note !== '') { 29145fc2659SGreg Roach // Convert HTML line endings to GEDCOM continuations 29245fc2659SGreg Roach $gedcom .= "\n1 NOTE " . strtr($note, ["\r\n" => "\n2 CONT "]); 29345fc2659SGreg Roach } 29445fc2659SGreg Roach 295d4265d07SGreg Roach return $gedcom; 296d4265d07SGreg Roach } 29713aa75d8SGreg Roach 29813aa75d8SGreg Roach /** 29913aa75d8SGreg Roach * Fetch a list of all files on disk (in folders used by any tree). 30013aa75d8SGreg Roach * 30113aa75d8SGreg Roach * @param FilesystemInterface $data_filesystem Fileystem to search 30213aa75d8SGreg Roach * @param string $media_folder Root folder 30313aa75d8SGreg Roach * @param bool $subfolders Include subfolders 30413aa75d8SGreg Roach * 305b5c8fd7eSGreg Roach * @return Collection<string> 30613aa75d8SGreg Roach */ 30713aa75d8SGreg Roach public function allFilesOnDisk(FilesystemInterface $data_filesystem, string $media_folder, bool $subfolders): Collection 30813aa75d8SGreg Roach { 30913aa75d8SGreg Roach $array = $data_filesystem->listContents($media_folder, $subfolders); 31013aa75d8SGreg Roach 31113aa75d8SGreg Roach return Collection::make($array) 31213aa75d8SGreg Roach ->filter(static function (array $metadata): bool { 31313aa75d8SGreg Roach return 31413aa75d8SGreg Roach $metadata['type'] === 'file' && 315*dec352c1SGreg Roach !str_contains($metadata['path'], '/thumbs/') && 316*dec352c1SGreg Roach !str_contains($metadata['path'], '/watermark/'); 31713aa75d8SGreg Roach }) 31813aa75d8SGreg Roach ->map(static function (array $metadata): string { 31913aa75d8SGreg Roach return $metadata['path']; 32013aa75d8SGreg Roach }); 32113aa75d8SGreg Roach } 32213aa75d8SGreg Roach 32313aa75d8SGreg Roach /** 32413aa75d8SGreg Roach * Fetch a list of all files on in the database. 32513aa75d8SGreg Roach * 32613aa75d8SGreg Roach * @param string $media_folder Root folder 32713aa75d8SGreg Roach * @param bool $subfolders Include subfolders 32813aa75d8SGreg Roach * 329b5c8fd7eSGreg Roach * @return Collection<string> 33013aa75d8SGreg Roach */ 33113aa75d8SGreg Roach public function allFilesInDatabase(string $media_folder, bool $subfolders): Collection 33213aa75d8SGreg Roach { 33313aa75d8SGreg Roach $query = DB::table('media_file') 33413aa75d8SGreg Roach ->join('gedcom_setting', 'gedcom_id', '=', 'm_file') 33513aa75d8SGreg Roach ->where('setting_name', '=', 'MEDIA_DIRECTORY') 33613aa75d8SGreg Roach //->where('multimedia_file_refn', 'LIKE', '%/%') 33713aa75d8SGreg Roach ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') 33813aa75d8SGreg Roach ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') 33913aa75d8SGreg Roach ->where(new Expression('setting_value || multimedia_file_refn'), 'LIKE', $media_folder . '%') 34013aa75d8SGreg Roach ->select(new Expression('setting_value || multimedia_file_refn AS path')) 34113aa75d8SGreg Roach ->orderBy(new Expression('setting_value || multimedia_file_refn')); 34213aa75d8SGreg Roach 34313aa75d8SGreg Roach if (!$subfolders) { 34413aa75d8SGreg Roach $query->where(new Expression('setting_value || multimedia_file_refn'), 'NOT LIKE', $media_folder . '%/%'); 34513aa75d8SGreg Roach } 34613aa75d8SGreg Roach 34713aa75d8SGreg Roach return $query->pluck('path'); 34813aa75d8SGreg Roach } 34913aa75d8SGreg Roach 35013aa75d8SGreg Roach /** 35113aa75d8SGreg Roach * Generate a list of all folders in either the database or the filesystem. 35213aa75d8SGreg Roach * 35313aa75d8SGreg Roach * @param FilesystemInterface $data_filesystem 35413aa75d8SGreg Roach * 355b5c8fd7eSGreg Roach * @return Collection<string,string> 35613aa75d8SGreg Roach */ 35713aa75d8SGreg Roach public function allMediaFolders(FilesystemInterface $data_filesystem): Collection 35813aa75d8SGreg Roach { 35913aa75d8SGreg Roach $db_folders = DB::table('media_file') 36013aa75d8SGreg Roach ->join('gedcom_setting', 'gedcom_id', '=', 'm_file') 36113aa75d8SGreg Roach ->where('setting_name', '=', 'MEDIA_DIRECTORY') 36213aa75d8SGreg Roach ->where('multimedia_file_refn', 'NOT LIKE', 'http://%') 36313aa75d8SGreg Roach ->where('multimedia_file_refn', 'NOT LIKE', 'https://%') 36413aa75d8SGreg Roach ->select(new Expression('setting_value || multimedia_file_refn AS path')) 36513aa75d8SGreg Roach ->pluck('path') 36613aa75d8SGreg Roach ->map(static function (string $path): string { 36713aa75d8SGreg Roach return dirname($path) . '/'; 36813aa75d8SGreg Roach }); 36913aa75d8SGreg Roach 37013aa75d8SGreg Roach $media_roots = DB::table('gedcom_setting') 37113aa75d8SGreg Roach ->where('setting_name', '=', 'MEDIA_DIRECTORY') 3725d32b84fSGreg Roach ->where('gedcom_id', '>', '0') 37313aa75d8SGreg Roach ->pluck('setting_value') 3748c627a69SGreg Roach ->uniqueStrict(); 37513aa75d8SGreg Roach 37613aa75d8SGreg Roach $disk_folders = new Collection($media_roots); 37713aa75d8SGreg Roach 37813aa75d8SGreg Roach foreach ($media_roots as $media_folder) { 37913aa75d8SGreg Roach $tmp = Collection::make($data_filesystem->listContents($media_folder, true)) 38013aa75d8SGreg Roach ->filter(static function (array $metadata) { 38113aa75d8SGreg Roach return $metadata['type'] === 'dir'; 38213aa75d8SGreg Roach }) 38313aa75d8SGreg Roach ->map(static function (array $metadata): string { 38413aa75d8SGreg Roach return $metadata['path'] . '/'; 38513aa75d8SGreg Roach }) 38613aa75d8SGreg Roach ->filter(static function (string $dir): bool { 387*dec352c1SGreg Roach return !str_contains($dir, '/thumbs/') && !str_contains($dir, 'watermarks'); 38813aa75d8SGreg Roach }); 38913aa75d8SGreg Roach 39013aa75d8SGreg Roach $disk_folders = $disk_folders->concat($tmp); 39113aa75d8SGreg Roach } 39213aa75d8SGreg Roach 39313aa75d8SGreg Roach return $disk_folders->concat($db_folders) 3948c627a69SGreg Roach ->uniqueStrict() 39513aa75d8SGreg Roach ->mapWithKeys(static function (string $folder): array { 39613aa75d8SGreg Roach return [$folder => $folder]; 39713aa75d8SGreg Roach }); 39813aa75d8SGreg Roach } 399d4265d07SGreg Roach} 400