xref: /webtrees/app/Services/MediaFileService.php (revision b5c8fd7e66957665381ee23f19cf39bda22bc768)
1d4265d07SGreg Roach<?php
2d4265d07SGreg Roach
3d4265d07SGreg Roach/**
4d4265d07SGreg Roach * webtrees: online genealogy
5d4265d07SGreg Roach * Copyright (C) 2019 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 intval;
44d501c45dSGreg Roachuse function ltrim;
45d501c45dSGreg Roachuse function min;
46d4265d07SGreg Roachuse function pathinfo;
47d4265d07SGreg Roachuse function preg_match;
48d4265d07SGreg Roachuse function sha1;
49d4265d07SGreg Roachuse function sort;
50d4265d07SGreg Roachuse function str_replace;
51d4265d07SGreg Roachuse function strpos;
52d4265d07SGreg Roachuse function strtolower;
53d501c45dSGreg Roachuse function substr;
54d4265d07SGreg Roachuse function trim;
55d4265d07SGreg Roach
56d4265d07SGreg Roachuse const PATHINFO_EXTENSION;
57d501c45dSGreg Roachuse const PHP_INT_MAX;
58d4265d07SGreg Roachuse const UPLOAD_ERR_OK;
59d4265d07SGreg Roach
60d4265d07SGreg Roach/**
61d4265d07SGreg Roach * Managing media files.
62d4265d07SGreg Roach */
63d4265d07SGreg Roachclass MediaFileService
64d4265d07SGreg Roach{
65d4265d07SGreg Roach    public const EDIT_RESTRICTIONS = [
66d4265d07SGreg Roach        'locked',
67d4265d07SGreg Roach    ];
68d4265d07SGreg Roach
69d4265d07SGreg Roach    public const PRIVACY_RESTRICTIONS = [
70d4265d07SGreg Roach        'none',
71d4265d07SGreg Roach        'privacy',
72d4265d07SGreg Roach        'confidential',
73d4265d07SGreg Roach    ];
74d4265d07SGreg Roach
75d4265d07SGreg Roach    /**
76d4265d07SGreg Roach     * What is the largest file a user may upload?
77d4265d07SGreg Roach     */
78d4265d07SGreg Roach    public function maxUploadFilesize(): string
79d4265d07SGreg Roach    {
80d501c45dSGreg Roach        $sizePostMax = $this->parseIniFileSize(ini_get('post_max_size'));
81d501c45dSGreg Roach        $sizeUploadMax = $this->parseIniFileSize(ini_get('upload_max_filesize'));
82d501c45dSGreg Roach
83d501c45dSGreg Roach        $bytes =  min($sizePostMax, $sizeUploadMax);
84d4265d07SGreg Roach        $kb    = intdiv($bytes + 1023, 1024);
85d4265d07SGreg Roach
86d4265d07SGreg Roach        return I18N::translate('%s KB', I18N::number($kb));
87d4265d07SGreg Roach    }
88d4265d07SGreg Roach
89d4265d07SGreg Roach    /**
90d501c45dSGreg Roach     * Returns the given size from an ini value in bytes.
91d501c45dSGreg Roach     *
92284014f8SGreg Roach     * @param string $size
93d501c45dSGreg Roach     *
94d501c45dSGreg Roach     * @return int
95d501c45dSGreg Roach     */
96284014f8SGreg Roach    private function parseIniFileSize(string $size): int
97d501c45dSGreg Roach    {
98d501c45dSGreg Roach        $number = (int) $size;
99d501c45dSGreg Roach
100d501c45dSGreg Roach        switch (substr($size, -1)) {
101d501c45dSGreg Roach            case 't':
102d501c45dSGreg Roach            case 'T':
103d501c45dSGreg Roach                return $number * 1024 ** 4;
104d501c45dSGreg Roach            case 'g':
105d501c45dSGreg Roach            case 'G':
106d501c45dSGreg Roach                return $number * 1024 ** 3;
107d501c45dSGreg Roach            case 'm':
108d501c45dSGreg Roach            case 'M':
109d501c45dSGreg Roach                return $number * 1024 ** 2;
110d501c45dSGreg Roach            case 'k':
111d501c45dSGreg Roach            case 'K':
112d501c45dSGreg Roach                return $number * 1024;
113d501c45dSGreg Roach            default:
114d501c45dSGreg Roach                return $number;
115d501c45dSGreg Roach        }
116d501c45dSGreg Roach    }
117d501c45dSGreg Roach
118d501c45dSGreg Roach    /**
119d4265d07SGreg Roach     * A list of key/value options for media types.
120d4265d07SGreg Roach     *
121d4265d07SGreg Roach     * @param string $current
122d4265d07SGreg Roach     *
123d4265d07SGreg Roach     * @return array
124d4265d07SGreg Roach     */
125d4265d07SGreg Roach    public function mediaTypes($current = ''): array
126d4265d07SGreg Roach    {
127d4265d07SGreg Roach        $media_types = GedcomTag::getFileFormTypes();
128d4265d07SGreg Roach
129d4265d07SGreg Roach        $media_types = ['' => ''] + [$current => $current] + $media_types;
130d4265d07SGreg Roach
131d4265d07SGreg Roach        return $media_types;
132d4265d07SGreg Roach    }
133d4265d07SGreg Roach
134d4265d07SGreg Roach    /**
135d4265d07SGreg Roach     * A list of media files not already linked to a media object.
136d4265d07SGreg Roach     *
137d4265d07SGreg Roach     * @param Tree                $tree
138a04bb9a2SGreg Roach     * @param FilesystemInterface $data_filesystem
139d4265d07SGreg Roach     *
140d4265d07SGreg Roach     * @return array
141d4265d07SGreg Roach     */
142a04bb9a2SGreg Roach    public function unusedFiles(Tree $tree, FilesystemInterface $data_filesystem): array
143d4265d07SGreg Roach    {
144d4265d07SGreg Roach        $used_files = DB::table('media_file')
145d4265d07SGreg Roach            ->where('m_file', '=', $tree->id())
146d4265d07SGreg Roach            ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
147d4265d07SGreg Roach            ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
148d4265d07SGreg Roach            ->pluck('multimedia_file_refn')
149d4265d07SGreg Roach            ->all();
150d4265d07SGreg Roach
151a04bb9a2SGreg Roach        $disk_files = $tree->mediaFilesystem($data_filesystem)->listContents('', true);
152d4265d07SGreg Roach
153d4265d07SGreg Roach        $disk_files = array_filter($disk_files, static function (array $item) {
154d4265d07SGreg Roach            // Older versions of webtrees used a couple of special folders.
155d4265d07SGreg Roach            return
156d4265d07SGreg Roach                $item['type'] === 'file' &&
157d4265d07SGreg Roach                strpos($item['path'], '/thumbs/') === false &&
158d4265d07SGreg Roach                strpos($item['path'], '/watermarks/') === false;
159d4265d07SGreg Roach        });
160d4265d07SGreg Roach
161d4265d07SGreg Roach        $disk_files = array_map(static function (array $item): string {
162d4265d07SGreg Roach            return $item['path'];
163d4265d07SGreg Roach        }, $disk_files);
164d4265d07SGreg Roach
165d4265d07SGreg Roach        $unused_files = array_diff($disk_files, $used_files);
166d4265d07SGreg Roach
167d4265d07SGreg Roach        sort($unused_files);
168d4265d07SGreg Roach
169d4265d07SGreg Roach        return array_combine($unused_files, $unused_files);
170d4265d07SGreg Roach    }
171d4265d07SGreg Roach
172d4265d07SGreg Roach    /**
173d4265d07SGreg Roach     * Store an uploaded file (or URL), either to be added to a media object
174d4265d07SGreg Roach     * or to create a media object.
175d4265d07SGreg Roach     *
176d4265d07SGreg Roach     * @param ServerRequestInterface $request
177d4265d07SGreg Roach     *
178d4265d07SGreg Roach     * @return string The value to be stored in the 'FILE' field of the media object.
179d4265d07SGreg Roach     */
180d4265d07SGreg Roach    public function uploadFile(ServerRequestInterface $request): string
181d4265d07SGreg Roach    {
182d4265d07SGreg Roach        $tree = $request->getAttribute('tree');
183d4265d07SGreg Roach        assert($tree instanceof Tree);
184d4265d07SGreg Roach
185a04bb9a2SGreg Roach        $data_filesystem = $request->getAttribute('filesystem.data');
186a04bb9a2SGreg Roach        assert($data_filesystem instanceof FilesystemInterface);
187a04bb9a2SGreg Roach
188d4265d07SGreg Roach        $params        = $request->getParsedBody();
189d4265d07SGreg Roach        $file_location = $params['file_location'];
190d4265d07SGreg Roach
191d4265d07SGreg Roach        switch ($file_location) {
192d4265d07SGreg Roach            case 'url':
193d4265d07SGreg Roach                $remote = $params['remote'];
194d4265d07SGreg Roach
195d4265d07SGreg Roach                if (strpos($remote, '://') !== false) {
196d4265d07SGreg Roach                    return $remote;
197d4265d07SGreg Roach                }
198d4265d07SGreg Roach
199d4265d07SGreg Roach                return '';
200d4265d07SGreg Roach
201d4265d07SGreg Roach            case 'unused':
202d4265d07SGreg Roach                $unused = $params['unused'];
203d4265d07SGreg Roach
204a04bb9a2SGreg Roach                if ($tree->mediaFilesystem($data_filesystem)->has($unused)) {
205d4265d07SGreg Roach                    return $unused;
206d4265d07SGreg Roach                }
207d4265d07SGreg Roach
208d4265d07SGreg Roach                return '';
209d4265d07SGreg Roach
210d4265d07SGreg Roach            case 'upload':
211d4265d07SGreg Roach            default:
212d4265d07SGreg Roach                $folder   = $params['folder'];
213d4265d07SGreg Roach                $auto     = $params['auto'];
214d4265d07SGreg Roach                $new_file = $params['new_file'];
215d4265d07SGreg Roach
216d4265d07SGreg Roach                /** @var UploadedFileInterface|null $uploaded_file */
217d4265d07SGreg Roach                $uploaded_file = $request->getUploadedFiles()['file'];
218d4265d07SGreg Roach                if ($uploaded_file === null || $uploaded_file->getError() !== UPLOAD_ERR_OK) {
219d4265d07SGreg Roach                    return '';
220d4265d07SGreg Roach                }
221d4265d07SGreg Roach
222d4265d07SGreg Roach                // The filename
223d4265d07SGreg Roach                $new_file = str_replace('\\', '/', $new_file);
224d4265d07SGreg Roach                if ($new_file !== '' && strpos($new_file, '/') === false) {
225d4265d07SGreg Roach                    $file = $new_file;
226d4265d07SGreg Roach                } else {
227d4265d07SGreg Roach                    $file = $uploaded_file->getClientFilename();
228d4265d07SGreg Roach                }
229d4265d07SGreg Roach
230d4265d07SGreg Roach                // The folder
231d4265d07SGreg Roach                $folder = str_replace('\\', '/', $folder);
232d4265d07SGreg Roach                $folder = trim($folder, '/');
233d4265d07SGreg Roach                if ($folder !== '') {
234d4265d07SGreg Roach                    $folder .= '/';
235d4265d07SGreg Roach                }
236d4265d07SGreg Roach
237d4265d07SGreg Roach                // Generate a unique name for the file?
238a04bb9a2SGreg Roach                if ($auto === '1' || $tree->mediaFilesystem($data_filesystem)->has($folder . $file)) {
239d4265d07SGreg Roach                    $folder    = '';
240d4265d07SGreg Roach                    $extension = pathinfo($uploaded_file->getClientFilename(), PATHINFO_EXTENSION);
241d4265d07SGreg Roach                    $file      = sha1((string) $uploaded_file->getStream()) . '.' . $extension;
242d4265d07SGreg Roach                }
243d4265d07SGreg Roach
244d4265d07SGreg Roach                try {
245a04bb9a2SGreg Roach                    $tree->mediaFilesystem($data_filesystem)->writeStream($folder . $file, $uploaded_file->getStream()->detach());
246d4265d07SGreg Roach
247d4265d07SGreg Roach                    return $folder . $file;
248d4265d07SGreg Roach                } catch (RuntimeException | InvalidArgumentException $ex) {
249d4265d07SGreg Roach                    FlashMessages::addMessage(I18N::translate('There was an error uploading your file.'));
250d4265d07SGreg Roach
251d4265d07SGreg Roach                    return '';
252d4265d07SGreg Roach                }
253d4265d07SGreg Roach        }
254d4265d07SGreg Roach    }
255d4265d07SGreg Roach
256d4265d07SGreg Roach    /**
257d4265d07SGreg Roach     * Convert the media file attributes into GEDCOM format.
258d4265d07SGreg Roach     *
259d4265d07SGreg Roach     * @param string $file
260d4265d07SGreg Roach     * @param string $type
261d4265d07SGreg Roach     * @param string $title
262d4265d07SGreg Roach     *
263d4265d07SGreg Roach     * @return string
264d4265d07SGreg Roach     */
265d4265d07SGreg Roach    public function createMediaFileGedcom(string $file, string $type, string $title): string
266d4265d07SGreg Roach    {
267d4265d07SGreg Roach        if (preg_match('/\.([a-z0-9]+)/i', $file, $match)) {
268d4265d07SGreg Roach            $extension = strtolower($match[1]);
269d4265d07SGreg Roach            $extension = str_replace('jpg', 'jpeg', $extension);
270d4265d07SGreg Roach            $extension = ' ' . $extension;
271d4265d07SGreg Roach        } else {
272d4265d07SGreg Roach            $extension = '';
273d4265d07SGreg Roach        }
274d4265d07SGreg Roach
275d4265d07SGreg Roach        $gedcom = '1 FILE ' . $file;
276d4265d07SGreg Roach        if ($type !== '') {
277d4265d07SGreg Roach            $gedcom .= "\n2 FORM" . $extension . "\n3 TYPE " . $type;
278d4265d07SGreg Roach        }
279d4265d07SGreg Roach        if ($title !== '') {
280d4265d07SGreg Roach            $gedcom .= "\n2 TITL " . $title;
281d4265d07SGreg Roach        }
282d4265d07SGreg Roach
283d4265d07SGreg Roach        return $gedcom;
284d4265d07SGreg Roach    }
28513aa75d8SGreg Roach
28613aa75d8SGreg Roach    /**
28713aa75d8SGreg Roach     * Fetch a list of all files on disk (in folders used by any tree).
28813aa75d8SGreg Roach     *
28913aa75d8SGreg Roach     * @param FilesystemInterface $data_filesystem Fileystem to search
29013aa75d8SGreg Roach     * @param string              $media_folder    Root folder
29113aa75d8SGreg Roach     * @param bool                $subfolders      Include subfolders
29213aa75d8SGreg Roach     *
293*b5c8fd7eSGreg Roach     * @return Collection<string>
29413aa75d8SGreg Roach     */
29513aa75d8SGreg Roach    public function allFilesOnDisk(FilesystemInterface $data_filesystem, string $media_folder, bool $subfolders): Collection
29613aa75d8SGreg Roach    {
29713aa75d8SGreg Roach        $array = $data_filesystem->listContents($media_folder, $subfolders);
29813aa75d8SGreg Roach
29913aa75d8SGreg Roach        return Collection::make($array)
30013aa75d8SGreg Roach            ->filter(static function (array $metadata): bool {
30113aa75d8SGreg Roach                return
30213aa75d8SGreg Roach                    $metadata['type'] === 'file' &&
30313aa75d8SGreg Roach                    strpos($metadata['path'], '/thumbs/') === false &&
30413aa75d8SGreg Roach                    strpos($metadata['path'], '/watermark/') === false;
30513aa75d8SGreg Roach            })
30613aa75d8SGreg Roach            ->map(static function (array $metadata): string {
30713aa75d8SGreg Roach                return $metadata['path'];
30813aa75d8SGreg Roach            });
30913aa75d8SGreg Roach    }
31013aa75d8SGreg Roach
31113aa75d8SGreg Roach    /**
31213aa75d8SGreg Roach     * Fetch a list of all files on in the database.
31313aa75d8SGreg Roach     *
31413aa75d8SGreg Roach     * @param string $media_folder Root folder
31513aa75d8SGreg Roach     * @param bool   $subfolders   Include subfolders
31613aa75d8SGreg Roach     *
317*b5c8fd7eSGreg Roach     * @return Collection<string>
31813aa75d8SGreg Roach     */
31913aa75d8SGreg Roach    public function allFilesInDatabase(string $media_folder, bool $subfolders): Collection
32013aa75d8SGreg Roach    {
32113aa75d8SGreg Roach        $query = DB::table('media_file')
32213aa75d8SGreg Roach            ->join('gedcom_setting', 'gedcom_id', '=', 'm_file')
32313aa75d8SGreg Roach            ->where('setting_name', '=', 'MEDIA_DIRECTORY')
32413aa75d8SGreg Roach            //->where('multimedia_file_refn', 'LIKE', '%/%')
32513aa75d8SGreg Roach            ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
32613aa75d8SGreg Roach            ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
32713aa75d8SGreg Roach            ->where(new Expression('setting_value || multimedia_file_refn'), 'LIKE', $media_folder . '%')
32813aa75d8SGreg Roach            ->select(new Expression('setting_value || multimedia_file_refn AS path'))
32913aa75d8SGreg Roach            ->orderBy(new Expression('setting_value || multimedia_file_refn'));
33013aa75d8SGreg Roach
33113aa75d8SGreg Roach        if (!$subfolders) {
33213aa75d8SGreg Roach            $query->where(new Expression('setting_value || multimedia_file_refn'), 'NOT LIKE', $media_folder . '%/%');
33313aa75d8SGreg Roach        }
33413aa75d8SGreg Roach
33513aa75d8SGreg Roach        return $query->pluck('path');
33613aa75d8SGreg Roach    }
33713aa75d8SGreg Roach
33813aa75d8SGreg Roach    /**
33913aa75d8SGreg Roach     * Generate a list of all folders in either the database or the filesystem.
34013aa75d8SGreg Roach     *
34113aa75d8SGreg Roach     * @param FilesystemInterface $data_filesystem
34213aa75d8SGreg Roach     *
343*b5c8fd7eSGreg Roach     * @return Collection<string,string>
34413aa75d8SGreg Roach     */
34513aa75d8SGreg Roach    public function allMediaFolders(FilesystemInterface $data_filesystem): Collection
34613aa75d8SGreg Roach    {
34713aa75d8SGreg Roach        $db_folders = DB::table('media_file')
34813aa75d8SGreg Roach            ->join('gedcom_setting', 'gedcom_id', '=', 'm_file')
34913aa75d8SGreg Roach            ->where('setting_name', '=', 'MEDIA_DIRECTORY')
35013aa75d8SGreg Roach            ->where('multimedia_file_refn', 'NOT LIKE', 'http://%')
35113aa75d8SGreg Roach            ->where('multimedia_file_refn', 'NOT LIKE', 'https://%')
35213aa75d8SGreg Roach            ->select(new Expression('setting_value || multimedia_file_refn AS path'))
35313aa75d8SGreg Roach            ->pluck('path')
35413aa75d8SGreg Roach            ->map(static function (string $path): string {
35513aa75d8SGreg Roach                return dirname($path) . '/';
35613aa75d8SGreg Roach            });
35713aa75d8SGreg Roach
35813aa75d8SGreg Roach        $media_roots = DB::table('gedcom_setting')
35913aa75d8SGreg Roach            ->where('setting_name', '=', 'MEDIA_DIRECTORY')
36013aa75d8SGreg Roach            ->pluck('setting_value')
36113aa75d8SGreg Roach            ->unique();
36213aa75d8SGreg Roach
36313aa75d8SGreg Roach        $disk_folders = new Collection($media_roots);
36413aa75d8SGreg Roach
36513aa75d8SGreg Roach        foreach ($media_roots as $media_folder) {
36613aa75d8SGreg Roach            $tmp = Collection::make($data_filesystem->listContents($media_folder, true))
36713aa75d8SGreg Roach                ->filter(static function (array $metadata) {
36813aa75d8SGreg Roach                    return $metadata['type'] === 'dir';
36913aa75d8SGreg Roach                })
37013aa75d8SGreg Roach                ->map(static function (array $metadata): string {
37113aa75d8SGreg Roach                    return $metadata['path'] . '/';
37213aa75d8SGreg Roach                })
37313aa75d8SGreg Roach                ->filter(static function (string $dir): bool {
37413aa75d8SGreg Roach                    return strpos($dir, '/thumbs/') === false && strpos($dir, 'watermarks') === false;
37513aa75d8SGreg Roach                });
37613aa75d8SGreg Roach
37713aa75d8SGreg Roach            $disk_folders = $disk_folders->concat($tmp);
37813aa75d8SGreg Roach        }
37913aa75d8SGreg Roach
38013aa75d8SGreg Roach        return $disk_folders->concat($db_folders)
38113aa75d8SGreg Roach            ->unique()
38213aa75d8SGreg Roach            ->mapWithKeys(static function (string $folder): array {
38313aa75d8SGreg Roach                return [$folder => $folder];
38413aa75d8SGreg Roach            });
38513aa75d8SGreg Roach    }
386d4265d07SGreg Roach}
387