dolibarr/htdocs/includes/swiftmailer/lib/classes/Swift/ByteStream/FileByteStream.php

215 lines
5.9 KiB
PHP
Raw Permalink Normal View History

2016-04-16 18:15:03 +02:00
<?php
/*
* This file is part of SwiftMailer.
* (c) 2004-2009 Chris Corbyn
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Allows reading and writing of bytes to and from a file.
*
2018-01-21 15:55:56 +01:00
* @author Chris Corbyn
2016-04-16 18:15:03 +02:00
*/
class Swift_ByteStream_FileByteStream extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_FileStream
{
/** The internal pointer offset */
2018-01-21 15:55:56 +01:00
private $offset = 0;
2016-04-16 18:15:03 +02:00
/** The path to the file */
2018-01-21 15:55:56 +01:00
private $path;
2016-04-16 18:15:03 +02:00
/** The mode this file is opened in for writing */
2018-01-21 15:55:56 +01:00
private $mode;
2016-04-16 18:15:03 +02:00
/** A lazy-loaded resource handle for reading the file */
2018-01-21 15:55:56 +01:00
private $reader;
2016-04-16 18:15:03 +02:00
/** A lazy-loaded resource handle for writing the file */
2018-01-21 15:55:56 +01:00
private $writer;
2016-04-16 18:15:03 +02:00
/** If stream is seekable true/false, or null if not known */
2018-01-21 15:55:56 +01:00
private $seekable = null;
2016-04-16 18:15:03 +02:00
/**
* Create a new FileByteStream for $path.
*
* @param string $path
* @param bool $writable if true
*/
public function __construct($path, $writable = false)
{
if (empty($path)) {
throw new Swift_IoException('The path cannot be empty');
}
2018-01-21 15:55:56 +01:00
$this->path = $path;
$this->mode = $writable ? 'w+b' : 'rb';
2016-04-16 18:15:03 +02:00
}
/**
* Get the complete path to the file.
*
* @return string
*/
public function getPath()
{
2018-01-21 15:55:56 +01:00
return $this->path;
2016-04-16 18:15:03 +02:00
}
/**
* Reads $length bytes from the stream into a string and moves the pointer
* through the stream by $length.
*
* If less bytes exist than are requested the
* remaining bytes are given instead. If no bytes are remaining at all, boolean
* false is returned.
*
* @param int $length
*
* @return string|bool
2018-01-21 15:55:56 +01:00
*
* @throws Swift_IoException
2016-04-16 18:15:03 +02:00
*/
public function read($length)
{
2018-01-21 15:55:56 +01:00
$fp = $this->getReadHandle();
2016-04-16 18:15:03 +02:00
if (!feof($fp)) {
$bytes = fread($fp, $length);
2018-01-21 15:55:56 +01:00
$this->offset = ftell($fp);
2016-04-16 18:15:03 +02:00
// If we read one byte after reaching the end of the file
// feof() will return false and an empty string is returned
2021-12-07 17:11:34 +01:00
if ((false === $bytes || '' === $bytes) && feof($fp)) {
2018-01-21 15:55:56 +01:00
$this->resetReadHandle();
2016-04-16 18:15:03 +02:00
return false;
}
return $bytes;
}
2018-01-21 15:55:56 +01:00
$this->resetReadHandle();
2016-04-16 18:15:03 +02:00
return false;
}
/**
* Move the internal read pointer to $byteOffset in the stream.
*
* @param int $byteOffset
*
* @return bool
*/
public function setReadPointer($byteOffset)
{
2018-01-21 15:55:56 +01:00
if (isset($this->reader)) {
$this->seekReadStreamToPosition($byteOffset);
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
$this->offset = $byteOffset;
2016-04-16 18:15:03 +02:00
}
/** Just write the bytes to the file */
2018-01-21 15:55:56 +01:00
protected function doCommit($bytes)
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
fwrite($this->getWriteHandle(), $bytes);
$this->resetReadHandle();
2016-04-16 18:15:03 +02:00
}
/** Not used */
2018-01-21 15:55:56 +01:00
protected function flush()
2016-04-16 18:15:03 +02:00
{
}
/** Get the resource for reading */
2018-01-21 15:55:56 +01:00
private function getReadHandle()
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
if (!isset($this->reader)) {
$pointer = @fopen($this->path, 'rb');
if (!$pointer) {
throw new Swift_IoException('Unable to open file for reading ['.$this->path.']');
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
$this->reader = $pointer;
2021-12-07 17:11:34 +01:00
if (0 != $this->offset) {
2018-01-21 15:55:56 +01:00
$this->getReadStreamSeekableStatus();
$this->seekReadStreamToPosition($this->offset);
2016-04-16 18:15:03 +02:00
}
}
2018-01-21 15:55:56 +01:00
return $this->reader;
2016-04-16 18:15:03 +02:00
}
/** Get the resource for writing */
2018-01-21 15:55:56 +01:00
private function getWriteHandle()
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
if (!isset($this->writer)) {
if (!$this->writer = fopen($this->path, $this->mode)) {
2021-12-07 17:11:34 +01:00
throw new Swift_IoException('Unable to open file for writing ['.$this->path.']');
2016-04-16 18:15:03 +02:00
}
}
2018-01-21 15:55:56 +01:00
return $this->writer;
2016-04-16 18:15:03 +02:00
}
/** Force a reload of the resource for reading */
2018-01-21 15:55:56 +01:00
private function resetReadHandle()
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
if (isset($this->reader)) {
fclose($this->reader);
$this->reader = null;
2016-04-16 18:15:03 +02:00
}
}
/** Check if ReadOnly Stream is seekable */
2018-01-21 15:55:56 +01:00
private function getReadStreamSeekableStatus()
2016-04-16 18:15:03 +02:00
{
2018-01-21 15:55:56 +01:00
$metas = stream_get_meta_data($this->reader);
$this->seekable = $metas['seekable'];
2016-04-16 18:15:03 +02:00
}
/** Streams in a readOnly stream ensuring copy if needed */
2018-01-21 15:55:56 +01:00
private function seekReadStreamToPosition($offset)
2016-04-16 18:15:03 +02:00
{
2021-12-07 17:11:34 +01:00
if (null === $this->seekable) {
2018-01-21 15:55:56 +01:00
$this->getReadStreamSeekableStatus();
2016-04-16 18:15:03 +02:00
}
2021-12-07 17:11:34 +01:00
if (false === $this->seekable) {
2018-01-21 15:55:56 +01:00
$currentPos = ftell($this->reader);
2016-04-16 18:15:03 +02:00
if ($currentPos < $offset) {
$toDiscard = $offset - $currentPos;
2018-01-21 15:55:56 +01:00
fread($this->reader, $toDiscard);
2016-04-16 18:15:03 +02:00
return;
}
2018-01-21 15:55:56 +01:00
$this->copyReadStream();
2016-04-16 18:15:03 +02:00
}
2018-01-21 15:55:56 +01:00
fseek($this->reader, $offset, SEEK_SET);
2016-04-16 18:15:03 +02:00
}
/** Copy a readOnly Stream to ensure seekability */
2018-01-21 15:55:56 +01:00
private function copyReadStream()
2016-04-16 18:15:03 +02:00
{
if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) {
/* We have opened a php:// Stream Should work without problem */
2021-12-07 17:11:34 +01:00
} elseif (\function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) {
2016-04-16 18:15:03 +02:00
/* We have opened a tmpfile */
} else {
throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available');
}
2018-01-21 15:55:56 +01:00
$currentPos = ftell($this->reader);
fclose($this->reader);
$source = fopen($this->path, 'rb');
2016-04-16 18:15:03 +02:00
if (!$source) {
2018-01-21 15:55:56 +01:00
throw new Swift_IoException('Unable to open file for copying ['.$this->path.']');
2016-04-16 18:15:03 +02:00
}
fseek($tmpFile, 0, SEEK_SET);
while (!feof($source)) {
fwrite($tmpFile, fread($source, 4096));
}
fseek($tmpFile, $currentPos, SEEK_SET);
fclose($source);
2018-01-21 15:55:56 +01:00
$this->reader = $tmpFile;
2016-04-16 18:15:03 +02:00
}
}