Small update
[iDB.git] / inc / misc / iuntar.php
blob67efcc596d0b75de38d03295a0001cd4d22ff81e
1 <?php
2 /*
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the Revised BSD License.
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 Revised BSD License for more details.
11 Copyright 2004-2019 iDB Support - https://idb.osdn.jp/support/category.php?act=view&id=1
12 Copyright 2004-2019 Game Maker 2k - https://idb.osdn.jp/support/category.php?act=view&id=2
13 iUnTar ver. 4.7 by Kazuki Przyborowski & Josep Sanz Campderros
15 $FileInfo: iuntar.php - Last Update: 08/02/2019 SVN 905 - Author: cooldude2k $
17 $File3Name = basename($_SERVER['SCRIPT_NAME']);
18 if ($File3Name=="iuntar.php"||$File3Name=="/iuntar.php") {
19 require('index.php');
20 exit(); }
22 // PHP iUnTAR Version 4.7
23 // license: Revised BSD license
24 // Kazuki Przyborowski (https://idb.osdn.jp/)
25 // Josep Sanz Campderros (http://saltos.net/)
26 function untar($tarfile,$outdir="./",$chmod=null,$extract=true,$lsonly=false,$findfile=null) {
27 $TarSize = filesize($tarfile);
28 $TarSizeEnd = $TarSize - 1024;
29 if($extract!==true&&$extract!==false) {
30 $extract = false; }
31 if($lsonly!==true&&$lsonly!==false) {
32 $lsonly = false; }
33 if($extract===true) {
34 $lsonly = false; }
35 if($extract===true) {
36 if($outdir!=""&&!file_exists($outdir)) {
37 mkdir($outdir,0777); } }
38 $thandle = fopen($tarfile, "rb");
39 $i = 0;
40 if($extract===false) {
41 $FileArray = null; $i = 0; }
42 $outdir = preg_replace('{/$}', '', $outdir)."/";
43 if(isset($findfile)) {
44 $qfindfile = preg_quote($findfile,"/"); }
45 if(!isset($findfile)) {
46 $qfindfile = null; }
47 while (ftell($thandle)<$TarSizeEnd) {
48 $FileName = null;
49 $FileMode = null;
50 $OwnerID = null;
51 $GroupID = null;
52 $FileSize = null;
53 $LastEdit = null;
54 $Checksum = null;
55 $FileType = null;
56 $LinkedFile = null;
57 $FileContent = null;
58 $FileName = $outdir.trim(fread($thandle,100));
59 fseek($thandle,56,SEEK_CUR);
60 $FileType = trim(fread($thandle,1));
61 fseek($thandle,-57,SEEK_CUR);
62 if($findfile!==null&&$FileType!="L"&&!preg_match("/".$qfindfile."/",$FileName)) {
63 fseek($thandle,8,SEEK_CUR);
64 fseek($thandle,8,SEEK_CUR);
65 fseek($thandle,8,SEEK_CUR);
66 $FileSize = octdec(trim(fread($thandle,12)));
67 fseek($thandle,12,SEEK_CUR);
68 fseek($thandle,8,SEEK_CUR);
69 $FileType = trim(fread($thandle,1));
70 fseek($thandle,100,SEEK_CUR);
71 fseek($thandle,255,SEEK_CUR);
72 if($FileType=="0"||$FileType=="7") {
73 fseek($thandle,$FileSize,SEEK_CUR); } }
74 if($findfile===null||$FileType=="L"||preg_match("/".$qfindfile."/",$FileName)) {
75 $FileMode = trim(fread($thandle,8));
76 if($chmod===null) {
77 $FileCHMOD = octdec("0".substr($FileMode,-3)); }
78 if($chmod!==null) {
79 $FileCHMOD = $chmod; }
80 $OwnerID = trim(fread($thandle,8));
81 $GroupID = trim(fread($thandle,8));
82 $FileSize = octdec(trim(fread($thandle,12)));
83 $LastEdit = octdec(trim(fread($thandle,12)));
84 $Checksum = octdec(trim(fread($thandle,8)));
85 $FileType = trim(fread($thandle,1));
86 $LinkedFile = trim(fread($thandle,100));
87 fseek($thandle,255,SEEK_CUR);
88 // LongLink support added by Josep Sanz Campderros
89 if($FileType=="L"&&$FileSize>0) {
90 $FileName = $outdir.trim(fread($thandle,$FileSize));
91 fseek($thandle,512-$FileSize,SEEK_CUR);
92 fseek($thandle,100,SEEK_CUR);
93 $FileMode = trim(fread($thandle,8));
94 if($chmod===null) {
95 $FileCHMOD = octdec("0".substr($FileMode,-3)); }
96 if($chmod!==null) {
97 $FileCHMOD = $chmod; }
98 $OwnerID = trim(fread($thandle,8));
99 $GroupID = trim(fread($thandle,8));
100 $FileSize = octdec(trim(fread($thandle,12)));
101 $LastEdit = trim(fread($thandle,12));
102 $Checksum = trim(fread($thandle,8));
103 $FileType = trim(fread($thandle,1));
104 $LinkedFile = trim(fread($thandle,100));
105 fseek($thandle,255,SEEK_CUR); } }
106 if($findfile===null||preg_match("/".$qfindfile."/",$FileName)) {
107 if($FileType=="0"||$FileType=="7") {
108 if($lsonly===true) {
109 fseek($thandle,$FileSize,SEEK_CUR); }
110 if($lsonly===false) {
111 // Empty files support added by Josep Sanz Campderros
112 if($FileSize===0) {
113 $FileContent = ""; }
114 if($FileSize>0) {
115 $FileContent = fread($thandle,$FileSize); } } }
116 if($FileType=="1") {
117 $FileContent = null; }
118 if($FileType=="2") {
119 $FileContent = null; }
120 if($FileType=="5") {
121 $FileContent = null; }
122 if($FileType=="0"||$FileType=="7") {
123 if($extract===true) {
124 $subhandle = fopen($FileName, "wb+");
125 fwrite($subhandle,$FileContent,$FileSize);
126 fclose($subhandle);
127 chmod($FileName,$FileCHMOD); } }
128 if($FileType=="1") {
129 if($extract===true) {
130 link($FileName,$LinkedFile); } }
131 if($FileType=="2") {
132 if($extract===true) {
133 symlink($LinkedFile,$FileName); } }
134 if($FileType=="5") {
135 if($extract===true) {
136 mkdir($FileName,$FileCHMOD); } }
137 if($FileType=="0"||$FileType=="1"||$FileType=="2"||$FileType=="5"||$FileType=="7") {
138 if($extract===false) {
139 $FileArray[$i]['FileName'] = $FileName;
140 $FileArray[$i]['FileMode'] = $FileMode;
141 $FileArray[$i]['OwnerID'] = $OwnerID;
142 $FileArray[$i]['GroupID'] = $GroupID;
143 $FileArray[$i]['FileSize'] = $FileSize;
144 $FileArray[$i]['LastEdit'] = $LastEdit;
145 $FileArray[$i]['Checksum'] = $Checksum;
146 $FileArray[$i]['FileType'] = $FileType;
147 $FileArray[$i]['LinkedFile'] = $LinkedFile;
148 if($lsonly===false) {
149 $FileArray[$i]['FileContent'] = $FileContent; } } } }
150 //touch($FileName,$LastEdit);
151 if($extract===false&&$findfile===null&&isset($FileArray[$i]['FileName'])) { ++$i; }
152 if($extract===false) {
153 if($findfile!==null&&preg_match("/".$qfindfile."/",$FileName)&&isset($FileArray[$i]['FileName'])) { ++$i; } }
154 if($extract===true) {
155 if($findfile!==null&&preg_match("/".$qfindfile."/",$FileName)) { ++$i; } }
156 if(($FileType=="0"||$FileType=="7")&$FileSize>0) {
157 $CheckSize = 512;
158 while ($CheckSize<$FileSize) {
159 if($CheckSize<$FileSize) {
160 $CheckSize = $CheckSize + 512; } }
161 $SeekSize = $CheckSize - $FileSize;
162 fseek($thandle,$SeekSize,SEEK_CUR); } }
163 fclose($thandle);
164 if($extract===true) {
165 return true; }
166 if($extract===false) {
167 return $FileArray; } }
168 function iuntar($tarfile,$outdir="./",$chmod=null,$extract=true,$lsonly=false,$findfile=null) {
169 return untar($tarfile,$outdir,$chmod,$extract,$lsonly,$findfile); }