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 */ 16namespace Fisharebest\Webtrees\Report; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\Date; 21use Fisharebest\Webtrees\Family; 22use Fisharebest\Webtrees\Filter; 23use Fisharebest\Webtrees\Functions\Functions; 24use Fisharebest\Webtrees\Functions\FunctionsDate; 25use Fisharebest\Webtrees\GedcomRecord; 26use Fisharebest\Webtrees\GedcomTag; 27use Fisharebest\Webtrees\I18N; 28use Fisharebest\Webtrees\Individual; 29use Fisharebest\Webtrees\Log; 30use Fisharebest\Webtrees\Media; 31use Fisharebest\Webtrees\Note; 32use Fisharebest\Webtrees\Place; 33use Fisharebest\Webtrees\Tree; 34use stdClass; 35use Symfony\Component\ExpressionLanguage\ExpressionLanguage; 36 37/** 38 * Class ReportParserGenerate - parse a report.xml file and generate the report. 39 */ 40class ReportParserGenerate extends ReportParserBase 41{ 42 /** @var bool Are we collecting data from <Footnote> elements */ 43 private $process_footnote = true; 44 45 /** @var bool Are we currently outputing data? */ 46 private $print_data = false; 47 48 /** @var bool[] Push-down stack of $print_data */ 49 private $print_data_stack = []; 50 51 /** @var int Are we processing GEDCOM data */ 52 private $process_gedcoms = 0; 53 54 /** @var int Are we processing conditionals */ 55 private $process_ifs = 0; 56 57 /** @var int Are we processing repeats */ 58 private $process_repeats = 0; 59 60 /** @var int Quantity of data to repeat during loops */ 61 private $repeat_bytes = 0; 62 63 /** @var string[] Repeated data when iterating over loops */ 64 private $repeats = []; 65 66 /** @var array[] Nested repeating data */ 67 private $repeats_stack = []; 68 69 /** @var ReportBase[] Nested repeating data */ 70 private $wt_report_stack = []; 71 72 /** @var resource Nested repeating data */ 73 private $parser; 74 75 /** @var resource[] Nested repeating data */ 76 private $parser_stack = []; 77 78 /** @var string The current GEDCOM record */ 79 private $gedrec = ''; 80 81 /** @var string[] Nested GEDCOM records */ 82 private $gedrec_stack = []; 83 84 /** @var ReportBaseElement The currently processed element */ 85 private $current_element; 86 87 /** @var ReportBaseElement The currently processed element */ 88 private $footnote_element; 89 90 /** @var string The GEDCOM fact currently being processed */ 91 private $fact = ''; 92 93 /** @var string The GEDCOM value currently being processed */ 94 private $desc = ''; 95 96 /** @var string The GEDCOM type currently being processed */ 97 private $type = ''; 98 99 /** @var int The current generational level */ 100 private $generation = 1; 101 102 /** @var array Source data for processing lists */ 103 private $list = []; 104 105 /** @var int Number of items in lists */ 106 private $list_total = 0; 107 108 /** @var int Number of items filtered from lists */ 109 private $list_private = 0; 110 111 /** @var string The filename of the XML report */ 112 protected $report; 113 114 /** @var ReportBase A factory for creating report elements */ 115 private $report_root; 116 117 /** @var ReportBaseElement Nested report elements */ 118 private $wt_report; 119 120 /** @var string[][] Variables defined in the report at run-time */ 121 private $vars; 122 123 /** @var Tree The current tree */ 124 private $tree; 125 126 /** 127 * Create a parser for a report 128 * 129 * @param string $report The XML filename 130 * @param ReportBase $report_root 131 * @param string[][] $vars 132 * @param Tree $tree 133 */ 134 public function __construct($report, ReportBase $report_root, array $vars, Tree $tree) 135 { 136 $this->report = $report; 137 $this->report_root = $report_root; 138 $this->wt_report = $report_root; 139 $this->current_element = new ReportBaseElement(); 140 $this->vars = $vars; 141 $this->tree = $tree; 142 143 parent::__construct($report, $report_root, $vars, $tree); 144 } 145 146 /** 147 * XML start element handler 148 * This function is called whenever a starting element is reached 149 * The element handler will be called if found, otherwise it must be HTML 150 * 151 * @param resource $parser the resource handler for the XML parser 152 * @param string $name the name of the XML element parsed 153 * @param array $attrs an array of key value pairs for the attributes 154 */ 155 protected function startElement($parser, $name, $attrs) 156 { 157 $newattrs = []; 158 159 foreach ($attrs as $key => $value) { 160 if (preg_match("/^\\$(\w+)$/", $value, $match)) { 161 if ((isset($this->vars[$match[1]]['id'])) && (!isset($this->vars[$match[1]]['gedcom']))) { 162 $value = $this->vars[$match[1]]['id']; 163 } 164 } 165 $newattrs[$key] = $value; 166 } 167 $attrs = $newattrs; 168 if ($this->process_footnote && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag')) { 169 $start_method = $name . 'StartHandler'; 170 $end_method = $name . 'EndHandler'; 171 if (method_exists($this, $start_method)) { 172 $this->$start_method($attrs); 173 } elseif (!method_exists($this, $end_method)) { 174 $this->htmlStartHandler($name, $attrs); 175 } 176 } 177 } 178 179 /** 180 * XML end element handler 181 * This function is called whenever an ending element is reached 182 * The element handler will be called if found, otherwise it must be HTML 183 * 184 * @param resource $parser the resource handler for the XML parser 185 * @param string $name the name of the XML element parsed 186 */ 187 protected function endElement($parser, $name) 188 { 189 if (($this->process_footnote || $name === 'Footnote') && ($this->process_ifs === 0 || $name === 'if') && ($this->process_gedcoms === 0 || $name === 'Gedcom') && ($this->process_repeats === 0 || $name === 'Facts' || $name === 'RepeatTag' || $name === 'List' || $name === 'Relatives')) { 190 $start_method = $name . 'StartHandler'; 191 $end_method = $name . 'EndHandler'; 192 if (method_exists($this, $end_method)) { 193 $this->$end_method(); 194 } elseif (!method_exists($this, $start_method)) { 195 $this->htmlEndHandler($name); 196 } 197 } 198 } 199 200 /** 201 * XML character data handler 202 * 203 * @param resource $parser the resource handler for the XML parser 204 * @param string $data the name of the XML element parsed 205 */ 206 protected function characterData($parser, $data) 207 { 208 if ($this->print_data && $this->process_gedcoms === 0 && $this->process_ifs === 0 && $this->process_repeats === 0) { 209 $this->current_element->addText($data); 210 } 211 } 212 213 /** 214 * XML <style> 215 * 216 * @param array $attrs an array of key value pairs for the attributes 217 */ 218 private function styleStartHandler($attrs) 219 { 220 if (empty($attrs['name'])) { 221 throw new \DomainException('REPORT ERROR Style: The "name" of the style is missing or not set in the XML file.'); 222 } 223 224 // array Style that will be passed on 225 $s = []; 226 227 // string Name af the style 228 $s['name'] = $attrs['name']; 229 230 // string Name of the DEFAULT font 231 $s['font'] = $this->wt_report->defaultFont; 232 if (!empty($attrs['font'])) { 233 $s['font'] = $attrs['font']; 234 } 235 236 // int The size of the font in points 237 $s['size'] = $this->wt_report->defaultFontSize; 238 if (!empty($attrs['size'])) { 239 $s['size'] = (int) $attrs['size']; 240 } // Get it as int to ignore all decimal points or text (if any text then int(0)) 241 242 // string B: bold, I: italic, U: underline, D: line trough, The default value is regular. 243 $s['style'] = ''; 244 if (!empty($attrs['style'])) { 245 $s['style'] = $attrs['style']; 246 } 247 248 $this->wt_report->addStyle($s); 249 } 250 251 /** 252 * XML <Doc> 253 * Sets up the basics of the document proparties 254 * 255 * @param array $attrs an array of key value pairs for the attributes 256 */ 257 private function docStartHandler($attrs) 258 { 259 $this->parser = $this->xml_parser; 260 261 // Custom page width 262 if (!empty($attrs['customwidth'])) { 263 $this->wt_report->pagew = (int) $attrs['customwidth']; 264 } // Get it as int to ignore all decimal points or text (if any text then int(0)) 265 // Custom Page height 266 if (!empty($attrs['customheight'])) { 267 $this->wt_report->pageh = (int) $attrs['customheight']; 268 } // Get it as int to ignore all decimal points or text (if any text then int(0)) 269 270 // Left Margin 271 if (isset($attrs['leftmargin'])) { 272 if ($attrs['leftmargin'] === '0') { 273 $this->wt_report->leftmargin = 0; 274 } elseif (!empty($attrs['leftmargin'])) { 275 $this->wt_report->leftmargin = (int) $attrs['leftmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0)) 276 } 277 } 278 // Right Margin 279 if (isset($attrs['rightmargin'])) { 280 if ($attrs['rightmargin'] === '0') { 281 $this->wt_report->rightmargin = 0; 282 } elseif (!empty($attrs['rightmargin'])) { 283 $this->wt_report->rightmargin = (int) $attrs['rightmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0)) 284 } 285 } 286 // Top Margin 287 if (isset($attrs['topmargin'])) { 288 if ($attrs['topmargin'] === '0') { 289 $this->wt_report->topmargin = 0; 290 } elseif (!empty($attrs['topmargin'])) { 291 $this->wt_report->topmargin = (int) $attrs['topmargin']; // Get it as int to ignore all decimal points or text (if any text then int(0)) 292 } 293 } 294 // Bottom Margin 295 if (isset($attrs['bottommargin'])) { 296 if ($attrs['bottommargin'] === '0') { 297 $this->wt_report->bottommargin = 0; 298 } elseif (!empty($attrs['bottommargin'])) { 299 $this->wt_report->bottommargin = (int) $attrs['bottommargin']; // Get it as int to ignore all decimal points or text (if any text then int(0)) 300 } 301 } 302 // Header Margin 303 if (isset($attrs['headermargin'])) { 304 if ($attrs['headermargin'] === '0') { 305 $this->wt_report->headermargin = 0; 306 } elseif (!empty($attrs['headermargin'])) { 307 $this->wt_report->headermargin = (int) $attrs['headermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0)) 308 } 309 } 310 // Footer Margin 311 if (isset($attrs['footermargin'])) { 312 if ($attrs['footermargin'] === '0') { 313 $this->wt_report->footermargin = 0; 314 } elseif (!empty($attrs['footermargin'])) { 315 $this->wt_report->footermargin = (int) $attrs['footermargin']; // Get it as int to ignore all decimal points or text (if any text then int(0)) 316 } 317 } 318 319 // Page Orientation 320 if (!empty($attrs['orientation'])) { 321 if ($attrs['orientation'] == 'landscape') { 322 $this->wt_report->orientation = 'landscape'; 323 } elseif ($attrs['orientation'] == 'portrait') { 324 $this->wt_report->orientation = 'portrait'; 325 } 326 } 327 // Page Size 328 if (!empty($attrs['pageSize'])) { 329 $this->wt_report->pageFormat = strtoupper($attrs['pageSize']); 330 } 331 332 // Show Generated By... 333 if (isset($attrs['showGeneratedBy'])) { 334 if ($attrs['showGeneratedBy'] === '0') { 335 $this->wt_report->showGenText = false; 336 } elseif ($attrs['showGeneratedBy'] === '1') { 337 $this->wt_report->showGenText = true; 338 } 339 } 340 341 $this->wt_report->setup(); 342 } 343 344 /** 345 * XML </Doc> 346 */ 347 private function docEndHandler() 348 { 349 $this->wt_report->run(); 350 } 351 352 /** 353 * XML <Header> 354 */ 355 private function headerStartHandler() 356 { 357 // Clear the Header before any new elements are added 358 $this->wt_report->clearHeader(); 359 $this->wt_report->setProcessing('H'); 360 } 361 362 /** 363 * XML <PageHeader> 364 */ 365 private function pageHeaderStartHandler() 366 { 367 $this->print_data_stack[] = $this->print_data; 368 $this->print_data = false; 369 $this->wt_report_stack[] = $this->wt_report; 370 $this->wt_report = $this->report_root->createPageHeader(); 371 } 372 373 /** 374 * XML <pageHeaderEndHandler> 375 */ 376 private function pageHeaderEndHandler() 377 { 378 $this->print_data = array_pop($this->print_data_stack); 379 $this->current_element = $this->wt_report; 380 $this->wt_report = array_pop($this->wt_report_stack); 381 $this->wt_report->addElement($this->current_element); 382 } 383 384 /** 385 * XML <bodyStartHandler> 386 */ 387 private function bodyStartHandler() 388 { 389 $this->wt_report->setProcessing('B'); 390 } 391 392 /** 393 * XML <footerStartHandler> 394 */ 395 private function footerStartHandler() 396 { 397 $this->wt_report->setProcessing('F'); 398 } 399 400 /** 401 * XML <Cell> 402 * 403 * @param array $attrs an array of key value pairs for the attributes 404 */ 405 private function cellStartHandler($attrs) 406 { 407 // string The text alignment of the text in this box. 408 $align = ''; 409 if (!empty($attrs['align'])) { 410 $align = $attrs['align']; 411 // RTL supported left/right alignment 412 if ($align == 'rightrtl') { 413 if ($this->wt_report->rtl) { 414 $align = 'left'; 415 } else { 416 $align = 'right'; 417 } 418 } elseif ($align == 'leftrtl') { 419 if ($this->wt_report->rtl) { 420 $align = 'right'; 421 } else { 422 $align = 'left'; 423 } 424 } 425 } 426 427 // string The color to fill the background of this cell 428 $bgcolor = ''; 429 if (!empty($attrs['bgcolor'])) { 430 $bgcolor = $attrs['bgcolor']; 431 } 432 433 // int Whether or not the background should be painted 434 $fill = 1; 435 if (isset($attrs['fill'])) { 436 if ($attrs['fill'] === '0') { 437 $fill = 0; 438 } elseif ($attrs['fill'] === '1') { 439 $fill = 1; 440 } 441 } 442 443 $reseth = true; 444 // boolean if true reset the last cell height (default true) 445 if (isset($attrs['reseth'])) { 446 if ($attrs['reseth'] === '0') { 447 $reseth = false; 448 } elseif ($attrs['reseth'] === '1') { 449 $reseth = true; 450 } 451 } 452 453 // mixed Whether or not a border should be printed around this box 454 $border = 0; 455 if (!empty($attrs['border'])) { 456 $border = $attrs['border']; 457 } 458 // string Border color in HTML code 459 $bocolor = ''; 460 if (!empty($attrs['bocolor'])) { 461 $bocolor = $attrs['bocolor']; 462 } 463 464 // int Cell height (expressed in points) The starting height of this cell. If the text wraps the height will automatically be adjusted. 465 $height = 0; 466 if (!empty($attrs['height'])) { 467 $height = (int) $attrs['height']; 468 } 469 // int Cell width (expressed in points) Setting the width to 0 will make it the width from the current location to the right margin. 470 $width = 0; 471 if (!empty($attrs['width'])) { 472 $width = (int) $attrs['width']; 473 } 474 475 // int Stretch carachter mode 476 $stretch = 0; 477 if (!empty($attrs['stretch'])) { 478 $stretch = (int) $attrs['stretch']; 479 } 480 481 // mixed Position the left corner of this box on the page. The default is the current position. 482 $left = '.'; 483 if (isset($attrs['left'])) { 484 if ($attrs['left'] === '.') { 485 $left = '.'; 486 } elseif (!empty($attrs['left'])) { 487 $left = (int) $attrs['left']; 488 } elseif ($attrs['left'] === '0') { 489 $left = 0; 490 } 491 } 492 // mixed Position the top corner of this box on the page. the default is the current position 493 $top = '.'; 494 if (isset($attrs['top'])) { 495 if ($attrs['top'] === '.') { 496 $top = '.'; 497 } elseif (!empty($attrs['top'])) { 498 $top = (int) $attrs['top']; 499 } elseif ($attrs['top'] === '0') { 500 $top = 0; 501 } 502 } 503 504 // string The name of the Style that should be used to render the text. 505 $style = ''; 506 if (!empty($attrs['style'])) { 507 $style = $attrs['style']; 508 } 509 510 // string Text color in html code 511 $tcolor = ''; 512 if (!empty($attrs['tcolor'])) { 513 $tcolor = $attrs['tcolor']; 514 } 515 516 // int Indicates where the current position should go after the call. 517 $ln = 0; 518 if (isset($attrs['newline'])) { 519 if (!empty($attrs['newline'])) { 520 $ln = (int) $attrs['newline']; 521 } elseif ($attrs['newline'] === '0') { 522 $ln = 0; 523 } 524 } 525 526 if ($align == 'left') { 527 $align = 'L'; 528 } elseif ($align == 'right') { 529 $align = 'R'; 530 } elseif ($align == 'center') { 531 $align = 'C'; 532 } elseif ($align == 'justify') { 533 $align = 'J'; 534 } 535 536 $this->print_data_stack[] = $this->print_data; 537 $this->print_data = true; 538 539 $this->current_element = $this->report_root->createCell( 540 $width, 541 $height, 542 $border, 543 $align, 544 $bgcolor, 545 $style, 546 $ln, 547 $top, 548 $left, 549 $fill, 550 $stretch, 551 $bocolor, 552 $tcolor, 553 $reseth 554 ); 555 } 556 557 /** 558 * XML </Cell> 559 */ 560 private function cellEndHandler() 561 { 562 $this->print_data = array_pop($this->print_data_stack); 563 $this->wt_report->addElement($this->current_element); 564 } 565 566 /** 567 * XML <Now /> element handler 568 */ 569 private function nowStartHandler() 570 { 571 $g = FunctionsDate::timestampToGedcomDate(WT_TIMESTAMP + WT_TIMESTAMP_OFFSET); 572 $this->current_element->addText($g->display()); 573 } 574 575 /** 576 * XML <PageNum /> element handler 577 */ 578 private function pageNumStartHandler() 579 { 580 $this->current_element->addText('#PAGENUM#'); 581 } 582 583 /** 584 * XML <TotalPages /> element handler 585 */ 586 private function totalPagesStartHandler() 587 { 588 $this->current_element->addText('{{:ptp:}}'); 589 } 590 591 /** 592 * Called at the start of an element. 593 * 594 * @param array $attrs an array of key value pairs for the attributes 595 */ 596 private function gedcomStartHandler($attrs) 597 { 598 if ($this->process_gedcoms > 0) { 599 $this->process_gedcoms++; 600 601 return; 602 } 603 604 $tag = $attrs['id']; 605 $tag = str_replace('@fact', $this->fact, $tag); 606 $tags = explode(':', $tag); 607 $newgedrec = ''; 608 if (count($tags) < 2) { 609 $tmp = GedcomRecord::getInstance($attrs['id'], $this->tree); 610 $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : ''; 611 } 612 if (empty($newgedrec)) { 613 $tgedrec = $this->gedrec; 614 $newgedrec = ''; 615 foreach ($tags as $tag) { 616 if (preg_match('/\$(.+)/', $tag, $match)) { 617 if (isset($this->vars[$match[1]]['gedcom'])) { 618 $newgedrec = $this->vars[$match[1]]['gedcom']; 619 } else { 620 $tmp = GedcomRecord::getInstance($match[1], $this->tree); 621 $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : ''; 622 } 623 } else { 624 if (preg_match('/@(.+)/', $tag, $match)) { 625 $gmatch = []; 626 if (preg_match("/\d $match[1] @([^@]+)@/", $tgedrec, $gmatch)) { 627 $tmp = GedcomRecord::getInstance($gmatch[1], $this->tree); 628 $newgedrec = $tmp ? $tmp->privatizeGedcom(Auth::accessLevel($this->tree)) : ''; 629 $tgedrec = $newgedrec; 630 } else { 631 $newgedrec = ''; 632 break; 633 } 634 } else { 635 $temp = explode(' ', trim($tgedrec)); 636 $level = $temp[0] + 1; 637 $newgedrec = Functions::getSubRecord($level, "$level $tag", $tgedrec); 638 $tgedrec = $newgedrec; 639 } 640 } 641 } 642 } 643 if (!empty($newgedrec)) { 644 $this->gedrec_stack[] = [$this->gedrec, $this->fact, $this->desc]; 645 $this->gedrec = $newgedrec; 646 if (preg_match("/(\d+) (_?[A-Z0-9]+) (.*)/", $this->gedrec, $match)) { 647 $this->fact = $match[2]; 648 $this->desc = trim($match[3]); 649 } 650 } else { 651 $this->process_gedcoms++; 652 } 653 } 654 655 /** 656 * Called at the end of an element. 657 */ 658 private function gedcomEndHandler() 659 { 660 if ($this->process_gedcoms > 0) { 661 $this->process_gedcoms--; 662 } else { 663 list($this->gedrec, $this->fact, $this->desc) = array_pop($this->gedrec_stack); 664 } 665 } 666 667 /** 668 * XML <textBoxStartHandler> 669 * 670 * @param array $attrs an array of key value pairs for the attributes 671 */ 672 private function textBoxStartHandler($attrs) 673 { 674 // string Background color code 675 $bgcolor = ''; 676 if (!empty($attrs['bgcolor'])) { 677 $bgcolor = $attrs['bgcolor']; 678 } 679 680 // boolean Wether or not fill the background color 681 $fill = true; 682 if (isset($attrs['fill'])) { 683 if ($attrs['fill'] === '0') { 684 $fill = false; 685 } elseif ($attrs['fill'] === '1') { 686 $fill = true; 687 } 688 } 689 690 // var boolean Whether or not a border should be printed around this box. 0 = no border, 1 = border. Default is 0 691 $border = false; 692 if (isset($attrs['border'])) { 693 if ($attrs['border'] === '1') { 694 $border = true; 695 } elseif ($attrs['border'] === '0') { 696 $border = false; 697 } 698 } 699 700 // int The starting height of this cell. If the text wraps the height will automatically be adjusted 701 $height = 0; 702 if (!empty($attrs['height'])) { 703 $height = (int) $attrs['height']; 704 } 705 // int Setting the width to 0 will make it the width from the current location to the margin 706 $width = 0; 707 if (!empty($attrs['width'])) { 708 $width = (int) $attrs['width']; 709 } 710 711 // mixed Position the left corner of this box on the page. The default is the current position. 712 $left = '.'; 713 if (isset($attrs['left'])) { 714 if ($attrs['left'] === '.') { 715 $left = '.'; 716 } elseif (!empty($attrs['left'])) { 717 $left = (int) $attrs['left']; 718 } elseif ($attrs['left'] === '0') { 719 $left = 0; 720 } 721 } 722 // mixed Position the top corner of this box on the page. the default is the current position 723 $top = '.'; 724 if (isset($attrs['top'])) { 725 if ($attrs['top'] === '.') { 726 $top = '.'; 727 } elseif (!empty($attrs['top'])) { 728 $top = (int) $attrs['top']; 729 } elseif ($attrs['top'] === '0') { 730 $top = 0; 731 } 732 } 733 // boolean After this box is finished rendering, should the next section of text start immediately after the this box or should it start on a new line under this box. 0 = no new line, 1 = force new line. Default is 0 734 $newline = false; 735 if (isset($attrs['newline'])) { 736 if ($attrs['newline'] === '1') { 737 $newline = true; 738 } elseif ($attrs['newline'] === '0') { 739 $newline = false; 740 } 741 } 742 // boolean 743 $pagecheck = true; 744 if (isset($attrs['pagecheck'])) { 745 if ($attrs['pagecheck'] === '0') { 746 $pagecheck = false; 747 } elseif ($attrs['pagecheck'] === '1') { 748 $pagecheck = true; 749 } 750 } 751 // boolean Cell padding 752 $padding = true; 753 if (isset($attrs['padding'])) { 754 if ($attrs['padding'] === '0') { 755 $padding = false; 756 } elseif ($attrs['padding'] === '1') { 757 $padding = true; 758 } 759 } 760 // boolean Reset this box Height 761 $reseth = false; 762 if (isset($attrs['reseth'])) { 763 if ($attrs['reseth'] === '1') { 764 $reseth = true; 765 } elseif ($attrs['reseth'] === '0') { 766 $reseth = false; 767 } 768 } 769 770 // string Style of rendering 771 $style = ''; 772 773 $this->print_data_stack[] = $this->print_data; 774 $this->print_data = false; 775 776 $this->wt_report_stack[] = $this->wt_report; 777 $this->wt_report = $this->report_root->createTextBox( 778 $width, 779 $height, 780 $border, 781 $bgcolor, 782 $newline, 783 $left, 784 $top, 785 $pagecheck, 786 $style, 787 $fill, 788 $padding, 789 $reseth 790 ); 791 } 792 793 /** 794 * XML <textBoxEndHandler> 795 */ 796 private function textBoxEndHandler() 797 { 798 $this->print_data = array_pop($this->print_data_stack); 799 $this->current_element = $this->wt_report; 800 $this->wt_report = array_pop($this->wt_report_stack); 801 $this->wt_report->addElement($this->current_element); 802 } 803 804 /** 805 * XLM <Text>. 806 * 807 * @param array $attrs an array of key value pairs for the attributes 808 */ 809 private function textStartHandler($attrs) 810 { 811 $this->print_data_stack[] = $this->print_data; 812 $this->print_data = true; 813 814 // string The name of the Style that should be used to render the text. 815 $style = ''; 816 if (!empty($attrs['style'])) { 817 $style = $attrs['style']; 818 } 819 820 // string The color of the text - Keep the black color as default 821 $color = ''; 822 if (!empty($attrs['color'])) { 823 $color = $attrs['color']; 824 } 825 826 $this->current_element = $this->report_root->createText($style, $color); 827 } 828 829 /** 830 * XML </Text> 831 */ 832 private function textEndHandler() 833 { 834 $this->print_data = array_pop($this->print_data_stack); 835 $this->wt_report->addElement($this->current_element); 836 } 837 838 /** 839 * XML <GetPersonName/> 840 * Get the name 841 * 1. id is empty - current GEDCOM record 842 * 2. id is set with a record id 843 * 844 * @param array $attrs an array of key value pairs for the attributes 845 */ 846 private function getPersonNameStartHandler($attrs) 847 { 848 $id = ''; 849 $match = []; 850 if (empty($attrs['id'])) { 851 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 852 $id = $match[1]; 853 } 854 } else { 855 if (preg_match('/\$(.+)/', $attrs['id'], $match)) { 856 if (isset($this->vars[$match[1]]['id'])) { 857 $id = $this->vars[$match[1]]['id']; 858 } 859 } else { 860 if (preg_match('/@(.+)/', $attrs['id'], $match)) { 861 $gmatch = []; 862 if (preg_match("/\d $match[1] @([^@]+)@/", $this->gedrec, $gmatch)) { 863 $id = $gmatch[1]; 864 } 865 } else { 866 $id = $attrs['id']; 867 } 868 } 869 } 870 if (!empty($id)) { 871 $record = GedcomRecord::getInstance($id, $this->tree); 872 if ($record === null) { 873 return; 874 } 875 if (!$record->canShowName()) { 876 $this->current_element->addText(I18N::translate('Private')); 877 } else { 878 $name = $record->getFullName(); 879 $name = preg_replace( 880 [ 881 '/<span class="starredname">/', 882 '/<\/span><\/span>/', 883 '/<\/span>/', 884 ], 885 [ 886 '«', 887 '', 888 '»', 889 ], 890 $name 891 ); 892 $name = strip_tags($name); 893 if (!empty($attrs['truncate'])) { 894 if (mb_strlen($name) > $attrs['truncate']) { 895 $name = mb_substr($name, 0, $attrs['truncate'] - 1) . '…'; 896 } 897 } else { 898 $addname = $record->getAddName(); 899 $addname = preg_replace( 900 [ 901 '/<span class="starredname">/', 902 '/<\/span><\/span>/', 903 '/<\/span>/', 904 ], 905 [ 906 '«', 907 '', 908 '»', 909 ], 910 $addname 911 ); 912 $addname = strip_tags($addname); 913 if (!empty($addname)) { 914 $name .= ' ' . $addname; 915 } 916 } 917 $this->current_element->addText(trim($name)); 918 } 919 } 920 } 921 922 /** 923 * XML <GedcomValue/> 924 * 925 * @param array $attrs an array of key value pairs for the attributes 926 */ 927 private function gedcomValueStartHandler($attrs) 928 { 929 $id = ''; 930 $match = []; 931 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 932 $id = $match[1]; 933 } 934 935 if (isset($attrs['newline']) && $attrs['newline'] == '1') { 936 $useBreak = '1'; 937 } else { 938 $useBreak = '0'; 939 } 940 941 $tag = $attrs['tag']; 942 if (!empty($tag)) { 943 if ($tag == '@desc') { 944 $value = $this->desc; 945 $value = trim($value); 946 $this->current_element->addText($value); 947 } 948 if ($tag == '@id') { 949 $this->current_element->addText($id); 950 } else { 951 $tag = str_replace('@fact', $this->fact, $tag); 952 if (empty($attrs['level'])) { 953 $temp = explode(' ', trim($this->gedrec)); 954 $level = $temp[0]; 955 if ($level == 0) { 956 $level++; 957 } 958 } else { 959 $level = $attrs['level']; 960 } 961 $tags = preg_split('/[: ]/', $tag); 962 $value = $this->getGedcomValue($tag, $level, $this->gedrec); 963 switch (end($tags)) { 964 case 'DATE': 965 $tmp = new Date($value); 966 $value = $tmp->display(); 967 break; 968 case 'PLAC': 969 $tmp = new Place($value, $this->tree); 970 $value = $tmp->getShortName(); 971 break; 972 } 973 if ($useBreak == '1') { 974 // Insert <br> when multiple dates exist. 975 // This works around a TCPDF bug that incorrectly wraps RTL dates on LTR pages 976 $value = str_replace('(', '<br>(', $value); 977 $value = str_replace('<span dir="ltr"><br>', '<br><span dir="ltr">', $value); 978 $value = str_replace('<span dir="rtl"><br>', '<br><span dir="rtl">', $value); 979 if (substr($value, 0, 6) == '<br>') { 980 $value = substr($value, 6); 981 } 982 } 983 $tmp = explode(':', $tag); 984 if (in_array(end($tmp), [ 985 'NOTE', 986 'TEXT', 987 ])) { 988 $value = Filter::formatText($value, $this->tree); // We'll strip HTML in addText() 989 } 990 $this->current_element->addText($value); 991 } 992 } 993 } 994 995 /** 996 * XML <RepeatTag> 997 * 998 * @param array $attrs an array of key value pairs for the attributes 999 */ 1000 private function repeatTagStartHandler($attrs) 1001 { 1002 $this->process_repeats++; 1003 if ($this->process_repeats > 1) { 1004 return; 1005 } 1006 1007 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 1008 $this->repeats = []; 1009 $this->repeat_bytes = xml_get_current_line_number($this->parser); 1010 1011 $tag = ''; 1012 if (isset($attrs['tag'])) { 1013 $tag = $attrs['tag']; 1014 } 1015 if (!empty($tag)) { 1016 if ($tag == '@desc') { 1017 $value = $this->desc; 1018 $value = trim($value); 1019 $this->current_element->addText($value); 1020 } else { 1021 $tag = str_replace('@fact', $this->fact, $tag); 1022 $tags = explode(':', $tag); 1023 $temp = explode(' ', trim($this->gedrec)); 1024 $level = $temp[0]; 1025 if ($level == 0) { 1026 $level++; 1027 } 1028 $subrec = $this->gedrec; 1029 $t = $tag; 1030 $count = count($tags); 1031 $i = 0; 1032 while ($i < $count) { 1033 $t = $tags[$i]; 1034 if (!empty($t)) { 1035 if ($i < ($count - 1)) { 1036 $subrec = Functions::getSubRecord($level, "$level $t", $subrec); 1037 if (empty($subrec)) { 1038 $level--; 1039 $subrec = Functions::getSubRecord($level, "@ $t", $this->gedrec); 1040 if (empty($subrec)) { 1041 return; 1042 } 1043 } 1044 } 1045 $level++; 1046 } 1047 $i++; 1048 } 1049 $level--; 1050 $count = preg_match_all("/$level $t(.*)/", $subrec, $match, PREG_SET_ORDER); 1051 $i = 0; 1052 while ($i < $count) { 1053 $i++; 1054 // Privacy check - is this a link, and are we allowed to view the linked object? 1055 $subrecord = Functions::getSubRecord($level, "$level $t", $subrec, $i); 1056 if (preg_match('/^\d ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@/', $subrecord, $xref_match)) { 1057 $linked_object = GedcomRecord::getInstance($xref_match[1], $this->tree); 1058 if ($linked_object && !$linked_object->canShow()) { 1059 continue; 1060 } 1061 } 1062 $this->repeats[] = $subrecord; 1063 } 1064 } 1065 } 1066 } 1067 1068 /** 1069 * XML </ RepeatTag> 1070 */ 1071 private function repeatTagEndHandler() 1072 { 1073 $this->process_repeats--; 1074 if ($this->process_repeats > 0) { 1075 return; 1076 } 1077 1078 // Check if there is anything to repeat 1079 if (count($this->repeats) > 0) { 1080 // No need to load them if not used... 1081 1082 $lineoffset = 0; 1083 foreach ($this->repeats_stack as $rep) { 1084 $lineoffset += $rep[1]; 1085 } 1086 //-- read the xml from the file 1087 $lines = file($this->report); 1088 while (strpos($lines[$lineoffset + $this->repeat_bytes], '<RepeatTag') === false) { 1089 $lineoffset--; 1090 } 1091 $lineoffset++; 1092 $reportxml = "<tempdoc>\n"; 1093 $line_nr = $lineoffset + $this->repeat_bytes; 1094 // RepeatTag Level counter 1095 $count = 1; 1096 while (0 < $count) { 1097 if (strstr($lines[$line_nr], '<RepeatTag') !== false) { 1098 $count++; 1099 } elseif (strstr($lines[$line_nr], '</RepeatTag') !== false) { 1100 $count--; 1101 } 1102 if (0 < $count) { 1103 $reportxml .= $lines[$line_nr]; 1104 } 1105 $line_nr++; 1106 } 1107 // No need to drag this 1108 unset($lines); 1109 $reportxml .= "</tempdoc>\n"; 1110 // Save original values 1111 $this->parser_stack[] = $this->parser; 1112 $oldgedrec = $this->gedrec; 1113 foreach ($this->repeats as $gedrec) { 1114 $this->gedrec = $gedrec; 1115 $repeat_parser = xml_parser_create(); 1116 $this->parser = $repeat_parser; 1117 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 1118 xml_set_element_handler($repeat_parser, [ 1119 $this, 1120 'startElement', 1121 ], [ 1122 $this, 1123 'endElement', 1124 ]); 1125 xml_set_character_data_handler($repeat_parser, [ 1126 $this, 1127 'characterData', 1128 ]); 1129 if (!xml_parse($repeat_parser, $reportxml, true)) { 1130 throw new \DomainException(sprintf( 1131 'RepeatTagEHandler XML error: %s at line %d', 1132 xml_error_string(xml_get_error_code($repeat_parser)), 1133 xml_get_current_line_number($repeat_parser) 1134 )); 1135 } 1136 xml_parser_free($repeat_parser); 1137 } 1138 // Restore original values 1139 $this->gedrec = $oldgedrec; 1140 $this->parser = array_pop($this->parser_stack); 1141 } 1142 list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack); 1143 } 1144 1145 /** 1146 * Variable lookup 1147 * Retrieve predefined variables : 1148 * @ desc GEDCOM fact description, example: 1149 * 1 EVEN This is a description 1150 * @ fact GEDCOM fact tag, such as BIRT, DEAT etc. 1151 * $ I18N::translate('....') 1152 * $ language_settings[] 1153 * 1154 * @param array $attrs an array of key value pairs for the attributes 1155 */ 1156 private function varStartHandler($attrs) 1157 { 1158 if (empty($attrs['var'])) { 1159 throw new \DomainException('REPORT ERROR var: The attribute "var=" is missing or not set in the XML file on line: ' . xml_get_current_line_number($this->parser)); 1160 } 1161 1162 $var = $attrs['var']; 1163 // SetVar element preset variables 1164 if (!empty($this->vars[$var]['id'])) { 1165 $var = $this->vars[$var]['id']; 1166 } else { 1167 $tfact = $this->fact; 1168 if (($this->fact === 'EVEN' || $this->fact === 'FACT') && $this->type !== ' ') { 1169 // Use : 1170 // n TYPE This text if string 1171 $tfact = $this->type; 1172 } 1173 $var = str_replace([ 1174 '@fact', 1175 '@desc', 1176 ], [ 1177 GedcomTag::getLabel($tfact), 1178 $this->desc, 1179 ], $var); 1180 if (preg_match('/^I18N::number\((.+)\)$/', $var, $match)) { 1181 $var = I18N::number((int) $match[1]); 1182 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $var, $match)) { 1183 $var = I18N::translate($match[1]); 1184 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $var, $match)) { 1185 $var = I18N::translateContext($match[1], $match[2]); 1186 } 1187 } 1188 // Check if variable is set as a date and reformat the date 1189 if (isset($attrs['date'])) { 1190 if ($attrs['date'] === '1') { 1191 $g = new Date($var); 1192 $var = $g->display(); 1193 } 1194 } 1195 $this->current_element->addText($var); 1196 $this->text = $var; // Used for title/descriptio 1197 } 1198 1199 /** 1200 * XML <Facts> 1201 * 1202 * @param array $attrs an array of key value pairs for the attributes 1203 */ 1204 private function factsStartHandler($attrs) 1205 { 1206 $this->process_repeats++; 1207 if ($this->process_repeats > 1) { 1208 return; 1209 } 1210 1211 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 1212 $this->repeats = []; 1213 $this->repeat_bytes = xml_get_current_line_number($this->parser); 1214 1215 $id = ''; 1216 $match = []; 1217 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1218 $id = $match[1]; 1219 } 1220 $tag = ''; 1221 if (isset($attrs['ignore'])) { 1222 $tag .= $attrs['ignore']; 1223 } 1224 if (preg_match('/\$(.+)/', $tag, $match)) { 1225 $tag = $this->vars[$match[1]]['id']; 1226 } 1227 1228 $record = GedcomRecord::getInstance($id, $this->tree); 1229 if (empty($attrs['diff']) && !empty($id)) { 1230 $facts = $record->getFacts(); 1231 Functions::sortFacts($facts); 1232 $this->repeats = []; 1233 $nonfacts = explode(',', $tag); 1234 foreach ($facts as $event) { 1235 if (!in_array($event->getTag(), $nonfacts)) { 1236 $this->repeats[] = $event->getGedcom(); 1237 } 1238 } 1239 } else { 1240 foreach ($record->getFacts() as $fact) { 1241 if ($fact->isPendingAddition() && $fact->getTag() !== 'CHAN') { 1242 $this->repeats[] = $fact->getGedcom(); 1243 } 1244 } 1245 } 1246 } 1247 1248 /** 1249 * XML </Facts> 1250 */ 1251 private function factsEndHandler() 1252 { 1253 $this->process_repeats--; 1254 if ($this->process_repeats > 0) { 1255 return; 1256 } 1257 1258 // Check if there is anything to repeat 1259 if (count($this->repeats) > 0) { 1260 $line = xml_get_current_line_number($this->parser) - 1; 1261 $lineoffset = 0; 1262 foreach ($this->repeats_stack as $rep) { 1263 $lineoffset += $rep[1]; 1264 } 1265 1266 //-- read the xml from the file 1267 $lines = file($this->report); 1268 while ($lineoffset + $this->repeat_bytes > 0 && strpos($lines[$lineoffset + $this->repeat_bytes], '<Facts ') === false) { 1269 $lineoffset--; 1270 } 1271 $lineoffset++; 1272 $reportxml = "<tempdoc>\n"; 1273 $i = $line + $lineoffset; 1274 $line_nr = $this->repeat_bytes + $lineoffset; 1275 while ($line_nr < $i) { 1276 $reportxml .= $lines[$line_nr]; 1277 $line_nr++; 1278 } 1279 // No need to drag this 1280 unset($lines); 1281 $reportxml .= "</tempdoc>\n"; 1282 // Save original values 1283 $this->parser_stack[] = $this->parser; 1284 $oldgedrec = $this->gedrec; 1285 $count = count($this->repeats); 1286 $i = 0; 1287 while ($i < $count) { 1288 $this->gedrec = $this->repeats[$i]; 1289 $this->fact = ''; 1290 $this->desc = ''; 1291 if (preg_match('/1 (\w+)(.*)/', $this->gedrec, $match)) { 1292 $this->fact = $match[1]; 1293 if ($this->fact === 'EVEN' || $this->fact === 'FACT') { 1294 $tmatch = []; 1295 if (preg_match('/2 TYPE (.+)/', $this->gedrec, $tmatch)) { 1296 $this->type = trim($tmatch[1]); 1297 } else { 1298 $this->type = ' '; 1299 } 1300 } 1301 $this->desc = trim($match[2]); 1302 $this->desc .= Functions::getCont(2, $this->gedrec); 1303 } 1304 $repeat_parser = xml_parser_create(); 1305 $this->parser = $repeat_parser; 1306 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 1307 xml_set_element_handler($repeat_parser, [ 1308 $this, 1309 'startElement', 1310 ], [ 1311 $this, 1312 'endElement', 1313 ]); 1314 xml_set_character_data_handler($repeat_parser, [ 1315 $this, 1316 'characterData', 1317 ]); 1318 if (!xml_parse($repeat_parser, $reportxml, true)) { 1319 throw new \DomainException(sprintf( 1320 'FactsEHandler XML error: %s at line %d', 1321 xml_error_string(xml_get_error_code($repeat_parser)), 1322 xml_get_current_line_number($repeat_parser) 1323 )); 1324 } 1325 xml_parser_free($repeat_parser); 1326 $i++; 1327 } 1328 // Restore original values 1329 $this->parser = array_pop($this->parser_stack); 1330 $this->gedrec = $oldgedrec; 1331 } 1332 list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack); 1333 } 1334 1335 /** 1336 * Setting upp or changing variables in the XML 1337 * The XML variable name and value is stored in $this->vars 1338 * 1339 * @param array $attrs an array of key value pairs for the attributes 1340 */ 1341 private function setVarStartHandler($attrs) 1342 { 1343 if (empty($attrs['name'])) { 1344 throw new \DomainException('REPORT ERROR var: The attribute "name" is missing or not set in the XML file'); 1345 } 1346 1347 $name = $attrs['name']; 1348 $value = $attrs['value']; 1349 $match = []; 1350 // Current GEDCOM record strings 1351 if ($value == '@ID') { 1352 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1353 $value = $match[1]; 1354 } 1355 } elseif ($value == '@fact') { 1356 $value = $this->fact; 1357 } elseif ($value == '@desc') { 1358 $value = $this->desc; 1359 } elseif ($value == '@generation') { 1360 $value = $this->generation; 1361 } elseif (preg_match("/@(\w+)/", $value, $match)) { 1362 $gmatch = []; 1363 if (preg_match("/\d $match[1] (.+)/", $this->gedrec, $gmatch)) { 1364 $value = str_replace('@', '', trim($gmatch[1])); 1365 } 1366 } 1367 if (preg_match("/\\$(\w+)/", $name, $match)) { 1368 $name = $this->vars["'" . $match[1] . "'"]['id']; 1369 } 1370 $count = preg_match_all("/\\$(\w+)/", $value, $match, PREG_SET_ORDER); 1371 $i = 0; 1372 while ($i < $count) { 1373 $t = $this->vars[$match[$i][1]]['id']; 1374 $value = preg_replace('/\$' . $match[$i][1] . '/', $t, $value, 1); 1375 $i++; 1376 } 1377 if (preg_match('/^I18N::number\((.+)\)$/', $value, $match)) { 1378 $value = I18N::number((int) $match[1]); 1379 } elseif (preg_match('/^I18N::translate\(\'(.+)\'\)$/', $value, $match)) { 1380 $value = I18N::translate($match[1]); 1381 } elseif (preg_match('/^I18N::translateContext\(\'(.+)\', *\'(.+)\'\)$/', $value, $match)) { 1382 $value = I18N::translateContext($match[1], $match[2]); 1383 } 1384 // Arithmetic functions 1385 if (preg_match("/(\d+)\s*([\-\+\*\/])\s*(\d+)/", $value, $match)) { 1386 switch ($match[2]) { 1387 case '+': 1388 $t = $match[1] + $match[3]; 1389 $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value); 1390 break; 1391 case '-': 1392 $t = $match[1] - $match[3]; 1393 $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value); 1394 break; 1395 case '*': 1396 $t = $match[1] * $match[3]; 1397 $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value); 1398 break; 1399 case '/': 1400 $t = $match[1] / $match[3]; 1401 $value = preg_replace('/' . $match[1] . "\s*([\-\+\*\/])\s*" . $match[3] . '/', $t, $value); 1402 break; 1403 } 1404 } 1405 if (strpos($value, '@') !== false) { 1406 $value = ''; 1407 } 1408 $this->vars[$name]['id'] = $value; 1409 } 1410 1411 /** 1412 * XML <if > start element 1413 * 1414 * @param array $attrs an array of key value pairs for the attributes 1415 */ 1416 private function ifStartHandler($attrs) 1417 { 1418 if ($this->process_ifs > 0) { 1419 $this->process_ifs++; 1420 1421 return; 1422 } 1423 1424 $condition = $attrs['condition']; 1425 $condition = $this->substituteVars($condition, true); 1426 $condition = str_replace([ 1427 ' LT ', 1428 ' GT ', 1429 ], [ 1430 '<', 1431 '>', 1432 ], $condition); 1433 // Replace the first accurance only once of @fact:DATE or in any other combinations to the current fact, such as BIRT 1434 $condition = str_replace('@fact:', $this->fact . ':', $condition); 1435 $match = []; 1436 $count = preg_match_all("/@([\w:\.]+)/", $condition, $match, PREG_SET_ORDER); 1437 $i = 0; 1438 while ($i < $count) { 1439 $id = $match[$i][1]; 1440 $value = '""'; 1441 if ($id == 'ID') { 1442 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1443 $value = "'" . $match[1] . "'"; 1444 } 1445 } elseif ($id === 'fact') { 1446 $value = '"' . $this->fact . '"'; 1447 } elseif ($id === 'desc') { 1448 $value = '"' . addslashes($this->desc) . '"'; 1449 } elseif ($id === 'generation') { 1450 $value = '"' . $this->generation . '"'; 1451 } else { 1452 $temp = explode(' ', trim($this->gedrec)); 1453 $level = $temp[0]; 1454 if ($level == 0) { 1455 $level++; 1456 } 1457 $value = $this->getGedcomValue($id, $level, $this->gedrec); 1458 if (empty($value)) { 1459 $level++; 1460 $value = $this->getGedcomValue($id, $level, $this->gedrec); 1461 } 1462 $value = preg_replace('/^@(' . WT_REGEX_XREF . ')@$/', '$1', $value); 1463 $value = '"' . addslashes($value) . '"'; 1464 } 1465 $condition = str_replace("@$id", $value, $condition); 1466 $i++; 1467 } 1468 1469 $ret = (new ExpressionLanguage())->evaluate($condition); 1470 1471 if (!$ret) { 1472 $this->process_ifs++; 1473 } 1474 } 1475 1476 /** 1477 * XML <if /> end element 1478 */ 1479 private function ifEndHandler() 1480 { 1481 if ($this->process_ifs > 0) { 1482 $this->process_ifs--; 1483 } 1484 } 1485 1486 /** 1487 * XML <Footnote > start element 1488 * Collect the Footnote links 1489 * GEDCOM Records that are protected by Privacy setting will be ignore 1490 * 1491 * @param array $attrs an array of key value pairs for the attributes 1492 */ 1493 private function footnoteStartHandler($attrs) 1494 { 1495 $id = ''; 1496 if (preg_match('/[0-9] (.+) @(.+)@/', $this->gedrec, $match)) { 1497 $id = $match[2]; 1498 } 1499 $record = GedcomRecord::getInstance($id, $this->tree); 1500 if ($record && $record->canShow()) { 1501 $this->print_data_stack[] = $this->print_data; 1502 $this->print_data = true; 1503 $style = ''; 1504 if (!empty($attrs['style'])) { 1505 $style = $attrs['style']; 1506 } 1507 $this->footnote_element = $this->current_element; 1508 $this->current_element = $this->report_root->createFootnote($style); 1509 } else { 1510 $this->print_data = false; 1511 $this->process_footnote = false; 1512 } 1513 } 1514 1515 /** 1516 * XML <Footnote /> end element 1517 * Print the collected Footnote data 1518 */ 1519 private function footnoteEndHandler() 1520 { 1521 if ($this->process_footnote) { 1522 $this->print_data = array_pop($this->print_data_stack); 1523 $temp = trim($this->current_element->getValue()); 1524 if (strlen($temp) > 3) { 1525 $this->wt_report->addElement($this->current_element); 1526 } 1527 $this->current_element = $this->footnote_element; 1528 } else { 1529 $this->process_footnote = true; 1530 } 1531 } 1532 1533 /** 1534 * XML <FootnoteTexts /> element 1535 */ 1536 private function footnoteTextsStartHandler() 1537 { 1538 $temp = 'footnotetexts'; 1539 $this->wt_report->addElement($temp); 1540 } 1541 1542 /** 1543 * XML element Forced line break handler - HTML code 1544 */ 1545 private function brStartHandler() 1546 { 1547 if ($this->print_data && $this->process_gedcoms === 0) { 1548 $this->current_element->addText('<br>'); 1549 } 1550 } 1551 1552 /** 1553 * XML <sp />element Forced space handler 1554 */ 1555 private function spStartHandler() 1556 { 1557 if ($this->print_data && $this->process_gedcoms === 0) { 1558 $this->current_element->addText(' '); 1559 } 1560 } 1561 1562 /** 1563 * XML <HighlightedImage/> 1564 * 1565 * @param array $attrs an array of key value pairs for the attributes 1566 */ 1567 private function highlightedImageStartHandler($attrs) 1568 { 1569 $id = ''; 1570 $match = []; 1571 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 1572 $id = $match[1]; 1573 } 1574 1575 // mixed Position the top corner of this box on the page. the default is the current position 1576 $top = '.'; 1577 if (isset($attrs['top'])) { 1578 if ($attrs['top'] === '0') { 1579 $top = 0; 1580 } elseif ($attrs['top'] === '.') { 1581 $top = '.'; 1582 } elseif (!empty($attrs['top'])) { 1583 $top = (int) $attrs['top']; 1584 } 1585 } 1586 1587 // mixed Position the left corner of this box on the page. the default is the current position 1588 $left = '.'; 1589 if (isset($attrs['left'])) { 1590 if ($attrs['left'] === '0') { 1591 $left = 0; 1592 } elseif ($attrs['left'] === '.') { 1593 $left = '.'; 1594 } elseif (!empty($attrs['left'])) { 1595 $left = (int) $attrs['left']; 1596 } 1597 } 1598 1599 // string Align the image in left, center, right 1600 $align = ''; 1601 if (!empty($attrs['align'])) { 1602 $align = $attrs['align']; 1603 } 1604 1605 // string Next Line should be T:next to the image, N:next line 1606 $ln = ''; 1607 if (!empty($attrs['ln'])) { 1608 $ln = $attrs['ln']; 1609 } 1610 1611 $width = 0; 1612 $height = 0; 1613 if (!empty($attrs['width'])) { 1614 $width = (int) $attrs['width']; 1615 } 1616 if (!empty($attrs['height'])) { 1617 $height = (int) $attrs['height']; 1618 } 1619 1620 $person = Individual::getInstance($id, $this->tree); 1621 $media_file = $person->findHighlightedMediaFile(); 1622 1623 if ($media_file !== null && $media_file->fileExists()) { 1624 $attributes = getimagesize($media_file->getServerFilename()) ?: [ 1625 0, 1626 0, 1627 ]; 1628 if ($width > 0 && $height == 0) { 1629 $perc = $width / $attributes[0]; 1630 $height = round($attributes[1] * $perc); 1631 } elseif ($height > 0 && $width == 0) { 1632 $perc = $height / $attributes[1]; 1633 $width = round($attributes[0] * $perc); 1634 } else { 1635 $width = $attributes[0]; 1636 $height = $attributes[1]; 1637 } 1638 $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln); 1639 $this->wt_report->addElement($image); 1640 } 1641 } 1642 1643 /** 1644 * XML <Image/> 1645 * 1646 * @param array $attrs an array of key value pairs for the attributes 1647 */ 1648 private function imageStartHandler($attrs) 1649 { 1650 // mixed Position the top corner of this box on the page. the default is the current position 1651 $top = '.'; 1652 if (isset($attrs['top'])) { 1653 if ($attrs['top'] === '0') { 1654 $top = 0; 1655 } elseif ($attrs['top'] === '.') { 1656 $top = '.'; 1657 } elseif (!empty($attrs['top'])) { 1658 $top = (int) $attrs['top']; 1659 } 1660 } 1661 1662 // mixed Position the left corner of this box on the page. the default is the current position 1663 $left = '.'; 1664 if (isset($attrs['left'])) { 1665 if ($attrs['left'] === '0') { 1666 $left = 0; 1667 } elseif ($attrs['left'] === '.') { 1668 $left = '.'; 1669 } elseif (!empty($attrs['left'])) { 1670 $left = (int) $attrs['left']; 1671 } 1672 } 1673 1674 // string Align the image in left, center, right 1675 $align = ''; 1676 if (!empty($attrs['align'])) { 1677 $align = $attrs['align']; 1678 } 1679 1680 // string Next Line should be T:next to the image, N:next line 1681 $ln = 'T'; 1682 if (!empty($attrs['ln'])) { 1683 $ln = $attrs['ln']; 1684 } 1685 1686 $width = 0; 1687 $height = 0; 1688 if (!empty($attrs['width'])) { 1689 $width = (int) $attrs['width']; 1690 } 1691 if (!empty($attrs['height'])) { 1692 $height = (int) $attrs['height']; 1693 } 1694 1695 $file = ''; 1696 if (!empty($attrs['file'])) { 1697 $file = $attrs['file']; 1698 } 1699 if ($file == '@FILE') { 1700 $match = []; 1701 if (preg_match("/\d OBJE @(.+)@/", $this->gedrec, $match)) { 1702 $mediaobject = Media::getInstance($match[1], $this->tree); 1703 $media_file = $mediaobject->firstImageFile(); 1704 1705 if ($media_file !== null && $media_file->fileExists()) { 1706 $attributes = getimagesize($media_file->getServerFilename()) ?: [ 1707 0, 1708 0, 1709 ]; 1710 if ($width > 0 && $height == 0) { 1711 $perc = $width / $attributes[0]; 1712 $height = round($attributes[1] * $perc); 1713 } elseif ($height > 0 && $width == 0) { 1714 $perc = $height / $attributes[1]; 1715 $width = round($attributes[0] * $perc); 1716 } else { 1717 $width = $attributes[0]; 1718 $height = $attributes[1]; 1719 } 1720 $image = $this->report_root->createImageFromObject($media_file, $left, $top, $width, $height, $align, $ln); 1721 $this->wt_report->addElement($image); 1722 } 1723 } 1724 } else { 1725 if (file_exists($file) && preg_match('/(jpg|jpeg|png|gif)$/i', $file)) { 1726 $size = getimagesize($file); 1727 if ($width > 0 && $height == 0) { 1728 $perc = $width / $size[0]; 1729 $height = round($size[1] * $perc); 1730 } elseif ($height > 0 && $width == 0) { 1731 $perc = $height / $size[1]; 1732 $width = round($size[0] * $perc); 1733 } else { 1734 $width = $size[0]; 1735 $height = $size[1]; 1736 } 1737 $image = $this->report_root->createImage($file, $left, $top, $width, $height, $align, $ln); 1738 $this->wt_report->addElement($image); 1739 } 1740 } 1741 } 1742 1743 /** 1744 * XML <Line> element handler 1745 * 1746 * @param array $attrs an array of key value pairs for the attributes 1747 */ 1748 private function lineStartHandler($attrs) 1749 { 1750 // Start horizontal position, current position (default) 1751 $x1 = '.'; 1752 if (isset($attrs['x1'])) { 1753 if ($attrs['x1'] === '0') { 1754 $x1 = 0; 1755 } elseif ($attrs['x1'] === '.') { 1756 $x1 = '.'; 1757 } elseif (!empty($attrs['x1'])) { 1758 $x1 = (int) $attrs['x1']; 1759 } 1760 } 1761 // Start vertical position, current position (default) 1762 $y1 = '.'; 1763 if (isset($attrs['y1'])) { 1764 if ($attrs['y1'] === '0') { 1765 $y1 = 0; 1766 } elseif ($attrs['y1'] === '.') { 1767 $y1 = '.'; 1768 } elseif (!empty($attrs['y1'])) { 1769 $y1 = (int) $attrs['y1']; 1770 } 1771 } 1772 // End horizontal position, maximum width (default) 1773 $x2 = '.'; 1774 if (isset($attrs['x2'])) { 1775 if ($attrs['x2'] === '0') { 1776 $x2 = 0; 1777 } elseif ($attrs['x2'] === '.') { 1778 $x2 = '.'; 1779 } elseif (!empty($attrs['x2'])) { 1780 $x2 = (int) $attrs['x2']; 1781 } 1782 } 1783 // End vertical position 1784 $y2 = '.'; 1785 if (isset($attrs['y2'])) { 1786 if ($attrs['y2'] === '0') { 1787 $y2 = 0; 1788 } elseif ($attrs['y2'] === '.') { 1789 $y2 = '.'; 1790 } elseif (!empty($attrs['y2'])) { 1791 $y2 = (int) $attrs['y2']; 1792 } 1793 } 1794 1795 $line = $this->report_root->createLine($x1, $y1, $x2, $y2); 1796 $this->wt_report->addElement($line); 1797 } 1798 1799 /** 1800 * XML <List> 1801 * 1802 * @param array $attrs an array of key value pairs for the attributes 1803 */ 1804 private function listStartHandler($attrs) 1805 { 1806 $this->process_repeats++; 1807 if ($this->process_repeats > 1) { 1808 return; 1809 } 1810 1811 $match = []; 1812 if (isset($attrs['sortby'])) { 1813 $sortby = $attrs['sortby']; 1814 if (preg_match("/\\$(\w+)/", $sortby, $match)) { 1815 $sortby = $this->vars[$match[1]]['id']; 1816 $sortby = trim($sortby); 1817 } 1818 } else { 1819 $sortby = 'NAME'; 1820 } 1821 1822 if (isset($attrs['list'])) { 1823 $listname = $attrs['list']; 1824 } else { 1825 $listname = 'individual'; 1826 } 1827 // Some filters/sorts can be applied using SQL, while others require PHP 1828 switch ($listname) { 1829 case 'pending': 1830 $rows = Database::prepare( 1831 "SELECT xref, CASE new_gedcom WHEN '' THEN old_gedcom ELSE new_gedcom END AS gedcom" . 1832 " FROM `##change`" . " WHERE (xref, change_id) IN (" . 1833 " SELECT xref, MAX(change_id)" . 1834 " FROM `##change`" . 1835 " WHERE status = 'pending' AND gedcom_id = :tree_id" . 1836 " GROUP BY xref" . 1837 " )" 1838 )->execute([ 1839 'tree_id' => $this->tree->getTreeId(), 1840 ])->fetchAll(); 1841 $this->list = []; 1842 foreach ($rows as $row) { 1843 $this->list[] = GedcomRecord::getInstance($row->xref, $this->tree, $row->gedcom); 1844 } 1845 break; 1846 case 'individual': 1847 $sql_select = "SELECT i_id AS xref, i_gedcom AS gedcom FROM `##individuals` "; 1848 $sql_join = ""; 1849 $sql_where = " WHERE i_file = :tree_id"; 1850 $sql_order_by = ""; 1851 $sql_params = ['tree_id' => $this->tree->getTreeId()]; 1852 foreach ($attrs as $attr => $value) { 1853 if (strpos($attr, 'filter') === 0 && $value) { 1854 $value = $this->substituteVars($value, false); 1855 // Convert the various filters into SQL 1856 if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) { 1857 $sql_join .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=i_file AND {$attr}.d_gid=i_id)"; 1858 $sql_where .= " AND {$attr}.d_fact = :{$attr}fact"; 1859 $sql_params[$attr . 'fact'] = $match[1]; 1860 $date = new Date($match[3]); 1861 if ($match[2] == 'LTE') { 1862 $sql_where .= " AND {$attr}.d_julianday2 <= :{$attr}date"; 1863 $sql_params[$attr . 'date'] = $date->maximumJulianDay(); 1864 } else { 1865 $sql_where .= " AND {$attr}.d_julianday1 >= :{$attr}date"; 1866 $sql_params[$attr . 'date'] = $date->minimumJulianDay(); 1867 } 1868 if ($sortby == $match[1]) { 1869 $sortby = ""; 1870 $sql_order_by .= ($sql_order_by ? ", " : " ORDER BY ") . "{$attr}.d_julianday1"; 1871 } 1872 unset($attrs[$attr]); // This filter has been fully processed 1873 } elseif (preg_match('/^NAME CONTAINS (.*)$/', $value, $match)) { 1874 // Do nothing, unless you have to 1875 if ($match[1] != '' || $sortby == 'NAME') { 1876 $sql_join .= " JOIN `##name` AS {$attr} ON (n_file=i_file AND n_id=i_id)"; 1877 // Search the DB only if there is any name supplied 1878 if ($match[1] != '') { 1879 $names = explode(' ', $match[1]); 1880 foreach ($names as $n => $name) { 1881 $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')"; 1882 $sql_params[$attr . 'name' . $n] = $name; 1883 } 1884 } 1885 // Let the DB do the name sorting even when no name was entered 1886 if ($sortby == 'NAME') { 1887 $sortby = ''; 1888 $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort"; 1889 } 1890 } 1891 unset($attrs[$attr]); // This filter has been fully processed 1892 } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) { 1893 $sql_where .= " AND i_gedcom REGEXP :{$attr}gedcom"; 1894 // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT" 1895 $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]); 1896 unset($attrs[$attr]); // This filter has been fully processed 1897 } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) { 1898 $sql_join .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file = i_file)"; 1899 $sql_join .= " JOIN `##placelinks` AS {$attr}b ON ({$attr}a.p_file = {$attr}b.pl_file AND {$attr}b.pl_p_id = {$attr}a.p_id AND {$attr}b.pl_gid = i_id)"; 1900 $sql_where .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')"; 1901 $sql_params[$attr . 'place'] = $match[1]; 1902 // Don't unset this filter. This is just initial filtering 1903 } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) { 1904 $sql_where .= " AND i_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')"; 1905 $sql_params[$attr . 'contains1'] = $match[1]; 1906 $sql_params[$attr . 'contains2'] = $match[2]; 1907 $sql_params[$attr . 'contains3'] = $match[3]; 1908 // Don't unset this filter. This is just initial filtering 1909 } 1910 } 1911 } 1912 1913 $this->list = []; 1914 $rows = Database::prepare( 1915 $sql_select . $sql_join . $sql_where . $sql_order_by 1916 )->execute($sql_params)->fetchAll(); 1917 1918 foreach ($rows as $row) { 1919 $this->list[$row->xref] = Individual::getInstance($row->xref, $this->tree, $row->gedcom); 1920 } 1921 break; 1922 1923 case 'family': 1924 $sql_select = "SELECT f_id AS xref, f_gedcom AS gedcom FROM `##families`"; 1925 $sql_join = ""; 1926 $sql_where = " WHERE f_file = :tree_id"; 1927 $sql_order_by = ""; 1928 $sql_params = ['tree_id' => $this->tree->getTreeId()]; 1929 foreach ($attrs as $attr => $value) { 1930 if (strpos($attr, 'filter') === 0 && $value) { 1931 $value = $this->substituteVars($value, false); 1932 // Convert the various filters into SQL 1933 if (preg_match('/^(\w+):DATE (LTE|GTE) (.+)$/', $value, $match)) { 1934 $sql_join .= " JOIN `##dates` AS {$attr} ON ({$attr}.d_file=f_file AND {$attr}.d_gid=f_id)"; 1935 $sql_where .= " AND {$attr}.d_fact = :{$attr}fact"; 1936 $sql_params[$attr . 'fact'] = $match[1]; 1937 $date = new Date($match[3]); 1938 if ($match[2] == 'LTE') { 1939 $sql_where .= " AND {$attr}.d_julianday2 <= :{$attr}date"; 1940 $sql_params[$attr . 'date'] = $date->maximumJulianDay(); 1941 } else { 1942 $sql_where .= " AND {$attr}.d_julianday1 >= :{$attr}date"; 1943 $sql_params[$attr . 'date'] = $date->minimumJulianDay(); 1944 } 1945 if ($sortby == $match[1]) { 1946 $sortby = ''; 1947 $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.d_julianday1"; 1948 } 1949 unset($attrs[$attr]); // This filter has been fully processed 1950 } elseif (preg_match('/^REGEXP \/(.+)\//', $value, $match)) { 1951 $sql_where .= " AND f_gedcom REGEXP :{$attr}gedcom"; 1952 // PDO helpfully escapes backslashes for us, preventing us from matching "\n1 FACT" 1953 $sql_params[$attr . 'gedcom'] = str_replace('\n', "\n", $match[1]); 1954 unset($attrs[$attr]); // This filter has been fully processed 1955 } elseif (preg_match('/^NAME CONTAINS (.+)$/', $value, $match)) { 1956 // Do nothing, unless you have to 1957 if ($match[1] != '' || $sortby == 'NAME') { 1958 $sql_join .= " JOIN `##name` AS {$attr} ON n_file = f_file AND n_id IN (f_husb, f_wife)"; 1959 // Search the DB only if there is any name supplied 1960 if ($match[1] != '') { 1961 $names = explode(' ', $match[1]); 1962 foreach ($names as $n => $name) { 1963 $sql_where .= " AND {$attr}.n_full LIKE CONCAT('%', :{$attr}name{$n}, '%')"; 1964 $sql_params[$attr . 'name' . $n] = $name; 1965 } 1966 } 1967 // Let the DB do the name sorting even when no name was entered 1968 if ($sortby == 'NAME') { 1969 $sortby = ''; 1970 $sql_order_by .= ($sql_order_by ? ', ' : ' ORDER BY ') . "{$attr}.n_sort"; 1971 } 1972 } 1973 unset($attrs[$attr]); // This filter has been fully processed 1974 } elseif (preg_match('/^(?:\w+):PLAC CONTAINS (.+)$/', $value, $match)) { 1975 $sql_join .= " JOIN `##places` AS {$attr}a ON ({$attr}a.p_file=f_file)"; 1976 $sql_join .= " JOIN `##placelinks` AS {$attr}b ON ({$attr}a.p_file={$attr}b.pl_file AND {$attr}b.pl_p_id={$attr}a.p_id AND {$attr}b.pl_gid=f_id)"; 1977 $sql_where .= " AND {$attr}a.p_place LIKE CONCAT('%', :{$attr}place, '%')"; 1978 $sql_params[$attr . 'place'] = $match[1]; 1979 // Don't unset this filter. This is just initial filtering 1980 } elseif (preg_match('/^(\w*):*(\w*) CONTAINS (.+)$/', $value, $match)) { 1981 $sql_where .= " AND f_gedcom LIKE CONCAT('%', :{$attr}contains1, '%', :{$attr}contains2, '%', :{$attr}contains3, '%')"; 1982 $sql_params[$attr . 'contains1'] = $match[1]; 1983 $sql_params[$attr . 'contains2'] = $match[2]; 1984 $sql_params[$attr . 'contains3'] = $match[3]; 1985 // Don't unset this filter. This is just initial filtering 1986 } 1987 } 1988 } 1989 1990 $this->list = []; 1991 $rows = Database::prepare( 1992 $sql_select . $sql_join . $sql_where . $sql_order_by 1993 )->execute($sql_params)->fetchAll(); 1994 1995 foreach ($rows as $row) { 1996 $this->list[$row->xref] = Family::getInstance($row->xref, $this->tree, $row->gedcom); 1997 } 1998 break; 1999 2000 default: 2001 throw new \DomainException('Invalid list name: ' . $listname); 2002 } 2003 2004 $filters = []; 2005 $filters2 = []; 2006 if (isset($attrs['filter1']) && count($this->list) > 0) { 2007 foreach ($attrs as $key => $value) { 2008 if (preg_match("/filter(\d)/", $key)) { 2009 $condition = $value; 2010 if (preg_match("/@(\w+)/", $condition, $match)) { 2011 $id = $match[1]; 2012 $value = "''"; 2013 if ($id == 'ID') { 2014 if (preg_match('/0 @(.+)@/', $this->gedrec, $match)) { 2015 $value = "'" . $match[1] . "'"; 2016 } 2017 } elseif ($id == 'fact') { 2018 $value = "'" . $this->fact . "'"; 2019 } elseif ($id == 'desc') { 2020 $value = "'" . $this->desc . "'"; 2021 } else { 2022 if (preg_match("/\d $id (.+)/", $this->gedrec, $match)) { 2023 $value = "'" . str_replace('@', '', trim($match[1])) . "'"; 2024 } 2025 } 2026 $condition = preg_replace("/@$id/", $value, $condition); 2027 } 2028 //-- handle regular expressions 2029 if (preg_match("/([A-Z:]+)\s*([^\s]+)\s*(.+)/", $condition, $match)) { 2030 $tag = trim($match[1]); 2031 $expr = trim($match[2]); 2032 $val = trim($match[3]); 2033 if (preg_match("/\\$(\w+)/", $val, $match)) { 2034 $val = $this->vars[$match[1]]['id']; 2035 $val = trim($val); 2036 } 2037 if ($val) { 2038 $searchstr = ''; 2039 $tags = explode(':', $tag); 2040 //-- only limit to a level number if we are specifically looking at a level 2041 if (count($tags) > 1) { 2042 $level = 1; 2043 foreach ($tags as $t) { 2044 if (!empty($searchstr)) { 2045 $searchstr .= "[^\n]*(\n[2-9][^\n]*)*\n"; 2046 } 2047 //-- search for both EMAIL and _EMAIL... silly double gedcom standard 2048 if ($t == 'EMAIL' || $t == '_EMAIL') { 2049 $t = '_?EMAIL'; 2050 } 2051 $searchstr .= $level . ' ' . $t; 2052 $level++; 2053 } 2054 } else { 2055 if ($tag == 'EMAIL' || $tag == '_EMAIL') { 2056 $tag = '_?EMAIL'; 2057 } 2058 $t = $tag; 2059 $searchstr = '1 ' . $tag; 2060 } 2061 switch ($expr) { 2062 case 'CONTAINS': 2063 if ($t == 'PLAC') { 2064 $searchstr .= "[^\n]*[, ]*" . $val; 2065 } else { 2066 $searchstr .= "[^\n]*" . $val; 2067 } 2068 $filters[] = $searchstr; 2069 break; 2070 default: 2071 $filters2[] = [ 2072 'tag' => $tag, 2073 'expr' => $expr, 2074 'val' => $val, 2075 ]; 2076 break; 2077 } 2078 } 2079 } 2080 } 2081 } 2082 } 2083 //-- apply other filters to the list that could not be added to the search string 2084 if ($filters) { 2085 foreach ($this->list as $key => $record) { 2086 foreach ($filters as $filter) { 2087 if (!preg_match('/' . $filter . '/i', $record->privatizeGedcom(Auth::accessLevel($this->tree)))) { 2088 unset($this->list[$key]); 2089 break; 2090 } 2091 } 2092 } 2093 } 2094 if ($filters2) { 2095 $mylist = []; 2096 foreach ($this->list as $indi) { 2097 $key = $indi->getXref(); 2098 $grec = $indi->privatizeGedcom(Auth::accessLevel($this->tree)); 2099 $keep = true; 2100 foreach ($filters2 as $filter) { 2101 if ($keep) { 2102 $tag = $filter['tag']; 2103 $expr = $filter['expr']; 2104 $val = $filter['val']; 2105 if ($val == "''") { 2106 $val = ''; 2107 } 2108 $tags = explode(':', $tag); 2109 $t = end($tags); 2110 $v = $this->getGedcomValue($tag, 1, $grec); 2111 //-- check for EMAIL and _EMAIL (silly double gedcom standard :P) 2112 if ($t == 'EMAIL' && empty($v)) { 2113 $tag = str_replace('EMAIL', '_EMAIL', $tag); 2114 $tags = explode(':', $tag); 2115 $t = end($tags); 2116 $v = Functions::getSubRecord(1, $tag, $grec); 2117 } 2118 2119 switch ($expr) { 2120 case 'GTE': 2121 if ($t == 'DATE') { 2122 $date1 = new Date($v); 2123 $date2 = new Date($val); 2124 $keep = (Date::compare($date1, $date2) >= 0); 2125 } elseif ($val >= $v) { 2126 $keep = true; 2127 } 2128 break; 2129 case 'LTE': 2130 if ($t == 'DATE') { 2131 $date1 = new Date($v); 2132 $date2 = new Date($val); 2133 $keep = (Date::compare($date1, $date2) <= 0); 2134 } elseif ($val >= $v) { 2135 $keep = true; 2136 } 2137 break; 2138 default: 2139 if ($v == $val) { 2140 $keep = true; 2141 } else { 2142 $keep = false; 2143 } 2144 break; 2145 } 2146 } 2147 } 2148 if ($keep) { 2149 $mylist[$key] = $indi; 2150 } 2151 } 2152 $this->list = $mylist; 2153 } 2154 2155 switch ($sortby) { 2156 case 'NAME': 2157 uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare'); 2158 break; 2159 case 'CHAN': 2160 uasort($this->list, function (GedcomRecord $x, GedcomRecord $y) { 2161 return $y->lastChangeTimestamp(true) - $x->lastChangeTimestamp(true); 2162 }); 2163 break; 2164 case 'BIRT:DATE': 2165 uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate'); 2166 break; 2167 case 'DEAT:DATE': 2168 uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate'); 2169 break; 2170 case 'MARR:DATE': 2171 uasort($this->list, '\Fisharebest\Webtrees\Family::compareMarrDate'); 2172 break; 2173 default: 2174 // unsorted or already sorted by SQL 2175 break; 2176 } 2177 2178 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 2179 $this->repeat_bytes = xml_get_current_line_number($this->parser) + 1; 2180 } 2181 2182 /** 2183 * XML <List> 2184 */ 2185 private function listEndHandler() 2186 { 2187 $this->process_repeats--; 2188 if ($this->process_repeats > 0) { 2189 return; 2190 } 2191 2192 // Check if there is any list 2193 if (count($this->list) > 0) { 2194 $lineoffset = 0; 2195 foreach ($this->repeats_stack as $rep) { 2196 $lineoffset += $rep[1]; 2197 } 2198 //-- read the xml from the file 2199 $lines = file($this->report); 2200 while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<List') === false) && (($lineoffset + $this->repeat_bytes) > 0)) { 2201 $lineoffset--; 2202 } 2203 $lineoffset++; 2204 $reportxml = "<tempdoc>\n"; 2205 $line_nr = $lineoffset + $this->repeat_bytes; 2206 // List Level counter 2207 $count = 1; 2208 while (0 < $count) { 2209 if (strpos($lines[$line_nr], '<List') !== false) { 2210 $count++; 2211 } elseif (strpos($lines[$line_nr], '</List') !== false) { 2212 $count--; 2213 } 2214 if (0 < $count) { 2215 $reportxml .= $lines[$line_nr]; 2216 } 2217 $line_nr++; 2218 } 2219 // No need to drag this 2220 unset($lines); 2221 $reportxml .= '</tempdoc>'; 2222 // Save original values 2223 $this->parser_stack[] = $this->parser; 2224 $oldgedrec = $this->gedrec; 2225 2226 $this->list_total = count($this->list); 2227 $this->list_private = 0; 2228 foreach ($this->list as $record) { 2229 if ($record->canShow()) { 2230 $this->gedrec = $record->privatizeGedcom(Auth::accessLevel($record->getTree())); 2231 //-- start the sax parser 2232 $repeat_parser = xml_parser_create(); 2233 $this->parser = $repeat_parser; 2234 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 2235 xml_set_element_handler($repeat_parser, [ 2236 $this, 2237 'startElement', 2238 ], [ 2239 $this, 2240 'endElement', 2241 ]); 2242 xml_set_character_data_handler($repeat_parser, [ 2243 $this, 2244 'characterData', 2245 ]); 2246 if (!xml_parse($repeat_parser, $reportxml, true)) { 2247 throw new \DomainException(sprintf( 2248 'ListEHandler XML error: %s at line %d', 2249 xml_error_string(xml_get_error_code($repeat_parser)), 2250 xml_get_current_line_number($repeat_parser) 2251 )); 2252 } 2253 xml_parser_free($repeat_parser); 2254 } else { 2255 $this->list_private++; 2256 } 2257 } 2258 $this->list = []; 2259 $this->parser = array_pop($this->parser_stack); 2260 $this->gedrec = $oldgedrec; 2261 } 2262 list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack); 2263 } 2264 2265 /** 2266 * XML <ListTotal> element handler 2267 * Prints the total number of records in a list 2268 * The total number is collected from 2269 * List and Relatives 2270 */ 2271 private function listTotalStartHandler() 2272 { 2273 if ($this->list_private == 0) { 2274 $this->current_element->addText($this->list_total); 2275 } else { 2276 $this->current_element->addText(($this->list_total - $this->list_private) . ' / ' . $this->list_total); 2277 } 2278 } 2279 2280 /** 2281 * XML <Relatives> 2282 * 2283 * @param array $attrs an array of key value pairs for the attributes 2284 */ 2285 private function relativesStartHandler($attrs) 2286 { 2287 $this->process_repeats++; 2288 if ($this->process_repeats > 1) { 2289 return; 2290 } 2291 2292 $sortby = 'NAME'; 2293 if (isset($attrs['sortby'])) { 2294 $sortby = $attrs['sortby']; 2295 } 2296 $match = []; 2297 if (preg_match("/\\$(\w+)/", $sortby, $match)) { 2298 $sortby = $this->vars[$match[1]]['id']; 2299 $sortby = trim($sortby); 2300 } 2301 2302 $maxgen = -1; 2303 if (isset($attrs['maxgen'])) { 2304 $maxgen = $attrs['maxgen']; 2305 } 2306 if ($maxgen == '*') { 2307 $maxgen = -1; 2308 } 2309 2310 $group = 'child-family'; 2311 if (isset($attrs['group'])) { 2312 $group = $attrs['group']; 2313 } 2314 if (preg_match("/\\$(\w+)/", $group, $match)) { 2315 $group = $this->vars[$match[1]]['id']; 2316 $group = trim($group); 2317 } 2318 2319 $id = ''; 2320 if (isset($attrs['id'])) { 2321 $id = $attrs['id']; 2322 } 2323 if (preg_match("/\\$(\w+)/", $id, $match)) { 2324 $id = $this->vars[$match[1]]['id']; 2325 $id = trim($id); 2326 } 2327 2328 $this->list = []; 2329 $person = Individual::getInstance($id, $this->tree); 2330 if (!empty($person)) { 2331 $this->list[$id] = $person; 2332 switch ($group) { 2333 case 'child-family': 2334 foreach ($person->getChildFamilies() as $family) { 2335 $husband = $family->getHusband(); 2336 $wife = $family->getWife(); 2337 if (!empty($husband)) { 2338 $this->list[$husband->getXref()] = $husband; 2339 } 2340 if (!empty($wife)) { 2341 $this->list[$wife->getXref()] = $wife; 2342 } 2343 $children = $family->getChildren(); 2344 foreach ($children as $child) { 2345 if (!empty($child)) { 2346 $this->list[$child->getXref()] = $child; 2347 } 2348 } 2349 } 2350 break; 2351 case 'spouse-family': 2352 foreach ($person->getSpouseFamilies() as $family) { 2353 $husband = $family->getHusband(); 2354 $wife = $family->getWife(); 2355 if (!empty($husband)) { 2356 $this->list[$husband->getXref()] = $husband; 2357 } 2358 if (!empty($wife)) { 2359 $this->list[$wife->getXref()] = $wife; 2360 } 2361 $children = $family->getChildren(); 2362 foreach ($children as $child) { 2363 if (!empty($child)) { 2364 $this->list[$child->getXref()] = $child; 2365 } 2366 } 2367 } 2368 break; 2369 case 'direct-ancestors': 2370 $this->addAncestors($this->list, $id, false, $maxgen); 2371 break; 2372 case 'ancestors': 2373 $this->addAncestors($this->list, $id, true, $maxgen); 2374 break; 2375 case 'descendants': 2376 $this->list[$id]->generation = 1; 2377 $this->addDescendancy($this->list, $id, false, $maxgen); 2378 break; 2379 case 'all': 2380 $this->addAncestors($this->list, $id, true, $maxgen); 2381 $this->addDescendancy($this->list, $id, true, $maxgen); 2382 break; 2383 } 2384 } 2385 2386 switch ($sortby) { 2387 case 'NAME': 2388 uasort($this->list, '\Fisharebest\Webtrees\GedcomRecord::compare'); 2389 break; 2390 case 'BIRT:DATE': 2391 uasort($this->list, '\Fisharebest\Webtrees\Individual::compareBirthDate'); 2392 break; 2393 case 'DEAT:DATE': 2394 uasort($this->list, '\Fisharebest\Webtrees\Individual::compareDeathDate'); 2395 break; 2396 case 'generation': 2397 $newarray = []; 2398 reset($this->list); 2399 $genCounter = 1; 2400 while (count($newarray) < count($this->list)) { 2401 foreach ($this->list as $key => $value) { 2402 $this->generation = $value->generation; 2403 if ($this->generation == $genCounter) { 2404 $newarray[$key] = new stdClass(); 2405 $newarray[$key]->generation = $this->generation; 2406 } 2407 } 2408 $genCounter++; 2409 } 2410 $this->list = $newarray; 2411 break; 2412 default: 2413 // unsorted 2414 break; 2415 } 2416 $this->repeats_stack[] = [$this->repeats, $this->repeat_bytes]; 2417 $this->repeat_bytes = xml_get_current_line_number($this->parser) + 1; 2418 } 2419 2420 /** 2421 * XML </ Relatives> 2422 */ 2423 private function relativesEndHandler() 2424 { 2425 $this->process_repeats--; 2426 if ($this->process_repeats > 0) { 2427 return; 2428 } 2429 2430 // Check if there is any relatives 2431 if (count($this->list) > 0) { 2432 $lineoffset = 0; 2433 foreach ($this->repeats_stack as $rep) { 2434 $lineoffset += $rep[1]; 2435 } 2436 //-- read the xml from the file 2437 $lines = file($this->report); 2438 while ((strpos($lines[$lineoffset + $this->repeat_bytes], '<Relatives') === false) && (($lineoffset + $this->repeat_bytes) > 0)) { 2439 $lineoffset--; 2440 } 2441 $lineoffset++; 2442 $reportxml = "<tempdoc>\n"; 2443 $line_nr = $lineoffset + $this->repeat_bytes; 2444 // Relatives Level counter 2445 $count = 1; 2446 while (0 < $count) { 2447 if (strpos($lines[$line_nr], '<Relatives') !== false) { 2448 $count++; 2449 } elseif (strpos($lines[$line_nr], '</Relatives') !== false) { 2450 $count--; 2451 } 2452 if (0 < $count) { 2453 $reportxml .= $lines[$line_nr]; 2454 } 2455 $line_nr++; 2456 } 2457 // No need to drag this 2458 unset($lines); 2459 $reportxml .= "</tempdoc>\n"; 2460 // Save original values 2461 $this->parser_stack[] = $this->parser; 2462 $oldgedrec = $this->gedrec; 2463 2464 $this->list_total = count($this->list); 2465 $this->list_private = 0; 2466 foreach ($this->list as $key => $value) { 2467 if (isset($value->generation)) { 2468 $this->generation = $value->generation; 2469 } 2470 $tmp = GedcomRecord::getInstance($key, $this->tree); 2471 $this->gedrec = $tmp->privatizeGedcom(Auth::accessLevel($this->tree)); 2472 2473 $repeat_parser = xml_parser_create(); 2474 $this->parser = $repeat_parser; 2475 xml_parser_set_option($repeat_parser, XML_OPTION_CASE_FOLDING, false); 2476 xml_set_element_handler($repeat_parser, [ 2477 $this, 2478 'startElement', 2479 ], [ 2480 $this, 2481 'endElement', 2482 ]); 2483 xml_set_character_data_handler($repeat_parser, [ 2484 $this, 2485 'characterData', 2486 ]); 2487 2488 if (!xml_parse($repeat_parser, $reportxml, true)) { 2489 throw new \DomainException(sprintf('RelativesEHandler XML error: %s at line %d', xml_error_string(xml_get_error_code($repeat_parser)), xml_get_current_line_number($repeat_parser))); 2490 } 2491 xml_parser_free($repeat_parser); 2492 } 2493 // Clean up the list array 2494 $this->list = []; 2495 $this->parser = array_pop($this->parser_stack); 2496 $this->gedrec = $oldgedrec; 2497 } 2498 list($this->repeats, $this->repeat_bytes) = array_pop($this->repeats_stack); 2499 } 2500 2501 /** 2502 * XML <Generation /> element handler 2503 * Prints the number of generations 2504 */ 2505 private function generationStartHandler() 2506 { 2507 $this->current_element->addText($this->generation); 2508 } 2509 2510 /** 2511 * XML <NewPage /> element handler 2512 * Has to be placed in an element (header, pageheader, body or footer) 2513 */ 2514 private function newPageStartHandler() 2515 { 2516 $temp = 'addpage'; 2517 $this->wt_report->addElement($temp); 2518 } 2519 2520 /** 2521 * XML <html> 2522 * 2523 * @param string $tag HTML tag name 2524 * @param array[] $attrs an array of key value pairs for the attributes 2525 */ 2526 private function htmlStartHandler($tag, $attrs) 2527 { 2528 if ($tag === 'tempdoc') { 2529 return; 2530 } 2531 $this->wt_report_stack[] = $this->wt_report; 2532 $this->wt_report = $this->report_root->createHTML($tag, $attrs); 2533 $this->current_element = $this->wt_report; 2534 2535 $this->print_data_stack[] = $this->print_data; 2536 $this->print_data = true; 2537 } 2538 2539 /** 2540 * XML </html> 2541 * 2542 * @param string $tag 2543 */ 2544 private function htmlEndHandler($tag) 2545 { 2546 if ($tag === 'tempdoc') { 2547 return; 2548 } 2549 2550 $this->print_data = array_pop($this->print_data_stack); 2551 $this->current_element = $this->wt_report; 2552 $this->wt_report = array_pop($this->wt_report_stack); 2553 if ($this->wt_report !== null) { 2554 $this->wt_report->addElement($this->current_element); 2555 } else { 2556 $this->wt_report = $this->current_element; 2557 } 2558 } 2559 2560 /** 2561 * Handle <Input> 2562 */ 2563 private function inputStartHandler() 2564 { 2565 // Dummy function, to prevent the default HtmlStartHandler() being called 2566 } 2567 2568 /** 2569 * Handle </Input> 2570 */ 2571 private function inputEndHandler() 2572 { 2573 // Dummy function, to prevent the default HtmlEndHandler() being called 2574 } 2575 2576 /** 2577 * Handle <Report> 2578 */ 2579 private function reportStartHandler() 2580 { 2581 // Dummy function, to prevent the default HtmlStartHandler() being called 2582 } 2583 2584 /** 2585 * Handle </Report> 2586 */ 2587 private function reportEndHandler() 2588 { 2589 // Dummy function, to prevent the default HtmlEndHandler() being called 2590 } 2591 2592 /** 2593 * XML </titleEndHandler> 2594 */ 2595 private function titleEndHandler() 2596 { 2597 $this->report_root->addTitle($this->text); 2598 } 2599 2600 /** 2601 * XML </descriptionEndHandler> 2602 */ 2603 private function descriptionEndHandler() 2604 { 2605 $this->report_root->addDescription($this->text); 2606 } 2607 2608 /** 2609 * Create a list of all descendants. 2610 * 2611 * @param string[] $list 2612 * @param string $pid 2613 * @param bool $parents 2614 * @param int $generations 2615 */ 2616 private function addDescendancy(&$list, $pid, $parents = false, $generations = -1) 2617 { 2618 $person = Individual::getInstance($pid, $this->tree); 2619 if ($person === null) { 2620 return; 2621 } 2622 if (!isset($list[$pid])) { 2623 $list[$pid] = $person; 2624 } 2625 if (!isset($list[$pid]->generation)) { 2626 $list[$pid]->generation = 0; 2627 } 2628 foreach ($person->getSpouseFamilies() as $family) { 2629 if ($parents) { 2630 $husband = $family->getHusband(); 2631 $wife = $family->getWife(); 2632 if ($husband) { 2633 $list[$husband->getXref()] = $husband; 2634 if (isset($list[$pid]->generation)) { 2635 $list[$husband->getXref()]->generation = $list[$pid]->generation - 1; 2636 } else { 2637 $list[$husband->getXref()]->generation = 1; 2638 } 2639 } 2640 if ($wife) { 2641 $list[$wife->getXref()] = $wife; 2642 if (isset($list[$pid]->generation)) { 2643 $list[$wife->getXref()]->generation = $list[$pid]->generation - 1; 2644 } else { 2645 $list[$wife->getXref()]->generation = 1; 2646 } 2647 } 2648 } 2649 $children = $family->getChildren(); 2650 foreach ($children as $child) { 2651 if ($child) { 2652 $list[$child->getXref()] = $child; 2653 if (isset($list[$pid]->generation)) { 2654 $list[$child->getXref()]->generation = $list[$pid]->generation + 1; 2655 } else { 2656 $list[$child->getXref()]->generation = 2; 2657 } 2658 } 2659 } 2660 if ($generations == -1 || $list[$pid]->generation + 1 < $generations) { 2661 foreach ($children as $child) { 2662 $this->addDescendancy($list, $child->getXref(), $parents, $generations); // recurse on the childs family 2663 } 2664 } 2665 } 2666 } 2667 2668 /** 2669 * Create a list of all ancestors. 2670 * 2671 * @param string[] $list 2672 * @param string $pid 2673 * @param bool $children 2674 * @param int $generations 2675 */ 2676 private function addAncestors(&$list, $pid, $children = false, $generations = -1) 2677 { 2678 $genlist = [$pid]; 2679 $list[$pid]->generation = 1; 2680 while (count($genlist) > 0) { 2681 $id = array_shift($genlist); 2682 if (strpos($id, 'empty') === 0) { 2683 continue; // id can be something like “empty7” 2684 } 2685 $person = Individual::getInstance($id, $this->tree); 2686 foreach ($person->getChildFamilies() as $family) { 2687 $husband = $family->getHusband(); 2688 $wife = $family->getWife(); 2689 if ($husband) { 2690 $list[$husband->getXref()] = $husband; 2691 $list[$husband->getXref()]->generation = $list[$id]->generation + 1; 2692 } 2693 if ($wife) { 2694 $list[$wife->getXref()] = $wife; 2695 $list[$wife->getXref()]->generation = $list[$id]->generation + 1; 2696 } 2697 if ($generations == -1 || $list[$id]->generation + 1 < $generations) { 2698 if ($husband) { 2699 $genlist[] = $husband->getXref(); 2700 } 2701 if ($wife) { 2702 $genlist[] = $wife->getXref(); 2703 } 2704 } 2705 if ($children) { 2706 foreach ($family->getChildren() as $child) { 2707 $list[$child->getXref()] = $child; 2708 if (isset($list[$id]->generation)) { 2709 $list[$child->getXref()]->generation = $list[$id]->generation; 2710 } else { 2711 $list[$child->getXref()]->generation = 1; 2712 } 2713 } 2714 } 2715 } 2716 } 2717 } 2718 2719 /** 2720 * get gedcom tag value 2721 * 2722 * @param string $tag The tag to find, use : to delineate subtags 2723 * @param int $level The gedcom line level of the first tag to find, setting level to 0 will cause it to use 1+ the level of the incoming record 2724 * @param string $gedrec The gedcom record to get the value from 2725 * 2726 * @return string the value of a gedcom tag from the given gedcom record 2727 */ 2728 private function getGedcomValue($tag, $level, $gedrec): string 2729 { 2730 if (empty($gedrec)) { 2731 return ''; 2732 } 2733 $tags = explode(':', $tag); 2734 $origlevel = $level; 2735 if ($level == 0) { 2736 $level = $gedrec[0] + 1; 2737 } 2738 2739 $subrec = $gedrec; 2740 foreach ($tags as $t) { 2741 $lastsubrec = $subrec; 2742 $subrec = Functions::getSubRecord($level, "$level $t", $subrec); 2743 if (empty($subrec) && $origlevel == 0) { 2744 $level--; 2745 $subrec = Functions::getSubRecord($level, "$level $t", $lastsubrec); 2746 } 2747 if (empty($subrec)) { 2748 if ($t == 'TITL') { 2749 $subrec = Functions::getSubRecord($level, "$level ABBR", $lastsubrec); 2750 if (!empty($subrec)) { 2751 $t = 'ABBR'; 2752 } 2753 } 2754 if (empty($subrec)) { 2755 if ($level > 0) { 2756 $level--; 2757 } 2758 $subrec = Functions::getSubRecord($level, "@ $t", $gedrec); 2759 if (empty($subrec)) { 2760 return ''; 2761 } 2762 } 2763 } 2764 $level++; 2765 } 2766 $level--; 2767 $ct = preg_match("/$level $t(.*)/", $subrec, $match); 2768 if ($ct == 0) { 2769 $ct = preg_match("/$level @.+@ (.+)/", $subrec, $match); 2770 } 2771 if ($ct == 0) { 2772 $ct = preg_match("/@ $t (.+)/", $subrec, $match); 2773 } 2774 if ($ct > 0) { 2775 $value = trim($match[1]); 2776 if ($t == 'NOTE' && preg_match('/^@(.+)@$/', $value, $match)) { 2777 $note = Note::getInstance($match[1], $this->tree); 2778 if ($note) { 2779 $value = $note->getNote(); 2780 } else { 2781 //-- set the value to the id without the @ 2782 $value = $match[1]; 2783 } 2784 } 2785 if ($level != 0 || $t != 'NOTE') { 2786 $value .= Functions::getCont($level + 1, $subrec); 2787 } 2788 2789 return $value; 2790 } 2791 2792 return ''; 2793 } 2794 2795 /** 2796 * Replace variable identifiers with their values. 2797 * 2798 * @param string $expression An expression such as "$foo == 123" 2799 * @param bool $quote Whether to add quotation marks 2800 * 2801 * @return string 2802 */ 2803 private function substituteVars($expression, $quote): string 2804 { 2805 return preg_replace_callback( 2806 '/\$(\w+)/', 2807 function ($matches) use ($quote) { 2808 if (isset($this->vars[$matches[1]]['id'])) { 2809 if ($quote) { 2810 return "'" . addcslashes($this->vars[$matches[1]]['id'], "'") . "'"; 2811 } else { 2812 return $this->vars[$matches[1]]['id']; 2813 } 2814 } else { 2815 Log::addErrorLog(sprintf('Undefined variable $%s in report', $matches[1])); 2816 2817 return '$' . $matches[1]; 2818 } 2819 }, 2820 $expression 2821 ); 2822 } 2823} 2824