⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcltar.lib.php

📁 Joomla15 - 最新开源CMS
💻 PHP
📖 第 1 页 / 共 5 页
字号:
	// ----- 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 ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
		  $p_remove_dir = "./".$p_remove_dir;
		if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
		  $p_remove_dir = substr($p_remove_dir, 2);
	  }

	  if (substr($p_filename, 0, strlen($p_remove_dir)) == $p_remove_dir)
	  {
		$v_stored_filename = substr($p_filename, strlen($p_remove_dir));
		TrFctMessage(__FILE__, __LINE__, 3, "Remove path '$p_remove_dir' in file '$p_filename' = '$v_stored_filename'");
	  }
	}
	if ($p_add_dir != "")
	{
	  if (substr($p_add_dir, -1) == "/")
		$v_stored_filename = $p_add_dir.$v_stored_filename;
	  else
		$v_stored_filename = $p_add_dir."/".$v_stored_filename;
	  TrFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
	}

	// ----- Check the path length
	if (strlen($v_stored_filename) > 99)
	{
	  // ----- Error log
	  PclErrorLog(-5, "Stored file name is too long (max. 99) : '$v_stored_filename'");

	  // ----- Return
	  TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
	  return PclErrorCode();
	}

	// ----- Look for a file
	if (is_file($p_filename))
	{
	  // ----- Open the source file
	  if (($v_file = fopen($p_filename, "rb")) == 0)
	  {
		// ----- Error log
		PclErrorLog(-2, "Unable to open file '$p_filename' in binary read mode");

		// ----- Return
		TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString());
		return PclErrorCode();
	  }

	  // ----- Call the header generation
	  if (($v_result = PclTarHandleHeader($p_tar, $p_filename, $p_mode, $p_header, $v_stored_filename)) != 1)
	  {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -