Commit c83dcd67 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Meilleur contrôle d'écriture des fichiers et de chargement des données

parent a70d209d
Loading
Loading
Loading
Loading
Loading
+34 −4
Original line number Diff line number Diff line
@@ -559,6 +559,10 @@ class Document
            throw new Exception('Zip extension not loaded');
        }

        if (!$data){
            throw new Exception('Wrong or empty data');
        }

        $odtFile = $this->tempFileName('odtfile_', 'odt');
        file_put_contents($odtFile, $data);

@@ -592,7 +596,7 @@ class Document
        }

        $this->archive = new ZipArchive();
        if (!true === $this->archive->open($odtFile, ZIPARCHIVE::CREATE)) {
        if (true !== $this->archive->open($odtFile, ZIPARCHIVE::CREATE)) {
            throw new Exception('OpenDocument file "' . $fileName . '" don\'t readable.');
        }

@@ -618,6 +622,11 @@ class Document
     */
    private function prepareSaving($filename = null): string
    {
        $actualFile = $this->getArchive()->filename;
        if (!is_writable($actualFile)) {
            throw new Exception('Impossible to create or update the document. Directory "'.dirname($actualFile).'" not reachable or writable');
        }

        if ($this->isMetaChanged()) {
            $this->getArchive()->addFromString('meta.xml', $this->getMeta()->saveXML());
        }
@@ -630,7 +639,6 @@ class Document
            $this->getArchive()->addFromString('content.xml', $this->getContent()->saveXML());
        }

        $actualFile = $this->getArchive()->filename;
        $this->getArchive()->close();
        $this->archive = null;

@@ -638,9 +646,15 @@ class Document
            if (!$filename) {
                $filename = $this->tempFileName('odt2pdf_', 'pdf');
            }

            $this->odtToPdf($actualFile, $filename);
            $actualFile = $filename;
        }else{
            if ($filename && $filename != $actualFile){
                if (!is_writable(dirname($filename))) {
                    throw new Exception('Impossible to create or update the document. Directory "'.dirname($filename).'" not reachable or writable');
                }
                copy($actualFile, $filename);
            }
        }

        return $actualFile;
@@ -655,8 +669,15 @@ class Document
     * @return Document
     * @throws Exception
     */
    public function odtToPdf($origine, $destination): Document
    public function odtToPdf(string $origine, string $destination): Document
    {
        if (!file_exists($origine)){
            throw new Exception("File \"$origine\" not reachable or writable.");
        }
        if (!is_writable(dirname($destination))) {
            throw new Exception('Impossible to create or update the document. Directory "'.dirname($destination).'" not reachable or writable');
        }

        $command = $this->getConvCommand();

        $command = str_replace(':host', $this->getHost(), $this->getConvCommand());
@@ -688,6 +709,15 @@ class Document



    public function saveToPdf(string $filename)
    {
        $this->odtToPdf($this->getArchive()->filename, $filename);

        return $this;
    }



    /**
     * @return string
     * @throws Exception