A lot of fix in phpOdt module

This commit is contained in:
Laurent Destailleur 2010-03-13 16:05:36 +00:00
parent f1d67cba3c
commit ed19e89531
3 changed files with 367 additions and 331 deletions

View File

@ -280,19 +280,18 @@ class doc_generic_odt extends ModeleThirdPartyDoc
//print "newdir=".$dir; //print "newdir=".$dir;
//print "newfile=".$newfile; //print "newfile=".$newfile;
//print "file=".$file; //print "file=".$file;
//exit; //print "conf->societe->dir_temp=".$conf->societe->dir_temp;
create_exdir($conf->societe->dir_temp); create_exdir($conf->societe->dir_temp);
// Open and load template // Open and load template
require_once(DOL_DOCUMENT_ROOT.'/includes/odtphp/odf.php'); require_once(DOL_DOCUMENT_ROOT.'/includes/odtphp/odf.php');
$odfHandler = new odf($srctemplatepath, array( $odfHandler = new odf($srctemplatepath, array(
'PATH_TO_TMP' => $conf->societe->dir_temp.'/', 'PATH_TO_TMP' => $conf->societe->dir_temp,
'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got bad compression method when using PhpZipProxy. 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got bad compression method when using PhpZipProxy.
'DELIMITER_LEFT' => '{', 'DELIMITER_LEFT' => '{',
'DELIMITER_RIGHT' => '}') 'DELIMITER_RIGHT' => '}')
); );
//print $odfHandler; exit; //print $odfHandler; exit;
// Make substitutions // Make substitutions
@ -324,6 +323,8 @@ class doc_generic_odt extends ModeleThirdPartyDoc
if (! empty($conf->global->MAIN_UMASK)) if (! empty($conf->global->MAIN_UMASK))
@chmod($file, octdec($conf->global->MAIN_UMASK)); @chmod($file, octdec($conf->global->MAIN_UMASK));
$odfHandler=null; // Destroy object
return 1; // Success return 1; // Success
} }
else else

View File

@ -1,6 +1,4 @@
<?php <?php
require 'zip/PclZipProxy.php';
require 'zip/PhpZipProxy.php';
require 'Segment.php'; require 'Segment.php';
class OdfException extends Exception class OdfException extends Exception
{} {}
@ -15,20 +13,22 @@ class OdfException extends Exception
* Id : $Id$ * Id : $Id$
* *
* @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com) * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
* @copyright GPL License 2010 - Laurent Destailleur - eldy@users.sourceforge.net
* @license http://www.gnu.org/copyleft/gpl.html GPL License * @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 1.3 * @version 1.4
*/ */
class Odf class Odf
{ {
protected $config = array( protected $config = array(
'ZIP_PROXY' => 'PclZipProxy', 'ZIP_PROXY' => 'PclZipProxy', // PclZipProxy, PhpZipProxy
'DELIMITER_LEFT' => '{', 'DELIMITER_LEFT' => '{',
'DELIMITER_RIGHT' => '}', 'DELIMITER_RIGHT' => '}',
'PATH_TO_TMP' => null 'PATH_TO_TMP' => '/tmp'
); );
protected $file; protected $file;
protected $contentXml; protected $contentXml;
protected $tmpfile; protected $tmpfile;
protected $tmpdir;
protected $images = array(); protected $images = array();
protected $vars = array(); protected $vars = array();
protected $segments = array(); protected $segments = array();
@ -41,6 +41,8 @@ class Odf
*/ */
public function __construct($filename, $config = array()) public function __construct($filename, $config = array())
{ {
clearstatcache();
if (! is_array($config)) { if (! is_array($config)) {
throw new OdfException('Configuration data must be provided as array'); throw new OdfException('Configuration data must be provided as array');
} }
@ -49,25 +51,48 @@ class Odf
$this->config[$configKey] = $configValue; $this->config[$configKey] = $configValue;
} }
} }
$md5uniqid = md5(uniqid());
if ($this->config['PATH_TO_TMP']) $this->tmpdir = preg_replace('|[\/]$|','',$this->config['PATH_TO_TMP']);
$this->tmpdir .= ($this->tmpdir?'/':'').$md5uniqid;
$this->tmpfile = $this->tmpdir.'/'.$md5uniqid.'.odt'; // We keep .odt extension to allow OpenOffice usage during debug.
// A working directory is required for some zip proxy like PclZipProxy
if (in_array($this->config['ZIP_PROXY'],array('PclZipProxy')) && ! is_dir($this->config['PATH_TO_TMP'])) {
throw new OdfException('Temporary directory '.$this->config['PATH_TO_TMP'].' must exists');
}
// Create tmp direcoty (will be deleted in destructor)
if (!file_exists($this->tmpdir)) {
$result=mkdir($this->tmpdir);
}
// Load zip proxy
$zipHandler = $this->config['ZIP_PROXY'];
define('PCLZIP_TEMPORARY_DIR',$this->tmpdir);
include_once('zip/'.$zipHandler.'.php');
if (! class_exists($this->config['ZIP_PROXY'])) { if (! class_exists($this->config['ZIP_PROXY'])) {
throw new OdfException($this->config['ZIP_PROXY'] . ' class not found - check your php settings'); throw new OdfException($this->config['ZIP_PROXY'] . ' class not found - check your php settings');
} }
$zipHandler = $this->config['ZIP_PROXY'];
$this->file = new $zipHandler(); $this->file = new $zipHandler();
if ($this->file->open($filename) !== true) {
if ($this->file->open($filename) !== true) { // This also create the tmpdir directory
throw new OdfException("Error while Opening the file '$filename' - Check your odt file"); throw new OdfException("Error while Opening the file '$filename' - Check your odt file");
} }
if (($this->contentXml = $this->file->getFromName('content.xml')) === false) { if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed"); throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
} }
$this->file->close(); // This also remove the tmpdir directory
$this->file->close(); //print "tmpdir=".$tmpdir;
//print "filename=".$filename;
//print "tmpfile=".$tmpfile;
copy($filename, $this->tmpfile);
$tmp = tempnam($this->config['PATH_TO_TMP'], md5(uniqid()));
copy($filename, $tmp);
$this->tmpfile = $tmp;
$this->_moveRowSegments(); $this->_moveRowSegments();
} }
/** /**
* Assing a template variable * Assing a template variable
* *
@ -244,15 +269,15 @@ IMG;
*/ */
private function _save() private function _save()
{ {
$this->file->open($this->tmpfile); $res=$this->file->open($this->tmpfile);
$this->_parse(); $this->_parse();
if (! $this->file->addFromString('content.xml', $this->contentXml)) { if (! $this->file->addFromString('content.xml', $this->contentXml)) {
throw new OdfException('Error during file export'); throw new OdfException('Error during file export addFromString');
} }
foreach ($this->images as $imageKey => $imageValue) { foreach ($this->images as $imageKey => $imageValue) {
$this->file->addFile($imageKey, 'Pictures/' . $imageValue); $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
} }
$this->file->close(); // seems to bug on windows CLI sometimes $this->file->close();
} }
/** /**
* Export the file as attached file by HTTP * Export the file as attached file by HTTP
@ -298,13 +323,42 @@ IMG;
{ {
return $this->tmpfile; return $this->tmpfile;
} }
/** /**
* Delete the temporary file when the object is destroyed * Delete the temporary file when the object is destroyed
*/ */
public function __destruct() { public function __destruct()
{
if (file_exists($this->tmpfile)) { if (file_exists($this->tmpfile)) {
unlink($this->tmpfile); unlink($this->tmpfile);
} }
if (file_exists($this->tmpdir)) {
$this->_rrmdir($this->tmpdir);
rmdir($this->tmpdir);
}
}
/**
* Empty the temporary working directory recursively
* @param $dir the temporary working directory
* @return void
*/
private function _rrmdir($dir)
{
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
if (is_dir($dir . '/' . $file)) {
$this->_rrmdir($dir . '/' . $file);
rmdir($dir . '/' . $file);
} else {
unlink($dir . '/' . $file);
}
}
}
closedir($handle);
}
} }
} }

View File

@ -14,12 +14,13 @@ class PclZipProxyException extends Exception
* Id : $Id$ * Id : $Id$
* *
* @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com) * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
* @copyright GPL License 2010 - Laurent Destailleur - eldy@users.sourceforge.net
* @license http://www.gnu.org/copyleft/gpl.html GPL License * @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 1.3 * @version 1.4
*/ */
class PclZipProxy implements ZipInterface class PclZipProxy implements ZipInterface
{ {
const TMP_DIR = '/tmp'; protected $tmpdir = '/tmp';
protected $openned = false; protected $openned = false;
protected $filename; protected $filename;
protected $pclzip; protected $pclzip;
@ -28,13 +29,15 @@ class PclZipProxy implements ZipInterface
* *
* @throws PclZipProxyException * @throws PclZipProxyException
*/ */
public function __construct() public function __construct($forcedir='')
{ {
if (! class_exists('PclZip')) { if (! class_exists('PclZip')) {
throw new PclZipProxyException('PclZip class not loaded - PclZip library throw new PclZipProxyException('PclZip class not loaded - PclZip library
is required for using PclZipProxy'); ; is required for using PclZipProxy'); ;
} }
if ($forcedir) $this->tmpdir=preg_replace('|[//\/]$|','',$forcedir); // $this->tmpdir must not contains / at the end
} }
/** /**
* Open a Zip archive * Open a Zip archive
* *
@ -46,14 +49,12 @@ class PclZipProxy implements ZipInterface
if (true === $this->openned) { if (true === $this->openned) {
$this->close(); $this->close();
} }
if (!file_exists(self::TMP_DIR)) {
mkdir(self::TMP_DIR);
}
$this->filename = $filename; $this->filename = $filename;
$this->pclzip = new PclZip($this->filename); $this->pclzip = new PclZip($this->filename);
$this->openned = true; $this->openned = true;
return true; return true;
} }
/** /**
* Retrieve the content of a file within the archive from its name * Retrieve the content of a file within the archive from its name
* *
@ -73,6 +74,7 @@ class PclZipProxy implements ZipInterface
} }
return false; return false;
} }
/** /**
* Add a file within the archive from a string * Add a file within the archive from a string
* *
@ -90,11 +92,13 @@ class PclZipProxy implements ZipInterface
} }
$localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname); $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
$localpath = dirname($localname); $localpath = dirname($localname);
$tmpfilename = self::TMP_DIR . '/' . basename($localname); $tmpfilename = $this->tmpdir . '/' . basename($localname);
if (false !== file_put_contents($tmpfilename, $contents)) { if (false !== file_put_contents($tmpfilename, $contents)) {
$this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname); //print "tmpfilename=".$tmpfilename;
//print "localname=".$localname;
$res=$this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
$add = $this->pclzip->add($tmpfilename, $add = $this->pclzip->add($tmpfilename,
PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR, PCLZIP_OPT_REMOVE_PATH, $this->tmpdir,
PCLZIP_OPT_ADD_PATH, $localpath); PCLZIP_OPT_ADD_PATH, $localpath);
unlink($tmpfilename); unlink($tmpfilename);
if (!empty($add)) { if (!empty($add)) {
@ -103,6 +107,7 @@ class PclZipProxy implements ZipInterface
} }
return false; return false;
} }
/** /**
* Add a file within the archive from a file * Add a file within the archive from a file
* *
@ -122,23 +127,24 @@ class PclZipProxy implements ZipInterface
if (isSet($localname)) { if (isSet($localname)) {
$localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname); $localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
$localpath = dirname($localname); $localpath = dirname($localname);
$tmpfilename = self::TMP_DIR . '/' . basename($localname); $tmpfilename = $this->tmpdir . '/' . basename($localname);
} else { } else {
$localname = basename($filename); $localname = basename($filename);
$tmpfilename = self::TMP_DIR . '/' . $localname; $tmpfilename = $this->tmpdir . '/' . $localname;
$localpath = ''; $localpath = '';
} }
if (file_exists($filename)) { if (file_exists($filename)) {
copy($filename, $tmpfilename); copy($filename, $tmpfilename);
$this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname); $this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
$this->pclzip->add($tmpfilename, $this->pclzip->add($tmpfilename,
PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR, PCLZIP_OPT_REMOVE_PATH, $this->tmpdir,
PCLZIP_OPT_ADD_PATH, $localpath); PCLZIP_OPT_ADD_PATH, $localpath);
unlink($tmpfilename); unlink($tmpfilename);
return true; return true;
} }
return false; return false;
} }
/** /**
* Close the Zip archive * Close the Zip archive
* @return true * @return true
@ -150,33 +156,8 @@ class PclZipProxy implements ZipInterface
} }
$this->pclzip = $this->filename = null; $this->pclzip = $this->filename = null;
$this->openned = false; $this->openned = false;
if (file_exists(self::TMP_DIR)) {
$this->_rrmdir(self::TMP_DIR);
rmdir(self::TMP_DIR);
}
return true; return true;
} }
/**
* Empty the temporary working directory recursively
* @param $dir the temporary working directory
* @return void
*/
private function _rrmdir($dir)
{
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
if (is_dir($dir . '/' . $file)) {
$this->_rrmdir($dir . '/' . $file);
rmdir($dir . '/' . $file);
} else {
unlink($dir . '/' . $file);
}
}
}
closedir($handle);
}
}
} }
?> ?>