Answer the question
In order to leave comments, you need to log in
How to stop editing a docx file using php?
Hello. There is a task:
The site stores documents in docx format, each of the documents can be edited. Is it possible to modify certain documents with PHP and disable editing?
In the document itself, this can be done by marking the document as final, thereby disallowing editing.
PS
Options for changing the XML structure of the docx file are welcome.
Thanks in advance for your reply.
Answer:
Having shoveled the entire docx file in two versions, normal and blocked, a blocking method was found.
We open the file as an archive and work with XML, everything is described inside the docx directories.
File: [Content_Types].xml
Add an entry
<Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>
<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>
<cp:contentStatus>Окончательное</cp:contentStatus>
</dcterms:modified>
Target="word/document.xml"/></Relationships>
Target="word/document.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/></Relationships>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><property fmtid="{D5CDD505-2E9C-101B-9397-08002B2CF9AE}" pid="2" name="_MarkAsFinal"><vt:bool>true</vt:bool></property></Properties>
Answer the question
In order to leave comments, you need to log in
If anyone is interested, here is the function with all the substitutions.
function NOEDIT_DOCX($docxFile){
if (!file_exists($docxFile)) {
die('Файл не найден.');
}
$zip = new ZipArchive();
if (!$zip->open($docxFile)) {
die('Файл не может быть открыт.');
}
//Заменяем все найденные переменные в файле на значения
$documentXml = $zip->getFromName('[Content_Types].xml');
$documentXml = str_replace('<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>', '<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/><Override PartName="/docProps/custom.xml" ContentType="application/vnd.openxmlformats-officedocument.custom-properties+xml"/>', $documentXml);
$zip->deleteName('[Content_Types].xml');
$zip->addFromString('[Content_Types].xml', $documentXml);
//Заменяем все найденные переменные в файле на значения
$documentXml = $zip->getFromName('docProps/core.xml');
$documentXml = str_replace('</dcterms:modified>', '</dcterms:modified><cp:contentStatus>Окончательное</cp:contentStatus>', $documentXml);
$zip->deleteName('docProps/core.xml');
$zip->addFromString('docProps/core.xml', $documentXml);
//Заменяем все найденные переменные в файле на значения
$documentXml = $zip->getFromName('_rels/.rels');
$documentXml = str_replace('Target="word/document.xml"/></Relationships>', 'Target="word/document.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/></Relationships>', $documentXml);
$zip->deleteName('_rels/.rels');
$zip->addFromString('_rels/.rels', $documentXml);
$zip->addFile('custom.xml', 'docProps/custom.xml');
//Закрываем и сохраняем архив
$zip->close();
}
NOEDIT_DOCX('docx/1.docx');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question