forked from fisharebest/webtrees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.php
470 lines (420 loc) · 16.2 KB
/
action.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?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\Functions\Functions;
use Fisharebest\Webtrees\Functions\FunctionsDb;
use Fisharebest\Webtrees\Functions\FunctionsEdit;
use Fisharebest\Webtrees\Functions\FunctionsImport;
/** @global Tree $WT_TREE */
global $WT_TREE;
require 'includes/session.php';
if (!Filter::checkCsrf()) {
http_response_code(406);
return;
}
switch (Filter::post('action', null, Filter::get('action'))) {
case 'accept-changes':
// Accept all the pending changes for a record
$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
if ($record && Auth::isModerator($record->getTree()) && $record->canShow() && $record->canEdit()) {
if ($record->isPendingDeletion()) {
FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */
I18N::translate('“%s” has been deleted.', $record->getFullName()));
} else {
FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */
I18N::translate('The changes to “%s” have been accepted.', $record->getFullName()));
}
FunctionsImport::acceptAllChanges($record->getXref(), $record->getTree()->getTreeId());
} else {
http_response_code(406);
}
break;
case 'copy-fact':
// Copy a fact to the clipboard
$xref = Filter::post('xref', WT_REGEX_XREF);
$fact_id = Filter::post('fact_id');
$record = GedcomRecord::getInstance($xref, $WT_TREE);
if ($record && $record->canEdit()) {
foreach ($record->getFacts() as $fact) {
if ($fact->getFactId() == $fact_id) {
switch ($fact->getTag()) {
case 'NOTE':
case 'SOUR':
case 'OBJE':
$type = 'all'; // paste this anywhere
break;
default:
$type = $record::RECORD_TYPE; // paste only to the same record type
break;
}
$clipboard = Session::get('clipboard');
if (!is_array($clipboard)) {
$clipboard = [];
}
$clipboard[$fact_id] = [
'type' => $type,
'factrec' => $fact->getGedcom(),
'fact' => $fact->getTag(),
];
// The clipboard only holds 10 facts
while (count($clipboard) > 10) {
array_shift($clipboard);
}
Session::put('clipboard', $clipboard);
FlashMessages::addMessage(I18N::translate('The record has been copied to the clipboard.'));
break 2;
}
}
}
break;
case 'create-media-object':
$MEDIA_DIRECTORY = $WT_TREE->getPreference('MEDIA_DIRECTORY');
// Create a media object, and return parameters needed by Select2
header('Content-type: application/json');
$auto = Filter::post('auto');
$folder = Filter::post('folder');
$note = Filter::post('note');
$type = Filter::post('type');
$title = Filter::post('title');
// No file uploaded?
if (empty($_FILES['file']) || !is_uploaded_file($_FILES['file']['tmp_name'])) {
http_response_code(406);
break;
}
// The filename
$file = $_FILES['file']['name'];
$format = strtolower(pathinfo($file, PATHINFO_EXTENSION));
$format = strtr($format, ['jpg' => 'jpeg']);
// The folder
$folder = trim($folder, '/');
// If the folder is invalid, or doesn't exist and can't be created, then use the root folder
if (strpos($folder, '..') !== false || !File::mkdir(WT_DATA_DIR . $MEDIA_DIRECTORY . $folder)) {
$folder = '';
}
// Generate a unique name for the file?
if ($auto === '1' || file_exists(WT_DATA_DIR . $MEDIA_DIRECTORY . $folder . $file)) {
$folder = '';
$file = sha1($_FILES['file']['tmp_name']) . '.' . $format;
}
if ($folder !== '') {
$folder .= '/';
}
if (move_uploaded_file($_FILES['file']['tmp_name'], WT_DATA_DIR . $MEDIA_DIRECTORY . $folder . $file)) {
Log::addMediaLog('Media file ' . $_FILES['file']['name'] . ' uploaded');
} else {
FlashMessages::addMessage(
I18N::translate('There was an error uploading your file.') .
'<br>' .
Functions::fileUploadErrorText($_FILES['mediafile']['error'])
);
break;
}
$gedcom = "0 @new@ OBJE\n1 FILE " . $folder . $file . "\n2 FORM " . $format;
if ($type !== '') {
$gedcom .= "\n3 TYPE " . $type;
}
if ($title !== '') {
$gedcom .= "\n2 TITL " . $title;
}
if ($note !== '') {
$gedcom .= "\n1 NOTE " . preg_replace('/\r?\n/', "\n2 CONT ", $note);
}
$media_object = $WT_TREE->createRecord($gedcom);
// Accept the new record. Rejecting it would leave the filesystem out-of-sync with the genealogy
FunctionsImport::acceptAllChanges($media_object->getXref(), $media_object->getTree()->getTreeId());
echo json_encode(['id' => $media_object->getXref(), 'text' => View::make('selects/media', ['media' => $media_object])]);
break;
case 'create-media-object-from-file':
$file = Filter::post('file');
$type = Filter::post('type');
$title = Filter::post('title');
$note = Filter::post('note');
if (preg_match('/\.([a-zA-Z0-9]+)$/', $file, $match)) {
$format = ' ' . $match[1];
} else {
$format = '';
}
$gedcom = "0 @new@ OBJE\n1 FILE " . $file . "\n2 FORM " . $format;
if ($type !== '') {
$gedcom .= "\n3 TYPE " . $type;
}
if ($title !== '') {
$gedcom .= "\n2 TITL " . $title;
}
if ($note !== '') {
$gedcom .= "\n1 NOTE " . preg_replace('/\r?\n/', "\n2 CONT ", $note);
}
$media_object = $WT_TREE->createRecord($gedcom);
// Accept the new record. Rejecting it would leave the filesystem out-of-sync with the genealogy
FunctionsImport::acceptAllChanges($media_object->getXref(), $media_object->getTree()->getTreeId());
header('Location: admin_media.php?files=unused');
break;
case 'create-note-object':
// Create a note, and return parameters needed by Select2
header('Content-type: application/json');
$note = Filter::post('note');
$gedcom = "0 @new@ NOTE " . str_replace("\n", "\n1 CONT ", $note);
$note_object = $WT_TREE->createRecord($gedcom);
echo json_encode(['id' => $note_object->getXref(), 'text' => View::make('selects/note', ['note' => $note_object])]);
break;
case 'create-repository':
// Create a repository, and return parameters needed by Select2
header('Content-type: application/json');
$repository_name = Filter::post('repository_name');
$gedcom = "0 @new@ REPO\n1 NAME " . $repository_name;
$repository = $WT_TREE->createRecord($gedcom);
echo json_encode(['id' => $repository->getXref(), 'text' => View::make('selects/repository', ['repository' => $repository])]);
break;
case 'create-source':
// Create a source, and return parameters needed by Select2
header('Content-type: application/json');
$TITL = Filter::post('TITL');
$ABBR = Filter::post('ABBR');
$AUTH = Filter::post('AUTH');
$PUBL = Filter::post('PUBL');
$TEXT = Filter::post('TEXT');
$REPO = Filter::post('REPO', WT_REGEX_XREF);
$CALN = Filter::post('CALN');
$gedcom = '0 @new@ SOUR';
if ($TITL !== '') {
$gedcom .= "\n1 TITL " . $TITL;
}
if ($ABBR !== '') {
$gedcom .= "\n1 ABBR " . $ABBR;
}
if ($AUTH !== '') {
$gedcom .= "\n1 AUTH " . $AUTH;
}
if ($PUBL !== '') {
$gedcom .= "\n1 PUBL " . $PUBL;
}
if ($TEXT !== '') {
$gedcom .= "\n1 TEXT " . str_replace("\n", "\n2 CONT ", $TEXT);
}
if ($REPO !== '') {
$gedcom .= "\n1 REPO @" . $REPO . '@';
if ($CALN !== '') {
$gedcom .= "\n2 CALN " . $CALN;
}
}
$source = $WT_TREE->createRecord($gedcom);
echo json_encode(['id' => $source->getXref(), 'text' => View::make('selects/source', ['source' => $source])]);
break;
case 'create-submitter':
// Create a submitter, and return parameters needed by Select2
header('Content-type: application/json');
$gedcom = '0 @new@ SUBM';
$submitter_name = Filter::post('submitter_name', null, '');
if ($submitter_name !== '') {
$gedcom .= "\n1 NAME " . $submitter_name;
}
$submitter_address = Filter::post('submitter_address', null, '');
if ($submitter_address !== '') {
$gedcom .= "\n1 ADDR " . $submitter_address;
}
$submitter = $WT_TREE->createRecord($gedcom);
echo json_encode(['id' => $submitter->getXref(), 'text' => View::make('selects/submitter', ['submitter' => $submitter])]);
break;
case 'paste-fact':
// Paste a fact from the clipboard
$xref = Filter::post('xref', WT_REGEX_XREF);
$fact_id = Filter::post('fact_id');
$record = GedcomRecord::getInstance($xref, $WT_TREE);
$clipboard = Session::get('clipboard');
if ($record && $record->canEdit() && isset($clipboard[$fact_id])) {
$record->createFact($clipboard[$fact_id]['factrec'], true);
}
break;
case 'delete-fact':
$xref = Filter::post('xref', WT_REGEX_XREF);
$fact_id = Filter::post('fact_id');
$record = GedcomRecord::getInstance($xref, $WT_TREE);
if ($record && $record->canShow() && $record->canEdit()) {
foreach ($record->getFacts() as $fact) {
if ($fact->getFactId() == $fact_id && $fact->canShow() && $fact->canEdit()) {
$record->deleteFact($fact_id, true);
break 2;
}
}
}
// Can’t find the record/fact, or don’t have permission to delete it.
http_response_code(406);
break;
case 'delete-record':
$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
if ($record && Auth::isEditor($record->getTree()) && $record->canShow() && $record->canEdit()) {
// Delete links to this record
foreach (FunctionsDb::fetchAllLinks($record->getXref(), $record->getTree()->getTreeId()) as $xref) {
$linker = GedcomRecord::getInstance($xref, $WT_TREE);
$old_gedcom = $linker->getGedcom();
$new_gedcom = FunctionsEdit::removeLinks($old_gedcom, $record->getXref());
// FunctionsDb::fetch_all_links() does not take account of pending changes. The links (or even the
// record itself) may have already been deleted.
if ($old_gedcom !== $new_gedcom) {
// If we have removed a link from a family to an individual, and it has only one member
if (preg_match('/^0 @' . WT_REGEX_XREF . '@ FAM/', $new_gedcom) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . WT_REGEX_XREF . ')@/', $new_gedcom, $match) == 1) {
// Delete the family
$family = GedcomRecord::getInstance($xref, $WT_TREE);
FlashMessages::addMessage(/* I18N: %s is the name of a family group, e.g. “Husband name + Wife name” */ I18N::translate('The family “%s” has been deleted because it only has one member.', $family->getFullName()));
$family->deleteRecord();
// Delete any remaining link to this family
if ($match) {
$relict = GedcomRecord::getInstance($match[2][0], $WT_TREE);
$new_gedcom = $relict->getGedcom();
$new_gedcom = FunctionsEdit::removeLinks($new_gedcom, $linker->getXref());
$relict->updateRecord($new_gedcom, false);
FlashMessages::addMessage(/* I18N: %s are names of records, such as sources, repositories or individuals */ I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', $relict->getFullName(), $family->getFullName()));
}
} else {
// Remove links from $linker to $record
FlashMessages::addMessage(/* I18N: %s are names of records, such as sources, repositories or individuals */ I18N::translate('The link from “%1$s” to “%2$s” has been deleted.', $linker->getFullName(), $record->getFullName()));
$linker->updateRecord($new_gedcom, false);
}
}
}
// Delete the record itself
$record->deleteRecord();
} else {
http_response_code(406);
}
break;
case 'delete-user':
$user = User::find(Filter::postInteger('user_id'));
if ($user && Auth::isAdmin() && Auth::user() !== $user) {
Log::addAuthenticationLog('Deleted user: ' . $user->getUserName());
$user->delete();
}
break;
case 'language':
// Change the current language
$language = Filter::post('language');
try {
I18N::init($language);
Session::put('locale', $language);
// Remember our selection
Auth::user()->setPreference('language', $language);
} catch (\Exception $ex) {
// Request for a non-existant language.
http_response_code(406);
}
break;
case 'masquerade':
$user = User::find(Filter::postInteger('user_id'));
if ($user && Auth::isAdmin() && Auth::user() !== $user) {
Log::addAuthenticationLog('Masquerade as user: ' . $user->getUserName());
Auth::login($user);
Session::put('masquerade', '1');
} else {
http_response_code(406);
}
break;
case 'unlink-media':
// Remove links from an individual and their spouse-family records to a media object.
// Used by the "unlink" option on the album (lightbox) tab.
$source = Individual::getInstance(Filter::post('source', WT_REGEX_XREF), $WT_TREE);
$target = Filter::post('target', WT_REGEX_XREF);
if ($source && $source->canShow() && $source->canEdit() && $target) {
// Consider the individual and their spouse-family records
$sources = $source->getSpouseFamilies();
$sources[] = $source;
foreach ($sources as $source) {
foreach ($source->getFacts() as $fact) {
if (!$fact->isPendingDeletion()) {
if ($fact->getValue() == '@' . $target . '@') {
// Level 1 links
$source->deleteFact($fact->getFactId(), true);
} elseif (strpos($fact->getGedcom(), ' @' . $target . '@')) {
// Level 2-3 links
$source->updateFact($fact->getFactId(), preg_replace(['/\n2 OBJE @' . $target . '@(\n[3-9].*)*/', '/\n3 OBJE @' . $target . '@(\n[4-9].*)*/'], '', $fact->getGedcom()), true);
}
}
}
}
} else {
http_response_code(406);
}
break;
case 'reject-changes':
// Reject all the pending changes for a record
$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
if ($record && $record->canEdit() && Auth::isModerator($record->getTree())) {
FlashMessages::addMessage(/* I18N: %s is the name of an individual, source or other record */ I18N::translate('The changes to “%s” have been rejected.', $record->getFullName()));
FunctionsImport::rejectAllChanges($record);
} else {
http_response_code(406);
}
break;
case 'select2-family':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::familySearch($WT_TREE, $page, $query));
break;
case 'select2-flag':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::flagSearch($page, $query));
break;
case 'select2-individual':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::individualSearch($WT_TREE, $page, $query));
break;
case 'select2-media':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::mediaObjectSearch($WT_TREE, $page, $query));
break;
case 'select2-note':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::noteSearch($WT_TREE, $page, $query));
break;
case 'select2-place':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::placeSearch($WT_TREE, $page, $query, true));
break;
case 'select2-repository':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::repositorySearch($WT_TREE, $page, $query));
break;
case 'select2-source':
$page = Filter::postInteger('page');
$query = Filter::post('q', null, '');
header('Content-Type: application/json');
echo json_encode(Select2::sourceSearch($WT_TREE, $page, $query));
break;
case 'theme':
// Change the current theme
$theme = Filter::post('theme');
if (Site::getPreference('ALLOW_USER_THEMES') === '1' && array_key_exists($theme, Theme::themeNames())) {
Session::put('theme_id', $theme);
// Remember our selection
Auth::user()->setPreference('theme', $theme);
} else {
// Request for a non-existant theme.
http_response_code(406);
}
break;
}