PHPWord/src/PhpWord/Writer/Word2007.php

373 lines
13 KiB
PHP
Raw Normal View History

2012-05-06 18:25:40 +02:00
<?php
/**
* PHPWord
2012-05-06 18:25:40 +02:00
*
2014-03-27 23:55:06 +07:00
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
2012-05-06 18:25:40 +02:00
*/
2014-03-12 10:09:42 -04:00
namespace PhpOffice\PhpWord\Writer;
use PhpOffice\PhpWord\Exceptions\Exception;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Footnote;
use PhpOffice\PhpWord\Media;
use PhpOffice\PhpWord\Writer\Word2007\ContentTypes;
use PhpOffice\PhpWord\Writer\Word2007\DocProps;
use PhpOffice\PhpWord\Writer\Word2007\Document;
use PhpOffice\PhpWord\Writer\Word2007\DocumentRels;
use PhpOffice\PhpWord\Writer\Word2007\Footer;
use PhpOffice\PhpWord\Writer\Word2007\Footnotes;
use PhpOffice\PhpWord\Writer\Word2007\FootnotesRels;
use PhpOffice\PhpWord\Writer\Word2007\Header;
use PhpOffice\PhpWord\Writer\Word2007\Rels;
use PhpOffice\PhpWord\Writer\Word2007\Styles;
/**
* Word2007 writer
*/
class Word2007 implements IWriter
2013-12-16 06:40:30 -05:00
{
/**
* PHPWord object
*
* @var PhpOffice\PhpWord\PhpWord
*/
2013-12-16 06:40:30 -05:00
private $_document;
/**
* Individual writers
*
* @var PhpOffice\PhpWord\Writer\Word2007\WriterPart
*/
2013-12-16 06:40:30 -05:00
private $_writerParts;
/**
* Disk caching directory
*
* @var string
*/
2013-12-16 06:40:30 -05:00
private $_diskCachingDirectory;
/**
* Use disk caching
*
* @var boolean
*/
2013-12-16 06:40:30 -05:00
private $_useDiskCaching = false;
/**
* Types of images
*
* @var array
*/
2013-12-16 06:40:30 -05:00
private $_imageTypes = array();
/**
* Types of objects
*
* @var array
*/
2013-12-16 06:40:30 -05:00
private $_objectTypes = array();
/**
* Create new Word2007 writer
*
* @param PhpOffice\PhpWord\PhpWord
*/
public function __construct(PhpWord $phpWord = null)
2013-12-16 06:40:30 -05:00
{
$this->_document = $phpWord;
2013-12-16 06:40:30 -05:00
$this->_diskCachingDirectory = './';
$this->_writerParts['contenttypes'] = new ContentTypes();
$this->_writerParts['rels'] = new Rels();
$this->_writerParts['docprops'] = new DocProps();
$this->_writerParts['documentrels'] = new DocumentRels();
$this->_writerParts['document'] = new Document();
$this->_writerParts['styles'] = new Styles();
$this->_writerParts['header'] = new Header();
$this->_writerParts['footer'] = new Footer();
$this->_writerParts['footnotes'] = new Footnotes();
$this->_writerParts['footnotesrels'] = new FootnotesRels();
2013-12-16 06:40:30 -05:00
foreach ($this->_writerParts as $writer) {
$writer->setParentWriter($this);
}
}
/**
* Save document by name
*
* @param string $pFilename
*/
2013-12-16 06:40:30 -05:00
public function save($pFilename = null)
{
if (!is_null($this->_document)) {
// If $pFilename is php://output or php://stdout, make it a temporary file...
$originalFilename = $pFilename;
if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
$pFilename = @tempnam(sys_get_temp_dir(), 'phpword_');
2013-12-16 06:40:30 -05:00
if ($pFilename == '') {
$pFilename = $originalFilename;
}
}
// Create new ZIP file and open it for writing
$objZip = new \ZipArchive();
2013-12-16 06:40:30 -05:00
// Try opening the ZIP file
if ($objZip->open($pFilename, \ZipArchive::OVERWRITE) !== true) {
if ($objZip->open($pFilename, \ZipArchive::CREATE) !== true) {
2013-12-16 06:40:30 -05:00
throw new Exception("Could not open " . $pFilename . " for writing.");
}
}
$sectionElements = array();
$_secElements = Media::getSectionMediaElements();
2013-12-16 06:40:30 -05:00
foreach ($_secElements as $element) { // loop through section media elements
if ($element['type'] != 'hyperlink') {
$this->_addFileToPackage($objZip, $element);
}
$sectionElements[] = $element;
}
$_hdrElements = Media::getHeaderMediaElements();
2013-12-16 06:40:30 -05:00
foreach ($_hdrElements as $_headerFile => $_hdrMedia) { // loop through headers
if (count($_hdrMedia) > 0) {
$objZip->addFromString('word/_rels/' . $_headerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_hdrMedia));
foreach ($_hdrMedia as $element) { // loop through header media elements
$this->_addFileToPackage($objZip, $element);
}
}
}
$_ftrElements = Media::getFooterMediaElements();
2013-12-16 06:40:30 -05:00
foreach ($_ftrElements as $_footerFile => $_ftrMedia) { // loop through footers
if (count($_ftrMedia) > 0) {
$objZip->addFromString('word/_rels/' . $_footerFile . '.xml.rels', $this->getWriterPart('documentrels')->writeHeaderFooterRels($_ftrMedia));
foreach ($_ftrMedia as $element) { // loop through footers media elements
$this->_addFileToPackage($objZip, $element);
}
}
}
$footnoteLinks = array();
$_footnoteElements = Footnote::getFootnoteLinkElements();
// loop through footnote link elements
foreach ($_footnoteElements as $element) {
$footnoteLinks[] = $element;
}
2013-12-16 06:40:30 -05:00
$_cHdrs = 0;
$_cFtrs = 0;
$rID = Media::countSectionMediaElements() + 6;
2013-12-16 06:40:30 -05:00
$_sections = $this->_document->getSections();
2012-05-06 18:25:40 +02:00
$footers = array();
2013-12-16 06:40:30 -05:00
foreach ($_sections as $section) {
$_headers = $section->getHeaders();
foreach ($_headers as $index => &$_header) {
$_cHdrs++;
$_header->setRelationId(++$rID);
$_headerFile = 'header' . $_cHdrs . '.xml';
$sectionElements[] = array('target' => $_headerFile, 'type' => 'header', 'rID' => $rID);
$objZip->addFromString('word/' . $_headerFile, $this->getWriterPart('header')->writeHeader($_header));
}
$_footer = $section->getFooter();
$footers[++$_cFtrs] = $_footer;
if (!is_null($_footer)) {
$_footer->setRelationId(++$rID);
$_footerCount = $_footer->getFooterCount();
$_footerFile = 'footer' . $_footerCount . '.xml';
$sectionElements[] = array('target' => $_footerFile, 'type' => 'footer', 'rID' => $rID);
$objZip->addFromString('word/' . $_footerFile, $this->getWriterPart('footer')->writeFooter($_footer));
}
2013-12-16 06:40:30 -05:00
}
if (Footnote::countFootnoteElements() > 0) {
$_allFootnotesCollection = Footnote::getFootnoteElements();
$_footnoteFile = 'footnotes.xml';
$sectionElements[] = array('target'=>$_footnoteFile, 'type'=>'footnotes', 'rID'=>++$rID);
$objZip->addFromString('word/'.$_footnoteFile, $this->getWriterPart('footnotes')->writeFootnotes($_allFootnotesCollection));
if (count($footnoteLinks) > 0) {
$objZip->addFromString('word/_rels/footnotes.xml.rels', $this->getWriterPart('footnotesrels')->writeFootnotesRels($footnoteLinks));
}
}
2013-12-16 06:40:30 -05:00
// build docx file
// Write dynamic files
$objZip->addFromString(
'[Content_Types].xml',
$this->getWriterPart('contenttypes')->writeContentTypes(
$this->_imageTypes,
$this->_objectTypes,
$_cHdrs,
$footers
)
);
2013-12-16 06:40:30 -05:00
$objZip->addFromString('_rels/.rels', $this->getWriterPart('rels')->writeRelationships($this->_document));
$objZip->addFromString('docProps/app.xml', $this->getWriterPart('docprops')->writeDocPropsApp($this->_document));
$objZip->addFromString('docProps/core.xml', $this->getWriterPart('docprops')->writeDocPropsCore($this->_document));
$objZip->addFromString('word/document.xml', $this->getWriterPart('document')->writeDocument($this->_document));
$objZip->addFromString('word/_rels/document.xml.rels', $this->getWriterPart('documentrels')->writeDocumentRels($sectionElements));
$objZip->addFromString('word/styles.xml', $this->getWriterPart('styles')->writeStyles($this->_document));
2012-05-06 18:25:40 +02:00
// Write static files
$objZip->addFile(__DIR__ . '/../_staticDocParts/numbering.xml', 'word/numbering.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/settings.xml', 'word/settings.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/theme1.xml', 'word/theme/theme1.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/webSettings.xml', 'word/webSettings.xml');
$objZip->addFile(__DIR__ . '/../_staticDocParts/fontTable.xml', 'word/fontTable.xml');
2013-12-16 06:40:30 -05:00
// Close file
if ($objZip->close() === false) {
throw new Exception("Could not close zip file $pFilename.");
}
// If a temporary file was used, copy it to the correct file stream
if ($originalFilename != $pFilename) {
if (copy($pFilename, $originalFilename) === false) {
throw new Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
}
@unlink($pFilename);
}
} else {
throw new Exception("PhpWord object unassigned.");
2013-12-16 06:40:30 -05:00
}
}
2014-03-12 10:09:42 -04:00
/**
* Check content types
*
2014-03-12 10:09:42 -04:00
* @param string $src
*/
private function checkContentTypes($src)
2013-12-16 06:40:30 -05:00
{
2014-03-12 10:09:42 -04:00
$extension = null;
if (stripos(strrev($src), strrev('.php')) === 0) {
2013-12-16 06:40:30 -05:00
$extension = 'php';
2014-03-12 10:09:42 -04:00
} else {
if (function_exists('exif_imagetype')) {
$imageType = exif_imagetype($src);
} else {
$tmp = getimagesize($src);
$imageType = $tmp[2];
}
if ($imageType === \IMAGETYPE_JPEG) {
$extension = 'jpg';
} elseif ($imageType === \IMAGETYPE_GIF) {
$extension = 'gif';
} elseif ($imageType === \IMAGETYPE_PNG) {
$extension = 'png';
} elseif ($imageType === \IMAGETYPE_BMP) {
$extension = 'bmp';
} elseif ($imageType === \IMAGETYPE_TIFF_II || $imageType === \IMAGETYPE_TIFF_MM) {
$extension = 'tif';
}
2013-12-16 06:40:30 -05:00
}
2014-03-12 10:09:42 -04:00
if (isset($extension)) {
2014-03-12 10:09:42 -04:00
$imageData = getimagesize($src);
$imageType = image_type_to_mime_type($imageData[2]);
$imageExtension = str_replace('.', '', image_type_to_extension($imageData[2]));
if ($imageExtension === 'jpeg') {
$imageExtension = 'jpg';
}
2014-03-12 10:09:42 -04:00
if (!in_array($imageType, $this->_imageTypes)) {
$this->_imageTypes[$imageExtension] = $imageType;
2013-12-16 06:40:30 -05:00
}
} else {
if (!in_array($extension, $this->_objectTypes)) {
$this->_objectTypes[] = $extension;
}
2013-12-16 06:40:30 -05:00
}
}
/**
* Get writer part
*
* @param string $pPartName Writer part name
* @return \PhpOffice\PhpWord\Writer\ODText\WriterPart
*/
2013-12-16 06:40:30 -05:00
public function getWriterPart($pPartName = '')
{
if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) {
return $this->_writerParts[strtolower($pPartName)];
} else {
return null;
}
}
/**
* Get use disk caching status
*
* @return boolean
*/
2013-12-16 06:40:30 -05:00
public function getUseDiskCaching()
{
return $this->_useDiskCaching;
}
/**
* Set use disk caching status
*
* @param boolean $pValue
* @param string $pDirectory
*/
2013-12-16 06:40:30 -05:00
public function setUseDiskCaching($pValue = false, $pDirectory = null)
{
$this->_useDiskCaching = $pValue;
if (!is_null($pDirectory)) {
if (is_dir($pDirectory)) {
$this->_diskCachingDirectory = $pDirectory;
} else {
throw new Exception("Directory does not exist: $pDirectory");
}
}
return $this;
}
2014-03-17 07:26:25 +07:00
/**
* Get disk caching directory
*
* @return string
2014-03-17 07:26:25 +07:00
*/
public function getDiskCachingDirectory()
{
return $this->_diskCachingDirectory;
}
/**
* Check content types
*
* @param mixed $objZip
* @param mixed $element
*/
2013-12-16 06:40:30 -05:00
private function _addFileToPackage($objZip, $element)
{
if (isset($element['isMemImage']) && $element['isMemImage']) {
$image = call_user_func($element['createfunction'], $element['source']);
ob_start();
call_user_func($element['imagefunction'], $image);
$imageContents = ob_get_contents();
ob_end_clean();
$objZip->addFromString('word/' . $element['target'], $imageContents);
imagedestroy($image);
2014-03-12 10:09:42 -04:00
$this->checkContentTypes($element['source']);
2013-12-16 06:40:30 -05:00
} else {
$objZip->addFile($element['source'], 'word/' . $element['target']);
2014-03-12 10:09:42 -04:00
$this->checkContentTypes($element['source']);
2013-12-16 06:40:30 -05:00
}
}
}