<%@language = "javascript" %>
<% /*
    LET OP: Dit bestand moeilijk te beantwoorden vragen kan opwerpen
            en is dan ook een 'undocumented' feature.

    $Revision$
    $Id$

    File:           appl/fac/fac_verify_sha.asp
    Description:    Verify Facilitor asp files
    Parameters:
    Context:        Manual call to check asp file consistency
                    Werkt in combinatie met de geautomatiseerde mkRelease
                    die in de appl-folder een .sha file met checksums plaatst.
    Note:           FACTAB authorization is required
*/ %>
<!--#include file="../Shared/common.inc"-->
<%
    var autfunction = "WEB_FACTAB";
    var authparams = user.checkAutorisation(autfunction);
    if (Server.ScriptTimeout<600) Server.ScriptTimeout=600;

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var oCrypto = new ActiveXObject("SLNKDWF.Crypto");

    // zoek de nieuwste sha file in de appl folder. Als het goed is, is er maar
    // eentje maar mensen oveschrijven nog wel eens een appl/ ipv vervangen
    var maxrev = -1;
    var shafile;
    var startFolder = fso.GetFolder(Server.MapPath("../"))
    var fc1 = new Enumerator(startFolder.Files);
    for (; !fc1.atEnd(); fc1.moveNext())
    {
      var fil = fc1.item();
      var filename = fil.Name;
      if (filename.match(/svnrevision_r\d*\.sha\.inc/i))
      {
        var rev = parseInt(filename.substring(13), 10);
        if (rev > maxrev)
        {
            maxrev = rev;
            shafile = filename;
        }
      }
    }
    if (maxrev < 0)
    {
        shared.internal_error("No .sha file found in " + Server.MapPath("../"));
        Response.End()
    }
%>
<html>
<head>
    <% FCLTHeader.Generate(); %>

<title>FACILITOR file checksum verifier</title>
</head>
<body id="listbody">
<%

var fsha = fso.OpenTextFile(Server.MapPath("../" + shafile));
var allsha = fsha.ReadAll().split("\n");
if (allsha.length == 1)
    var allsha = allsha.split(/\x0A/); // Misschien alleen UNIX LF's?

fsha.Close();
var ff = fso.GetFile(Server.MapPath("../" + shafile));

Response.Write("<h1>Checking {0} files from appl/{1} ({2})</h1>".format(allsha.length, shafile, toDateTimeString(ff.DateLastModified)));
Response.Flush();

var allfiles = { };
// Some known safe files
allfiles["../appl/" + shafile] = 1;
allfiles["../scanudls.asp"] = 1;
allfiles["../sso_sgf.asp"] = 1;
allfiles["../json2.js"] = 1;
allfiles["../default.asp"] = 1;

%><table class='rstable'>
<thead><tr><th>File</th><th>Message</th><th>Comment</th></tr></thead>
<%
var cntbad = 0;
for (var i in allsha)
{
    var regel = allsha[i];
    var spl = regel.split(" ");
    if (spl.length == 2)
    {
        var sha_should = spl[0];
        var fname = spl[1].substring(1, spl[1].length); // was 'length-1' toen we CR+LF hadden?
        fname = fname.replace(/\\/g,'/');
        allfiles[fname.toLowerCase()] = 1;
        try {
            var sha_is = oCrypto.hex_sha1_file(Server.MapPath("../" + fname));
            if (sha_should != sha_is)
            {   // Revisienummer zoeken kan helaas niet. Leuk bedacht maar uit de minified bestanden is het verwijderd.
                var ff = fso.GetFile(Server.MapPath("../" + fname));
                rev = toDateTimeString(ff.DateLastModified);
                Response.Write("<tr><td>" + fname.replace(/^\.\.\//,"/") + "</td><td>SHA mismatch</td><td>" + rev + "</td></tr>");
                cntbad ++;
            }
        }
        catch(e)
        {
            Response.Write("<tr><td>" + fname.replace(/^\.\.\//,"/") + "</td><td>Error reading</td><td>" + safe.html(e.description) + "</td></tr>");
            cntbad++;
        }
    }
}
%></table><%
if (!cntbad)
    Response.Write("<p>No modified files detected</p>");

Response.Write("<h1>Looking for unknown files</h1>");

var rootdir = Server.MapPath("../../") + "\\";
var cntunknown = 0;
function scanFolder(scanDir, lvl)
{
    if (scanDir.match(/CUST\/$/i))
            return;
    if (scanDir.match(/TEMP\/$/i))
            return;
    if (scanDir.match(/SOAP\/$/i))
            return;
    // Response.Write("<br>------------- ["+scanDir + "] -------------")
    startFolder = fso.GetFolder(rootdir + scanDir)
    var fc1 = new Enumerator(startFolder.Files);
    for (; !fc1.atEnd(); fc1.moveNext())
    {
      var fil = fc1.item();
      var filename = fil.Name;
      if (filename == "web.config") // die is niet erg
        continue;

      if (!(("../" + scanDir + filename).toLowerCase() in allfiles))
      {
        var sha1 = oCrypto.hex_sha1_file(rootdir + scanDir + filename);
        var ff = fso.GetFile(rootdir + scanDir + filename);
        rev = toDateTimeString(ff.DateLastModified);
        Response.Write("<tr><td>" + (scanDir + filename) + "</td><td>" + rev + "</td><td>" + sha1 + "</td></tr>");
        cntunknown++;
        //Response.Write("<br>Unknown file: " + sha1 + " /" + scanDir + filename);
      }
    }
    // En nu recursief alle subfolders.
    var fc1 = new Enumerator(startFolder.SubFolders);
    for (; !fc1.atEnd(); fc1.moveNext())
    {
      fil = fc1.item();
      scanFolder(scanDir + fil.Name + "/", lvl+1);
    }
}
%><table class='rstable'>
<thead><tr><th>File</th><th>Message</th><th>SHA</th></tr></thead>
<%
scanFolder("", 0)
%></table><%
if (!cntunknown)
    Response.Write("<p>No unkwown files detected</p>");


%>
</body>
</html>