Gastenboek
Heb je iets leuks te vertellen, of wil je graag iets delen? Dat kan hier!
Naam
E-mail
Bericht
<% ' /////////////////////////////////////////////////////////////////////////// %> <% ' ########################################################################### ' # ' # file: ' # guestbook/index.asp ' # ' # description: ' # guestbook application. ' # ' # author: ' # Uwe Keim, uk-software.de ' # zeta software GbR, Germany (www.zeta-software.de) ' # ' # history: ' # 2000-01-13 Uwe Keim fixed typo when deleting from the database. ' # 2000-01-13 Uwe Keim added count-down. ' # created resource strings. ' # 1999-12-30 Uwe Keim added admin mode. ' # added multiple pages and other stuff. ' # 1999-11-24 Uwe Keim file created. ' # ' ########################################################################### ' /////////////////////////////////////////////////////////////////////////// ' // configuration. ' how many entries are being displayed on one page. ' normally you would specify here something between 20 and 50. const HITS_PER_PAGE = 4 ' string resources, used by other parts of the page. ' used in order to separate code from content. '-todo: add more. const IDS_ANONYMOUS = "Annoniem" const IDS_FIELDSMISSING = "Niet alle nodige velden zijn gevuld. Bericht wordt niet geplaats." const IDS_ERRORDELETE = "Een fout is opgetreden bij het verwijderen." const IDS_MAIL = "Stuur e-mail naar" const IDS_ERROR = "Er is een fout opgetreden." ' /////////////////////////////////////////////////////////////////////////// ' // global constants/variables. ' there is a global mode for the guestbook. ' depending on that mode, this page behaves different. ' you therefore don't need different pages do to things like ' e.g. inserting or deleting. ' supported modes. const MODE_NORMAL = 1 ' normal viewing of the guestbook. const MODE_ERROR = 2 ' an error occured and is displayed. const MODE_INSERT = 4 ' inserting of a item into the guestbook. const MODE_DELETE = 8 ' deleting of an item of the guestbook. const MODE_ADMIN = 16 ' administration-mode: allows deletion of articles. ' read mode. default to normal. dim mode mode = Request("mode") if mode="" then mode = MODE_NORMAL mode = CLng(mode) ' /////////////////////////////////////////////////////////////////////////// ' // general helper functions. ' minimum and maximum functions. function min( a, b ) if ab then max=a else max=b end function ' formats a date. function fmtDate( in_str ) fmtDate = FormatDateTime( in_str, 2 ) end function ' replaces '\n' with '
' function fmtMl( in_str ) if IsNull(in_str) then fmtMl = "" exit function end if dim str if in_str<>"" then str = Replace( in_str, vbCrLf, vbCr ) str = Replace( str , vbLf , vbCr ) str = Replace( str , vbCr , "
" ) fmtMl = str else fmtMl = in_str end if end function ' allow several html codes, but not all! function fmtText( byval txt ) ' first encode all. txt = Server.HtmlEncode(txt) ' then decode the allowed tags. txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) txt = Replace( txt, Server.HtmlEncode(""), "" ) fmtText = fmtMl(txt) end function ' /////////////////////////////////////////////////////////////////////////// ' // url functions. ' these functions should be used whenever you need a ' tag. instead of specifying an asp page and adding some cryptic ' parameters, you call one of these functions. ' with this approach, you can centralize the url-parameters ' in one place. ' the url of this page. ' automatically adds admin mode if currently active. function myselfUrl( mode_ ) if (CLng(mode) and CLng(MODE_ADMIN))<>CLng(0) then mode_ = CLng(mode_) or CLng(MODE_ADMIN) end if myselfUrl = Request.ServerVariables("SCRIPT_NAME") & "?mode=" & mode_ end function ' url for viewing the guestbook in normal mode. function normalUrl normalUrl = myselfUrl(MODE_NORMAL) end function ' url for displaying an error. function errorUrl( error_text ) errorUrl = myselfUrl(MODE_ERROR) & "&error=" & Server.UrlEncode(error_text) end function ' url for inserting a new entry to the guestbook. function insertUrl insertUrl = myselfUrl(MODE_INSERT) end function ' url for deleting an entry from the guestbook. function deleteUrl( id ) deleteUrl = myselfUrl(MODE_DELETE) & "&id=" & Server.UrlEncode(id) end function function naviUrl( page ) naviUrl = myselfUrl(MODE_NORMAL) & "&pg=" & page end function ' /////////////////////////////////////////////////////////////////////////// ' // special helper functions. ' creates a connection to the database. function openDb() set openDb = Server.CreateObject("ADODB.Connection") openDb.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Persist Security Info=False;" & _ "Data Source=" &Server.MapPath("guestbook.mdb") end function ' insert a new entry to the guestbook. ' returns true when successfull, false when failed. function insertEntry( byref conn ) dim name dim email dim text ' read parameters. name = Request("gb_name") email = Request("gb_email") text = Request("gb_text") ' check parameters. if name="" then name = IDS_ANONYMOUS if text="" then insertEntry = false exit function end if if Instr(text,"href")<>0 then insertEntry = false exit function end if ' open recordset. dim rs set rs = Server.CreateObject("ADODB.Recordset") rs.Open "Guestbook", conn, 2, 3 ' create new recordset. rs.AddNew rs("Date") = Now rs("Name") = name if email<>"" then rs("EMail") = email else rs("EMail") = null end if rs("Text") = text rs.Update ' succeeded. insertEntry = true end function ' delete an entry from the guestbook. ' returns true when successfull, false when failed. function deleteEntry( byref conn ) ' read parameters. dim id id = Request("id") ' check parameters. if id="" then deleteEntry = false exit function end if on error resume next conn.Execute "DELETE FROM Guestbook WHERE No=" &id if err<>0 then deleteEntry = false else deleteEntry = true end if end function ' check, whether a mode is currently active. function hasMode( mode_chk ) if (CLng(mode) and CLng(mode_chk))<>0 then hasMode = true else hasMode = false end if end function ' /////////////////////////////////////////////////////////////////////////// ' // "main function" and html code. ' open the db. dim conn set conn = openDb ' *************************************************************************** ' new entry. if hasMode(MODE_INSERT) then ' when a new entry is posted, insert it. if not insertEntry(conn) then Response.Redirect errorUrl(IDS_FIELDSMISSING) else Response.Redirect normalUrl end if end if ' *************************************************************************** ' delete entry. if hasMode(MODE_DELETE) then ' when an entry needs to be delete, do it. if not deleteEntry(conn) then Response.Redirect errorUrl(IDS_ERRORDELETE& " : " &err.Description) else Response.Redirect normalUrl end if end if ' *************************************************************************** ' count the total number of entries. dim rs set rs = conn.Execute("SELECT COUNT(*) FROM Guestbook") dim sum_cnt sum_cnt = rs(0) ' *************************************************************************** ' calculate: pages, start, end, etc. dim cur_page if Request("pg")="" then cur_page = 0 else cur_page = Request("pg") end if dim total_pages total_pages = CLng(CLng(sum_cnt)/CLng(HITS_PER_PAGE)) if (CLng(CLng(sum_cnt) mod CLng(HITS_PER_PAGE))>0) and _ (CLng(sum_cnt)>CLng(HITS_PER_PAGE)) then total_pages = total_pages+1 end if if total_pages<=0 then total_pages=1 ' the # of the displayed hits. dim hit_display_start, hit_display_end hit_display_start = 1+cur_page*HITS_PER_PAGE hit_display_end = hit_display_start+min(CLng(sum_cnt),CLng(HITS_PER_PAGE))-1 if hit_display_end>sum_cnt then hit_display_end=sum_cnt ' --------------------------------- ' the numbers must be displayed descending. dim cntdwn_start dim cntdwn_end cntdwn_start = sum_cnt-hit_display_start+1 cntdwn_end = sum_cnt-hit_display_end+1 ' *************************************************************************** ' navigation flags. ' whether there exists a previous or a next page. dim can_prev_page, can_next_page, can_prevornext_page if CLng(cur_page)>0 then can_prev_page=true else can_prev_page=false end if if CLng(cur_page)0) can_last_page = (can_prevornext_page) and (CLng(cur_page)<(CLng(total_pages)-1)) ' *************************************************************************** ' query entries. set rs = conn.Execute("SELECT * FROM Guestbook ORDER BY Date DESC") ' navigate to start-recordset. if not (rs.Bof and rs.Eof) then rs.Move hit_display_start-1 end if ' end of the pure asp-part. ' /////////////////////////////////////////////////////////////////////////// %> <% if hasMode(MODE_ERROR) then %>

<%=IDS_ERROR &" : "& Request("error")%>

<% end if %>
Er zijn <%=sum_cnt%> berichten in het gastenboek.  Dit is pagina <%=cur_page+1%> van <%=total_pages%>  Bericht <%=cntdwn_start%> van <%=cntdwn_end%>.
<%end if%> <%end if%> <%end if%> <%end if%>
<% if can_first_page then %>Eerste<% if can_prev_page then %>Vorige<% if can_next_page then %>Volgende<% if can_last_page then %>Laatste
<% ' display discrete pages to jump directly to. dim i for i=1 to total_pages %> <% next %>
Pagina <%=i%> 
<% ' *************************************************************************** ' output all entries. dim cntdwn cntdwn = cntdwn_start dim cnt cnt = 0 while not rs.Eof and CLng(cnt) <% cntdwn = cntdwn-1 rs.MoveNext wend %>
<%=cntdwn%>. <%if e then%>" href="mailto:<%=rs("EMail")%>"><%end if%><%=rs("Name")%><%if e then%><%end if%> <%=fmtDate(rs("Date"))%> <%if hasMode(MODE_ADMIN) then%> ">Delete entry<%end if%>
<%=fmtText(rs("Text"))%>