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 FROM vtiger_notes WHERE notesid = ?"; $result = $adb->pquery($query, array($fileId)); if ($adb->num_rows($result) > 0) { $noteInfo = $adb->fetchByAssoc($result); 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 нет, пытаемся найти локальный файл $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: ' . $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' . PHP_EOL, FILE_APPEND); throw new AppException('File not found on server'); } } ?>