user-specific downloads

  • Posts: 23
  • Thank you received: 0
  • Hikashop Business
1 year 3 months ago #347464

-- Joomla version -- : 4.latest
-- PHP version -- : 8.1

Hello,
I sell books and e-books. I managed to write a Hikashop plugin that adds user-specific information in the header of each page of the pdf (social DRM) of an e-book. It all works nice..thanks to your help in other posts.

The last problem is delivery to the customer. The source file is called book1.pdf (for example) and is in a safe place. After the user-specific information has been added, it is called book1InvoiceNumber.pdf....where InvoiceNumber is derived from $order. I have 10 of these books; some are 5 Mb....some are 100 Mb....so emailing them as an attachment is not an option.

The easiest option to get the adjusted pdf to the customer is to automatically save it in the 'safe' directory, and email the link to the customer. And once in a while clean it up manually. That falls within my technical abilities. By 'safe' I mean out of sight of people who are trying to guess the path to a directory.

The fanciest option would be to use the download options from Hikashop. That is...add the source pdf to the Product -Images and Files, and once the order is confirmed, let Hikashop email the customer with the download link and also the options to limit the number of downloads and the download time. Problem: Currently, Hikashop does not know that the pdf link has changed to a user-specific pdf file. Is it possible to change the link to the pdf file in the user confirmation email, and also on the user-download section? Or formulated differently....is it difficult to do this?

Alain

Please Log in or Create an account to join the conversation.

  • Posts: 81361
  • Thank you received: 13037
  • MODERATOR
1 year 3 months ago #347467

Hi,

That's indeed the solution I would recommend.
HikaShop has the event onBeforeDownloadFile(&$filename, &$do, &$file, $options)
You can implement it in a plugin of the group "hikashop".
$filename contains the full path on the hard driver of the server for the file being requested. So you'll have to change it to the path of a PDF generated for the current order
$do can be set to false in order to cancel the sending of the file.
$file contains an object with data about the file being requested including the order_id in $file->order_id so you can use it to get the data of the order with the loadFullOrder function and generate the modified PDF on the spot.
Doing it like this, you don't need to check the status of the order, or whether the user is authenticated or not, etc. That's already done for you upstream.

Please Log in or Create an account to join the conversation.

  • Posts: 23
  • Thank you received: 0
  • Hikashop Business
1 year 3 months ago #347483

Hello,
I can certainly see how nice that would be!

I gave it a try. I made a new plugin. Without the plugin, I am able to download the original pdf. When I enable the plugin, then I can see that the plugin is being executed. And I am able to access the order number (and therefore all info I need to make new pdfs on the fly).

But I get "File not found" when I click on the download link. So..it trying to change the filename (which is good)...but to the wrong thing. Nothing to see in the log file.

Below is part of the code that I made.

1. I guess that the error must be in $filename. It is the full path + new file name?
2. So, if the original pdf is in /media/com_hikashop/upload/safe/Paper1.pdf and the newly generated file is in /media/com_hikashop/upload/safe/downloads/Paper1.pdf...then that should all work?
3. I assume that the "/home/username/website address/" has to be included in $filename?
4. Could the error be due to the subdirectory downloads being in an .htaccess protected directory?
5. The filename itself can be different (e.g. PaperNewName.pdf) ?

Any idea what is causing the error?

Alain


function onBeforeDownloadFile(&$filename, &$do, &$file, $options){
            if(!@include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_hikashop'.DS.'helpers'.DS.'helper.php')){ return false; }
         
            //Step 1: Get order ID.
            $order_id = $file->order_id;


            //Step 2:  From the manual: $filename is the path to the file name.
            //I assume that this is the path to the new file.
            //As a test, I created a copy of the original pdf and copied
            //it into a directory with the name 'downloads'. Hence,
            //com_hikashop/upload/safe contains the original and
            //com_hikashop/upload/safe/downloads the newly modified pdf for 
            //the customer. Eventually, $filename has to be created at the 
            //very end of this file.
            $filename = "/home/***/***.com/media/com_hikashop/upload/safe/downloads/Paper1.pdf";
            $do = FALSE;
            
          
            //Step 3: Load the complete information of an order 
            //via loadFullOrder.
            $app = JFactory::getApplication();
            $orderClass = hikashop_get('class.order');
            $FullOrder = $orderClass->loadFullOrder($order_id, true, false);
 
            // And from here onward it is piece of cake!

}

Last edit: 1 year 3 months ago by nicolas.

Please Log in or Create an account to join the conversation.

  • Posts: 81361
  • Thank you received: 13037
  • MODERATOR
1 year 3 months ago #347492

Hi,

1. The $filename seems good in your code.

2. It needs to be the full path, like in your code.

3. Yes

4. No. That's the whole point. If you remove the htaccess then anyone could potentially access the files in the folder by directly entering the correct path in his browser.

5. Yes, the file name can be different, as long as it exists.

6. You simply get that error because of the line:

$do = false;
which cancels the file download.
In your case, you don't want to change $do in your plugin.

The following user(s) said Thank You: highstat@highstat.com

Please Log in or Create an account to join the conversation.

Time to create page: 0.061 seconds
Powered by Kunena Forum