/administrator/includes/pcl/pcltar.lib.php
PHP | 3576 lines | 1996 code | 511 blank | 1069 comment | 619 complexity | ac1f83c707bc3fd3daab475c82349cae MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * @version $Id: pcltar.lib.php 47 2005-09-15 02:55:27Z rhuk $
- * @package Joomla
- */
- // no direct access
- defined( '_VALID_MOS' ) or die( 'Restricted access' );
- // --------------------------------------------------------------------------------
- // PhpConcept Library - Tar Module 1.3
- // --------------------------------------------------------------------------------
- // License GNU/GPL - Vincent Blavet - August 2001
- // http://www.phpconcept.net
- // --------------------------------------------------------------------------------
- //
- // Presentation :
- // PclTar is a library that allow you to create a GNU TAR + GNU ZIP archive,
- // to add files or directories, to extract all the archive or a part of it.
- // So far tests show that the files generated by PclTar are readable by
- // gzip tools and WinZip application.
- //
- // Description :
- // See readme.txt (English & Fran�ais) and http://www.phpconcept.net
- //
- // Warning :
- // This library and the associated files are non commercial, non professional
- // work.
- // It should not have unexpected results. However if any damage is caused by
- // this software the author can not be responsible.
- // The use of this software is at the risk of the user.
- //
- // --------------------------------------------------------------------------------
- // ----- Look for double include
- if (!defined("PCL_TAR"))
- {
- define( "PCL_TAR", 1 );
- // ----- Configuration variable
- // Theses values may be changed by the user of PclTar library
- if (!isset($g_pcltar_lib_dir))
- $g_pcltar_lib_dir = "lib";
- // ----- Error codes
- // -1 : Unable to open file in binary write mode
- // -2 : Unable to open file in binary read mode
- // -3 : Invalid parameters
- // -4 : File does not exist
- // -5 : Filename is too long (max. 99)
- // -6 : Not a valid tar file
- // -7 : Invalid extracted file size
- // -8 : Unable to create directory
- // -9 : Invalid archive extension
- // -10 : Invalid archive format
- // -11 : Unable to delete file (unlink)
- // -12 : Unable to rename file (rename)
- // -13 : Invalid header checksum
- // --------------------------------------------------------------------------------
- // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
- // --------------------------------------------------------------------------------
- // ----- Global variables
- $g_pcltar_version = "1.3";
- // ----- Extract extension type (.php3/.php/...)
- // $g_pcltar_extension = substr(strrchr(basename($PATH_TRANSLATED), '.'), 1);
- $g_pcltar_extension = substr(strrchr(basename(@$_SERVER["PATH_TRANSLATED"]), '.'), 1);
- // ----- Include other libraries
- // This library should be called by each script before the include of PhpZip
- // Library in order to limit the potential 'lib' directory path problem.
- if (!defined("PCLERROR_LIB"))
- {
- include($g_pcltar_lib_dir."/pclerror.lib.".$g_pcltar_extension);
- }
- if (!defined("PCLTRACE_LIB"))
- {
- include($g_pcltar_lib_dir."/pcltrace.lib.".$g_pcltar_extension);
- }
- // --------------------------------------------------------------------------------
- // Function : PclTarCreate()
- // Description :
- // Creates a new archive with name $p_tarname containing the files and/or
- // directories indicated in $p_list. If the tar filename extension is
- // ".tar", the file will not be compressed. If it is ".tar.gz" or ".tgz"
- // it will be a gzip compressed tar archive.
- // If you want to use an other extension, you must indicate the mode in
- // $p_mode ("tar" or "tgz").
- // $p_add_dir and $p_remove_dir give you the ability to store a path
- // which is not the real path of the files.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_filelist : An array containing file or directory names, or
- // a string containing one filename or directory name, or
- // a string containing a list of filenames and/or directory
- // names separated by spaces.
- // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive,
- // if $p_mode is not specified, it will be determined by the extension.
- // $p_add_dir : Path to add in the filename path archived
- // $p_remove_dir : Path to remove in the filename path archived
- // Return Values :
- // 1 on success, or an error code (see table at the beginning).
- // --------------------------------------------------------------------------------
- function PclTarCreate($p_tarname, $p_filelist="", $p_mode="", $p_add_dir="", $p_remove_dir="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarCreate", "tar=$p_tarname, file='$p_filelist', mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
- $v_result=1;
- // ----- Look for default mode
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- // ----- Extract the tar format from the extension
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Trace
- TrFctMessage(__FILE__, __LINE__, 1, "Auto mode selected : found $p_mode");
- }
- // ----- Look if the $p_filelist is really an array
- if (is_array($p_filelist))
- {
- // ----- Call the create fct
- $v_result = PclTarHandleCreate($p_tarname, $p_filelist, $p_mode, $p_add_dir, $p_remove_dir);
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_filelist))
- {
- // ----- Create a list with the elements from the string
- $v_list = explode(" ", $p_filelist);
- // ----- Call the create fct
- $v_result = PclTarHandleCreate($p_tarname, $v_list, $p_mode, $p_add_dir, $p_remove_dir);
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type p_filelist");
- $v_result = -3;
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarAdd()
- // Description :
- // PLEASE DO NOT USE ANY MORE THIS FUNCTION. Use PclTarAddList().
- //
- // This function is maintained only for compatibility reason
- //
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_filelist : An array containing file or directory names, or
- // a string containing one filename or directory name, or
- // a string containing a list of filenames and/or directory
- // names separated by spaces.
- // Return Values :
- // 1 on success,
- // Or an error code (see list on top).
- // --------------------------------------------------------------------------------
- function PclTarAdd($p_tarname, $p_filelist)
- {
- TrFctStart(__FILE__, __LINE__, "PclTarAdd", "tar=$p_tarname, file=$p_filelist");
- $v_result=1;
- $v_list_detail = array();
- // ----- Extract the tar format from the extension
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Look if the $p_filelist is really an array
- if (is_array($p_filelist))
- {
- // ----- Call the add fct
- $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $v_list_detail, "", "");
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_filelist))
- {
- // ----- Create a list with the elements from the string
- $v_list = explode(" ", $p_filelist);
- // ----- Call the add fct
- $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $v_list_detail, "", "");
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type p_filelist");
- $v_result = -3;
- }
- // ----- Cleaning
- unset($v_list_detail);
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarAddList()
- // Description :
- // Add a list of files or directories ($p_filelist) in the tar archive $p_tarname.
- // The list can be an array of file/directory names or a string with names
- // separated by one space.
- // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
- // different from the real path of the file. This is usefull if you want to have PclTar
- // running in any directory, and memorize relative path from an other directory.
- // If $p_mode is not set it will be automatically computed from the $p_tarname
- // extension (.tar, .tar.gz or .tgz).
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_filelist : An array containing file or directory names, or
- // a string containing one filename or directory name, or
- // a string containing a list of filenames and/or directory
- // names separated by spaces.
- // $p_add_dir : Path to add in the filename path archived
- // $p_remove_dir : Path to remove in the filename path archived
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // 1 on success,
- // Or an error code (see list on top).
- // --------------------------------------------------------------------------------
- function PclTarAddList($p_tarname, $p_filelist, $p_add_dir="", $p_remove_dir="", $p_mode="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarAddList", "tar=$p_tarname, file=$p_filelist, p_add_dir='$p_add_dir', p_remove_dir='$p_remove_dir', mode=$p_mode");
- $v_result=1;
- $p_list_detail = array();
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- }
- // ----- Look if the $p_filelist is really an array
- if (is_array($p_filelist))
- {
- // ----- Call the add fct
- $v_result = PclTarHandleAppend($p_tarname, $p_filelist, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_filelist))
- {
- // ----- Create a list with the elements from the string
- $v_list = explode(" ", $p_filelist);
- // ----- Call the add fct
- $v_result = PclTarHandleAppend($p_tarname, $v_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type p_filelist");
- $v_result = -3;
- }
- // ----- Return
- if ($v_result != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0);
- return 0;
- }
- TrFctEnd(__FILE__, __LINE__, $p_list_detail);
- return $p_list_detail;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarList()
- // Description :
- // Gives the list of all the files present in the tar archive $p_tarname.
- // The list is the function result, it will be 0 on error.
- // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
- // function will determine the type of the archive.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // 0 on error (Use PclErrorCode() and PclErrorString() for more info)
- // or
- // An array containing file properties. Each file properties is an array of
- // properties.
- // The properties (array field names) are :
- // filename, size, mode, uid, gid, mtime, typeflag, status
- // Exemple : $v_list = PclTarList("my.tar");
- // for ($i=0; $i<sizeof($v_list); $i++)
- // echo "Filename :'".$v_list[$i][filename]."'<br>";
- // --------------------------------------------------------------------------------
- function PclTarList($p_tarname, $p_mode="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarList", "tar=$p_tarname, mode='$p_mode'");
- $v_result=1;
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Call the extracting fct
- $p_list = array();
- if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "list", "", $p_mode, "")) != 1)
- {
- unset($p_list);
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $p_list);
- return $p_list;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarExtract()
- // Description :
- // Extract all the files present in the archive $p_tarname, in the directory
- // $p_path. The relative path of the archived files are keep and become
- // relative to $p_path.
- // If a file with the same name already exists it will be replaced.
- // If the path to the file does not exist, it will be created.
- // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
- // function will determine the type of the archive.
- // Parameters :
- // $p_tarname : Name of an existing tar file.
- // $p_path : Path where the files will be extracted. The files will use
- // their memorized path from $p_path.
- // If $p_path is "", files will be extracted in "./".
- // $p_remove_path : Path to remove (from the file memorized path) while writing the
- // extracted files. If the path does not match the file path,
- // the file is extracted with its memorized path.
- // $p_path and $p_remove_path are commulative.
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // Same as PclTarList()
- // --------------------------------------------------------------------------------
- function PclTarExtract($p_tarname, $p_path="./", $p_remove_path="", $p_mode="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarExtract", "tar='$p_tarname', path='$p_path', remove_path='$p_remove_path', mode='$p_mode'");
- $v_result=1;
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleExtract($p_tarname, 0, $p_list, "complete", $p_path, $p_mode, $p_remove_path)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $p_list);
- return $p_list;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarExtractList()
- // Description :
- // Extract the files present in the archive $p_tarname and specified in
- // $p_filelist, in the directory
- // $p_path. The relative path of the archived files are keep and become
- // relative to $p_path.
- // If a directory is sp�cified in the list, all the files from this directory
- // will be extracted.
- // If a file with the same name already exists it will be replaced.
- // If the path to the file does not exist, it will be created.
- // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
- // function will determine the type of the archive.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_filelist : An array containing file or directory names, or
- // a string containing one filename or directory name, or
- // a string containing a list of filenames and/or directory
- // names separated by spaces.
- // $p_path : Path where the files will be extracted. The files will use
- // their memorized path from $p_path.
- // If $p_path is "", files will be extracted in "./".
- // $p_remove_path : Path to remove (from the file memorized path) while writing the
- // extracted files. If the path does not match the file path,
- // the file is extracted with its memorized path.
- // $p_path and $p_remove_path are commulative.
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // Same as PclTarList()
- // --------------------------------------------------------------------------------
- function PclTarExtractList($p_tarname, $p_filelist, $p_path="./", $p_remove_path="", $p_mode="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarExtractList", "tar=$p_tarname, list, path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
- $v_result=1;
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Look if the $p_filelist is really an array
- if (is_array($p_filelist))
- {
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleExtract($p_tarname, $p_filelist, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_filelist))
- {
- // ----- Create a list with the elements from the string
- $v_list = explode(" ", $p_filelist);
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleExtract($p_tarname, $v_list, $p_list, "partial", $p_path, $v_tar_mode, $p_remove_path)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type p_filelist");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $p_list);
- return $p_list;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarExtractIndex()
- // Description :
- // Extract the files present in the archive $p_tarname and specified at
- // the indexes in $p_index, in the directory
- // $p_path. The relative path of the archived files are keep and become
- // relative to $p_path.
- // If a directory is specified in the list, the directory only is created. All
- // the file stored in this archive for this directory
- // are not extracted.
- // If a file with the same name already exists it will be replaced.
- // If the path to the file does not exist, it will be created.
- // Depending on the $p_tarname extension (.tar, .tar.gz or .tgz) the
- // function will determine the type of the archive.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_index : A single index (integer) or a string of indexes of files to
- // extract. The form of the string is "0,4-6,8-12" with only numbers
- // and '-' for range or ',' to separate ranges. No spaces or ';'
- // are allowed.
- // $p_path : Path where the files will be extracted. The files will use
- // their memorized path from $p_path.
- // If $p_path is "", files will be extracted in "./".
- // $p_remove_path : Path to remove (from the file memorized path) while writing the
- // extracted files. If the path does not match the file path,
- // the file is extracted with its memorized path.
- // $p_path and $p_remove_path are commulative.
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // Same as PclTarList()
- // --------------------------------------------------------------------------------
- function PclTarExtractIndex($p_tarname, $p_index, $p_path="./", $p_remove_path="", $p_mode="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarExtractIndex", "tar=$p_tarname, index='$p_index', path=$p_path, remove_path='$p_remove_path', mode='$p_mode'");
- $v_result=1;
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Look if the $p_index is really an integer
- if (is_integer($p_index))
- {
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleExtractByIndexList($p_tarname, "$p_index", $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_index))
- {
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleExtractByIndexList($p_tarname, $p_index, $p_list, $p_path, $p_remove_path, $v_tar_mode)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type $p_index");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $p_list);
- return $p_list;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarDelete()
- // Description :
- // This function deletes from the archive $p_tarname the files which are listed
- // in $p_filelist. $p_filelist can be a string with file names separated by
- // spaces, or an array containing the file names.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_filelist : An array or a string containing file names to remove from the
- // archive.
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // List of the files which are kept in the archive (same format as PclTarList())
- // --------------------------------------------------------------------------------
- function PclTarDelete($p_tarname, $p_filelist, $p_mode="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarDelete", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
- $v_result=1;
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Look if the $p_filelist is really an array
- if (is_array($p_filelist))
- {
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleDelete($p_tarname, $p_filelist, $p_list, $p_mode)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_filelist))
- {
- // ----- Create a list with the elements from the string
- $v_list = explode(" ", $p_filelist);
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleDelete($p_tarname, $v_list, $p_list, $p_mode)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type p_filelist");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $p_list);
- return $p_list;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarUpdate()
- // Description :
- // This function updates the files in $p_filelist which are already in the
- // $p_tarname archive with an older last modified date. If the file does not
- // exist, it is added at the end of the archive.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_filelist : An array or a string containing file names to update from the
- // archive.
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // Return Values :
- // List of the files contained in the archive. The field status contains
- // "updated", "not_updated", "added" or "ok" for the files not concerned.
- // --------------------------------------------------------------------------------
- function PclTarUpdate($p_tarname, $p_filelist, $p_mode="", $p_add_dir="", $p_remove_dir="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarUpdate", "tar='$p_tarname', list='$p_filelist', mode='$p_mode'");
- $v_result=1;
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Look if the $p_filelist is really an array
- if (is_array($p_filelist))
- {
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleUpdate($p_tarname, $p_filelist, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Look if the $p_filelist is a string
- else if (is_string($p_filelist))
- {
- // ----- Create a list with the elements from the string
- $v_list = explode(" ", $p_filelist);
- // ----- Call the extracting fct
- if (($v_result = PclTarHandleUpdate($p_tarname, $v_list, $p_list, $p_mode, $p_add_dir, $p_remove_dir)) != 1)
- {
- TrFctEnd(__FILE__, __LINE__, 0, PclErrorString());
- return(0);
- }
- }
- // ----- Invalid variable
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid variable type p_filelist");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $p_list);
- return $p_list;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarMerge()
- // Description :
- // This function add the content of $p_tarname_add at the end of $p_tarname.
- // Parameters :
- // $p_tarname : Name of an existing tar file
- // $p_tarname_add : Name of an existing tar file taht will be added at the end
- // of $p_tarname.
- // $p_mode : 'tar' or 'tgz', if not set, will be determined by $p_tarname extension
- // $p_mode_add : 'tar' or 'tgz', if not set, will be determined by $p_tarname_add
- // extension
- // Return Values :
- // List of the files contained in the archive. The field status contains
- // "updated", "not_updated", "added" or "ok" for the files not concerned.
- // --------------------------------------------------------------------------------
- function PclTarMerge($p_tarname, $p_tarname_add, $p_mode="", $p_mode_add="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarMerge", "tar='$p_tarname', tar_add='$p_tarname_add', mode='$p_mode', mode_add='$p_mode_add'");
- $v_result=1;
- // ----- Check the parameters
- if (($p_tarname == "") || ($p_tarname_add == ""))
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid empty archive name");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Extract the tar format from the extension
- if (($p_mode == "") || (($p_mode!="tar") && ($p_mode!="tgz")))
- {
- if (($p_mode = PclTarHandleExtension($p_tarname)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- if (($p_mode_add == "") || (($p_mode_add!="tar") && ($p_mode_add!="tgz")))
- {
- if (($p_mode_add = PclTarHandleExtension($p_tarname_add)) == "")
- {
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return 0;
- }
- }
- // ----- Clear filecache
- clearstatcache();
- // ----- Check the file size
- if ((!is_file($p_tarname)) ||
- (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
- {
- // ----- Error log
- if (!is_file($p_tarname))
- PclErrorLog(-4, "Archive '$p_tarname' does not exist");
- else
- PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- if ((!is_file($p_tarname_add)) ||
- (((($v_size_add = filesize($p_tarname_add)) % 512) != 0) && ($p_mode_add=="tar")))
- {
- // ----- Error log
- if (!is_file($p_tarname_add))
- PclErrorLog(-4, "Archive '$p_tarname_add' does not exist");
- else
- PclErrorLog(-6, "Archive '$p_tarname_add' has invalid size ".filesize($p_tarname_add)."(not a 512 block multiple)");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Look for compressed archive
- if ($p_mode == "tgz")
- {
- // ----- Open the file in read mode
- if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
- {
- // ----- Error log
- PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Open a temporary file in write mode
- $v_temp_tarname = uniqid("pcltar-").".tmp";
- TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
- if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
- {
- // ----- Close tar file
- gzclose($p_tar);
- // ----- Error log
- PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Read the first 512 bytes block
- $v_buffer = gzread($p_tar, 512);
- // ----- Read the following blocks but not the last one
- if (!gzeof($p_tar))
- {
- TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
- $i=1;
- // ----- Read new 512 block and write the already read
- do{
- // ----- Write the already read block
- $v_binary_data = pack("a512", "$v_buffer");
- gzputs($v_temp_tar, $v_binary_data);
- $i++;
- TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
- // ----- Read next block
- $v_buffer = gzread($p_tar, 512);
- } while (!gzeof($p_tar));
- TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
- }
- }
- // ----- Look for uncompressed tar file
- else if ($p_mode=="tar")
- {
- // ----- Open the tar file
- if (($p_tar = fopen($p_tarname, "r+b")) == 0)
- {
- // ----- Error log
- PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Go to the beginning of last block
- TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
- fseek($p_tar, $v_size-512);
- TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
- }
- // ----- Look for unknown type
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid tar mode $p_mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Look for type of archive to add
- if ($p_mode_add == "tgz")
- {
- TrFctMessage(__FILE__, __LINE__, 4, "Opening file $p_tarname_add");
- // ----- Open the file in read mode
- if (($p_tar_add = @gzopen($p_tarname_add, "rb")) == 0)
- {
- // ----- Error log
- PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Read the first 512 bytes block
- $v_buffer = gzread($p_tar_add, 512);
- // ----- Read the following blocks but not the last one
- if (!gzeof($p_tar_add))
- {
- TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
- $i=1;
- // ----- Read new 512 block and write the already read
- do{
- // ----- Write the already read block
- $v_binary_data = pack("a512", "$v_buffer");
- if ($p_mode=="tar")
- fputs($p_tar, $v_binary_data);
- else
- gzputs($v_temp_tar, $v_binary_data);
- $i++;
- TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
- // ----- Read next block
- $v_buffer = gzread($p_tar_add, 512);
- } while (!gzeof($p_tar_add));
- TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
- }
- // ----- Close the files
- gzclose($p_tar_add);
- }
- // ----- Look for uncompressed tar file
- else if ($p_mode=="tar")
- {
- // ----- Open the file in read mode
- if (($p_tar_add = @fopen($p_tarname_add, "rb")) == 0)
- {
- // ----- Error log
- PclErrorLog(-2, "Unable to open file '$p_tarname_add' in binary read mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Read the first 512 bytes block
- $v_buffer = fread($p_tar_add, 512);
- // ----- Read the following blocks but not the last one
- if (!feof($p_tar_add))
- {
- TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
- $i=1;
- // ----- Read new 512 block and write the already read
- do{
- // ----- Write the already read block
- $v_binary_data = pack("a512", "$v_buffer");
- if ($p_mode=="tar")
- fputs($p_tar, $v_binary_data);
- else
- gzputs($v_temp_tar, $v_binary_data);
- $i++;
- TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
- // ----- Read next block
- $v_buffer = fread($p_tar_add, 512);
- } while (!feof($p_tar_add));
- TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
- }
- // ----- Close the files
- fclose($p_tar_add);
- }
- // ----- Call the footer of the tar archive
- $v_result = PclTarHandleFooter($p_tar, $p_mode);
- // ----- Look for closing compressed archive
- if ($p_mode == "tgz")
- {
- // ----- Close the files
- gzclose($p_tar);
- gzclose($v_temp_tar);
- // ----- Unlink tar file
- if (!@unlink($p_tarname))
- {
- // ----- Error log
- PclErrorLog(-11, "Error while deleting archive name $p_tarname");
- }
- // ----- Rename tar file
- if (!@rename($v_temp_tarname, $p_tarname))
- {
- // ----- Error log
- PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // ----- Look for closing uncompressed tar file
- else if ($p_mode=="tar")
- {
- // ----- Close the tarfile
- fclose($p_tar);
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
- // ***** *****
- // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarHandleCreate()
- // Description :
- // Parameters :
- // $p_tarname : Name of the tar file
- // $p_list : An array containing the file or directory names to add in the tar
- // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
- // Return Values :
- // --------------------------------------------------------------------------------
- function PclTarHandleCreate($p_tarname, $p_list, $p_mode, $p_add_dir="", $p_remove_dir="")
- {
- TrFctStart(__FILE__, __LINE__, "PclTarHandleCreate", "tar=$p_tarname, list, mode=$p_mode, add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
- $v_result=1;
- $v_list_detail = array();
- // ----- Check the parameters
- if (($p_tarname == "") || (($p_mode != "tar") && ($p_mode != "tgz")))
- {
- // ----- Error log
- if ($p_tarname == "")
- PclErrorLog(-3, "Invalid empty archive name");
- else
- PclErrorLog(-3, "Unknown mode '$p_mode'");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Look for tar file
- if ($p_mode == "tar")
- {
- // ----- Open the tar file
- if (($p_tar = fopen($p_tarname, "wb")) == 0)
- {
- // ----- Error log
- PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Call the adding fct inside the tar
- if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
- {
- // ----- Call the footer of the tar archive
- $v_result = PclTarHandleFooter($p_tar, $p_mode);
- }
- // ----- Close the tarfile
- fclose($p_tar);
- }
- // ----- Look for tgz file
- else
- {
- // ----- Open the tar file
- if (($p_tar = @gzopen($p_tarname, "wb")) == 0)
- {
- // ----- Error log
- PclErrorLog(-1, "Unable to open file [$p_tarname] in binary write mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Call the adding fct inside the tar
- if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $v_list_detail, $p_add_dir, $p_remove_dir)) == 1)
- {
- // ----- Call the footer of the tar archive
- $v_result = PclTarHandleFooter($p_tar, $p_mode);
- }
- // ----- Close the tarfile
- gzclose($p_tar);
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarHandleAppend()
- // Description :
- // Parameters :
- // $p_tarname : Name of the tar file
- // $p_list : An array containing the file or directory names to add in the tar
- // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
- // Return Values :
- // --------------------------------------------------------------------------------
- function PclTarHandleAppend($p_tarname, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
- {
- TrFctStart(__FILE__, __LINE__, "PclTarHandleAppend", "tar=$p_tarname, list, mode=$p_mode");
- $v_result=1;
- // ----- Check the parameters
- if ($p_tarname == "")
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid empty archive name");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- clearstatcache();
- // ----- Check the file size
- if ((!is_file($p_tarname)) ||
- (((($v_size = filesize($p_tarname)) % 512) != 0) && ($p_mode=="tar")))
- {
- // ----- Error log
- if (!is_file($p_tarname))
- PclErrorLog(-4, "Archive '$p_tarname' does not exist");
- else
- PclErrorLog(-6, "Archive '$p_tarname' has invalid size ".filesize($p_tarname)."(not a 512 block multiple)");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Look for compressed archive
- if ($p_mode == "tgz")
- {
- // ----- Open the file in read mode
- if (($p_tar = @gzopen($p_tarname, "rb")) == 0)
- {
- // ----- Error log
- PclErrorLog(-2, "Unable to open file '$p_tarname' in binary read mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Open a temporary file in write mode
- $v_temp_tarname = uniqid("pcltar-").".tmp";
- TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file $v_temp_tarname");
- if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0)
- {
- // ----- Close tar file
- gzclose($p_tar);
- // ----- Error log
- PclErrorLog(-1, "Unable to open file '$v_temp_tarname' in binary write mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Read the first 512 bytes block
- $v_buffer = gzread($p_tar, 512);
- // ----- Read the following blocks but not the last one
- if (!gzeof($p_tar))
- {
- TrFctMessage(__FILE__, __LINE__, 3, "More than one 512 block file");
- $i=1;
- // ----- Read new 512 block and write the already read
- do{
- // ----- Write the already read block
- $v_binary_data = pack("a512", "$v_buffer");
- gzputs($v_temp_tar, $v_binary_data);
- $i++;
- TrFctMessage(__FILE__, __LINE__, 3, "Reading block $i");
- // ----- Read next block
- $v_buffer = gzread($p_tar, 512);
- } while (!gzeof($p_tar));
- TrFctMessage(__FILE__, __LINE__, 3, "$i 512 bytes blocks");
- }
- // ----- Call the adding fct inside the tar
- if (($v_result = PclTarHandleAddList($v_temp_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
- {
- // ----- Call the footer of the tar archive
- $v_result = PclTarHandleFooter($v_temp_tar, $p_mode);
- }
- // ----- Close the files
- gzclose($p_tar);
- gzclose($v_temp_tar);
- // ----- Unlink tar file
- if (!@unlink($p_tarname))
- {
- // ----- Error log
- PclErrorLog(-11, "Error while deleting archive name $p_tarname");
- }
- // ----- Rename tar file
- if (!@rename($v_temp_tarname, $p_tarname))
- {
- // ----- Error log
- PclErrorLog(-12, "Error while renaming temporary file $v_temp_tarname to archive name $p_tarname");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // ----- Look for uncompressed tar file
- else if ($p_mode=="tar")
- {
- // ----- Open the tar file
- if (($p_tar = fopen($p_tarname, "r+b")) == 0)
- {
- // ----- Error log
- PclErrorLog(-1, "Unable to open file '$p_tarname' in binary write mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Go to the beginning of last block
- TrFctMessage(__FILE__, __LINE__, 4, "Position before :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
- fseek($p_tar, $v_size-512);
- TrFctMessage(__FILE__, __LINE__, 4, "Position after :".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
- // ----- Call the adding fct inside the tar
- if (($v_result = PclTarHandleAddList($p_tar, $p_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir)) == 1)
- {
- // ----- Call the footer of the tar archive
- $v_result = PclTarHandleFooter($p_tar, $p_mode);
- }
- // ----- Close the tarfile
- fclose($p_tar);
- }
- // ----- Look for unknown type
- else
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid tar mode $p_mode");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarHandleAddList()
- // Description :
- // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
- // different from the real path of the file. This is usefull if you want to have PclTar
- // running in any directory, and memorize relative path from an other directory.
- // Parameters :
- // $p_tar : File descriptor of the tar archive
- // $p_list : An array containing the file or directory names to add in the tar
- // $p_mode : "tar" for normal tar archive, "tgz" for gzipped tar archive
- // $p_list_detail : list of added files with their properties (specially the status field)
- // $p_add_dir : Path to add in the filename path archived
- // $p_remove_dir : Path to remove in the filename path archived
- // Return Values :
- // --------------------------------------------------------------------------------
- function PclTarHandleAddList($p_tar, $p_list, $p_mode, &$p_list_detail, $p_add_dir, $p_remove_dir)
- {
- TrFctStart(__FILE__, __LINE__, "PclTarHandleAddList", "tar='$p_tar', list, mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
- $v_result=1;
- $v_header = array();
- // ----- Recuperate the current number of elt in list
- $v_nb = sizeof($p_list_detail);
- // ----- Check the parameters
- if ($p_tar == 0)
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Check the arguments
- if (sizeof($p_list) == 0)
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Loop on the files
- for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
- {
- // ----- Recuperate the filename
- $p_filename = $p_list[$j];
- TrFctMessage(__FILE__, __LINE__, 2, "Looking for file [$p_filename]");
- // ----- Skip empty file names
- if ($p_filename == "")
- {
- TrFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
- continue;
- }
- // ----- Check the filename
- if (!file_exists($p_filename))
- {
- // ----- Error log
- TrFctMessage(__FILE__, __LINE__, 2, "File '$p_filename' does not exists");
- PclErrorLog(-4, "File '$p_filename' does not exists");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Check the path length
- if (strlen($p_filename) > 99)
- {
- // ----- Error log
- PclErrorLog(-5, "File name is too long (max. 99) : '$p_filename'");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- TrFctMessage(__FILE__, __LINE__, 4, "File position before header =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
- // ----- Add the file
- if (($v_result = PclTarHandleAddFile($p_tar, $p_filename, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
- {
- // ----- Return status
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // ----- Store the file infos
- $p_list_detail[$v_nb++] = $v_header;
- // ----- Look for directory
- if (is_dir($p_filename))
- {
- TrFctMessage(__FILE__, __LINE__, 2, "$p_filename is a directory");
- // ----- Look for path
- if ($p_filename != ".")
- $v_path = $p_filename."/";
- else
- $v_path = "";
- // ----- Read the directory for files and sub-directories
- $p_hdir = opendir($p_filename);
- $p_hitem = readdir($p_hdir); // '.' directory
- $p_hitem = readdir($p_hdir); // '..' directory
- while ($p_hitem = readdir($p_hdir))
- {
- // ----- Look for a file
- if (is_file($v_path.$p_hitem))
- {
- TrFctMessage(__FILE__, __LINE__, 4, "Add the file '".$v_path.$p_hitem."'");
- // ----- Add the file
- if (($v_result = PclTarHandleAddFile($p_tar, $v_path.$p_hitem, $p_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1)
- {
- // ----- Return status
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // ----- Store the file infos
- $p_list_detail[$v_nb++] = $v_header;
- }
- // ----- Recursive call to PclTarHandleAddFile()
- else
- {
- TrFctMessage(__FILE__, __LINE__, 4, "'".$v_path.$p_hitem."' is a directory");
- // ----- Need an array as parameter
- $p_temp_list[0] = $v_path.$p_hitem;
- $v_result = PclTarHandleAddList($p_tar, $p_temp_list, $p_mode, $p_list_detail, $p_add_dir, $p_remove_dir);
- }
- }
- // ----- Free memory for the recursive loop
- unset($p_temp_list);
- unset($p_hdir);
- unset($p_hitem);
- }
- else
- {
- TrFctMessage(__FILE__, __LINE__, 4, "File position after blocks =".($p_mode=="tar"?ftell($p_tar):gztell($p_tar)));
- }
- }
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, $v_result);
- return $v_result;
- }
- // --------------------------------------------------------------------------------
- // --------------------------------------------------------------------------------
- // Function : PclTarHandleAddFile()
- // Description :
- // Parameters :
- // Return Values :
- // --------------------------------------------------------------------------------
- function PclTarHandleAddFile($p_tar, $p_filename, $p_mode, &$p_header, $p_add_dir, $p_remove_dir)
- {
- TrFctStart(__FILE__, __LINE__, "PclTarHandleAddFile", "tar='$p_tar', filename='$p_filename', p_mode='$p_mode', add_dir='$p_add_dir', remove_dir='$p_remove_dir'");
- $v_result=1;
- // ----- Check the parameters
- if ($p_tar == 0)
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid file descriptor in file ".__FILE__.", line ".__LINE__);
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Skip empty file names
- if ($p_filename == "")
- {
- // ----- Error log
- PclErrorLog(-3, "Invalid file list parameter (invalid or empty list)");
- // ----- Return
- TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
- return PclErrorCode();
- }
- // ----- Calculate the stored filename
- $v_stored_filename = $p_filename;
- if ($p_remove_dir != "")
- {
- if (substr($p_remove_dir, -1) != '/')
- $p_remove_dir .= "/";
- if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
- {
- if …
Large files files are truncated, but you can click here to view the full file