]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
- $reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
- $this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
- return $this;
- }
- /**
- * Display all the current template variables
- *
- * @return string
- */
- public function printVars()
- {
- return print_r('' . print_r($this->vars, true) . '
', true);
- }
- /**
- * Display the XML content of the file from odt document
- * as it is at the moment
- *
- * @return string
- */
- public function __toString()
- {
- return $this->contentXml;
- }
- /**
- * Display loop segments declared with setSegment()
- *
- * @return string
- */
- public function printDeclaredSegments()
- {
- return '' . print_r(implode(' ', array_keys($this->segments)), true) . '';
- }
- /**
- * Declare a segment in order to use it in a loop
- *
- * @param string $segment
- * @throws OdfException
- * @return Segment
- */
- public function setSegment($segment)
- {
- if (array_key_exists($segment, $this->segments)) {
- return $this->segments[$segment];
- }
- // $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)\[!--\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) {
- throw new OdfException("'$segment' segment not found in the document");
- }
- $this->segments[$segment] = new Segment($segment, $m[1], $this);
- return $this->segments[$segment];
- }
- /**
- * Save the odt file on the disk
- *
- * @param string $file name of the desired file
- * @throws OdfException
- * @return void
- */
- public function saveToDisk($file = null)
- {
- if ($file !== null && is_string($file)) {
- if (file_exists($file) && !(is_file($file) && is_writable($file))) {
- throw new OdfException('Permission denied : can\'t create ' . $file);
- }
- $this->_save();
- copy($this->tmpfile, $file);
- } else {
- $this->_save();
- }
- }
- /**
- * Internal save
- *
- * @throws OdfException
- * @return void
- */
- private function _save()
- {
- $this->file->open($this->tmpfile);
- $this->_parse();
- if (! $this->file->addFromString('content.xml', $this->contentXml)) {
- throw new OdfException('Error during file export');
- }
- foreach ($this->images as $imageKey => $imageValue) {
- $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
- }
- $this->file->close(); // seems to bug on windows CLI sometimes
- }
- /**
- * Export the file as attached file by HTTP
- *
- * @param string $name (optionnal)
- * @throws OdfException
- * @return void
- */
- public function exportAsAttachedFile($name="")
- {
- $this->_save();
- if (headers_sent($filename, $linenum)) {
- throw new OdfException("headers already sent ($filename at $linenum)");
- }
+ /**
+ * Merge template variables
+ * Called automatically for a save
+ *
+ * @return void
+ */
+ private function _parse()
+ {
+ $this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml);
+ }
+ /**
+ * Add the merged segment to the document
+ *
+ * @param Segment $segment
+ * @throws OdfException
+ * @return odf
+ */
+ public function mergeSegment(Segment $segment)
+ {
+ if (! array_key_exists($segment->getName(), $this->segments)) {
+ throw new OdfException($segment->getName() . 'cannot be parsed, has it been set yet ?');
+ }
+ $string = $segment->getName();
+ // $reg = '@]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
+ $reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
+ $this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
+ return $this;
+ }
+ /**
+ * Display all the current template variables
+ *
+ * @return string
+ */
+ public function printVars()
+ {
+ return print_r('' . print_r($this->vars, true) . '
', true);
+ }
+ /**
+ * Display the XML content of the file from odt document
+ * as it is at the moment
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return $this->contentXml;
+ }
+ /**
+ * Display loop segments declared with setSegment()
+ *
+ * @return string
+ */
+ public function printDeclaredSegments()
+ {
+ return '' . print_r(implode(' ', array_keys($this->segments)), true) . '';
+ }
+ /**
+ * Declare a segment in order to use it in a loop
+ *
+ * @param string $segment
+ * @throws OdfException
+ * @return Segment
+ */
+ public function setSegment($segment)
+ {
+ if (array_key_exists($segment, $this->segments)) {
+ return $this->segments[$segment];
+ }
+ // $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)\[!--\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) {
+ throw new OdfException("'$segment' segment not found in the document");
+ }
+ $this->segments[$segment] = new Segment($segment, $m[1], $this);
+ return $this->segments[$segment];
+ }
+ /**
+ * Save the odt file on the disk
+ *
+ * @param string $file name of the desired file
+ * @throws OdfException
+ * @return void
+ */
+ public function saveToDisk($file = null)
+ {
+ if ($file !== null && is_string($file)) {
+ if (file_exists($file) && !(is_file($file) && is_writable($file))) {
+ throw new OdfException('Permission denied : can\'t create ' . $file);
+ }
+ $this->_save();
+ copy($this->tmpfile, $file);
+ } else {
+ $this->_save();
+ }
+ }
+ /**
+ * Internal save
+ *
+ * @throws OdfException
+ * @return void
+ */
+ private function _save()
+ {
+ $res=$this->file->open($this->tmpfile);
+ $this->_parse();
+ if (! $this->file->addFromString('content.xml', $this->contentXml)) {
+ throw new OdfException('Error during file export addFromString');
+ }
+ foreach ($this->images as $imageKey => $imageValue) {
+ $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
+ }
+ $this->file->close();
+ }
+ /**
+ * Export the file as attached file by HTTP
+ *
+ * @param string $name (optionnal)
+ * @throws OdfException
+ * @return void
+ */
+ public function exportAsAttachedFile($name="")
+ {
+ $this->_save();
+ if (headers_sent($filename, $linenum)) {
+ throw new OdfException("headers already sent ($filename at $linenum)");
+ }
- if( $name == "" )
- {
- $name = md5(uniqid()) . ".odt";
- }
+ if( $name == "" )
+ {
+ $name = md5(uniqid()) . ".odt";
+ }
- header('Content-type: application/vnd.oasis.opendocument.text');
- header('Content-Disposition: attachment; filename="'.$name.'"');
- readfile($this->tmpfile);
- }
- /**
- * Returns a variable of configuration
- *
- * @return string The requested variable of configuration
- */
- public function getConfig($configKey)
- {
- if (array_key_exists($configKey, $this->config)) {
- return $this->config[$configKey];
- }
- return false;
- }
- /**
- * Returns the temporary working file
- *
- * @return string le chemin vers le fichier temporaire de travail
- */
- public function getTmpfile()
- {
- return $this->tmpfile;
- }
- /**
- * Delete the temporary file when the object is destroyed
- */
- public function __destruct() {
- if (file_exists($this->tmpfile)) {
- unlink($this->tmpfile);
- }
- }
+ header('Content-type: application/vnd.oasis.opendocument.text');
+ header('Content-Disposition: attachment; filename="'.$name.'"');
+ readfile($this->tmpfile);
+ }
+ /**
+ * Returns a variable of configuration
+ *
+ * @return string The requested variable of configuration
+ */
+ public function getConfig($configKey)
+ {
+ if (array_key_exists($configKey, $this->config)) {
+ return $this->config[$configKey];
+ }
+ return false;
+ }
+ /**
+ * Returns the temporary working file
+ *
+ * @return string le chemin vers le fichier temporaire de travail
+ */
+ public function getTmpfile()
+ {
+ return $this->tmpfile;
+ }
+
+ /**
+ * Delete the temporary file when the object is destroyed
+ */
+ public function __destruct()
+ {
+ 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);
+ }
+ }
}
?>
\ No newline at end of file
diff --git a/htdocs/includes/odtphp/zip/PclZipProxy.php b/htdocs/includes/odtphp/zip/PclZipProxy.php
index 21072135e58..dbda9760f56 100644
--- a/htdocs/includes/odtphp/zip/PclZipProxy.php
+++ b/htdocs/includes/odtphp/zip/PclZipProxy.php
@@ -14,12 +14,13 @@ class PclZipProxyException extends Exception
* Id : $Id$
*
* @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
- * @version 1.3
+ * @version 1.4
*/
class PclZipProxy implements ZipInterface
{
- const TMP_DIR = '/tmp';
+ protected $tmpdir = '/tmp';
protected $openned = false;
protected $filename;
protected $pclzip;
@@ -28,58 +29,59 @@ class PclZipProxy implements ZipInterface
*
* @throws PclZipProxyException
*/
- public function __construct()
+ public function __construct($forcedir='')
{
if (! class_exists('PclZip')) {
throw new PclZipProxyException('PclZip class not loaded - PclZip library
is required for using PclZipProxy'); ;
}
- }
+ if ($forcedir) $this->tmpdir=preg_replace('|[//\/]$|','',$forcedir); // $this->tmpdir must not contains / at the end
+ }
+
/**
* Open a Zip archive
- *
+ *
* @param string $filename the name of the archive to open
* @return true if openning has succeeded
- */
+ */
public function open($filename)
{
if (true === $this->openned) {
$this->close();
- }
- if (!file_exists(self::TMP_DIR)) {
- mkdir(self::TMP_DIR);
}
$this->filename = $filename;
$this->pclzip = new PclZip($this->filename);
$this->openned = true;
return true;
}
+
/**
* Retrieve the content of a file within the archive from its name
- *
+ *
* @param string $name the name of the file to extract
* @return the content of the file in a string
- */
+ */
public function getFromName($name)
{
if (false === $this->openned) {
return false;
}
$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);
if (!empty($extraction)) {
return $extraction[0]['content'];
- }
+ }
return false;
}
+
/**
* Add a file within the archive from a string
- *
+ *
* @param string $localname the local path to the file in the archive
* @param string $contents the content of the file
* @return true if the file has been successful added
- */
+ */
public function addFromString($localname, $contents)
{
if (false === $this->openned) {
@@ -90,26 +92,29 @@ class PclZipProxy implements ZipInterface
}
$localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
$localpath = dirname($localname);
- $tmpfilename = self::TMP_DIR . '/' . basename($localname);
+ $tmpfilename = $this->tmpdir . '/' . basename($localname);
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,
- PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR,
+ PCLZIP_OPT_REMOVE_PATH, $this->tmpdir,
PCLZIP_OPT_ADD_PATH, $localpath);
unlink($tmpfilename);
if (!empty($add)) {
return true;
- }
+ }
}
return false;
}
+
/**
* Add a file within the archive from a file
- *
+ *
* @param string $filename the path to the file we want to add
* @param string $localname the local path to the file in the archive
* @return true if the file has been successful added
- */
+ */
public function addFile($filename, $localname = null)
{
if (false === $this->openned) {
@@ -118,65 +123,41 @@ class PclZipProxy implements ZipInterface
if ((file_exists($this->filename) && !is_writable($this->filename))
|| !file_exists($filename)) {
return false;
- }
+ }
if (isSet($localname)) {
$localname = preg_replace("/(?:\.|\/)*(.*)/", "\\1", $localname);
$localpath = dirname($localname);
- $tmpfilename = self::TMP_DIR . '/' . basename($localname);
+ $tmpfilename = $this->tmpdir . '/' . basename($localname);
} else {
$localname = basename($filename);
- $tmpfilename = self::TMP_DIR . '/' . $localname;
+ $tmpfilename = $this->tmpdir . '/' . $localname;
$localpath = '';
}
if (file_exists($filename)) {
copy($filename, $tmpfilename);
$this->pclzip->delete(PCLZIP_OPT_BY_NAME, $localname);
$this->pclzip->add($tmpfilename,
- PCLZIP_OPT_REMOVE_PATH, self::TMP_DIR,
+ PCLZIP_OPT_REMOVE_PATH, $this->tmpdir,
PCLZIP_OPT_ADD_PATH, $localpath);
unlink($tmpfilename);
return true;
}
return false;
}
+
/**
* Close the Zip archive
* @return true
- */
+ */
public function close()
{
if (false === $this->openned) {
return false;
- }
+ }
$this->pclzip = $this->filename = null;
$this->openned = false;
- if (file_exists(self::TMP_DIR)) {
- $this->_rrmdir(self::TMP_DIR);
- rmdir(self::TMP_DIR);
- }
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);
- }
- }
}
?>
\ No newline at end of file