113 lines
3.1 KiB
PHP
113 lines
3.1 KiB
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* Открытие файла через Nextcloud (РАБОЧАЯ ВЕРСИЯ v2)
|
||
|
|
* Использует Redis индекс для быстрого получения FileID
|
||
|
|
*/
|
||
|
|
|
||
|
|
// Отключаем вывод ошибок в браузер
|
||
|
|
error_reporting(E_ALL);
|
||
|
|
ini_set('display_errors', 0);
|
||
|
|
|
||
|
|
// Отключаем buffering
|
||
|
|
if (ob_get_level()) ob_end_clean();
|
||
|
|
|
||
|
|
header('Content-Type: application/json; charset=utf-8');
|
||
|
|
header('Cache-Control: no-cache, must-revalidate');
|
||
|
|
|
||
|
|
$recordId = isset($_GET['recordId']) ? (int)$_GET['recordId'] : 0;
|
||
|
|
|
||
|
|
if ($recordId <= 0) {
|
||
|
|
echo json_encode(['success' => false, 'error' => 'Invalid recordId']);
|
||
|
|
exit;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 1. Получаем filename из БД
|
||
|
|
$db = new mysqli('localhost', 'ci20465_72new', 'EcY979Rn', 'ci20465_72new');
|
||
|
|
|
||
|
|
if ($db->connect_error) {
|
||
|
|
throw new Exception('DB connection failed');
|
||
|
|
}
|
||
|
|
|
||
|
|
$db->set_charset('utf8mb4');
|
||
|
|
|
||
|
|
$stmt = $db->prepare("SELECT filename FROM vtiger_notes WHERE notesid = ?");
|
||
|
|
$stmt->bind_param('i', $recordId);
|
||
|
|
$stmt->execute();
|
||
|
|
$result = $stmt->get_result();
|
||
|
|
$row = $result->fetch_assoc();
|
||
|
|
$db->close();
|
||
|
|
|
||
|
|
if (!$row || empty($row['filename'])) {
|
||
|
|
throw new Exception('File not found in DB');
|
||
|
|
}
|
||
|
|
|
||
|
|
$fileName = $row['filename'];
|
||
|
|
|
||
|
|
// 2. Извлекаем S3 путь из URL
|
||
|
|
$bucketId = 'f9825c87-4e3558f6-f9b6-405c-ad3d-d1535c49b61c';
|
||
|
|
$fileName = rawurldecode($fileName);
|
||
|
|
$pos = strpos($fileName, $bucketId . '/');
|
||
|
|
|
||
|
|
if ($pos === false) {
|
||
|
|
throw new Exception('Invalid S3 path in filename');
|
||
|
|
}
|
||
|
|
|
||
|
|
$s3Path = substr($fileName, $pos + strlen($bucketId) + 1);
|
||
|
|
|
||
|
|
// 3. Получаем FileID из Redis
|
||
|
|
$redis = new Redis();
|
||
|
|
if (!$redis->connect('crm.clientright.ru', 6379)) {
|
||
|
|
throw new Exception('Redis connection failed');
|
||
|
|
}
|
||
|
|
|
||
|
|
$redis->auth('CRM_Redis_Pass_2025_Secure!');
|
||
|
|
|
||
|
|
$redisKey = "crm:nc:fileid:" . $s3Path;
|
||
|
|
$cached = $redis->get($redisKey);
|
||
|
|
|
||
|
|
if (!$cached) {
|
||
|
|
$redis->close();
|
||
|
|
throw new Exception('FileID not found in Redis index. Key: ' . substr($redisKey, 0, 100));
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = json_decode($cached, true);
|
||
|
|
$fileId = $data['fileId'] ?? null;
|
||
|
|
|
||
|
|
$redis->close();
|
||
|
|
|
||
|
|
if (!$fileId) {
|
||
|
|
throw new Exception('Invalid FileID data in Redis');
|
||
|
|
}
|
||
|
|
|
||
|
|
// 4. Формируем URL для Nextcloud
|
||
|
|
$nextcloudUrl = 'https://office.clientright.ru:8443';
|
||
|
|
$ncPath = '/crm/' . $s3Path;
|
||
|
|
$dirPath = dirname($ncPath);
|
||
|
|
|
||
|
|
$redirectUrl = $nextcloudUrl . '/apps/files/files/' . $fileId . '?dir=' . urlencode($dirPath) . '&openfile=true';
|
||
|
|
|
||
|
|
// 5. Возвращаем успешный ответ
|
||
|
|
echo json_encode([
|
||
|
|
'success' => true,
|
||
|
|
'fileId' => $fileId,
|
||
|
|
'redirectUrl' => $redirectUrl,
|
||
|
|
'source' => 'redis',
|
||
|
|
'recordId' => $recordId
|
||
|
|
]);
|
||
|
|
|
||
|
|
} catch (Exception $e) {
|
||
|
|
http_response_code(500);
|
||
|
|
echo json_encode([
|
||
|
|
'success' => false,
|
||
|
|
'error' => $e->getMessage(),
|
||
|
|
'recordId' => $recordId
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
exit;
|
||
|
|
?>
|
||
|
|
|
||
|
|
|
||
|
|
|