forked from PHPOffice/PHPWord
-
Notifications
You must be signed in to change notification settings - Fork 7
/
TemplateProcessor.php
1099 lines (988 loc) · 36.8 KB
/
TemplateProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
* @copyright 2010-2017 PHPWord contributors
* @license http:https://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Escaper\RegExp;
use PhpOffice\PhpWord\Escaper\Xml;
use PhpOffice\PhpWord\Exception\CopyFileException;
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Shared\ZipArchive;
use Zend\Stdlib\StringUtils;
class TemplateProcessor
{
const MAXIMUM_REPLACEMENTS_DEFAULT = -1;
const SEARCH_LEFT = -1;
const SEARCH_RIGHT = 1;
const SEARCH_AROUND = 0;
/**
* Enable/disable setValue('key') becoming setValue('${key}') automatically.
* Call it like: TemplateProcessor::$ensureMacroCompletion = false;
*
* @var bool
*/
public static $ensureMacroCompletion = true;
/**
* ZipArchive object.
*
* @var mixed
*/
protected $zipClass;
/**
* @var string Temporary document filename (with path)
*/
protected $tempDocumentFilename;
/**
* Content of main document part (in XML format) of the temporary document
*
* @var string
*/
protected $tempDocumentMainPart;
/**
* Content of headers (in XML format) of the temporary document
*
* @var string[]
*/
protected $tempDocumentHeaders = array();
/**
* Content of footers (in XML format) of the temporary document
*
* @var string[]
*/
protected $tempDocumentFooters = array();
/**
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception
*
* @param string $documentTemplate The fully qualified template filename
*
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
*/
public function __construct($documentTemplate)
{
// Temporary document filename initialization
$this->tempDocumentFilename = tempnam(Settings::getTempDir(), 'PhpWord');
if (false === $this->tempDocumentFilename) {
throw new CreateTemporaryFileException();
}
// Template file cloning
if (false === copy($documentTemplate, $this->tempDocumentFilename)) {
throw new CopyFileException($documentTemplate, $this->tempDocumentFilename);
}
// Temporary document content extraction
$this->zipClass = new ZipArchive();
$this->zipClass->open($this->tempDocumentFilename);
$index = 1;
while (false !== $this->zipClass->locateName($this->getHeaderName($index))) {
$this->tempDocumentHeaders[$index] = $this->fixBrokenMacros(
$this->zipClass->getFromName($this->getHeaderName($index))
);
$index++;
}
$index = 1;
while (false !== $this->zipClass->locateName($this->getFooterName($index))) {
$this->tempDocumentFooters[$index] = $this->fixBrokenMacros(
$this->zipClass->getFromName($this->getFooterName($index))
);
$index++;
}
$this->tempDocumentMainPart = $this->fixBrokenMacros($this->zipClass->getFromName($this->getMainPartName()));
}
/**
* @param string $xml
* @param \XSLTProcessor $xsltProcessor
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*
* @return string
*/
protected function transformSingleXml($xml, $xsltProcessor)
{
$domDocument = new \DOMDocument();
if (false === $domDocument->loadXML($xml)) {
throw new Exception('Could not load the given XML document.');
}
$transformedXml = $xsltProcessor->transformToXml($domDocument);
if (false === $transformedXml) {
throw new Exception('Could not transform the given XML document.');
}
return $transformedXml;
}
/**
* @param mixed $xml
* @param \XSLTProcessor $xsltProcessor
*
* @return mixed
*/
protected function transformXml($xml, $xsltProcessor)
{
if (is_array($xml)) {
foreach ($xml as &$item) {
$item = $this->transformSingleXml($item, $xsltProcessor);
}
return (array) $xml;
}
$xml = $this->transformSingleXml($xml, $xsltProcessor);
return (string) $xml;
}
/**
* Applies XSL style sheet to template's parts.
*
* Note: since the method doesn't make any guess on logic of the provided XSL style sheet,
* make sure that output is correctly escaped. Otherwise you may get broken document.
*
* @param \DOMDocument $xslDomDocument
* @param array $xslOptions
* @param string $xslOptionsUri
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function applyXslStyleSheet($xslDomDocument, $xslOptions = array(), $xslOptionsUri = '')
{
$xsltProcessor = new \XSLTProcessor();
$xsltProcessor->importStylesheet($xslDomDocument);
if (false === $xsltProcessor->setParameter($xslOptionsUri, $xslOptions)) {
throw new Exception('Could not set values for the given XSL style sheet parameters.');
}
$this->tempDocumentHeaders = (array) $this->transformXml($this->tempDocumentHeaders, $xsltProcessor);
$this->tempDocumentMainPart = (string) $this->transformXml($this->tempDocumentMainPart, $xsltProcessor);
$this->tempDocumentFooters = (array) $this->transformXml($this->tempDocumentFooters, $xsltProcessor);
}
/**
* @param string $macro If written as VALUE it will return ${VALUE} if static::$ensureMacroCompletion
* @param bool $closing False by default, if set to true, will add ${/ } around the macro
*
* @return string
*/
protected static function ensureMacroCompleted($macro, $closing = false)
{
if (static::$ensureMacroCompletion && substr($macro, 0, 2) !== '${' && substr($macro, -1) !== '}') {
$macro = '${' . ($closing ? '/' : '') . $macro . '}';
}
return $macro;
}
/**
* @param string $subject
*
* @return string
*/
protected static function ensureUtf8Encoded($subject)
{
if (!StringUtils::isValidUtf8($subject)) {
$subject = utf8_encode($subject);
}
return $subject;
}
/**
* @param mixed $search macro name you want to replace (or an array of these)
* @param mixed $replace replace string (or an array of these)
* @param int $limit How many times it will have to replace the same variable all over the document
*/
public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
{
if (is_array($search)) {
foreach ($search as &$item) {
$item = static::ensureMacroCompleted($item);
}
unset($item);
} else {
$search = static::ensureMacroCompleted($search);
}
if (is_array($replace)) {
foreach ($replace as &$item) {
$item = static::ensureUtf8Encoded($item);
}
unset($item);
} else {
$replace = static::ensureUtf8Encoded($replace);
}
if (Settings::isOutputEscapingEnabled()) {
$xmlEscaper = new Xml();
$replace = $xmlEscaper->escape($replace);
}
$this->tempDocumentHeaders = (array) $this->setValueForPart(
$search,
$replace,
(array) $this->tempDocumentHeaders,
$limit
);
$this->tempDocumentMainPart = (string) $this->setValueForPart(
$search,
$replace,
(string) $this->tempDocumentMainPart,
$limit
);
$this->tempDocumentFooters = (array) $this->setValueForPart(
$search,
$replace,
(array) $this->tempDocumentFooters,
$limit
);
}
/**
* Replaces a closed block with text
*
* @param string $blockname The blockname without '${}'. Your macro must end with slash, i.e.: ${value/}
* @param mixed $replace Array or the text can be multiline (contain \n); It will then cloneBlock()
* @param int $limit
*/
public function setBlock($blockname, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
{
if (is_string($replace) && preg_match('~\R~u', $replace)) {
$replace = preg_split('~\R~u', $replace);
}
if (is_array($replace)) {
$this->processBlock($blockname, count($replace), true, false);
foreach ($replace as $oneVal) {
$this->setValue($blockname, $oneVal, 1);
}
} else {
$this->setValue($blockname, $replace, $limit);
}
}
/**
* Expose zip class
*
* To replace an image: $templateProcessor->zip()->AddFromString("word/media/image1.jpg", file_get_contents($file));
* (note that to add an image you also need to add some xml in the document, and a relation from Id to zip-filename)
* To read a file: $templateProcessor->zip()->getFromName("word/media/image1.jpg");
*
* @return object
*/
public function zip()
{
return $this->zipClass;
}
/**
* If $throwException is true, it throws an exception, else it returns $elseReturn
*
* @param string $exceptionText
* @param bool $throwException
* @param mixed $elseReturn
*
* @return mixed
*/
private function failGraciously($exceptionText, $throwException, $elseReturn)
{
if ($throwException) {
throw new Exception($exceptionText);
}
return $elseReturn;
}
/**
* Returns array of all variables in template.
*
* @return string[]
*/
public function getVariables()
{
$variables = $this->getVariablesForPart($this->tempDocumentMainPart);
foreach ($this->tempDocumentHeaders as $headerXML) {
$variables = array_merge($variables, $this->getVariablesForPart($headerXML));
}
foreach ($this->tempDocumentFooters as $footerXML) {
$variables = array_merge($variables, $this->getVariablesForPart($footerXML));
}
return array_unique($variables);
}
/**
* Clone a string and enumerate ( i.e. ${macro#1} )
*
* @param string $text Must be a variable as we use references for speed
* @param int $numberOfClones How many times $text needs to be duplicated
* @param bool $incrementVariables If true, the macro's inside the string get numerated
*
* @return string
*/
protected static function cloneSlice(&$text, $numberOfClones = 1, $incrementVariables = true)
{
$result = '';
for ($i = 1; $i <= $numberOfClones; $i++) {
if ($incrementVariables) {
$result .= preg_replace('/\$\{(.*?)(\/?)\}/', '\${\\1#' . $i . '\\2}', $text);
} else {
$result .= $text;
}
}
return $result;
}
/**
* Process a table row in a template document.
*
* @param string $search
* @param int $numberOfClones
* @param mixed $replace (true to clone, or a string to replace)
* @param bool $incrementVariables
* @param bool $throwException
*
* @throws \PhpOffice\PhpWord\Exception\Exception
* @return string|false Returns the row cloned or false if the $search macro is not found
*/
private function processRow(
$search,
$numberOfClones = 1,
$replace = true,
$incrementVariables = true,
$throwException = false
) {
return $this->processSegment(
static::ensureMacroCompleted($search),
'w:tr',
0,
$numberOfClones,
'MainPart',
function (&$xmlSegment, &$segmentStart, &$segmentEnd, &$part) use (&$replace) {
if (strpos($xmlSegment, '<w:vMerge w:val="restart"')) {
$extraRowEnd = $segmentEnd;
while (true) {
$extraRowStart = $extraRowEnd + 1;
$extraRowEnd = strpos($part, '</w:tr>', $extraRowStart);
if (!$extraRowEnd) {
break;
}
$extraRowEnd += strlen('</w:tr>');
// If tmpXmlRow doesn't contain continue, this row is no longer part of the spanned row.
$tmpXmlRow = substr($part, $extraRowStart, ($extraRowEnd - $extraRowStart));
if (!preg_match('#<w:vMerge ?/>#', $tmpXmlRow)
&& !preg_match('#<w:vMerge w:val="continue" ?/>#', $tmpXmlRow)
) {
break;
}
// This row was a spanned row, update $segmentEnd and search for the next row.
$segmentEnd = $extraRowEnd;
}
$xmlSegment = substr($part, $segmentStart, ($segmentEnd - $segmentStart));
}
return $replace;
},
$incrementVariables,
$throwException
);
}
/**
* Clone a table row in a template document.
*
* @param string $search
* @param int $numberOfClones
* @param bool $incrementVariables
* @param bool $throwException
*
* @throws \PhpOffice\PhpWord\Exception\Exception
* @return mixed Returns true if row cloned succesfully or or false if the $search macro is not found
*/
public function cloneRow(
$search,
$numberOfClones = 1,
$incrementVariables = true,
$throwException = false
) {
return $this->processRow($search, $numberOfClones, true, $incrementVariables, $throwException);
}
/**
* Get a row. (first block found)
*
* @param string $search
* @param bool $throwException
*
* @return string|null
*/
public function getRow($search, $throwException = false)
{
return $this->processRow($search, 0, false, false, $throwException);
}
/**
* Replace a row.
*
* @param string $search a macro name in a table row
* @param string $replacement The replacement <w:tr> xml string. Be careful and keep the xml uncorrupted.
* @param bool $throwException false by default (it then returns false or null on errors)
*
* @return mixed true (replaced), false ($search not found) or null (no tags found around $search)
*/
public function replaceRow($search, $replacement = '', $throwException = false)
{
return $this->processRow($search, 1, (string) $replacement, false, $throwException);
}
/**
* Delete a row containing the given variable
*
* @param string $search
*
* @return bool
*/
public function deleteRow($search)
{
return $this->processRow($search, 0, '', false, false);
}
/**
* process a block.
*
* @param string $blockname The blockname without '${}'
* @param int $clones
* @param mixed $replace
* @param bool $incrementVariables
* @param bool $throwException
*
* @throws \PhpOffice\PhpWord\Exception\Exception
* @return mixed The cloned string if successful, false ($blockname not found) or Null (no paragraph found)
*/
private function processBlock(
$blockname,
$clones = 1,
$replace = true,
$incrementVariables = true,
$throwException = false
) {
$startSearch = static::ensureMacroCompleted($blockname);
$endSearch = static::ensureMacroCompleted($blockname, true);
if (substr($blockname, -1) == '/') { // singleton/closed block
return $this->processSegment(
$startSearch,
'w:p',
0,
$clones,
'MainPart',
$replace,
$incrementVariables,
$throwException
);
}
$startTagPos = strpos($this->tempDocumentMainPart, $startSearch);
$endTagPos = strpos($this->tempDocumentMainPart, $endSearch, $startTagPos);
if (!$startTagPos || !$endTagPos) {
return $this->failGraciously(
"Can not find block '$blockname', template variable not found or variable contains markup.",
$throwException,
false
);
}
$startBlockStart = $this->findOpenTagLeft($this->tempDocumentMainPart, '<w:p>', $startTagPos, $throwException);
$startBlockEnd = $this->findCloseTagRight($this->tempDocumentMainPart, '</w:p>', $startTagPos);
if (!$startBlockStart || !$startBlockEnd) {
return $this->failGraciously(
"Can not find start paragraph around block '$blockname'",
$throwException,
null
);
}
$endBlockStart = $this->findOpenTagLeft($this->tempDocumentMainPart, '<w:p>', $endTagPos, $throwException);
$endBlockEnd = $this->findCloseTagRight($this->tempDocumentMainPart, '</w:p>', $endTagPos);
if (!$endBlockStart || !$endBlockEnd) {
return $this->failGraciously(
"Can not find end paragraph around block '$blockname'",
$throwException,
null
);
}
if ($startBlockEnd == $endBlockEnd) { // inline block
$startBlockStart = $startTagPos;
$startBlockEnd = $startTagPos + strlen($startSearch);
$endBlockStart = $endTagPos;
$endBlockEnd = $endTagPos + strlen($endSearch);
}
$xmlBlock = $this->getSlice($this->tempDocumentMainPart, $startBlockEnd, $endBlockStart);
if ($replace !== false) {
if ($replace === true) {
$replace = static::cloneSlice($xmlBlock, $clones, $incrementVariables);
}
$this->tempDocumentMainPart =
$this->getSlice($this->tempDocumentMainPart, 0, $startBlockStart)
. $replace
. $this->getSlice($this->tempDocumentMainPart, $endBlockEnd);
return true;
}
return $xmlBlock;
}
/**
* Clone a block.
*
* @param string $blockname The blockname without '${}', it will search for '${BLOCKNAME}' and '${/BLOCKNAME}
* @param int $clones How many times the block needs to be cloned
* @param bool $incrementVariables true by default (variables get appended #1, #2 inside the cloned blocks)
* @param bool $throwException false by default (it then returns false or null on errors)
*
* @throws \PhpOffice\PhpWord\Exception\Exception
* @return mixed True if successful, false ($blockname not found) or null (no paragraph found)
*/
public function cloneBlock(
$blockname,
$clones = 1,
$incrementVariables = true,
$throwException = false
) {
return $this->processBlock($blockname, $clones, true, $incrementVariables, $throwException);
}
/**
* Get a block. (first block found)
*
* @param string $blockname The blockname without '${}'
* @param bool $throwException false by default
*
* @return mixed a string when $blockname is found, false ($blockname not found) or null (no paragraph found)
*/
public function getBlock($blockname, $throwException = false)
{
return $this->processBlock($blockname, 0, false, false, $throwException);
}
/**
* Replace a block.
*
* @param string $blockname The name of the macro start and end (without the macro marker ${})
* @param string $replacement The replacement xml
* @param bool $throwException false by default
*
* @return mixed false-ish on no replacement, true-ish on replacement
*/
public function replaceBlock($blockname, $replacement = '', $throwException = false)
{
return $this->processBlock($blockname, 0, (string) $replacement, false, $throwException);
}
/**
* Delete a block of text.
*
* @param string $blockname
*
* @return mixed true-ish on block found and deleted, falseish on block not found
*/
public function deleteBlock($blockname)
{
return $this->replaceBlock($blockname, '', false);
}
/**
* process a segment.
*
* @param string $needle If this is a macro, you need to add the ${} around it yourself
* @param string $xmltag an xml tag without brackets, for example: w:p
* @param int $direction in which direction should be searched. -1 left, 1 right. Default 0: around
* @param int $clones How many times the segment needs to be cloned
* @param string $docPart 'MainPart' (default) 'Footers:1' (first footer) or 'Headers:1' (first header)
* @param mixed $replace true (default/cloneSegment) false(getSegment) string(replaceSegment) function(callback)
* @param bool $incrementVariables true by default (variables get appended #1, #2 inside the cloned blocks)
* @param bool $throwException false by default (it then returns false or null on errors)
*
* @return mixed The segment(getSegment), false (no $needle), null (no tags), true (clone/replace)
*/
public function processSegment(
$needle,
$xmltag,
$direction = self::SEARCH_AROUND,
$clones = 1,
$docPart = 'MainPart',
$replace = true,
$incrementVariables = true,
$throwException = false
) {
$docPart = preg_split('/:/', $docPart);
if (count($docPart) > 1) {
$part = &$this->{'tempDocument' . $docPart[0]}[$docPart[1]];
} else {
$part = &$this->{'tempDocument' . $docPart[0]};
}
$needlePos = strpos($part, $needle);
if ($needlePos === false) {
return $this->failGraciously(
"Can not find macro '$needle', text not found or text contains markup.",
$throwException,
false
);
}
$directionStart = $direction == self::SEARCH_RIGHT ? 'findOpenTagRight' : 'findOpenTagLeft';
$directionEnd = $direction == self::SEARCH_LEFT ? 'findCloseTagLeft' : 'findCloseTagRight';
$segmentStart = $this->{$directionStart}($part, "<$xmltag>", $needlePos, $throwException);
$segmentEnd = $this->{$directionEnd}($part, "</$xmltag>", $needlePos, $throwException);
if ($segmentStart >= $segmentEnd && $segmentEnd) {
if ($direction == self::SEARCH_RIGHT) {
$segmentEnd = $this->findCloseTagRight($part, "</$xmltag>", $segmentStart);
} else {
$segmentStart = $this->findOpenTagLeft($part, "<$xmltag>", $segmentEnd - 1, $throwException);
}
}
if (!$segmentStart || !$segmentEnd) {
return $this->failGraciously(
"Can not find <$xmltag> ($segmentStart,$segmentEnd) around segment '$needle'",
$throwException,
null
);
}
$xmlSegment = $this->getSlice($part, $segmentStart, $segmentEnd);
while (is_callable($replace)) {
$replace = $replace($xmlSegment, $segmentStart, $segmentEnd, $part);
}
if ($replace !== false) {
if ($replace === true) {
$replace = static::cloneSlice($xmlSegment, $clones, $incrementVariables);
}
$part =
$this->getSlice($part, 0, $segmentStart)
. $replace
. $this->getSlice($part, $segmentEnd);
return true;
}
return $xmlSegment;
}
/**
* Clone a segment.
*
* @param string $needle If this is a macro, you need to add the ${} around it yourself
* @param string $xmltag an xml tag without brackets, for example: w:p
* @param int $direction in which direction should be searched. -1 left, 1 right. Default 0: around
* @param int $clones How many times the segment needs to be cloned
* @param string $docPart 'MainPart' (default) 'Footers:1' (first footer) or 'Headers:1' (first header)
* @param bool $incrementVariables true by default (variables get appended #1, #2 inside the cloned blocks)
* @param bool $throwException false by default (it then returns false or null on errors)
*
* @return mixed Returns true when succesfully cloned, false (no $needle found), null (no tags found)
*/
public function cloneSegment(
$needle,
$xmltag,
$direction = self::SEARCH_AROUND,
$clones = 1,
$docPart = 'MainPart',
$incrementVariables = true,
$throwException = false
) {
return $this->processSegment(
$needle,
$xmltag,
$direction,
$clones,
$docPart,
true,
$incrementVariables,
$throwException
);
}
/**
* Get a segment. (first segment found)
*
* @param string $needle If this is a macro, you need to add the ${} around it yourself
* @param string $xmltag an xml tag without brackets, for example: w:p
* @param int $direction in which direction should be searched. -1 left, 1 right. Default 0: around
* @param string $docPart 'MainPart' (default) 'Footers:1' (first footer) or 'Headers:1' (first header)
* @param bool $throwException false by default (it then returns false or null on errors)
*
* @return mixed Segment String, false ($needle not found) or null (no tags found around $needle)
*/
public function getSegment($needle, $xmltag, $direction = 0, $docPart = 'MainPart', $throwException = false)
{
return $this->processSegment($needle, $xmltag, $direction, 0, $docPart, false, false, $throwException);
}
/**
* Replace a segment.
*
* @param string $needle If this is a macro, you need to add the ${} around it yourself
* @param string $xmltag an xml tag without brackets, for example: w:p
* @param int $direction in which direction should be searched. -1 left, 1 right. Default 0: around
* @param string $replacement The replacement xml string. Be careful and keep the xml uncorrupted.
* @param string $docPart 'MainPart' (default) 'Footers:1' (first footer) or 'Headers:2' (second header)
* @param bool $throwException false by default (it then returns false or null on errors)
*
* @return mixed true (replaced), false ($needle not found) or null (no tags found around $needle)
*/
public function replaceSegment(
$needle,
$xmltag,
$direction = self::SEARCH_AROUND,
$replacement = '',
$docPart = 'MainPart',
$throwException = false
) {
return $this->processSegment(
$needle,
$xmltag,
$direction,
0,
$docPart,
(string) $replacement,
false,
$throwException
);
}
/**
* Delete a segment.
*
* @param string $needle If this is a macro, you need to add the ${} yourself
* @param string $xmltag an xml tag without brackets, for example: w:p
* @param int $direction in which direction should be searched. -1 left, 1 right. Default 0: around
* @param string $docPart 'MainPart' (default) 'Footers:1' (first footer) or 'Headers:1' (second header)
*
* @return mixed true (segment deleted), false ($needle not found) or null (no tags found around $needle)
*/
public function deleteSegment($needle, $xmltag, $direction = self::SEARCH_AROUND, $docPart = 'MainPart')
{
return $this->replaceSegment($needle, $xmltag, $direction, '', $docPart, false);
}
/**
* Saves the result document.
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*
* @return string The filename of the document
*/
public function save()
{
foreach ($this->tempDocumentHeaders as $index => $xml) {
$this->zipClass->addFromString($this->getHeaderName($index), $xml);
}
$this->zipClass->addFromString($this->getMainPartName(), $this->tempDocumentMainPart);
foreach ($this->tempDocumentFooters as $index => $xml) {
$this->zipClass->addFromString($this->getFooterName($index), $xml);
}
// Close zip file
if (false === $this->zipClass->close()) {
throw new Exception('Could not close zip file.');
}
return $this->tempDocumentFilename;
}
/**
* Saves the result document to the user defined file.
*
* @since 0.8.0
*
* @param string $fileName
*/
public function saveAs($fileName)
{
$tempFileName = $this->save();
if (file_exists($fileName)) {
unlink($fileName);
}
/*
* Note: we do not use `rename` function here, because it looses file ownership data on Windows platform.
* As a result, user cannot open the file directly getting "Access denied" message.
*
* @see https://github.com/PHPOffice/PHPWord/issues/532
*/
copy($tempFileName, $fileName);
unlink($tempFileName);
}
/**
* Finds parts of broken macros and sticks them together.
* Macros, while being edited, could be implicitly broken by some of the word processors.
* In order to limit side-effects, we limit matches to only inside (inner) paragraphs
*
* @param string $documentPart The document part in XML representation
*
* @return string
*/
protected function fixBrokenMacros($documentPart)
{
$paragraphs = preg_split(
'@(</?w:p\b[^>]*>)@',
$documentPart,
-1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE
);
foreach ($paragraphs as &$paragraph) {
$paragraph = preg_replace_callback(
'|\$(?:<[^\${}]+>)?\{[^{}]*\}|U',
function ($match) {
return strip_tags($match[0]);
},
$paragraph
);
}
return implode('', $paragraphs);
}
/**
* Find and replace macros in the given XML section.
*
* @param mixed $search
* @param mixed $replace
* @param mixed $documentPartXML Array or string (Header/Footer)
* @param int $limit
*
* @return mixed
*/
protected function setValueForPart($search, $replace, $documentPartXML, $limit)
{
// Shift-Enter
if (is_array($replace)) {
foreach ($replace as &$item) {
$item = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $item);
}
unset($item);
} else {
$replace = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $replace);
}
// Note: we can't use the same function for both cases here, because of performance considerations.
if (self::MAXIMUM_REPLACEMENTS_DEFAULT === $limit) {
return str_replace($search, $replace, $documentPartXML);
}
$regExpEscaper = new RegExp();
return preg_replace($regExpEscaper->escape($search), $replace, $documentPartXML, $limit);
}
/**
* Find all variables in $documentPartXML.
*
* @param string $documentPartXML
*
* @return string[]
*/
protected static function getVariablesForPart($documentPartXML)
{
preg_match_all('/\$\{([^<>{}]*?)}/i', $documentPartXML, $matches);
return $matches[1];
}
/**
* Get the name of the header file for $index.
*
* @param int $index
*
* @return string
*/
protected function getHeaderName($index)
{
return sprintf('word/header%d.xml', $index);
}
/**
* @return string
*/
protected function getMainPartName()
{
return 'word/document.xml';
}
/**
* Get the name of the footer file for $index.
*
* @param int $index
*
* @return string
*/
protected function getFooterName($index)
{
return sprintf('word/footer%d.xml', $index);
}
/**
* Find the start position of the nearest tag before $offset.
*
* @param string $searchString The string we are searching in (the mainbody or an array element of Footers/Headers)
* @param string $tag Fully qualified tag, for example: '<w:p>' (with brackets!)
* @param int $offset Do not look from the beginning, but starting at $offset
* @param bool $throwException
*
* @throws \PhpOffice\PhpWord\Exception\Exception
* @return int Zero if not found (due to the nature of xml, your document never starts at 0)
*/
protected function findOpenTagLeft(&$searchString, $tag, $offset = 0, $throwException = false)
{
$tagStart = strrpos(
$searchString,
substr($tag, 0, -1) . ' ',
((strlen($searchString) - $offset) * -1)
);
if ($tagStart === false) {
$tagStart = strrpos(
$searchString,
$tag,
((strlen($searchString) - $offset) * -1)
);
if ($tagStart === false) {
return $this->failGraciously(
'Can not find the start position of the item to clone.',
$throwException,
0
);
}
}