Test Failed
Branch master (4a3c5b)
by Greg
12:31
created

action.php (6 issues)

Labels
Severity
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2017 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
namespace Fisharebest\Webtrees;
17
18
use Fisharebest\Webtrees\Functions\Functions;
19
use Fisharebest\Webtrees\Functions\FunctionsDb;
20
use Fisharebest\Webtrees\Functions\FunctionsEdit;
21
use Fisharebest\Webtrees\Functions\FunctionsImport;
22
use Fisharebest\Webtrees\Http\Controllers\EditController;
23
use Symfony\Component\HttpFoundation\Request;
24
25
/** @global Tree $WT_TREE */
26
global $WT_TREE;
27
28
require 'includes/session.php';
29
30
if (!Filter::checkCsrf()) {
31
	http_response_code(406);
32
33
	return;
34
}
35
36
switch (Filter::post('action', null, Filter::get('action'))) {
37
case 'accept-changes':
38
	// Accept all the pending changes for a record
39
	$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
40
	if ($record && Auth::isModerator($record->getTree()) && $record->canShow() && $record->canEdit()) {
41
		if ($record->isPendingDeletion()) {
42
			FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */
43
				I18N::translate('“%s” has been deleted.', $record->getFullName()));
44
		} else {
45
			FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */
46
				I18N::translate('The changes to “%s” have been accepted.', $record->getFullName()));
47
		}
48
		FunctionsImport::acceptAllChanges($record->getXref(), $record->getTree()->getTreeId());
49
	} else {
50
		http_response_code(406);
51
	}
52
	break;
53
54
case 'copy-fact':
55
	// Copy a fact to the clipboard
56
	$xref    = Filter::post('xref', WT_REGEX_XREF);
57
	$fact_id = Filter::post('fact_id');
58
59
	$record = GedcomRecord::getInstance($xref, $WT_TREE);
60
61
	if ($record && $record->canEdit()) {
62
		foreach ($record->getFacts() as $fact) {
63
			if ($fact->getFactId() == $fact_id) {
64
				switch ($fact->getTag()) {
65
				case 'NOTE':
66
				case 'SOUR':
67
				case 'OBJE':
68
					$type = 'all'; // paste this anywhere
69
					break;
70
				default:
71
					$type = $record::RECORD_TYPE; // paste only to the same record type
72
					break;
73
				}
74
				$clipboard = Session::get('clipboard');
75
				if (!is_array($clipboard)) {
76
					$clipboard = [];
77
				}
78
				$clipboard[$fact_id] = [
79
					'type'    => $type,
80
					'factrec' => $fact->getGedcom(),
81
					'fact'    => $fact->getTag(),
82
					];
83
				// The clipboard only holds 10 facts
84
				while (count($clipboard) > 10) {
85
					array_shift($clipboard);
86
				}
87
				Session::put('clipboard', $clipboard);
88
				FlashMessages::addMessage(I18N::translate('The record has been copied to the clipboard.'));
89
				break 2;
90
			}
91
		}
92
	}
93
	break;
94
95
case 'create-media-object':
96
	$request         = Request::createFromGlobals();
97
	$request->attributes->set('tree', $WT_TREE);
98
	$edit_controller = new EditController;
99
	$response        = $edit_controller->createMediaFile($request);
100
	$response->prepare($request)->send();
101
	break;
102
103
case 'create-media-object-from-file':
104
	$file  = Filter::post('file');
105
	$type  = Filter::post('type');
106
	$title = Filter::post('title');
107
	$note  = Filter::post('note');
108
109
	if (preg_match('/\.([a-zA-Z0-9]+)$/', $file, $match)) {
110
		$format = ' ' . $match[1];
111
	} else {
112
		$format = '';
113
	}
114
115
	$gedcom = "0 @new@ OBJE\n1 FILE " . $file . "\n2 FORM " . $format;
116
117
	if ($type !== '') {
118
		$gedcom .= "\n3 TYPE " . $type;
119
	}
120
121
	if ($title !== '') {
122
		$gedcom .= "\n2 TITL " . $title;
123
	}
124
	if ($note !== '') {
125
		$gedcom .= "\n1 NOTE " . preg_replace('/\r?\n/', "\n2 CONT ", $note);
126
	}
127
	$media_object = $WT_TREE->createRecord($gedcom);
128
	// Accept the new record.  Rejecting it would leave the filesystem out-of-sync with the genealogy
129
	FunctionsImport::acceptAllChanges($media_object->getXref(), $media_object->getTree()->getTreeId());
130
	header('Location: admin_media.php?files=unused');
131
	break;
132
133 View Code Duplication
case 'create-note-object':
134
	// Create a note, and return parameters needed by Select2
135
	header('Content-type: application/json');
136
	$note        = Filter::post('note');
137
	$gedcom      = "0 @new@ NOTE " . str_replace("\n", "\n1 CONT ", $note);
138
	$note_object = $WT_TREE->createRecord($gedcom);
139
	echo json_encode(['id' => $note_object->getXref(), 'text' => View::make('selects/note', ['note' => $note_object])]);
140
	break;
141
142 View Code Duplication
case 'create-repository':
143
	// Create a repository, and return parameters needed by Select2
144
	header('Content-type: application/json');
145
	$repository_name = Filter::post('repository_name');
146
	$gedcom          = "0 @new@ REPO\n1 NAME " . $repository_name;
147
	$repository      = $WT_TREE->createRecord($gedcom);
148
	echo json_encode(['id' => $repository->getXref(), 'text' => View::make('selects/repository', ['repository' => $repository])]);
149
	break;
150
151
case 'create-source':
152
	// Create a source, and return parameters needed by Select2
153
	header('Content-type: application/json');
154
	$TITL   = Filter::post('TITL');
155
	$ABBR   = Filter::post('ABBR');
156
	$AUTH   = Filter::post('AUTH');
157
	$PUBL   = Filter::post('PUBL');
158
	$TEXT   = Filter::post('TEXT');
159
	$REPO   = Filter::post('REPO', WT_REGEX_XREF);
160
	$CALN   = Filter::post('CALN');
161
	$gedcom = '0 @new@ SOUR';
162
	if ($TITL !== '') {
163
		$gedcom .= "\n1 TITL " . $TITL;
164
	}
165
	if ($ABBR !== '') {
166
		$gedcom .= "\n1 ABBR " . $ABBR;
167
	}
168
	if ($AUTH !== '') {
169
		$gedcom .= "\n1 AUTH " . $AUTH;
170
	}
171
	if ($PUBL !== '') {
172
		$gedcom .= "\n1 PUBL " . $PUBL;
173
	}
174
	if ($TEXT !== '') {
175
		$gedcom .= "\n1 TEXT " . str_replace("\n", "\n2 CONT ", $TEXT);
176
	}
177
	if ($REPO !== '') {
178
		$gedcom .= "\n1 REPO @" . $REPO . '@';
179
		if ($CALN !== '') {
180
			$gedcom .= "\n2 CALN " . $CALN;
181
		}
182
	}
183
	$source = $WT_TREE->createRecord($gedcom);
184
	echo json_encode(['id' => $source->getXref(), 'text' => View::make('selects/source', ['source' => $source])]);
185
	break;
186
187
case 'create-submitter':
188
	// Create a submitter, and return parameters needed by Select2
189
	header('Content-type: application/json');
190
	$gedcom         = '0 @new@ SUBM';
191
	$submitter_name = Filter::post('submitter_name', null, '');
192
	if ($submitter_name !== '') {
193
		$gedcom .= "\n1 NAME " . $submitter_name;
194
	}
195
	$submitter_address = Filter::post('submitter_address', null, '');
196
	if ($submitter_address !== '') {
197
		$gedcom .= "\n1 ADDR " . $submitter_address;
198
	}
199
	$submitter = $WT_TREE->createRecord($gedcom);
200
	echo json_encode(['id' => $submitter->getXref(), 'text' => View::make('selects/submitter', ['submitter' => $submitter])]);
201
	break;
202
203
case 'paste-fact':
204
	// Paste a fact from the clipboard
205
	$xref      = Filter::post('xref', WT_REGEX_XREF);
206
	$fact_id   = Filter::post('fact_id');
207
	$record    = GedcomRecord::getInstance($xref, $WT_TREE);
208
	$clipboard = Session::get('clipboard');
209
210
	if ($record && $record->canEdit() && isset($clipboard[$fact_id])) {
211
		$record->createFact($clipboard[$fact_id]['factrec'], true);
212
	}
213
	break;
214
215
case 'delete-fact':
216
	$xref    = Filter::post('xref', WT_REGEX_XREF);
217
	$fact_id = Filter::post('fact_id');
218
219
	$record = GedcomRecord::getInstance($xref, $WT_TREE);
220
	if ($record && $record->canShow() && $record->canEdit()) {
221
		foreach ($record->getFacts() as $fact) {
222
			if ($fact->getFactId() == $fact_id && $fact->canShow() && $fact->canEdit()) {
223
				$record->deleteFact($fact_id, true);
224
				break 2;
225
			}
226
		}
227
	}
228
229
	// Can’t find the record/fact, or don’t have permission to delete it.
230
	http_response_code(406);
231
	break;
232
233
case 'delete-record':
234
	$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
235
	if ($record && Auth::isEditor($record->getTree()) && $record->canShow() && $record->canEdit()) {
236
		// Delete links to this record
237
		foreach (FunctionsDb::fetchAllLinks($record->getXref(), $record->getTree()->getTreeId()) as $xref) {
238
			$linker     = GedcomRecord::getInstance($xref, $WT_TREE);
239
			$old_gedcom = $linker->getGedcom();
240
			$new_gedcom = FunctionsEdit::removeLinks($old_gedcom, $record->getXref());
241
			// FunctionsDb::fetch_all_links() does not take account of pending changes. The links (or even the
242
			// record itself) may have already been deleted.
243
			if ($old_gedcom !== $new_gedcom) {
244
				// If we have removed a link from a family to an individual, and it has only one member
245
				if (preg_match('/^0 @' . WT_REGEX_XREF . '@ FAM/', $new_gedcom) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . WT_REGEX_XREF . ')@/', $new_gedcom, $match) == 1) {
246
					// Delete the family
247
					$family = GedcomRecord::getInstance($xref, $WT_TREE);
248
					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()));
249
					$family->deleteRecord();
250
					// Delete any remaining link to this family
251
					if ($match) {
252
						$relict     = GedcomRecord::getInstance($match[2][0], $WT_TREE);
253
						$new_gedcom = $relict->getGedcom();
254
						$new_gedcom = FunctionsEdit::removeLinks($new_gedcom, $linker->getXref());
255
						$relict->updateRecord($new_gedcom, false);
256
						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()));
257
					}
258
				} else {
259
					// Remove links from $linker to $record
260
					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()));
261
					$linker->updateRecord($new_gedcom, false);
262
				}
263
			}
264
		}
265
		// Delete the record itself
266
		$record->deleteRecord();
267
	} else {
268
		http_response_code(406);
269
	}
270
	break;
271
272
case 'delete-user':
273
	$user = User::find(Filter::postInteger('user_id'));
274
275
	if ($user && Auth::isAdmin() && Auth::user() !== $user) {
276
		Log::addAuthenticationLog('Deleted user: ' . $user->getUserName());
277
		$user->delete();
278
	}
279
	break;
280
281
case 'language':
282
	// Change the current language
283
	$language = Filter::post('language');
284
	try {
285
		I18N::init($language);
286
		Session::put('locale', $language);
287
		// Remember our selection
288
		Auth::user()->setPreference('language', $language);
289
	} catch (\Exception $ex) {
290
		DebugBar::addThrowable($ex);
291
292
		// Request for a non-existant language.
293
		http_response_code(406);
294
	}
295
	break;
296
297
case 'masquerade':
298
	$user = User::find(Filter::postInteger('user_id'));
299
300
	if ($user && Auth::isAdmin() && Auth::user() !== $user) {
301
		Log::addAuthenticationLog('Masquerade as user: ' . $user->getUserName());
302
		Auth::login($user);
303
		Session::put('masquerade', '1');
304
	} else {
305
		http_response_code(406);
306
	}
307
	break;
308
309
case 'unlink-media':
310
	// Remove links from an individual and their spouse-family records to a media object.
311
	// Used by the "unlink" option on the album (lightbox) tab.
312
	$source = Individual::getInstance(Filter::post('source', WT_REGEX_XREF), $WT_TREE);
313
	$target = Filter::post('target', WT_REGEX_XREF);
314
	if ($source && $source->canShow() && $source->canEdit() && $target) {
315
		// Consider the individual and their spouse-family records
316
		$sources   = $source->getSpouseFamilies();
0 ignored issues
show
The method getSpouseFamilies() does not exist on Fisharebest\Webtrees\Repository. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
		/** @scrutinizer ignore-call */ 
317
  $sources   = $source->getSpouseFamilies();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method getSpouseFamilies() does not exist on Fisharebest\Webtrees\Source. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
		/** @scrutinizer ignore-call */ 
317
  $sources   = $source->getSpouseFamilies();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method getSpouseFamilies() does not exist on Fisharebest\Webtrees\Family. Did you maybe mean getSpouse()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
		/** @scrutinizer ignore-call */ 
317
  $sources   = $source->getSpouseFamilies();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method getSpouseFamilies() does not exist on Fisharebest\Webtrees\Note. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
		/** @scrutinizer ignore-call */ 
317
  $sources   = $source->getSpouseFamilies();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method getSpouseFamilies() does not exist on Fisharebest\Webtrees\Media. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
		/** @scrutinizer ignore-call */ 
317
  $sources   = $source->getSpouseFamilies();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
The method getSpouseFamilies() does not exist on Fisharebest\Webtrees\GedcomRecord. It seems like you code against a sub-type of Fisharebest\Webtrees\GedcomRecord such as Fisharebest\Webtrees\Individual. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

316
		/** @scrutinizer ignore-call */ 
317
  $sources   = $source->getSpouseFamilies();
Loading history...
317
		$sources[] = $source;
318
		foreach ($sources as $source) {
319
			foreach ($source->getFacts() as $fact) {
320
				if (!$fact->isPendingDeletion()) {
321
					if ($fact->getValue() == '@' . $target . '@') {
322
						// Level 1 links
323
						$source->deleteFact($fact->getFactId(), true);
324
					} elseif (strpos($fact->getGedcom(), ' @' . $target . '@')) {
325
						// Level 2-3 links
326
						$source->updateFact($fact->getFactId(), preg_replace(['/\n2 OBJE @' . $target . '@(\n[3-9].*)*/', '/\n3 OBJE @' . $target . '@(\n[4-9].*)*/'], '', $fact->getGedcom()), true);
327
					}
328
				}
329
			}
330
		}
331
	} else {
332
		http_response_code(406);
333
	}
334
	break;
335
336
case 'reject-changes':
337
	// Reject all the pending changes for a record
338
	$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
339
	if ($record && $record->canEdit() && Auth::isModerator($record->getTree())) {
340
		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()));
341
		FunctionsImport::rejectAllChanges($record);
342
	} else {
343
		http_response_code(406);
344
	}
345
	break;
346
347 View Code Duplication
case 'select2-family':
348
	$page  = Filter::postInteger('page');
349
	$query = Filter::post('q', null, '');
350
	header('Content-Type: application/json');
351
	echo json_encode(Select2::familySearch($WT_TREE, $page, $query));
352
	break;
353
354 View Code Duplication
case 'select2-flag':
355
	$page  = Filter::postInteger('page');
356
	$query = Filter::post('q', null, '');
357
	header('Content-Type: application/json');
358
	echo json_encode(Select2::flagSearch($page, $query));
359
	break;
360
361 View Code Duplication
case 'select2-individual':
362
	$page  = Filter::postInteger('page');
363
	$query = Filter::post('q', null, '');
364
	header('Content-Type: application/json');
365
	echo json_encode(Select2::individualSearch($WT_TREE, $page, $query));
366
	break;
367
368 View Code Duplication
case 'select2-media':
369
	$page  = Filter::postInteger('page');
370
	$query = Filter::post('q', null, '');
371
	header('Content-Type: application/json');
372
	echo json_encode(Select2::mediaObjectSearch($WT_TREE, $page, $query));
373
	break;
374
375 View Code Duplication
case 'select2-note':
376
	$page  = Filter::postInteger('page');
377
	$query = Filter::post('q', null, '');
378
	header('Content-Type: application/json');
379
	echo json_encode(Select2::noteSearch($WT_TREE, $page, $query));
380
	break;
381
382 View Code Duplication
case 'select2-place':
383
	$page  = Filter::postInteger('page');
384
	$query = Filter::post('q', null, '');
385
	header('Content-Type: application/json');
386
	echo json_encode(Select2::placeSearch($WT_TREE, $page, $query, true));
387
	break;
388
389 View Code Duplication
case 'select2-repository':
390
	$page  = Filter::postInteger('page');
391
	$query = Filter::post('q', null, '');
392
	header('Content-Type: application/json');
393
	echo json_encode(Select2::repositorySearch($WT_TREE, $page, $query));
394
	break;
395
396 View Code Duplication
case 'select2-source':
397
	$page  = Filter::postInteger('page');
398
	$query = Filter::post('q', null, '');
399
	header('Content-Type: application/json');
400
	echo json_encode(Select2::sourceSearch($WT_TREE, $page, $query));
401
	break;
402
403
case 'theme':
404
	// Change the current theme
405
	$theme = Filter::post('theme');
406
	if (Site::getPreference('ALLOW_USER_THEMES') === '1' && array_key_exists($theme, Theme::themeNames())) {
407
		Session::put('theme_id', $theme);
408
		// Remember our selection
409
		Auth::user()->setPreference('theme', $theme);
410
	} else {
411
		// Request for a non-existant theme.
412
		http_response_code(406);
413
	}
414
	break;
415
}
416