xref: /webtrees/app/Services/UpgradeService.php (revision f7cf8a155e2743f3d124eef3d30a558ab062fa4b)
10d11ac7eSGreg Roach<?php
23976b470SGreg Roach
30d11ac7eSGreg Roach/**
40d11ac7eSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
60d11ac7eSGreg Roach * This program is free software: you can redistribute it and/or modify
70d11ac7eSGreg Roach * it under the terms of the GNU General Public License as published by
80d11ac7eSGreg Roach * the Free Software Foundation, either version 3 of the License, or
90d11ac7eSGreg Roach * (at your option) any later version.
100d11ac7eSGreg Roach * This program is distributed in the hope that it will be useful,
110d11ac7eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
120d11ac7eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130d11ac7eSGreg Roach * GNU General Public License for more details.
140d11ac7eSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
160d11ac7eSGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
200d11ac7eSGreg Roachnamespace Fisharebest\Webtrees\Services;
210d11ac7eSGreg Roach
226ccdf4f0SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
234459dc9aSGreg Roachuse Fisharebest\Webtrees\Carbon;
24d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpServerErrorException;
257def76c7SGreg Roachuse Fisharebest\Webtrees\I18N;
260d11ac7eSGreg Roachuse Fisharebest\Webtrees\Site;
278d0ebef0SGreg Roachuse Fisharebest\Webtrees\Webtrees;
280d11ac7eSGreg Roachuse GuzzleHttp\Client;
290d11ac7eSGreg Roachuse GuzzleHttp\Exception\RequestException;
307def76c7SGreg Roachuse Illuminate\Support\Collection;
317def76c7SGreg Roachuse League\Flysystem\Filesystem;
32*f7cf8a15SGreg Roachuse League\Flysystem\FilesystemException;
33*f7cf8a15SGreg Roachuse League\Flysystem\FilesystemOperator;
34*f7cf8a15SGreg Roachuse League\Flysystem\StorageAttributes;
35*f7cf8a15SGreg Roachuse League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;
367def76c7SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter;
377def76c7SGreg Roachuse ZipArchive;
383976b470SGreg Roach
39fbb4b472SGreg Roachuse function rewind;
400d11ac7eSGreg Roach
410d11ac7eSGreg Roach/**
420d11ac7eSGreg Roach * Automatic upgrades.
430d11ac7eSGreg Roach */
440d11ac7eSGreg Roachclass UpgradeService
450d11ac7eSGreg Roach{
467def76c7SGreg Roach    // Options for fetching files using GuzzleHTTP
477def76c7SGreg Roach    private const GUZZLE_OPTIONS = [
487def76c7SGreg Roach        'connect_timeout' => 25,
497def76c7SGreg Roach        'read_timeout'    => 25,
507def76c7SGreg Roach        'timeout'         => 55,
517def76c7SGreg Roach    ];
527def76c7SGreg Roach
537def76c7SGreg Roach    // Transfer stream data in blocks of this number of bytes.
547def76c7SGreg Roach    private const READ_BLOCK_SIZE = 65535;
557def76c7SGreg Roach
567def76c7SGreg Roach    // Only check the webtrees server once per day.
5716d6367aSGreg Roach    private const CHECK_FOR_UPDATE_INTERVAL = 24 * 60 * 60;
580d11ac7eSGreg Roach
590d11ac7eSGreg Roach    // Fetch information about upgrades from here.
600d11ac7eSGreg Roach    // Note: earlier versions of webtrees used svn.webtrees.net, so we must maintain both URLs.
6116d6367aSGreg Roach    private const UPDATE_URL = 'https://dev.webtrees.net/build/latest-version.txt';
620d11ac7eSGreg Roach
630d11ac7eSGreg Roach    // If the update server doesn't respond after this time, give up.
6416d6367aSGreg Roach    private const HTTP_TIMEOUT = 3.0;
650d11ac7eSGreg Roach
667def76c7SGreg Roach    /** @var TimeoutService */
677def76c7SGreg Roach    private $timeout_service;
687def76c7SGreg Roach
697def76c7SGreg Roach    /**
707def76c7SGreg Roach     * UpgradeService constructor.
717def76c7SGreg Roach     *
727def76c7SGreg Roach     * @param TimeoutService $timeout_service
737def76c7SGreg Roach     */
747def76c7SGreg Roach    public function __construct(TimeoutService $timeout_service)
757def76c7SGreg Roach    {
767def76c7SGreg Roach        $this->timeout_service = $timeout_service;
777def76c7SGreg Roach    }
787def76c7SGreg Roach
797def76c7SGreg Roach    /**
807def76c7SGreg Roach     * Unpack webtrees.zip.
817def76c7SGreg Roach     *
827def76c7SGreg Roach     * @param string $zip_file
837def76c7SGreg Roach     * @param string $target_folder
8425d7fe95SGreg Roach     *
8525d7fe95SGreg Roach     * @return void
867def76c7SGreg Roach     */
8725d7fe95SGreg Roach    public function extractWebtreesZip(string $zip_file, string $target_folder): void
887def76c7SGreg Roach    {
897def76c7SGreg Roach        // The Flysystem ZIP archive adapter is painfully slow, so use the native PHP library.
907def76c7SGreg Roach        $zip = new ZipArchive();
917def76c7SGreg Roach
92320f6a24SGreg Roach        if ($zip->open($zip_file) === true) {
937def76c7SGreg Roach            $zip->extractTo($target_folder);
947def76c7SGreg Roach            $zip->close();
957def76c7SGreg Roach        } else {
96d501c45dSGreg Roach            throw new HttpServerErrorException('Cannot read ZIP file. Is it corrupt?');
977def76c7SGreg Roach        }
987def76c7SGreg Roach    }
997def76c7SGreg Roach
1007def76c7SGreg Roach    /**
1017def76c7SGreg Roach     * Create a list of all the files in a webtrees .ZIP archive
1027def76c7SGreg Roach     *
1034db4b4a9SGreg Roach     * @param string $zip_file
1044db4b4a9SGreg Roach     *
105b5c8fd7eSGreg Roach     * @return Collection<string>
106*f7cf8a15SGreg Roach     * @throws FilesystemException
1077def76c7SGreg Roach     */
1084db4b4a9SGreg Roach    public function webtreesZipContents(string $zip_file): Collection
10923de9ab7SGreg Roach    {
110*f7cf8a15SGreg Roach        $zip_provider   = new FilesystemZipArchiveProvider($zip_file, 0755);
111*f7cf8a15SGreg Roach        $zip_adapter    = new ZipArchiveAdapter($zip_provider, 'webtrees');
112*f7cf8a15SGreg Roach        $zip_filesystem = new Filesystem($zip_adapter);
1137def76c7SGreg Roach
114*f7cf8a15SGreg Roach        $files = $zip_filesystem->listContents('', Filesystem::LIST_DEEP)
115*f7cf8a15SGreg Roach            ->filter(static function (StorageAttributes $attributes): bool {
116*f7cf8a15SGreg Roach                return $attributes->isFile();
1177def76c7SGreg Roach            })
118*f7cf8a15SGreg Roach            ->map(static function (StorageAttributes $attributes): string {
119*f7cf8a15SGreg Roach                return $attributes->path();
1207def76c7SGreg Roach            });
121*f7cf8a15SGreg Roach
122*f7cf8a15SGreg Roach        return new Collection($files);
1237def76c7SGreg Roach    }
1247def76c7SGreg Roach
1257def76c7SGreg Roach    /**
1267def76c7SGreg Roach     * Fetch a file from a URL and save it in a filesystem.
1277def76c7SGreg Roach     * Use streams so that we can copy files larger than our available memory.
1287def76c7SGreg Roach     *
1297def76c7SGreg Roach     * @param string             $url
130*f7cf8a15SGreg Roach     * @param FilesystemOperator $filesystem
1317def76c7SGreg Roach     * @param string             $path
1327def76c7SGreg Roach     *
1337def76c7SGreg Roach     * @return int The number of bytes downloaded
1347def76c7SGreg Roach     */
135*f7cf8a15SGreg Roach    public function downloadFile(string $url, FilesystemOperator $filesystem, string $path): int
1367def76c7SGreg Roach    {
1377def76c7SGreg Roach        // Overwrite any previous/partial/failed download.
138*f7cf8a15SGreg Roach        if ($filesystem->fileExists($path)) {
1397def76c7SGreg Roach            $filesystem->delete($path);
1407def76c7SGreg Roach        }
1417def76c7SGreg Roach
1427def76c7SGreg Roach        // We store the data in PHP temporary storage.
143e364afe4SGreg Roach        $tmp = fopen('php://temp', 'wb+');
1447def76c7SGreg Roach
1457def76c7SGreg Roach        // Read from the URL
1467def76c7SGreg Roach        $client   = new Client();
1477def76c7SGreg Roach        $response = $client->get($url, self::GUZZLE_OPTIONS);
1487def76c7SGreg Roach        $stream   = $response->getBody();
1497def76c7SGreg Roach
1507def76c7SGreg Roach        // Download the file to temporary storage.
1517def76c7SGreg Roach        while (!$stream->eof()) {
1527def76c7SGreg Roach            fwrite($tmp, $stream->read(self::READ_BLOCK_SIZE));
1537def76c7SGreg Roach
154fbb4b472SGreg Roach            if ($this->timeout_service->isTimeNearlyUp()) {
155d501c45dSGreg Roach                throw new HttpServerErrorException(I18N::translate('The server’s time limit has been reached.'));
1567def76c7SGreg Roach            }
1577def76c7SGreg Roach        }
1587def76c7SGreg Roach
1597def76c7SGreg Roach        if (is_resource($stream)) {
1607def76c7SGreg Roach            fclose($stream);
1617def76c7SGreg Roach        }
1627def76c7SGreg Roach
1637def76c7SGreg Roach        // Copy from temporary storage to the file.
164fbb4b472SGreg Roach        $bytes = ftell($tmp);
1657def76c7SGreg Roach        rewind($tmp);
1667def76c7SGreg Roach        $filesystem->writeStream($path, $tmp);
1677def76c7SGreg Roach        fclose($tmp);
1687def76c7SGreg Roach
1697def76c7SGreg Roach        return $bytes;
1707def76c7SGreg Roach    }
1717def76c7SGreg Roach
1727def76c7SGreg Roach    /**
1737def76c7SGreg Roach     * Move (copy and delete) all files from one filesystem to another.
1747def76c7SGreg Roach     *
175*f7cf8a15SGreg Roach     * @param FilesystemOperator $source
176*f7cf8a15SGreg Roach     * @param FilesystemOperator $destination
17725d7fe95SGreg Roach     *
17825d7fe95SGreg Roach     * @return void
179*f7cf8a15SGreg Roach     * @throws FilesystemException
1807def76c7SGreg Roach     */
181*f7cf8a15SGreg Roach    public function moveFiles(FilesystemOperator $source, FilesystemOperator $destination): void
18223de9ab7SGreg Roach    {
183*f7cf8a15SGreg Roach        foreach ($source->listContents('', Filesystem::LIST_DEEP) as $attributes) {
184*f7cf8a15SGreg Roach            if ($attributes->isFile()) {
185*f7cf8a15SGreg Roach                $destination->write($attributes->path(), $source->read($attributes->path()));
186*f7cf8a15SGreg Roach                $source->delete($attributes->path());
1877def76c7SGreg Roach
188fbb4b472SGreg Roach                if ($this->timeout_service->isTimeNearlyUp()) {
189d501c45dSGreg Roach                    throw new HttpServerErrorException(I18N::translate('The server’s time limit has been reached.'));
1907def76c7SGreg Roach                }
1917def76c7SGreg Roach            }
1927def76c7SGreg Roach        }
1937def76c7SGreg Roach    }
1947def76c7SGreg Roach
1950d11ac7eSGreg Roach    /**
196fbb4b472SGreg Roach     * Delete files in $destination that aren't in $source.
197fbb4b472SGreg Roach     *
198*f7cf8a15SGreg Roach     * @param FilesystemOperator $filesystem
1995d4b7ec2SGreg Roach     * @param Collection<string> $folders_to_clean
2005d4b7ec2SGreg Roach     * @param Collection<string> $files_to_keep
20125d7fe95SGreg Roach     *
20225d7fe95SGreg Roach     * @return void
203fbb4b472SGreg Roach     */
204*f7cf8a15SGreg Roach    public function cleanFiles(FilesystemOperator $filesystem, Collection $folders_to_clean, Collection $files_to_keep): void
205fbb4b472SGreg Roach    {
206fbb4b472SGreg Roach        foreach ($folders_to_clean as $folder_to_clean) {
207*f7cf8a15SGreg Roach            foreach ($filesystem->listContents($folder_to_clean, Filesystem::LIST_DEEP) as $path) {
208fbb4b472SGreg Roach                if ($path['type'] === 'file' && !$files_to_keep->contains($path['path'])) {
209fbb4b472SGreg Roach                    $filesystem->delete($path['path']);
210fbb4b472SGreg Roach                }
211fbb4b472SGreg Roach
212fbb4b472SGreg Roach                // If we run out of time, then just stop.
213fbb4b472SGreg Roach                if ($this->timeout_service->isTimeNearlyUp()) {
214fbb4b472SGreg Roach                    return;
215fbb4b472SGreg Roach                }
216fbb4b472SGreg Roach            }
217fbb4b472SGreg Roach        }
218fbb4b472SGreg Roach    }
219fbb4b472SGreg Roach
220fbb4b472SGreg Roach    /**
2210d11ac7eSGreg Roach     * @return bool
2220d11ac7eSGreg Roach     */
2230d11ac7eSGreg Roach    public function isUpgradeAvailable(): bool
2240d11ac7eSGreg Roach    {
2250d11ac7eSGreg Roach        // If the latest version is unavailable, we will have an empty sting which equates to version 0.
2260d11ac7eSGreg Roach
2278d0ebef0SGreg Roach        return version_compare(Webtrees::VERSION, $this->fetchLatestVersion()) < 0;
2280d11ac7eSGreg Roach    }
2290d11ac7eSGreg Roach
2300d11ac7eSGreg Roach    /**
2310d11ac7eSGreg Roach     * What is the latest version of webtrees.
2320d11ac7eSGreg Roach     *
2330d11ac7eSGreg Roach     * @return string
2340d11ac7eSGreg Roach     */
2350d11ac7eSGreg Roach    public function latestVersion(): string
2360d11ac7eSGreg Roach    {
2370d11ac7eSGreg Roach        $latest_version = $this->fetchLatestVersion();
2380d11ac7eSGreg Roach
23965e02381SGreg Roach        [$version] = explode('|', $latest_version);
2400d11ac7eSGreg Roach
2410d11ac7eSGreg Roach        return $version;
2420d11ac7eSGreg Roach    }
2430d11ac7eSGreg Roach
2440d11ac7eSGreg Roach    /**
2450d11ac7eSGreg Roach     * Where can we download the latest version of webtrees.
2460d11ac7eSGreg Roach     *
2470d11ac7eSGreg Roach     * @return string
2480d11ac7eSGreg Roach     */
2490d11ac7eSGreg Roach    public function downloadUrl(): string
2500d11ac7eSGreg Roach    {
2510d11ac7eSGreg Roach        $latest_version = $this->fetchLatestVersion();
2520d11ac7eSGreg Roach
25365e02381SGreg Roach        [, , $url] = explode('|', $latest_version . '||');
2540d11ac7eSGreg Roach
2550d11ac7eSGreg Roach        return $url;
2560d11ac7eSGreg Roach    }
2570d11ac7eSGreg Roach
2587def76c7SGreg Roach    public function startMaintenanceMode(): void
2597def76c7SGreg Roach    {
2607def76c7SGreg Roach        $message = I18N::translate('This website is being upgraded. Try again in a few minutes.');
2617def76c7SGreg Roach
262f397d0fdSGreg Roach        file_put_contents(Webtrees::OFFLINE_FILE, $message);
2637def76c7SGreg Roach    }
2647def76c7SGreg Roach
2657def76c7SGreg Roach    public function endMaintenanceMode(): void
2667def76c7SGreg Roach    {
267f397d0fdSGreg Roach        if (file_exists(Webtrees::OFFLINE_FILE)) {
268f397d0fdSGreg Roach            unlink(Webtrees::OFFLINE_FILE);
2697def76c7SGreg Roach        }
2707def76c7SGreg Roach    }
2717def76c7SGreg Roach
2720d11ac7eSGreg Roach    /**
2730d11ac7eSGreg Roach     * Check with the webtrees.net server for the latest version of webtrees.
2740d11ac7eSGreg Roach     * Fetching the remote file can be slow, so check infrequently, and cache the result.
2750d11ac7eSGreg Roach     * Pass the current versions of webtrees, PHP and MySQL, as the response
2760d11ac7eSGreg Roach     * may be different for each. The server logs are used to generate
2770d11ac7eSGreg Roach     * installation statistics which can be found at http://dev.webtrees.net/statistics.html
2780d11ac7eSGreg Roach     *
2790d11ac7eSGreg Roach     * @return string
2800d11ac7eSGreg Roach     */
2810d11ac7eSGreg Roach    private function fetchLatestVersion(): string
2820d11ac7eSGreg Roach    {
2830d11ac7eSGreg Roach        $last_update_timestamp = (int) Site::getPreference('LATEST_WT_VERSION_TIMESTAMP');
2840d11ac7eSGreg Roach
2854459dc9aSGreg Roach        $current_timestamp = Carbon::now()->unix();
286bf57b580SGreg Roach
287bf57b580SGreg Roach        if ($last_update_timestamp < $current_timestamp - self::CHECK_FOR_UPDATE_INTERVAL) {
2880d11ac7eSGreg Roach            try {
2890d11ac7eSGreg Roach                $client = new Client([
2900d11ac7eSGreg Roach                    'timeout' => self::HTTP_TIMEOUT,
2910d11ac7eSGreg Roach                ]);
2920d11ac7eSGreg Roach
2930d11ac7eSGreg Roach                $response = $client->get(self::UPDATE_URL, [
2940d11ac7eSGreg Roach                    'query' => $this->serverParameters(),
2950d11ac7eSGreg Roach                ]);
2960d11ac7eSGreg Roach
2976ccdf4f0SGreg Roach                if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
2980d11ac7eSGreg Roach                    Site::setPreference('LATEST_WT_VERSION', $response->getBody()->getContents());
299bf57b580SGreg Roach                    Site::setPreference('LATEST_WT_VERSION_TIMESTAMP', (string) $current_timestamp);
3000d11ac7eSGreg Roach                }
3010d11ac7eSGreg Roach            } catch (RequestException $ex) {
302c6a0ce5cSGreg Roach                // Can't connect to the server?
303c6a0ce5cSGreg Roach                // Use the existing information about latest versions.
3040d11ac7eSGreg Roach            }
3050d11ac7eSGreg Roach        }
3060d11ac7eSGreg Roach
3070d11ac7eSGreg Roach        return Site::getPreference('LATEST_WT_VERSION');
3080d11ac7eSGreg Roach    }
3090d11ac7eSGreg Roach
3100d11ac7eSGreg Roach    /**
3110d11ac7eSGreg Roach     * The upgrade server needs to know a little about this server.
3125d4b7ec2SGreg Roach     *
3135d4b7ec2SGreg Roach     * @return array<string,string>
3140d11ac7eSGreg Roach     */
3158f53f488SRico Sonntag    private function serverParameters(): array
3160d11ac7eSGreg Roach    {
3170d11ac7eSGreg Roach        $operating_system = DIRECTORY_SEPARATOR === '/' ? 'u' : 'w';
3180d11ac7eSGreg Roach
3190d11ac7eSGreg Roach        return [
3208d0ebef0SGreg Roach            'w' => Webtrees::VERSION,
3210d11ac7eSGreg Roach            'p' => PHP_VERSION,
3220d11ac7eSGreg Roach            'o' => $operating_system,
3230d11ac7eSGreg Roach        ];
3240d11ac7eSGreg Roach    }
3250d11ac7eSGreg Roach}
326