

--bho new
function CheckIEClickLink(tabRoot)
	
	if tabRoot == nil then
		return nil
	end
	--XLMessageBox("CheckThunderIEPlugin6.9")
	local instdir = tabRoot['rule']['thuderiePlugin@']['instdir']
	if instdir == "{nil}" then return 2 end 
	--XLMessageBox("instdir: "..instdir)
	local result = 0
	local Obj = XLGetObject("Xunlei.OSShell.Obj")
	
	local exePath = tabRoot['rule']['thuderiePlugin@']['exePath']
	if Obj:FileExists(exePath) == false then
		return 3
	end
	
	local bhoDllPath=""
	for i=0, #tabRoot['rule']['thuderiePlugin']['items'] do
		if tabRoot['rule']['thuderiePlugin']['items'][i]~=nil then
			local nametype = tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['nametype']
			local OrgDllName=instdir.."\\"..tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['name']..".dll"
			local progid = tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['progid']
			local strCLSID = tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['clsid']
			if Obj:FileExists(OrgDllName)==false then
				return 3
			end
			local strDllName=""
			if nametype=="0" then
				strDllName= instdir.."\\"..tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['name'].."_Now.dll"
			elseif nametype=="1"	then
				local ver=""
				ver = Obj:GetFileVerStr(OrgDllName)			
				strDllName = instdir.."\\"..tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['name']..ver..".dll"
			end
			local i,j = string.find(string.lower(strDllName), "bho")
			if i~=nil and j~=nil then 
				bhoDllPath=strDllName
			end
			if progid~=nil then
				local clsid =  Obj:GetCLSIDFromProgID(progid)
				if clsid~= strCLSID then
					result=1 
					break
				end
			end
			local regdllpath = Obj:QueryRegValue("HKEY_CLASSES_ROOT", "CLSID\\"..strCLSID.."\\InprocServer32", "");
			local bRet = true
			if  regdllpath==nil or string.lower(regdllpath) ~= string.lower(strDllName)  or Obj:FileExists(regdllpath)==false then
				result=1
				break
			end
		end
	end
	local regValue= Obj:QueryRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{889D2FEB-5411-4565-8998-1DD2C5261283}", "")
	if regValue==nil then
		result=1
	end
	--HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer
	local regExePath=Obj:QueryRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Thunder Network\\ThunderOem\\thunder_backwnd", "path")
	if regExePath==nil or string.lower(regExePath)~=string.lower(exePath) then
		return 1
	end
--	if result==1 then
--		local bDefalutDownload = false
--		local regValue=Obj:QueryRegValue("HKEY_CURRENT_USER","SOFTWARE\\Microsoft\\Internet Explorer", "DownLoadUI")
--		if regValue~=nil and regValue=="{802F530B-A8F6-4631-AE49-6BACAAC6373E}" then
--			local regdllpath=Obj:QueryRegValue("HKEY_CLASSES_ROOT", "CLSID\\{802F530B-A8F6-4631-AE49-6BACAAC6373E}\\InprocServer32","");
--			if regdllpath~=nil and regdllpath~="" and string.lower(regdllpath)==string.lower(bhoDllPath) and Obj:FileExists(regdllpath) then
--				bDefalutDownload=true
--			end
--		elseif regValue==nil then
--			local regValue=Obj:QueryRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Microsoft\\Internet Explorer", "DownLoadUI")
--			if regValue~=nil and regValue=="{802F530B-A8F6-4631-AE49-6BACAAC6373E}" then
--				local regdllpath=Obj:QueryRegValue("HKEY_CLASSES_ROOT", "CLSID\\{802F530B-A8F6-4631-AE49-6BACAAC6373E}\\InprocServer32","");
--				if regdllpath~=nil and regdllpath~="" and string.lower(regdllpath)==string.lower(bhoDllPath) and Obj:FileExists(regdllpath) then
--					bDefalutDownload=true
--				end
--			end
--		end		
--		if bDefalutDownload==true then
--			result=0
--		else
--			result=1
--		end
--	end
	return result
end

function FixIEClickLink(tabRoot)		
	if tabRoot == nil then
		return nil
	end
	local instdir = tabRoot['rule']['thuderiePlugin@']['instdir']	
	if instdir == "{nil}" then
		return 2 
	end 
	
	local result = 0
	local Obj = XLGetObject("Xunlei.OSShell.Obj")			

	for i=0, #tabRoot['rule']['thuderiePlugin']['items'] do
		if tabRoot['rule']['thuderiePlugin']['items'][i]~=nil then
			local strClsid = tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['clsid']
			local nametype= tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['nametype']
			local progid = 	tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['progid']
			local OrgDllName=instdir.."\\"..tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['name']..".dll"
			local DllName=""		
			if nametype=="0" then
				DllName=instdir.."\\"..tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['name'].."_Now.dll"
			elseif nametype=="1" then
				local ver=""
				ver = Obj:GetFileVerStr(OrgDllName)			
				if ver==nil or ver=="" then return 3 end  --文件不对得不到版本
				DllName=instdir.."\\"..tabRoot['rule']['thuderiePlugin']['items'][i]['item@']['name']..ver..".dll"
			end
			if Obj:FileExists(OrgDllName)==false then
				return 3
			end
			local bForceReg=false
			if progid~=nil then
				local clsid =  Obj:GetCLSIDFromProgID(progid)
				if clsid~= strClsid then
					bForceReg=true
				end
			end
			local regdllpath = Obj:QueryRegValue("HKEY_CLASSES_ROOT", "CLSID\\"..strClsid.."\\InprocServer32", "");
			local bRet = true		
			if  regdllpath==nil or string.lower(regdllpath) ~= string.lower(DllName) or Obj:FileExists(regdllpath)==false or bForceReg==true then
				bForceReg=false
				local bRet
				local bisNotXp = Obj:XL_IsUACOS()
				--XLMessageBox("bisNotXp="..tostring(bisNotXp))
				--XLMessageBox("OrgDllName="..OrgDllName)
				if bisNotXp==false and string.find(string.lower(OrgDllName),"xunleibho")~=nil then
					--XLMessageBox("着你罗！")
					local systemdir = Obj:GetSystemPath()
					bRet = Obj:ShellExecute(systemdir.."\\regsvr32.exe", " -s \""..OrgDllName.."\"", 0, 1)
				else
					bRet = Obj:RegisterDll(OrgDllName) 
				end
				if bRet==false then
					result=1
					break
				end
			end
		end
	end	
	local regValue= Obj:QueryRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{889D2FEB-5411-4565-8998-1DD2C5261283}", "")
	if regValue==nil then
		if false==Obj:SetRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects\\{889D2FEB-5411-4565-8998-1DD2C5261283}", "", 1, "XunleiBHO") then
			result=1
		end
	end
	local exePath = tabRoot['rule']['thuderiePlugin@']['exePath']
	local regExePath=Obj:QueryRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Thunder Network\\ThunderOem\\thunder_backwnd", "path")
	if regExePath==nil or string.lower(regExePath)~=string.lower(exePath) then
		if Obj:SetRegValue("HKEY_LOCAL_MACHINE", "SOFTWARE\\Thunder Network\\ThunderOem\\thunder_backwnd", "path", 1, exePath)==false then
			result = 1
		end
	end
	local regValue=Obj:QueryRegValue("HKEY_CURRENT_USER", "SOFTWARE\\Microsoft\\Internet Explorer", "DownLoadUI")
	if regValue==nil or regValue~="{802F530B-A8F6-4631-AE49-6BACAAC6373E}" then
		Obj:SetRegValue("HKEY_CURRENT_USER", "SOFTWARE\\Microsoft\\Internet Explorer", "DownLoadUI", 1, "{802F530B-A8F6-4631-AE49-6BACAAC6373E}")
	end
	local bCheckResult = CheckIEClickLink(tabRoot)
	if bCheckResult==nil or bCheckResult~=0 then
		result = 1
	else
		result = 0
	end
	return result
end

function CheckIERMenu(tabRoot)
	
	if tabRoot == nil then
		return nil
	end
	--XLMessageBox("CheckIERMenu")
	local instdir = tabRoot['rule']['thuderiePlugin@']['instdir']
	if instdir=="{nil}" then return 2 end 
	--XLMessageBox("instdir: "..instdir)
	local result = 0
	local Obj=XLGetObject("Xunlei.OSShell.Obj")
	
	local strGetUrl = instdir.."\\GetUrl.htm"
	local strGetAllUrl = instdir.."\\GetAllUrl.htm"
	if Obj:FileExists(strGetUrl)==false or Obj:FileExists(strGetAllUrl)==false then
		return 3
	end
   --XLMessageBox("strGetUrl = "..strGetUrl)
   --XLMessageBox("strGetAllUrl = "..strGetAllUrl)
	local bCheckGeturl=false
	local bCheckGetallurl=false
	local tabSubkey = Obj:GetRegSubItem("HKEY_CURRENT_USER", "Software\\Microsoft\\Internet Explorer\\MenuExt")
	if tabSubkey==nil then
		result=1
	end
	for i=1, #tabSubkey do
		local strName=Obj:QueryRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..tabSubkey[i], "Name")
		if strName~=nil and string.lower(strName)=="xl_geturl" then
			if bCheckGeturl==true then  --已经检测过，多余的右键菜单
				result=1
			end
			local strValue = Obj:QueryRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..tabSubkey[i], "")
			if strValue==nil or string.lower(strGetUrl)~=string.lower(strValue) then
				--XLMessageBox("url fail!")
				--XLMessageBox("strGetUrl="..strGetUrl.." strValue="..strValue)
				result=1
			elseif Obj:FileExists(strValue)==false then
				return 3
			end
			local dwValue = Obj:QueryRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..tabSubkey[i], "Contexts")
			if dwValue==nil or dwValue~=34  then
				--XLMessageBox("~=34")
				result=1
			end
			bCheckGeturl=true
		elseif strName~=nil and string.lower(strName)=="xl_getallurl" then			
			if bCheckGetallurl==true then	--已经检测过，多余的右键菜单
				result=1
			end	
			local strValue = Obj:QueryRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..tabSubkey[i], "")
			if strValue==nil or string.lower(strGetAllUrl)~=string.lower(strValue) then
				--XLMessageBox("allurl fail!")
				--XLMessageBox("strGetUrl="..strGetUrl.." strValue="..strValue)
				result=1
			elseif Obj:FileExists(strValue)==false then
				return 3
			end
			local dwValue = Obj:QueryRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..tabSubkey[i], "Contexts")
			if dwValue==nil or dwValue~=243  then
				--XLMessageBox("tabSubkey[i] = "..tabSubkey[i])
				--XLMessageBox("~=243")
				result=1
			end
			bCheckGetallurl=true
		end
	end
	if bCheckGetallurl==false or bCheckGeturl==false then 
		result=1 
	end
	
	return result		
end

function FixIERMenu(tabRoot)	
	--XLMessageBox("FixIERMenu")
	if tabRoot == nil then
		return nil
	end
	local instdir = tabRoot['rule']['thuderiePlugin@']['instdir']	
	if instdir == "{nil}" then return 2 end 
	--XLMessageBox("instdir="..instdir)
	local result = 0
	local Obj = XLGetObject("Xunlei.OSShell.Obj")
	
	function FixIEMenuExt (tab, strAppPath)
		--XLMessageBox("FixIEMenuExt")
		local strGetUrl = instdir.."\\GetUrl.htm"
		local strGetAllUrl = instdir.."\\GetAllUrl.htm"
		
		--XLMessageBox("strGetUrl = "..strGetUrl)
		--XLMessageBox("strGetAllUrl = "..strGetAllUrl)
		local name1 = tab['rule']['thuderiePlugin']['iemenutxts'][1]['iemenutxt@']['name']	   
		local name2 = tab['rule']['thuderiePlugin']['iemenutxts'][2]['iemenutxt@']['name']
		local bRet =  true
		--先删除所有右键残余链接
		local tabSubkey = Obj:GetRegSubItem("HKEY_CURRENT_USER", "Software\\Microsoft\\Internet Explorer\\MenuExt")
		for i=1, #tabSubkey do
			local strName=Obj:QueryRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\"..tabSubkey[i], "Name")
			if strName~=nil then
				if string.lower(strName)=="xl_geturl" or string.lower(strName)=="xl_getallurl" then
					if Obj:DeleteRegKey("HKEY_CURRENT_USER", "Software\\Microsoft\\Internet Explorer\\MenuExt\\"..tabSubkey[i])==false then
						bRet = false
					end
				end
			end
		end
	   
		-- 修复使用迅雷下载
		
		bRet = Obj:SetRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..name1,  "",  1,  strGetUrl)
		bRet = bRet and Obj:SetRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..name1,  "Contexts",  4,  "34")   		
		bRet = bRet and Obj:SetRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..name1,  "Name",  1,  "xl_geturl")
		--修复使用迅雷下载全部链接    
		bRet = bRet and Obj:SetRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..name2,  "",  1,  strGetAllUrl)
		bRet = bRet and Obj:SetRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..name2,  "Contexts",  4,  "243")   		
		bRet = bRet and Obj:SetRegValue("HKEY_CURRENT_USER",  "Software\\Microsoft\\Internet Explorer\\MenuExt\\" ..name2,  "Name",  1,  "xl_getallurl")
		--XLMessageBox("bRet = "..tostring(bRet))
		return bRet 
	end
			
	if FixIEMenuExt(tabRoot, instdir)==false then
		result = 1
	end
	if CheckIERMenu(tabRoot)==nil or CheckIERMenu(tabRoot)~=0 then
		result = 1 
	else
		result = 0
	end
	--XLMessageBox("result:"..tostring(result))
	return result
end

--检测看看插件
function CheckKankanIEPlugin(tabRoot)
	
	if tabRoot == nil then
		return nil
	end
	local Obj = XLGetObject("Xunlei.OSShell.Obj")	
	--迅雷5IE 插件

	--防止兼容模式打开
	local bIsVistaOrWin7 = Obj:XL_IsUACOS()
	if bIsVistaOrWin7==false then
		local sysDir = Obj:GetSystemPath()
		local tcpPath = sysDir.."\\drivers\\tcpip.sys"
		local filever = Obj:GetFileVerStr(tcpPath)
		--XLMessageBox(filever:sub(1,1))
		if filever:sub(1,1)=="6" then
			return 0
		end
	end
	
	local result = 0
	for i=0, #tabRoot['rule']['thunderkankanPlugin']['items'] do
		if tabRoot['rule']['thunderkankanPlugin']['items'][i]~=nil then
			local dir = tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['dir']
			if dir == "{nil}" then return 2 end 
			local dllpath=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['dllpath']
			local dllProgid=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['progid']
			local bLocalServer=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['bLocalServer']
			local dllFullpath=dir..dllpath
			if Obj:FileExists(dllFullpath)==false then
				return 3
			end
			local clsid=""
			if dllProgid=="" then			
				clsid = tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['clsid']
			else
				clsid=Obj:GetCLSIDFromProgID(dllProgid)
			end
			if clsid==nil then result=1 end
			local filePath=""
			if bLocalServer=="0" then
				filePath = Obj:QueryRegValue("HKEY_CLASSES_ROOT", "\\CLSID\\"..clsid.."\\InprocServer32", "")
			else
				filePath = Obj:QueryRegValue("HKEY_CLASSES_ROOT", "\\CLSID\\"..clsid.."\\LocalServer32", "")
			end
			if filePath==nil or Obj:FileExists(filePath) == false then
				if bLocalServer=="1" then                          --xmp.exe
					local xmpPath = dir.."Program\\Xmp.exe"
					if Obj:FileExists(xmpPath)==false then
						return 3
					end
				end
				result=1
			end		
		end	
	end	
	--XLMessageBox("result: "..result)
	return result
end

function FixKankanIEPlugin(tabRoot)	
	
	if tabRoot == nil then
		return nil
	end
	--XLMessageBox("FixKankanIEPlugin")
	local Obj = XLGetObject("Xunlei.OSShell.Obj")					
	local result = 0
	
	for i=0, #tabRoot['rule']['thunderkankanPlugin']['items'] do  
		if tabRoot['rule']['thunderkankanPlugin']['items'][i]~=nil then 
			local dir=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['dir']
			if dir == "{nil}" then return 2 end 
			local dllpath=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['dllpath']
			local dllProgid=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['progid']
			local bLocalServer=tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['bLocalServer']
			local clsid=""
			if dllProgid=="" then			
				clsid = tabRoot['rule']['thunderkankanPlugin']['items'][i]['item@']['clsid']
			else
				clsid=Obj:GetCLSIDFromProgID(dllProgid)
			end
			local filePath=""
			if clsid~=nil then
				if bLocalServer=="0" then
					filePath = Obj:QueryRegValue("HKEY_CLASSES_ROOT", "\\CLSID\\"..clsid.."\\InprocServer32", "")
				else
					filePath = Obj:QueryRegValue("HKEY_CLASSES_ROOT", "\\CLSID\\"..clsid.."\\LocalServer32", "")
				end
			end
			if filePath==nil or Obj:FileExists(filePath) == false then
				if Obj:RegisterDll(dir..dllpath)==false then
					result = 1
					break
				end
			end
		end
	end
	local ret=CheckKankanIEPlugin(tabRoot)
	if ret==nil or ret~=0 then
		result = 1
	else
		result = 0 
	end
	return result
	--迅雷5IE 插件	
end