home *** CD-ROM | disk | FTP | other *** search
/ Hot Shareware 37 / hot37.iso / FICHEROS / LVB / CGIT201.ZIP / DEMOGUES.BA_ / DEMOGUES.BA
Text File  |  1998-06-08  |  8KB  |  143 lines

  1. Attribute VB_Name = "modGuestbook"
  2. 'VB5-CGI Objects script example: demoGuestbook.bas
  3. 'Copyright 1997, 1998 EazyWare - http://www.eazyware.com/vb5-cgi
  4. '---------------------------------------------------------------
  5. 'Guest book script which stores the entries in a text file.
  6. '- Creates the HTML document itself, just provide a link to the .exe file.
  7. '- Shows all information on one page.
  8. '- Validates the entries and highlights the missing input fields without JavaScript.
  9. '- Removes HTML tags from entries.
  10. '- Shows the last entry on top of the list.
  11. '- Shows additional information like host and used browser.
  12. '- Doesen't allow to save the same entry multiple times (reload button).
  13. '
  14. 'To use the script on WIN95/NT with a Web server you need to:
  15. '- Register VB5CGI.DLL and VB5HTML.DLL using RegSvr32.exe.
  16. '- Copy MSVBVM50.DLL (SP2) to the %System32% or script directory.
  17. '- Compile this script to an .exe (use 'Unattended Execution' in Project Properties).
  18. '
  19. 'Remarks:
  20. '- I use '+' instead of '&' to concat strings, because of better performance (type String).
  21. '- The .exe file can be renamed after compilation, because the script name is retrieved dynamically.
  22. '---------------------------------------------------------------------------------------------------
  23.  
  24. Option Explicit
  25. Private Const METHOD = "GET"            'Method to be used, either 'GET' or 'POST'
  26. Private GuestBookFile As String         'Guest book text filename
  27. Private CGI  As New VB5CGI.clsCGI       'Instance the VB5CGI Object (needs VB5CGI.DLL)
  28. Private HTML As New VB5HTML.clsHTML     'Instance the VB5HTML Object (needs VB5HTML.DLL)
  29.  
  30. Sub Main()
  31. Dim pos As Integer
  32. Dim msg As String
  33.     
  34.     With HTML
  35.         GuestBookFile = CGI.GetPath + "demoGuestbook.txt"   'Set the guest book file accordingly
  36.         .ErrorSubText = "<BR><HR SIZE=""1"">Please send your comments to " + _
  37.                         "<A HREF=""mailto:tools@eazyware.com"">EazyWare</A>"
  38.         If .InitQueryString(False, 1024) Then               'Retrieve the query string (from 'GET' or 'POST' method), limit to 1024 chars
  39.             'A query string exists, validate all entries
  40.             If Not .HasKeyValidStringContent("name", htmlLetterUS + htmlSpace, 3, 40) Then
  41.                 msg = "Please enter your name (US letters, spaces, 3 to 40 characters):"
  42.                 pos = 1
  43.             End If
  44.             If (pos = 0) And Not .HasKeyValidEmail("email") Then
  45.                 msg = "Please enter your valid e-mail address:"                                               'And end the script
  46.                 pos = 2
  47.             End If
  48.             If (pos = 0) And Not .HasKeyValidStringLen("comment", 1, 300) Then
  49.                 msg = "Please enter your comment (1 to 300 characters):"                                               'And end the script
  50.                 pos = 3
  51.             End If
  52.             .PageBegin "VB5-CGI Objects demo: Guest Book", "#FFFFFF"
  53.             If pos > 0 Then
  54.                 'Validation error occured, show the form with highlighted input fields, but don't show the guest book entries
  55.                 .BodyHTML GetGuestbookForm(.GetKeyString("name", True), .GetKeyString("email", True), _
  56.                                            .GetKeyString("comment", True), pos, msg)
  57.             Else
  58.                 'No validation error, add the new entry to the top and show all of them
  59.                 msg = AddGuestBookEntry(.GetKeyString("name", True), .GetKeyString("email", True), _
  60.                                   .GetKeyString("comment", True))
  61.                 .BodyHTML GetGuestbookForm(, , , , "Thanks " + .GetKeyString("name", True) + _
  62.                                           ", your entry has been added to the top!") + msg
  63.             End If
  64.             .PageEnd                'Submit the page
  65.         Else
  66.             'No query string received, show the guest book form and existing entries
  67.             msg = HTML.GetFileText(GuestBookFile, False)
  68.             If msg = "" Then msg = "<HR SIZE=""1""><B>No guest book entry yet ...</B>"
  69.             .PageBegin "VB5-CGI Objects demo: Guest Book", "#FFFFFF"
  70.             .BodyHTML GetGuestbookForm() + msg
  71.             .PageEnd                'Submit the page
  72.         End If
  73.     End With
  74. End Sub
  75.  
  76. 'Returns the the guestbook form as HTML and highlights an invalid input field
  77. Private Function GetGuestbookForm(Optional Name As String, Optional Email As String, Optional Comment As String, _
  78.                                   Optional ErrorPos As Integer, Optional FormTitle As String) As String
  79. Const ERROR_COLOR = "BGCOLOR=""#FF0000"" "
  80. Dim msg As String
  81. Dim namecol As String
  82. Dim emailcol As String
  83. Dim commentcol As String
  84.  
  85.     Select Case ErrorPos
  86.     Case 1
  87.         namecol = ERROR_COLOR       'Invalid name
  88.     Case 2
  89.         emailcol = ERROR_COLOR      'Invalid e-mail
  90.     Case 3
  91.         commentcol = ERROR_COLOR    'Invalid comment
  92.     End Select
  93.     If FormTitle = "" Then FormTitle = "Would you like to sign the guest book?"
  94.     msg = "<H2>VB5-CGI Objects demo: Guest Book</H2>" + vbCrLf
  95.     msg = msg + "<B>CGI script: " + CGI.GetScriptName + "</B><HR>" + vbCrLf
  96.     msg = msg + "<B>" + FormTitle + "</B>" + vbCrLf
  97.     msg = msg + "<FORM ACTION=""" + CGI.GetScriptName + """ METHOD=""" + METHOD + """>" + vbCrLf
  98.     msg = msg + "<TABLE CELLSPACING=""2"" CELLPADDING=""2"">" + vbCrLf
  99.     msg = msg + "<TR>" + vbCrLf
  100.     msg = msg + "<TD WIDTH=""150"" ALIGN=""RIGHT"" VALIGN=""TOP"" NOWRAP>Your name:</TD>" + vbCrLf
  101.     msg = msg + "<TD " + namecol + "NOWRAP><INPUT TYPE=""Text"" NAME=""name"" Value=""" + Name + """ SIZE=""40"" MAXLENGTH=""40""></TD>" + vbCrLf
  102.     msg = msg + "</TR><TR>" + vbCrLf
  103.     msg = msg + "<TD WIDTH=""150"" ALIGN=""RIGHT"" VALIGN=""TOP"" NOWRAP>Your e-mail address:</TD>" + vbCrLf
  104.     msg = msg + "<TD " + emailcol + "NOWRAP><INPUT TYPE=""Text"" NAME=""email"" Value=""" + Email + """ SIZE=""40"" MAXLENGTH=""40""></TD>" + vbCrLf
  105.     msg = msg + "</TR><TR>" + vbCrLf
  106.     msg = msg + "<TD WIDTH=""150"" ALIGN=""RIGHT"" VALIGN=""TOP"" NOWRAP>Your comments:</TD>" + vbCrLf
  107.     msg = msg + "<TD " + commentcol + "NOWRAP><TEXTAREA NAME=""comment"" COLS=""40"" ROWS=""3"" WRAP=""VIRTUAL"">" + Comment + "</TEXTAREA></TD>" + vbCrLf
  108.     msg = msg + "</TR><TR>" + vbCrLf
  109.     msg = msg + "<TD></TD>" + vbCrLf
  110.     msg = msg + "<TD><INPUT TYPE=""Submit"" VALUE=""Add to guest book""</TD>" + vbCrLf
  111.     msg = msg + "</TR></TABLE></FORM>" + vbCrLf
  112.     GetGuestbookForm = msg
  113. End Function
  114.  
  115. 'Adds a new entry to the guest book file and returns all guest book entries
  116. Private Function AddGuestBookEntry(Name As String, Email As String, Comment As String) As String
  117. Dim ffree  As Integer
  118. Dim addmsg As String
  119. Dim oldmsg As String
  120.  
  121.     On Error GoTo ErrorRoutine
  122.     oldmsg = HTML.GetFileText(GuestBookFile, False) 'Get the guest book entries
  123.     addmsg = "<HR SIZE=""1""><B>" + Comment + "</B><BR>" + vbCrLf
  124.     addmsg = addmsg + "<FONT SIZE=""-1"">Added by: <A HREF=""mailto:" + Email + """>" + Name + "</A> on: "
  125.     If InStr(oldmsg, addmsg) Then           'Check, if new entry already exists in guest book
  126.         HTML.ErrorPage Name + ", you already entered the same content!<P>This new entry will not be added to the guest book."
  127.         Exit Function
  128.     End If
  129.     addmsg = addmsg + Format$(Now(), "Long Date") + " at: " + Format$(Now(), "Long Time")
  130.     addmsg = addmsg + " host: " + CGI.EnvRemoteHost + " browser: " + CGI.EnvHTTPUserAgent + "</FONT>" + vbCrLf
  131.     addmsg = addmsg + oldmsg
  132.     ffree = FreeFile()
  133.     Open GuestBookFile For Output Lock Write As #ffree
  134.        Print #ffree, addmsg                 'Save it to the guest book
  135.     Close #ffree
  136.     AddGuestBookEntry = addmsg
  137.     Exit Function
  138.  
  139. ErrorRoutine:
  140.     HTML.ErrorPage "File error occured, please try again!<P>Description: " + Err.Description
  141.     Exit Function
  142. End Function
  143.