X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (https://forum.x-cart.com/forumdisplay.php?f=20)
-   -   Keeping track e-goods downloads (date and ip) (https://forum.x-cart.com/showthread.php?t=61114)

esoftrade 10-05-2011 01:31 AM

Keeping track e-goods downloads (date and ip)
 
I wanted to keep track of the egoods downloads of my site.
I added the great idea from minorgod to add the downloads counter.
But I wanted more so that I can know when and from where an e-good had been downloaded.

There are only 4 simple modifications to do.

Those modifications works for xcart version 4.4.3 but may works also for newer release, it is just a matter of testing :-)

1. Create a new table in DB in your DB console

Code:

CREATE TABLE `xcart_download_keys_downloads` (
  `download_key` char(100) NOT NULL default '',
  `id_download` tinyint(4) NOT NULL default '0',
  `date` int(11) NOT NULL default '0',
  `ip` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`download_key`,`id_download`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;



2. Add the new table in init.php

Add the following statement in the sql_tbl array (preferably right after the download_keys statement:
PHP Code:

'download_keys'                     => 'xcart_download_keys',
         
'download_keys_downloads'           => 'xcart_download_keys_downloads'


3. Add the new table in the delete statements (keep your DB clean)

In the func_delete_product() function: in the if ($delete_all === true) section, add the complete clean of the table:

PHP Code:

db_query("DELETE FROM $sql_tbl[download_keys]");
db_query("DELETE FROM $sql_tbl[download_keys_downloads]"); 


Still in the func_delete_product() function, add a statement to delete content of the table for a given product id (when a product is removed) - you can skip this if you prefer to keep an history of all downloads, even for deleted products:

PHP Code:

db_query("DELETE FROM $sql_tbl[download_keys_downloads] WHERE download_key=(SELECT download_key FROM $sql_tbl[download_keys] WHERE productid='$productid')");
    
db_query("DELETE FROM $sql_tbl[download_keys] WHERE productid='$productid'"); 


Note that this statement must be done before the delete from download_keys. Otherwise, you will not delete anything since the entries will have been deleted! :D/


4. Update the table whenever an e-good is downloaded

In download.php, locate the following statement (line 165 for me):
PHP Code:

if (empty($id)) 
exit(); 

If you added minorgod functionality, you should have it just below.
We will add new code below again (note that minorgod functionality is not required, but it is a good to get idea).

This is to retrieve and increment the id.

PHP Code:

if (empty($id))
    exit();

$res func_query_first("SELECT productid, expires, number_downloads FROM $sql_tbl[download_keys] WHERE download_key = '$id'");
$number_downloads=$res['number_downloads']+1;

$resDownloadTime func_query_first("SELECT MAX(id_download) AS max_id FROM $sql_tbl[download_keys_downloads] WHERE download_key = '$id'");
if (empty(
$resDownloadTime)){
    
$id_download=0;
} else {
    
$id_download=$resDownloadTime['max_id']+1;



Next, update the DB once the e-good has been downloaded:

PHP Code:

if ($fd) {

            while (
                !
feof($fd)
                && 
connection_status() == 0
            
) {

                print(
fread($fd8192));

                
flush();

            }

            
fclose($fd);

            
// Update the nb of times the egood has been downloaded
            
$query="UPDATE $sql_tbl[download_keys] SET number_downloads='$number_downloads' WHERE download_key='$id'";
            
db_query($query);
            
            
// Update the key download information with date and ip
            
$query="INSERT INTO $sql_tbl[download_keys_downloads] (download_key, id_download, date_download, ip) VALUES ('$id', '$id_download', '".XC_TIME."', INET_ATON('$CLIENT_IP'))";
            
db_query($query);
        } else {

            print(
$data);

            
flush();

        } 



That's it! Let me know if this works for you or if you find any enhancement or bug.

Thanks
Fred


All times are GMT -8. The time now is 09:20 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.