xref: /webtrees/app/Schema/Migration22.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Schema;
19
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\DebugBar;
22use Fisharebest\Webtrees\File;
23use Throwable;
24
25/**
26 * Upgrade the database schema from version 22 to version 23.
27 */
28class Migration22 implements MigrationInterface
29{
30    /**
31     * Upgrade to to the next version
32     *
33     * @return void
34     */
35    public function upgrade()
36    {
37        // - data update for 1.4.0 media changes
38        $_cfgs = Database::prepare(
39            "SELECT gs1.gedcom_id AS gedcom_id, gs1.setting_value AS media_directory, gs2.setting_value AS use_media_firewall, gs3.setting_value AS media_firewall_thumbs, gs4.setting_value AS media_firewall_rootdir" .
40            " FROM `##gedcom_setting` gs1" .
41            " LEFT JOIN `##gedcom_setting` gs2 ON (gs1.gedcom_id = gs2.gedcom_id AND gs2.setting_name='USE_MEDIA_FIREWALL')" .
42            " LEFT JOIN `##gedcom_setting` gs3 ON (gs1.gedcom_id = gs3.gedcom_id AND gs3.setting_name='MEDIA_FIREWALL_THUMBS')" .
43            " LEFT JOIN `##gedcom_setting` gs4 ON (gs1.gedcom_id = gs4.gedcom_id AND gs4.setting_name='MEDIA_FIREWALL_ROOTDIR')" .
44            " WHERE gs1.setting_name = 'MEDIA_DIRECTORY'"
45        )->fetchAll();
46
47        // The constant WT_DATA_DIR is not defined yet (although it was when this script was originally written).
48        $WT_DATA_DIR = realpath('data');
49
50        // Check the config for each tree
51        foreach ($_cfgs as $_cfg) {
52            if ($_cfg->use_media_firewall) {
53                // We’re using the media firewall.
54                $_mf_dir = realpath($_cfg->media_firewall_rootdir) . DIRECTORY_SEPARATOR;
55                if ($_mf_dir == $WT_DATA_DIR) {
56                    // We’re already storing our media in the data folder - nothing to do.
57                } else {
58                    // We’ve chosen a custom location for our media folder - need to update our media-folder to point to it.
59                    // We have, for example,
60                    // $_mf_dir = /home/fisharebest/my_pictures/
61                    // $WT_DATA_DIR = /home/fisharebest/public_html/webtrees/data/
62                    // Therefore we need to calculate ../../../my_pictures/
63                    $_media_dir = '';
64                    $_tmp_dir   = $WT_DATA_DIR;
65                    while (strpos($_mf_dir, $_tmp_dir) !== 0) {
66                        $_media_dir .= '../';
67                        $_tmp_dir = preg_replace('~[^/\\\\]+[/\\\\]$~', '', $_tmp_dir);
68                        if ($_tmp_dir == '') {
69                            // Shouldn't get here - but this script is not allowed to fail...
70                            continue 2;
71                        }
72                    }
73                    $_media_dir .= $_cfg->media_directory;
74                    Database::prepare(
75                        "UPDATE `##gedcom_setting`" .
76                        " SET setting_value = :media_dir" .
77                        " WHERE gedcom_id = :tree_id AND setting_name='MEDIA_DIRECTORY'"
78                    )->execute([
79                        'media_dir' => $_media_dir,
80                        'tree_id'   => $_cfg->gedcom_id,
81                    ]);
82                }
83            } else {
84                // Not using the media firewall - just move the public folder to the new location (if we can).
85                if (is_dir(WT_ROOT . $_cfg->media_directory) && !file_exists($WT_DATA_DIR . $_cfg->media_directory)) {
86                    try {
87                        rename(WT_ROOT . $_cfg->media_directory, $WT_DATA_DIR . $_cfg->media_directory);
88                    } catch (Throwable $ex) {
89                        DebugBar::addThrowable($ex);
90
91                        // Cannot move the folder?
92                    }
93                    File::delete($WT_DATA_DIR . $_cfg->media_directory . '.htaccess');
94                    File::delete($WT_DATA_DIR . $_cfg->media_directory . 'index.php');
95                    File::delete($WT_DATA_DIR . $_cfg->media_directory . 'Mediainfo.txt');
96                    File::delete($WT_DATA_DIR . $_cfg->media_directory . 'thumbs/Thumbsinfo.txt');
97                }
98            }
99        }
100
101        // Delete old settings
102        Database::exec("DELETE FROM `##gedcom_setting` WHERE setting_name IN ('USE_MEDIA_FIREWALL', 'MEDIA_FIREWALL_THUMBS', 'MEDIA_FIREWALL_ROOTDIR')");
103    }
104}
105