Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
import_tar.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Import Set of documents and files with directories
8  *
9  * @author Anakeen
10  * @version $Id: import_tar.php,v 1.8 2007/08/02 15:34:12 eric Exp $
11  * @package FDL
12  * @subpackage
13  */
14 /**
15  */
16 
17 include_once ("FDL/import_file.php");
18 include_once ("WHAT/Lib.Prefix.php");
19 
20 define("TARUPLOAD", DEFAULT_PUBDIR . "/var/upload/");
21 define("TAREXTRACT", "/extract/");
22 define("TARTARS", "/tars/");
23 
25 {
26  global $pubdir;
27  $dtar = $action->getParam("FREEDOM_UPLOADDIR");
28  if (substr($dtar, 0, 1) != '/') {
29  $dtar = $pubdir . '/' . $dtar;
30  }
31  if ($dtar == "") $dtar = TARUPLOAD;
32  return $dtar . "/" . $action->user->login . TARTARS;
33 }
34 function getTarExtractDir(Action & $action, $tar)
35 {
36  global $pubdir;
37  $dtar = $action->getParam("FREEDOM_UPLOADDIR");
38  if (substr($dtar, 0, 1) != '/') {
39  $dtar = $pubdir . '/' . $dtar;
40  }
41  if ($dtar == "") $dtar = TARUPLOAD;
42  return $dtar . "/" . $action->user->login . TAREXTRACT . $tar . "_D";
43 }
44 /**
45  * import a directory files
46  * @param action $action current action
47  * @param string $ftar tar file
48  */
49 function import_tar(&$action, $ftar, $dirid = 0, $famid = 7)
50 {
51 }
52 /**
53  * import a directory files
54  * @param action $action current action
55  * @param string $ldir local directory path
56  * @param int $dirid folder id to add new documents
57  * @param int $famid default family for raw files
58  * @param int $dfldid
59  * @param bool $onlycsv if true only fdl.csv file is imported
60  * @param bool $analyze dry-mode it true
61  * @param string $csvLinebreak default line break sequence
62  * @return array
63  */
64 function import_directory(&$action, $ldir, $dirid = 0, $famid = 7, $dfldid = 2, $onlycsv = false, $analyze = false, $csvLinebreak = '\n')
65 {
66  // first see if fdl.csv file
67  global $importedFiles;
68 
69  $dbaccess = $action->dbaccess;
70  $tr = array();
71  if (is_dir($ldir)) {
72  if ($handle = opendir($ldir)) {
73  $lfamid = 0;
74  $lfldid = 0;
75  while (false !== ($file = readdir($handle))) {
76  $absfile = str_replace("//", "/", "$ldir/$file");
77  if (is_file($absfile) && ($file == "fdl.csv")) {
78  $tr = analyze_csv($absfile, $dbaccess, $dirid, $lfamid, $lfldid, $analyze);
79  }
80  }
81  if ($lfamid > 0) $famid = $lfamid; // set local default family identifier
82  if ($lfldid > 0) $dfldid = $lfldid; // set local default family folder identifier
83  rewinddir($handle);
84  /* This is the correct way to loop over the directory. */
85  $defaultdoc = createDoc($dbaccess, $famid);
86  if (!$defaultdoc) $action->AddWarningMsg(sprintf(_("you cannot create this kind [%s] of document") , $famid));
87  $fimgattr = null;
88  if (($lfamid == 0) && ($famid == 7)) {
89  $defaultimg = createDoc($dbaccess, "IMAGE");
90  $fimgattr = $defaultimg->GetFirstFileAttributes();
91  }
92  $newdir = createDoc($dbaccess, $dfldid);
93  if (!$newdir) $action->AddWarningMsg(sprintf(_("you cannot create this kind [%s] of folder") , $dfldid));
94  $ffileattr = $defaultdoc->GetFirstFileAttributes();
95 
96  $dir = null;
97  if ($dirid > 0) {
98  /*
99  * @var Dir $dir
100  */
102  }
103 
104  $nfile = 0;
105  while (false !== ($file = readdir($handle))) {
106  $nfile++;
107  $absfile = str_replace("//", "/", "$ldir/$file");
108  $level = substr_count($absfile, "/");
109  $index = "f$level/$nfile";
110  if (is_file($absfile)) {
111  if (!$onlycsv) { // add also unmarked files
112  if (!isset($importedFiles[$absfile])) {
113  if (!isUTF8($file)) $file = utf8_encode($file);
114  if (!isUTF8($ldir)) $ldir = utf8_encode($ldir);
115  $tr[$index] = array(
116  "err" => ($defaultdoc) ? "" : sprintf(_("you cannot create this kind [%s] of document") , $famid) ,
117  "folderid" => 0,
118  "foldername" => $ldir,
119  "filename" => $file,
120  "title" => "$file",
121  "specmsg" => "",
122  "id" => "",
123  "anaclass" => "fileclass",
124  "familyid" => 0,
125  "familyname" => "",
126  "action" => ""
127  );
128  $err = AddVaultFile($dbaccess, $absfile, $analyze, $vfid);
129 
130  if ($err != "") {
131  $tr[$index]["err"] = $err;
132  } else {
133  if (($lfamid == 0) && ($famid == 7) && (substr($vfid, 0, 5) == "image")) {
134  $ddoc = & $defaultimg;
135  $fattr = $fimgattr->id;
136  } else {
137  $ddoc = & $defaultdoc;
138  if ($ffileattr) {
139  $fattr = $ffileattr->id;
140  } else {
141  $tr[$index]["err"] = "no file attribute";
142  }
143  }
144 
145  $tr[$index]["familyname"] = $ddoc->fromname;
146  $tr[$index]["familyid"] = $ddoc->fromid;
147  $tr[$index]["action"] = N_("to be add");
148  if (!$analyze) {
149  $ddoc->Init();
150  $ddoc->setValue($fattr, $vfid);
151  $err = $ddoc->Add();
152  if ($err != "") {
153  $tr[$index]["action"] = N_("not added");
154  $tr[$index]["err"] = $err;
155  } else {
156  $ddoc->addHistoryEntry(sprintf("create by import from archive %s", substr(basename($ldir) , 0, -2)));
157  $tr[$index]["action"] = N_("added");
158  $tr[$index]["id"] = $ddoc->id;
159  $ddoc->postStore();
160  $ddoc->Modify();
161  if ($dirid > 0) {
162  $dir->insertDocument($ddoc->id);
163  }
164  $tr[$index]["title"] = $ddoc->getTitle();
165  $tr[$index]["id"] = $ddoc->id;
166  $tr[$index]["familyid"] = $ddoc->fromid;
167  $tr[$index]["familyname"] = $ddoc->fromname;
168  }
169  }
170  }
171  }
172  }
173  } else if (is_dir($absfile) && ($file[0] != '.')) {
174 
175  if (!isUTF8($file)) $file = utf8_encode($file);
176  if (!isUTF8($ldir)) $ldir = utf8_encode($ldir);
177 
178  if ((!$onlycsv) || (!preg_match("/^[0-9]+-.*_D$/i", $file))) {
179  $tr[$index] = array(
180  "err" => ($newdir) ? "" : sprintf(_("you cannot create this kind [%s] of folder") , $dfldid) ,
181  "folderid" => 0,
182  "foldername" => $ldir,
183  "filename" => $file,
184  "title" => "$file",
185  "specmsg" => "",
186  "id" => "",
187  "anaclass" => "fldclass",
188  "familyid" => $newdir->fromid,
189  "familyname" => $newdir->fromname,
190  "action" => N_("to be add")
191  );
192  if (!$analyze) {
193  $newdir->Init();
194  $newdir->setTitle($file);
195  $err = $newdir->Add();
196  if ($err != "") {
197  $tr[$index]["action"] = N_("not added");
198  } else {
199  $tr[$index]["action"] = N_("added");
200  $tr[$index]["id"] = $newdir->id;
201  if ($dirid > 0) {
202  $dir->insertDocument($newdir->id);
203  }
204  }
205  }
206  }
207  $itr = import_directory($action, $absfile, $newdir->id, $famid, $dfldid, $onlycsv, $analyze);
208  $tr = array_merge($tr, $itr);
209  }
210  }
211  closedir($handle);
212  return $tr;
213  }
214  } else {
215  $err = sprintf("cannot open local directory %s", $ldir);
216  return array(
217  "err" => $err
218  );
219  }
220  return array();
221 }
222 
223 function analyze_csv($fdlcsv, $dbaccess, $dirid, &$famid, &$dfldid, $analyze, $csvLinebreak = '\n')
224 {
225  $tr = array();
226  $fcsv = fopen($fdlcsv, "r");
227  if ($fcsv) {
228  $ldir = dirname($fdlcsv);
229  $nline = 0;
230  $nbdoc = 0;
231  $tcolorder = array();
232  $separator = $enclosure = "auto";
233  importDocumentDescription::detectAutoCsvOptions($fdlcsv, $separator, $enclosure);
234  if ($separator == '') {
235  $separator = ';';
236  }
237  if ($enclosure == '') {
238  $enclosure = '"';
239  }
240  while ($data = fgetcsv($fcsv, 0, $separator, $enclosure)) {
241  $nline++;
242  $level = substr_count($ldir, "/");
243  $index = "c$level/$nline";
244  $tr[$index] = array(
245  "err" => "",
246  "msg" => "",
247  "specmsg" => "",
248  "folderid" => 0,
249  "foldername" => "",
250  "filename" => "",
251  "title" => "",
252  "id" => "",
253  "values" => array() ,
254  "familyid" => 0,
255  "familyname" => "",
256  "action" => "-"
257  );
258  if ($csvLinebreak) {
259  $data = array_map(function ($v) use ($csvLinebreak)
260  {
261  return str_replace($csvLinebreak, "\n", $v);
262  }
263  , $data);
264  }
265  switch ($data[0]) {
266  // -----------------------------------
267 
268  case "DFAMID":
269  $famid = $data[1];
270  //print "\n\n change famid to $famid\n";
271  break;
272  // -----------------------------------
273 
274  case "DFLDID":
275  $dfldid = $data[1];
276  //print "\n\n change dfldid to $dfldid\n";
277  break;
278 
279  case "ORDER":
280  if (is_numeric($data[1])) $orfromid = $data[1];
281  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
282 
283  $tcolorder[$orfromid] = getOrder($data);
284  $tr[$index]["action"] = sprintf(_("new column order %s") , implode(" - ", $tcolorder[$orfromid]));
285  break;
286 
287  case "KEYS":
288  if (is_numeric($data[1])) $orfromid = $data[1];
289  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
290 
291  $tkeys[$orfromid] = getOrder($data);
292  if (($tkeys[$orfromid][0] == "") || (count($tkeys[$orfromid]) == 0)) {
293  $tr[$index]["err"] = sprintf(_("error in import keys : %s") , implode(" - ", $tkeys[$orfromid]));
294  unset($tkeys[$orfromid]);
295  $tr[$index]["action"] = "ignored";
296  } else {
297  $tr[$index]["action"] = sprintf(_("new import keys : %s") , implode(" - ", $tkeys[$orfromid]));
298  }
299  break;
300 
301  case "DOC":
302  if (is_numeric($data[1])) $fromid = $data[1];
303  else $fromid = getFamIdFromName($dbaccess, $data[1]);
304  if (isset($tkeys[$fromid])) $tk = $tkeys[$fromid];
305  else $tk = array(
306  "title"
307  );
308  $tr[$index] = csvAddDoc($dbaccess, $data, $dirid, $analyze, $ldir, "update", $tk, array() , $tcolorder[$fromid]);
309  if ($tr[$index]["err"] == "") $nbdoc++;
310 
311  break;
312  }
313  }
314  fclose($fcsv);
315  }
316  return $tr;
317  }
318  /**
319  * decode characters wihich comes from windows zip
320  * @param $s string to decode
321  * @return string decoded string
322  */
323  function WNGBdecode($s)
324  {
325  $td = array(
326  144 => "É",
327  130 => "é",
328  133 => "à",
329  135 => "ç",
330  138 => "è",
331  151 => "ù",
332  212 => "È",
333  210 => "Ê",
334  128 => "Ç",
335  183 => "ê",
336  136 => "û",
337  183 => "À",
338  136 => "ê",
339  150 => "û",
340  147 => "ô",
341  137 => "ë",
342  139 => "ï"
343  );
344 
345  $s2 = $s;
346  for ($i = 0; $i < strlen($s); $i++) {
347  if (isset($td[ord($s[$i]) ])) $s2[$i] = $td[ord($s[$i]) ];
348  }
349  return $s2;
350  }
351  /**
352  * rename file name which comes from windows zip
353  * @param string $ldir directory to decode
354  * @return string empty string on success, non-empty string with error message on failure
355  */
357  {
358  $handle = opendir($ldir);
359  if ($handle === false) {
360  return sprintf(_("Error opening directory '%s'.") , $ldir);
361  }
362  while (false !== ($file = readdir($handle))) {
363  if ($file[0] != ".") {
364  $afile = "$ldir/$file";
365 
366  if (is_file($afile)) {
367  if (rename($afile, "$ldir/" . WNGBdecode($file)) === false) {
368  return sprintf(_("Error renaming '%s' to '%s'.") , $afile, WNGBdecode($file));
369  };
370  } else if (is_dir($afile)) {
371  if (($err = WNGBDirRename($afile)) != '') {
372  return $err;
373  }
374  }
375  }
376  }
377 
378  closedir($handle);
379  if (rename($ldir, WNGBdecode($ldir)) === false) {
380  return sprintf(_("Error renaming '%s' to '%s'.") , $ldir, WNGBdecode($ldir));
381  }
382  return '';
383  }
384 
global $action
import_tar(&$action, $ftar, $dirid=0, $famid=7)
Definition: import_tar.php:49
global $pubdir
Definition: vault_init.php:18
$ldir
Definition: resizeimg.php:145
analyze_csv($fdlcsv, $dbaccess, $dirid, &$famid, &$dfldid, $analyze, $csvLinebreak= '\n')
Definition: import_tar.php:223
static detectAutoCsvOptions($csvFileName, &$separator= 'auto', &$enclosure= 'auto')
getTarExtractDir(Action &$action, $tar)
Definition: import_tar.php:34
$file
const TARTARS
Definition: import_tar.php:22
const TAREXTRACT
Definition: import_tar.php:21
if($famId) $s
getOrder(array $orderdata)
Definition: import_file.php:97
$csvLinebreak
isUTF8($string)
Definition: Lib.Common.php:748
const DEFAULT_PUBDIR
Definition: Lib.Prefix.php:28
getTarUploadDir(Action &$action)
Definition: import_tar.php:24
createDoc($dbaccess, $fromid, $control=true, $defaultvalues=true, $temporary=false)
WNGBDirRename($ldir)
Definition: import_tar.php:356
AddVaultFile($dbaccess, $path, $analyze, &$vid)
const TARUPLOAD
Definition: import_tar.php:20
getFamIdFromName($dbaccess, $name)
new_Doc($dbaccess, $id= '', $latest=false)
$dir
Definition: resizeimg.php:144
getParam($name, $def="")
$dbaccess
Definition: checkVault.php:17
csvAddDoc($dbaccess, $data, $dirid=0, $analyze=false, $ldir= '', $policy="add", $tkey=array("title"), $prevalues=array(), $torder=array())
Definition: import_file.php:64
if($file) if($subject==""&&$file) if($subject=="") $err
$analyze
N_($s)
Definition: Lib.Common.php:18
WNGBdecode($s)
Definition: import_tar.php:323
$data
import_directory(&$action, $ldir, $dirid=0, $famid=7, $dfldid=2, $onlycsv=false, $analyze=false, $csvLinebreak= '\n')
Definition: import_tar.php:64
← centre documentaire © anakeen