We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 43888
    • 19 Posts
    hi,
    in my site the users can upload pictures, and the files are correctly placed in /assets/uploads
    i noticed that, knowing the file name, anyone typing the url http://mysite/assets/uploads/guessfilename the browser is allowed to see it, while the uploaded file is intended to be seen only by the same user that uploaded it.
    what is the best way to accomplish this in your opinion?
    thanks in advance
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      Mark Hamstra Reply #2, 13 years ago
      If you want to make sure there is no way a file name can be guessed, you'll have to store them outside the webroot. But then to show them, you would need to add some sort of controller that checks if the user is supposed to be able of viewing it or not, and only if that is allowed show the image.

      A slightly easier way would be to rename file names to a random hash on upload. That will make guessing the filename a whole lot harder, but not impossible.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 43888
        • 19 Posts
        Quote from: markh at Oct 01, 2013, 10:21 AM
        If you want to make sure there is no way a file name can be guessed, you'll have to store them outside the webroot. But then to show them, you would need to add some sort of controller that checks if the user is supposed to be able of viewing it or not, and only if that is allowed show the image.

        A slightly easier way would be to rename file names to a random hash on upload. That will make guessing the filename a whole lot harder, but not impossible.

        i think that using a random name is not enough, i like more the idea of storing the files outside webroot... any idea on how to implement that controller?
        thanks again
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          You can use Static Resources to represent the files. With a bit of fiddling you can even use a Media Source with the static resource; basically you need to have some method of prepending the Media Source's base path to the path of the file in the Static Resource. You can even upload a file while selecting the path from the drop-down while editing the Static Resource. You get the MODx file browser; select your Media Source, and select or upload and select files. It can get interesting if you need to upload large files, you need to make sure that your web server, your PHP configuration and the MODx system settings allow the upload of such large files.

          Right now I use a chunk with the same path as the Media Source's base path, [[$pathPrefix]]secure/images/image.png, so that I can edit the chunk if I ever change the base path of the Media Source, rather than saving the full path to the database. MODx will oblige by processing the chunk to result in the full path.

          I'm working on a plugin to automatically fetch the base path of a given Media Source, taken from a TV, and re-write the static resource's path at run-time.
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 43888
            • 19 Posts
            Quote from: sottwell at Oct 02, 2013, 05:00 AM
            You can use Static Resources to represent the files. With a bit of fiddling you can even use a Media Source with the static resource; basically you need to have some method of prepending the Media Source's base path to the path of the file in the Static Resource. You can even upload a file while selecting the path from the drop-down while editing the Static Resource. You get the MODx file browser; select your Media Source, and select or upload and select files. It can get interesting if you need to upload large files, you need to make sure that your web server, your PHP configuration and the MODx system settings allow the upload of such large files.

            Right now I use a chunk with the same path as the Media Source's base path, [[$pathPrefix]]secure/images/image.png, so that I can edit the chunk if I ever change the base path of the Media Source, rather than saving the full path to the database. MODx will oblige by processing the chunk to result in the full path.

            I'm working on a plugin to automatically fetch the base path of a given Media Source, taken from a TV, and re-write the static resource's path at run-time.

            thank you for your answer, but i'm afraid it's a bit too complex for me smiley
            i'll look for some documentation to support me and better understand, for the moment i can only thank you
              • 28120
              • 380 Posts
              The way I do it is by using cookies.

              Create a .htaccess file in the upload folder

              RewriteEngine on
              RewriteCond %{HTTP_COOKIE} !(myCookie=someObscureString) 
              RewriteRule ^(.*) http://www.domain.com/error-page.html [L]
              


              The above means if a cookie isn't set they get re-directed to error-page.html

              When a user logs on I set the cookie.

              One downside is that it's the same cookie for everyone so I use it for things like marketing events allowing attendees to see post event materials.

              If this downside is ok, groups not individuals, then it's simple.