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: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281:
| unit UpdateSystem;
interface
uses Windows, SysUtils, Classes, IdHTTP, ContNrs, IniFiles, DECFmt, DECHash, Forms;
type TUpdateFileList = class; TUpdateFile = class;
TUpdate = class(TComponent) private FFileListURL: String; FFileSpaceURL: String; protected procedure GetFileList(AFileList: TUpdateFileList); procedure DownloadFiles(AFileList: TUpdateFileList); public function IsNewVersionAvailable(AFileList: TUpdateFileList): Boolean; procedure Update; published property FileListURL: String read FFileListURL write FFileListURL; property FileSpaceURL: String read FFileSpaceURL write FFileSpaceURL; end;
TUpdateFileList = class(TObjectList) protected procedure SetItem(Index: Integer; Value: TUpdateFile); function GetItem(Index: Integer): TUpdateFile; public procedure LoadFromFile(AFileName: String); procedure SaveToFile(AFileName: String); property Items[Index: Integer]: TUpdateFile read GetItem write SetItem; default; end;
TUpdateFile = class(TPersistent) private FFileName: String; FChecksum: String; FUpdated: Boolean; public property FileName: String read FFileName write FFileName; property Checksum: String read FChecksum write FChecksum; property Updated: Boolean read FUpdated write FUpdated; end;
procedure Register; function IsWindowsAdmin: Boolean; function IsFileWriteable(AFileName: String): Boolean;
const SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5)) ; SECURITY_BUILTIN_DOMAIN_RID = $00000020; DOMAIN_ALIAS_RID_ADMINS = $00000220;
implementation
procedure Register; begin RegisterComponents('Samples', [TUpdate]); end;
function IsWindowsAdmin: Boolean; var hAccessToken: THandle; ptgGroups: PTokenGroups; dwInfoBufferSize: DWORD; psidAdministrators: PSID; g: Integer; bSuccess: BOOL; begin Result := False;
bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, hAccessToken) ; if not bSuccess then begin if GetLastError = ERROR_NO_TOKEN then bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hAccessToken) ; end;
if bSuccess then begin GetMem(ptgGroups, 1024) ;
bSuccess := GetTokenInformation(hAccessToken, TokenGroups, ptgGroups, 1024, dwInfoBufferSize) ;
CloseHandle(hAccessToken) ;
if bSuccess then begin AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, psidAdministrators) ;
for g := 0 to ptgGroups.GroupCount - 1 do if EqualSid(psidAdministrators, ptgGroups.Groups[g].Sid) then begin Result := True; Break; end;
FreeSid(psidAdministrators) ; end;
FreeMem(ptgGroups) ; end; end;
function IsFileWriteable(AFileName: String): Boolean; var hFile: THandle; begin Result := False;
hFile := CreateFile(PChar(AFileName), GENERIC_WRITE, 0, nil, OPEN_EXISTING, 0,0);
if (hFile = INVALID_HANDLE_VALUE) then Result := GetLastError <> ERROR_ACCESS_DENIED else CloseHandle(hFile); end;
procedure TUpdate.Update; var Files: TUpdateFileList; begin if not IsFileWriteable(ExtractFilePath(ParamStr(0)) + 'temp.tmp') then raise Exception.Create('Sie haben nicht die nötigen Rechte um Dateien zu verändern! Starten Sie das Programm als Administrator.');
if FileExists(ExtractFilePath(ParamStr(0)) + '_' + ExtractFileName(ParamStr(0))) then DeleteFile(ExtractFilePath(ParamStr(0)) + '_' + ExtractFileName(ParamStr(0)));
Files := TUpdateFileList.Create; try if IsNewVersionAvailable(Files) then DownloadFiles(Files); finally Files.Free; end; end;
function TUpdate.IsNewVersionAvailable(AFileList: TUpdateFileList): Boolean; var Files: TUpdateFileList; iFile: Integer; begin Result := False;
if Assigned(AFileList) then Files := AFileList else Files := TUpdateFileList.Create;
if not Assigned(Files) then Exit;
Files.Clear; GetFileList(Files); for iFile := 0 to Files.Count - 1 do begin Application.ProcessMessages; if (not FileExists(ExtractFilePath(ParamStr(0)) + Files[iFile].FileName)) or (AnsiLowerCase(THash_MD5.CalcFile(ExtractFilePath(ParamStr(0)) + Files[iFile].FileName, TFormat_HEX)) <> AnsiLowerCase(Files[iFile].Checksum)) then begin Files[iFile].Updated := True; Result := True; end; end;
if (not Assigned(AFileList)) and Assigned(Files) then Files.Free; end;
procedure TUpdate.GetFileList(AFileList: TUpdateFileList); var s: String; http: TIdHTTP; fs: TFileStream; begin if Assigned(AFileList) then begin http := TIdHTTP.Create(nil); try fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + 'filelist.dat', fmCreate or fmOpenWrite); try http.Get(FFileListURL, fs); finally fs.Free; AFileList.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'filelist.dat'); end; finally http.Free; end; end; end;
procedure TUpdate.DownloadFiles(AFileList: TUpdateFileList); var iFile: Integer; http: TIdHTTP; fs: TFileStream; begin if Assigned(AFileList) then begin http := TIdHTTP.Create(nil); try for iFile := 0 to AFileList.Count - 1 do begin Application.ProcessMessages; if AFileList[iFile].Updated then begin if AnsiLowerCase(AFileList[iFile].FileName) = AnsiLowerCase(ExtractFileName(ParamStr(0))) then RenameFile(ParamStr(0), '_' + ExtractFileName(ParamStr(0)));
fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + AFileList[iFile].FileName, fmCreate or fmOpenWrite); try http.Get(FFileSpaceURL + AFileList[iFile].FileName, fs); finally fs.Free; end; end; end; finally http.Free; end; end; end;
procedure TUpdateFileList.SetItem(Index: Integer; Value: TUpdateFile); begin inherited Items[Index] := Value; end;
function TUpdateFileList.GetItem(Index: Integer): TUpdateFile; begin Result := inherited Items[Index] as TUpdateFile; end;
procedure TUpdateFileList.LoadFromFile(AFileName: string); var ini: TMemIniFile; iFile: Integer; NewFile: TUpdateFile; begin ini := TMemIniFile.Create(AFileName); try for iFile := 0 to ini.ReadInteger('General', 'FileCount', 0) - 1 do begin NewFile := TUpdateFile.Create; NewFile.FFileName := ini.ReadString('File' + IntToStr(iFile), 'FileName', ''); NewFile.FChecksum := ini.ReadString('File' + IntToStr(iFile), 'Checksum', ''); Add(NewFile); end; finally ini.Free; end; end;
procedure TUpdateFileList.SaveToFile(AFileName: string); var ini: TMemIniFile; iFile: Integer; begin ini := TMemIniFile.Create(AFileName); try ini.Clear; ini.WriteInteger('General', 'FileCount', Count); for iFile := 0 to Count - 1 do begin ini.WriteString('File' + IntToStr(iFile), 'FileName', Items[iFile].FileName); ini.WriteString('File' + IntToStr(iFile), 'Checksum', Items[iFile].Checksum); end; ini.UpdateFile; finally ini.Free; end; end;
end. |