<?php
namespace Engine\GenDocument\Storage; class General { protected $document = null; public function generateDocumentSaveData($flags=[]) { $encodeKey=''; $addUser = $this->getUser('addUser'); $modUser = $this->getUser('modUser'); $exportUser = $this->getUser('currentUser'); $result=[]; $page = new \DTMDocument(); if (!$this->document) { throw new \Exception("Document template is not defined. You need to call the method loadTemplate before."); } $page->importFromStorage($this->document->templateContent); $result['page'] = serialize($page->exportToStorage(['mysql'=>true])); $result['fieldsDetails'] = serialize($this->document->fieldsDetails); $result['customFieldsDetails']= serialize($this->document->customFieldsDetails); $result['documentLength'] = strlen($result['page']).'.'.md5($result['page'].$encodeKey); $result['documentVersion'] = $page->version; $result['author'] = $addUser.','.$modUser; $result['date'] = time(); $result['documentType'] = $this->document->docType; $result['pivot'] = \App\Config\IssueDocuments::getInstance()->getDocPivot($this->document->docType); $result['name'] = $this->document->name; $result['exporter'] = $exportUser; $result['documentFlags']['compressed']= (bool)$flags['compress']; $result['documentFlags']['encoded'] = (bool)$flags['encode']; $header ="DCS.DTM\0"; $footer=""; $body=[]; $body['documentVersion'] = $result['documentVersion']; $body['documentLength'] = $result['documentLength']; $body['date'] = $result['date']; $body['pivot'] = $result['pivot']; $body['name'] = $result['name']; $body['documentType'] = $result['documentType']; $body['flags'] = sprintf("%d%d%d%d", (int)$result['documentFlags']['compressed'], (int)$result['documentFlags']['encoded'], 0, 0); $body['author'] = $result['author']; $body['exporter'] = $result['exporter']; $body['fieldsDetails_length'] = strlen($result['fieldsDetails']); $body['customFieldsDetails_length']=strlen($result['customFieldsDetails']); $body['page_length'] = strlen($result['page']); $body['fieldsDetails'] = $result['fieldsDetails']; $body['customFieldsDetails'] = $result['customFieldsDetails']; $body['page'] = $result['page']; $bodyStr = serialize($body); if($flags['compress']){ $bodyStr = gzdeflate($bodyStr, 9); } $header.= $body['flags'] . "\0"; $header.= strlen($bodyStr) . "\0"; return $header.$bodyStr.$footer; } public function importDocumentSaveData($templateContent) { $encodeKey = ''; $template = $templateContent; $header = substr($templateContent,0,8); if($header!="DCS.DTM\0") return "invalid file"; $template = substr($templateContent,8); $templateFlags = substr($template,0 , 4); $templateFlags = str_split($templateFlags,1); $template = substr($template,5); $bodyLength = (int)substr($template,0,8); $template = substr($template,strpos($template,"\0")+1); $body = substr($template,0,$bodyLength); if($templateFlags[0]){ $body = gzinflate($body); } $body = unserialize($body); $page = new \DTMDocument(); $documentVersion = explodeClean('.',$body['documentVersion']); $thisDocumentVersion = explodeClean('.',$page->version); if($documentVersion[0]!=$thisDocumentVersion[0]) return "incompatible document versions"; $body['documentLength'] = explode(".",$body['documentLength']); if($body['documentLength'][0]!=strlen($body['page'])) return "corrupted file(size)"; if($body['documentLength'][1]!=md5($body['page'].$encodeKey)) return "corrupted file"; $result=[]; $result['exportTime'] = $body['date']; $result['documentName'] = $body['name']; $result['documentType'] = $body['documentType']; $result['documentPivot'] = $body['pivot']; $result['documentAuthor'] = explode(",",$body['author']); $result['documentExporter']=$body['exporter']; $result['fieldsDetails'] = unserialize($body['fieldsDetails']); $result['customFieldsDetails']= unserialize($body['customFieldsDetails']); $result['page'] = unserialize($body['page']); return $result; } public function loadTemplate($idTemplate) { throw new \Exception("This function should be implemented"); } public function updateTemplate($idTemplate, $templateDataArr) { throw new \Exception("This function should be implemented"); } public function getUser($which = 'addUser') { throw new \Exception("This function should be implemented"); } } 