Skip to content

Commit 21711d1

Browse files
committed
Some Fixes
Fix PHPOffice#2490. Fix PHPOffice#2188.
1 parent 35efca0 commit 21711d1

File tree

22 files changed

+360
-18
lines changed

22 files changed

+360
-18
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ parameters:
8080
count: 1
8181
path: src/PhpWord/IOFactory.php
8282

83-
-
84-
message: "#^Method PhpOffice\\\\PhpWord\\\\PhpWord\\:\\:getDefaultFontSize\\(\\) should return int but returns float\\|int\\.$#"
85-
count: 1
86-
path: src/PhpWord/PhpWord.php
87-
8883
-
8984
message: "#^Method PhpOffice\\\\PhpWord\\\\Reader\\\\AbstractReader\\:\\:openFile\\(\\) should return resource but return statement is missing\\.$#"
9085
count: 1

phpstan-baseline.php73.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ parameters:
7070
count: 1
7171
path: src/PhpWord/IOFactory.php
7272

73-
-
74-
message: "#^Method PhpOffice\\\\PhpWord\\\\PhpWord\\:\\:getDefaultFontSize\\(\\) should return int but returns float\\|int\\.$#"
75-
count: 1
76-
path: src/PhpWord/PhpWord.php
77-
7873
-
7974
message: "#^Method PhpOffice\\\\PhpWord\\\\Reader\\\\AbstractReader\\:\\:openFile\\(\\) should return resource but return statement is missing\\.$#"
8075
count: 1

src/PhpWord/IOFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class IOFactory
3636
*/
3737
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
3838
{
39-
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3'], true)) {
39+
if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3', 'PDF\DomPDF', 'PDF\MPDF', 'PDF\TCPDF'], true)) {
4040
throw new Exception("\"{$name}\" is not a valid writer.");
4141
}
4242

src/PhpWord/PhpWord.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ public function getDefaultFontColor(): string
296296
/**
297297
* Get default font size.
298298
*
299-
* @return int
299+
* @return float|int
300300
*/
301301
public function getDefaultFontSize()
302302
{
@@ -363,6 +363,16 @@ public function save($filename, $format = 'Word2007', $download = false)
363363
return true;
364364
}
365365

366+
public static function noPhar(string $path): void
367+
{
368+
$dontUse = false;
369+
if (filter_var($path, FILTER_VALIDATE_URL) || (preg_match('/^([\w\s\x00-\x1f]+):/u', $path) && !preg_match('/^([\w]+):/u', $path))) {
370+
if (!preg_match('~^((s3):)|(php://(output|memory|temp)$)~', $path)) {
371+
throw new Exception('Invalid protocol used in filename');
372+
}
373+
}
374+
}
375+
366376
/**
367377
* Create new section.
368378
*

src/PhpWord/Reader/MsDoc.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class MsDoc extends AbstractReader implements ReaderInterface
7777
private $arrayFib = [];
7878

7979
/**
80-
* @var string[]
80+
* @var array<array<string, string>>
8181
*/
8282
private $arrayFonts = [];
8383

@@ -1263,7 +1263,7 @@ private function readRecordSttbfFfn(): void
12631263
$xszAlt .= mb_chr($char, 'UTF-8');
12641264
} while ($char != 0);
12651265
}
1266-
$this->arrayFonts[] = [ //* @phpstan-ignore-line
1266+
$this->arrayFonts[] = [
12671267
'main' => $xszFfn,
12681268
'alt' => $xszAlt,
12691269
];
@@ -1936,7 +1936,7 @@ private function readPrl($data, $pos, $cbNum)
19361936
case 0x4F:
19371937
$oStylePrl->styleFont['name'] = '';
19381938
if (isset($this->arrayFonts[$operand])) {
1939-
$oStylePrl->styleFont['name'] = $this->arrayFonts[$operand]['main']; //* @phpstan-ignore-line
1939+
$oStylePrl->styleFont['name'] = $this->arrayFonts[$operand]['main'];
19401940
}
19411941

19421942
break;

src/PhpWord/TemplateProcessor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ protected function savePartWithRels($fileName, $xml): void
10461046
*/
10471047
public function saveAs($fileName): void
10481048
{
1049+
PhpWord::noPhar($fileName);
10491050
$tempFileName = $this->save();
10501051

10511052
if (file_exists($fileName)) {

src/PhpWord/Writer/EPub3.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function __construct(?PhpWord $phpWord = null)
6363
*/
6464
public function save(string $filename): void
6565
{
66+
PhpWord::noPhar($filename);
6667
$filename = $this->getTempFile($filename);
6768
$zip = $this->getZipArchive($filename);
6869

src/PhpWord/Writer/HTML.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function __construct(?PhpWord $phpWord = null)
8989
*/
9090
public function save(string $filename): void
9191
{
92+
PhpWord::noPhar($filename);
9293
$this->writeFile($this->openFile($filename), $this->getContent());
9394
}
9495

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
3+
/**
4+
* This file is part of PHPWord - A pure PHP library for reading and writing
5+
* word processing documents.
6+
*
7+
* PHPWord is free software distributed under the terms of the GNU Lesser
8+
* General Public License version 3 as published by the Free Software Foundation.
9+
*
10+
* For the full copyright and license information, please read the LICENSE
11+
* file that was distributed with this source code. For the full list of
12+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13+
*
14+
* @see https://github.com/PHPOffice/PHPWord
15+
*
16+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17+
*/
18+
19+
namespace PhpOffice\PhpWord\Writer\HTML\Element;
20+
21+
use PhpOffice\PhpWord\Style;
22+
use PhpOffice\PhpWord\Style\Font;
23+
use PhpOffice\PhpWord\Style\Paragraph;
24+
use PhpOffice\PhpWord\Writer\HTML;
25+
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
26+
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
27+
28+
/**
29+
* PreserveText element HTML writer. Issue 2188.
30+
*/
31+
class PreserveText extends AbstractElement
32+
{
33+
/**
34+
* Opening tags.
35+
*
36+
* @var string
37+
*/
38+
private $openingTags = '';
39+
40+
/**
41+
* Closing tag.
42+
*
43+
* @var string
44+
*/
45+
private $closingTags = '';
46+
47+
/**
48+
* Write text.
49+
*
50+
* @return string
51+
*/
52+
public function write()
53+
{
54+
$this->processFontStyle();
55+
56+
/** @var \PhpOffice\PhpWord\Element\PreserveText */
57+
$element = $this->element;
58+
$text = $element->getText();
59+
if (is_array($text)) {
60+
$text = implode('', $text);
61+
}
62+
63+
$text = $this->parentWriter->escapeHTML($text ?? '');
64+
if (!$this->withoutP && !trim($text)) {
65+
$text = '&nbsp;';
66+
}
67+
68+
$content = '';
69+
$content .= $this->writeOpening();
70+
$content .= $this->openingTags;
71+
$content .= $text;
72+
$content .= $this->closingTags;
73+
$content .= $this->writeClosing();
74+
75+
return $content;
76+
}
77+
78+
/**
79+
* Write opening.
80+
*
81+
* @return string
82+
*/
83+
protected function writeOpening()
84+
{
85+
$content = '';
86+
if (!$this->withoutP) {
87+
$style = $this->getParagraphStyle();
88+
$content .= "<p{$style}>";
89+
}
90+
91+
return $content;
92+
}
93+
94+
/**
95+
* Write ending.
96+
*
97+
* @return string
98+
*/
99+
protected function writeClosing()
100+
{
101+
$content = '';
102+
103+
if (!$this->withoutP) {
104+
$content .= '</p>' . PHP_EOL;
105+
}
106+
107+
return $content;
108+
}
109+
110+
/**
111+
* Write paragraph style.
112+
*
113+
* @return string
114+
*/
115+
private function getParagraphStyle()
116+
{
117+
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
118+
$element = $this->element;
119+
$style = '';
120+
if (!method_exists($element, 'getParagraphStyle')) {
121+
return $style;
122+
}
123+
124+
$paragraphStyle = $element->getParagraphStyle();
125+
$pStyleIsObject = ($paragraphStyle instanceof Paragraph);
126+
if ($pStyleIsObject) {
127+
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
128+
$styleWriter->setParentWriter($this->parentWriter);
129+
$style = $styleWriter->write();
130+
} elseif (is_string($paragraphStyle)) {
131+
$style = $paragraphStyle;
132+
}
133+
if ($style) {
134+
$attribute = $pStyleIsObject ? 'style' : 'class';
135+
$style = " {$attribute}=\"{$style}\"";
136+
}
137+
138+
return $style;
139+
}
140+
141+
/**
142+
* Get font style.
143+
*/
144+
private function processFontStyle(): void
145+
{
146+
/** @var \PhpOffice\PhpWord\Element\Text $element Type hint */
147+
$element = $this->element;
148+
149+
$attributeStyle = $attributeLang = '';
150+
$lang = null;
151+
152+
$fontStyle = $element->getFontStyle();
153+
if ($fontStyle instanceof Font) {
154+
// Attribute style
155+
$styleWriter = new FontStyleWriter($fontStyle);
156+
$fontCSS = $styleWriter->write();
157+
if ($fontCSS) {
158+
$attributeStyle = ' style="' . $fontCSS . '"';
159+
} else {
160+
$className = $fontStyle->getStyleName();
161+
if ($className) {
162+
$attributeStyle = ' class="' . $className . '"';
163+
}
164+
}
165+
// Attribute Lang
166+
$lang = $fontStyle->getLang();
167+
} elseif (!empty($fontStyle)) {
168+
// Attribute class
169+
$attributeStyle = ' class="' . $fontStyle . '"';
170+
// Attribute Lang
171+
/** @var Font $cssClassStyle */
172+
$cssClassStyle = Style::getStyle($fontStyle);
173+
if ($cssClassStyle !== null && method_exists($cssClassStyle, 'getLang')) {
174+
$lang = $cssClassStyle->getLang();
175+
}
176+
}
177+
178+
if ($lang) {
179+
$attributeLang = $lang->getLatin();
180+
if (!$attributeLang) {
181+
$attributeLang = $lang->getEastAsia();
182+
}
183+
if (!$attributeLang) {
184+
$attributeLang = $lang->getBidirectional();
185+
}
186+
if ($attributeLang) {
187+
$attributeLang = " lang='$attributeLang'";
188+
}
189+
}
190+
191+
if ($attributeStyle || $attributeLang) {
192+
$this->openingTags = "<span$attributeLang$attributeStyle>";
193+
$this->closingTags = '</span>';
194+
}
195+
}
196+
}

src/PhpWord/Writer/ODText.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function __construct(?PhpWord $phpWord = null)
7272
*/
7373
public function save(string $filename): void
7474
{
75+
PhpWord::noPhar($filename);
7576
$filename = $this->getTempFile($filename);
7677
$zip = $this->getZipArchive($filename);
7778

0 commit comments

Comments
 (0)