REM    _____________________________________________________
REM   |\___________________________________________________/| 
REM   | |                                                 | |
REM   | | SOURCE TITLE:    VDS CGI - Guestbook Example    | |
REM   | | ------------                                    | |
REM   | | SOURCE AUTHOR:   Garrett R. Hylltun             | |
REM   | | -------------                                   | |
REM   | |                                                 | |
REM   | | DATE:            07 March, 2000                 | |
REM   | | ----                                            | |
REM   | | REQUIREMENTS:    VDS 3.x; vdsstdio.dll          | |
REM   | |                  or vdsdll(inet.dll)            | |
REM   | | ------------                                    | |
REM   | | SOURCE STATUS:   Public Domain                  | |
REM   | | -------------                                   | |
REM   | | SOURCE DESCRIPTION:                             | |
REM   | | ------------------                              | |
REM   | |   This example shows you how to use the         | |
REM   | | vdsstdio.dll in a CGI environment.  This        | |
REM   | | should also serve as a basic tutor on how to    | |
REM   | | use VDS for a Web Page Form, and also shows     | |
REM   | | you how to Parse and Decode the incoming Form   | |
REM   | | data.                                           | |
REM   | |                                                 | |
REM   | |   Original Parse and URI Decode are here with   | |
REM   | | compliments from Tommy Sools and Garrett        | |
REM   | | Hylltun.                                        | |
REM   | |                                                 | |
REM   | |   Always use the Option Errortrap.  If you      | |
REM   | | don't, and your script encounters an error,     | |
REM   | | you will most likely hang the server leaving    | |
REM   | | it dead for any cgi use.  Also, do not use      | |
REM   | | TITLE in your cgi scripts.. When your script    | |
REM   | | is run, you'll have a taskbar icon pop up on    | |
REM   | | the servers screen.  I assure you, that who     | |
REM   | | ever runs your server will not be happy with    | |
REM   | | this happening each time your script is run     | |
REM   | | in the cgi.                                     | |
REM   | |                                                 | |
REM   | |   You can use the VDSDLL's inet.dll for this    | |
REM   | | instead of using the vdsstdio.dll... But if you | |
REM   | | do, remove the parse routine in this, and make  | |
REM   | | sure you adjust the code here to use the VDSDLL | |
REM   | | syntax and commands.                            | |
REM   | |_________________________________________________| |
REM   |/___________________________________________________\|

Option Errortrap,error
External vdsstdio.dll,yourpasswordgoeshere
REM   --- |-------------------------------------------------| ---
REM   --- | Declare to the vdsstdio.dll that this script is | ---
REM   --- | a cgi script.                                   | ---
REM   --- |-------------------------------------------------| ---
  CONSOLE CGI
REM   --- |-------------------------------------------------| ---
REM   --- | Check to see if the user clicked a link that is | ---
REM   --- | in our generated page from this script.  The    | ---
REM   --- | only string possible from this script is 'view'.| ---
REM   --- |-------------------------------------------------| ---
  %A = @env(QUERY_STRING)
  If %A
    goto ViewGuestbook
  End
REM   --- |-------------------------------------------------| ---
REM   --- | Grab the incoming form and check to see if      | ---
REM   --- | there is any data.  If there is no data, then   | ---
REM   --- | send them to the initial form.                  | ---
REM   --- |-------------------------------------------------| ---
  %%Data = @console(read)
  If @null(%%Data)
    goto ShowForm
  End
REM   --- |-------------------------------------------------| ---
REM   --- | Data is present in the console.  Now let's URI  | ---
REM   --- | Decode all of it and split the info and the     | ---
REM   --- | labels.  The info goes into list 2, and the     | ---
REM   --- | labels go into list 1.  Mind you, they are in   | ---
REM   --- | order in these lists.                           | ---
REM   --- |-------------------------------------------------| ---
  list create,1
  list create,2
  repeat
    gosub URIparse
    list add,1,%%URIcommand
    list add,2,%%URIdata
  until @not(%%Data)
  Goto ProcessingDone

:URIparse
  if @zero(@pos("&",%%Data))
    %%Command = %%Data
    %%Data =
  else
    %%Command = @substr(%%Data,1,@pred(@pos("&",%%Data)))
    %%Data = @strdel(%%Data,1,@pos("&",%%Data))
  end
  %%URIcommand = @substr(%%Command,1,@pred(@pos("=",%%Command)))
  %%URIdata = @substr(%%Command,@succ(@pos("=",%%Command)),@len(%%Command))
  gosub URIconvert
  exit
:URIconvert
  repeat
    if @not(@zero(@pos("+",%%URIdata)))
      %%URIdata = @strins(%%URIdata,@pos("+",%%URIdata)," ")
      %%URIdata = @strdel(%%URIdata,@pos("+",%%URIdata),@pos("+",%%URIdata))
    end
  until @zero(@pos("+",%%URIdata))
  %%LastPos = 1
  repeat
    if @not(@zero(@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata)))))
      if @equal(@substr(@substr(%%URIdata,%%LastPos,@len(%%URIdata)),@succ(@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata)))),@sum(@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata))),2)),25)
        %%LastPos = @sum(%%LastPos,@pos("%25",@substr(%%URIdata,%%LastPos,@len(%%URIdata))))
      else
        %%URIdata = @strins(%%URIdata,@pred(@sum(%%LastPos,@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata))))),@chr($@substr(@substr(%%URIdata,%%LastPos,@len(%%URIdata)),@succ(@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata)))),@sum(@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata))),2))))
        %%URIdata = @strdel(%%URIdata, @pred(@sum(%%LastPos,@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata))))), @sum(@sum(%%LastPos,@pos("%",@substr(%%URIdata,%%LastPos,@len(%%URIdata)))),1))
      end
    end
  until @zero(@pos("%", @substr(%%URIdata,%%LastPos,@len(%%URIdata)) ))
  repeat
    if @not(@zero(@pos("%25",%%URIdata)))
      %%URIdata = @strins(%%URIdata,@pos("%25",%%URIdata),"%")
      %%URIdata = @strdel(%%URIdata,@pos("%25",%%URIdata),@sum(@pos("%25",%%URIdata),2))
    end
  until @zero(@pos("%25",%%URIdata))
  exit

:ProcessingDone
REM   --- |-------------------------------------------------| ---
REM   --- | Here, we are done decoding and parsing the      | ---
REM   --- | incoming form submission.  Now we just show     | ---
REM   --- | them what they just entered.                    | ---
REM   --- |-------------------------------------------------| ---
  Console write, Content-type: text/html@chr(10)
  Console write,@chr(10)
  Console write,<html><head><title>My Guestbook</title></head>@chr(10)
  Console write,<body bgcolor=@chr(34)#FFFFFF@chr(34)>@chr(10)
  Console write,<table border=@chr(34)0@chr(34) width=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,height=@chr(34)100@chr(37)@chr(34) cellspacing=@chr(34)0@chr(34) @chr(10)
  Console write,cellpadding=@chr(34)0@chr(34)><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) align=@chr(34)center@chr(34) @chr(10)
  Console write,valign=@chr(34)middle@chr(34)><table border=@chr(34)0@chr(34) @chr(10)
  Console write,width=@chr(34)400@chr(34) cellspacing=@chr(34)1@chr(34) @chr(10)
  Console write,cellpadding=@chr(34)4@chr(34) bgcolor=@chr(34)#0000FF@chr(34)><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#000080@chr(34)><font @chr(10)
  Console write,color=@chr(34)#00FFFF@chr(34) face=@chr(34)Verdana@chr(34)><b>Thank @chr(10)
  Console write,you for Signing My Guestbook</b></font></td></tr><tr><td @chr(10)
  Console write,width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#FFFFFF@chr(34)><table @chr(10)
  Console write,border=@chr(34)0@chr(34) width=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,cellspacing=@chr(34)0@chr(34) cellpadding=@chr(34)2@chr(34)><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34)><small><font @chr(10)
  REM --- Name below
  Console write,face=@chr(34)Verdana@chr(34)>@item(2,0)</font></small></td>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) align=@chr(34)right@chr(34)></td></tr>@chr(10)
  REM --- Email below
  Console write,<tr><td><small><font face=@chr(34)Verdana@chr(34)>@item(2,1)</font></small></td>@chr(10)
  Console write,<td align=@chr(34)right@chr(34)></td></tr><tr><td><small>@chr(10)
  REM --- URL below
  Console write,<font face=@chr(34)Verdana@chr(34)>@item(2,2)</font></small></td>@chr(10)
  Console write,<td align=@chr(34)right@chr(34)></td></tr><tr>@chr(10)
  REM --- Comments below
  Console write,<td><small><font face=@chr(34)Verdana@chr(34)>@item(2,3)</font>@chr(10)
  Console write,</small></td><td></td></tr><tr>@chr(10)
  Console write,<td colspan=@chr(34)2@chr(34) align=@chr(34)center@chr(34)><p @chr(10)
  Console write,align=@chr(34)left@chr(34)></td></tr></table></td></tr><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#C0C0C0@chr(34)><p @chr(10)
  Console write,align=@chr(34)center@chr(34)><font face=@chr(34)Verdana@chr(34)>[ <a@chr(10)
  Console write,href=@chr(34)vdscgi.exe?view@chr(34)>View My Guestbook</a> ]</font></td>@chr(10)
  Console write,</tr></table></td></tr></table></body></html>@chr(10)
REM   --- |-------------------------------------------------| ---
REM   --- | Here we open our guestbook db file and add the  | ---
REM   --- | new entry into the db and save it.  We also     | ---
REM   --- | close out all our lists here.                   | ---
REM   --- |-------------------------------------------------| ---
  List Create,3
  List Loadfile,3,@path(%0)guestbook.db
  List Insert,3,@item(2,0)|@item(2,1)|@item(2,2)|@item(2,3)
  List Savefile,3,@path(%0)guestbook.db
  List Close,3
  List Close,1
  List Close,2
  Exit

:ShowForm
REM   --- |-------------------------------------------------| ---
REM   --- | Here we make our initial form, in this case,    | ---
REM   --- | let's make a Guestbook to play with.  We must   | ---
REM   --- | generate a HTML page to give to the user.       | ---
REM   --- |                                                 | ---
REM   --- | First, you would make a web page using your     | ---
REM   --- | favorite HTML Editor.  Once you have your page, | ---
REM   --- | you will then want to convert it use in VDS.    | ---
REM   --- | The first thing you want to convert, is the     | ---
REM   --- | character of @.  So do a search and replace     | ---
REM   --- | for it.. Change it to @chr(64).  You'll then    | ---
REM   --- | want to change the Comma's to @chr(44), then    | ---
REM   --- | Percents to @chr(37), and finally, Quote        | ---
REM   --- | Marks to @chr(34).                              | ---
REM   --- |                                                 | ---
REM   --- | This makes you HTML code fully VDS compatible.  | ---
REM   --- | Below is a chart of the characters that need    | ---
REM   --- | to be converted.                                | ---
REM   --- |                                                 | ---
REM   --- | @ = @chr(64)                                    | ---
REM   --- | , = @chr(44)                                    | ---
REM   --- | % = @chr(37)                                    | ---
REM   --- | " = @chr(34)                                    | ---
REM   --- |                                                 | ---
REM   --- | Console Write, should precede each line and     | ---
REM   --- | @Chr(10) should end each line.                  | ---
REM   --- |                                                 | ---
REM   --- | Please use the very first line always. using    | ---
REM   --- | any other Content header may give you ill       | ---
REM   --- | effects.                                        | ---
REM   --- |-------------------------------------------------| ---
  Console write, Content-type: text/html@chr(10)
  Console write,@chr(10)
  Console write,<html><head><title>My Guestbook</title></head>@chr(10)
  Console write,<body bgcolor=@chr(34)#FFFFFF@chr(34)><form ACTION=@chr(34)vdscgi.exe@chr(34) @chr(10)
  Console write,METHOD=POST><table border=@chr(34)0@chr(34) @chr(10)
  Console write,width=@chr(34)100@chr(37)@chr(34) height=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,cellspacing=@chr(34)0@chr(34) cellpadding=@chr(34)0@chr(34)><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) align=@chr(34)center@chr(34) @chr(10)
  Console write,valign=@chr(34)middle@chr(34)><table border=@chr(34)0@chr(34) @chr(10)
  Console write,width=@chr(34)400@chr(34) cellspacing=@chr(34)1@chr(34) @chr(10)
  Console write,cellpadding=@chr(34)4@chr(34) bgcolor=@chr(34)#0000FF@chr(34)><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#000080@chr(34)><b><font @chr(10)
  Console write,color=@chr(34)#00FFFF@chr(34) face=@chr(34)Verdana@chr(34)>Please @chr(10)
  Console write,Sign My Guestbook</font></b></td></tr><tr><td width=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,bgcolor=@chr(34)#FFFFFF@chr(34)><table border=@chr(34)0@chr(34) width=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,cellspacing=@chr(34)0@chr(34) cellpadding=@chr(34)2@chr(34)><tr><td @chr(10)
  Console write,width=@chr(34)25@chr(37)@chr(34)><small><font face=@chr(34)Verdana@chr(34)>Name:@chr(10)
  Console write,</font></small></td><td width=@chr(34)75@chr(37)@chr(34) align=@chr(34)right@chr(34)><font @chr(10)
  Console write,face=@chr(34)Verdana@chr(34)><input type=@chr(34)text@chr(34) name=@chr(34)NAME@chr(34) @chr(10)
  Console write,size=@chr(34)29@chr(34)></font></td></tr><tr>@chr(10)
  Console write,<td><small><font face=@chr(34)Verdana@chr(34)>Email:</font></small></td>@chr(10)
  Console write,<td align=@chr(34)right@chr(34)><font face=@chr(34)Verdana@chr(34)><input @chr(10)
  Console write,type=@chr(34)text@chr(34) name=@chr(34)EMAIL@chr(34) size=@chr(34)29@chr(34)></font></td>@chr(10)
  Console write,</tr><tr><td><small><font face=@chr(34)Verdana@chr(34)>Home Page:</font></small></td>@chr(10)
  Console write,<td align=@chr(34)right@chr(34)><font face=@chr(34)Verdana@chr(34)><input @chr(10)
  Console write,type=@chr(34)text@chr(34) name=@chr(34)URL@chr(34) size=@chr(34)29@chr(34)></font></td>@chr(10)
  Console write,</tr><tr><td><small><font face=@chr(34)Verdana@chr(34)>Comments:</font></small></td>@chr(10)
  Console write,<td><font face=@chr(34)Verdana@chr(34)></font></td></tr><tr>@chr(10)
  Console write,<td colspan=@chr(34)2@chr(34) align=@chr(34)center@chr(34)><font @chr(10)
  Console write,face=@chr(34)Verdana@chr(34)><textarea rows=@chr(34)3@chr(34) name=@chr(34)COMMENTS@chr(34) @chr(10)
  Console write,cols=@chr(34)39@chr(34)></textarea></font><p><fontface=@chr(34)Verdana@chr(34)><input @chr(10)
  Console write,type=@chr(34)submit@chr(34) value=@chr(34)Submit@chr(34) name=@chr(34)B1@chr(34)> @chr(10)
  Console write,<input type=@chr(34)reset@chr(34) value=@chr(34)Reset@chr(34) name=@chr(34)B2@chr(34)></font></td>@chr(10)
  Console write,</tr></table></td></tr><tr><td width=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,bgcolor=@chr(34)#C0C0C0@chr(34)><p align=@chr(34)center@chr(34)><font @chr(10)
  Console write,face=@chr(34)Verdana@chr(34)>[ <a href=@chr(34)vdscgi.exe?view@chr(34)>View My @chr(10)
  Console write,Guestbook</a> ]</font></td></tr></table></td></tr></table></form></body></html>@chr(10)
  Exit
REM   --- |-------------------------------------------------| ---
REM   --- | Exit the scirpt when done generating the        | ---
REM   --- | initial page.                                   | ---
REM   --- |-------------------------------------------------| ---
:ViewGuestbook
REM   --- |-------------------------------------------------| ---
REM   --- | Here we only generate a page, then open our db  | ---
REM   --- | of guestbook entries and add them to the HTML.  | ---
REM   --- |-------------------------------------------------| ---
  List Create,3
  List Loadfile,3,@path(%0)guestbook.db
  Console write, Content-type: text/html@chr(10)
  Console write,@chr(10)
  Console write,<html><head><title>My Guestbook</title></head>@chr(10)
  Console write,<body bgcolor=@chr(34)#FFFFFF@chr(34)>@chr(10)
  Console write,<table border=@chr(34)0@chr(34) width=@chr(34)100@chr(37)@chr(34) @chr(10)
  Console write,height=@chr(34)100@chr(37)@chr(34) cellspacing=@chr(34)0@chr(34) @chr(10)
  Console write,cellpadding=@chr(34)0@chr(34)><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) align=@chr(34)center@chr(34) @chr(10)
  Console write,valign=@chr(34)middle@chr(34)><table border=@chr(34)0@chr(34) @chr(10)
  Console write,width=@chr(34)400@chr(34) cellspacing=@chr(34)1@chr(34) @chr(10)
  Console write,cellpadding=@chr(34)4@chr(34) bgcolor=@chr(34)#0000FF@chr(34)>@chr(10)
  Console write,<tr><td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#000080@chr(34)>@chr(10)
  Console write,<font color=@chr(34)#00FFFF@chr(34) face=@chr(34)Verdana@chr(34)><b>My @chr(10)
  Console write,Guestbook</b></font></td></tr><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#FFFFFF@chr(34)>@chr(10)
REM   --- |-------------------------------------------------| ---
REM   --- | If the list isn't empty, then let's go through  | ---
REM   --- | and parse it and print it, Else, put a message  | ---
REM   --- | that there's no entries.                        | ---
REM   --- |-------------------------------------------------| ---
  If @not(@null(@item(3)))
    Repeat
      Parse "%%Name;%%Email;%%URL;%%Comments", @item(3)
      Console write,<table border=@chr(34)0@chr(34) width=@chr(34)100@chr(37)@chr(34) @chr(10)
      Console write,cellspacing=@chr(34)0@chr(34) cellpadding=@chr(34)2@chr(34)><tr>@chr(10)
      Console write,<td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#0000FF@chr(34)>@chr(10)
      Console write,<strong><font face=@chr(34)Verdana@chr(34) color=@chr(34)#FFFFFF@chr(34)>@chr(10)
      Console write,<small>@chr(10)
      Console write,%%Name@chr(10)
      Console write,</small></font></strong></td></tr><tr><td><small><font face=@chr(34)Verdana@chr(34)>@chr(10)
      Console write,%%Email@chr(10)
      Console write,</font></small></td></tr><tr><td><small><font face=@chr(34)Verdana@chr(34)>@chr(10)
      Console write,%%URL@chr(10)
      Console write,</font></small></td></tr><tr><td><small><font face=@chr(34)Verdana@chr(34)>@chr(10)
      Console write,%%Comments@chr(10)
      Console write,</font></small></td></tr><tr><td align=@chr(34)center@chr(34)><hr @chr(10)
      Console write,noshade size=@chr(34)1@chr(34)></td></tr></table><P>@chr(10)
      %A = @next(3)
    Until @null(@item(3))
  Else
    Console write,No one has signed my guest book yet.  :-(@chr(10)
  End
  Console write,</td></tr><tr>@chr(10)
  Console write,<td width=@chr(34)100@chr(37)@chr(34) bgcolor=@chr(34)#C0C0C0@chr(34)><p @chr(10)
  Console write,align=@chr(34)center@chr(34)><font face=@chr(34)Verdana@chr(34)>[ <a @chr(10)
  Console write,href=@chr(34)vdscgi.exe@chr(34)>Sign My Guestbook</a> @chr(10)
  Console write,]</font></td></tr></table></td></tr></table></body></html>@chr(10)
  List Close,3
  Exit


:error
REM   --- |-------------------------------------------------| ---
REM   --- | If there is a program error.                    | ---
REM   --- |-------------------------------------------------| ---
  Console write, Content-type: text/html@chr(10)
  Console write,@chr(10)
  Console write,<html><head>@chr(10)
  Console write,<title>Script Error</title>@chr(10)
  Console write,</head><body bgcolor="#FFFFFF" text="#000080">@chr(10)
  Console write,<div align="center"><center><p><textarea rows="8" name="NULL" cols="58">@chr(10)
  Console write,Error Code:   @error(E)@chr(10)
  Console write,Line Number:  @error(N)@chr(10)
  Console write,Date:         @datetime(dddddd)@chr(10)
  Console write,Time:         @datetime(tt)@chr(10)
  Console write,Script:       %0@chr(10)
  Console write,</textarea></p></center></div>@chr(10)
  Console write,</body></html>@chr(10)
  Exit

