forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_trees_manage.php
276 lines (260 loc) · 11.4 KB
/
admin_trees_manage.php
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
// UI for online updating of the GEDCOM configuration.
//
// webtrees: Web based Family History software
// Copyright (C) 2013 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
define('WT_SCRIPT_NAME', 'admin_trees_manage.php');
require './includes/session.php';
require WT_ROOT.'includes/functions/functions_edit.php';
$controller=new WT_Controller_Page();
$controller
->requireAdminLogin()
->setPageTitle(WT_I18N::translate('Family trees'));
// Don’t allow the user to cancel the request. We do not want to be left
// with an incomplete transaction.
ignore_user_abort(true);
// $path is the full path to the (possibly temporary) file.
// $filename is the actual filename (no folder).
function import_gedcom_file($gedcom_id, $path, $filename) {
// Read the file in blocks of roughly 64K. Ensure that each block
// contains complete gedcom records. This will ensure we don’t split
// multi-byte characters, as well as simplifying the code to import
// each block.
$file_data='';
$fp=fopen($path, 'rb');
WT_DB::exec("START TRANSACTION");
WT_DB::prepare("DELETE FROM `##gedcom_chunk` WHERE gedcom_id=?")->execute(array($gedcom_id));
while (!feof($fp)) {
$file_data.=fread($fp, 65536);
// There is no strrpos() function that searches for substrings :-(
for ($pos=strlen($file_data)-1; $pos>0; --$pos) {
if ($file_data[$pos]=='0' && ($file_data[$pos-1]=="\n" || $file_data[$pos-1]=="\r")) {
// We’ve found the last record boundary in this chunk of data
break;
}
}
if ($pos) {
WT_DB::prepare(
"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
)->execute(array($gedcom_id, substr($file_data, 0, $pos)));
$file_data=substr($file_data, $pos);
}
}
WT_DB::prepare(
"INSERT INTO `##gedcom_chunk` (gedcom_id, chunk_data) VALUES (?, ?)"
)->execute(array($gedcom_id, $file_data));
set_gedcom_setting($gedcom_id, 'gedcom_filename', $filename);
WT_DB::exec("COMMIT");
fclose($fp);
}
// Process POST actions
switch (WT_Filter::post('action')) {
case 'delete':
$gedcom_id = WT_Filter::postInteger('gedcom_id');
if (WT_Filter::checkCsrf() && $gedcom_id) {
WT_Tree::delete($gedcom_id);
}
header('Location: '.WT_SERVER_NAME.WT_SCRIPT_PATH.WT_SCRIPT_NAME);
break;
case 'setdefault':
if (WT_Filter::checkCsrf()) {
WT_Site::preference('DEFAULT_GEDCOM', WT_Filter::post('default_ged'));
}
break;
case 'new_tree':
$ged_name=basename(WT_Filter::post('ged_name'));
if (WT_Filter::checkCsrf() && $ged_name) {
WT_Tree::create($ged_name);
}
break;
case 'replace_upload':
$gedcom_id = WT_Filter::postInteger('gedcom_id');
// Make sure the gedcom still exists
if (WT_Filter::checkCsrf() && get_gedcom_from_id($gedcom_id)) {
foreach ($_FILES as $FILE) {
if ($FILE['error'] == 0 && is_readable($FILE['tmp_name'])) {
import_gedcom_file($gedcom_id, $FILE['tmp_name'], $FILE['name']);
}
}
}
header('Location: '.WT_SERVER_NAME.WT_SCRIPT_PATH.WT_SCRIPT_NAME.'?keep_media'.$gedcom_id.'='.WT_Filter::postBool('keep_media'.$gedcom_id));
exit;
case 'replace_import':
$gedcom_id = WT_Filter::postInteger('gedcom_id');
// Make sure the gedcom still exists
if (WT_Filter::checkCsrf() && get_gedcom_from_id($gedcom_id)) {
$ged_name = basename(WT_Filter::post('ged_name'));
import_gedcom_file($gedcom_id, WT_DATA_DIR.$ged_name, $ged_name);
}
header('Location: '.WT_SERVER_NAME.WT_SCRIPT_PATH.WT_SCRIPT_NAME.'?keep_media'.$gedcom_id.'='.WT_Filter::postBool('keep_media'.$gedcom_id));
exit;
}
$controller->pageHeader();
// Process GET actions
switch (WT_Filter::get('action')) {
case 'uploadform':
case 'importform':
$gedcom_id=WT_Filter::getInteger('gedcom_id');
$gedcom_name=get_gedcom_from_id($gedcom_id);
// Check it exists
if (!$gedcom_name) {
break;
}
echo '<p>', WT_I18N::translate('This will delete all the genealogical data from <b>%s</b> and replace it with data from another GEDCOM.', $gedcom_name), '</p>';
// the javascript in the next line strips any path associated with the file before comparing it to the current GEDCOM name (both Chrome and IE8 include c:\fakepath\ in the filename).
$previous_gedcom_filename=get_gedcom_setting($gedcom_id, 'gedcom_filename');
echo '<form name="replaceform" method="post" enctype="multipart/form-data" action="', WT_SCRIPT_NAME, '" onsubmit="var newfile = document.replaceform.ged_name.value; newfile = newfile.substr(newfile.lastIndexOf(\'\\\\\')+1); if (newfile!=\'', WT_Filter::escapeHtml($previous_gedcom_filename), '\' && \'\' != \'', WT_Filter::escapeHtml($previous_gedcom_filename), '\') return confirm(\'', WT_Filter::escapeHtml(WT_I18N::translate('You have selected a GEDCOM with a different name. Is this correct?')), '\'); else return true;">';
echo '<input type="hidden" name="gedcom_id" value="', $gedcom_id, '">';
echo WT_Filter::getCsrf();
if (WT_Filter::get('action')=='uploadform') {
echo '<input type="hidden" name="action" value="replace_upload">';
echo '<input type="file" name="ged_name">';
} else {
echo '<input type="hidden" name="action" value="replace_import">';
$d=opendir(WT_DATA_DIR);
$files=array();
while (($f=readdir($d))!==false) {
if (!is_dir(WT_DATA_DIR.$f) && is_readable(WT_DATA_DIR.$f)) {
$fp=fopen(WT_DATA_DIR.$f, 'rb');
$header=fread($fp, 64);
fclose($fp);
if (preg_match('/^('.WT_UTF8_BOM.')?0 *HEAD/', $header)) {
$files[]=$f;
}
}
}
if ($files) {
sort($files);
echo WT_DATA_DIR, '<select name="ged_name">';
foreach ($files as $file) {
echo '<option value="', WT_Filter::escapeHtml($file), '"';
if ($file==$previous_gedcom_filename) {
echo ' selected="selected"';
}
echo'>', WT_Filter::escapeHtml($file), '</option>';
}
echo '</select>';
} else {
echo '<p>', WT_I18N::translate('No GEDCOM files found. You need to copy files to the <b>%s</b> directory on your server.', WT_DATA_DIR);
echo '</form>';
exit;
}
}
echo '<br><br><input type="checkbox" name="keep_media', $gedcom_id, '" value="1">';
echo WT_I18N::translate('If you have created media objects in webtrees, and have edited your gedcom off-line using a program that deletes media objects, then check this box to merge the current media objects with the new GEDCOM.');
echo '<br><br><input type="submit" value="', WT_I18N::translate('continue'), '">';
echo '</form>';
exit;
}
// List the gedcoms available to this user
foreach (WT_Tree::GetAll() as $tree) {
if (userGedcomAdmin(WT_USER_ID, $tree->tree_id)) {
echo
'<table class="gedcom_table">',
'<tr><th>', WT_I18N::translate('Family tree'),
'</th><th><a class="accepted" href="index.php?ctype=gedcom&ged=', $tree->tree_name_url, '" dir="auto">',
$tree->tree_title_html, '</a>',
'</th></tr><tr><th class="accepted">', $tree->tree_name_html,
'</th><td>';
// The third row shows an optional progress bar and a list of maintenance options
$importing=WT_DB::prepare(
"SELECT 1 FROM `##gedcom_chunk` WHERE gedcom_id=? AND imported=0 LIMIT 1"
)->execute(array($tree->tree_id))->fetchOne();
if ($importing) {
$in_progress=WT_DB::prepare(
"SELECT 1 FROM `##gedcom_chunk` WHERE gedcom_id=? AND imported=1 LIMIT 1"
)->execute(array($tree->tree_id))->fetchOne();
if (!$in_progress) {
echo '<div id="import', $tree->tree_id, '"><div id="progressbar', $tree->tree_id, '"><div style="position:absolute;">', WT_I18N::translate('Deleting old genealogy data…'), '</div></div></div>';
$controller->addInlineJavascript(
'jQuery("#progressbar'.$tree->tree_id.'").progressbar({value: 0});'
);
} else {
echo '<div id="import', $tree->tree_id, '"></div>';
}
$controller->addInlineJavascript(
'jQuery("#import'.$tree->tree_id.'").load("import.php?gedcom_id='.$tree->tree_id.'&keep_media'.$tree->tree_id.'='.WT_Filter::get('keep_media'.$tree->tree_id).'");'
);
echo '<table border="0" width="100%" id="actions', $tree->tree_id, '" style="display:none">';
} else {
echo '<table border="0" width="100%" id="actions', $tree->tree_id, '">';
}
echo
'<tr align="center">',
// export
'<td><a href="admin_trees_export.php?ged=', $tree->tree_name_url, '" onclick="return modalDialog(\'admin_trees_export.php?ged=', $tree->tree_name_url, '\', \'', WT_I18N::translate('Export'), '\');">', WT_I18N::translate('Export'), '</a>',
help_link('export_gedcom'),
'</td>',
// import
'<td><a href="', WT_SCRIPT_NAME, '?action=importform&gedcom_id=', $tree->tree_id, '">', WT_I18N::translate('Import'), '</a>',
help_link('import_gedcom'),
'</td>',
// download
'<td><a href="admin_trees_download.php?ged=', $tree->tree_name_url,'">', WT_I18N::translate('Download'), '</a>',
help_link('download_gedcom'),
'</td>',
// upload
'<td><a href="', WT_SCRIPT_NAME, '?action=uploadform&gedcom_id=', $tree->tree_id, '">', WT_I18N::translate('Upload'), '</a>',
help_link('upload_gedcom'),
'</td>',
// delete
'<td>',
'<a href="#" onclick="if (confirm(\''.WT_Filter::escapeJs(WT_I18N::translate('Are you sure you want to delete “%s”?', $tree->tree_name)),'\')) document.delete_form', $tree->tree_id, '.submit(); return false;">', WT_I18N::translate('Delete'), '</a>',
'<form name="delete_form', $tree->tree_id ,'" method="post" action="', WT_SCRIPT_NAME ,'">',
'<input type="hidden" name="action" value="delete">',
'<input type="hidden" name="gedcom_id" value="', $tree->tree_id, '">',
WT_Filter::getCsrf(),
'</form>',
'</td></tr></table></td></tr></table><br>';
}
}
// Options for creating new gedcoms and setting defaults
if (WT_USER_IS_ADMIN) {
echo '<table class="gedcom_table2"><tr>';
if (count(WT_Tree::GetAll())>1) {
echo '<th>', WT_I18N::translate('Default family tree'), help_link('default_gedcom'), '</th>';
}
echo '<th>', WT_I18N::translate('Create a new family tree'), help_link('add_new_gedcom'), '</th></tr><tr>';
if (count(WT_Tree::GetAll())>1) {
echo
'<td><form name="defaultform" method="post" action="', WT_SCRIPT_NAME, '">',
'<input type="hidden" name="action" value="setdefault">',
WT_Filter::getCsrf(),
select_edit_control('default_ged', WT_Tree::getNameList(), '', WT_Site::preference('DEFAULT_GEDCOM'), 'onchange="document.defaultform.submit();"'),
'</form></td>';
}
echo
'<td class="button">',
'<form name="createform" method="post" action="', WT_SCRIPT_NAME, '">',
WT_Filter::getCsrf(),
'<input type="hidden" name="action" value="new_tree">',
'<input name="ged_name">',
' <input type="submit" value="', WT_I18N::translate('save') , '">',
'</form>',
'</td>',
'</tr></table><br>';
// display link to PGV-WT transfer wizard on first visit to this page, before any GEDCOM is loaded
if (count(WT_Tree::GetAll())==0 && get_user_count()==1) {
echo
'<div class="center">',
'<a style="color:green; font-weight:bold;" href="admin_pgv_to_wt.php">',
WT_I18N::translate('Click here for PhpGedView to <b>webtrees</b> transfer wizard'),
'</a>',
help_link('PGV_WIZARD'),
'</div>';
}
}