PROBLEM :
I need files on my server to be encrypted and it works perfectly fine for .txt, .doc, .xls, .ppt but not with .docx, .xlsx and .pptx.
The problem when I try to edit a docx (or xlsx, pptx) is that the file gets corrupted by the way I encrypt/decrypt since it's not a proper way to edit a docx. So when Microsoft Word tries to open it, it says it's corrupted and it opens it as 'Document1.docx' and not as'MyFileName.docx' and when saving I have to give the name again and with pptx I even have to give the path to the webdav folder the document is in.
QUESTION :
Is there any way to get it to save in the right place without having to type the path ?
CODE :
Here is the code I use to encrypt the files :
$ext = explode( '.', basename($path));
if (in_array("doc", $ext) || in_array("docx", $ext)) {
$handle = fopen("$davPath/$path", "rb");
$data_file = fread($handle, filesize("$davPath/$path"));
fclose($handle);
} else {
$data_file = file_get_contents("$davPath/$path");
}
$encrypt_data_file = $encryption->encrypt($data_file);
if (file_put_contents("$davPath/encrypt_" . basename($path),$encrypt_data_file)) {
unlink("$davPath/" . basename($path));
rename("$davPath/encrypt_" . basename($path),"$davPath/" . basename($path));
return true;
} else {
return false;
}
And here is the code I use to decrypt them :
$ext = explode( '.', basename($uri));
if(is_file($davPath."/".$uri)) {
if (in_array("doc", $ext) || in_array("docx", $ext)) {
$handle = fopen("$davPath/$uri", "rb");
$data_file = fread($handle, filesize("$davPath/$uri"));
fclose($handle);
} else {
$data_file = file_get_contents("$davPath/$uri");
}
}
if ($data_file != false) {
$decrypt_data_file = $encryption->decrypt($data_file);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($uri));
header('Content-Location: '.$_SERVER['SCRIPT_URI']);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
flush();
echo $decrypt_data_file;
return false;
}
PS : I did find a workaround which consists in having the file decrypted on the server during the modification but I would really like not to have to do that.