home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / links.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  14.3 KB  |  351 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. '******** BEGIN CONSTANTS ********
  15. 'Response.Write Server.MapPath("links.htm")
  16. Const LINKS_FILE = "D:\Webs\asp101\www\samples\links.htm"
  17. '******** END CONSTANTS ********
  18.  
  19. '******** BEGIN FUCTION DECLARATIONS ********
  20. Function GetLinksFileAsString()
  21.     Dim objFSO, objInputFile
  22.     Dim strTemp ' as String
  23.  
  24.     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  25.     Set objInputFile = objFSO.OpenTextFile(LINKS_FILE)
  26.  
  27.     strTemp = objInputFile.ReadAll
  28.  
  29.     objInputFile.Close
  30.     Set objInputFile = Nothing
  31.     Set objFSO = Nothing
  32.     
  33.     GetLinksFileAsString = strTemp
  34. End Function
  35.  
  36. Function GetLinkCount(strFullLinksFileText)
  37.     Dim I ' as Integer
  38.     I = 1
  39.     Do While Instr(1, strFullLinksFileText, "<!-- Begin Link " & I & " -->") <> 0
  40.         I = I + 1
  41.     Loop
  42.     GetLinkCount = I - 1
  43. End Function
  44.  
  45. ' Not called directly by runtime code!
  46. ' - wrapped by the next few functions to encapsulate parameters
  47. Function GetHTMLBetweenStrings(strFullLinksFileText, strStart, strEnd)
  48.     Dim iStart, iEnd
  49.     
  50.     iStart = Instr(1, strFullLinksFileText, strStart) + Len(strStart)
  51.     iEnd = Instr(iStart, strFullLinksFileText, strEnd)
  52.     
  53.     GetHTMLBetweenStrings = Mid(strFullLinksFileText, iStart, iEnd - iStart)
  54. End Function
  55.  
  56. Function GetLinkHTML(iLinkNumber, strFullLinksFileText)
  57.     GetLinkHTML = GetHTMLBetweenStrings(strFullLinksFileText, "<!-- Begin Link " & iLinkNumber & " -->", "<!-- End Link " & iLinkNumber & " -->")
  58. End Function
  59.  
  60. Function GetCommentHTML(iLinkNumber, strFullLinksFileText)
  61.     GetCommentHTML = GetHTMLBetweenStrings(strFullLinksFileText, "<!-- Begin Comment " & iLinkNumber & " -->", "<!-- End Comment " & iLinkNumber & " -->")
  62. End Function
  63.  
  64. Function GetHTMLBetweenLinkAndComment(iLinkNumber, strFullLinksFileText)
  65.     GetHTMLBetweenLinkAndComment = GetHTMLBetweenStrings(strFullLinksFileText, "<!-- End Link " & iLinkNumber & " -->", "<!-- Begin Comment " & iLinkNumber & " -->")
  66. End Function
  67.  
  68. Function GetHTMLBetweenLinks(iPreviousLinkNumber, strFullLinksFileText)
  69.     GetHTMLBetweenLinks = GetHTMLBetweenStrings(strFullLinksFileText, "<!-- End Comment " & iPreviousLinkNumber & " -->", "<!-- Begin Link " & iPreviousLinkNumber + 1 & " -->")
  70. End Function
  71.  
  72. Function GetHeaderHTML(strFullLinksFileText)
  73.     Dim strTemp
  74.     Dim strEnd
  75.     Dim iStart, iEnd
  76.     strTemp = strFullLinksFileText
  77.     
  78.     strEnd = "<!-- Begin Link 1 -->"
  79.     
  80.     iStart = 1
  81.     iEnd = Instr(iStart, strFullLinksFileText, strEnd)
  82.     
  83.     strTemp = Mid(strTemp, iStart, iEnd - iStart)
  84.  
  85.     GetHeaderHTML = strTemp
  86. End Function
  87.  
  88. Function GetFooterHTML(strFullLinksFileText, iLinkCount)
  89.     Dim strTemp
  90.     Dim strStart
  91.     Dim iStart, iEnd
  92.     strTemp = strFullLinksFileText
  93.     
  94.     strStart = "<!-- End Comment " & iLinkCount & " -->"
  95.     
  96.     iStart = Instr(1, strFullLinksFileText, strStart) + Len(strStart)
  97.     iEnd = Len(strFullLinksFileText) + 1
  98.  
  99.     strTemp = Mid(strTemp, iStart, iEnd - iStart)
  100.  
  101.     GetFooterHTML = strTemp
  102. End Function
  103.  
  104. Sub WriteLinksFileToDisk(strFullLinksFileText)
  105.     Dim objFSO, objLinksFile
  106.     Dim strTemp ' as String
  107.  
  108.     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  109.     Set objLinksFile = objFSO.CreateTextFile(LINKS_FILE, True)
  110.  
  111.     objLinksFile.Write strFullLinksFileText
  112.  
  113.     objLinksFile.Close
  114.     Set objLinksFile = Nothing
  115.     Set objFSO = Nothing
  116. End Sub
  117. '******** END FUCTION DECLARATIONS ********
  118.  
  119.  
  120. '******** BEGIN RUNTIME CODE ********
  121. Dim strInput ' as String - will contain the whole links html page!
  122. Dim strOutput ' as String - will contain modified version!
  123. Dim strTemp ' as String - used for string building etc.
  124. Dim iLinkCount ' as Integer - number of links we're dealing with
  125. Dim iLink ' as Integer - stores what link # we're performing the operation on
  126. Dim I ' as Integer - standard loop controller
  127.  
  128. ' Get the current links page and place it into strInput
  129. strInput = GetLinksFileAsString()
  130.  
  131. ' Get the current number of links on the links page
  132. iLinkCount = GetLinkCount(strInput)
  133.  
  134. iLink = Request.QueryString("link")
  135. If IsNumeric(iLink) Then
  136.     iLink = CInt(iLink)
  137. Else
  138.     iLink = 0
  139. End If
  140.  
  141.  
  142. ' Debugging Lines!
  143. 'Response.Write "="
  144. 'Response.Write strInput
  145. 'Response.Write iLinkCount
  146. 'Response.Write GetHeaderHTML(strInput)
  147. 'Response.Write GetLinkHTML(1, strInput)
  148. 'Response.Write GetCommentHTML(1, strInput)
  149. 'Response.Write GetFooterHTML(strInput, iLinkCount)
  150. 'Response.Write "="
  151.  
  152. ' Figure out what we're supposed to do with the file now that we have it!
  153. ' Options implemented are add, edit, delete, and the default case "view"
  154. ' Sort is a choice but has been (in the spirit of my old Calculus text) "left as an exercise for the reader!"
  155. ' If anyone is actually interested, leave me a comment and we MIGHT do it for you!
  156. Select Case Request.QueryString("action")
  157.     Case "add" ' Add a new link - This could be done in less code using the Replace function
  158.         ' If we've already entered new link, write links to links file o/w show add form
  159.         If Request.Form("write") = "yes" Then
  160.             ' Start the new string with the everthing up to the first link
  161.             strOutput = GetHeaderHTML(strInput)
  162.         
  163.             ' Loop through existing links placing them into the new string
  164.             For I = 1 to iLinkCount
  165.                 strOutput = strOutput & "<!-- Begin Link " & I & " -->" & GetLinkHTML(I, strInput) & "<!-- End Link " & I & " -->"
  166.                 strOutput = strOutput & GetHTMLBetweenLinkAndComment(I, strInput)
  167.                 strOutput = strOutput & "<!-- Begin Comment " & I & " -->" & GetCommentHTML(I, strInput) & "<!-- End Comment " & I & " -->"
  168.                 If I <> iLinkCount Then
  169.                     strOutput = strOutput & GetHTMLBetweenLinks(I, strInput)
  170.                 Else
  171.                     strOutput = strOutput & GetHTMLBetweenLinks(I - 1, strInput)
  172.                 End If
  173.             Next 'I
  174.         
  175.             ' Add the new link to the end of the new string
  176.             strOutput = strOutput & "<!-- Begin Link " & iLinkCount + 1 & " -->" & Request.Form("link") & "<!-- End Link " & iLinkCount + 1 & " -->"
  177.             strOutput = strOutput & GetHTMLBetweenLinkAndComment(iLinkCount, strInput)
  178.             strOutput = strOutput & "<!-- Begin Comment " & iLinkCount + 1 & " -->" & Request.Form("comment") & "<!-- End Comment " & iLinkCount + 1 & " -->"
  179.         
  180.             ' Tack on the rest of the html from the page
  181.             strOutput = strOutput & GetFooterHTML(strInput, iLinkCount)
  182.         
  183.             ' Save the new links file to the disk
  184.             WriteLinksFileToDisk(strOutput)                
  185.  
  186.             ' Show options for navigation
  187.             Response.Write Request.Form("link") & " <B>has been added!</B><BR>" & vbCrLf
  188.             Response.Write "<BR>" & vbCrLf
  189.         Else
  190.             %>
  191.             <FORM ACTION="./links.asp?action=add" METHOD="post">
  192.                 <INPUT TYPE="hidden" NAME="write" VALUE="yes"></INPUT>
  193.                 <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  194.                     <TR><TD ALIGN="right"><B>Link:</B> </TD><TD><INPUT TYPE="text" SIZE=80 NAME="link" VALUE="<%= Server.HTMLEncode("<A HREF=""http://www.asp101.com""><IMG SRC=""./images/asp101-100x30.gif"" BORDER=""0""></A>") %>"></INPUT></TD></TR>
  195.                     <TR><TD ALIGN="right"><B>Comment:</B> </TD><TD><INPUT TYPE="text" SIZE=80 NAME="comment" VALUE="The best Active Server Pages site on the net!"></INPUT></TD></TR>
  196.                 </TABLE>    
  197.                 <INPUT TYPE="reset" VALUE="Reset Form">
  198.                 <INPUT TYPE="submit" VALUE="Add Link">
  199.             </FORM>
  200.             <%
  201.         End If
  202.  
  203.         ' Show options for navigation
  204.         Response.Write "<A HREF=""./links.asp"">Back to Link List</A><BR>" & vbCrLf
  205.     Case "edit" ' Edit an existing link
  206.         ' If we've already made changes then write links to links file o/w show change form
  207.         If Request.Form("write") = "yes" Then
  208.             ' Start the new string with the everthing up to the first link
  209.             strOutput = GetHeaderHTML(strInput)
  210.         
  211.             ' Loop through existing links placing them into the new string except for link to be changed
  212.             For I = 1 to iLinkCount
  213.                 If I = iLink Then
  214.                     strOutput = strOutput & "<!-- Begin Link " & I & " -->" & Request.Form("link") & "<!-- End Link " & I & " -->"
  215.                     strOutput = strOutput & GetHTMLBetweenLinkAndComment(I, strInput)
  216.                     strOutput = strOutput & "<!-- Begin Comment " & I & " -->" & Request.Form("comment") & "<!-- End Comment " & I & " -->"
  217.                 Else
  218.                     strOutput = strOutput & "<!-- Begin Link " & I & " -->" & GetLinkHTML(I, strInput) & "<!-- End Link " & I & " -->"
  219.                     strOutput = strOutput & GetHTMLBetweenLinkAndComment(I, strInput)
  220.                     strOutput = strOutput & "<!-- Begin Comment " & I & " -->" & GetCommentHTML(I, strInput) & "<!-- End Comment " & I & " -->"
  221.                 End If
  222.                 If I <> iLinkCount Then
  223.                     strOutput = strOutput & GetHTMLBetweenLinks(I, strInput)
  224.                 End If
  225.             Next 'I
  226.         
  227.             ' Tack on the rest of the html from the page
  228.             strOutput = strOutput & GetFooterHTML(strInput, iLinkCount)
  229.         
  230.             ' Save the new links file to the disk
  231.             WriteLinksFileToDisk(strOutput)                
  232.  
  233.             Response.Write Request.Form("link") & " <B>has been updated!</B><BR>" & vbCrLf
  234.             Response.Write "<BR>" & vbCrLf
  235.         Else
  236.             %>
  237.             <FORM ACTION="./links.asp?action=edit&link=<%= iLink %>" METHOD="post">
  238.                 <INPUT TYPE="hidden" NAME="write" VALUE="yes"></INPUT>
  239.                 <TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
  240.                     <TR><TD ALIGN="right"><B>Link:</B> </TD><TD><INPUT TYPE="text" SIZE=80 NAME="link" VALUE="<%= Server.HTMLEncode(GetLinkHTML(iLink, strInput)) %>"></INPUT></TD></TR>
  241.                     <TR><TD ALIGN="right"><B>Comment:</B> </TD><TD><INPUT TYPE="text" SIZE=80 NAME="comment" VALUE="<%= Server.HTMLEncode(GetCommentHTML(iLink, strInput)) %>"></INPUT></TD></TR>
  242.                 </TABLE>    
  243.                 <INPUT TYPE="reset" VALUE="Reset Form">
  244.                 <INPUT TYPE="submit" VALUE="Update Link">
  245.             </FORM>
  246.             <%
  247.         End If
  248.         
  249.         ' Show options for navigation
  250.         Response.Write "<A HREF=""./links.asp"">Back to Link List</A><BR>" & vbCrLf
  251.     Case "delete" ' Delete an existing link
  252.         If iLinkCount <= 2 Then
  253.             %>
  254.             Currently you are not allowed to remove the last two links!<BR>
  255.             <BR>
  256.             Since this script was designed to work with any HTML formatting, if the number
  257.             of links falls below two, the script would have no way of figuring out what
  258.             type of separator goes between links; therefore, we can't let you delete the
  259.             last two.  If you want to remove one of them, please add another link first!<BR>
  260.             <BR>
  261.             Okay, so you caught us in a bug!  Just think of it as an added feature!
  262.             (The script maintains a minimum number of links so your site never looks lame!)<BR>
  263.             <BR>
  264.             Here's our 3 part rationalization:<BR>
  265.             <OL>
  266.             <LI>If you have two or fewer links, do you really need this script!
  267.             <LI>For those of you trying to delete all the links and re-enter them to avoid doing the sort routine, shame on you, it really isn't that hard!
  268.             <LI>It's a reasonable price to play for the added flexability of allowing you guys to format your own link pages and still being able to use the script to update them!
  269.             </OL>
  270.             <BR>
  271.             <%
  272.         Else
  273.             ' Start the new string with the everthing up to the first link
  274.             strOutput = GetHeaderHTML(strInput)
  275.         
  276.             ' Loop through existing links placing them into the new string except for link to be deleted
  277.             ' Note we also need to decrement the ids of the links after the one removed!
  278.             For I = 1 to iLink - 1
  279.                 strOutput = strOutput & "<!-- Begin Link " & I & " -->" & GetLinkHTML(I, strInput) & "<!-- End Link " & I & " -->"
  280.                 strOutput = strOutput & GetHTMLBetweenLinkAndComment(I, strInput)
  281.                 strOutput = strOutput & "<!-- Begin Comment " & I & " -->" & GetCommentHTML(I, strInput) & "<!-- End Comment " & I & " -->"
  282.                 If I <> iLinkCount - 1 Then
  283.                     strOutput = strOutput & GetHTMLBetweenLinks(I, strInput)
  284.                 End If
  285.             Next 'I
  286.             ' Notice we never hit iLink!
  287.             For I = iLink + 1 to iLinkCount
  288.                 strOutput = strOutput & "<!-- Begin Link " & I - 1 & " -->" & GetLinkHTML(I, strInput) & "<!-- End Link " & I - 1 & " -->"
  289.                 strOutput = strOutput & GetHTMLBetweenLinkAndComment(I, strInput)
  290.                 strOutput = strOutput & "<!-- Begin Comment " & I - 1 & " -->" & GetCommentHTML(I, strInput) & "<!-- End Comment " & I - 1 & " -->"
  291.                 If I <> iLinkCount Then
  292.                     strOutput = strOutput & GetHTMLBetweenLinks(I, strInput)
  293.                 End If
  294.             Next 'I
  295.         
  296.             ' Tack on the rest of the html from the page
  297.             strOutput = strOutput & GetFooterHTML(strInput, iLinkCount)
  298.         
  299.             ' Save the new links file to the disk
  300.             WriteLinksFileToDisk(strOutput)                
  301.  
  302.             ' Show options for navigation
  303.             Response.Write GetLinkHTML(iLink, strInput) & " <B>has been deleted!</B><BR>" & vbCrLf
  304.             Response.Write "<BR>" & vbCrLf
  305.         End If
  306.         
  307.         Response.Write "<A HREF=""./links.asp"">Back to Link List</A><BR>" & vbCrLf
  308.     Case "sort" ' Sort existing links (Yeah Right!?!)
  309.         ' We'll leave this for those experts out ther who feel like tackling it!
  310.         ' Hint: add a column to the view table where the user can enter
  311.         ' integers representing the order they want to put them in.  You could
  312.         ' also just force an alphabetic sort if you exclusively use text links,
  313.         ' but like I said... this one is for ya'll to solve!  (We're just too
  314.         ' tired of looking at this code at this point!)
  315.     Case Else ' Default - "view"
  316.         %>
  317.         <TABLE BORDER=0>
  318.         <TR>
  319.         <TD><TABLE BORDER=1>
  320.             <TR>
  321.             <TD><B>Current Links</B></TD>
  322.             <TD><B>Edit</B></TD>
  323.             <TD><B>Delete</B></TD>
  324.             </TR>
  325.         <% For I = 1 to iLinkCount %>
  326.             <TR>
  327.             <TD><%= GetLinkHTML(I, strInput) %></TD>
  328.             <TD><A HREF="./links.asp?action=edit&link=<%= I %>">Edit This Link</A></TD>
  329.             <TD><A HREF="./links.asp?action=delete&link=<%= I %>">Delete This Link</A></TD>
  330.             </TR>
  331.         <% Next 'I %>
  332.             </TABLE></TD>
  333.         </TR>
  334.         <TR>
  335.         <TD ALIGN="right"><A HREF="./links.asp?action=add">Add a New Link</A></TD>
  336.         </TR>
  337.         </TABLE>
  338.         <%
  339. End Select
  340. ' For those of you who made it this far trying to    ____/_
  341. ' read the code, we salute you!  Just save it and    \--/-/    
  342. ' give it a try already!  It's relatively safe,       \o /
  343. ' just make sure you have a backup copy of your        \/
  344. ' links page before you try it!  It has been know      ||
  345. ' to eat them when they're not commented to its        ||
  346. ' liking!  Besides that it won't hurt anything!       ====
  347. '
  348. ' Okay, so you try drawing a martini in ascii-art.  "Damn-it Jim! We're Scripters, Not Artists!"
  349. %>
  350. <A HREF="./links.htm">View the generated links page!</A>
  351.