These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | |||
23 | /** @global Tree $WT_TREE */ |
||
24 | global $WT_TREE; |
||
0 ignored issues
–
show
|
|||
25 | |||
26 | require 'includes/session.php'; |
||
27 | |||
28 | if (!Filter::checkCsrf()) { |
||
29 | http_response_code(406); |
||
30 | |||
31 | return; |
||
32 | } |
||
33 | |||
34 | switch (Filter::post('action', null, Filter::get('action'))) { |
||
35 | case 'accept-changes': |
||
36 | // Accept all the pending changes for a record |
||
37 | $record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE); |
||
38 | if ($record && Auth::isModerator($record->getTree()) && $record->canShow() && $record->canEdit()) { |
||
39 | if ($record->isPendingDeletion()) { |
||
40 | FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */ |
||
41 | I18N::translate('“%s” has been deleted.', $record->getFullName())); |
||
42 | } else { |
||
43 | FlashMessages::addMessage(/* I18N: %s is the name of a genealogy record */ |
||
44 | I18N::translate('The changes to “%s” have been accepted.', $record->getFullName())); |
||
45 | } |
||
46 | FunctionsImport::acceptAllChanges($record->getXref(), $record->getTree()->getTreeId()); |
||
47 | } else { |
||
48 | http_response_code(406); |
||
49 | } |
||
50 | break; |
||
51 | |||
52 | case 'copy-fact': |
||
53 | // Copy a fact to the clipboard |
||
54 | $xref = Filter::post('xref', WT_REGEX_XREF); |
||
55 | $fact_id = Filter::post('fact_id'); |
||
56 | |||
57 | $record = GedcomRecord::getInstance($xref, $WT_TREE); |
||
58 | |||
59 | if ($record && $record->canEdit()) { |
||
60 | foreach ($record->getFacts() as $fact) { |
||
61 | if ($fact->getFactId() == $fact_id) { |
||
62 | switch ($fact->getTag()) { |
||
63 | case 'NOTE': |
||
64 | case 'SOUR': |
||
65 | case 'OBJE': |
||
66 | $type = 'all'; // paste this anywhere |
||
67 | break; |
||
68 | default: |
||
69 | $type = $record::RECORD_TYPE; // paste only to the same record type |
||
70 | break; |
||
71 | } |
||
72 | $clipboard = Session::get('clipboard'); |
||
73 | if (!is_array($clipboard)) { |
||
74 | $clipboard = []; |
||
75 | } |
||
76 | $clipboard[$fact_id] = [ |
||
77 | 'type' => $type, |
||
78 | 'factrec' => $fact->getGedcom(), |
||
79 | 'fact' => $fact->getTag(), |
||
80 | ]; |
||
81 | // The clipboard only holds 10 facts |
||
82 | while (count($clipboard) > 10) { |
||
83 | array_shift($clipboard); |
||
84 | } |
||
85 | Session::put('clipboard', $clipboard); |
||
86 | FlashMessages::addMessage(I18N::translate('The record has been copied to the clipboard.')); |
||
87 | break 2; |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | break; |
||
92 | |||
93 | case 'create-media-object': |
||
94 | $MEDIA_DIRECTORY = $WT_TREE->getPreference('MEDIA_DIRECTORY'); |
||
95 | |||
96 | // Create a media object, and return parameters needed by Select2 |
||
97 | header('Content-type: application/json'); |
||
98 | $filename = 'eek.jpg'; |
||
99 | $auto = Filter::post('auto'); |
||
100 | $folder = Filter::post('folder'); |
||
101 | $note = Filter::post('note'); |
||
102 | $type = Filter::post('type'); |
||
103 | $title = Filter::post('title'); |
||
104 | |||
105 | // No file uploaded? |
||
106 | if (empty($_FILES['file']) || !is_uploaded_file($_FILES['file']['tmp_name'])) { |
||
107 | http_response_code(406); |
||
108 | break; |
||
109 | } |
||
110 | |||
111 | // The filename |
||
112 | $file = $_FILES['file']['name']; |
||
113 | $format = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); |
||
114 | if ($format === 'jpg') { |
||
115 | $format = 'jpeg'; |
||
116 | } |
||
117 | |||
118 | // The folder |
||
119 | $folder = trim($folder, '/'); |
||
120 | |||
121 | // If the folder is invalid, or doesn't exist and can't be created, then use the root folder |
||
122 | if (strpos($folder, '..') !== false || !File::mkdir(WT_DATA_DIR . $MEDIA_DIRECTORY . $folder)) { |
||
123 | $folder = ''; |
||
124 | } |
||
125 | |||
126 | // Generate a unique name for the file? |
||
127 | if ($auto === '1' || file_exists(WT_DATA_DIR . $MEDIA_DIRECTORY . $folder . $file)) { |
||
128 | $folder = ''; |
||
129 | $file = sha1($_FILES['file']['tmp_name']) . $format; |
||
130 | } |
||
131 | |||
132 | if ($folder !== '') { |
||
133 | $folder .= '/'; |
||
134 | } |
||
135 | |||
136 | if (move_uploaded_file($_FILES['file']['tmp_name'], WT_DATA_DIR . $MEDIA_DIRECTORY . $folder . $file)) { |
||
0 ignored issues
–
show
WT_DATA_DIR . $MEDIA_DIRECTORY . $folder . $file can contain request data and is used in file manipulation context(s) leading to a potential security vulnerability.
General Strategies to prevent injectionIn general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:
if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
throw new \InvalidArgumentException('This input is not allowed.');
}
For numeric data, we recommend to explicitly cast the data: $sanitized = (integer) $tainted;
![]() |
|||
137 | Log::addMediaLog('Media file ' . $_FILES['file']['name'] . ' uploaded'); |
||
138 | } else { |
||
139 | FlashMessages::addMessage( |
||
140 | I18N::translate('There was an error uploading your file.') . |
||
141 | '<br>' . |
||
142 | Functions::fileUploadErrorText($_FILES['mediafile']['error']) |
||
143 | ); |
||
144 | break; |
||
145 | } |
||
146 | |||
147 | |||
148 | $gedcom = "0 @new@ OBJE\n1 FILE " . $folder . $file . "\n2 FORM " . $format; |
||
149 | if ($type !== '') { |
||
150 | $gedcom .= "\n3 TYPE " . $type; |
||
151 | } |
||
152 | if ($title !== '') { |
||
153 | $gedcom .= "\n2 TITL " . $title; |
||
154 | } |
||
155 | if ($note !== '') { |
||
156 | $gedcom .= "\n1 NOTE " . preg_replace('/\r?\n/', "\n2 CONT ", $note); |
||
157 | } |
||
158 | $media_object = $WT_TREE->createRecord($gedcom); |
||
159 | // Accept the new record. Rejecting it would leave the filesystem out-of-sync with the genealogy |
||
160 | FunctionsImport::acceptAllChanges($media_object->getXref(), $media_object->getTree()->getTreeId()); |
||
161 | echo json_encode(['id' => $media_object->getXref(), 'text' => Select2::mediaObjectValue($media_object)]); |
||
162 | break; |
||
163 | |||
164 | View Code Duplication | case 'create-note-object': |
|
165 | // Create a note, and return parameters needed by Select2 |
||
166 | header('Content-type: application/json'); |
||
167 | $note = Filter::post('note'); |
||
168 | $gedcom = "0 @new@ NOTE " . str_replace("\n", "\n1 CONT ", $note); |
||
169 | $note_object = $WT_TREE->createRecord($gedcom); |
||
170 | echo json_encode(['id' => $note_object->getXref(), 'text' => Select2::noteValue($note_object)]); |
||
171 | break; |
||
172 | |||
173 | View Code Duplication | case 'create-repository': |
|
174 | // Create a repository, and return parameters needed by Select2 |
||
175 | header('Content-type: application/json'); |
||
176 | $repository_name = Filter::post('repository_name'); |
||
177 | $gedcom = "0 @new@ REPO\n1 NAME " . $repository_name; |
||
178 | $repository = $WT_TREE->createRecord($gedcom); |
||
179 | echo json_encode(['id' => $repository->getXref(), 'text' => Select2::repositoryValue($repository)]); |
||
180 | break; |
||
181 | |||
182 | case 'create-source': |
||
183 | // Create a source, and return parameters needed by Select2 |
||
184 | header('Content-type: application/json'); |
||
185 | $TITL = Filter::post('TITL'); |
||
186 | $ABBR = Filter::post('ABBR'); |
||
187 | $AUTH = Filter::post('AUTH'); |
||
188 | $PUBL = Filter::post('PUBL'); |
||
189 | $TEXT = Filter::post('TEXT'); |
||
190 | $REPO = Filter::post('REPO', WT_REGEX_XREF); |
||
191 | $CALN = Filter::post('CALN'); |
||
192 | $gedcom = '0 @new@ SOUR'; |
||
193 | if ($TITL !== '') { |
||
194 | $gedcom .= "\n1 TITL " . $TITL; |
||
195 | } |
||
196 | if ($ABBR !== '') { |
||
197 | $gedcom .= "\n1 ABBR " . $ABBR; |
||
198 | } |
||
199 | if ($AUTH !== '') { |
||
200 | $gedcom .= "\n1 AUTH " . $AUTH; |
||
201 | } |
||
202 | if ($PUBL !== '') { |
||
203 | $gedcom .= "\n1 PUBL " . $PUBL; |
||
204 | } |
||
205 | if ($TEXT !== '') { |
||
206 | $gedcom .= "\n1 TEXT " . str_replace("\n", "\n2 CONT ", $TEXT); |
||
207 | } |
||
208 | if ($REPO !== '') { |
||
209 | $gedcom .= "\n1 REPO @" . $REPO . '@'; |
||
210 | if ($CALN !== '') { |
||
211 | $gedcom .= "\n2 CALN " . $CALN; |
||
212 | } |
||
213 | } |
||
214 | $source = $WT_TREE->createRecord($gedcom); |
||
215 | echo json_encode(['id' => $source->getXref(), 'text' => Select2::sourceValue($source)]); |
||
216 | break; |
||
217 | |||
218 | case 'create-submitter': |
||
219 | // Create a submitter, and return parameters needed by Select2 |
||
220 | header('Content-type: application/json'); |
||
221 | $gedcom = '0 @new@ SUBM'; |
||
222 | $submitter_name = Filter::post('submitter_name', null, ''); |
||
223 | if ($submitter_name !== '') { |
||
224 | $gedcom .= "\n1 NAME " . $submitter_name; |
||
225 | } |
||
226 | $submitter_address = Filter::post('submitter_address', null, ''); |
||
227 | if ($submitter_address !== '') { |
||
228 | $gedcom .= "\n1 ADDR " . $submitter_address; |
||
229 | } |
||
230 | $submitter = $WT_TREE->createRecord($gedcom); |
||
231 | echo json_encode(['id' => $submitter->getXref(), 'text' => Select2::submitterValue($submitter)]); |
||
232 | break; |
||
233 | |||
234 | case 'paste-fact': |
||
235 | // Paste a fact from the clipboard |
||
236 | $xref = Filter::post('xref', WT_REGEX_XREF); |
||
237 | $fact_id = Filter::post('fact_id'); |
||
238 | $record = GedcomRecord::getInstance($xref, $WT_TREE); |
||
239 | $clipboard = Session::get('clipboard'); |
||
240 | |||
241 | if ($record && $record->canEdit() && isset($clipboard[$fact_id])) { |
||
242 | $record->createFact($clipboard[$fact_id]['factrec'], true); |
||
243 | } |
||
244 | break; |
||
245 | |||
246 | case 'delete-fact': |
||
247 | $xref = Filter::post('xref', WT_REGEX_XREF); |
||
248 | $fact_id = Filter::post('fact_id'); |
||
249 | |||
250 | $record = GedcomRecord::getInstance($xref, $WT_TREE); |
||
251 | if ($record && $record->canShow() && $record->canEdit()) { |
||
252 | foreach ($record->getFacts() as $fact) { |
||
253 | if ($fact->getFactId() == $fact_id && $fact->canShow() && $fact->canEdit()) { |
||
254 | $record->deleteFact($fact_id, true); |
||
255 | break 2; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | // Can’t find the record/fact, or don’t have permission to delete it. |
||
261 | http_response_code(406); |
||
262 | break; |
||
263 | |||
264 | case 'delete-record': |
||
265 | $record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE); |
||
266 | if ($record && Auth::isEditor($record->getTree()) && $record->canShow() && $record->canEdit()) { |
||
267 | // Delete links to this record |
||
268 | foreach (FunctionsDb::fetchAllLinks($record->getXref(), $record->getTree()->getTreeId()) as $xref) { |
||
269 | $linker = GedcomRecord::getInstance($xref, $WT_TREE); |
||
270 | $old_gedcom = $linker->getGedcom(); |
||
271 | $new_gedcom = FunctionsEdit::removeLinks($old_gedcom, $record->getXref()); |
||
272 | // FunctionsDb::fetch_all_links() does not take account of pending changes. The links (or even the |
||
273 | // record itself) may have already been deleted. |
||
274 | if ($old_gedcom !== $new_gedcom) { |
||
275 | // If we have removed a link from a family to an individual, and it has only one member |
||
276 | if (preg_match('/^0 @' . WT_REGEX_XREF . '@ FAM/', $new_gedcom) && preg_match_all('/\n1 (HUSB|WIFE|CHIL) @(' . WT_REGEX_XREF . ')@/', $new_gedcom, $match) == 1) { |
||
277 | // Delete the family |
||
278 | $family = GedcomRecord::getInstance($xref, $WT_TREE); |
||
279 | 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())); |
||
280 | $family->deleteRecord(); |
||
281 | // Delete any remaining link to this family |
||
282 | if ($match) { |
||
283 | $relict = GedcomRecord::getInstance($match[2][0], $WT_TREE); |
||
284 | $new_gedcom = $relict->getGedcom(); |
||
285 | $new_gedcom = FunctionsEdit::removeLinks($new_gedcom, $linker->getXref()); |
||
286 | $relict->updateRecord($new_gedcom, false); |
||
287 | 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())); |
||
288 | } |
||
289 | } else { |
||
290 | // Remove links from $linker to $record |
||
291 | 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())); |
||
292 | $linker->updateRecord($new_gedcom, false); |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 | // Delete the record itself |
||
297 | $record->deleteRecord(); |
||
298 | } else { |
||
299 | http_response_code(406); |
||
300 | } |
||
301 | break; |
||
302 | |||
303 | case 'delete-user': |
||
304 | $user = User::find(Filter::postInteger('user_id')); |
||
305 | |||
306 | if ($user && Auth::isAdmin() && Auth::user() !== $user) { |
||
307 | Log::addAuthenticationLog('Deleted user: ' . $user->getUserName()); |
||
308 | $user->delete(); |
||
309 | } |
||
310 | break; |
||
311 | |||
312 | case 'language': |
||
313 | // Change the current language |
||
314 | $language = Filter::post('language'); |
||
315 | try { |
||
316 | I18N::init($language); |
||
317 | Session::put('locale', $language); |
||
318 | // Remember our selection |
||
319 | Auth::user()->setPreference('language', $language); |
||
320 | } catch (\Exception $ex) { |
||
321 | // Request for a non-existant language. |
||
322 | http_response_code(406); |
||
323 | } |
||
324 | break; |
||
325 | |||
326 | case 'masquerade': |
||
327 | $user = User::find(Filter::postInteger('user_id')); |
||
328 | |||
329 | if ($user && Auth::isAdmin() && Auth::user() !== $user) { |
||
330 | Log::addAuthenticationLog('Masquerade as user: ' . $user->getUserName()); |
||
331 | Auth::login($user); |
||
332 | Session::put('masquerade', '1'); |
||
333 | } else { |
||
334 | http_response_code(406); |
||
335 | } |
||
336 | break; |
||
337 | |||
338 | case 'unlink-media': |
||
339 | // Remove links from an individual and their spouse-family records to a media object. |
||
340 | // Used by the "unlink" option on the album (lightbox) tab. |
||
341 | $source = Individual::getInstance(Filter::post('source', WT_REGEX_XREF), $WT_TREE); |
||
342 | $target = Filter::post('target', WT_REGEX_XREF); |
||
343 | if ($source && $source->canShow() && $source->canEdit() && $target) { |
||
344 | // Consider the individual and their spouse-family records |
||
345 | $sources = $source->getSpouseFamilies(); |
||
346 | $sources[] = $source; |
||
347 | foreach ($sources as $source) { |
||
348 | foreach ($source->getFacts() as $fact) { |
||
349 | if (!$fact->isPendingDeletion()) { |
||
350 | if ($fact->getValue() == '@' . $target . '@') { |
||
351 | // Level 1 links |
||
352 | $source->deleteFact($fact->getFactId(), true); |
||
353 | } elseif (strpos($fact->getGedcom(), ' @' . $target . '@')) { |
||
354 | // Level 2-3 links |
||
355 | $source->updateFact($fact->getFactId(), preg_replace(['/\n2 OBJE @' . $target . '@(\n[3-9].*)*/', '/\n3 OBJE @' . $target . '@(\n[4-9].*)*/'], '', $fact->getGedcom()), true); |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | } else { |
||
361 | http_response_code(406); |
||
362 | } |
||
363 | break; |
||
364 | |||
365 | case 'reject-changes': |
||
366 | // Reject all the pending changes for a record |
||
367 | $record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE); |
||
368 | if ($record && $record->canEdit() && Auth::isModerator($record->getTree())) { |
||
369 | 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())); |
||
370 | FunctionsImport::rejectAllChanges($record); |
||
371 | } else { |
||
372 | http_response_code(406); |
||
373 | } |
||
374 | break; |
||
375 | |||
376 | View Code Duplication | case 'select2-family': |
|
377 | $page = Filter::postInteger('page'); |
||
378 | $query = Filter::post('q', null, ''); |
||
379 | header('Content-Type: application/json'); |
||
380 | echo json_encode(Select2::familySearch($WT_TREE, $page, $query)); |
||
381 | break; |
||
382 | |||
383 | View Code Duplication | case 'select2-individual': |
|
384 | $page = Filter::postInteger('page'); |
||
385 | $query = Filter::post('q', null, ''); |
||
386 | header('Content-Type: application/json'); |
||
387 | echo json_encode(Select2::individualSearch($WT_TREE, $page, $query)); |
||
388 | break; |
||
389 | |||
390 | View Code Duplication | case 'select2-media': |
|
391 | $page = Filter::postInteger('page'); |
||
392 | $query = Filter::post('q', null, ''); |
||
393 | header('Content-Type: application/json'); |
||
394 | echo json_encode(Select2::mediaObjectSearch($WT_TREE, $page, $query)); |
||
395 | break; |
||
396 | |||
397 | View Code Duplication | case 'select2-note': |
|
398 | $page = Filter::postInteger('page'); |
||
399 | $query = Filter::post('q', null, ''); |
||
400 | header('Content-Type: application/json'); |
||
401 | echo json_encode(Select2::noteSearch($WT_TREE, $page, $query)); |
||
402 | break; |
||
403 | |||
404 | View Code Duplication | case 'select2-place': |
|
405 | $page = Filter::postInteger('page'); |
||
406 | $query = Filter::post('q', null, ''); |
||
407 | header('Content-Type: application/json'); |
||
408 | echo json_encode(Select2::placeSearch($WT_TREE, $page, $query, true)); |
||
409 | break; |
||
410 | |||
411 | View Code Duplication | case 'select2-repository': |
|
412 | $page = Filter::postInteger('page'); |
||
413 | $query = Filter::post('q', null, ''); |
||
414 | header('Content-Type: application/json'); |
||
415 | echo json_encode(Select2::repositorySearch($WT_TREE, $page, $query)); |
||
416 | break; |
||
417 | |||
418 | View Code Duplication | case 'select2-source': |
|
419 | $page = Filter::postInteger('page'); |
||
420 | $query = Filter::post('q', null, ''); |
||
421 | header('Content-Type: application/json'); |
||
422 | echo json_encode(Select2::sourceSearch($WT_TREE, $page, $query)); |
||
423 | break; |
||
424 | |||
425 | case 'theme': |
||
426 | // Change the current theme |
||
427 | $theme = Filter::post('theme'); |
||
428 | if (Site::getPreference('ALLOW_USER_THEMES') === '1' && array_key_exists($theme, Theme::themeNames())) { |
||
429 | Session::put('theme_id', $theme); |
||
430 | // Remember our selection |
||
431 | Auth::user()->setPreference('theme', $theme); |
||
432 | } else { |
||
433 | // Request for a non-existant theme. |
||
434 | http_response_code(406); |
||
435 | } |
||
436 | break; |
||
437 | } |
||
438 |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state