123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- <?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.
- *
- * @link https://github.com/PHPOffice/PHPWord
- * @copyright 2010-2014 PHPWord contributors
- * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
- */
-
- namespace PhpOffice\PhpWord\Writer;
-
- use PhpOffice\PhpWord\Exception\CopyFileException;
- use PhpOffice\PhpWord\Exception\Exception;
- use PhpOffice\PhpWord\PhpWord;
- use PhpOffice\PhpWord\Settings;
- use PhpOffice\PhpWord\Shared\ZipArchive;
-
- /**
- * Abstract writer class
- *
- * @since 0.10.0
- */
- abstract class AbstractWriter implements WriterInterface
- {
- /**
- * PHPWord object
- *
- * @var \PhpOffice\PhpWord\PhpWord
- */
- protected $phpWord = null;
-
- /**
- * Part name and file name pairs
- *
- * @var array
- */
- protected $parts = array();
-
- /**
- * Individual writers
- *
- * @var array
- */
- protected $writerParts = array();
-
- /**
- * Paths to store media files
- *
- * @var array
- */
- protected $mediaPaths = array('image' => '', 'object' => '');
-
- /**
- * Use disk caching
- *
- * @var bool
- */
- private $useDiskCaching = false;
-
- /**
- * Disk caching directory
- *
- * @var string
- */
- private $diskCachingDirectory = './';
-
- /**
- * Temporary directory
- *
- * @var string
- */
- private $tempDir = '';
-
- /**
- * Original file name
- *
- * @var string
- */
- private $originalFilename;
-
- /**
- * Temporary file name
- *
- * @var string
- */
- private $tempFilename;
-
- /**
- * Get PhpWord object
- *
- * @return \PhpOffice\PhpWord\PhpWord
- * @throws \PhpOffice\PhpWord\Exception\Exception
- */
- public function getPhpWord()
- {
- if (!is_null($this->phpWord)) {
- return $this->phpWord;
- } else {
- throw new Exception("No PhpWord assigned.");
- }
- }
-
- /**
- * Set PhpWord object
- *
- * @param \PhpOffice\PhpWord\PhpWord
- * @return self
- */
- public function setPhpWord(PhpWord $phpWord = null)
- {
- $this->phpWord = $phpWord;
- return $this;
- }
-
- /**
- * Get writer part
- *
- * @param string $partName Writer part name
- * @return mixed
- */
- public function getWriterPart($partName = '')
- {
- if ($partName != '' && isset($this->writerParts[strtolower($partName)])) {
- return $this->writerParts[strtolower($partName)];
- } else {
- return null;
- }
- }
-
- /**
- * Get use disk caching status
- *
- * @return bool
- */
- public function isUseDiskCaching()
- {
- return $this->useDiskCaching;
- }
-
- /**
- * Set use disk caching status
- *
- * @param bool $value
- * @param string $directory
- * @return self
- * @throws \PhpOffice\PhpWord\Exception\Exception
- */
- public function setUseDiskCaching($value = false, $directory = null)
- {
- $this->useDiskCaching = $value;
-
- if (!is_null($directory)) {
- if (is_dir($directory)) {
- $this->diskCachingDirectory = $directory;
- } else {
- throw new Exception("Directory does not exist: $directory");
- }
- }
-
- return $this;
- }
-
- /**
- * Get disk caching directory
- *
- * @return string
- */
- public function getDiskCachingDirectory()
- {
- return $this->diskCachingDirectory;
- }
-
- /**
- * Get temporary directory
- *
- * @return string
- */
- public function getTempDir()
- {
- return $this->tempDir;
- }
-
- /**
- * Set temporary directory
- *
- * @param string $value
- * @return self
- */
- public function setTempDir($value)
- {
- if (!is_dir($value)) {
- mkdir($value);
- }
- $this->tempDir = $value;
-
- return $this;
- }
-
- /**
- * Get temporary file name
- *
- * If $filename is php://output or php://stdout, make it a temporary file
- *
- * @param string $filename
- * @return string
- */
- protected function getTempFile($filename)
- {
- // Temporary directory
- $this->setTempDir(Settings::getTempDir() . '/PHPWordWriter/');
-
- // Temporary file
- $this->originalFilename = $filename;
- if (strtolower($filename) == 'php://output' || strtolower($filename) == 'php://stdout') {
- $filename = tempnam(Settings::getTempDir(), 'PhpWord');
- if (false === $filename) {
- $filename = $this->originalFilename;
- }
- }
- $this->tempFilename = $filename;
-
- return $this->tempFilename;
- }
-
- /**
- * Cleanup temporary file.
- *
- * @return void
- * @throws \PhpOffice\PhpWord\Exception\CopyFileException
- */
- protected function cleanupTempFile()
- {
- if ($this->originalFilename != $this->tempFilename) {
- // @codeCoverageIgnoreStart
- // Can't find any test case. Uncomment when found.
- if (false === copy($this->tempFilename, $this->originalFilename)) {
- throw new CopyFileException($this->tempFilename, $this->originalFilename);
- }
- // @codeCoverageIgnoreEnd
- @unlink($this->tempFilename);
- }
-
- $this->clearTempDir();
- }
-
- /**
- * Clear temporary directory.
- *
- * @return void
- */
- protected function clearTempDir()
- {
- if (is_dir($this->tempDir)) {
- $this->deleteDir($this->tempDir);
- }
- }
-
- /**
- * Get ZipArchive object
- *
- * @param string $filename
- * @return \PhpOffice\PhpWord\Shared\ZipArchive
- * @throws \Exception
- */
- protected function getZipArchive($filename)
- {
- // Remove any existing file
- if (file_exists($filename)) {
- unlink($filename);
- }
-
- // Try opening the ZIP file
- $zip = new ZipArchive();
-
- // @codeCoverageIgnoreStart
- // Can't find any test case. Uncomment when found.
- if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) {
- if ($zip->open($filename, ZipArchive::CREATE) !== true) {
- throw new \Exception("Could not open '{$filename}' for writing.");
- }
- }
- // @codeCoverageIgnoreEnd
-
- return $zip;
- }
-
- /**
- * Open file for writing
- *
- * @param string $filename
- * @return resource
- * @throws \Exception
- * @since 0.11.0
- */
- protected function openFile($filename)
- {
- $filename = $this->getTempFile($filename);
- $fileHandle = fopen($filename, 'w');
- // @codeCoverageIgnoreStart
- // Can't find any test case. Uncomment when found.
- if ($fileHandle === false) {
- throw new \Exception("Could not open '{$filename}' for writing.");
- }
- // @codeCoverageIgnoreEnd
-
- return $fileHandle;
- }
-
- /**
- * Write content to file.
- *
- * @since 0.11.0
- *
- * @param resource $fileHandle
- * @param string $content
- * @return void
- */
- protected function writeFile($fileHandle, $content)
- {
- fwrite($fileHandle, $content);
- fclose($fileHandle);
- $this->cleanupTempFile();
- }
-
- /**
- * Add files to package.
- *
- * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
- * @param mixed $elements
- * @return void
- */
- protected function addFilesToPackage(ZipArchive $zip, $elements)
- {
- foreach ($elements as $element) {
- $type = $element['type']; // image|object|link
-
- // Skip nonregistered types and set target
- if (!isset($this->mediaPaths[$type])) {
- continue;
- }
- $target = $this->mediaPaths[$type] . $element['target'];
-
- // Retrive GD image content or get local media
- 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();
- $zip->addFromString($target, $imageContents);
- imagedestroy($image);
- } else {
- $this->addFileToPackage($zip, $element['source'], $target);
- }
- }
- }
-
- /**
- * Add file to package.
- *
- * Get the actual source from an archive image.
- *
- * @param \PhpOffice\PhpWord\Shared\ZipArchive $zipPackage
- * @param string $source
- * @param string $target
- * @return void
- */
- protected function addFileToPackage($zipPackage, $source, $target)
- {
- $isArchive = strpos($source, 'zip://') !== false;
- $actualSource = null;
- if ($isArchive) {
- $source = substr($source, 6);
- list($zipFilename, $imageFilename) = explode('#', $source);
-
- $zip = new ZipArchive;
- if ($zip->open($zipFilename) !== false) {
- if ($zip->locateName($imageFilename)) {
- $zip->extractTo($this->getTempDir(), $imageFilename);
- $actualSource = $this->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
- }
- }
- $zip->close();
- } else {
- $actualSource = $source;
- }
-
- if (!is_null($actualSource)) {
- $zipPackage->addFile($actualSource, $target);
- }
- }
-
- /**
- * Delete directory.
- *
- * @param string $dir
- * @return void
- */
- private function deleteDir($dir)
- {
- foreach (scandir($dir) as $file) {
- if ($file === '.' || $file === '..') {
- continue;
- } elseif (is_file($dir . "/" . $file)) {
- unlink($dir . "/" . $file);
- } elseif (is_dir($dir . "/" . $file)) {
- $this->deleteDir($dir . "/" . $file);
- }
- }
-
- rmdir($dir);
- }
-
- /**
- * Get use disk caching status
- *
- * @deprecated 0.10.0
- * @codeCoverageIgnore
- */
- public function getUseDiskCaching()
- {
- return $this->isUseDiskCaching();
- }
- }
|