Files
crm.clientright.ru/modules/ModComments/actions/ViewFile.php

106 lines
4.6 KiB
PHP
Raw Normal View History

<?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_ViewFile_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_VIEW: 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_VIEW_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_VIEW_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_VIEW_INFO: ' . print_r($fileInfo, true) . PHP_EOL, FILE_APPEND);
// Собираем S3 URL из 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_VIEW_S3: ' . $s3Url . PHP_EOL, FILE_APPEND);
// Получаем содержимое файла с S3 и отдаем браузеру
$fileContent = file_get_contents($s3Url);
if ($fileContent !== false) {
// Устанавливаем правильные заголовки для просмотра в браузере
header('Content-Type: ' . $fileInfo['type']);
header('Content-Length: ' . strlen($fileContent));
header('Content-Disposition: inline; filename="' . $fileInfo['name'] . '"');
// Отдаем содержимое файла
echo $fileContent;
exit;
} else {
file_put_contents('logs/debug.log', '[' . date('Y-m-d H:i:s') . '] MODCOMMENTS_VIEW_ERROR: Failed to get file content from S3' . PHP_EOL, FILE_APPEND);
throw new AppException('Failed to load file content');
}
}
}
// Если S3 URL нет, пытаемся найти локальный файл
$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_VIEW_LOCAL: ' . $filePath . PHP_EOL, FILE_APPEND);
header('Content-Type: ' . $fileInfo['type']);
header('Content-Length: ' . filesize($filePath));
header('Content-Disposition: inline; filename="' . $fileInfo['name'] . '"');
readfile($filePath);
exit;
}
file_put_contents('logs/debug.log', '[' . date('Y-m-d H:i:s') . '] MODCOMMENTS_VIEW_ERROR: File not found anywhere' . PHP_EOL, FILE_APPEND);
throw new AppException('File not found on server');
}
}
?>