• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/tar_gz.php
  • Класс: CArchiver
  • Вызов: CArchiver::_extractList
private function _extractList($p_path, &$p_list_detail, $p_mode, $p_file_list, $p_remove_path)
{
	$v_nb = 0;
	$v_extract_all = true;
	$v_listing = false;

	$p_path = str_replace("\", "/", $p_path);
	$p_path = $this->io->GetPhysicalName($p_path);

	if ($p_path == ''
		|| (mb_substr($p_path, 0, 1) != '/'
			&& mb_substr($p_path, 0, 3) != "../"
			&& !mb_strpos($p_path, ':')))
	{
		$p_path = "./" . $p_path;
	}

	$p_remove_path = str_replace("\", "/", $p_remove_path);
	if (($p_remove_path != '') && (mb_substr($p_remove_path, -1) != '/'))
	{
		$p_remove_path .= '/';
	}

	$p_remove_path_size = mb_strlen($p_remove_path);

	switch ($p_mode)
	{
		case "complete" :
			break;
		case "partial" :
			$v_extract_all = false;
			break;
		case "list" :
			$v_extract_all = false;
			$v_listing = true;
			break;
		default :
			$this->_arErrors[] = ["ERR_PARAM", str_replace("#EXTRACT_MODE#", $p_mode, GetMessage("MAIN_ARCHIVE_ERR_PARAM"))];
			return false;
	}

	clearstatcache();

	while (strlen($v_binary_data = $this->_readBlock()) != 0)
	{
		$v_extract_file = false;

		if (!$this->_readHeader($v_binary_data, $v_header))
		{
			return false;
		}

		if ($v_header['filename'] == '')
		{
			continue;
		}

		// ----- Look for long filename
		if ($v_header['typeflag'] == 'L')
		{
			if (!$this->_readLongHeader($v_header))
			{
				return false;
			}
		}
		if ((!$v_extract_all) && (is_array($p_file_list)))
		{
			// ----- By default no unzip if the file is not found
			$l = count($p_file_list);
			for ($i = 0; $i < $l; $i++)
			{
				// ----- Look if it is a directory
				if (mb_substr($p_file_list[$i], -1) == '/')
				{
					// ----- Look if the directory is in the filename path
					if ((mb_strlen($v_header['filename']) > mb_strlen($p_file_list[$i]))
						&& (mb_substr($v_header['filename'], 0, mb_strlen($p_file_list[$i])) == $p_file_list[$i]))
					{
						$v_extract_file = true;
						break;
					}
				}
				elseif ($p_file_list[$i] == $v_header['filename'])
				{
					// ----- It is a file, so compare the file names
					$v_extract_file = true;
					break;
				}
			}
		}
		else
		{
			$v_extract_file = true;
		}

		// ----- Look if this file need to be extracted
		if (($v_extract_file) && (!$v_listing))
		{
			if (($p_remove_path != '')
				&& (mb_substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path))
			{
				$v_header['filename'] = mb_substr($v_header['filename'], $p_remove_path_size);
			}
			if (($p_path != './') && ($p_path != '/'))
			{
				while (mb_substr($p_path, -1) == '/')
				{
					$p_path = mb_substr($p_path, 0, mb_strlen($p_path) - 1);
				}

				if (mb_substr($v_header['filename'], 0, 1) == '/')
				{
					$v_header['filename'] = $p_path . $v_header['filename'];
				}
				else
				{
					$v_header['filename'] = $p_path . '/' . $v_header['filename'];
				}
			}
			if (file_exists($v_header['filename']))
			{
				if ((@is_dir($v_header['filename'])) && ($v_header['typeflag'] == ''))
				{
					$this->_arErrors[] = ["DIR_EXISTS", str_replace("#FILE_NAME#", removeDocRoot($this->io->GetLogicalName($v_header['filename'])), GetMessage("MAIN_ARCHIVE_DIR_EXISTS"))];
					return false;
				}

				if ((is_file($v_header['filename'])) && ($v_header['typeflag'] == "5"))
				{
					$this->_arErrors[] = ["FILE_EXISTS", str_replace("#FILE_NAME#", removeDocRoot($this->io->GetLogicalName($v_header['filename'])), GetMessage("MAIN_ARCHIVE_FILE_EXISTS"))];
					return false;
				}
				if (!is_writeable($v_header['filename']))
				{
					$this->_arErrors[] = ["FILE_PERMS", str_replace("#FILE_NAME#", removeDocRoot($this->io->GetLogicalName($v_header['filename'])), GetMessage("MAIN_ARCHIVE_FILE_PERMS"))];
					return false;
				}
			}
			elseif (($this->_dirCheck(($v_header['typeflag'] == "5" ? $v_header['filename'] : dirname($v_header['filename'])))) != 1)
			{
				$this->_arErrors[] = ["NO_DIR", str_replace("#FILE_NAME#", removeDocRoot($this->io->GetLogicalName($v_header['filename'])), GetMessage("MAIN_ARCHIVE_NO_DIR"))];
				return false;
			}

			if ($this->checkPermissions && !CBXArchive::IsFileSafe($v_header['filename']))
			{
				$this->_jumpBlock(ceil(($v_header['size'] / 512)));
			}
			//should we overwrite existent files?
			elseif ((file_exists($v_header['filename']) && $this->replaceExistentFiles) || !(file_exists($v_header['filename'])))
			{
				if ($v_header['typeflag'] == "5")
				{
					if (!@file_exists($v_header['filename']))
					{
						if (!@mkdir($v_header['filename'], BX_DIR_PERMISSIONS))
						{
							$this->_arErrors[] = ["ERR_CREATE_DIR", str_replace("#DIR_NAME#", removeDocRoot($this->io->GetLogicalName($v_header['filename'])), GetMessage("MAIN_ARCHIVE_ERR_CREATE_DIR"))];
							return false;
						}
					}
				}
				else
				{
					if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0)
					{
						$this->_arErrors[] = ["ERR_CREATE_FILE", str_replace("#FILE_NAME#", removeDocRoot($this->io->GetLogicalName($v_header['filename'])), GetMessage("MAIN_ARCHIVE_ERR_CREATE_FILE"))];
						return false;
					}
					else
					{
						$n = floor($v_header['size'] / 512);
						for ($i = 0; $i < $n; $i++)
						{
							$v_content = $this->_readBlock();
							fwrite($v_dest_file, $v_content, 512);
						}
						if (($v_header['size'] % 512) != 0)
						{
							$v_content = $this->_readBlock();
							fwrite($v_dest_file, $v_content, ($v_header['size'] % 512));
						}

						@fclose($v_dest_file);

						@chmod($v_header['filename'], BX_FILE_PERMISSIONS);
						@touch($v_header['filename'], $v_header['mtime']);
					}

					clearstatcache();
					if (filesize($v_header['filename']) != $v_header['size'])
					{
						$this->_arErrors[] = ["ERR_SIZE_CHECK", str_replace(["#FILE_NAME#", "#SIZE#", "#EXP_SIZE#"], [removeDocRoot($v_header['size']), filesize($v_header['filename']), $v_header['size']], GetMessage("MAIN_ARCHIVE_ERR_SIZE_CHECK"))];
						return false;
					}
				}
			}
			else
			{
				$this->_jumpBlock(ceil(($v_header['size'] / 512)));
			}
		}
		else
		{
			$this->_jumpBlock(ceil(($v_header['size'] / 512)));
		}

		if ($v_listing || $v_extract_file)
		{
			if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename'])
			{
				$v_file_dir = '';
			}
			if ((mb_substr($v_header['filename'], 0, 1) == '/') && ($v_file_dir == ''))
			{
				$v_file_dir = '/';
			}

			$p_list_detail[$v_nb++] = $v_header;
		}
	}

	return true;
}