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,297 +13,353 @@ 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 $images = array(); protected $tmpdir;
protected $vars = array(); protected $images = array();
protected $segments = array(); protected $vars = array();
const PIXEL_TO_CM = 0.026458333; protected $segments = array();
/** const PIXEL_TO_CM = 0.026458333;
* Class constructor /**
* * Class constructor
* @param string $filename the name of the odt file *
* @throws OdfException * @param string $filename the name of the odt file
*/ * @throws OdfException
public function __construct($filename, $config = array()) */
{ public function __construct($filename, $config = array())
if (! is_array($config)) { {
throw new OdfException('Configuration data must be provided as array'); clearstatcache();
}
foreach ($config as $configKey => $configValue) {
if (array_key_exists($configKey, $this->config)) {
$this->config[$configKey] = $configValue;
}
}
if (! class_exists($this->config['ZIP_PROXY'])) {
throw new OdfException($this->config['ZIP_PROXY'] . ' class not found - check your php settings');
}
$zipHandler = $this->config['ZIP_PROXY'];
$this->file = new $zipHandler();
if ($this->file->open($filename) !== true) {
throw new OdfException("Error while Opening the file '$filename' - Check your odt file");
}
if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
}
$this->file->close(); if (! is_array($config)) {
throw new OdfException('Configuration data must be provided as array');
}
foreach ($config as $configKey => $configValue) {
if (array_key_exists($configKey, $this->config)) {
$this->config[$configKey] = $configValue;
}
}
$tmp = tempnam($this->config['PATH_TO_TMP'], md5(uniqid())); $md5uniqid = md5(uniqid());
copy($filename, $tmp); if ($this->config['PATH_TO_TMP']) $this->tmpdir = preg_replace('|[\/]$|','',$this->config['PATH_TO_TMP']);
$this->tmpfile = $tmp; $this->tmpdir .= ($this->tmpdir?'/':'').$md5uniqid;
$this->_moveRowSegments(); $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
* Assing a template variable 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');
* @param string $key name of the variable within the template }
* @param string $value replacement value
* @param bool $encode if true, special XML characters are encoded // Create tmp direcoty (will be deleted in destructor)
* @throws OdfException if (!file_exists($this->tmpdir)) {
* @return odf $result=mkdir($this->tmpdir);
*/ }
public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
{ // Load zip proxy
if (strpos($this->contentXml, $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']) === false) { $zipHandler = $this->config['ZIP_PROXY'];
throw new OdfException("var $key not found in the document"); define('PCLZIP_TEMPORARY_DIR',$this->tmpdir);
} include_once('zip/'.$zipHandler.'.php');
$value = $encode ? htmlspecialchars($value) : $value; if (! class_exists($this->config['ZIP_PROXY'])) {
$value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value; throw new OdfException($this->config['ZIP_PROXY'] . ' class not found - check your php settings');
$this->vars[$this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']] = str_replace("\n", "<text:line-break/>", $value); }
return $this; $this->file = new $zipHandler();
}
/** if ($this->file->open($filename) !== true) { // This also create the tmpdir directory
* Assign a template variable as a picture throw new OdfException("Error while Opening the file '$filename' - Check your odt file");
* }
* @param string $key name of the variable within the template if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
* @param string $value path to the picture throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
* @throws OdfException }
* @return odf $this->file->close(); // This also remove the tmpdir directory
*/
public function setImage($key, $value) //print "tmpdir=".$tmpdir;
{ //print "filename=".$filename;
$filename = strtok(strrchr($value, '/'), '/.'); //print "tmpfile=".$tmpfile;
$file = substr(strrchr($value, '/'), 1);
$size = @getimagesize($value); copy($filename, $this->tmpfile);
if ($size === false) {
throw new OdfException("Invalid image"); $this->_moveRowSegments();
} }
list ($width, $height) = $size;
$width *= self::PIXEL_TO_CM; /**
$height *= self::PIXEL_TO_CM; * Assing a template variable
$xml = <<<IMG *
<draw:frame draw:style-name="fr1" draw:name="$filename" text:anchor-type="char" svg:width="{$width}cm" svg:height="{$height}cm" draw:z-index="3"><draw:image xlink:href="Pictures/$file" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame> * @param string $key name of the variable within the template
* @param string $value replacement value
* @param bool $encode if true, special XML characters are encoded
* @throws OdfException
* @return odf
*/
public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
{
if (strpos($this->contentXml, $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']) === false) {
throw new OdfException("var $key not found in the document");
}
$value = $encode ? htmlspecialchars($value) : $value;
$value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
$this->vars[$this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']] = str_replace("\n", "<text:line-break/>", $value);
return $this;
}
/**
* Assign a template variable as a picture
*
* @param string $key name of the variable within the template
* @param string $value path to the picture
* @throws OdfException
* @return odf
*/
public function setImage($key, $value)
{
$filename = strtok(strrchr($value, '/'), '/.');
$file = substr(strrchr($value, '/'), 1);
$size = @getimagesize($value);
if ($size === false) {
throw new OdfException("Invalid image");
}
list ($width, $height) = $size;
$width *= self::PIXEL_TO_CM;
$height *= self::PIXEL_TO_CM;
$xml = <<<IMG
<draw:frame draw:style-name="fr1" draw:name="$filename" text:anchor-type="char" svg:width="{$width}cm" svg:height="{$height}cm" draw:z-index="3"><draw:image xlink:href="Pictures/$file" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame>
IMG; IMG;
$this->images[$value] = $file; $this->images[$value] = $file;
$this->setVars($key, $xml, false); $this->setVars($key, $xml, false);
return $this; return $this;
} }
/** /**
* Move segment tags for lines of tables * Move segment tags for lines of tables
* Called automatically within the constructor * Called automatically within the constructor
* *
* @return void * @return void
*/ */
private function _moveRowSegments() private function _moveRowSegments()
{ {
// Search all possible rows in the document // Search all possible rows in the document
$reg1 = "#<table:table-row[^>]*>(.*)</table:table-row>#smU"; $reg1 = "#<table:table-row[^>]*>(.*)</table:table-row>#smU";
preg_match_all($reg1, $this->contentXml, $matches); preg_match_all($reg1, $this->contentXml, $matches);
for ($i = 0, $size = count($matches[0]); $i < $size; $i++) { for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
// Check if the current row contains a segment row.* // Check if the current row contains a segment row.*
$reg2 = '#\[!--\sBEGIN\s(row.[\S]*)\s--\](.*)\[!--\sEND\s\\1\s--\]#sm'; $reg2 = '#\[!--\sBEGIN\s(row.[\S]*)\s--\](.*)\[!--\sEND\s\\1\s--\]#sm';
if (preg_match($reg2, $matches[0][$i], $matches2)) { if (preg_match($reg2, $matches[0][$i], $matches2)) {
$balise = str_replace('row.', '', $matches2[1]); $balise = str_replace('row.', '', $matches2[1]);
// Move segment tags around the row // Move segment tags around the row
$replace = array( $replace = array(
'[!-- BEGIN ' . $matches2[1] . ' --]' => '', '[!-- BEGIN ' . $matches2[1] . ' --]' => '',
'[!-- END ' . $matches2[1] . ' --]' => '', '[!-- END ' . $matches2[1] . ' --]' => '',
'<table:table-row' => '[!-- BEGIN ' . $balise . ' --]<table:table-row', '<table:table-row' => '[!-- BEGIN ' . $balise . ' --]<table:table-row',
'</table:table-row>' => '</table:table-row>[!-- END ' . $balise . ' --]' '</table:table-row>' => '</table:table-row>[!-- END ' . $balise . ' --]'
); );
$replacedXML = str_replace(array_keys($replace), array_values($replace), $matches[0][$i]); $replacedXML = str_replace(array_keys($replace), array_values($replace), $matches[0][$i]);
$this->contentXml = str_replace($matches[0][$i], $replacedXML, $this->contentXml); $this->contentXml = str_replace($matches[0][$i], $replacedXML, $this->contentXml);
}
} }
} }
} /**
/** * Merge template variables
* Merge template variables * Called automatically for a save
* Called automatically for a save *
* * @return void
* @return void */
*/ private function _parse()
private function _parse() {
{ $this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml);
$this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml); }
} /**
/** * Add the merged segment to the document
* Add the merged segment to the document *
* * @param Segment $segment
* @param Segment $segment * @throws OdfException
* @throws OdfException * @return odf
* @return odf */
*/ public function mergeSegment(Segment $segment)
public function mergeSegment(Segment $segment) {
{ if (! array_key_exists($segment->getName(), $this->segments)) {
if (! array_key_exists($segment->getName(), $this->segments)) { throw new OdfException($segment->getName() . 'cannot be parsed, has it been set yet ?');
throw new OdfException($segment->getName() . 'cannot be parsed, has it been set yet ?'); }
} $string = $segment->getName();
$string = $segment->getName(); // $reg = '@<text:p[^>]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
// $reg = '@<text:p[^>]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU'; $reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
$reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU'; $this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
$this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml); return $this;
return $this; }
} /**
/** * Display all the current template variables
* Display all the current template variables *
* * @return string
* @return string */
*/ public function printVars()
public function printVars() {
{ return print_r('<pre>' . print_r($this->vars, true) . '</pre>', true);
return print_r('<pre>' . print_r($this->vars, true) . '</pre>', true); }
} /**
/** * Display the XML content of the file from odt document
* Display the XML content of the file from odt document * as it is at the moment
* as it is at the moment *
* * @return string
* @return string */
*/ public function __toString()
public function __toString() {
{ return $this->contentXml;
return $this->contentXml; }
} /**
/** * Display loop segments declared with setSegment()
* Display loop segments declared with setSegment() *
* * @return string
* @return string */
*/ public function printDeclaredSegments()
public function printDeclaredSegments() {
{ return '<pre>' . print_r(implode(' ', array_keys($this->segments)), true) . '</pre>';
return '<pre>' . print_r(implode(' ', array_keys($this->segments)), true) . '</pre>'; }
} /**
/** * Declare a segment in order to use it in a loop
* Declare a segment in order to use it in a loop *
* * @param string $segment
* @param string $segment * @throws OdfException
* @throws OdfException * @return Segment
* @return Segment */
*/ public function setSegment($segment)
public function setSegment($segment) {
{ if (array_key_exists($segment, $this->segments)) {
if (array_key_exists($segment, $this->segments)) { return $this->segments[$segment];
return $this->segments[$segment]; }
} // $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)<text:p\s.*>\[!--\sEND\s$segment\s--\]#sm";
// $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)<text:p\s.*>\[!--\sEND\s$segment\s--\]#sm"; $reg = "#\[!--\sBEGIN\s$segment\s--\](.*)\[!--\sEND\s$segment\s--\]#sm";
$reg = "#\[!--\sBEGIN\s$segment\s--\](.*)\[!--\sEND\s$segment\s--\]#sm"; if (preg_match($reg, html_entity_decode($this->contentXml), $m) == 0) {
if (preg_match($reg, html_entity_decode($this->contentXml), $m) == 0) { throw new OdfException("'$segment' segment not found in the document");
throw new OdfException("'$segment' segment not found in the document"); }
} $this->segments[$segment] = new Segment($segment, $m[1], $this);
$this->segments[$segment] = new Segment($segment, $m[1], $this); return $this->segments[$segment];
return $this->segments[$segment]; }
} /**
/** * Save the odt file on the disk
* Save the odt file on the disk *
* * @param string $file name of the desired file
* @param string $file name of the desired file * @throws OdfException
* @throws OdfException * @return void
* @return void */
*/ public function saveToDisk($file = null)
public function saveToDisk($file = null) {
{ if ($file !== null && is_string($file)) {
if ($file !== null && is_string($file)) { if (file_exists($file) && !(is_file($file) && is_writable($file))) {
if (file_exists($file) && !(is_file($file) && is_writable($file))) { throw new OdfException('Permission denied : can\'t create ' . $file);
throw new OdfException('Permission denied : can\'t create ' . $file); }
} $this->_save();
$this->_save(); copy($this->tmpfile, $file);
copy($this->tmpfile, $file); } else {
} else { $this->_save();
$this->_save(); }
} }
} /**
/** * Internal save
* Internal save *
* * @throws OdfException
* @throws OdfException * @return void
* @return void */
*/ private function _save()
private function _save() {
{ $res=$this->file->open($this->tmpfile);
$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 addFromString');
throw new OdfException('Error during file export'); }
} 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();
$this->file->close(); // seems to bug on windows CLI sometimes }
} /**
/** * Export the file as attached file by HTTP
* Export the file as attached file by HTTP *
* * @param string $name (optionnal)
* @param string $name (optionnal) * @throws OdfException
* @throws OdfException * @return void
* @return void */
*/ public function exportAsAttachedFile($name="")
public function exportAsAttachedFile($name="") {
{ $this->_save();
$this->_save(); if (headers_sent($filename, $linenum)) {
if (headers_sent($filename, $linenum)) { throw new OdfException("headers already sent ($filename at $linenum)");
throw new OdfException("headers already sent ($filename at $linenum)"); }
}
if( $name == "" ) if( $name == "" )
{ {
$name = md5(uniqid()) . ".odt"; $name = md5(uniqid()) . ".odt";
} }
header('Content-type: application/vnd.oasis.opendocument.text'); header('Content-type: application/vnd.oasis.opendocument.text');
header('Content-Disposition: attachment; filename="'.$name.'"'); header('Content-Disposition: attachment; filename="'.$name.'"');
readfile($this->tmpfile); readfile($this->tmpfile);
} }
/** /**
* Returns a variable of configuration * Returns a variable of configuration
* *
* @return string The requested variable of configuration * @return string The requested variable of configuration
*/ */
public function getConfig($configKey) public function getConfig($configKey)
{ {
if (array_key_exists($configKey, $this->config)) { if (array_key_exists($configKey, $this->config)) {
return $this->config[$configKey]; return $this->config[$configKey];
} }
return false; return false;
} }
/** /**
* Returns the temporary working file * Returns the temporary working file
* *
* @return string le chemin vers le fichier temporaire de travail * @return string le chemin vers le fichier temporaire de travail
*/ */
public function getTmpfile() public function getTmpfile()
{ {
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() { */
if (file_exists($this->tmpfile)) { public function __destruct()
unlink($this->tmpfile); {
} if (file_exists($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,58 +29,59 @@ 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
* *
* @param string $filename the name of the archive to open * @param string $filename the name of the archive to open
* @return true if openning has succeeded * @return true if openning has succeeded
*/ */
public function open($filename) public function open($filename)
{ {
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
* *
* @param string $name the name of the file to extract * @param string $name the name of the file to extract
* @return the content of the file in a string * @return the content of the file in a string
*/ */
public function getFromName($name) public function getFromName($name)
{ {
if (false === $this->openned) { if (false === $this->openned) {
return false; return false;
} }
$name = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $name); $name = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $name);
$extraction = $this->pclzip->extract(PCLZIP_OPT_BY_NAME, $name, $extraction = $this->pclzip->extract(PCLZIP_OPT_BY_NAME, $name,
PCLZIP_OPT_EXTRACT_AS_STRING); PCLZIP_OPT_EXTRACT_AS_STRING);
if (!empty($extraction)) { if (!empty($extraction)) {
return $extraction[0]['content']; return $extraction[0]['content'];
} }
return false; return false;
} }
/** /**
* Add a file within the archive from a string * Add a file within the archive from a string
* *
* @param string $localname the local path to the file in the archive * @param string $localname the local path to the file in the archive
* @param string $contents the content of the file * @param string $contents the content of the file
* @return true if the file has been successful added * @return true if the file has been successful added
*/ */
public function addFromString($localname, $contents) public function addFromString($localname, $contents)
{ {
if (false === $this->openned) { if (false === $this->openned) {
@ -90,26 +92,29 @@ 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)) {
return true; return true;
} }
} }
return false; return false;
} }
/** /**
* Add a file within the archive from a file * Add a file within the archive from a file
* *
* @param string $filename the path to the file we want to add * @param string $filename the path to the file we want to add
* @param string $localname the local path to the file in the archive * @param string $localname the local path to the file in the archive
* @return true if the file has been successful added * @return true if the file has been successful added
*/ */
public function addFile($filename, $localname = null) public function addFile($filename, $localname = null)
{ {
if (false === $this->openned) { if (false === $this->openned) {
@ -118,65 +123,41 @@ class PclZipProxy implements ZipInterface
if ((file_exists($this->filename) && !is_writable($this->filename)) if ((file_exists($this->filename) && !is_writable($this->filename))
|| !file_exists($filename)) { || !file_exists($filename)) {
return false; return false;
} }
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
*/ */
public function close() public function close()
{ {
if (false === $this->openned) { if (false === $this->openned) {
return false; return false;
} }
$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);
}
}
} }
?> ?>