A
A
Andrew2018-06-30 11:57:03
PHP
Andrew, 2018-06-30 11:57:03

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"/>

After recording
<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>

File: docProps/core.xml
Add entry
<cp:contentStatus>Окончательное</cp:contentStatus>
After
</dcterms:modified>
File: _rels/.rels
Replace entry
Target="word/document.xml"/></Relationships>
With entry
Target="word/document.xml"/><Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties" Target="docProps/custom.xml"/></Relationships>

File: docProps/custom.xml
Create a file in the docProps directory with the following content.
<?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

1 answer(s)
A
Andrew, 2018-06-30
@Morterset

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 question

Ask a Question

731 491 924 answers to any question