Skip to content

Commit 95f16cc

Browse files
committed
Temporary files naming logic in PHPWord_Template can lead to a collision (GH-38)
1 parent 9557dd9 commit 95f16cc

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

Classes/PHPWord/Template.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,20 @@ class PHPWord_Template {
6363
* @param string $strFilename
6464
*/
6565
public function __construct($strFilename) {
66-
$path = dirname($strFilename);
67-
$this->_tempFileName = $path.DIRECTORY_SEPARATOR.time().'.docx';
66+
$this->_tempFileName = tempnam(sys_get_temp_dir(), '');
67+
if ($this->_tempFileName !== false) {
68+
// Copy the source File to the temp File
69+
if(!copy($strFilename, $this->_tempFileName)){
70+
throw new PHPWord_Exception('Could not copy the template from '.$strFilename.' to '.$this->_tempFileName.'.');
71+
}
6872

69-
// Copy the source File to the temp File
70-
if(!copy($strFilename, $this->_tempFileName)){
71-
throw new PHPWord_Exception('Could not copy the template from '.$strFilename.' to '.$this->_tempFileName.'.');
72-
}
73+
$this->_objZip = new ZipArchive();
74+
$this->_objZip->open($this->_tempFileName);
7375

74-
$this->_objZip = new ZipArchive();
75-
$this->_objZip->open($this->_tempFileName);
76-
77-
$this->_documentXML = $this->_objZip->getFromName('word/document.xml');
76+
$this->_documentXML = $this->_objZip->getFromName('word/document.xml');
77+
} else {
78+
throw new PHPWord_Exception('Could not create temporary file with unique name in the default temporary directory.');
79+
}
7880
}
7981

8082
/**

changelog.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
**************************************************************************************
2424

2525
Fixed in branch for release 0.7 :
26-
- Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input
2726
- Bugfix: (RomanSyroeshko) GH-32 - "Warning: Invalid error type specified in ...\PHPWord.php on line 226" is thrown when the specified template file is not found
27+
- Bugfix: (RomanSyroeshko) GH-34 - PHPWord_Shared_String.IsUTF8 returns FALSE for Cyrillic UTF-8 input
28+
- Bugfix: (RomanSyroeshko) GH-38 - Temporary files naming logic in PHPWord_Template can lead to a collision
2829
- Feature: (Progi1984) GH-1 - Implement RTF Writer
2930
- Feature: (Progi1984) GH-2 - Implement ODT Writer
3031
- Feature: (kaystrobach) - Word2007 : Add rowspan and colspan to cells

0 commit comments

Comments
 (0)