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

    File:               base64_png_save.asp
    Description:        Opvangscript van BijlagenSignature.asp
    Parameters:
        extfilter       extensie filter
        pkey            ALG_RUIMTE_KEY of ...
        pModule         "ALG" of ...
        pKenmerk_key    ALG_KENMERK_KEY of ...
    Context:            Vanuit uploadform.asp
    Note:               Met TAMPER bescherming!
*/

JSON_Result = true;
%>
<!-- #include file="../Shared/common.inc" -->
<!-- #include file="../Shared/flexfiles.inc" -->
<!-- #include file="../Shared/kenmerk_common.inc" -->
<%
protectQS.verify(); // tamper check
protectRequest.validateToken();

var pKey = getQParamInt("key", -1);
var pNiveau = getQParamSafe("niveau", "");
var pModule = getQParamSafe("module");
var pKenmerk_key = getQParamInt("kenmerk_key", -1);

var params = flexProps(pModule, pKey, String(pKenmerk_key), pNiveau);
__Log(params);
__Log("Opslaan onder: " + params.AttachPath);
CreateFullPath(params.AttachPath);

// Verwijderen doen we altijd als we hier komen; bij insert verwijderen we ook eerst alles om vervolgens de nieuwe handtekening te uploaden.
for (var f=0; f < params.files.length; f++) {
    DeleteFile(params.AttachPath + params.files[f].name, params);
}
var data = getFParam("data", "");

var result = {};
if (data)
{
    var Attachment = data.split(",")[1]; // haal data:image/png;base64, er af
    var now = new Date();
    var SafeName = "signature_{0}-{1}-{2}_{3}-{4}-{5}.png".format(padout(now.getFullYear()),
                                                                  padout(now.getMonth()+1),
                                                                  padout(now.getDate()),
                                                                  padout(now.getHours()),
                                                                  padout(now.getMinutes()),
                                                                  padout(now.getSeconds()));

    try
    {
      encodedString2File(params.AttachPath + SafeName, Attachment, "bin.base64");
    }
    catch (e)
    {
      result.message = L("lcl_shared_upload_error_start") + e.description + L("lcl_shared_upload_error_end");
    }
    var oCrypto = new ActiveXObject("SLNKDWF.Crypto");
    var sql = "BEGIN "
            + "  flx.setflexbijlage"
                    + "(" + safe.quoted_sql(pModule)    // MLD | RES | ....
                    + ", " + pKenmerk_key
                    + ", " + pKey
                    + ", " + safe.quoted_sql(params.AttachSubPath)
                    + ", " + "NULL" // diskfilename
                    + ", " + safe.quoted_sql(SafeName)
                    + ", " + fso.GetFile(params.AttachPath + SafeName).Size
                    + ", " + "SYSDATE"
                    + ", " + safe.quoted_sql(oCrypto.hex_sha1_file(params.AttachPath + SafeName))
                    + ");"
            + " END;";
    Oracle.Execute(sql);

    var transitParam = buildTransitParam(["tmpfolder"]);

    result.safefilename = SafeName;
    var params = flexProps(pModule, pKey, pKenmerk_key, pNiveau);
    var bijlagen_key = (pKey > 0 ? params.files[0].key : -1);
    var fp_params = { mime_type: "image/png"
                    , contentdp: "inline"
                    , name: SafeName
                    , niveau: pNiveau
                    };
    if (pKenmerk_key > 0)
        fp_params.kenmerk_key = pKenmerk_key;
    result.fullPath = protectQS.create(OpenFlexFile(pModule, bijlagen_key, fp_params) + transitParam);
}

result.success = true;
Response.Write(JSON.stringify(result));
Response.End;
%>
<% ASPPAGE_END(); %>
