Skip to content

Commit

Permalink
Page cache fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ssigwart committed Mar 9, 2021
1 parent b38e153 commit 0e456ce
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ class TCPDF {
*/
protected $pageCacheFile = null;

/**
* Page cache file is readonly
* @protected
*/
protected $roPageCacheFile = false;

/**
* Page cache index
* @protected
Expand Down Expand Up @@ -7807,6 +7813,10 @@ public function _destroy($destroyall=false, $preserve_objcopy=false) {
}
}
}

// Close page cache file
$this->_closePageCacheFile();

$preserve = array(
'file_id',
'internal_encoding',
Expand All @@ -7820,7 +7830,8 @@ public function _destroy($destroyall=false, $preserve_objcopy=false) {
'signature_max_length',
'byterange_string',
'tsa_timestamp',
'tsa_data'
'tsa_data',
'pageCacheFile'
);
foreach (array_keys(get_object_vars($this)) as $val) {
if ($destroyall OR !in_array($val, $preserve)) {
Expand All @@ -7829,10 +7840,11 @@ public function _destroy($destroyall=false, $preserve_objcopy=false) {
}
}
}
// Close page cache file
$this->_closePageCacheFile();
}

/** Page cache reference counts */
protected static $pageCacheRefCnts = [];

/**
* Update page cache file
*
Expand All @@ -7843,6 +7855,8 @@ public function usePageCacheFile($memSizeInBytes)
$this->pageCacheFile = fopen('php://temp/maxmemory:' . $memSizeInBytes, 'w+');
if ($this->pageCacheFile === false)
$this->pageCacheFile = null;
else
self::$pageCacheRefCnts[(int)$this->pageCacheFile] = 1;
}

/**
Expand All @@ -7852,12 +7866,25 @@ private function _closePageCacheFile()
{
if ($this->pageCacheFile !== null)
{
fclose($this->pageCacheFile);
self::$pageCacheRefCnts[(int)$this->pageCacheFile]--;
if (self::$pageCacheRefCnts[(int)$this->pageCacheFile] == 0)
@fclose($this->pageCacheFile); // Suppress error since this might be the end of the PHP script
$this->pageCacheFile = null;
$this->pageCacheIndex = [];
}
}

/** Handle cloning */
public function __clone()
{
// Update ref count
if ($this->pageCacheFile !== null)
{
self::$pageCacheRefCnts[(int)$this->pageCacheFile]++;
$this->roPageCacheFile = true;
}
}

/**
* Check for locale-related bug
* @protected
Expand Down Expand Up @@ -20869,6 +20896,25 @@ protected function getPageBuffer($page) {
}
*/

// Duplicate file if needed
if ($this->roPageCacheFile)
{
$origPageCacheFile = $this->pageCacheFile;
self::$pageCacheRefCnts[(int)$this->pageCacheFile]--;
$this->pageCacheFile = fopen('php://temp', 'w+');
if ($this->pageCacheFile === false)
$this->pageCacheFile = null;
else
{
self::$pageCacheRefCnts[(int)$this->pageCacheFile] = 1;
// Copy data
fseek($origPageCacheFile, 0, SEEK_SET);
while (($copyContent = fread($origPageCacheFile, 8192)) != false)
fwrite($this->pageCacheFile, $copyContent);
}
$this->roPageCacheFile = false;
}

fseek($this->pageCacheFile, 0, SEEK_END);
$this->pageCacheIndex[$lastPage] = [ftell($this->pageCacheFile), strlen($this->pages[$lastPage])];
fwrite($this->pageCacheFile, $this->pages[$lastPage]);
Expand Down

0 comments on commit 0e456ce

Please sign in to comment.