rejetto forum

Software => HFS ~ HTTP File Server => HTML & templates => Topic started by: LeoNeeson on October 29, 2018, 10:12:15 PM

Title: Implementing Base64 encoding/decoding as a Macro function
Post by: LeoNeeson on October 29, 2018, 10:12:15 PM
EDIT: As reference, I comment this thread was started here (http://rejetto.com/forum/index.php?topic=11754.msg1064148#msg1064148) and then moved here. The idea started after reading the code of mobil-light_V4.2beta.zip (http://rejetto.com/forum/index.php?topic=11754.msg1064130#msg1064130) by dj (thanks him for the inspiration, Mars for the actual code implementation (http://rejetto.com/forum/index.php?topic=13081.msg1064142#msg1064142) that you will find below, and Rejetto, because he took this idea into consideration). Now it follows the original post...



Offtopic: Reading your template, I always wondered is a file section (in the HFS template), could store a file encoded in Base64, and that be decoded by the server on-the-fly (delivering the decoded output).

I see you have write this:

Code: [Select]
[mystyle.css]
{.mime|text/css.}
a {text-decoration: none}

...and I expected this to deliver the same output (but it doesn't):

Code: [Select]
[mystyle.css]
{.mime|text/css;base64.}
YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==

I already know that is possible to directly write Base64 code, like this:

Code: [Select]
<link rel="stylesheet" type="text/css" href="data:text/css;base64,YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==" />
But I was wondering if is it possible make HFS decode Base64 'on-the-fly' using a macro like: {.mime|text/css;base64.}

Also, neither of the following works:

Code: [Select]
[mystyle.css]
{.base64|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

Code: [Select]
[mystyle.css]
{.base64decode|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

I'm curious to know if this is possible using macros. ???
Title: Re: base64 Re: Responsive small screen template
Post by: dj on October 30, 2018, 05:42:54 AM
I can't test at the moment, but I don't find the scripting command base64decode in the docu (http://www.rejetto.com/wiki/index.php?title=HFS:_scripting_commands).
Why do you need this?
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on October 30, 2018, 09:37:29 AM
they do not exist in hfs, but this is not the realm of the impossible
Title: Re: base64 Re: Responsive small screen template
Post by: rejetto on October 31, 2018, 04:35:37 PM
@leo
i don't understand what's the point of doing something like that
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on October 31, 2018, 09:58:01 PM

But I was wondering if is it possible make HFS decode Base64 'on-the-fly' using a macro like: {.mime|text/css;base64.}

Also, neither of the following works:

Code: [Select]
[mystyle.css]
{.base64|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

Code: [Select]
[mystyle.css]
{.base64decode|YSB7dGV4dC1kZWNvcmF0aW9uOiBub25lfQ==.}

I'm curious to know if this is possible using macros. ???

it would be enough simply to add this in scriptlib.pas before 'encodeuri'

Quote
    if name = 'encode64' then
      result:=base64encode(p);

    if name = 'decode64' then
      result:=base64decode(p);
      

including OverbyteicsMD5and OverbyteicsSha1 in the uses,we can allow two other codifications

 
Quote
   if name = 'encodemd5' then
      result:=strMD5(p);

    if name = 'encodesha1' then
      result:=SHA1ofStr(p);   

the latter two can be used to code the sending of a verification code or password

a wacky example as how encrypt download url and redirect it to be managed in a section

   
Quote
   {.if|{.=|%item-type%|folder.}|<a href="%item-url%">|<a href="/~download?name=%item-name%&id={.encode64|%item-full-url%.}&md5={.encodemd5|%item-url%.}">.}

[download|no log]
<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="refresh" content="0; url={.decode64|{.urlvar|id.}.}">
        <script type="text/javascript">
            window.location.href = "{.decode64|{.urlvar|id.}.}"
        </script>
        <title>Page Redirection</title>
    </head>
    <body>
        <!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->
        If you are not redirected automatically, follow this <a href='{.decode64|{.urlvar|id.}.}'>direct link </a>.
    </body>
</html>
   

Title: Re: base64 Re: Responsive small screen template
Post by: LeoNeeson on November 02, 2018, 08:42:24 AM
@leo
i don't understand what's the point of doing something like that
Why do you need this?
Because I'm a Base64 fan?... ;D Talking seriously, I'm surprised this was not already a Macro, since those functions (base64encode and base64decode) are part of the source code (I thought they were already accessible using a Macro). The code that Mars wrote, would be a useful addition, for many things. I can't make a list of useful cases, but having Base64 encoding/decoding at Macro level, could come handy for many things. Mars added a nice example (and also including MD5/SHA1 encoding), but there are many uses of Base64 (https://stackoverflow.com/a/201502).

it would be enough simply to add this in scriptlib.pas before 'encodeuri'
Code: [Select]
    if name = 'encode64' then
      result:=base64encode(p);

    if name = 'decode64' then
      result:=base64decode(p);
That's great, and an easy addition! :)
Title: Re: base64 Re: Responsive small screen template
Post by: rejetto on November 03, 2018, 06:52:10 PM
ok, i will add these
Code: [Select]
    if name = 'base64' then
      result:=base64encode(p);
    if name = 'base64decode' then
      result:=base64decode(p);
    if name = 'md5' then
        result:=strMD5(p);

but couldn't find the sha1 available
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 09:07:54 PM
for my use I would just add aliases  ;D

encode64=base64|$1
decode64=base64decode|$1
encodemd5=md5|$1

I was based on these two macros, encodeuri and decodeuri,  which was in the sense of continuity

do not confuse the md5 and md5 file macros that have different uses

Title: Re: base64 Re: Responsive small screen template
Post by: LeoNeeson on November 03, 2018, 09:14:03 PM
(Much better having a separated topic) :)

but couldn't find the sha1 available
I think you will find the required 'OverbyteIcsSha1.pas' on \Delphi\vc32 folder of the 'OverbyteIcsV7Gold.zip (http://www.overbyte.eu/arch/OverbyteIcsV7Gold.zip)' file. Mars could confirm this, since he tested this.
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 09:36:01 PM
it seems that I'm not up to date I'm only with OverbyteIcsV6
Quote
// ============================================================================
// D5-implementation of "US Secure Hash Algorithm 1 (SHA1)" (RFC3174)
// Copyright (c) 2001, Juergen Haible.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ============================================================================

{------------------------------------------------------------------------------
Update by F. Piette for ICS (http://www.overbyte.be)
Jan 10, 2004 Defined uint32_t as LongWord instead of LongInt
Jul 23, 2004 Revised SHA1Reset to check for nil reference to comply with RFC-3174
             Made the unit compatible with Delphi 2
May 08, 2007 Added a few casts to uint32_t to constants.
Jul 30, 2007 V2.00 Updated for .NET
Mar 24, 2008 V2.01 Made some changes to prepare code for Unicode
                   Use only AnsiString

------------------------------------------------------------------------------}


 with a portable vesion of Borland® Delphi® for Microsoft® Windows™ Version 10.0.2558.35231

all the resources I add are placed in a separate subdirectory in lib and added to the options in the form $ (BDS) \ lib \ ... ,  this allows my delphi to be transferred to any media as usb key for use  it on any machine
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 09:47:49 PM
@LeoNeeson
do you have what it takes on your machine to compile hfs sources and create your executable?
Title: Re: base64 Re: Responsive small screen template
Post by: LeoNeeson on November 03, 2018, 09:56:34 PM
@LeoNeeson
do you have what it takes on your machine to compile hfs sources and create your executable?
I haven't tested yet, because I need to have all the components listed on developer notes.txt, but I'm working on it (I currently have Portable Delphi and OverByte's ICS component only). I need some time to find the rest and put everything in the correct folder.

...with a portable version of Borland® Delphi® for Microsoft® Windows™ Version 10.0.2558.35231
I recently found that version (since I'm trying to get all the components to build HFS by myself), and I think the version you are referring is: Portable Turbo Delphi Lite 1.0d (http://www.andyaska.com/?act=download&mode=detail&id=34).

I don't know if there is a big difference in the SHA1 implementation of OverbyteIcsV6 and OverbyteIcsV7. I only cited version 7 because I've read (in developer notes.txt), that Rejetto is using that version.
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 10:09:37 PM
If you lack the resources to complete your installation, I will be able to provide them but in a few days, the time to get your hands on the archives
Title: Re: base64 Re: Responsive small screen template
Post by: LeoNeeson on November 03, 2018, 10:22:23 PM
If you lack the resources to complete your installation, I will be able to provide them but in a few days, the time to get your hands on the archives
Yes, I will happy if you could share it with me :) (because I find component installation complex to me). There is no hurry, take your time. (you could send me the link by private message or directly post it here).

EDIT: To make the file more light, you could do a folder comparison between you own portable Delphi folder, and the default package of Portable Turbo Delphi Lite 1.0d. Perhaps you could use a folder diff utility like Meld (http://meldmerge.org/help/folder-mode.html) or WinMerge (http://winmerge.org/?lang=fr) (or any other Directory Comparison utility (https://www.google.com/search?q=Directory+Comparison+utility+for+Windows&sa=N)). Just to be sure, don't forget to make a backup of you portable Delphi before using any of those utilities.
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 10:34:33 PM
I do not bother to compare or install packages, I just have PAS files accessible in Delphi and functional.
Title: Re: base64 Re: Responsive small screen template
Post by: LeoNeeson on November 03, 2018, 10:42:04 PM
I do not bother to compare or install packages, I just have PAS files accessible in Delphi and functional.
Do you mean it's enough to copy all the PAS files in \Turbo Delphi Portable\lib? Doesn't have to make any other changes to make it work? I would appreciate if you could guide me...
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 10:57:39 PM
it's enough for me to check that minor improvements work for hfs, but I know that basically I can not guarantee an executable free of defects, it's just to propose to rejetto the result of modifications
the only times I share the exe is to show the result we can have to use
Title: Re: base64 Re: Responsive small screen template
Post by: LeoNeeson on November 03, 2018, 11:08:15 PM
it's enough for me to check that minor improvements work for hfs, but I know that basically I can not guarantee an executable free of defects, it's just to propose to rejetto the result of modifications
the only times I share the exe is to show the result we can have to use
:) Yes, I already know that. I was referring if you could help me with Delphi component installation, so I could build HFS by myself. But don't worry, I will investigate on how to install everything and make it work. (I wish I could directly speak french with you, without using a translator, since Google Translator sometimes gives me a bad translation). Sorry if my English is not good...

Have a nice weekend everyone! ;)
(I will log-off of the forum now)
Title: Re: base64 Re: Responsive small screen template
Post by: Mars on November 03, 2018, 11:09:47 PM
it is already very late and I have to get up early, we will continue much later if it does not hurry you  ;)
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: rejetto on November 04, 2018, 09:52:40 PM
for my use I would just add aliases  ;D

encode64=base64|$1
decode64=base64decode|$1
encodemd5=md5|$1

I was based on these two macros, encodeuri and decodeuri,  which was in the sense of continuity
do not confuse the md5 and md5 file macros that have different uses

i see, but i don't like having just '64'  instead of 'base64', and the 'encodeuri' name was copied from javascript.
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: rejetto on November 04, 2018, 10:19:02 PM
ok, i've found the sha1
Code: [Select]
    if name = 'sha1' then
        result:=SHA1toHex(sha1OfStr(p));

it's not included in beta6, anyway
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: LeoNeeson on November 11, 2018, 03:44:04 AM
ok, i've found the sha1
Code: [Select]
    if name = 'sha1' then
        result:=SHA1toHex(sha1OfStr(p));

it's not included in beta6, anyway
Do you mean about SHA1 is not included in beta 6, or also Base64 decode and encode macro? Because I did some test, and it seems Base64 macro was not included on beta 6.
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: Mars on November 11, 2018, 11:51:47 AM
And with {.base64 decode| blablabla.} ??
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: LeoNeeson on November 11, 2018, 07:34:53 PM
And with {.base64 decode| blablabla.} ??
{.base64 decode| VGhpcyBpcyBhIHNhbXBsZSB0ZXh0.}
No, I've just tried it and it doesn't work. :-[

@Mars: don't forget to read and reply my last private message...  ;)
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: Mars on November 11, 2018, 10:42:18 PM
good news, the macro works as indicated by rejetto, I checked the wording in the exe, it's good base64decode
Code: [Select]
{.set|encoded| {.base64|this object will store some %symbols% in the javascript space, so that libs can read them.}.}
{.^encoded.}
<hr>
{.base64decode|{.^encoded.}.}
<hr>
{.base64decode| VGhpcyBpcyBhIHNhbXBsZSB0ZXh0.}
<hr>
{.base64decode|dGhpcyBvYmplY3Qgd2lsbCBzdG9yZSBzb21lICVzeW1ib2xzJSBpbiB0aGUgamF2YXNjcmlwdCBzcGFjZSwgc28gdGhhdCBsaWJzIGNhbiByZWFkIHRoZW0= .}
<hr>

concerning your MP, I took note but it's a big recovery work and my upload is not that high,
  we will have to wait, wait and wait  ;D
Title: Re: Implementing Base64 encoding/decoding as a Macro function
Post by: LeoNeeson on November 12, 2018, 07:06:46 AM
good news, the macro works as indicated by rejetto, I checked the wording in the exe, it's good base64decode
Code: [Select]
{.set|encoded| {.base64|this object will store some %symbols% in the javascript space, so that libs can read them.}.}
{.^encoded.}
<hr>
{.base64decode|{.^encoded.}.}
<hr>
{.base64decode| VGhpcyBpcyBhIHNhbXBsZSB0ZXh0.}
<hr>
{.base64decode|dGhpcyBvYmplY3Qgd2lsbCBzdG9yZSBzb21lICVzeW1ib2xzJSBpbiB0aGUgamF2YXNjcmlwdCBzcGFjZSwgc28gdGhhdCBsaWJzIGNhbiByZWFkIHRoZW0= .}
<hr>
Yes, it's working fine now, thanks for the example.

Code: [Select]
[+]
<div style="font-family:tahoma; font-size:12pt;">
{.base64decode| VGhpcyBpcyBhIHNhbXBsZSB0ZXh0.}
</div>