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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
<?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 League\Flysystem\Filesystem;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
require 'includes/session.php';
$controller = new PageController;
$controller
->setPageTitle(I18N::translate($controller->tree()->getTitleHtml()) . ' — ' . I18N::translate('Export a GEDCOM file'))
->restrictAccess(Auth::isManager($controller->tree()));
// Validate user parameters
$action = Filter::get('action', 'download');
$convert = Filter::get('convert');
$zip = Filter::get('zip');
$media = Filter::get('media');
$media_path = Filter::get('media-path');
$privatize_export = Filter::get('privatize_export', 'none|visitor|user|gedadmin');
if ($action === 'download') {
switch ($privatize_export) {
default:
case 'gedadmin':
$access_level = Auth::PRIV_NONE;
break;
case 'user':
$access_level = Auth::PRIV_USER;
break;
case 'visitor':
$access_level = Auth::PRIV_PRIVATE;
break;
case 'none':
$access_level = Auth::PRIV_HIDE;
break;
}
$exportOptions = [
'privatize' => $privatize_export,
'toANSI' => $convert === 'on' ? 'yes' : 'no',
'path' => $media_path,
];
// What to call the downloaded file
$download_filename = $controller->tree()->getName();
if (strtolower(substr($download_filename, -4, 4)) != '.ged') {
$download_filename .= '.ged';
}
if ($zip === 'on' || $media === 'on') {
// Export the GEDCOM to an in-memory stream.
$tmp_stream = tmpfile();
FunctionsExport::exportGedcom($controller->tree(), $tmp_stream, $exportOptions);
rewind($tmp_stream);
// Create a new/empty .ZIP file
$temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-');
$zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file));
$zip_filesystem->writeStream($download_filename, $tmp_stream);
if ($media === 'on') {
$rows = Database::prepare(
"SELECT m_id, m_gedcom FROM `##media` WHERE m_file = :tree_id AND m_filename NOT LIKE '%://%'"
)->execute([
'tree_id' => $controller->tree()->getTreeId(),
])->fetchAll();
$path = $controller->tree()->getPreference('MEDIA_DIRECTORY');
foreach ($rows as $row) {
$record = Media::getInstance($row->m_id, $controller->tree(), $row->m_gedcom);
if ($record->canShow($access_level) && file_exists($record->getServerFilename())) {
$fp = fopen($record->getServerFilename(), 'r');
$zip_filesystem->writeStream($path . $record->getFilename(), $fp);
fclose($fp);
}
}
}
// The ZipArchiveAdapter may or may not close the stream.
if (is_resource($tmp_stream)) {
fclose($tmp_stream);
}
// Need to force-close the filesystem
$zip_filesystem = null;
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $download_filename . '.zip"');
header('Content-length: ' . filesize($temp_zip_file));
readfile($temp_zip_file);
unlink($temp_zip_file);
} else {
header('Content-Type: text/plain; charset=UTF-8');
header('Content-Disposition: attachment; filename="' . $download_filename . '"');
// Stream the GEDCOM file straight to the browser.
$stream = fopen('php://output', 'w');
FunctionsExport::exportGedcom($controller->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="<?= $controller->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="<?= $controller->tree()->getNameHtml() ?>">
<!-- DOWNLOAD OPTIONS -->
<fieldset class="form-group">
<div class="row">
<legend class="col-form-legend col-sm-3">
<?= I18N::translate('Export preferences') ?>
</legend>
<div class="col-sm-9">
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="zip">
<?= I18N::translate('Compress the GEDCOM file') ?>
</label>
</div>
<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>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="media">
<?= I18N::translate('Include media (automatically zips files) ') ?>
</label>
</div>
<?php if ($controller->tree()->getPreference('GEDCOM_MEDIA_PATH')): ?>
<label>
<input type="checkbox" name="media-path" value="<?= Html::escape($controller->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($controller->tree()->getPreference('GEDCOM_MEDIA_PATH')) . '</code>') ?>
</p>
<?php endif ?>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="convert">
<?= I18N::translate('Convert from UTF-8 to ISO-8859-1') ?>
</label>
</div>
<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>
</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">
<div class="form-check form-check-inline">
<label>
<input type="radio" name="privatize_export" value="none" checked>
<?= I18N::translate('None') ?>
</label>
</div>
<div class="form-check form-check-inline">
<label>
<input type="radio" name="privatize_export" value="gedadmin">
<?= I18N::translate('Manager') ?>
</label>
</div>
<div class="form-check form-check-inline">
<label>
<input type="radio" name="privatize_export" value="user">
<?= I18N::translate('Member') ?>
</label>
</div>
<div class="form-check form-check-inline">
<label>
<input type="radio" name="privatize_export" value="visitor">
<?= I18N::translate('Visitor') ?>
</label>
</div>
</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>
|