Vtiger Customer Portal - fixing bug with file download error

Vtiger Customer Portal - fixing bug with file download error

There is a small bug in the Customer Portal - when trying to download files, an error occurs, as a result of which the user receives a file of zero size. The bug is reproduced not in all cases, but only when trying to download files with incorrect syntax in the file name.

There is a small bug in the Customer Portal - when trying to download files, an error occurs, as a result of which the user receives a file of zero size. The bug is reproduced not in all cases, but only when trying to download files with incorrect syntax in the file name. However, it can be solved by making small edits to the modules/CustomerPortal/modules/CustomerPortal/apis/DownloadFile.php file. It is this file that is responsible for finding and transferring the file you need to the Customer Portal.

So, the first thing you need to do is find the following lines in this file:

$filepathQuery = 'SELECT path,name FROM vtiger_attachments WHERE attachmentsid = ?';

$fileres = $adb->pquery($filepathQuery, array($fileid));

$filepath = $adb->query_result($fileres, 0, 'path');

$filename = $adb->query_result($fileres, 0, 'name');

$filename = decode_html($filename);

You should remove this lines and insert following:

$filepathQuery = 'SELECT path,name,storedname FROM vtiger_attachments WHERE attachmentsid = ?';

$fileres = $adb->pquery($filepathQuery, array($fileid));

$filepath = $adb->query_result($fileres, 0, 'path');

if($adb->query_result($fileres, 0, 'storedname')) { 

     $filename = $adb->query_result($fileres, 0, 'storedname');

} else {

     $filename = $adb->query_result($fileres, 0, 'name');

}

$realfilename = decode_html($adb->query_result($fileres, 0, 'name'));

As a result of analyzing the problem, it turned out that this situation often arises due to problems in the file naming. Therefore, when receiving a request to download a file, we first need to check the actual file name in the file system, and not registered in the database.

We add the actual file name to the realfilename variable. Now we need to change the variable in all places of use, namely, we are looking for the following lines (around 72):

$saved_filename = $fileid."_".$filename;

$filenamewithpath = $filepath.$saved_filename;

$filesize = filesize($filenamewithpath);

$fileDetails = array();

$fileDetails['fileid'] = $fileid;

$fileDetails['filename'] = $filename;

And change it to following:

$saved_filename = $fileid."_".$filename;

$filenamewithpath = $filepath.$saved_filename;

$filesize = filesize($filenamewithpath);

$fileDetails = array();

$fileDetails['fileid'] = $fileid;

$fileDetails['filename'] = $realfilename;

After line 88 find following code:

$fileid = $attachmentDetails['attachmentsid'];

$filename = $attachmentDetails['name'];

$filepath = $attachmentDetails['path'];

And change it to this:

$fileid = $attachmentDetails['attachmentsid'];

$filename = $attachmentDetails['storedname'];

if(empty($filename)){

    $filename = $attachmentDetails['name'];

}

$filepath = $attachmentDetails['path'];

$realfilename = decode_html($attachmentDetails['name']);

And around line 100 change line:

$fileDetails['filename'] = $filename;

To this line:

$fileDetails['filename'] = $realfilename;

After carrying out these manipulations, the files from the Customer Portal must be downloaded correctly.