We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37032
    • 64 Posts
    Hi there,

    I need a custom md5_file output filter or snippet but I can't find any around. I'm not a system developer unfortunately so can't make one myself sad

    Anyone have something that I can use or even help me make one?

    Thanks / T
      • 1343 ☆ A M B ☆
      • 2,213 Posts
      It would be helpful to know why the default md5 output filter fails to meet your needs, and what you are trying to do.
        Patrick | Server Wrangler
        About Me: Website | Tweets |  MODX Hosting
        • 3749
        • 24,544 Posts
        He wants md5_file, which is different from md5, though we could definitely use more information.

        I'm wondering if a regular snippet might be a better choice:

        [[Md5File? &file=`full/path/to/file`]]



        <?php
        /* MD5File snippet */
        return md5_file($scriptProperties['file']);


        To do it as an output filter would be slower, but if you need that, you'll have to show us what the tag would look like.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 37032
          • 64 Posts
          Sorry for being vague guys.

          My idea was to append a query parameter with the md5 output as value on those CSS, JavaScript files (chunks) that I'm editing often. Why? To get around client-side cache. The nice part with md5_file is if you decide to revert a file, users would most likely still have it cached.

          Let's say I have a chunk with the following content:
          <script src="assets/js/my-module.js"></script>
          


          I would like the output to be:
          <script src="assets/js/my-module.js?build=1447511cd64b5b437824eae28319cab8"></script>
          


          BobRay's Md5File snippet does the job. Thanks! smiley
          <script src="assets/js/my-module.js?build=[[Md5File? &file=`assets/js/my-module.j`]]"></script>
          


          Idea: Would it be possible to access the HTML tag's source or href attribute and get rid of the &file parameter. It would a bit neater because the information is all-ready there so to speak. But if it is at the expense of performance I rather write it manually.

          / T
            • 37032
            • 64 Posts
            After some discussions with a colleague of mine we came up with this solution:

            Chunk:
            <script src="[[Md5File? &file=`assets/js/my.js`]]"></script>
            


            Snippet:
            <?php
            /* MD5File snippet */
            return '/' . $scriptProperties['file'] . '?build=' . md5_file($scriptProperties['file']);
            


            This way you don't have to write the file path twice.
            [ed. note: tomahl last edited this post 13 years ago.]
              • 3749
              • 24,544 Posts
              That's a very cool idea, though I think you'd need to call it uncached for it to work:

              <script src="[[!Md5File? &file=`assets/js/my.js`]]"></script>


              Also, you're going to do a full hash of the entire file every time the page loads and md5_file() is not noted for its speed. This would be about 3x faster if it works on your server:

              <?php
              /* MD5File snippet */
              $path = $scriptProperties['file'];
              $result = split('=',exec("openssl md5 $path"));
              return '/' . $path . '?build=' . $result[1]); 
              


              Much faster yet (for the user) would be to just append a version number manually, though if you've got a lot of files I can see why you might not want to do it that way.

              Here's what I generally do:

              $modx->regClientCSS($path . '?build=' . $version);
              
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 37032
                • 64 Posts
                Quote from: BobRay at Sep 20, 2013, 02:01 PM
                That's a very cool idea, though I think you'd need to call it uncached for it to work:

                <script src="[[!Md5File? &file=`assets/js/my.js`]]"></script>


                Also, you're going to do a full hash of the entire file every time the page loads and md5_file() is not noted for its speed. This would be about 3x faster if it works on your server:

                <!--?php
                /* MD5File snippet */
                $path = $scriptProperties['file'];
                $result = split('=',exec("openssl md5 $path"));
                return '/' . $path . '?build=' . $result[1]); 
                


                Much faster yet (for the user) would be to just append a version number manually, though if you've got a lot of files I can see why you might not want to do it that way.

                Here's what I generally do:

                $modx--->regClientCSS($path . '?build=' . $version);
                

                I tried to call it uncached but it made no difference. I'm not sure that is the best way of doing it anyway. At the moment I just clear the site cache after uploading all modified files and they all get a new "build" value. That way the snippet doesn't have to run on every page load..or am I wrong? Also, you think it would be better to use your snippet anyway?

                Your last line of code doesn't say me much, I'm not a programmer you know. smiley If you got the time, please show me an example of how to use it the full way.

                Not being a programmer is quite frustrating when you got lot of ideas. wink

                Thanks for support Bob.
                [ed. note: tomahl last edited this post 13 years ago.]
                  • 3749
                  • 24,544 Posts
                  Yes, clearing the site cache would take care of the problem and you could call it cached, which should make it much faster (though not as fast as the exec version).

                  As for my snippet, I'm not sure how much faster it would be, but the file's hash value would never have to be calculated. Hash methods like md5 are intentionally somewhat slow to make it harder to do a brute-force attack. My snippet would work like this:

                  [[AddCss? &path=`path/to/file` &version=`1.1`]]


                  /* Add Css snippet */
                  $modx->regClientCSS($scriptProperties['path'] . 
                      '?build=' . $scriptProperties['version']);
                  return '';
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 37032
                    • 64 Posts
                    Quote from: BobRay at Sep 20, 2013, 02:36 PM
                    Yes, clearing the site cache would take care of the problem and you could call it cached, which should make it much faster (though not as fast as the exec version).

                    As for my snippet, I'm not sure how much faster it would be, but the file's hash value would never have to be calculate. Hash methods like md5 are intentionally slow to make it harder to do a brute-force attack. My snippet would work like this:

                    [[AddCss? &path=`path/to/file` &version=`1.1`]]


                    /* Add Css snippet */
                    $modx->regClientCSS($scriptProperties['path'] . 
                        '?build=' . $scriptProperties['version']);
                    return '';

                    My server didn't like your improved version so I'll probably stick with the one I got at the moment. It does the job. smiley

                    Your last snippet would require to much manual work but that's also a handy snippet. Saving it for another day. smiley

                    Thanks