CoverYourASP --> Serving up the source

Free membership

Join in the fun! Sign in
Member Services

Site navigation
Download the entire site!
Search my articles
Free Magazines
Browse the directory

Send me feedback
Buy my boxer shorts

Recommend this page
Printer-friendly page

Resources I recommend
Link to my site
Advertising slashed!
About your privacy
Legal stuff
Site statistics
44 active users
5692 visitors today
4879 pages today
how is this done?
Tools I use

CoverYourASP
Copyright © 1999-2016 James Shaw.
All rights reserved.

ASP.NET Blog
RSS submissions
E-commerce

Now open source with SourceForge!

In order to serve up the actual source code for my ASP pages I use the FileSystemObject. This component allows you to create, modify or delete files on the server, given the correct o/s permissions of course.

The source code for the ShowSource.asp page is shown below. This is called to display the source code for a page whose name is passed in the QueryString. If you haven't read the discussion of how all the pages of my site take advantage of SSI, read that first.

Did you know you can download all the source code (and the database) of this site? Then get my newsletter to be emailed when I update the source code!

Please spread the word by recommending my site to your friends and colleagues!

This is JScript (server-side JavaScript), not the more common VBScript. More...

ShowSource.asp

<!--#include file = "/include/Startup.html"-->
<!--#include file = "/utils/ShowFile.html"-->

<%
// ============================================
// NOTE: all source code downloaded from CoverYourASP was written by
// James Shaw (unless stated otherwise), and is copyright (c) 2000-2002
// by James Shaw. You can use the code for any purpose, but do not
// publish or distribute the content in any way.
//
// See http://CoverYourASP.com/Legal.asp for up-to-date details.
// ============================================

// get the page to display from the URL
var sSourcePage = "" + Request.QueryString ( "page" );

// strip asp extension if given
sSourcePage = sSourcePage.replace ( /.asp/i, '' );

var sSourcePageLWC = sSourcePage.toLowerCase ( );

// make sure it's a page I allow them to view!
switch ( sSourcePageLWC )
{
case "wider":
case "articleadmin":
case "articlecolumns":
case "com":
case "handle404":
case "handle500100":
case "categories":
case "categorypage":
case "columns":
case "contestprizes":
case "contestwinner":
case "dbadmin":
case "dbedittable":
case "dbeditrecord":
case "dbaddrecord":
case "dbdeleterecord":
case "dbshowtables":
case "formatting":
case "resultspage":
case "date":
case "default":
case "contact":
case "subscribe":
case "mailtolist":
case "mailtomembers":
case "debug":
case "rotate":
case "utils/init":
case "utils/footer":
case "recommend":
case "surveydescr":
case "validateemail":
case "memberlogin":
case "memberservices":
case "memberdelete":
case "memberedit":
case "personalize":
case "showdiary":
case "sendpassword":
case "testclasses":
case "bannerreports":
case "allcontent":
case "searchsurvey":
case "utils/banners":
case "utils/browser":
case "utils/database":
case "utils/email":
case "utils/footer":
case "utils/format":
case "utils/header":
case "utils/init":
case "utils/login":
case "utils/news":
case "utils/newsletter":
case "utils/pageclasses":
case "utils/personalize":
case "utils/search":
case "utils/showcategory":
case "utils/showfile":
case "utils/survey":
case "utils/walkfolders":
   break;

case "work":   // this page was renamed
   sSourcePage = 'default';
   break;

case "":
case "undefined":
default:
   Init ( "Oops - cannot view that source" );
   Header ( 'Cannot view source file' );
   BadContent ( sSourcePage );
   Footer ( );

   // stop the script - go no further!
   Response.End ( );
   break;
}

// increment the parent articles counter
sIncArticlePage = sSourcePage + '/.html';

// output relevant meta tags
Init( "ASP source example" );

// output common top of page
// dont show hyperlink if it's in a subfolder
if ( -1 != sIncArticlePage.indexOf ( '/' ) )
   Header( sIncArticlePage + '"</a> --> Source' );
else
   Header( '<a href="' + sIncArticlePage + '">"' + sIncArticlePage + '"</a> --> Source' );

// output page content
Content ( );

// output common bottom of page
Footer ( );

// ============================================
// the content of this page
// ============================================
function Content ( )
{
   Out ( '<td valign="top" class="content">' );

      // create handle to FileSystemObject
      var oFSO = Server.CreateObject ( 'Scripting.FileSystemObject' );

      // each source file that I show source for could have a related
      // documentation file for us to display before and after the
      // source. for now, I use a generic header and footer file. the
      // 'true' tells ShowFile to pass through any HTML to the browser.

      Out ( 'This page shows the actual source code used on this site. ' );
      
      if ( -1 == sIncArticlePage.indexOf ( '/' ) )
         Out ( 'You can read the article that discusses this code <a href="' + sIncArticlePage + '">here</a>.<p>' );
         
      Out ( '<b>If this is the first CYA source code you\'ve seen you should <a href="/ssi.html">read this overview</a> first.</b><p>' );

      ShowSource ( oFSO, sIncArticlePage, true, true );

      // show any extra utility file(s) too
      switch ( sSourcePageLWC )
      {
      case "rotate":
         ShowSource ( oFSO, '/utils/Banners.html', true, true );
         ShowSource ( oFSO, '/utils/Init.html', true, true );
         break;

      case "wider":
         ShowSource ( oFSO, '/include/Startup.html', true, true );
         ShowSource ( oFSO, '/utils/Header.html', true, true );
         break;

      case "categories":
      case "categorypage":
      case "columns":
         ShowSource ( oFSO, '/utils/ShowCategory.html', true, true );
         break;

      case "debug":
         ShowSource ( oFSO, '/utils/Init.html', true, true );
         break;

      case "formatting":
         ShowSource ( oFSO, '/utils/Format.html', true, true );
         break;
      
      case "surveydescr":
      case "searchsurvey":
         ShowSource ( oFSO, '/utils/Survey.html', true, true );
         break;

      case "utils/footer":
         ShowSource ( oFSO, '/StatCounter.html', true, true );
         break;
      
      case "memberlogin":
         ShowSource ( oFSO, '/utils/Login.html', true, true );
         ShowSource ( oFSO, '/MemberLogout.html', true, true );
         break;

      case "testclasses":
         ShowSource ( oFSO, '/utils/PageClasses.html', true, true );
         break;

      case "default":
      case "allcontent":
         ShowSource ( oFSO, '/utils/Personalize.html', true, true );
         break;
      }

      // did the page include database?
      switch ( sSourcePageLWC )
      {
      case "articleadmin":
      case "categories":
      case "categorypage":
      case "columns":
      case "default":
      case "allcontent":
      case "resultspage":
      case "subscribe":
      case "mailtolist":
      case "surveydescr":
      case "memberlogin":
      case "memberdelete":
      case "memberedit":
      case "personalize":
      case "showdiary":
      case "sendpassword":
      case "bannerreports":
         ShowSource ( oFSO, '/utils/Database.html', true, true );
         break;
      }

      // did the page include email?
      switch ( sSourcePageLWC )
      {
      case "contact":
      case "subscribe":
      case "mailtolist":
      case "recommend":
      case "validateemail":
      case "handle404":
      case "handle500100":
      case "memberdelete":
      case "memberedit":
      case "sendpassword":
         ShowSource ( oFSO, '/utils/Email.html', true, true );
         break;
      }

      // show any extra info (e.g. db tables)
      switch ( sSourcePageLWC )
      {
      case "categories":
      case "categorypage":
      case "columns":
         Out ( '<h5>The Categories table</h5>' );

         Out ( '<p>The layout of the table is simple. There are three fields: CategoryID, ParentCategoryID and TopCategoryID. The entire database is contained in the <a href="/CYA.html">download</a>.' );

         Out ( '<p><img src="images/Categories.jpg">' );
         break;

      case "resultspage":
         Out ( '<h5>The Content table</h5>' );

         Out ( '<p>The layout of the table is simple, but the entire database is contained in the <a href="/CYA.html">download</a>!' );

         Out ( '<p><img src="images/Content.jpg">' );
         break;

      case "subscribe":
      case "mailtolist":
         Out ( '<h5>The MailingList table</h5>' );

         Out ( '<p>The layout of the table couldn\'t be much simpler - there\'s only one field! The entire database is contained in the <a href="/CYA.html">download</a>!' );

         Out ( '<p><img src="images/MailingList.jpg">' );
         break;
      }

      // show the generic footer
      Out ( '<p>Hopefully much of this is self-explanatory. If not, or if you see ways that I can improve the code, please <a href="/contact.html">drop me a line</a>.' );

      Out ( '<p>To see the source code for <i>this</i> page, click on the icon below.' );

      // I've finished with the object so free the resource
      oFSO = null;

      Out ( '<p><center><a href="/FSO.html"><img src="/images/source.gif" border=0></a></center>' );

      ShowBottomBanner()

   Out ( '</td>' );
   Out ( '<td background="/images/gx/navgap.gif" valign="top">' );

      // show rotating banners
      ShowBanners ( 8 );

   Out ( '</td>' );
}

// ============================================
// what happens if no source file is specified, or it's an invalid one
// ============================================
function BadContent ( sFile )
{
   Out ( '<td valign="top" class="content">' );

      Out ( 'Sorry, but you didn\'t specify a valid filename to view.' );

      Out ( '<p>Please try looking for the article in question on our <a href="/default.html">front page</a>, <a href="/Search.html">search for it</a>, or <a href="/Contact.html">contact me</a> for help.' );

      // tell me about it, in case it's a bad link!
//      SendEmail ( 'ShowSource', 'InvalidSource@' + sHostDomain, '', 'Invalid source file requested', 'ShowSource could not display the source code for ' + sFile + ' when requested from ' + Request.ServerVariables ( 'HTTP_REFERER' ) );

      ShowBottomBanner()

   Out ( '</td>' );
   Out ( '<td background="/images/gx/navgap.gif" valign="top">' );

      // show rotating banners
      ShowBanners ( 1 );

   Out ( '</td>' );
}
%>

utils/ShowFile.asp

<%
// ============================================
// NOTE: all source code downloaded from CoverYourASP was written by
// James Shaw (unless stated otherwise), and is copyright (c) 2000-2002
// by James Shaw. You can use the code for any purpose, but do not
// publish or distribute the content in any way.
//
// See http://CoverYourASP.com/Legal.asp for up-to-date details.
// ============================================

// have we advertized our newsletter yet?
var bDoneLink = false;
var sPreSearch = '<span style="color: #000000; background-color: #66ffff; border: 0px #66ffff;">';
var sPostSearch = '</span>';

// ============================================
// display the contents of the given file
// ============================================
function ShowFile ( oFSO, sFile, bPassHTML, bShowName, bLiveLinks, sSearch )
{
   var ForReading = 1;
   // var ForWriting = 2;
   // var ForAppending = 8;

   // open asp file for reading
   var fFile = oFSO.OpenTextFile ( Server.MapPath( sFile ), ForReading );

   // read entire file contents into variable
   var s = fFile.ReadAll ( );

   // close the file ASAP to free resources
   fFile.Close ( );

   if ( !bPassHTML )
   {
      // replace & with &amp; so HTML displayed, not interpreted
      s = s.replace ( /&/g, '&amp;' );

      // replace < with &lt; so HTML displayed, not interpreted
      s = s.replace ( /</g, '&lt;' );

      // replace newline with HTML equivalent
      s = s.replace ( /\n/g, '<br>' );

      // replace 2 spaces with non-breaking spaces
      s = s.replace ( /  /g, '&nbsp;&nbsp;' );

      // replace tabs with 3 spaces
      s = s.replace ( /\t/g, '&nbsp;&nbsp;&nbsp;' );

      // find hyperlinks
      if ( bLiveLinks )
         s = s.replace ( /(http\:\/\/([a-zA-Z0-9=\?\&\-\.\_\;\/])*)/gi, '<a href="$1" target="CYAExternal" rel="nofollow">$1</a>' );
//         s = s.replace ( /(https?\:\S*)/gi, '<a href="$1" target="CYAExternal" rel="nofollow">$1</a>' );

      // find search strings
      if ( sSearch != undefined && sSearch.length )
      {
         var re = new RegExp ( sSearch, 'gi' );  
         s = s.replace ( re, sPreSearch + sSearch + sPostSearch );
      }

      // change font color for source code
      s = '<font color="black">' + s + '</font>';

      // show filename if wanted
      if ( bShowName )
         s = '<h4>' + sFile + '</h4>' + s;
   }

   // output the data
   Out ( s );
}

// ============================================
// show a source file outside the table
// ============================================
function ShowSource ( oFSO, sFile, bShowName, bShowLink )
{
   // advertize our newsletter before the first source file
   if ( bShowLink && !bDoneLink )
   {
      bDoneLink = true;
      Out ( '<p><a href="/CYA.html"><img src="/images/source.gif" align="left" hspace="10" border="0"></a>Did you know you can <a href="/CYA.html">download</a> all the source code (and the database) of this site? Then <a href="/subscribe.html">get my newsletter</a> to be emailed when I update the source code!' );

      Out ( '<p>Please spread the word by <a href="/Recommend.html">recommending my site</a> to your friends and colleagues!' );

      Out ( '<p><table bgcolor="#dddddd"><tr><td>' );
         Out ( 'This is JScript (server-side JavaScript), not the more common VBScript.</b> <a href="/Snippet_snip_49.html">More...</a>' );
      Out ( '</td></tr></table>' );
   }

   Out ( '<p><table cellpadding="10"><tr><td bgcolor="#ff9900">' );

      // show source file
      ShowFile ( oFSO, sFile, false, bShowName, false );

   Out ( '</td></tr></table>' );
}
%>

Featured sponsor
My favorite resources

CoverYourASP Mugs, T-shirts, caps - even Boxer shorts...
I don't make a penny from these, but they're a lot of fun! Don't you need a new mouse mat?


Qualify for Free Trade Magazines

Free subscriptions to industry leading publications for those who qualify!


New Proposal Kit Professional 5.1
Brand yourself as a top professional: create quotes and amazing proposals and get many legal documents free!

The latter saved me 3 times the purchase price on the first day I owned it!


I share my content

Supporting ASPRSS

Do you need a quick and easy way to link to my articles? All the information you need is published with ASPRSS...


See my source code
wherever you see this icon...

You can also download the entire site source code for FREE!