• Resolved mickna

    (@mickna)


    One more Question:

    Our client do not need any renaming of the documents. He want the files stored with the original file name.

    Is there a way to bypass the file name encryption?

    Thank you,

    Michael

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor nwjames

    (@nwjames)

    @mickna,

    It is, of course, quite easy to do this. But the real question you need to discuss with your client is to understand what problem you are trying to solve by this.

    The reason why the file name is obscured by the MD5 encoding is to ensure that access is made via the WP front-end and not by knowing/guessing the name and uploading it directly. These reasons are explained in the plug-in text on wordpress.org in the Overview section under Access Control and Enterprise Security.

    So if you really want to give up this designed-in funcionality, then create a file called wpdr_original_name.php in the directory wp-content/mu-plugins and copy this code into it

    <?php
    add_filter( 'wp_handle_upload_prefilter', 'filename_capture', 1 );
    
    if ( ! function_exists('filename_capture') ) {
    	/**
    	 * Save uploaded file name.
    	 *
    	 * @since 0.5
    	 * @param array $file file data from WP.
    	 * @return array $file file with new filename
    	 */
    	function filename_capture( $file ) {
    		global $wpdr_filename;
    		$wpdr_filename = $file['name'];
    		return $file;
    	}
    }
    
    add_filter( 'document_internal_filename', 'revert_filename' );
    
    if ( ! function_exists('revert_filename') ) {
    	/**
    	 * Filters the encoded file name for the attached document (on save).
    	 *
    	 * @param array $file encoded file name.
    	 */
    	function revert_filename( $file ) {
    		global $wpdr_filename;
    		$file['name'] = $wpdr_filename;
    		return $file;
    	}
    }

    The file will generally be saved using the original name – but, of course, if you upload an update to a document with the same name, then the new one will be given a different name to the existing one. This may be not what is wanted.

    I would not recommend that you use this.

    Regards,

    Neil James

    Thread Starter mickna

    (@mickna)

    @nwjames:

    I really appreciate your thoughts and more over your fantastic support and help.

    I am aware of your concerns and if we would have a normal WP I completely agree.

    In our case, we have an Intranet with no connection to the Internet. Also all documents are PDFs and all employs can download all documents. Therefore we do not have any restriction.

    Also every document has a really special naming and it will cost more time to name the custom post like the document name, as just upload this document.

    Again thank you for your thoughts, great support and (for me) a live saving plugin 🙂

    Michael

    • This reply was modified 1 year, 2 months ago by mickna.
    Thread Starter mickna

    (@mickna)

    Woops. I am sorry….

    I can’t see a directory called wp-content/mu-plugins

    If I create one and place the php code, nothing has changed. The file is renamed to post name.

    If I insert the code as a snipped the filter seams to work, but I get

    “” has failed to upload.
    Sorry, you are not allowed to upload this file type.

    For a PDF, which I can upload without these filters.

    Any ideas?

    Thank you,

    Michael

    • This reply was modified 1 year, 2 months ago by mickna.
    Plugin Contributor nwjames

    (@nwjames)

    @mickna,

    I’m sorry that you’re having problems. The upload process being used is that used in the standard WordPress Media upload process. Where these filters come in is to take the file block – which looks something like:

    [name] => 150.pdf
    [type] => application/pdf
    [tmp_name] => /tmp/phprHylFv
    [error] => 0
    [size] => 75421

    The first function acts at low priority and saves the file name – here 150.pdf – and stores it away. [It does this for every upload that you do. The second function acts in the same place (but only for documents) and literally just after the plugin has changed the name to something like 427042a7dd7a3bd7508d990144b2f96d.pdf resets it to 150.pdf.

    So the first test is check whether you can upload your file as a Media file without these filters active. This should work. If it does not, then the problem is outside the plugin.

    Then repeat with the filters active. Again it should work – but if it didn’t then there is a problem with the first filter function used. Then try it as a document. If it fails, then the problem is with the second function text.

    Hope this gives some pointers.

    Just to pick up an element. You state “The file is renamed to post name.”

    What should happen after these filters are executed is that the file contained as $file['tmp_name'] is loaded into the uploads directory using the name held in $file['name']as a basis for both an attachment post and the name as stored in the uploads directory.

    So in my example case, it will seek to create an attachment post with name and title of “150” with the file being stored as …/150.php. If an attachment post already exists with that name, then these will be saved with the name “150-1” and possibly the file name being altered as well to avoid a clash.

    The attachment post will be created so that its parent is set to the document post that you see on the screen.

    There is no process to technically align the document post title to the attachment file name. But I may have read too much into the comment.

    Hope this helps,

    Neil James

    Thread Starter mickna

    (@mickna)

    @nwjames:

    I really, really appreciate your support!

    To check, if I came across function 2 I did:

    add_filter( 'wp_handle_upload_prefilter', 'filename_capture', 1 );
    
    if ( ! function_exists('filename_capture') ) {
    /**
    * Save uploaded file name.
    *
    * @since 0.5
    * @param array $file file data from WP.
    * @return array $file file with new filename
    */
    function filename_capture( $file ) {
    global $wpdr_filename;
    $wpdr_filename = $file['name'];
    $file['name'] = "funct01";
    return $file;
    }
    } add_filter( 'document_internal_filename', 'revert_filename' ); if ( ! function_exists('revert_filename') ) {
    /**
    * Filters the encoded file name for the attached document (on save).
    *
    * @param array $file encoded file name.
    */
    function revert_filename( $file ) {
    global $wpdr_filename;
    $file['name'] = $wpdr_filename;
    $file['name'] = "funct02";
    return $file;
    }
    }

    As I did not specify a suffix, WP outputs:

    “funct02” has failed to upload.
    Sorry, you are not allowed to upload this file type.

    Great! This means I came across fct. 2 = revert_filename.

    If I use a suffix to test further $file[‘name’] = “funct02.pdf”;

    I came back to the upload screen and guess what? The file has the name of the posting and not “funct02.pdf”. Wired….

    So we set a new $file[‘name’] but it seams to be ignored. Sure the hook / filter “document_internal_filename” is the right one at this time?

    Plugin Contributor nwjames

    (@nwjames)

    @mickna

    Thank you for your note.

    I took your copy of your code, added <?php at the top and saved it as a .php file in the mu-plugins directory as I indicated in my first reply.

    I then created a document called Test snd uploaded a file. Or rather I tried to, but like you, since the file name was set to funct02, it was rejected. I then changed the code to set the name to funct02.pdf. I then repeated the upload and it then uploaded successfully.

    The result is as I expected. Assuming that the defaults of the plugin and installation have not been changed – and the site is example.com, then the result of these operations are:

    A document post accessible via http://example.com/documents/test or http://example.com/documents/test.pdf

    A non-accessible attachment post with a name and title set to funct02. This attachment post has as its parent post the document post referred to above and an associated file accessible via http://example.com/wp-content/uploads/2023/04/funct02.pdf

    This file can be accessed whether you are logged on to WordPress instance or not.

    The design objective is that the name of the file on the server is hidden from public view or from directly accessing the file, i.e. it has to be accessed via the WP interface.

    For me, it is working as requested – “He want the files stored with the original file name.” But I wonder whether he asked for the correct thing. Is it the attachment or the document that should be set to the file name? You access the files by the document post name.

    Regards,

    Neil James

    Thread Starter mickna

    (@mickna)

    But Neil,

    there is no mu-plugins directory wp-content/mu-plugins

    Also I can not find a mu-plugins under wp-content/plugins/wp-document-revisions

    If I create a directory wp-content/mu-plugins and place the file there (of course with <?php nothing happens.

    What I did for testing was a copy&paste to function.php (better: as a snippet with snippet plugin)

    I feel stupid :/

    Plugin Contributor nwjames

    (@nwjames)

    @mickna

    Sorry for any confusion on this. Standard WP configuration comes with a directory /wp-content. In there are various sub-directories including /plugins to hold your downloaded plugins, or /uploads to hold your uploaded media.

    Additionally there can be a directory called /mu-plugins. You need to create it as it is not delivered automatically. In it, you can place .php files. They will be executed automatically. You cannot switch them off – you have to remove them. If you go to your plugins page, you will see them listed in the summary (All/ Active/ Inactive/ Recently Active/ Must-Use)

    You can put the code in your functions.php file – but this is linked to the theme being used. You change the theme and you lose the code. Or, you can use a snippets plug-in – as you have done. They are all equivalent.

    Possibly one of the issues with seeing what is happening is that the WP Document Revisions goes to some effort to hide the attachment post from being accessed.

    I will assue that the code is active and you have set the second function to set the file name to funct02.pdfthere. You can go to the “Validate Structure” sub-menu. You should see the document Test (with link http://example.com/documents/test.pdf ) with message Document attachment does not appear to be md5 encoded and a button to Fix it. This checks the filename itself, and not the attachment post name.

    Separately you can look in your file uploads directory – and you should see the file funct02.pdf there.

    When you said that setting the name to just funct02 then the upload failed indicates to me that this code is being executed. So I do not understand why you think nothing happens.

    Just to answer your earlier question. The first function is based on a filter wp_handle_upload_prefilter with priority 1. This is so that it occurs before the WPDR plugin function that runs at standard priority. This is the function that changes the name to the encoded value. This code has its own filter document_internal_filename which occurs immediately after the name is encoded. By using this filter I know that I am only changing the name for Documents because there is check made for them. You could have other plug-ins that manipulate file uploads in their own way.

    Hope this is of use,

    Neil James

    Thread Starter mickna

    (@mickna)

    @nwjames :

    First of all: I am blown away from your efforts to help me. Never ever get this kind of detailed, skillful and patient answer / support.

    “Separately you can look in your file uploads directory” – Yes! I was too lazy and only looked at the downloadlink itself. *shame on me*

    The File is stored with its original name! Bahm!

    The file is downloaded with the name of the posting. This is something I have to fix now. So: Upload is perfect. Download I have to look by myself. I am sure I can handle this, as I believe there is also a WP filter.

    Again: Thank you so much Neil. I really, really appreciate your help and I am so thankful 🙂

    Michael

    Plugin Contributor nwjames

    (@nwjames)

    @mickna

    Thank you for your kind words.

    I’ll mark this as resolved.

    Regards,

    Neil

    Thread Starter mickna

    (@mickna)

    Hello James,

    I just play around and I have one more point:

    it seams, the document is saved with its original file name – great 🙂 But the link, which is generated for “Latest Version of the Document Download” is set to the post title. I am confused:

    If I download the file, it has also the post title as name. However, on the server, the document has its original name. how can I prevent this? My client just wants to preserve his document name also for downloading.

    Thank you,

    Michael

    Thread Starter mickna

    (@mickna)

    I’ll open a new thread 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘No renaming – possible’ is closed to new replies.