2025-10-16 11:17:21 +03:00
< ? php
/*+***********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ( " License " ); You may not use this file except in compliance with the License
* The Original Code is : vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger .
* Portions created by vtiger are Copyright ( C ) vtiger .
* All Rights Reserved .
*************************************************************************************/
class ModComments_DownloadFile_Action extends Vtiger_Action_Controller {
public function checkPermission ( Vtiger_Request $request ) {
$moduleName = $request -> getModule ();
$moduleModel = Vtiger_Module_Model :: getInstance ( $moduleName );
$currentUserPrivilegesModel = Users_Privileges_Model :: getCurrentUserPrivilegesModel ();
if ( ! $currentUserPrivilegesModel -> hasModulePermission ( $moduleModel -> getId ())) {
throw new AppException ( vtranslate ( 'LBL_PERMISSION_DENIED' ));
}
}
public function process ( Vtiger_Request $request ) {
$fileId = $request -> get ( 'fileid' );
$recordId = $request -> get ( 'record' );
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD: recordId=' . $recordId . ', fileId=' . $fileId . PHP_EOL , FILE_APPEND );
if ( empty ( $fileId )) {
throw new AppException ( 'File ID is required' );
}
// Проверяем что файл связан с этим комментарием
global $adb ;
$query = " SELECT COUNT(*) as count FROM vtiger_seattachmentsrel WHERE crmid = ? AND attachmentsid = ? " ;
$result = $adb -> pquery ( $query , array ( $recordId , $fileId ));
$row = $adb -> fetchByAssoc ( $result );
if ( $row [ 'count' ] == 0 ) {
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_ERROR: File not linked to comment' . PHP_EOL , FILE_APPEND );
throw new AppException ( 'File not found or not accessible' );
}
// Получаем информацию о файле
$query = " SELECT * FROM vtiger_attachments WHERE attachmentsid = ? " ;
$result = $adb -> pquery ( $query , array ( $fileId ));
if ( $adb -> num_rows ( $result ) == 0 ) {
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_ERROR: File not found in attachments' . PHP_EOL , FILE_APPEND );
throw new AppException ( 'File not found' );
}
$fileInfo = $adb -> fetchByAssoc ( $result );
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_INFO: ' . print_r ( $fileInfo , true ) . PHP_EOL , FILE_APPEND );
// Проверяем есть ли S3 URL в vtiger_notes
$query = " SELECT s3_url, filename, filelocationtype FROM vtiger_notes WHERE notesid = ? " ;
$result = $adb -> pquery ( $query , array ( $fileId ));
if ( $adb -> num_rows ( $result ) > 0 ) {
$noteInfo = $adb -> fetchByAssoc ( $result );
// Если filelocationtype = 'E', используем filename как прямую ссылку
if ( $noteInfo [ 'filelocationtype' ] == 'E' && ! empty ( $noteInfo [ 'filename' ])) {
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_EXTERNAL: ' . $noteInfo [ 'filename' ] . PHP_EOL , FILE_APPEND );
// Перенаправляем на внешнюю ссылку
header ( 'Location: ' . $noteInfo [ 'filename' ]);
exit ;
}
// Иначе проверяем s3_url
if ( ! empty ( $noteInfo [ 's3_url' ])) {
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_S3: ' . $noteInfo [ 's3_url' ] . PHP_EOL , FILE_APPEND );
// Перенаправляем на S3 URL
header ( 'Location: ' . $noteInfo [ 's3_url' ]);
exit ;
}
}
// Если S3 URL не найден в notes, пытаемся собрать е г о из attachments
if ( ! empty ( $fileInfo [ 'path' ]) && ! empty ( $fileInfo [ 'storedname' ])) {
// Проверяем что это S3 путь
if ( strpos ( $fileInfo [ 'path' ], 's3://' ) === 0 ) {
// Извлекаем bucket из path
$bucket = str_replace ( 's3://' , '' , $fileInfo [ 'path' ]);
// Собираем S3 URL
$s3Url = 'https://s3.twcstorage.ru/' . $bucket . '/' . $fileInfo [ 'storedname' ];
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_S3_CONSTRUCTED: ' . $s3Url . PHP_EOL , FILE_APPEND );
// Перенаправляем на S3 URL
header ( 'Location: ' . $s3Url );
exit ;
}
}
// Если S3 URL нет, пытаемся найти локальный файл
// Сначала пробуем путь с storedname (новый формат)
$filePath = $fileInfo [ 'path' ] . $fileInfo [ 'storedname' ];
if ( file_exists ( $filePath )) {
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_LOCAL_STORED: ' . $filePath . PHP_EOL , FILE_APPEND );
header ( 'Content-Type: ' . $fileInfo [ 'type' ]);
header ( 'Content-Disposition: attachment; filename="' . $fileInfo [ 'name' ] . '"' );
header ( 'Content-Length: ' . filesize ( $filePath ));
readfile ( $filePath );
exit ;
}
// Если не найден, пробуем старый формат
$filePath = $fileInfo [ 'path' ] . $fileInfo [ 'attachmentsid' ] . '_' . $fileInfo [ 'name' ];
if ( file_exists ( $filePath )) {
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_LOCAL_OLD: ' . $filePath . PHP_EOL , FILE_APPEND );
header ( 'Content-Type: ' . $fileInfo [ 'type' ]);
header ( 'Content-Disposition: attachment; filename="' . $fileInfo [ 'name' ] . '"' );
header ( 'Content-Length: ' . filesize ( $filePath ));
readfile ( $filePath );
exit ;
}
file_put_contents ( 'logs/debug.log' , '[' . date ( 'Y-m-d H:i:s' ) . '] MODCOMMENTS_DOWNLOAD_ERROR: File not found anywhere. Tried: ' . $fileInfo [ 'path' ] . $fileInfo [ 'storedname' ] . ' and ' . $fileInfo [ 'path' ] . $fileInfo [ 'attachmentsid' ] . '_' . $fileInfo [ 'name' ] . PHP_EOL , FILE_APPEND );
throw new AppException ( 'File not found on server' );
}
}
2025-09-26 10:43:05 +03:00
?>