This topic is locked

Guide 19 - Identify who downloads a file

6/18/2021 5:26:32 PM
PHPRunner Tips and Tricks
fhumanes author

This example arises because one of the developers with phprunner wishes to identify who and when the files of an application are downloaded.

As we all know, by default, phprunner shows the files that have previously been loaded and offers a link to download it. This situation is very powerful and almost always is preferred but sometimes it may interest us to know when and who has downloaded the files.

Objective

Show files associated with a record without the download link and clicking, registering the download and sending email to the system administrator.

DEMO: https://fhumanes.com/download

Test users:

admin/admin
user1/user1

To download the example, access my portal.

For any questions, contact me in my email fernandohumanes@gmail.com

fhumanes author 6/18/2021

Technical solution

The example is very simple and for any expert developer, it has very little to learn with him, but as novice developers may still have, some concepts may be applied to their projects.

img alt
As you can see the "Files" field has the standard aspect, but the download link has been removed.

Pressing on the field, it appears:

img alt
In this case, the standard Phprunner display is displayed, but before showing the records, it has been recorded that the user has "made the download of the files" and Email has been sent to the system administrator.

img alt
To make a special presentation of the "File" field I have defined a "CUSTOM" format with the following code:

$fileArray = my_json_decode($value);
$value = '';

foreach ($fileArray as &$file) {
$image = getIconByFileType($file['type'], $file['usrName']);
$value .= '<img style="vertical-align: middle;" src="images/icons/'.$image.'">'.$file['usrName'].'
';
}

To register download and send email I used the Process record ralues event, with this code:

// Insert table Trace
$data = array();
$data["dnld_files_id"] = $values['id_dnld_files'];
$data["dnld_user_id"] = $_SESSION['id_user'];
$data["date_dnld"] = now();
DB::Insert("dnld_trace", $data );

// Send email to Admiistrator
$email="info@fhumanes.com"; // Email of Administrator
$userName = Security::getDisplayName();
$file = $values['title'];
$id_file = $values['id_dnld_files'];

$message=<<<EOT
Hello,
There has been a download of a file:

ID File: <b>$id_file</b>

Title of the file: <b>$file</b>

User making download: <b>$userName</b>
Greetings,

EOT;
$subject="Download File";
runner_mail(array('to' => $email, 'subject' => $subject,
'htmlbody' => $message, 'charset' => 'UTF-8'));

I attached the project and the backup of the sample database, so that you can install them on your PCs. I remember that you do not provide you with the file directory of the example, so you will have to give new records high with the "admin" user.