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'); } } ?>