Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
ExportXmlDocument.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp;
8 
10 {
11  /**
12  * @var \Doc
13  */
14  protected $document = null;
15  protected $exportProfil = false;
16 
17  protected $exportFiles = false;
18  protected $exportDocumentNumericIdentiers = false;
19  protected $attributeToExport = array();
20  protected $includeSchemaReference = false;
22 
23  protected $verifyAttributeAccess = true;
24  protected $writeToFile = false;
25  /**
26  * If true, attribute with "I" visibility are not returned
27  * @param boolean $verifyAttributeAccess
28  */
29  public function setVerifyAttributeAccess($verifyAttributeAccess)
30  {
31  $this->verifyAttributeAccess = $verifyAttributeAccess;
32  }
33  /**
34  * @param mixed $structureAttributes
35  */
36  public function setStructureAttributes($structureAttributes)
37  {
38  $this->structureAttributes = $structureAttributes;
39  }
40  /**
41  * @param array $attributeToExport
42  */
43  public function setAttributeToExport($attributeToExport)
44  {
45  $this->attributeToExport = $attributeToExport;
46  }
47  /**
48  * @param boolean $exportDocumentNumericIdentiers
49  */
50  public function setExportDocumentNumericIdentiers($exportDocumentNumericIdentiers)
51  {
52  $this->exportDocumentNumericIdentiers = $exportDocumentNumericIdentiers;
53  }
54  /**
55  * @param boolean $exportFiles
56  */
57  public function setExportFiles($exportFiles)
58  {
59  $this->exportFiles = $exportFiles;
60  }
61  /**
62  * @param boolean $includeSchemaReference
63  */
64  public function setIncludeSchemaReference($includeSchemaReference)
65  {
66  $this->includeSchemaReference = $includeSchemaReference;
67  }
68  /**
69  * @param \Doc $document
70  */
71  public function setDocument($document)
72  {
73  $this->document = $document;
74  }
75 
76  public function getXml()
77  {
78  if ($this->exportFiles) {
79  throw new Exception("EXPC0103");
80  }
81  return $this->export();
82  }
83 
84  public function writeTo($filePath)
85  {
86  $this->export($filePath);
87  }
88  protected function export($outfile = "")
89  {
90  $lay = new \Layout(getLayoutFile("FDL", "exportxml.xml"));
91  //$lay=&$this->document->lay;
92  $lay->set("famname", strtolower($this->document->fromname));
93  $lay->set("id", ($this->exportDocumentNumericIdentiers ? $this->document->id : ''));
94  if ($this->document->locked != - 1) {
95  $lay->set("name", $this->document->name);
96  } else {
97  $lay->set("name", "");
98  }
99  $lay->set("revision", $this->document->revision);
100  $lay->set("version", $this->document->getVersion());
101  $lay->set("state", $this->document->getState());
102  $lay->set("title", htmlspecialchars($this->document->getTitle() , ENT_QUOTES));
103  $lay->set("mdate", strftime("%FT%H:%M:%S", $this->document->revdate));
104  $lay->set("flat", (!$this->includeSchemaReference || !$this->structureAttributes));
105  $la = $this->document->GetFieldAttributes();
106  $level1 = array();
107 
108  foreach ($la as $k => $v) {
109  if ((!$v) || ($v->getOption("autotitle") == "yes") || ($v->usefor == 'Q')) unset($la[$k]);
110  }
111  $option = new \exportOptionAttribute();
112  $option->outFile = $outfile;
113 
114  foreach ($la as $k => & $v) {
115  if (($v->id != \Adoc::HIDDENFIELD) && ($v->type == 'frame' || $v->type == "tab") && ((!$v->fieldSet) || $v->fieldSet->id == \Adoc::HIDDENFIELD)) {
116 
117  $level1[] = array(
118  "level" => $this->getStructXmlValue($v)
119  );
120  }
121  }
122  $lay->setBlockData("top", $level1);
123  if ($outfile) {
124 
125  $this->writeToFile = true;
126  if ($this->exportFiles) {
127  $xmlcontent = $lay->gen();
128  $fo = fopen($outfile, "w");
129  if (!$fo) {
130 
131  throw new Exception("EXPC0101", $outfile);
132  }
133  $pos = strpos($xmlcontent, "[FILE64");
134 
135  $bpos = 0;
136  while ($pos !== false) {
137  if (fwrite($fo, substr($xmlcontent, $bpos, $pos - $bpos))) {
138  $bpos = strpos($xmlcontent, "]", $pos) + 1;
139 
140  $filepath = substr($xmlcontent, $pos + 8, ($bpos - $pos - 9));
141  /* If you want to encode a large file, you should encode it in chunks that
142  are a multiple of 57 bytes. This ensures that the base64 lines line up
143  and that you do not end up with padding in the middle. 57 bytes of data
144  fills one complete base64 line (76 == 57*4/3):*/
145  $ff = fopen($filepath, "r");
146  $size = 6 * 1024 * 57;
147  while ($buf = fread($ff, $size)) {
148  fwrite($fo, base64_encode($buf));
149  }
150  $pos = strpos($xmlcontent, "[FILE64", $bpos);
151  } else {
152  throw new Exception("EXPC0102", $outfile);
153  }
154  }
155  fwrite($fo, substr($xmlcontent, $bpos));
156  fclose($fo);
157  } else {
158  if (file_put_contents($outfile, $lay->gen()) === false) {
159 
160  throw new Exception("EXPC0100", $outfile);
161  }
162  }
163  } else {
164  $this->writeToFile = false;
165  return $lay->gen();
166  }
167  return '';
168  }
169  /**
170  * export values as xml fragment
171  *
172  * @param \NormalAttribute $attribute
173  * @param int $indexValue (in case of multiple value)
174  *
175  * @return string
176  */
177  protected function getAttributeXmlValue(\NormalAttribute $attribute, $indexValue)
178  {
179  $doc = $this->document;
180  if ($this->verifyAttributeAccess && !VerifyAttributeAccess::isAttributeAccessGranted($this->document, $attribute)) {
181  return sprintf("<%s granted=\"false\"/>", $attribute->id);
182  }
183 
184  if ($indexValue > - 1) $v = $doc->getMultipleRawValues($attribute->id, null, $indexValue);
185  else $v = $doc->getRawValue($attribute->id, null);
186  //if (! $v) return sprintf("<!-- no value %s -->",$attribute->id);
187  if ($attribute->getOption("autotitle") == "yes") {
188  return sprintf("<!--autotitle %s %s -->", $attribute->id, $v);
189  }
190  if (($v === null) && ($attribute->type != 'array')) {
191  if (($attribute->type == 'file') || ($attribute->type == 'image')) return sprintf('<%s mime="" title="" xsi:nil="true"/>', $attribute->id);
192  else return sprintf('<%s xsi:nil="true"/>', $attribute->id);
193  }
194  switch ($attribute->type) {
195  case 'timestamp':
196  case 'date':
197  $v = stringDateToIso($v);
198  return sprintf("<%s>%s</%s>", $attribute->id, $v, $attribute->id);
199  case 'array':
200  $la = $doc->getAttributes();
201  $xmlvalues = array();
202  $av = $doc->getArrayRawValues($attribute->id);
203  $axml = array();
204  foreach ($av as $k => $col) {
205  $xmlvalues = array();
206  foreach ($col as $aid => $aval) {
207  $oa = $doc->getAttribute($aid);
208  if (empty($this->attributeToExport[$doc->fromid]) || in_array($aid, $this->attributeToExport[$doc->fromid])) {
209  $indexValue = $k;
210  $xmlvalues[] = $this->getAttributeXmlValue($oa, $indexValue);
211  }
212  }
213  $axml[] = sprintf("<%s>%s</%s>", $attribute->id, implode("\n", $xmlvalues) , $attribute->id);
214  }
215  $indexValue = - 1; // restore initial index
216  return implode("\n", $axml);
217  case 'image':
218  case 'file':
219 
220  if (preg_match(PREGEXPFILE, $v, $reg)) {
221  if ($this->exportDocumentNumericIdentiers) {
222  $vid = $reg[2];
223  } else {
224  $vid = '';
225  }
226  $mime = $reg[1];
227  $name = htmlspecialchars($reg[3], ENT_QUOTES);
228  $base = getParam("CORE_EXTERNURL");
229  $href = $base . str_replace('&', '&amp;', $doc->getFileLink($attribute->id));
230  if ($this->exportFiles) {
231  $path = $doc->vault_filename_fromvalue($v, true);
232 
233  if (is_file($path)) {
234  if ($this->writeToFile) {
235  return sprintf('<%s vid="%s" mime="%s" title="%s">[FILE64:%s]</%s>', $attribute->id, $vid, $mime, $name, $path, $attribute->id);
236  } else {
237  return sprintf('<%s vid="%s" mime="%s" title="%s">%s</%s>', $attribute->id, $vid, $mime, $name, base64_encode(file_get_contents($path)) , $attribute->id);
238  }
239  } else {
240  return sprintf('<!-- file not found --><%s vid="%s" mime="%s" title="%s"/>', $attribute->id, $vid, $mime, $name, $attribute->id);
241  }
242  } else {
243  return sprintf('<%s vid="%s" mime="%s" href="%s" title="%s"/>', $attribute->id, $vid, $mime, $href, $name);
244  }
245  } else {
246  return sprintf("<%s>%s</%s>", $attribute->id, $v, $attribute->id);
247  }
248  case 'thesaurus':
249  case 'account':
250  case 'docid':
251  if (!$v) {
252  return sprintf('<%s xsi:nil="true"/>', $attribute->id);
253  } else {
254  $info = getTDoc($doc->dbaccess, $v, array() , array(
255  "title",
256  "name",
257  "id",
258  "revision",
259  "initid",
260  "locked"
261  ));
262 
263  if ($info) {
264  $docid = $info["id"];
265  $docRevOption = $attribute->getOption("docrev", "latest");
266  $latestTitle = ($docRevOption === "latest");
267 
268  $revAttr = "";
269  if ($latestTitle) {
270  $docid = $info["initid"];
271  if ($info["locked"] == - 1) {
272  $info["title"] = $doc->getLastTitle($docid);
273  }
274  } elseif ($docRevOption === "fixed") {
275  $revAttr = sprintf(' revision="%d" ', $info["revision"]);
276  } elseif (preg_match('/^state\(([^\)]+)\)/', $docRevOption, $matches)) {
277  $revAttr = sprintf(' revision="state:%s" ', htmlspecialchars($matches[1], ENT_QUOTES));
278  }
279 
280  if ($info["name"]) {
281  $info["name"] = htmlspecialchars($info["name"], ENT_QUOTES);
282 
283  if ($this->exportDocumentNumericIdentiers) {
284  return sprintf('<%s id="%s" name="%s"%s>%s</%s>', $attribute->id, $docid, $info["name"], $revAttr, $attribute->encodeXml($info["title"]) , $attribute->id);
285  } else {
286  if ($revAttr) {
287  addWarningMsg(sprintf(_("Doc %s : Attribut \"%s\" reference revised identifier : importation not support revision links without identifiers") , $doc->getTitle() , $attribute->getLabel()));
288  }
289  return sprintf('<%s name="%s"%s>%s</%s>', $attribute->id, $info["name"], $revAttr, $attribute->encodeXml($info["title"]) , $attribute->id);
290  }
291  } else {
292  if ($this->exportDocumentNumericIdentiers) {
293  return sprintf('<%s id="%s"%s>%s</%s>', $attribute->id, $docid, $revAttr, $attribute->encodeXml($info["title"]) , $attribute->id);
294  } else {
295 
296  return sprintf('<%s>%s</%s>', $attribute->id, $attribute->encodeXml($info["title"]) , $attribute->id);
297  }
298  }
299  } else {
300  if ((strpos($v, '<BR>') === false) && (strpos($v, "\n") === false)) {
301  return sprintf('<%s id="%s">%s</%s>', $attribute->id, $v, _("unreferenced document") , $attribute->id);
302  } else {
303 
304  $tids = explode("\n", str_replace('<BR>', "\n", $v));
305  $mName = array();
306  $mId = array();
307  $foundName = false;
308  foreach ($tids as $id) {
309  $lName = getNameFromId($doc->dbaccess, $id);
310  $mName[] = $lName;
311  $mId[] = $id;
312  if ($lName) $foundName = true;
313  }
314  $sIds = '';
315  if ($this->exportDocumentNumericIdentiers) {
316  $sIds = sprintf('id="%s"', implode(',', $mId));
317  }
318  $sName = '';
319  if ($foundName) {
320 
321  $sName = sprintf('name="%s"', implode(',', $mName));
322  }
323  return sprintf('<%s %s %s>%s</%s>', $attribute->id, $sName, $sIds, _("multiple document") , $attribute->id);
324  }
325  }
326  }
327  default:
328  return sprintf("<%s>%s</%s>", $attribute->id, $attribute->encodeXml($v) , $attribute->id);
329  }
330  }
331 
332  protected function getXmlValue(\BasicAttribute $attribute, $indexValue)
333  {
334  if ($attribute->isNormal === true) {
335  /**
336  * @var \NormalAttribute $attribute
337  */
338  return $this->getAttributeXmlValue($attribute, $indexValue);
339  } else {
340  /**
341  * @var \FieldSetAttribute $attribute
342  */
343  return $this->getStructXmlValue($attribute, $indexValue);
344  }
345  }
346  /**
347  * export values as xml fragment
348  *
349  * @param \FieldSetAttribute $structAttribute
350  * @param int $indexValue
351  *
352  * @return string
353  */
354  protected function getStructXmlValue(\FieldSetAttribute $structAttribute, $indexValue = - 1)
355  {
356  $doc = $this->document;
357  $la = $doc->getAttributes();
358  $xmlvalues = array();
359  foreach ($la as $k => $v) {
360  /**
361  * @var \NormalAttribute $v
362  */
363  if ($v->fieldSet && $v->fieldSet->id == $structAttribute->id && (empty($this->attributeToExport[$doc->fromid]) || in_array($v->id, $this->attributeToExport[$doc->fromid]))) {
364  $value = $this->getXmlValue($v, $indexValue);
365  if ($v->type == "htmltext" && $this->exportFiles) {
366  $value = $v->prepareHtmltextForExport($value);
367  if ($this->exportFiles) {
368  $value = preg_replace_callback('/(&lt;img.*?)src="(((?=.*docid=(.*?)&)(?=.*attrid=(.*?)&)(?=.*index=(-?[0-9]+)))|(file\/(.*?)\/[0-9]+\/(.*?)\/(-?[0-9]+))).*?"/', function ($matches)
369  {
370  if (isset($matches[7])) {
371  $docid = $matches[8];
372  $attrid = $matches[9];
373  $index = $matches[10] == "-1" ? 0 : $matches[10];
374  } else {
375  $docid = $matches[4];
376  $index = $matches[6] == "-1" ? 0 : $matches[6];
377  $attrid = $matches[5];
378  }
379  $docimg = new_Doc(getDbAccess() , $docid);
380  $attr = $docimg->getAttribute($attrid);
381  $tfiles = $docimg->vault_properties($attr);
382  $f = $tfiles[$index];
383  $f["name"] = htmlspecialchars($f["name"], ENT_QUOTES);
384  if (is_file($f["path"])) {
385  if ($this->writeToFile) {
386  return sprintf('%s title="%s" src="data:%s;base64,[FILE64:%s]"', "\n" . $matches[1], unaccent($f["name"]) , $f["mime_s"], $f["path"]);
387  } else {
388  return sprintf('%s title="%s" src="data:%s;base64,%s"', "\n" . $matches[1], unaccent($f["name"]) , $f["mime_s"], base64_encode(file_get_contents($f["path"])));
389  }
390  } else {
391  return sprintf('%s title="%s" src="data:%s;base64,file not found"', "\n" . $matches[1], unaccent($f["name"]) , $f["mime_s"]);
392  }
393  }
394  , $value);
395  }
396  }
397  $xmlvalues[] = $value;
398  }
399  }
400  if (!$this->structureAttributes) return implode("\n", $xmlvalues);
401  else return sprintf("<%s>%s</%s>", $structAttribute->id, implode("\n", $xmlvalues) , $structAttribute->id);
402  }
403 }
static encodeXml($s, $quot=false)
setIncludeSchemaReference($includeSchemaReference)
setExportDocumentNumericIdentiers($exportDocumentNumericIdentiers)
getTDoc($dbaccess, $id, $sqlfilters=array(), $result=array())
stringDateToIso($date, $format=false, $withT=false)
Definition: Lib.Util.php:246
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
$attribute
$size
Definition: resizeimg.php:110
getAttributeXmlValue(\NormalAttribute $attribute, $indexValue)
addWarningMsg($msg)
Definition: Lib.Common.php:95
const PREGEXPFILE
Definition: Class.Doc.php:54
setExportFiles($exportFiles)
setVerifyAttributeAccess($verifyAttributeAccess)
$docid
Definition: cleanFamily.php:13
getLayoutFile($app, $layfile)
Definition: Lib.Common.php:258
$outfile
$path
Definition: dav.php:39
getParam($name, $def="")
must be in core or global type
Definition: Lib.Common.php:193
setAttributeToExport($attributeToExport)
getDbAccess()
Definition: Lib.Common.php:368
new_Doc($dbaccess, $id= '', $latest=false)
getNameFromId($dbaccess, $id)
$info
Definition: geticon.php:30
unaccent($s)
Definition: Lib.Util.php:569
setStructureAttributes($structureAttributes)
$value
getOption($x, $def="")
← centre documentaire © anakeen