Update via web page backend

Share your advanced PureBasic knowledge/code with the community.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Update via web page backend

Post by Nituvious »

Heres a couple simple routines to download application updates from a web server. It comes in two parts. A couple PureBasic procedures and a PHP script. The program grabs HTTP headers and looks for 'AppVersion: ' and 'AppDownload: ', and saves them in a structured variable.

The php script just sends two custom headers, one containing the version and the other is the location of the file to download.

PB:

Code: Select all

Structure VERSION_INFO
	
	major.l
	minor.l
	release.l
	
EndStructure

Structure UPDATE_DATA
	
	appdownload.s
	appversion.l
	
EndStructure

InitNetwork()


Procedure DownloadLatestUpdate(source.s, destination.s, packageName.s)
	
	; returns 1 on success, 0 error
	
	Define.l download, e, text, ret = -1, font
	
	download = OpenWindow(#PB_Any, 0, 0, 250, 90, "Updater", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
	font = LoadFont(#PB_Any, "Arial", 12)
	text = TextGadget(#PB_Any, 0, 30, 250, 90, "Attempting to download update...", #PB_Text_Center)
	SetGadgetFont(text, FontID(font))

	While e <> #PB_Event_CloseWindow
		e = WaitWindowEvent(1)
		If ret = -1
			If ReceiveHTTPFile(source.s, destination.s + packageName.s)
				SetGadgetText(text, "Downloaded update! Close this window to open file.") : ret = 1
			Else 
				SetGadgetText(text, "Failed to retrieve update.") : ret = 0
			EndIf
		EndIf
	Wend
	
	If ret : RunProgram(packageName.s, "", destination.s) : EndIf

	FreeGadget(text)
	FreeFont(font)
	CloseWindow(download)
	
	ProcedureReturn ret
	
EndProcedure

Procedure GetLatestVersion(url.s, *updatedata.UPDATE_DATA)
	
	; returns 0 on failure, 1 if only one header is found, and 2 on success
	
	Define.s str, line
	Define.l c, i, ret = 0
	
	str.s = GetHTTPHeader(url.s)
	c = CountString(str.s, Chr(10))
	If c > 0 : For i = 1 To c

			line.s = StringField(str.s, i, Chr(10))			
			If FindString(line.s, "AppDownload: ")
				*updatedata\appdownload = StringField(line.s, 2, " ")
				Debug *updatedata\appdownload
				ret + 1
			EndIf
			If FindString(line.s, "AppVersion: ")
				*updatedata\appversion = Val(StringField(line.s, 2, " "))
				Debug *updatedata\appversion
				ret + 1
			EndIf

	Next i : EndIf
	ProcedureReturn ret
	
EndProcedure

Procedure ConvertVersion(*versioninfo.VERSION_INFO)
	
	ProcedureReturn Val(Str(*versioninfo\major) + Str(*versioninfo\minor) + Str(*versioninfo\release))

EndProcedure

updateinfo.UPDATE_DATA
version.VERSION_INFO\major = 1
version\minor   = 2
version\release = 29


urlToVersion.s = "http://mywebsite.com/apphomepage/index.php"
appPath.s = "C:\MyApp\"
If GetLatestVersion(urlToVersion, updateinfo) = 2

	If  updateinfo\appversion > ConvertVersion(version)
		r = MessageRequester("Updater", "There is an update available!" + #LF$ + "Would you like to download it now?", #PB_MessageRequester_YesNo)
		If r = #PB_MessageRequester_Yes
			DownloadLatestUpdate(updateinfo\appdownload, appPath.s, "MyApp.zip")
		EndIf
	EndIf

EndIf
PHP:

Code: Select all

<?PHP 

	header("AppVersion: 1230");
	header("AppDownload: http://myapp.com/download.php?file=MyApp.zip");

?>
<!DOCTYPE html>
<html>
	<head></head>
	<body></body>
</html>
▓▓▓▓▓▒▒▒▒▒░░░░░
jesperbrannmark
Enthusiast
Enthusiast
Posts: 536
Joined: Mon Feb 16, 2009 10:42 am
Location: sweden
Contact:

Re: Update via web page backend

Post by jesperbrannmark »

Great!
I am just thinking out loud right now.
My ideal update program would work like this:

1. See if the program is started with programparameter - if so, taskkill the program that is in programparameter and copy myself to that file location and name. (replace)
2. See if File X is in tempfolder, if so run it with this files location and filename as programparameter
3. See if appversion is latest -> otherwise download File X to tempfolder
4. Run program as normal la-la-la
5. If File X is downloaded to tempfolder that means this was not the latest version.
-> Notify the user that a the program will autoupdate when they quit ? (click here to update now)
-> Upon exit open File X with this files location and filename as programparameter
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: Update via web page backend

Post by Nituvious »

Sounds good!
I needed something to quickly update a program after I've published it on a web server. The GUI procedure is just a PoC thing for using headers to grab update information.
▓▓▓▓▓▒▒▒▒▒░░░░░
Post Reply