<%@ Language=JavaScript %>
<% /*
    $Revision$
    $Id$

    File:           fac_StreamFile.asp
    Description:    Streamt een systeemfile. Internal use only, daarom minstens PRSSYS

    Parameters:     logger : 0=reguliere logfile, 1=putorders, 2=immediate putorders, 3=AiAi
                    ago    : aantal maanden (Putorders) of dagen (AiAi) terug
                    dtstamp: (AiAi): vaste datum
    Context:
    Note:           Let op dat we wel met de Responsebuffer limiet zitten.
                    Ongedocumenteerd wordt echter (via function StreamFile) de
                    parameter zipfile=1 ondersteund
*/ %>
<%
DOCTYPE_Disable = 1;
var TERMS_CONDITIONS_OK = true;
var Logging_disable = 1; // Anders geeft *opvragen* van de logfile al logging

if (Application("otap_environment") == "O")
    ANONYMOUS_Allowed = 1;

%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../shared/FlexFiles.inc" -->
<%
    Application("otap_environment") == "O" || user.checkAutorisation("WEB_PRSSYS"); // Sommige files worden we nog strenger

    var logger = getQParamInt("logger", -1); // 0=normal, 1=puo_scheduled, 2=puo_immediate. 3=aiai

    if (logger == 0)
    {
        Application("otap_environment") == "O" || user.checkAutorisation("WEB_FACTAB");
        var prev = getQParam("prev", ""); // "2025-08-13" for log_PROR_2025-08-13.html
        if (prev)
        {
            var logfilename = safe.filename("log_" + customerId + "_" + prev + ".html");
        }
        else
        {
            var logfilename = "log_" + customerId + ".html";
        }
        var filePath = shared.tempFolder() + "/";
        StreamFile(filePath, logfilename, { mime: "text/html", pcontentdp: "inline"});
        Response.End;
    }

    if (logger == 1 || logger == 2) // Putorders
    {
        var ago = getQParamInt("ago", 0);
        var dt = new Date();
        dt.setMonth(dt.getMonth() - ago); // zoveel maanden in het verleden
        var month = dt.getMonth() + 1;
        var year = dt.getYear();
        var schedulelog = logger == 1;
        var logname = "putorders" + (schedulelog?"":"I") + "_" + customerId + "_" + year + "_" + padout(month) + ".log";
        var pathname = shared.tempFolder() + "/";
        var filePath = pathname;
        StreamFile(filePath, logname, { mime: "text/plain", pcontentdp: "inline"});
        Response.End;
    }

    if (logger == 3) // AiAi
    {
        Application("otap_environment") == "O" || user.checkAutorisation("WEB_FACTAB");
        var f = fso.GetFolder(Server.MapPath(rooturl + "/") + "/temp/");
        var filePath = Server.MapPath(rooturl + "/") + "/temp/_AiAi_error";
        if (!fso.FolderExists(filePath))
            abort_with_warning("No AiAi folder?");

        var dtstamp = getQParamInt("dt", 0);
        if (dtstamp > 0)
        {
            var dt = new Date(dtstamp);
        }
        else
        {
            var ago = getQParamInt("ago", 0);
            var dt = new Date();
            dt.setDate(dt.getDate() - ago); // zoveel dagen in het verleden
        }
        var nowtxt = toISODateString(dt);
        var logfilename = "log_" + nowtxt + ".html";
        StreamFile(filePath, logfilename, {mime: "text/html", pcontentdp: "inline"});
        Response.End;
    }

    if (logger == 4) // hMailserver event
    {
        Application("otap_environment") == "O" || user.checkAutorisation("WEB_FACTAB");
        var filePath = "c:\\Program Files (x86)\\hMailServer\\Logs\\";
        var logfilename = "hmailserver_events.log";
        var ago = getQParamInt("ago", 0);
        if (ago > 0)
        {
            var dt = new Date();
            dt.setDate(dt.getDate() - ago); // zoveel dagen in het verleden
            var nowtxt = toISODateString(dt);
            var logfilename = "hmailserver_events_" + nowtxt + ".log";
        }
        StreamFile(filePath, logfilename, { mime: "text/plain", pcontentdp: "inline"});
        Response.End;
    }

    if (logger == 5) // Performance (requests/sec)
    {
        user.checkAutorisation("WEB_FACFAC");
        var csvname = getQParamSafe("csvname");
        var filePath = "D:\\Apps\\Tools\\LogParser\\";
        var logfilename = csvname + ".csv";
        StreamFile(filePath, logfilename, { mime: "text/csv", pcontentdp: "inline"});
        Response.End;
    }

    if (logger == 6) // Database (TNS-) errors
    {
        user.checkAutorisation("WEB_FACFAC");
        var filePath = shared.tempFolder() + "/../";
        var logfilename = "log_database.html";
        StreamFile(filePath, logfilename, { mime: "text/html", pcontentdp: "inline"});
        Response.End;
    }

    if (logger == 7) // Tasker
    {
        user.checkAutorisation("WEB_FACFAC");
        var ago = getQParamInt("ago", 0);
        var dt = new Date();
        dt.setMonth(dt.getMonth() - ago); // zoveel maanden in het verleden
        var month = dt.getMonth() + 1;
        var year = dt.getYear();
        var logname = "tasker" + "_" + customerId + "_" + year + "_" + padout(month) + ".log";
        var pathname = shared.tempFolder() + "/";
        var filePath = pathname;
        StreamFile(filePath, logname, { mime: "text/plain", pcontentdp: "inline"});
        Response.End;
    }

%>
<% ASPPAGE_END(); %>
