fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 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 <https://www.gnu.org/licenses/>. |
||
| 16 | */ |
||
| 17 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Services; |
||
| 21 | |||
| 22 | use Fisharebest\Webtrees\Webtrees; |
||
| 23 | use InvalidArgumentException; |
||
| 24 | |||
| 25 | use function file_get_contents; |
||
| 26 | use function file_put_contents; |
||
| 27 | use function is_dir; |
||
| 28 | use function is_file; |
||
| 29 | use function is_link; |
||
| 30 | use function is_readable; |
||
| 31 | use function is_string; |
||
| 32 | use function realpath; |
||
| 33 | use function rmdir; |
||
| 34 | use function unlink; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Manage the site's online/offline status. |
||
| 38 | */ |
||
| 39 | readonly class MaintenanceModeService |
||
| 40 | { |
||
| 41 | private const string OFFLINE_FILE = 'offline.txt'; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 42 | |||
| 43 | public function __construct( |
||
| 44 | private string $data_dir = Webtrees::DATA_DIR |
||
| 45 | ) { |
||
| 46 | if (!is_dir($data_dir)) { |
||
| 47 | throw new InvalidArgumentException($data_dir . ' does not exist'); |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | public function file(): string |
||
| 52 | { |
||
| 53 | // Remove any '/../' from the path. |
||
| 54 | return realpath($this->data_dir) . DIRECTORY_SEPARATOR . self::OFFLINE_FILE; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function isOffline(): bool |
||
| 58 | { |
||
| 59 | $file = $this->file(); |
||
| 60 | |||
| 61 | return is_file($file) || is_link($file) || is_dir($file); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function message(): string |
||
| 65 | { |
||
| 66 | $file = $this->file(); |
||
| 67 | |||
| 68 | if ($this->isOffline() && is_file($file) && is_readable($file)) { |
||
| 69 | $message = file_get_contents($file); |
||
| 70 | |||
| 71 | if (is_string($message)) { |
||
| 72 | return $message; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | return ''; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function offline(string $message = ''): void |
||
| 80 | { |
||
| 81 | $this->online(); |
||
| 82 | |||
| 83 | file_put_contents($this->file(), $message); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function online(): void |
||
| 87 | { |
||
| 88 | $file = $this->file(); |
||
| 89 | |||
| 90 | if (is_dir($file)) { |
||
| 91 | rmdir($file); |
||
| 92 | } elseif (is_link($file) || is_file($file)) { |
||
| 93 | unlink($file); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 |