Hi, I am opening a file in collabora but can open and write in collbora. collabora is changing my content in json. this is my code
function parseWopiRequest($uri) {
$path = parse_url($uri, PHP_URL_PATH);
preg_match('~^/wopi.php/files/([[:alnum:]_-]+)~', $path, $matches);
$numMatches = count($matches);
$fileId = $matches[1]; // Extract the file ID from the URL
$file_root = $_GET['root_folder']; //'templates/2024-03';
$file_ext = $_GET['file_ext']; //'docx';
$can_edit = $_GET['can_edit']; //1;
$DOCUMENT_ROOT = '/var/www/html/demo/media/'.$file_root.'/';
$token = generateAccessToken($fileId);
error_log('INFO: access token: ' . $token);
if($numMatches >= 1){
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET': wopiGetFile($DOCUMENT_ROOT,$matches[1],$file_ext,$can_edit); break;
case 'POST': wopiPutFile($DOCUMENT_ROOT,$matches[1],$file_ext,$can_edit); break;
}
}
elseif($numMatches == 2){
wopiCheckFileInfo($DOCUMENT_ROOT,$matches[1],$file_ext,$can_edit);
}
}
/**
* Parses the URL in order to extract from the query section the access token value.
*/
function getAccessToken($uri) {
$query = parse_url($uri, PHP_URL_QUERY);
if ($query) {
$params = explode('&', $query);
foreach ($params as $param) {
$pair = explode('=', $param);
if ($pair && count($pair) == 2) {
if ($pair[0] == 'access_token') {
return $pair[1];
}
}
}
}
return '';
}
function wopiCheckFileInfo($DOCUMENT_ROOT,$documentId,$file_ext,$edit) {
$filePath = $DOCUMENT_ROOT . basename($documentId.'.'.$file_ext); // Basic sanitation
// echo $filePath;
// die();
if (file_exists($filePath)) {
$response = [
'BaseFileName' => basename($filePath),
'Size' => filesize($filePath),
'OwnerId' => 'haider@legai.tech',
'Version' => '1',
'UserCanWrite' => $edit
];
header('Content-Type: application/json');
echo json_encode($response);
}
}
function wopiGetFile($DOCUMENT_ROOT,$documentId,$file_ext,$edit) {
$filePath = $DOCUMENT_ROOT . basename($documentId.'.'.$file_ext); // Basic sanitation
if (file_exists($filePath)) {
$response = [
'BaseFileName' => basename($filePath),
'Size' => filesize($filePath),
'OwnerId' => 'haider@legai.tech',
'Version' => '1',
'UserCanWrite' => $edit
];
header('Content-Type: application/json');
echo json_encode($response);
}
}
function wopiPutFile($DOCUMENT_ROOT,$documentId,$file_ext,$edit) {
error_log('INFO: wopiPutFile invoked: document id: ' . $documentId);
$filePath = $DOCUMENT_ROOT . basename($documentId.'.'.$file_ext); // Basic sanitation
$fh = fopen('php://input', 'r');
$fileContent = '';
while ($line = fgets($fh)) {
$fileContent = $fileContent . $line;
}
fclose($fh);
updateFileContent($filePath, $fileContent);
// you can check the new file content on the apache error log file
error_log('INFO: ' . $fileContent);
}
function updateFileContent($filePath, $newContent) {
// Use file_put_contents to replace the content of the file
// The LOCK_EX flag is used to acquire an exclusive lock on the file during writing
if (file_put_contents($filePath, $newContent, LOCK_EX) !== false) {
echo "File content updated successfully.";
} else {
echo "Error updating file content.";
}
}
$requestUri = $_SERVER['REQUEST_URI'];
parseWopiRequest($requestUri);