diff --git a/include/tcpdf_page_cache_reference_counts.php b/include/tcpdf_page_cache_reference_counts.php new file mode 100644 index 00000000..401c2ce4 --- /dev/null +++ b/include/tcpdf_page_cache_reference_counts.php @@ -0,0 +1,75 @@ +. +// +// See LICENSE.TXT file for more information. +// ------------------------------------------------------------------- +// +// Description : Class to keep track of page cache references +// +//============================================================+ + +/** + * @file + * PHP page cache reference counts class for TCPDF + * @package com.tecnick.tcpdf + */ + +/** + * @class TCPDF_PAGE_CACHE_REFERENCE_COUNTS + * PHP page cache reference counts class for TCPDF + * @package com.tecnick.tcpdf + * @version 1.0.000 + */ +class TCPDF_PAGE_CACHE_REFERENCE_COUNTS +{ + /** @var int Number of references */ + private $numReferences = 0; + + /** + * Increment reference count + */ + public function incrementReferenceCount() + { + $this->numReferences++; + } + + /** + * Decrement reference count + */ + public function decrementReferenceCount() + { + $this->numReferences--; + } + + /** + * Get reference count + * + * @return int Reference count + */ + public function getReferenceCount() + { + return $this->numReferences; + } +} diff --git a/tcpdf.php b/tcpdf.php index 4de17033..9db0b099 100644 --- a/tcpdf.php +++ b/tcpdf.php @@ -119,6 +119,8 @@ require_once(dirname(__FILE__).'/include/tcpdf_images.php'); // TCPDF static methods and data require_once(dirname(__FILE__).'/include/tcpdf_static.php'); +// TCPDF page cache reference counts +require_once(dirname(__FILE__).'/include/tcpdf_page_cache_reference_counts.php'); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -195,6 +197,13 @@ class TCPDF { */ protected $pageCacheIndex = array(); + /** + * Page cache reference counts + * @protected + * @var TCPDF_PAGE_CACHE_REFERENCE_COUNTS|null + */ + protected $pageCacheRefCnts = null; + /** * Current document state. * @protected @@ -7912,9 +7921,6 @@ public function _destroy($destroyall=false, $preserve_objcopy=false) { } } - /** Page cache reference counts */ - protected static $pageCacheRefCnts = array(); - /** * Update page cache file * @@ -7927,7 +7933,9 @@ public function usePageCacheFile($memSizeInBytes) if ($this->pageCacheFile === false) { $this->pageCacheFile = null; } else { - self::$pageCacheRefCnts[(int)$this->pageCacheFile] = 1; + if ($this->pageCacheRefCnts == null) + $this->pageCacheRefCnts = new TCPDF_PAGE_CACHE_REFERENCE_COUNTS(); + $this->pageCacheRefCnts->incrementReferenceCount(); } } @@ -7938,8 +7946,8 @@ private function _closePageCacheFile() { if ($this->pageCacheFile !== null) { - self::$pageCacheRefCnts[(int)$this->pageCacheFile]--; - if (self::$pageCacheRefCnts[(int)$this->pageCacheFile] == 0) { + $this->pageCacheRefCnts->decrementReferenceCount(); + if ($this->pageCacheRefCnts->getReferenceCount() == 0) { // This might be the end of the PHP script and the temporary file may be // removed before this class, so check if it's still a valid resource. if (is_resource($this->pageCacheFile)) { @@ -7961,7 +7969,7 @@ public function __clone() // Update ref count if ($this->pageCacheFile !== null) { - self::$pageCacheRefCnts[(int)$this->pageCacheFile]++; + $this->pageCacheRefCnts->incrementReferenceCount(); $this->roPageCacheFile = true; } } @@ -21044,13 +21052,15 @@ protected function getPageBuffer($page) { { $origPageCacheFile = $this->pageCacheFile; // Remove reference count to file and open a new page cache file - self::$pageCacheRefCnts[(int)$this->pageCacheFile]--; + $this->pageCacheRefCnts->decrementReferenceCount(); $this->pageCacheFile = fopen('php://temp', 'w+'); if ($this->pageCacheFile === false) { $this->pageCacheFile = null; + $this->pageCacheRefCnts = null; } else { // Add reference to new page cache file - self::$pageCacheRefCnts[(int)$this->pageCacheFile] = 1; + $this->pageCacheRefCnts = new TCPDF_PAGE_CACHE_REFERENCE_COUNTS(); + $this->pageCacheRefCnts->incrementReferenceCount(); // Copy data from old page cache file to new one fseek($origPageCacheFile, 0, SEEK_SET); while (($copyContent = fread($origPageCacheFile, 8192)) != false) {