Subversion Repositories php-qbpwcf

Rev

Rev 226 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
#!/usr/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
26
 
27
計算檔案的checksum in 16 位元
28
 
29
參考資料:
30
https://www.programmersought.com/article/41936075398/
31
https://www.twblogs.net/a/5d3f3d47bd9eee51fbf902b2
32
https://www.itread01.com/content/1547685555.html
33
 
34
*/
35
 
36
#指派命名空間
37
namespace qbpwcf;
38
 
39
#取得 lib path
40
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
41
 
42
#如果執行失敗
43
if($status!==0){
44
 
45
	#debug
46
	var_dump(__LINE__,$output);
47
 
48
	#結束執行,回傳shell 1.
49
	exit(1);
50
 
51
	}#if end
52
 
53
#儲存lib path
54
$folderOfUsrLib=$output[0];
55
 
56
#以該檔案的實際位置的 lib path 為 include path 首位
57
$output=array();
58
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
59
 
60
#如果執行失敗
61
if($status!==0){
62
 
63
	#debug
64
	var_dump(__LINE__,$output);
65
 
66
	#結束執行,回傳shell 1.
67
	exit(1);
68
 
69
	}#if end
70
 
71
#設置 include path 
72
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
73
 
74
#匯入外部套件
75
include("allInOne.php");
76
 
77
#如果沒有指定檔案參數
78
if(!isset($_SERVER["argv"][1]))
79
{
80
	echo "檔案參數是必須的".PHP_EOL;
81
	exit;
82
 
83
}#if end
84
 
85
#取得檔案內容
86
#函式說明:
87
#依據行號分隔抓取檔案的內容,結果會回傳一個陣列
88
#回傳的變數說明:
89
#$result["status"],執行是否成功,"true"代表成功;"fasle"代表失敗.
90
#$result["error"],錯誤訊息提示.
91
#$result["warning"],警告訊息.
92
#$result["function"],當前執行的函數名稱.
93
#$result["fileContent"],爲檔案的內容陣列.
94
#$result["lineCount"],爲檔案內容總共的行數.
95
#$result["fullContent"],為檔案的完整內容.
96
#$result["base64data"],為檔案的base64內容.
97
#$result["mimeType"],為檔案的mime type.
98
#必填的參數:
99
#$conf["filePositionAndName"],字串,爲檔案的位置以及名稱.
100
$conf["filePositionAndName"]=$_SERVER["argv"][1];
101
#$conf["fileArgu"],字串,php變數__FILE__的內容,亦即該檔案在檔案系統的絕對路徑
102
$conf["fileArgu"]=__FILE__;
103
#可省略參數:
104
#$conf["web"],是要取得網路上的檔案則為"true";反之則為"false".
105
$conf["web"]="false";
106
#參考資料:
107
#file(),取得檔案內容的行數.
108
#file=>http:#php.net/manual/en/function.file.php
109
#rtrim(),剔除透過file()取得每行內容結尾的換行符號.
110
#filesize=>http://php.net/manual/en/function.filesize.php
111
$getFileContent=fileAccess::getFileContent($conf);
112
unset($conf);
113
 
114
#如果檔案取得失敗
115
if($getFileContent["status"]==="false")
116
{
117
	#印出內容
118
	var_dump($getFileContent);
119
 
120
	#結束執行
121
	exit;
122
}
123
 
124
#取得檔案的bytes長度
125
$fileBytes=strlen($getFileContent["fullContent"]);
126
 
127
#取得checksum bytes 1 的位置
128
$checksumAddr1=(int)($fileBytes/2);
129
 
130
#取得checksum bytes 2 的位置
131
$checksumAddr2=(int)($fileBytes/2)+1;
132
 
133
#取得checksum的第一個bytes位置
134
$checksumBytes1=$getFileContent["fullContent"][$checksumAddr1];
135
 
136
#取得checksum的第二個bytes位置
137
$checksumBytes2=$getFileContent["fullContent"][$checksumAddr2];
138
 
139
#將 checksum 位置的內容設置為 00
140
$getFileContent["fullContent"][$checksumAddr1]=chr(base_convert("00",16,10)); 
141
$getFileContent["fullContent"][$checksumAddr2]=chr(base_convert("00",16,10)); 
142
 
143
#存放結果變數
144
$sumOfHex4num=0;
145
 
146
#迴圈
147
for($i=0;$i<$fileBytes;$i=$i+2)
148
{
149
	#不存在就用00替補
150
	if(!isset($getFileContent["fullContent"][$i+1]))
151
	{
152
		$getFileContent["fullContent"][$i+1]="00";
153
	}
154
 
155
	#取得4個bytes的16進位數值
156
	$hex2bytes=sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i]),10,16)).sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i+1]),10,16));
157
 
158
	#加總
159
	$sumOfHex4num=$sumOfHex4num+(int)base_convert($hex2bytes,16,10);
160
 
161
}#for end
162
 
163
#更新成16進位
164
$sumOfHex4num=base_convert($sumOfHex4num,10,16);
165
 
166
#儲存加總的地方
167
$newSumOfHex4num=0;
168
 
169
#如果加總之後的長度大於4
170
while(strlen($sumOfHex4num)>4)
171
{
172
	#如果長度不是4的倍數
173
	if(strlen($sumOfHex4num)%4!==0)
174
	{
175
		//前面補一個0
176
		$sumOfHex4num="0".$sumOfHex4num;
177
 
178
		//繼續while
179
		continue;
180
	}
181
 
182
	for($i=0;$i<strlen($sumOfHex4num);$i=$i+4)
183
	{
184
		#取得4個的16進位數值
185
		$hex2bytes=$sumOfHex4num[$i].$sumOfHex4num[$i+1].$sumOfHex4num[$i+2].$sumOfHex4num[$i+3];
186
 
187
		#加總
188
		$newSumOfHex4num=$newSumOfHex4num+base_convert($hex2bytes,16,10);
189
 
190
	}#for end
191
 
192
	#更新成16進位
193
	$newSumOfHex4num=base_convert($newSumOfHex4num,10,16);
194
 
195
	#設置回儲存加總的地方
196
	$sumOfHex4num=$newSumOfHex4num;
197
 
198
}#while end
199
 
200
#取得 16 bit checksum
201
$checksumIn16bit=sprintf("%04s",base_convert(base_convert("ffff",16,10)-base_convert($sumOfHex4num,16,10),10,16));
202
 
203
#將 checksum 位置的內容設置為 checksum1 跟 checksum2
204
$getFileContent["fullContent"][$checksumAddr1]=$checksumBytes1;
205
$getFileContent["fullContent"][$checksumAddr2]=$checksumBytes2;
206
 
207
#存放結果變數
208
$sumOfHex4num=0;
209
 
210
#迴圈
211
for($i=0;$i<$fileBytes;$i=$i+2)
212
{
213
	#不存在就用00替補
214
	if(!isset($getFileContent["fullContent"][$i+1]))
215
	{
216
		$getFileContent["fullContent"][$i+1]="00";
217
	}
218
 
219
	#取得4個bytes的16進位數值
220
	$hex2bytes=sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i]),10,16)).sprintf("%02s",base_convert(ord($getFileContent["fullContent"][$i+1]),10,16));
221
 
222
	#加總
223
	$sumOfHex4num=$sumOfHex4num+(int)base_convert($hex2bytes,16,10);
224
 
225
}#for end
226
 
227
#更新成16進位
228
$sumOfHex4num=base_convert($sumOfHex4num,10,16);
229
 
230
#儲存加總的地方
231
$newSumOfHex4num=0;
232
 
233
#如果加總之後的長度大於4
234
while(strlen($sumOfHex4num)>4)
235
{
236
	#如果長度不是4的倍數
237
	if(strlen($sumOfHex4num)%4!==0)
238
	{
239
		//前面補一個0
240
		$sumOfHex4num="0".$sumOfHex4num;
241
 
242
		//繼續while
243
		continue;
244
	}
245
 
246
	for($i=0;$i<strlen($sumOfHex4num);$i=$i+4)
247
	{
248
		#取得4個的16進位數值
249
		$hex2bytes=$sumOfHex4num[$i].$sumOfHex4num[$i+1].$sumOfHex4num[$i+2].$sumOfHex4num[$i+3];
250
 
251
		#加總
252
		$newSumOfHex4num=$newSumOfHex4num+base_convert($hex2bytes,16,10);
253
 
254
	}#for end
255
 
256
	#更新成16進位
257
	$newSumOfHex4num=base_convert($newSumOfHex4num,10,16);
258
 
259
	#設置回儲存加總的地方
260
	$sumOfHex4num=$newSumOfHex4num;
261
 
262
}#while end
263
 
264
#取得 16 bit checksum
265
$shouldBe0000=sprintf("%04s",base_convert(base_convert("ffff",16,10)-base_convert($sumOfHex4num,16,10),10,16));
266
 
267
#如果加總的反數為0
268
if($shouldBe0000==="0000")
269
{
270
	#印出check sum
271
	echo $checksumIn16bit;
272
}
273
else
274
{
275
	#debug
276
	echo $shouldBe0000."!=0000";
277
}
278
 
279
?>