xref: /webtrees/app/Schema/Migration22.php (revision 76692c8b291f16d9251d67f27078779f6737fe7e)
13bfb94b0SGreg Roach<?php
23bfb94b0SGreg Roach/**
33bfb94b0SGreg Roach * webtrees: online genealogy
43bfb94b0SGreg Roach * Copyright (C) 2015 webtrees development team
53bfb94b0SGreg Roach * This program is free software: you can redistribute it and/or modify
63bfb94b0SGreg Roach * it under the terms of the GNU General Public License as published by
73bfb94b0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
83bfb94b0SGreg Roach * (at your option) any later version.
93bfb94b0SGreg Roach * This program is distributed in the hope that it will be useful,
103bfb94b0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
113bfb94b0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123bfb94b0SGreg Roach * GNU General Public License for more details.
133bfb94b0SGreg Roach * You should have received a copy of the GNU General Public License
143bfb94b0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
153bfb94b0SGreg Roach */
16*76692c8bSGreg Roachnamespace Fisharebest\Webtrees\Schema;
17*76692c8bSGreg Roach
183bfb94b0SGreg Roachuse Fisharebest\Webtrees\Database;
193bfb94b0SGreg Roachuse Fisharebest\Webtrees\File;
203bfb94b0SGreg Roach
213bfb94b0SGreg Roach/**
22*76692c8bSGreg Roach * Upgrade the database schema from version 22 to version 23.
233bfb94b0SGreg Roach */
243bfb94b0SGreg Roachclass Migration22 implements MigrationInterface {
25*76692c8bSGreg Roach	/**
26*76692c8bSGreg Roach	 * Upgrade to to the next version
27*76692c8bSGreg Roach	 */
283bfb94b0SGreg Roach	public function upgrade() {
293bfb94b0SGreg Roach		// - data update for 1.4.0 media changes
303bfb94b0SGreg Roach		$_cfgs = Database::prepare(
313bfb94b0SGreg Roach			"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" .
323bfb94b0SGreg Roach			" FROM `##gedcom_setting` gs1" .
333bfb94b0SGreg Roach			" LEFT JOIN `##gedcom_setting` gs2 ON (gs1.gedcom_id = gs2.gedcom_id AND gs2.setting_name='USE_MEDIA_FIREWALL')" .
343bfb94b0SGreg Roach			" LEFT JOIN `##gedcom_setting` gs3 ON (gs1.gedcom_id = gs3.gedcom_id AND gs3.setting_name='MEDIA_FIREWALL_THUMBS')" .
353bfb94b0SGreg Roach			" LEFT JOIN `##gedcom_setting` gs4 ON (gs1.gedcom_id = gs4.gedcom_id AND gs4.setting_name='MEDIA_FIREWALL_ROOTDIR')" .
363bfb94b0SGreg Roach			" WHERE gs1.setting_name = 'MEDIA_DIRECTORY'"
373bfb94b0SGreg Roach		)->fetchAll();
383bfb94b0SGreg Roach
393bfb94b0SGreg Roach		// The constant WT_DATA_DIR is not defined yet (although it was when this script was originally written).
403bfb94b0SGreg Roach		$WT_DATA_DIR = realpath('data');
413bfb94b0SGreg Roach
423bfb94b0SGreg Roach		// Check the config for each tree
433bfb94b0SGreg Roach		foreach ($_cfgs as $_cfg) {
443bfb94b0SGreg Roach			if ($_cfg->use_media_firewall) {
453bfb94b0SGreg Roach				// We’re using the media firewall.
463bfb94b0SGreg Roach				$_mf_dir = realpath($_cfg->media_firewall_rootdir) . DIRECTORY_SEPARATOR;
473bfb94b0SGreg Roach				if ($_mf_dir == $WT_DATA_DIR) {
483bfb94b0SGreg Roach					// We’re already storing our media in the data folder - nothing to do.
493bfb94b0SGreg Roach				} else {
503bfb94b0SGreg Roach					// We’ve chosen a custom location for our media folder - need to update our media-folder to point to it.
513bfb94b0SGreg Roach					// We have, for example,
523bfb94b0SGreg Roach					// $_mf_dir = /home/fisharebest/my_pictures/
533bfb94b0SGreg Roach					// $WT_DATA_DIR = /home/fisharebest/public_html/webtrees/data/
543bfb94b0SGreg Roach					// Therefore we need to calculate ../../../my_pictures/
553bfb94b0SGreg Roach					$_media_dir = '';
563bfb94b0SGreg Roach					$_tmp_dir   = $WT_DATA_DIR;
573bfb94b0SGreg Roach					while (strpos($_mf_dir, $_tmp_dir) !== 0) {
583bfb94b0SGreg Roach						$_media_dir .= '../';
593bfb94b0SGreg Roach						$_tmp_dir = preg_replace('~[^/\\\\]+[/\\\\]$~', '', $_tmp_dir);
603bfb94b0SGreg Roach						if ($_tmp_dir == '') {
613bfb94b0SGreg Roach							// Shouldn't get here - but this script is not allowed to fail...
623bfb94b0SGreg Roach							continue 2;
633bfb94b0SGreg Roach						}
643bfb94b0SGreg Roach					}
653bfb94b0SGreg Roach					$_media_dir .= $_cfg->media_directory;
663bfb94b0SGreg Roach					Database::prepare(
673bfb94b0SGreg Roach						"UPDATE `##gedcom_setting`" .
683bfb94b0SGreg Roach						" SET setting_value=?" .
693bfb94b0SGreg Roach						" WHERE gedcom_id=? AND setting_name='MEDIA_DIRECTORY'"
703bfb94b0SGreg Roach					)->execute(array($_media_dir, $_cfg->gedcom_id));
713bfb94b0SGreg Roach				}
723bfb94b0SGreg Roach			} else {
733bfb94b0SGreg Roach				// Not using the media firewall - just move the public folder to the new location (if we can).
743bfb94b0SGreg Roach				if (
753bfb94b0SGreg Roach					file_exists(WT_ROOT . $_cfg->media_directory) &&
763bfb94b0SGreg Roach					is_dir(WT_ROOT . $_cfg->media_directory) &&
773bfb94b0SGreg Roach					!file_exists($WT_DATA_DIR . $_cfg->media_directory)
783bfb94b0SGreg Roach				) {
793bfb94b0SGreg Roach					try {
803bfb94b0SGreg Roach						rename(WT_ROOT . $_cfg->media_directory, $WT_DATA_DIR . $_cfg->media_directory);
813bfb94b0SGreg Roach					} catch (\ErrorException $ex) {
823bfb94b0SGreg Roach						// Cannot move the folder?
833bfb94b0SGreg Roach					}
843bfb94b0SGreg Roach					File::delete($WT_DATA_DIR . $_cfg->media_directory . '.htaccess');
853bfb94b0SGreg Roach					File::delete($WT_DATA_DIR . $_cfg->media_directory . 'index.php');
863bfb94b0SGreg Roach					File::delete($WT_DATA_DIR . $_cfg->media_directory . 'Mediainfo.txt');
873bfb94b0SGreg Roach					File::delete($WT_DATA_DIR . $_cfg->media_directory . 'thumbs/Thumbsinfo.txt');
883bfb94b0SGreg Roach				}
893bfb94b0SGreg Roach			}
903bfb94b0SGreg Roach		}
913bfb94b0SGreg Roach
923bfb94b0SGreg Roach		// Delete old settings
933bfb94b0SGreg Roach		Database::exec("DELETE FROM `##gedcom_setting` WHERE setting_name IN ('USE_MEDIA_FIREWALL', 'MEDIA_FIREWALL_THUMBS', 'MEDIA_FIREWALL_ROOTDIR')");
943bfb94b0SGreg Roach	}
953bfb94b0SGreg Roach}
96