2014-03-10 22:41:41 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* 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.
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @see https://github.com/PHPOffice/PHPWord
|
2022-09-16 11:45:45 +02:00
|
|
|
*
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Reader;
|
|
|
|
|
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-03-15 09:27:48 -04:00
|
|
|
|
2014-03-10 22:41:41 +07:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Reader abstract class.
|
2014-03-24 00:26:10 +07:00
|
|
|
*
|
2014-05-21 22:10:36 +07:00
|
|
|
* @since 0.8.0
|
2015-11-15 21:34:36 +04:00
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @codeCoverageIgnore Abstract class
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-04-08 00:23:49 +07:00
|
|
|
abstract class AbstractReader implements ReaderInterface
|
2014-03-10 22:41:41 +07:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Read data only?
|
|
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @var bool
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-03-11 19:26:56 +07:00
|
|
|
protected $readDataOnly = true;
|
2014-03-10 22:41:41 +07:00
|
|
|
|
2014-03-15 09:27:48 -04:00
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* File pointer.
|
2014-03-17 07:26:25 +07:00
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @var bool|resource
|
|
|
|
|
*/
|
2014-05-27 23:41:15 +07:00
|
|
|
protected $fileHandle;
|
2014-03-10 22:41:41 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read data only?
|
|
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @return bool
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-05-04 15:13:31 +07:00
|
|
|
public function isReadDataOnly()
|
2014-03-11 19:26:56 +07:00
|
|
|
{
|
2014-03-11 02:43:04 +07:00
|
|
|
// return $this->readDataOnly;
|
2014-03-11 19:26:56 +07:00
|
|
|
return true;
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Set read data only.
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
2014-05-15 14:41:08 +07:00
|
|
|
* @param bool $value
|
2022-09-16 11:45:45 +02:00
|
|
|
*
|
2014-04-08 16:09:31 +07:00
|
|
|
* @return self
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-05-15 14:41:08 +07:00
|
|
|
public function setReadDataOnly($value = true)
|
2014-03-11 19:26:56 +07:00
|
|
|
{
|
2014-05-15 14:41:08 +07:00
|
|
|
$this->readDataOnly = $value;
|
2017-11-04 22:44:12 +01:00
|
|
|
|
2014-03-10 22:41:41 +07:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-16 11:45:45 +02:00
|
|
|
* Open file for reading.
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
2014-05-15 14:41:08 +07:00
|
|
|
* @param string $filename
|
2015-10-10 19:06:23 +04:00
|
|
|
*
|
2017-11-04 22:44:12 +01:00
|
|
|
* @return resource
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-05-15 14:41:08 +07:00
|
|
|
protected function openFile($filename)
|
2014-03-10 22:41:41 +07:00
|
|
|
{
|
|
|
|
|
// Check if file exists
|
2014-05-15 14:41:08 +07:00
|
|
|
if (!file_exists($filename) || !is_readable($filename)) {
|
2017-11-04 22:44:12 +01:00
|
|
|
throw new Exception("Could not open $filename for reading! File does not exist.");
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open file
|
2022-09-16 11:45:45 +02:00
|
|
|
$this->fileHandle = fopen($filename, 'rb');
|
2014-03-11 19:26:56 +07:00
|
|
|
if ($this->fileHandle === false) {
|
2017-11-04 22:44:12 +01:00
|
|
|
throw new Exception("Could not open file $filename for reading.");
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-08 00:23:49 +07:00
|
|
|
* Can the current ReaderInterface read the file?
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
2014-05-15 14:41:08 +07:00
|
|
|
* @param string $filename
|
2022-09-16 11:45:45 +02:00
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @return bool
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-05-15 14:41:08 +07:00
|
|
|
public function canRead($filename)
|
2014-03-10 22:41:41 +07:00
|
|
|
{
|
|
|
|
|
// Check if file exists
|
|
|
|
|
try {
|
2014-05-15 14:41:08 +07:00
|
|
|
$this->openFile($filename);
|
2014-03-10 22:41:41 +07:00
|
|
|
} catch (Exception $e) {
|
2014-03-11 19:26:56 +07:00
|
|
|
return false;
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
2014-04-08 03:27:19 +07:00
|
|
|
if (is_resource($this->fileHandle)) {
|
|
|
|
|
fclose($this->fileHandle);
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-15 09:27:48 -04:00
|
|
|
return true;
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
2014-03-23 17:37:26 +07:00
|
|
|
}
|