Files
CRM/query_file_download.php
BACHIR SOULDI 2794e62571 first commit
2025-09-28 08:49:37 +01:00

69 lines
2.4 KiB
PHP

<?php
require_once 'include/utils/utils.php';
require_once 'includes/Loader.php';
require_once 'include/Webservices/SessionManager.php';
vimport ('includes.runtime.EntryPoint');
$accessToken = "0Ge*PhfL^V3J*1JEp26yiW*p#1DQb4UvU1oOy6PQxlep&ycIsf";
function getFileDetails($attachmentId) {
$db = PearDatabase::getInstance();
$fileDetails = array();
$query = "SELECT * FROM vtiger_attachments WHERE attachmentsid = ? ";
$params = array($attachmentId);
$result = $db->pquery($query, $params);
while($row = $db->fetch_array($result)){
if(!empty($row)){
$fileDetails[] = $row;
}
}
return $fileDetails;
}
function downloadFile($attachmentId) {
$attachments = getFileDetails($attachmentId);
// print_r($attachments);die;
if(is_array($attachments[0])) {
$fileDetails = $attachments[0];
} else {
$fileDetails = $attachments;
}
$fileContent = false;
if (!empty ($fileDetails)) {
$filePath = $fileDetails['path'];
$fileName = $fileDetails['name'];
$fileName = html_entity_decode($fileName, ENT_QUOTES, vglobal('default_charset'));
$savedFile = $fileDetails['attachmentsid']."_".$fileName;
$fileSize = filesize($filePath.$savedFile);
$fileSize = $fileSize + ($fileSize % 1024);
if (fopen($filePath.$savedFile, "r")) {
$fileContent = fread(fopen($filePath.$savedFile, "r"), $fileSize);
header("Content-type: ".$fileDetails['type']);
header("Pragma: public");
header("Cache-Control: private");
header("Content-Disposition: attachment; filename=\"$fileName\"");
header('Content-Length: ' . $fileSize);
header("Content-Description: PHP Generated Data");
header("Content-Encoding: none");
}
}
echo $fileContent;
}
if($accessToken == $_POST['accesstoken']) {
$fileid = $_POST['file_id'];
if(isset($fileid)) {
downloadFile($fileid);
}
} else{
$httpStatusCode = 401;
$httpStatusMsg = 'Request lacks valid authentication credentials for the target resource';
$phpSapiName = substr(php_sapi_name(), 0, 3);
if ($phpSapiName == 'cgi' || $phpSapiName == 'fpm') {
header('Status: '.$httpStatusCode.' '.$httpStatusMsg);
} else {
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
header($protocol.' '.$httpStatusCode.' '.$httpStatusMsg);
}
}