18 include_once (
"FDL/import_tar.php");
19 include_once (
'FDL/Class.XMLSplitter.php');
59 $splitdir = uniqid(
getTmpDir() .
"/xmlsplit");
61 if (!is_dir($splitdir)) {
62 throw new \Dcp\Exception(
"IMPC0002", $splitdir);
69 system(sprintf(
"/bin/rm -fr %s ", $splitdir));
76 $splitdir = uniqid(
getTmpDir() .
"/xmlsplit");
78 if (!is_dir($splitdir)) {
79 throw new \Dcp\Exception(
"IMPC0003", $splitdir);
81 self::unZipXmlDocument($zipFile, $splitdir);
86 system(sprintf(
"/bin/rm -fr %s ", $splitdir));
94 $zipfiles = realpath($zipfiles);
95 $ll = exec(sprintf(
"cd %s && unzip %s", $splitdir, $zipfiles) ,
$out, $retval);
97 throw new \Dcp\Exception(
"IMPC0004", $zipfiles, $ll);
109 if ($handle = opendir($splitdir)) {
111 while (
false !== (
$file = readdir($handle))) {
112 if (
$file[0] !=
"." && is_file(
"$splitdir/$file")) {
120 foreach ($files as
$file) {
121 $this->importXmlFileDocument(
"$splitdir/$file",
$log);
130 public function importXmlFileDocument($xmlfile, &
$log)
132 static $families = array();
143 "values" => array() ,
149 if (!is_file($xmlfile)) {
150 $err = sprintf(_(
"Xml import file %s not found") , $xmlfile);
156 $splitdir = dirname($xmlfile);
160 $dom = new \Dcp\Utils\XDOMDocument();
162 $dom->load($xmlfile, 0, $error);
164 catch(\Dcp\Utils\XDOMDocumentException $e) {
165 $log[
"action"] =
'ignored';
166 $log[
"err"] = $e->getMessage();
167 return $e->getMessage();
170 $root = $dom->documentElement;
171 $id = $root->getAttribute(
"id");
172 $name = $root->getAttribute(
"name");
173 $key = $root->getAttribute(
"key");
174 $folders = $root->getAttribute(
"folders");
176 $tkey = explode(
',', $key);
177 foreach ($tkey as & $v) {
182 $family = $root->tagName;
184 if (!isset($families[
$famid])) {
192 $la = $families[
$famid]->getNormalAttributes();
201 $rootAttrs = $root->attributes;
203 foreach ($rootAttrs as $rname => $ra) {
204 $v = $root->getAttribute($rname);
206 $tord[] =
"extra:$rname";
215 foreach (
$la as $k => & $v) {
216 $n = $dom->getElementsByTagName($v->id);
221 foreach ($n as $item) {
228 $id = $item->getAttribute(
"id");
230 $logicalName = $item->getAttribute(
"name");
231 $name = $item->getAttribute(
"name");
233 if (strpos($name,
',') !==
false) {
234 $names = explode(
',', $name);
236 foreach ($names as $lname) {
239 $id = implode(
",", $lids);
246 if ($item->nodeValue) {
247 $afamid = $v->format;
250 $msg.= sprintf(_(
"No identifier found for relation '%s' %s in %s file") .
"\n", $logicalName ? $logicalName : $item->nodeValue, $v->id, $xmlfile);
255 if ($v->getOption(
"multiple") ==
"yes") {
256 $id = str_replace(
',',
'\n', $id);
257 if ($v->inArray()) $id = str_replace(array(
267 $href = $item->getAttribute(
"href");
271 $vid = $item->getAttribute(
"vid");
272 $mime = $item->getAttribute(
"mime");
273 $title = $item->getAttribute(
"title");
275 $val[] =
"$mime|$vid|$title";
283 $val[] = str_replace(
"\n",
" ", str_replace(
">\n",
">", $item->nodeValue));
287 $val[] = $item->nodeValue;
293 $tdoc[] = implode(
"\n", $val);
296 $o = new \importSingleDocument();
297 if ($tkey)
$o->setKey($tkey);
298 if ($tord)
$o->setOrder($tord);
301 $o->setFilePath($splitdir);
302 $o->setVerifyAttributeAccess($this->verifyAttributeAccess);
304 $folders = str_replace(
',',
' ', $folders);
305 $tfolders = explode(
' ', $folders);
306 foreach ($tfolders as $k => $aFolder) {
307 if (!$aFolder) unset($tfolders[$k]);
311 $o->setTargetDirectories($tfolders);
313 } elseif (!empty($opt[
"dirid"])) {
314 $o->setTargetDirectory($opt[
"dirid"]);
318 $log =
$o->getImportResult();
321 $log[
"err"].=
"\n" . $msg;
322 $log[
"action"] =
"ignored";
328 $xs = new \XMLSplitter($splitdir);
329 $xs->split($xmlfiles);
334 if ($handle = opendir($splitdir)) {
335 while (
false !== (
$file = readdir($handle))) {
336 if (
$file[0] !=
".") {
337 if (!is_dir(
"$splitdir/$file")) {
347 $len = fputs($fd, $str);
348 if ($len ===
false || $len != strlen($str)) {
349 $metadata = stream_get_meta_data($fd);
350 $filename = ((is_array($metadata) && isset($metadata[
'uri'])) ? $metadata[
'uri'] :
'*unknown*file*');
352 throw new \Dcp\Exception(
"IMPC0012",
$filename);
364 static $mediaindex = 0;
366 if (!file_exists(
$file)) {
367 throw new \Dcp\Exception(
"IMPC0001",
$file);
370 if (!is_dir(
"$dir/$mediadir")) mkdir(
"$dir/$mediadir");
371 $f = fopen(
$file,
"r");
373 throw new \Dcp\Exception(
"IMPC0009",
$file);
375 $nf = fopen(
$file .
".new",
"w");
377 throw new \Dcp\Exception(
"IMPC0010",
$file .
".new");
381 $buffer = fgets($f, 4096);
383 if (preg_match(
"/<([a-z_0-9-]+)[^>]*mime=\"[^\"]+\"(.*)>(.*)/", $buffer, $reg)) {
384 if ((substr($reg[2], -1) !=
"/") && (substr($reg[2], -strlen($reg[1]) - 3) !=
'></' . $reg[1])) {
386 if (preg_match(
"/<([a-z_0-9-]+)[^>]*title=\"([^\"]+)\"/", $buffer, $regtitle)) {
388 }
else if (preg_match(
"/<([a-z_0-9-]+)[^>]*title='([^']+)'/", $buffer, $regtitle)) {
391 if (strpos(
$title, DIRECTORY_SEPARATOR) !==
false) {
392 throw new \Dcp\Exception(
"IMPC0005", DIRECTORY_SEPARATOR,
$title);
394 $mediaIndexDir = sprintf(
"%s/%s/%d",
$dir, $mediadir, $mediaindex);
395 if (!file_exists($mediaIndexDir)) {
396 if (mkdir($mediaIndexDir) ===
false) {
397 throw new \Dcp\Exception(
"IMPC0006", $mediaIndexDir);
400 if (!is_dir($mediaIndexDir)) {
401 throw new \Dcp\Exception(
"IMPC0007", $mediaIndexDir);
403 $rfin = sprintf(
"%s/%d/%s", $mediadir, $mediaindex,
$title);
404 $fin = sprintf(
"%s/%s",
$dir, $rfin);
405 $fi = fopen($fin,
"w");
407 throw new \Dcp\Exception(
"IMPC0008", $fi);
409 if (preg_match(
"/(.*)(<$tag [^>]*)>/", $buffer, $regend)) {
412 if (preg_match(
"/>([^<]*)<\/$tag>(.*)/", $buffer, $regend)) {
414 self::fputsError($fi, $regend[1]);
415 self::fputsError($nf,
"</$tag>");
416 self::fputsError($nf, $regend[2]);
419 self::fputsError($fi, $reg[3]);
421 while (!feof($f) && (!$findtheend)) {
422 $buffer = fgets($f, 4096);
423 if (preg_match(
"/(.*)<\/$tag>(.*)/", $buffer, $regend)) {
424 self::fputsError($fi, $regend[1]);
425 self::fputsError($nf,
"</$tag>");
426 self::fputsError($nf, $regend[2]);
429 self::fputsError($fi, $buffer);
434 self::base64Decodefile($fin);
436 self::fputsError($nf, $buffer);
438 }
else if (preg_match(
"/<img.*?src=\"data:[^;]*;base64,(.*)/", $buffer, $reg)) {
439 if (preg_match(
"/<img.*?title=\"([^\"]+)\"/", $buffer, $regtitle)) {
441 }
else if (preg_match(
"/<img.*?title='([^']+)'/", $buffer, $regtitle)) {
444 if (strpos(
$title, DIRECTORY_SEPARATOR) !==
false) {
445 throw new \Dcp\Exception(
"IMPC0005", DIRECTORY_SEPARATOR,
$title);
447 $mediaIndexDir = sprintf(
"%s/%s/%d",
$dir, $mediadir, $mediaindex);
448 if (!file_exists($mediaIndexDir)) {
449 if (mkdir($mediaIndexDir) ===
false) {
450 throw new \Dcp\Exception(
"IMPC0006", $mediaIndexDir);
453 if (!is_dir($mediaIndexDir)) {
454 throw new \Dcp\Exception(
"IMPC0007", $mediaIndexDir);
456 $rfin = sprintf(
"%s/%d/%s", $mediadir, $mediaindex,
$title);
457 $fin = sprintf(
"%s/%s",
$dir, $rfin);
458 $fi = fopen($fin,
"w");
460 throw new \Dcp\Exception(
"IMPC0008", $fi);
462 if (preg_match(
"/(.*)(<img.*?)src=\"data:[^;]*;base64,/", $buffer, $regend)) {
463 $chaintoput = $regend[1] . $regend[2] .
' src="file://' . $rfin .
'"';
464 self::fputsError($nf, $chaintoput);
466 if (preg_match(
"/<img.*?src=\"data:[^;]*;base64,([^\"]*)\"(.*)/", $buffer, $regend)) {
468 self::fputsError($fi, $regend[1]);
469 self::fputsError($nf, $regend[2]);
472 self::fputsError($fi, $reg[1]);
474 while (!feof($f) && (!$findtheend)) {
475 $buffer = fgets($f, 4096);
476 if (preg_match(
"/([^\"]*)\"(.*)/", $buffer, $regend)) {
477 self::fputsError($fi, $regend[1]);
478 self::fputsError($nf, $regend[2]);
481 self::fputsError($fi, $buffer);
486 self::base64Decodefile($fin);
488 self::fputsError($nf, $buffer);
499 if (rename(
$file .
".new",
$file) ===
false) {
500 throw new \Dcp\Exception(
"IMPC0011",
$file .
".new",
$file);
506 $tmpdest = uniqid(
getTmpDir() .
"/fdlbin");
507 $chunkSize = 1024 * 30;
509 $dst = fopen($tmpdest,
'wb');
510 while (!feof($src)) {
511 fwrite($dst, base64_decode(fread($src, $chunkSize)));
extractFilesFromXmlDirectory($splitdir)
static extractFileFromXmlDocument($file)
static escapeEntities($str)
setVerifyAttributeAccess($verifyAttributeAccess)
importXmlDirectory($splitdir)
getIdFromTitle($dbaccess, $title, $famid="", $only=false)
splitXmlDocument($xmlfiles, $splitdir)
importSingleXmlFile($xmlFile)
static fputsError($fd, $str)
static extractFilesFromXmlDirectory($splitdir)
getFamIdFromName($dbaccess, $name)
setImportDirectory($dirid)
extractFileFromXmlDocument($file)
static base64Decodefile($filename)
static unescapeEntities($str)
getIdFromName($dbaccess, $name)
if($file) if($subject==""&&$file) if($subject=="") $err
static splitXmlDocument($xmlfiles, $splitdir)
static unZipXmlDocument($zipfiles, $splitdir)