Skip to content

Commit ec3c62b

Browse files
author
Roman Syroeshko
committed
PHPOffice#483. Output escaping for OOXML.
1 parent 4954f28 commit ec3c62b

File tree

81 files changed

+790
-697
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+790
-697
lines changed

README.md

+10-18
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,9 @@ $phpWord = new \PhpOffice\PhpWord\PhpWord();
9393
$section = $phpWord->addSection();
9494
// Adding Text element to the Section having font styled by default...
9595
$section->addText(
96-
htmlspecialchars(
97-
'"Learn from yesterday, live for today, hope for tomorrow. '
98-
. 'The important thing is not to stop questioning." '
99-
. '(Albert Einstein)'
100-
)
96+
'"Learn from yesterday, live for today, hope for tomorrow. '
97+
. 'The important thing is not to stop questioning." '
98+
. '(Albert Einstein)'
10199
);
102100

103101
/*
@@ -109,11 +107,9 @@ $section->addText(
109107

110108
// Adding Text element with font customized inline...
111109
$section->addText(
112-
htmlspecialchars(
113-
'"Great achievement is usually born of great sacrifice, '
114-
. 'and is never the result of selfishness." '
115-
. '(Napoleon Hill)'
116-
),
110+
'"Great achievement is usually born of great sacrifice, '
111+
. 'and is never the result of selfishness." '
112+
. '(Napoleon Hill)',
117113
array('name' => 'Tahoma', 'size' => 10)
118114
);
119115

@@ -124,11 +120,9 @@ $phpWord->addFontStyle(
124120
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
125121
);
126122
$section->addText(
127-
htmlspecialchars(
128-
'"The greatest accomplishment is not in never falling, '
129-
. 'but in rising again after you fall." '
130-
. '(Vince Lombardi)'
131-
),
123+
'"The greatest accomplishment is not in never falling, '
124+
. 'but in rising again after you fall." '
125+
. '(Vince Lombardi)',
132126
$fontStyleName
133127
);
134128

@@ -137,9 +131,7 @@ $fontStyle = new \PhpOffice\PhpWord\Style\Font();
137131
$fontStyle->setBold(true);
138132
$fontStyle->setName('Tahoma');
139133
$fontStyle->setSize(13);
140-
$myTextElement = $section->addText(
141-
htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')
142-
);
134+
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
143135
$myTextElement->setFontStyle($fontStyle);
144136

145137
// Saving the document as OOXML file...

docs/general.rst

+25-25
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
2424
$section = $phpWord->addSection();
2525
// Adding Text element to the Section having font styled by default...
2626
$section->addText(
27-
htmlspecialchars(
28-
'"Learn from yesterday, live for today, hope for tomorrow. '
29-
. 'The important thing is not to stop questioning." '
30-
. '(Albert Einstein)',
31-
ENT_COMPAT,
32-
'UTF-8'
33-
)
27+
'"Learn from yesterday, live for today, hope for tomorrow. '
28+
. 'The important thing is not to stop questioning." '
29+
. '(Albert Einstein)'
3430
);
3531
3632
/*
@@ -42,13 +38,9 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
4238
4339
// Adding Text element with font customized inline...
4440
$section->addText(
45-
htmlspecialchars(
46-
'"Great achievement is usually born of great sacrifice, '
47-
. 'and is never the result of selfishness." '
48-
. '(Napoleon Hill)',
49-
ENT_COMPAT,
50-
'UTF-8'
51-
),
41+
'"Great achievement is usually born of great sacrifice, '
42+
. 'and is never the result of selfishness." '
43+
. '(Napoleon Hill)',
5244
array('name' => 'Tahoma', 'size' => 10)
5345
);
5446
@@ -59,13 +51,9 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
5951
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
6052
);
6153
$section->addText(
62-
htmlspecialchars(
63-
'"The greatest accomplishment is not in never falling, '
64-
. 'but in rising again after you fall." '
65-
. '(Vince Lombardi)',
66-
ENT_COMPAT,
67-
'UTF-8'
68-
),
54+
'"The greatest accomplishment is not in never falling, '
55+
. 'but in rising again after you fall." '
56+
. '(Vince Lombardi)',
6957
$fontStyleName
7058
);
7159
@@ -74,9 +62,7 @@ folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
7462
$fontStyle->setBold(true);
7563
$fontStyle->setName('Tahoma');
7664
$fontStyle->setSize(13);
77-
$myTextElement = $section->addText(
78-
htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)', ENT_COMPAT, 'UTF-8')
79-
);
65+
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
8066
$myTextElement->setFontStyle($fontStyle);
8167
8268
// Saving the document as OOXML file...
@@ -130,8 +116,22 @@ included with PHPWord.
130116
131117
\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
132118
119+
Output escaping
120+
~~~~~~~~~~~~~~~
121+
122+
Writing documents of some formats, especially XML-based, requires correct output escaping.
123+
Without it your document may become broken when you put special characters like ampersand, quotes, and others in it.
124+
125+
Escaping can be performed in two ways: outside of the library by a software developer and inside of the library by built-in mechanism.
126+
By default, the built-in mechanism is disabled for backward compatibility with versions prior to v0.13.0.
127+
To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord configuration file or use the following instruction at runtime:
128+
129+
.. code-block:: php
130+
131+
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
132+
133133
Default font
134-
------------
134+
~~~~~~~~~~~~
135135

136136
By default, every text appears in Arial 10 point. You can alter the
137137
default font by using the following two functions:

phpword.ini.dist

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
[General]
55

6-
compatibility = true
7-
zipClass = ZipArchive
8-
pdfRendererName = DomPDF
9-
pdfRendererPath =
10-
; tempDir = "C:\PhpWordTemp"
6+
compatibility = true
7+
zipClass = ZipArchive
8+
pdfRendererName = DomPDF
9+
pdfRendererPath =
10+
; tempDir = "C:\PhpWordTemp"
11+
outputEscapingEnabled = false
1112

1213
[Font]
1314

samples/Sample_01_SimpleText.php

+44-39
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
// New Word Document
55
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7-
$phpWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
8-
$phpWord->addParagraphStyle('pStyle', array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));
7+
8+
$fontStyleName = 'rStyle';
9+
$phpWord->addFontStyle($fontStyleName, array('bold' => true, 'italic' => true, 'size' => 16, 'allCaps' => true, 'doubleStrikethrough' => true));
10+
11+
$paragraphStyleName = 'pStyle';
12+
$phpWord->addParagraphStyle($paragraphStyleName, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER, 'spaceAfter' => 100));
13+
914
$phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
1015

1116
// New portrait section
1217
$section = $phpWord->addSection();
1318

1419
// Simple text
15-
$section->addTitle(htmlspecialchars('Welcome to PhpWord', ENT_COMPAT, 'UTF-8'), 1);
16-
$section->addText(htmlspecialchars('Hello World!', ENT_COMPAT, 'UTF-8'));
20+
$section->addTitle('Welcome to PhpWord', 1);
21+
$section->addText('Hello World!');
1722

1823
// Two text break
1924
$section->addTextBreak(2);
2025

21-
// Defined style
22-
$section->addText(htmlspecialchars('I am styled by a font style definition.', ENT_COMPAT, 'UTF-8'), 'rStyle');
23-
$section->addText(htmlspecialchars('I am styled by a paragraph style definition.', ENT_COMPAT, 'UTF-8'), null, 'pStyle');
24-
$section->addText(htmlspecialchars('I am styled by both font and paragraph style.', ENT_COMPAT, 'UTF-8'), 'rStyle', 'pStyle');
26+
// Define styles
27+
$section->addText('I am styled by a font style definition.', $fontStyleName);
28+
$section->addText('I am styled by a paragraph style definition.', null, $paragraphStyleName);
29+
$section->addText('I am styled by both font and paragraph style.', $fontStyleName, $paragraphStyleName);
2530

2631
$section->addTextBreak();
2732

@@ -30,39 +35,39 @@
3035
$fontStyle['size'] = 20;
3136

3237
$textrun = $section->addTextRun();
33-
$textrun->addText(htmlspecialchars('I am inline styled ', ENT_COMPAT, 'UTF-8'), $fontStyle);
34-
$textrun->addText(htmlspecialchars('with ', ENT_COMPAT, 'UTF-8'));
35-
$textrun->addText(htmlspecialchars('color', ENT_COMPAT, 'UTF-8'), array('color' => '996699'));
36-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
37-
$textrun->addText(htmlspecialchars('bold', ENT_COMPAT, 'UTF-8'), array('bold' => true));
38-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
39-
$textrun->addText(htmlspecialchars('italic', ENT_COMPAT, 'UTF-8'), array('italic' => true));
40-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
41-
$textrun->addText(htmlspecialchars('underline', ENT_COMPAT, 'UTF-8'), array('underline' => 'dash'));
42-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
43-
$textrun->addText(htmlspecialchars('strikethrough', ENT_COMPAT, 'UTF-8'), array('strikethrough' => true));
44-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
45-
$textrun->addText(htmlspecialchars('doubleStrikethrough', ENT_COMPAT, 'UTF-8'), array('doubleStrikethrough' => true));
46-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
47-
$textrun->addText(htmlspecialchars('superScript', ENT_COMPAT, 'UTF-8'), array('superScript' => true));
48-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
49-
$textrun->addText(htmlspecialchars('subScript', ENT_COMPAT, 'UTF-8'), array('subScript' => true));
50-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
51-
$textrun->addText(htmlspecialchars('smallCaps', ENT_COMPAT, 'UTF-8'), array('smallCaps' => true));
52-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
53-
$textrun->addText(htmlspecialchars('allCaps', ENT_COMPAT, 'UTF-8'), array('allCaps' => true));
54-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
55-
$textrun->addText(htmlspecialchars('fgColor', ENT_COMPAT, 'UTF-8'), array('fgColor' => 'yellow'));
56-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
57-
$textrun->addText(htmlspecialchars('scale', ENT_COMPAT, 'UTF-8'), array('scale' => 200));
58-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
59-
$textrun->addText(htmlspecialchars('spacing', ENT_COMPAT, 'UTF-8'), array('spacing' => 120));
60-
$textrun->addText(htmlspecialchars(', ', ENT_COMPAT, 'UTF-8'));
61-
$textrun->addText(htmlspecialchars('kerning', ENT_COMPAT, 'UTF-8'), array('kerning' => 10));
62-
$textrun->addText(htmlspecialchars('. ', ENT_COMPAT, 'UTF-8'));
38+
$textrun->addText('I am inline styled ', $fontStyle);
39+
$textrun->addText('with ');
40+
$textrun->addText('color', array('color' => '996699'));
41+
$textrun->addText(', ');
42+
$textrun->addText('bold', array('bold' => true));
43+
$textrun->addText(', ');
44+
$textrun->addText('italic', array('italic' => true));
45+
$textrun->addText(', ');
46+
$textrun->addText('underline', array('underline' => 'dash'));
47+
$textrun->addText(', ');
48+
$textrun->addText('strikethrough', array('strikethrough' => true));
49+
$textrun->addText(', ');
50+
$textrun->addText('doubleStrikethrough', array('doubleStrikethrough' => true));
51+
$textrun->addText(', ');
52+
$textrun->addText('superScript', array('superScript' => true));
53+
$textrun->addText(', ');
54+
$textrun->addText('subScript', array('subScript' => true));
55+
$textrun->addText(', ');
56+
$textrun->addText('smallCaps', array('smallCaps' => true));
57+
$textrun->addText(', ');
58+
$textrun->addText('allCaps', array('allCaps' => true));
59+
$textrun->addText(', ');
60+
$textrun->addText('fgColor', array('fgColor' => 'yellow'));
61+
$textrun->addText(', ');
62+
$textrun->addText('scale', array('scale' => 200));
63+
$textrun->addText(', ');
64+
$textrun->addText('spacing', array('spacing' => 120));
65+
$textrun->addText(', ');
66+
$textrun->addText('kerning', array('kerning' => 10));
67+
$textrun->addText('. ');
6368

6469
// Link
65-
$section->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8'));
70+
$section->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord on GitHub');
6671
$section->addTextBreak();
6772

6873
// Image

samples/Sample_02_TabStops.php

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
echo date('H:i:s'), ' Create new PhpWord object', EOL;
66
$phpWord = new \PhpOffice\PhpWord\PhpWord();
77

8-
// Ads styles
8+
// Define styles
9+
$multipleTabsStyleName = 'multipleTab';
910
$phpWord->addParagraphStyle(
10-
'multipleTab',
11+
$multipleTabsStyleName,
1112
array(
1213
'tabs' => array(
1314
new \PhpOffice\PhpWord\Style\Tab('left', 1550),
@@ -16,22 +17,20 @@
1617
)
1718
)
1819
);
19-
$phpWord->addParagraphStyle(
20-
'rightTab',
21-
array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090)))
22-
);
23-
$phpWord->addParagraphStyle(
24-
'centerTab',
25-
array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680)))
26-
);
20+
21+
$rightTabStyleName = 'rightTab';
22+
$phpWord->addParagraphStyle($rightTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('right', 9090))));
23+
24+
$leftTabStyleName = 'centerTab';
25+
$phpWord->addParagraphStyle($leftTabStyleName, array('tabs' => array(new \PhpOffice\PhpWord\Style\Tab('center', 4680))));
2726

2827
// New portrait section
2928
$section = $phpWord->addSection();
3029

3130
// Add listitem elements
32-
$section->addText(htmlspecialchars("Multiple Tabs:\tOne\tTwo\tThree", ENT_COMPAT, 'UTF-8'), null, 'multipleTab');
33-
$section->addText(htmlspecialchars("Left Aligned\tRight Aligned", ENT_COMPAT, 'UTF-8'), null, 'rightTab');
34-
$section->addText(htmlspecialchars("\tCenter Aligned", ENT_COMPAT, 'UTF-8'), null, 'centerTab');
31+
$section->addText("Multiple Tabs:\tOne\tTwo\tThree", null, $multipleTabsStyleName);
32+
$section->addText("Left Aligned\tRight Aligned", null, $rightTabStyleName);
33+
$section->addText("\tCenter Aligned", null, $leftTabStyleName);
3534

3635
// Save file
3736
echo write($phpWord, basename(__FILE__, '.php'), $writers);

samples/Sample_03_Sections.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@
77

88
// New portrait section
99
$section = $phpWord->addSection(array('borderColor' => '00FF00', 'borderSize' => 12));
10-
$section->addText(htmlspecialchars('I am placed on a default section.', ENT_COMPAT, 'UTF-8'));
10+
$section->addText('I am placed on a default section.');
1111

1212
// New landscape section
1313
$section = $phpWord->addSection(array('orientation' => 'landscape'));
14-
$section->addText(
15-
htmlspecialchars(
16-
'I am placed on a landscape section. Every page starting from this section will be landscape style.',
17-
ENT_COMPAT,
18-
'UTF-8'
19-
)
20-
);
14+
$section->addText('I am placed on a landscape section. Every page starting from this section will be landscape style.');
2115
$section->addPageBreak();
2216
$section->addPageBreak();
2317

2418
// New portrait section
2519
$section = $phpWord->addSection(
2620
array('paperSize' => 'Folio', 'marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)
2721
);
28-
$section->addText(htmlspecialchars('This section uses other margins with folio papersize.', ENT_COMPAT, 'UTF-8'));
22+
$section->addText('This section uses other margins with folio papersize.');
2923

3024
// New portrait section with Header & Footer
3125
$section = $phpWord->addSection(
@@ -38,9 +32,9 @@
3832
'footerHeight' => 50,
3933
)
4034
);
41-
$section->addText(htmlspecialchars('This section and we play with header/footer height.', ENT_COMPAT, 'UTF-8'));
42-
$section->addHeader()->addText(htmlspecialchars('Header', ENT_COMPAT, 'UTF-8'));
43-
$section->addFooter()->addText(htmlspecialchars('Footer', ENT_COMPAT, 'UTF-8'));
35+
$section->addText('This section and we play with header/footer height.');
36+
$section->addHeader()->addText('Header');
37+
$section->addFooter()->addText('Footer');
4438

4539
// Save file
4640
echo write($phpWord, basename(__FILE__, '.php'), $writers);

0 commit comments

Comments
 (0)