Passed
Branch master (60084b)
by Greg
10:38
created

action.php (9 issues)

1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2018 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\FunctionsDb;
19
use Fisharebest\Webtrees\Functions\FunctionsEdit;
20
use Fisharebest\Webtrees\Functions\FunctionsImport;
21
22
/** @global Tree $WT_TREE */
23
global $WT_TREE;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
24
25
require 'includes/session.php';
26
27
if (!Filter::checkCsrf()) {
28
	http_response_code(406);
29
30
	return;
31
}
32
33
switch (Filter::post('action', null, Filter::get('action'))) {
34
case 'accept-changes':
35
	// Accept all the pending changes for a record
36
	$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
37
	if ($record && Auth::isModerator($record->getTree()) && $record->canShow() && $record->canEdit()) {
38
		if ($record->isPendingDeletion()) {
39
			FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */
40
				I18N::translate('“%s” has been deleted.', $record->getFullName()));
41
		} else {
42
			FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */
43
				I18N::translate('The changes to “%s” have been accepted.', $record->getFullName()));
44
		}
45
		FunctionsImport::acceptAllChanges($record->getXref(), $record->getTree()->getTreeId());
46
	} else {
47
		http_response_code(406);
48
	}
49
	break;
50
51
case 'copy-fact':
52
	// Copy a fact to the clipboard
53
	$xref    = Filter::post('xref', WT_REGEX_XREF);
54
	$fact_id = Filter::post('fact_id');
55
56
	$record = GedcomRecord::getInstance($xref, $WT_TREE);
57
58
	if ($record && $record->canEdit()) {
59
		foreach ($record->getFacts() as $fact) {
60
			if ($fact->getFactId() == $fact_id) {
61
				switch ($fact->getTag()) {
62
					case 'NOTE':
63
					case 'SOUR':
64
					case 'OBJE':
65
						$type = 'all'; // paste this anywhere
66
						break;
67
					default:
68
						$type = $record::RECORD_TYPE; // paste only to the same record type
69
						break;
70
				}
71
				$clipboard = Session::get('clipboard');
72
				if (!is_array($clipboard)) {
73
					$clipboard = [];
74
				}
75
				$clipboard[$fact_id] = [
76
					'type'    => $type,
77
					'factrec' => $fact->getGedcom(),
78
					'fact'    => $fact->getTag(),
79
					];
80
				// The clipboard only holds 10 facts
81
				while (count($clipboard) > 10) {
82
					array_shift($clipboard);
83
				}
84
				Session::put('clipboard', $clipboard);
85
				FlashMessages::addMessage(I18N::translate('The record has been copied to the clipboard.'));
86
				break 2;
87
			}
88
		}
89
	}
90
	break;
91
92
case 'create-media-object-from-file':
93
	$file  = Filter::post('file');
94
	$type  = Filter::post('type');
95
	$title = Filter::post('title');
96
	$note  = Filter::post('note');
97
98
	if (preg_match('/\.([a-zA-Z0-9]+)$/', $file, $match)) {
99
		$format = ' ' . $match[1];
100
	} else {
101
		$format = '';
102
	}
103
104
	$gedcom = "0 @new@ OBJE\n1 FILE " . $file . "\n2 FORM " . $format;
105
106
	if ($type !== '') {
107
		$gedcom .= "\n3 TYPE " . $type;
108
	}
109
110
	if ($title !== '') {
111
		$gedcom .= "\n2 TITL " . $title;
112
	}
113
	if ($note !== '') {
114
		$gedcom .= "\n1 NOTE " . preg_replace('/\r?\n/', "\n2 CONT ", $note);
115
	}
116
	$media_object = $WT_TREE->createRecord($gedcom);
117
	// Accept the new record.  Rejecting it would leave the filesystem out-of-sync with the genealogy
118
	FunctionsImport::acceptAllChanges($media_object->getXref(), $media_object->getTree()->getTreeId());
119
	header('Location: admin_media.php?files=unused');
120
	break;
121
122
case 'paste-fact':
123
	// Paste a fact from the clipboard
124
	$xref      = Filter::post('xref', WT_REGEX_XREF);
125
	$fact_id   = Filter::post('fact_id');
126
	$record    = GedcomRecord::getInstance($xref, $WT_TREE);
127
	$clipboard = Session::get('clipboard');
128
129
	if ($record && $record->canEdit() && isset($clipboard[$fact_id])) {
130
		$record->createFact($clipboard[$fact_id]['factrec'], true);
131
	}
132
	break;
133
134
case 'delete-fact':
135
	$xref    = Filter::post('xref', WT_REGEX_XREF);
136
	$fact_id = Filter::post('fact_id');
137
138
	$record = GedcomRecord::getInstance($xref, $WT_TREE);
139
	if ($record && $record->canShow() && $record->canEdit()) {
140
		foreach ($record->getFacts() as $fact) {
141
			if ($fact->getFactId() == $fact_id && $fact->canShow() && $fact->canEdit()) {
142
				$record->deleteFact($fact_id, true);
143
				break 2;
144
			}
145
		}
146
	}
147
148
	// Can’t find the record/fact, or don’t have permission to delete it.
149
	http_response_code(406);
150
	break;
151
152
case 'delete-record':
153
	$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
154
	if ($record && Auth::isEditor($record->getTree()) && $record->canShow() && $record->canEdit()) {
155
		// Delete links to this record
156
		foreach (FunctionsDb::fetchAllLinks($record->getXref(), $record->getTree()->getTreeId()) as $xref) {
157
			$linker     = GedcomRecord::getInstance($xref, $WT_TREE);
158
			$old_gedcom = $linker->getGedcom();
159
			$new_gedcom = FunctionsEdit::removeLinks($old_gedcom, $record->getXref());
160
			// FunctionsDb::fetch_all_links() does not take account of pending changes. The links (or even the
161
			// record itself) may have already been deleted.
162
			if ($old_gedcom !== $new_gedcom) {
163
				// If we have removed a link from a family to an individual, and it has only one member
164
				if (preg_match('/^0 @' . WT_REGEX_XREF . '@ FAM/', $new_gedcom) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . WT_REGEX_XREF . ')@/', $new_gedcom, $match) == 1) {
165
					// Delete the family
166
					$family = GedcomRecord::getInstance($xref, $WT_TREE);
167
					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()));
168
					$family->deleteRecord();
169
					// Delete any remaining link to this family
170
					if ($match) {
171
						$relict     = GedcomRecord::getInstance($match[2][0], $WT_TREE);
172
						$new_gedcom = $relict->getGedcom();
173
						$new_gedcom = FunctionsEdit::removeLinks($new_gedcom, $linker->getXref());
174
						$relict->updateRecord($new_gedcom, false);
175
						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()));
176
					}
177
				} else {
178
					// Remove links from $linker to $record
179
					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()));
180
					$linker->updateRecord($new_gedcom, false);
181
				}
182
			}
183
		}
184
		// Delete the record itself
185
		$record->deleteRecord();
186
	} else {
187
		http_response_code(406);
188
	}
189
	break;
190
191
case 'delete-user':
192
	$user = User::find(Filter::postInteger('user_id'));
193
194
	if ($user && Auth::isAdmin() && Auth::user() !== $user) {
0 ignored issues
show
The condition $user && Fisharebest\Web...\Auth::user() !== $user can never be true.
Loading history...
195
		Log::addAuthenticationLog('Deleted user: ' . $user->getUserName());
196
		$user->delete();
197
	}
198
	break;
199
200
case 'language':
201
	// Change the current language
202
	$language = Filter::post('language');
203
	try {
204
		I18N::init($language);
205
		Session::put('locale', $language);
206
		// Remember our selection
207
		Auth::user()->setPreference('language', $language);
208
	} catch (\Exception $ex) {
209
		DebugBar::addThrowable($ex);
210
211
		// Request for a non-existant language.
212
		http_response_code(406);
213
	}
214
	break;
215
216
case 'masquerade':
217
	$user = User::find(Filter::postInteger('user_id'));
218
219
	if ($user && Auth::isAdmin() && Auth::user() !== $user) {
0 ignored issues
show
The condition $user && Fisharebest\Web...\Auth::user() !== $user can never be true.
Loading history...
220
		Log::addAuthenticationLog('Masquerade as user: ' . $user->getUserName());
221
		Auth::login($user);
222
		Session::put('masquerade', '1');
223
	} else {
224
		http_response_code(406);
225
	}
226
	break;
227
228
case 'unlink-media':
229
	// Remove links from an individual and their spouse-family records to a media object.
230
	// Used by the "unlink" option on the album (lightbox) tab.
231
	$source = Individual::getInstance(Filter::post('source', WT_REGEX_XREF), $WT_TREE);
232
	$target = Filter::post('target', WT_REGEX_XREF);
233
	if ($source && $source->canShow() && $source->canEdit() && $target) {
234
		// Consider the individual and their spouse-family records
235
		$sources   = $source->getSpouseFamilies();
0 ignored issues
show
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

235
		/** @scrutinizer ignore-call */ 
236
  $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\Repository. ( Ignorable by Annotation )

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

235
		/** @scrutinizer ignore-call */ 
236
  $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

235
		/** @scrutinizer ignore-call */ 
236
  $sources   = $source->getSpouseFamilies();
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

235
		/** @scrutinizer ignore-call */ 
236
  $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

235
		/** @scrutinizer ignore-call */ 
236
  $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

235
		/** @scrutinizer ignore-call */ 
236
  $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...
236
		$sources[] = $source;
237
		foreach ($sources as $source) {
238
			foreach ($source->getFacts() as $fact) {
239
				if (!$fact->isPendingDeletion()) {
240
					if ($fact->getValue() == '@' . $target . '@') {
241
						// Level 1 links
242
						$source->deleteFact($fact->getFactId(), true);
243
					} elseif (strpos($fact->getGedcom(), ' @' . $target . '@')) {
244
						// Level 2-3 links
245
						$source->updateFact($fact->getFactId(), preg_replace(['/\n2 OBJE @' . $target . '@(\n[3-9].*)*/', '/\n3 OBJE @' . $target . '@(\n[4-9].*)*/'], '', $fact->getGedcom()), true);
246
					}
247
				}
248
			}
249
		}
250
	} else {
251
		http_response_code(406);
252
	}
253
	break;
254
255
case 'reject-changes':
256
	// Reject all the pending changes for a record
257
	$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
258
	if ($record && $record->canEdit() && Auth::isModerator($record->getTree())) {
259
		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()));
260
		FunctionsImport::rejectAllChanges($record);
261
	} else {
262
		http_response_code(406);
263
	}
264
	break;
265
266
case 'select2-family':
267
	$page  = Filter::postInteger('page');
268
	$query = Filter::post('q', null, '');
269
	header('Content-Type: application/json');
270
	echo json_encode(Select2::familySearch($WT_TREE, $page, $query));
271
	break;
272
273
case 'select2-flag':
274
	$page  = Filter::postInteger('page');
275
	$query = Filter::post('q', null, '');
276
	header('Content-Type: application/json');
277
	echo json_encode(Select2::flagSearch($page, $query));
278
	break;
279
280
case 'select2-individual':
281
	$page  = Filter::postInteger('page');
282
	$query = Filter::post('q', null, '');
283
	header('Content-Type: application/json');
284
	echo json_encode(Select2::individualSearch($WT_TREE, $page, $query));
285
	break;
286
287
case 'select2-media':
288
	$page  = Filter::postInteger('page');
289
	$query = Filter::post('q', null, '');
290
	header('Content-Type: application/json');
291
	echo json_encode(Select2::mediaObjectSearch($WT_TREE, $page, $query));
292
	break;
293
294
case 'select2-note':
295
	$page  = Filter::postInteger('page');
296
	$query = Filter::post('q', null, '');
297
	header('Content-Type: application/json');
298
	echo json_encode(Select2::noteSearch($WT_TREE, $page, $query));
299
	break;
300
301
case 'select2-place':
302
	$page  = Filter::postInteger('page');
303
	$query = Filter::post('q', null, '');
304
	header('Content-Type: application/json');
305
	echo json_encode(Select2::placeSearch($WT_TREE, $page, $query, true));
306
	break;
307
308
case 'select2-repository':
309
	$page  = Filter::postInteger('page');
310
	$query = Filter::post('q', null, '');
311
	header('Content-Type: application/json');
312
	echo json_encode(Select2::repositorySearch($WT_TREE, $page, $query));
313
	break;
314
315
case 'select2-source':
316
	$page  = Filter::postInteger('page');
317
	$query = Filter::post('q', null, '');
318
	header('Content-Type: application/json');
319
	echo json_encode(Select2::sourceSearch($WT_TREE, $page, $query));
320
	break;
321
322
case 'select2-submitter':
323
	$page  = Filter::postInteger('page');
324
	$query = Filter::post('q', null, '');
325
	header('Content-Type: application/json');
326
	echo json_encode(Select2::submitterSearch($WT_TREE, $page, $query));
327
	break;
328
329
case 'theme':
330
	// Change the current theme
331
	$theme = Filter::post('theme');
332
	if (Site::getPreference('ALLOW_USER_THEMES') === '1' && array_key_exists($theme, Theme::themeNames())) {
333
		Session::put('theme_id', $theme);
334
		// Remember our selection
335
		Auth::user()->setPreference('theme', $theme);
336
	} else {
337
		// Request for a non-existant theme.
338
		http_response_code(406);
339
	}
340
	break;
341
}
342