-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandler-other.php
215 lines (199 loc) · 5.22 KB
/
handler-other.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
error_reporting(0);
if ($argc > 1){
$dir = $argv[1];
}
$allowPref = array(
'Package',
'Section',
'Installed-Size',
'Maintainer',
'Architecture',
'Version',
'Depends',
'Filename',
'Size',
'MD5sum',
'SHA1',
'SHA256',
'SHA512',
'Description',
'Name',
'Author',
'Depiction',
'Source',
'Priority',
'Essential',
'Pre-Depends',
'Recommends',
'Suggests',
'Conflicts',
'Provides',
'Replaces',
'Enhances',
'Origin',
'Bugs',
'Homepage',
'Website',
'Icon'
);
$packname = 'Packages';
$handler = opendir($dir);
while (($filename = readdir($handler)) !== false) {
//务必使用!==,防止目录下出现类似文件名“0”等情况
if ($filename != "." && $filename != "..") {
if(substr(strrchr($filename, '.'), 1)=='deb'){
$files[] = $filename ;
}
}
}
closedir($handler);
echo "生成中,请稍后...\r\n";
unlink($packname);
unlink("{$packname}.bz2");
$packages_content = '';
$file_count = count($files);
$i = 0;
$now_process_num = 0;
$package_array = array();
$tmp_array = array();
$tmp_key_array = array();
foreach ($files as $v) {
unset($tmp_array,$tmp_key);
$tmp_array = GetControlInfo($v);
if(!is_array($tmp_array))
continue;
$tmp_key = array_keys($tmp_array);
$tmp_key = $tmp_key[0];
foreach ($tmp_array[$tmp_key] as $k=>$v){
$package_array[$tmp_key][$k] = $v;
$tmp_key_array[$tmp_key][] = $k;
}
$i++;
$process_num = intval($i/$file_count*100);
if($process_num>$now_process_num){
$now_process_num = $process_num;
echo "读取控制信息:{$process_num}%\r\n";
}
}
$i = 0;
$now_process_num = 0;
foreach ($tmp_key_array as $k=>$v){
$newv = quickSort($v);
foreach($newv as $kk=>$vv){
//echo $k."\r\n";
$packages_content .= $package_array[$k][$vv].PHP_EOL;
}
$i++;
$process_num = intval($i/$file_count*100);
if($process_num>$now_process_num){
$now_process_num = $process_num;
echo "正在生成文件:{$process_num}%\r\n";
}
}
file_put_contents($packname, $packages_content);
file_put_contents("{$packname}.bz2", bzcompress($packages_content));
function GetControlInfo($filename){
global $dir;
unlink('tmp\control.tar');
unlink('tmp\control');
exec('bin\ar\ar -t "' . $dir . '\\' . $filename . '"',$file_array);
foreach($file_array as $v){
if(stripos($v,'control')!==false){
$tar_name = $v;
}
}
// $re = system('bin\ar\ar -x ' . $dir . '\\' . $filename . ' ' . $tar_name,$code);
exec('bin\ar\ar -x "' . $dir . '\\' . $filename . '" ' .$tar_name);
// echo $filename . "\r\n";
exec('bin\7z\7z e -otmp -aoa '.$tar_name);
if(is_file('tmp\control.tar')){
$tmp_tar = 'tmp\control.tar';
}else{
$handler = opendir('tmp');
while (($tmpfilename = readdir($handler)) !== false) {
if ($tmpfilename != "." && $filename != "..") {
if(stripos($tmpfilename,'tmp')!==false){
$tmp_tar = 'tmp\\'.$tmpfilename;
break;
}
}
}
}
exec('bin\7z\7z e -otmp -aoa ' . $tmp_tar);
$control_string = file_get_contents('tmp\control');
unlink($tmp_tar);
unlink('tmp\control');
unlink($tar_name);
$full_filename = './' . $dir . '/' . $filename;
$package_array = makePackage($full_filename,$control_string);
return $package_array;
}
function makePackage($filename,$control_string){
global $allowPref;
$control_fields = explode("\n", $control_string);
$control = array();
foreach ($control_fields as $field) {
if (!strlen(trim($field)))
continue;
$tmp = explode(":", $field, 2);
if(trim($tmp[1]) && stripos($field,':') !== false){
$tmp_last = $tmp;
$control[$tmp[0]] = trim($tmp[1]);
}elseif(stripos($field,':') === false)
$control[$tmp_last[0]] .= PHP_EOL .$tmp[0];
}
if (!isset($control['Package']) || !isset($control['Version']))
return false;
if(trim($control['Depiction']))
$control['Depiction'] = str_ireplace('http://repo.auxiliumdev.com/','https://lwlsw.github.io/repo/',$control['Depiction']);
$control['Size'] = filesize($filename);
$control['MD5sum'] = md5_file($filename);
$control['SHA256'] = hash_file('sha256',$filename);
$control['SHA512'] = hash_file('sha512',$filename);
if ($control['MD5sum'] === false)
return false;
unset($control['Filename']);
$control["Filename"] = $filename;
$control_sort = array();
foreach($allowPref as $v){
if($control[$v]){
$control_sort[$v] = $control[$v];
}
}
foreach ($control_sort as $k => $v)
if(is_numeric($k))
$packages_string .= $v . "\n";
else
$packages_string .= $k . ": " . $v . "\n";
$packages_Array[$control['Package']][$control['Version']] = $packages_string;
return $packages_Array;
}
//版本号排序
function quickSort($arr) {
//先判断是否需要继续进行
$length = count($arr);
if($length <= 1) {
return $arr;
}
//选择第一个元素作为基准
$base_num = $arr[0];
//遍历除了标尺外的所有元素,按照大小关系放入两个数组内
//初始化两个数组
$left_array = array(); //小于基准的
$right_array = array(); //大于基准的
for($i=1; $i<$length; $i++) {
if(version_compare($base_num,$arr[$i]) < 0) {
//放入左边数组
$left_array[] = $arr[$i];
} else {
//放入右边
$right_array[] = $arr[$i];
}
}
//再分别对左边和右边的数组进行相同的排序处理方式递归调用这个函数
$left_array = quickSort($left_array);
$right_array = quickSort($right_array);
//合并
return array_merge($left_array, array($base_num), $right_array);
}