From c810d23f97fc07dafa5d292906429191aa17e526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helgi=20=C3=9Eormar=20=C3=9Eorbj=C3=B6rnsson?= Date: Wed, 15 Aug 2007 23:54:59 +0000 Subject: [PATCH] Bug #9511 Shared /tmp dir assumed # Added setTmpDir for people that have openbase_dir. # If people try to use the class with out setting a tmp dir # but do have open_basedir on then the class fails silently at the moment git-svn-id: https://svn.php.net/repository/pear/packages/Spreadsheet_Excel_Writer/trunk@241286 c90b9560-bf6c-de11-be94-00142212c4b1 --- Spreadsheet/Excel/Writer/Worksheet.php | 42 +++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/Spreadsheet/Excel/Writer/Worksheet.php b/Spreadsheet/Excel/Writer/Worksheet.php index 03a1108..5d6fe7e 100644 --- a/Spreadsheet/Excel/Writer/Worksheet.php +++ b/Spreadsheet/Excel/Writer/Worksheet.php @@ -391,6 +391,7 @@ class Spreadsheet_Excel_Writer_Worksheet extends Spreadsheet_Excel_Writer_BIFFwr //$this->ext_sheets = array(); $this->_filehandle = ''; $this->_using_tmpfile = true; + $this->_tmp_dir = ''; //$this->fileclosed = 0; //$this->offset = 0; $this->_xls_rowmax = $rowmax; @@ -473,17 +474,50 @@ class Spreadsheet_Excel_Writer_Worksheet extends Spreadsheet_Excel_Writer_BIFFwr */ function _initialize() { + if ($this->_using_tmpfile == false) { + return; + } + + if ($this->_tmp_dir === '' && ini_get('open_basedir') === false) { + //return new PEAR_Error('open_basedir restriction in effect, please use setTmpDir() for this to work'); + return; + } + // Open tmp file for storing Worksheet data - $fh = tmpfile(); - if ($fh) { - // Store filehandle - $this->_filehandle = $fh; + if ($this->_tmp_dir === '') { + $fh = tmpfile(); } else { + // For people with open base dir restriction + $tmpfilename = tempnam($this->_tmp_dir, "Spreadsheet_Excel_Writer"); + $fh = @fopen($tmpfilename, "w+b"); + } + + if ($fh === false) { // If tmpfile() fails store data in memory $this->_using_tmpfile = false; + } else { + // Store filehandle + $this->_filehandle = $fh; } } + /** + * Sets the temp dir used for storing the Excel file + * + * @access public + * @param string $dir The dir to be used as temp dir + * @return true if given dir is valid, false otherwise + */ + function setTempDir($dir) + { + if (is_dir($dir)) { + $this->_tmp_dir = $dir; + return true; + } + + return false; + } + /** * Add data to the beginning of the workbook (note the reverse order) * and to the end of the workbook.