Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
exportxmlfld.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Export Document from Folder
9  *
10  * @author Anakeen 2003
11  * @version $Id: exportfld.php,v 1.44 2009/01/12 13:23:11 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  * @subpackage
15  */
16 /**
17  */
18 
19 include_once ("FDL/Lib.Dir.php");
20 include_once ("FDL/Lib.Util.php");
21 include_once ("FDL/Class.DocAttr.php");
22 include_once ("VAULT/Class.VaultFile.php");
23 include_once ("FDL/import_file.php");
24 include_once ("FDL/Class.SearchDoc.php");
25 /**
26  * Exportation as xml of documents from folder or searches
27  * @param Action &$action current action
28  * @global fldid Http var : folder identificator to export
29  * @global wfile Http var : (Y|N) if Y export attached file export format will be tgz
30  * @global flat Http var : (Y|N) if Y specid column is set with identificator of document
31  * @global eformat Http var : (X|Y) I: Y: only one xml, X: zip by document with files
32  * @global log Http var : log file output
33  * @global selection Http var : JSON document selection object
34  */
35 function exportxmlfld(Action & $action, $aflid = "0", $famid = "")
36 {
37  if (ini_get("max_execution_time") < 3600) ini_set("max_execution_time", 3600); // 60 minutes
38  $dbaccess = $action->GetParam("FREEDOM_DB");
39  $fldid = $action->getArgument("id", $aflid);
40  $wprof = false; // no profil
41  $wfile = (substr(strtolower($action->getArgument("wfile", "N")) , 0, 1) == "y"); // with files
42  $wident = (substr(strtolower($action->getArgument("wident", "Y")) , 0, 1) == "y"); // with numeric identificator
43  $flat = (substr(strtolower($action->getArgument("flat")) , 0, 1) == "y"); // flat xml
44  $eformat = strtoupper($action->getArgument("eformat", "X")); // export format
45  $selection = $action->getArgument("selection"); // export selection object (JSON)
46  $log = $action->getArgument("log"); // log file
47  $configxml = $action->getArgument("config");
48  $flog = false;
49  if ($log) {
50  $flog = fopen($log, "w");
51  if (!$flog) {
52  exportExit($action, sprintf(_("cannot write log in %s") , $log));
53  }
54  fputs($flog, sprintf("EXPORT BEGIN OK : %s\n", Doc::getTimeDate(0, true)));
55  fputs($flog, sprintf("EXPORT OPTION FLAT : %s\n", ($flat) ? "yes" : "no"));
56  fputs($flog, sprintf("EXPORT OPTION WFILE : %s\n", ($wfile) ? "yes" : "no"));
57  fputs($flog, sprintf("EXPORT OPTION CONFIG : %s\n", ($configxml) ? "yes" : "no"));
58  }
59  // constitution options for filter attributes
60  $exportAttribute = array();
61  if ($configxml) {
62  if (!file_exists($configxml)) exportExit($action, sprintf(_("config file %s not found") , $configxml));
63 
64  $xml = @simplexml_load_file($configxml);
65 
66  if ($xml === false) {
67  exportExit($action, sprintf(_("parse error config file %s : %s") , $configxml, print_r(libxml_get_last_error() , true)));
68  }
69  foreach ($xml->family as $family) {
70  $afamid = @current($family->attributes()->name);
71  if (!$afamid) exportExit($action, sprintf(_("Config file %s : family name not set") , $configxml));
72  $fam = new_doc($dbaccess, $afamid);
73  if ((!$fam->isAlive()) || ($fam->doctype != 'C')) exportExit($action, sprintf(_("Config file %s : family name [%s] not match a know family") , $configxml, $afamid));
74  $exportAttribute[$fam->id] = array();
75  foreach ($family->attribute as $attribute) {
76  $aid = @current($attribute->attributes()->name);
77 
78  if (!$aid) exportExit($action, sprintf(_("Config file %s : attribute name not set") , $configxml));
79  $oa = $fam->getAttribute($aid);
80  if (!$oa) exportExit($action, sprintf(_("Config file %s : unknow attribute name %s") , $configxml, $aid));
81  $exportAttribute[$fam->id][$oa->id] = $oa->id;
82  $exportAttribute[$fam->id][$oa->fieldSet->id] = $oa->fieldSet->id;
83  }
84  }
85  }
86  // set the export's search
87  if ((!$fldid) && $selection) {
88  $selection = json_decode($selection);
89  include_once ("DATA/Class.DocumentSelection.php");
90  $os = new Fdl_DocumentSelection($selection);
91  $ids = $os->getIdentificators();
92  $s = new SearchDoc($dbaccess);
93 
94  $s->addFilter(getSqlCond($ids, "id", true));
95  $s->setObjectReturn();
96  $exportname = "selection";
97  } else {
98  if (!$fldid) exportExit($action, _("no export folder specified"));
99 
100  $fld = new_Doc($dbaccess, $fldid);
101  if ($fldid && (!$fld->isAlive())) exportExit($action, sprintf(_("folder/search %s not found") , $fldid));
102  if ($famid == "") $famid = $action->getArgument("famid");
103  $exportname = str_replace(array(
104  " ",
105  "'",
106  '/'
107  ) , array(
108  "_",
109  "",
110  "-"
111  ) , $fld->title);
112  //$tdoc = getChildDoc($dbaccess, $fldid,"0","ALL",array(),$action->user->id,"TABLE",$famid);
113  $s = new SearchDoc($dbaccess, $famid);
114  $s->setObjectReturn();
115 
116  $s->dirid = $fld->id;
117  }
118  $s->search();
119  $err = $s->searchError();
120  if ($err) exportExit($action, $err);
121 
122  $foutdir = uniqid(getTmpDir() . "/exportxml");
123  if (!mkdir($foutdir)) exportExit($action, sprintf("cannot create directory %s", $foutdir));
124  //$fname=sprintf("%s/FDL/Layout/fdl.xsd",DEFAULT_PUBDIR);
125  //copy($fname,"$foutdir/fdl.xsd");
126  $xsd = array();
127  $count = 0;
128  if ($flog) {
129  fputs($flog, sprintf("EXPORT OPTION ID : %s <%s>\n", $fldid, $fld->getTitle()));
130  }
131 
132  while ($doc = $s->nextDoc()) {
133  //print $doc->exportXml();
134  if ($doc->doctype != 'C') {
135  $ftitle = str_replace(array(
136  '/',
137  '\\',
138  '?',
139  '*',
140  ':'
141  ) , '-', $doc->getTitle());
142  $fname = sprintf("%s/%s{%d}.xml", $foutdir, $ftitle, $doc->id);
143  $err = $doc->exportXml($xml, $wfile, $fname, $wident, $flat, $exportAttribute);
144  // file_put_contents($fname,$doc->exportXml($wfile));
145  if ($err) exportExit($action, $err);
146  $count++;
147  if ($flog) fputs($flog, sprintf("EXPORT DOC OK : <%s> [%d]\n", $doc->getTitle() , $doc->id));
148  if (!isset($xsd[$doc->fromid])) {
149  $fam = new_doc($dbaccess, $doc->fromid);
150  $fname = sprintf("%s/%s.xsd", $foutdir, strtolower($fam->name));
151  file_put_contents($fname, $fam->getXmlSchema());
152  $xsd[$doc->fromid] = true;
153  }
154  }
155  }
156 
157  if ($flog) {
158  fputs($flog, sprintf("EXPORT COUNT OK : %d\n", $count));
159  fputs($flog, sprintf("EXPORT END OK : %s\n", Doc::getTimeDate(0, true)));
160  fclose($flog);
161  }
162 
163  if ($eformat == "X") {
164  $zipfile = uniqid(getTmpDir() . "/xml") . ".zip";
165  system(sprintf("cd %s && zip -r %s -- * > /dev/null", escapeshellarg($foutdir) , escapeshellarg($zipfile)) , $ret);
166  if (is_file($zipfile)) {
167  system(sprintf("rm -fr %s", $foutdir));
168  Http_DownloadFile($zipfile, "$exportname.zip", "application/x-zip", false, false, true);
169  } else {
170  exportExit($action, _("Zip Archive cannot be created"));
171  }
172  } elseif ($eformat == "Y") {
173  $xmlfile = uniqid(getTmpDir() . "/xml") . ".xml";
174 
175  $fh = fopen($xmlfile, 'x');
176  if ($fh === false) {
177  return exportExit($action, sprintf("%s (Error creating file '%s')", _("Xml file cannot be created") , htmlspecialchars($xmlfile)));
178  }
179  /* Print XML header */
180  $xml_head = <<<EOF
181 <?xml version="1.0" encoding="UTF-8"?>
182 <documents date="%s" author="%s" name="%s">
183 
184 EOF;
185  $xml_head = sprintf($xml_head, htmlspecialchars(strftime("%FT%T")) , htmlspecialchars(User::getDisplayName($action->user->id)) , htmlspecialchars($exportname));
186  $xml_footer = "</documents>";
187 
188  $ret = fwrite($fh, $xml_head);
189  if ($ret === false) {
190  return exportExit($action, sprintf("%s (Error writing to file '%s')", _("Xml file cannot be created") , htmlspecialchars($xmlfile)));
191  }
192  fflush($fh);
193  /* chdir into dir containing the XML files
194  * and concatenate them into the output file
195  */
196  $cwd = getcwd();
197  $ret = chdir($foutdir);
198  if ($ret === false) {
199  return exportExit($action, sprintf("%s (Error chdir to '%s')", _("Xml file cannot be created") , htmlspecialchars($foutdir)));
200  }
201 
202  $cmd = sprintf("cat -- *xml | grep -v '<?xml version=\"1.0\" encoding=\"UTF-8\"?>' >> %s", escapeshellarg($xmlfile));
203  system($cmd, $ret);
204 
205  $ret = chdir($cwd);
206  if ($ret === false) {
207  return exportExit($action, sprintf("%s (Error chdir to '%s')", _("Xml file cannot be created") , htmlspecialchars($cwd)));
208  }
209  /* Print XML footer */
210  $ret = fseek($fh, 0, SEEK_END);
211  if ($ret === - 1) {
212  return exportExit($action, sprintf("%s (Error fseek '%s')", _("Xml file cannot be created") , htmlspecialchars($xmlfile)));
213  }
214 
215  $ret = fwrite($fh, $xml_footer);
216  if ($ret === false) {
217  return exportExit($action, sprintf("%s (Error writing to file '%s')", _("Xml file cannot be created") , htmlspecialchars($xmlfile)));
218  }
219  fflush($fh);
220  fclose($fh);
221 
222  if (is_file($xmlfile)) {
223  system(sprintf("rm -fr %s", escapeshellarg($foutdir)));
224  Http_DownloadFile($xmlfile, "$exportname.xml", "text/xml", false, false, true);
225  } else {
226  exportExit($action, _("Xml file cannot be created"));
227  }
228  }
229 }
231 {
232  $log = $action->getArgument("log");
233  if ($log) {
234  if (file_put_contents($log, "EXPORT " . _("ERROR :") . $err) === false) {
235  $err = sprintf(_("Cannot write to log %s") , $log) . "\n" . $err;
236  }
237  }
238  $action->exitError($err);
239 }
240 ?>
← centre documentaire © anakeen - published under CC License - Dynacase