summaryrefslogtreecommitdiff
path: root/admin_trees_download.php
blob: f3821dd4afaa6dfd6d6333f709976f75183e4b17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2017 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees;

use Fisharebest\Webtrees\Controller\PageController;
use Fisharebest\Webtrees\Functions\FunctionsExport;
use PclZip;

/** @global Tree $WT_TREE */
global $WT_TREE;

require 'includes/session.php';

$controller = new PageController;
$controller
	->setPageTitle(I18N::translate($WT_TREE->getTitleHtml()) . ' — ' . I18N::translate('Export a GEDCOM file'))
	->restrictAccess(Auth::isManager($WT_TREE));

// Validate user parameters
$action           = Filter::get('action', 'download');
$convert          = Filter::get('convert', 'yes|no', 'no');
$zip              = Filter::get('zip', 'yes|no', 'no');
$conv_path        = Filter::get('conv_path');
$privatize_export = Filter::get('privatize_export', 'none|visitor|user|gedadmin');

if ($action === 'download') {
	$exportOptions = [
		'privatize' => $privatize_export,
		'toANSI'    => $convert,
		'path'      => $conv_path,
	];

	// What to call the downloaded file
	$download_filename = $WT_TREE->getName();
	if (strtolower(substr($download_filename, -4, 4)) != '.ged') {
		$download_filename .= '.ged';
	}

	if ($zip === 'yes') {
		$temp_dir = WT_DATA_DIR . 'tmp-' . $WT_TREE->getName() . '-' . date('YmdHis') . '/';
		$zip_file = $download_filename . '.zip';

		if (!File::mkdir($temp_dir)) {
			echo 'Error : Could not create temporary path!';

			return;
		}

		// Create the unzipped GEDCOM on disk, so we can ZIP it.
		$stream = fopen($temp_dir . $download_filename, 'w');
		FunctionsExport::exportGedcom($WT_TREE, $stream, $exportOptions);
		fclose($stream);

		// Create a ZIP file containing the GEDCOM file.
		$comment = 'Created by ' . WT_WEBTREES . ' ' . WT_VERSION . ' on ' . date('r') . '.';
		$archive = new PclZip($temp_dir . $zip_file);
		$v_list  = $archive->add($temp_dir . $download_filename, \PCLZIP_OPT_COMMENT, $comment, \PCLZIP_OPT_REMOVE_PATH, $temp_dir);
		if ($v_list == 0) {
			echo 'Error : ' . $archive->errorInfo(true);
		} else {
			header('Content-Type: application/zip');
			header('Content-Disposition: attachment; filename="' . $zip_file . '"');
			header('Content-length: ' . filesize($temp_dir . $zip_file));
			readfile($temp_dir . $zip_file);
			File::delete($temp_dir);
		}
	} else {
		header('Content-Type: text/plain; charset=UTF-8');
		header('Content-Disposition: attachment; filename="' . $download_filename . '"');
		// Stream the GEDCOM file straight to the browser.
		// We could open "php://compress.zlib" to create a .gz file or "php://compress.bzip2" to create a .bz2 file
		$stream = fopen('php://output', 'w');
		FunctionsExport::exportGedcom($WT_TREE, $stream, $exportOptions);
		fclose($stream);
	}

	return;
}

$controller->pageHeader();

echo Bootstrap4::breadcrumbs([
	'admin.php'              => I18N::translate('Control panel'),
	'admin_trees_manage.php' => I18N::translate('Manage family trees'),
], $controller->getPageTitle());
?>

<h1><?= $controller->getPageTitle() ?></h1>

<form class="form form-horizontal" method="post" action="admin_trees_export.php">
	<?= Filter::getCsrf() ?>
	<input type="hidden" name="ged" value="<?= $WT_TREE->getNameHtml() ?>">

	<div class="row form-group">
		<label for="submit-export" class="col-sm-3 col-form-label">
			<?= I18N::translate('A file on the server') ?>
		</label>
		<div class="col-sm-9">
			<button id="submit-export" type="submit" class="btn btn-primary">
				<?= /* I18N: A button label. */ I18N::translate('continue') ?>
			</button>
		</div>
	</div>
</form>

<hr>

<form class="form form-horizontal">
	<input type="hidden" name="action" value="download">
	<input type="hidden" name="ged" value="<?= $WT_TREE->getNameHtml() ?>">

	<!-- DOWNLOAD OPTIONS -->
	<fieldset class="form-group">
		<div class="row">
			<legend class="col-form-legend col-sm-3">
				<?= I18N::translate('Export preferences') ?>
			</legend>
			<!-- ZIP FILES -->
			<div class="col-sm-9">
				<label>
					<input type="checkbox" name="zip" value="yes">
					<?= I18N::translate('Compress the GEDCOM file') ?>
				</label>
				<p class="small muted">
					<?= I18N::translate('To reduce the size of the download, you can compress the data into a .ZIP file. You will need to uncompress the .ZIP file before you can use it.') ?>
				</p>

			<!-- CONVERT TO ISO8859-1 -->
				<label>
					<input type="checkbox" name="convert" value="yes">
					<?= I18N::translate('Convert from UTF-8 to ISO-8859-1') ?>
				</label>
				<p class="small muted">
					<?= I18N::translate('webtrees uses UTF-8 encoding for accented letters, special characters and non-Latin scripts. If you want to use this GEDCOM file with genealogy software that does not support UTF-8, then you can create it using ISO-8859-1 encoding.') ?>
				</p>

				<!-- GEDCOM_MEDIA_PATH -->
				<?php if ($WT_TREE->getPreference('GEDCOM_MEDIA_PATH')): ?>
				<label>
					<input type="checkbox" name="conv_path" value="<?= Html::escape($WT_TREE->getPreference('GEDCOM_MEDIA_PATH')) ?>">
					<?= /* I18N: A media path (e.g. C:\aaa\bbb\ccc\) in a GEDCOM file */ I18N::translate('Add the GEDCOM media path to filenames') ?>
				</label>
				<p>
					<?= /* I18N: %s is the name of a folder. */ I18N::translate('Media filenames will be prefixed by %s.', '<code dir="ltr">' . Html::escape($WT_TREE->getPreference('GEDCOM_MEDIA_PATH')) . '</code>') ?>
				</p>
				<?php endif ?>
			</div>
		</div>
	</fieldset>

	<!-- PRIVACY OPTIONS -->
	<fieldset class="form-group">
		<div class="row">
			<legend class="col-form-legend col-sm-3">
				<?= I18N::translate('Apply privacy settings') ?>
			</legend>
			<div class="col-sm-9">
				<label>
					<input type="radio" name="privatize_export" value="none" checked>
					<?= I18N::translate('None') ?>
				</label>
				<br>
				<label>
					<input type="radio" name="privatize_export" value="gedadmin">
					<?= I18N::translate('Manager') ?>
				</label>
				<br>
				<label>
					<input type="radio" name="privatize_export" value="user">
					<?= I18N::translate('Member') ?>
				</label>
				<br>
				<label>
					<input type="radio" name="privatize_export" value="visitor">
					<?= I18N::translate('Visitor') ?>
				</label>
			</div>
		</div>
	</fieldset>

	<div class="row form-group">
		<label for="submit-export" class="col-sm-3 col-form-label">
			<?= I18N::translate('A file on your computer') ?>
		</label>
		<div class="col-sm-9">
			<button id="submit-export" type="submit" class="btn btn-primary">
				<?= /* I18N: A button label. */ I18N::translate('continue') ?>
			</button>
		</div>
	</div>
</form>

</form>