...
procedure AddTrustedSite(Site:string);
procedure DecodeSiteName(Site:string;
var Protocol:string;
var Prefix:string;
var Domain:string);
implementation
uses Registry, StrUtils;
...
procedure TForm1.Button1Click(Sender: TObject);
begin
try
AddTrustedSite(Edit1.Text);
except
on e:Exception do
ShowMessage('Try logging in as Administrator : '+e.Message);
end;
end;
///////////////////////////////////////////////////////////////
// procedure : AddTrustedSite
// purpose : Perform in IE6 the equivalent of user actions:
// - Tools|Internet Options|Security|Trusted|
// <Type and Add site>
//
// assumptions:
// Assumes that the Site is passed in following format:
[protocol://][{prefix.}...] domain.extension
//////////////////////////////////////////////////////////////
procedure AddTrustedSite(Site:string);
var
reg:TRegistry;
sDomain:string;
sPrefix:string;
sProtocol:string;
begin
DecodeSiteName(Site,sProtocol,sPrefix,sDomain);
reg:=TRegistry.Create;
try
reg.RootKey:=HKEY_CURRENT_USER;
if reg.OpenKey(TRUSTED_ZONES_KEY,false) then
begin
//Make sure the domain is added as a key in the trusted zones
reg.OpenKey(sDomain,true);
//If there is a prefix create a subkey of it
if sPrefix<>'' then
reg.OpenKey(sPrefix,true);
//Add a value into the current key
if sProtocol='' then
reg.WriteInteger('*',TRUSTED)
else
reg.WriteInteger(sProtocol,TRUSTED);
end
else
raise Exception.Create('Failed to open Trusted Zones registry');
finally
reg.Free;
end;
end;
//////////////////////////////////////////////////////////////////
// procedure: DecodeSiteName
// purpose : Decode given Site string into three parts according
// to site format specified in AddTrustedSite comments
//////////////////////////////////////////////////////////////////
procedure DecodeSiteName(Site:string;
var Protocol:string;
var Prefix:string; var Domain:string);
var
nPos,nDomainWords:integer;
slTmp:TStringList;
begin
//Extract the protocol if there is any
nPos:=Pos('://',Site);
if nPos>0 then
begin
Protocol:=Copy(Site,1,nPos-1);
Site:=Copy(Site,nPos+3,length(Site)-(nPos+2));
end;
//Ignore any text after the trailing "/"
nPos:=Pos('/',Site);
if nPos>0 then
Site:=Copy(Site,1,nPos-1);
slTmp:=TStringList.Create;
with slTmp do
try
Delimiter:='.';
DelimitedText:=Site;
//Are there at least three words in Site Address ??
if Count>2 then
begin
//If the last words in the site are less than 2 letters long
//Then the domain is made out of last three words
//otherwise it is made out of the last two words
if (length(Strings[Count-1])<3) and (length(Strings[Count-2])<3) then
begin
Domain:=Strings[Count-3]+'.'+Strings[Count-2]+'.'+Strings[Count-1];
nDomainWords:=3;
end
else
begin
Domain:=Strings[Count-2]+'.'+Strings[Count-1];
nDomainWords:=2;
end;
//The prefix is the remaining part of the Site address (if any is left)
if Count>nDomainWords then
Prefix:=StringReplace(Site,'.'+Domain,'',[])
else
Prefix:='';
end
else
begin
Domain:=Site;
Prefix:='';
end;
finally
slTmp.Free;
end;
end;
...