2010-02-15 22:39:13 +00:00
< ? php
2024-12-16 10:12:42 +00:00
if ( ! defined ( 'ODTPHP_PATHTOPCLZIP' )) {
define ( 'ODTPHP_PATHTOPCLZIP' , DOL_DOCUMENT_ROOT . '/includes/odtphp/zip/pclzip/' );
}
2011-08-14 14:20:23 +00:00
require_once ODTPHP_PATHTOPCLZIP . 'pclzip.lib.php' ;
2010-02-15 22:39:13 +00:00
require_once 'ZipInterface.php' ;
class PclZipProxyException extends Exception
{ }
/**
* Proxy class for the PclZip library
* You need PHP 5.2 at least
* You need Zip Extension or PclZip library
* Encoding : ISO - 8859 - 1
*
* @ copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska ( http :// www . anaska . com )
2010-03-13 16:05:36 +00:00
* @ copyright GPL License 2010 - Laurent Destailleur - eldy @ users . sourceforge . net
2019-09-23 19:55:30 +00:00
* @ license https :// www . gnu . org / copyleft / gpl . html GPL License
2010-03-13 16:05:36 +00:00
* @ version 1.4
2010-02-15 22:39:13 +00:00
*/
class PclZipProxy implements ZipInterface
{
2010-03-13 16:05:36 +00:00
protected $tmpdir = '/tmp' ;
2010-02-15 22:39:13 +00:00
protected $openned = false ;
protected $filename ;
protected $pclzip ;
/**
* Class constructor
*
* @ throws PclZipProxyException
*/
2010-03-13 16:05:36 +00:00
public function __construct ( $forcedir = '' )
2010-02-15 22:39:13 +00:00
{
if ( ! class_exists ( 'PclZip' )) {
throw new PclZipProxyException ( ' PclZip class not loaded - PclZip library
is required for using PclZipProxy ' ); ;
}
2010-03-13 16:05:36 +00:00
if ( $forcedir ) $this -> tmpdir = preg_replace ( '|[//\/]$|' , '' , $forcedir ); // $this->tmpdir must not contains / at the end
}
2010-02-15 22:39:13 +00:00
/**
* Open a Zip archive
2010-03-13 16:05:36 +00:00
*
2010-02-15 22:39:13 +00:00
* @ param string $filename the name of the archive to open
2023-12-22 14:22:40 +00:00
* @ return bool true if openning has succeeded
2010-03-13 16:05:36 +00:00
*/
2010-02-15 22:39:13 +00:00
public function open ( $filename )
{
if ( true === $this -> openned ) {
$this -> close ();
}
$this -> filename = $filename ;
$this -> pclzip = new PclZip ( $this -> filename );
$this -> openned = true ;
return true ;
}
2010-03-13 16:05:36 +00:00
2010-02-15 22:39:13 +00:00
/**
* Retrieve the content of a file within the archive from its name
2010-03-13 16:05:36 +00:00
*
2010-02-15 22:39:13 +00:00
* @ param string $name the name of the file to extract
2023-12-22 14:22:40 +00:00
* @ return string the content of the file in a string
2010-03-13 16:05:36 +00:00
*/
2010-02-15 22:39:13 +00:00
public function getFromName ( $name )
{
if ( false === $this -> openned ) {
return false ;
}
$name = preg_replace ( " /(?: \ .| \ /)*(.*)/ " , " \\ 1 " , $name );
2010-03-13 16:05:36 +00:00
$extraction = $this -> pclzip -> extract ( PCLZIP_OPT_BY_NAME , $name ,
2010-02-15 22:39:13 +00:00
PCLZIP_OPT_EXTRACT_AS_STRING );
if ( ! empty ( $extraction )) {
return $extraction [ 0 ][ 'content' ];
2010-03-13 16:05:36 +00:00
}
2010-02-15 22:39:13 +00:00
return false ;
}
2010-03-13 16:05:36 +00:00
2010-02-15 22:39:13 +00:00
/**
* Add a file within the archive from a string
2010-03-13 16:05:36 +00:00
*
2010-02-15 22:39:13 +00:00
* @ param string $localname the local path to the file in the archive
* @ param string $contents the content of the file
2023-12-22 14:22:40 +00:00
* @ return bool true if the file has been successful added
2010-03-13 16:05:36 +00:00
*/
2010-02-15 22:39:13 +00:00
public function addFromString ( $localname , $contents )
{
if ( false === $this -> openned ) {
return false ;
}
if ( file_exists ( $this -> filename ) && ! is_writable ( $this -> filename )) {
return false ;
}
2025-05-09 13:39:07 +00:00
2010-02-15 22:39:13 +00:00
$localname = preg_replace ( " /(?: \ .| \ /)*(.*)/ " , " \\ 1 " , $localname );
$localpath = dirname ( $localname );
2010-03-13 16:05:36 +00:00
$tmpfilename = $this -> tmpdir . '/' . basename ( $localname );
2010-02-15 22:39:13 +00:00
if ( false !== file_put_contents ( $tmpfilename , $contents )) {
2010-03-13 16:05:36 +00:00
//print "tmpfilename=".$tmpfilename;
//print "localname=".$localname;
2025-05-09 13:39:07 +00:00
$res = $this -> pclzip -> delete ( PCLZIP_OPT_BY_NAME , $localname ); // after this with pclzip handler, the odt/zip file is ocrrupted. No way to edit the zip with file roller.
2010-02-15 22:39:13 +00:00
$add = $this -> pclzip -> add ( $tmpfilename ,
2010-03-13 16:05:36 +00:00
PCLZIP_OPT_REMOVE_PATH , $this -> tmpdir ,
2010-02-15 22:39:13 +00:00
PCLZIP_OPT_ADD_PATH , $localpath );
unlink ( $tmpfilename );
if ( ! empty ( $add )) {
return true ;
2010-03-13 16:05:36 +00:00
}
2010-02-15 22:39:13 +00:00
}
return false ;
}
2010-03-13 16:05:36 +00:00
2010-02-15 22:39:13 +00:00
/**
* Add a file within the archive from a file
2010-03-13 16:05:36 +00:00
*
2010-02-15 22:39:13 +00:00
* @ param string $filename the path to the file we want to add
* @ param string $localname the local path to the file in the archive
2023-12-22 14:22:40 +00:00
* @ return bool true if the file has been successful added
2010-03-13 16:05:36 +00:00
*/
2010-02-15 22:39:13 +00:00
public function addFile ( $filename , $localname = null )
{
if ( false === $this -> openned ) {
return false ;
}
if (( file_exists ( $this -> filename ) && ! is_writable ( $this -> filename ))
|| ! file_exists ( $filename )) {
return false ;
2010-03-13 16:05:36 +00:00
}
2010-02-15 22:39:13 +00:00
if ( isSet ( $localname )) {
$localname = preg_replace ( " /(?: \ .| \ /)*(.*)/ " , " \\ 1 " , $localname );
$localpath = dirname ( $localname );
2010-03-13 16:05:36 +00:00
$tmpfilename = $this -> tmpdir . '/' . basename ( $localname );
2010-02-15 22:39:13 +00:00
} else {
$localname = basename ( $filename );
2010-03-13 16:05:36 +00:00
$tmpfilename = $this -> tmpdir . '/' . $localname ;
2010-02-15 22:39:13 +00:00
$localpath = '' ;
}
if ( file_exists ( $filename )) {
copy ( $filename , $tmpfilename );
$this -> pclzip -> delete ( PCLZIP_OPT_BY_NAME , $localname );
$this -> pclzip -> add ( $tmpfilename ,
2010-03-13 16:05:36 +00:00
PCLZIP_OPT_REMOVE_PATH , $this -> tmpdir ,
2010-02-15 22:39:13 +00:00
PCLZIP_OPT_ADD_PATH , $localpath );
unlink ( $tmpfilename );
return true ;
}
return false ;
}
2010-03-13 16:05:36 +00:00
2010-02-15 22:39:13 +00:00
/**
* Close the Zip archive
2023-12-22 14:22:40 +00:00
* @ return bool true
2010-03-13 16:05:36 +00:00
*/
2010-02-15 22:39:13 +00:00
public function close ()
{
if ( false === $this -> openned ) {
return false ;
2010-03-13 16:05:36 +00:00
}
2010-02-15 22:39:13 +00:00
$this -> pclzip = $this -> filename = null ;
$this -> openned = false ;
return true ;
}
}