Platform  3.1
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  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Import Set of documents and files with directories
9  *
10  * @author Anakeen 2000
11  * @version $Id: import_tar.php,v 1.8 2007/08/02 15:34:12 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/import_file.php");
20 
21 define("TARUPLOAD", getTmpDir() . "/upload/");
22 define("TAREXTRACT", "/extract/");
23 define("TARTARS", "/tars/");
24 
26 {
27  $dtar = $action->getParam("FREEDOM_UPLOADDIR");
28  if ($dtar == "") $dtar = TARUPLOAD;
29  return $dtar . "/" . $action->user->login . TARTARS;
30 }
31 function getTarExtractDir(&$action, $tar)
32 {
33  $dtar = $action->getParam("FREEDOM_UPLOADDIR");
34  if ($dtar == "") $dtar = TARUPLOAD;
35  return $dtar . "/" . $action->user->login . TAREXTRACT . $tar . "_D";
36 }
37 /**
38  * import a directory files
39  * @param action $action current action
40  * @param string $ftar tar file
41  */
42 function import_tar(&$action, $ftar, $dirid = 0, $famid = 7)
43 {
44 }
45 /**
46  * import a directory files
47  * @param action $action current action
48  * @param string $ldir local directory path
49  */
50 function import_directory(&$action, $ldir, $dirid = 0, $famid = 7, $dfldid = 2, $onlycsv = false, $analyze = false)
51 {
52  // first see if fdl.csv file
53  global $importedFiles;
54 
55  $dbaccess = $action->GetParam("FREEDOM_DB");
56  $tr = array();
57  if (is_dir($ldir)) {
58  if ($handle = opendir($ldir)) {
59  $lfamid = 0;
60  while (false !== ($file = readdir($handle))) {
61  $absfile = "$ldir/$file";
62  $absfile = str_replace("//", "/", "$ldir/$file");
63 
64  if (is_file($absfile) && ($file == "fdl.csv")) {
65  $tr = analyze_csv($absfile, $dbaccess, $dirid, $lfamid, $lfldid, $analyze);
66  }
67  }
68  if ($lfamid > 0) $famid = $lfamid; // set local default family identificator
69  if ($lfldid > 0) $dfldid = $lfldid; // set local default family folder identificator
70  rewinddir($handle);
71  /* This is the correct way to loop over the directory. */
72  $defaultdoc = createDoc($dbaccess, $famid);
73  if (!$defaultdoc) $action->AddWarningMsg(sprintf(_("you cannot create this kind [%s] of document") , $famid));
74  if (($lfamid == 0) && ($famid == 7)) {
75  $defaultimg = createDoc($dbaccess, "IMAGE");
76  $fimgattr = $defaultimg->GetFirstFileAttributes();
77  }
78  $newdir = createDoc($dbaccess, $dfldid);
79  if (!$newdir) $action->AddWarningMsg(sprintf(_("you cannot create this kind [%s] of folder") , $dfldid));
80  $ffileattr = $defaultdoc->GetFirstFileAttributes();
81 
82  if ($dirid > 0) {
83  $dir = new_Doc($dbaccess, $dirid);
84  }
85 
86  $nfile = 0;
87  while (false !== ($file = readdir($handle))) {
88  $nfile++;
89  $absfile = str_replace("//", "/", "$ldir/$file");
90  $level = substr_count($absfile, "/");
91  $index = "f$level/$nfile";
92  if (is_file($absfile)) {
93  if (!$onlycsv) { // add also unmarked files
94  if (!isset($importedFiles[$absfile])) {
95  if (!isUTF8($file)) $file = utf8_encode($file);
96  if (!isUTF8($ldir)) $ldir = utf8_encode($ldir);
97  $tr[$index] = array(
98  "err" => ($defaultdoc) ? "" : sprintf(_("you cannot create this kind [%s] of document") , $famid) ,
99  "folderid" => 0,
100  "foldername" => $ldir,
101  "filename" => $file,
102  "title" => "",
103  "specmsg" => "",
104  "id" => 0,
105  "anaclass" => "fileclass",
106  "familyid" => $ddoc->fromid,
107  "familyname" => "",
108  "action" => ""
109  );
110  $err = AddVaultFile($dbaccess, $absfile, $analyze, $vfid);
111 
112  if ($err != "") {
113  $tr[$index]["err"] = $err;
114  } else {
115  if (($lfamid == 0) && ($famid == 7) && (substr($vfid, 0, 5) == "image")) {
116  $ddoc = & $defaultimg;
117  $fattr = $fimgattr->id;
118  } else {
119  $ddoc = & $defaultdoc;
120  $fattr = $ffileattr->id;
121  }
122  $tr[$index]["familyid"] = $ddoc->fromid;
123  $tr[$index]["action"] = _("to be add");
124  if (!$analyze) {
125  $ddoc->Init();
126  $ddoc->setValue($fattr, $vfid);
127  $err = $ddoc->Add();
128  if ($err != "") {
129  $tr[$index]["action"] = _("not added");
130  } else {
131  $ddoc->addComment(sprintf("create by import from archive %s", substr(basename($ldir) , 0, -2)));
132  $tr[$index]["action"] = _("added");
133  $tr[$index]["id"] = $ddoc->id;
134  $ddoc->PostModify();
135  $ddoc->Modify();
136  if ($dirid > 0) {
137  $dir->AddFile($ddoc->id);
138  }
139  $tr[$index]["title"] = $ddoc->getTitle();
140  $tr[$index]["id"] = $ddoc->id;
141  $tr[$index]["familyid"] = $ddoc->fromid;
142  $tr[$index]["familyname"] = $ddoc->fromname;
143  }
144  }
145  }
146  }
147  }
148  } else if (is_dir($absfile) && ($file[0] != '.')) {
149 
150  if (!isUTF8($file)) $file = utf8_encode($file);
151  if (!isUTF8($ldir)) $ldir = utf8_encode($ldir);
152 
153  if ((!$onlycsv) || (!preg_match("/^[0-9]+-.*_D$/i", $file))) {
154  $tr[$index] = array(
155  "err" => ($newdir) ? "" : sprintf(_("you cannot create this kind [%s] of folder") , $dfldid) ,
156  "folderid" => 0,
157  "foldername" => $ldir,
158  "filename" => $file,
159  "title" => "",
160  "specmsg" => "",
161  "id" => 0,
162  "anaclass" => "fldclass",
163  "familyid" => $newdir->fromid,
164  "familyname" => "",
165  "action" => _("to be add")
166  );
167  if (!$analyze) {
168  $newdir->Init();
169  $newdir->setTitle($file);
170  $err = $newdir->Add();
171  if ($err != "") {
172  $tr[$index]["action"] = _("not added");
173  } else {
174  $tr[$index]["action"] = _("added");
175  if ($dirid > 0) {
176  $dir->AddFile($newdir->id);
177  }
178  }
179  }
180  }
181  $itr = import_directory($action, $absfile, $newdir->id, $famid, $dfldid, $onlycsv, $analyze);
182  $tr = array_merge($tr, $itr);
183  }
184  }
185 
186  closedir($handle);
187  return $tr;
188  }
189  } else {
190  $err = sprintf("cannot open local directory %s", $ldir);
191  return array(
192  "err" => $err
193  );
194  }
195 }
196 
197 function analyze_csv($fdlcsv, $dbaccess, $dirid, &$famid, &$dfldid, $analyze)
198 {
199  $tr = array();
200  $fcsv = fopen($fdlcsv, "r");
201  if ($fcsv) {
202  $ldir = dirname($fdlcsv);
203  while ($data = fgetcsv($fcsv, 0, ";")) {
204  $nline++;
205  $level = substr_count($ldir, "/");
206  $index = "c$level/$nline";
207  switch ($data[0]) {
208  // -----------------------------------
209 
210  case "DFAMID":
211  $famid = $data[1];
212  //print "\n\n change famid to $famid\n";
213  break;
214  // -----------------------------------
215 
216  case "DFLDID":
217  $dfldid = $data[1];
218  //print "\n\n change dfldid to $dfldid\n";
219  break;
220 
221  case "ORDER":
222  if (is_numeric($data[1])) $orfromid = $data[1];
223  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
224 
225  $tcolorder[$orfromid] = getOrder($data);
226  $tr[$index]["action"] = sprintf(_("new column order %s") , implode(" - ", $tcolorder[$orfromid]));
227  break;
228 
229  case "KEYS":
230  if (is_numeric($data[1])) $orfromid = $data[1];
231  else $orfromid = getFamIdFromName($dbaccess, $data[1]);
232 
233  $tkeys[$orfromid] = getOrder($data);
234  if (($tkeys[$orfromid][0] == "") || (count($tkeys[$orfromid]) == 0)) {
235  $tr[$index]["err"] = sprintf(_("error in import keys : %s") , implode(" - ", $tkeys[$orfromid]));
236  unset($tkeys[$orfromid]);
237  $tr[$index]["action"] = "ignored";
238  } else {
239  $tr[$index]["action"] = sprintf(_("new import keys : %s") , implode(" - ", $tkeys[$orfromid]));
240  }
241  break;
242 
243  case "DOC":
244  if (is_numeric($data[1])) $fromid = $data[1];
245  else $fromid = getFamIdFromName($dbaccess, $data[1]);
246  if (isset($tkeys[$fromid])) $tk = $tkeys[$fromid];
247  else $tk = array(
248  "title"
249  );
250  $tr[$index] = csvAddDoc($dbaccess, $data, $dirid, $analyze, $ldir, "update", $tk, array() , $tcolorder[$fromid]);
251  if ($tr[$index]["err"] == "") $nbdoc++;
252  if ($tr[$index]["action"] != "") $tr[$index]["action"] = _($tr[$index]["action"]);
253  break;
254  }
255  }
256  fclose($fcsv);
257  }
258  return $tr;
259  }
260  /**
261  * decode characters wihich comes from windows zip
262  * @param $s string to decode
263  * @return string decoded string
264  */
265  function WNGBdecode($s)
266  {
267  $td = array(
268  144 => "É",
269  130 => "é",
270  133 => "à",
271  135 => "ç",
272  138 => "è",
273  151 => "ù",
274  212 => "È",
275  210 => "Ê",
276  128 => "Ç",
277  183 => "ê",
278  136 => "û",
279  183 => "À",
280  136 => "ê",
281  150 => "û",
282  147 => "ô",
283  137 => "ë",
284  139 => "ï"
285  );
286 
287  $s2 = $s;
288  for ($i = 0; $i < strlen($s); $i++) {
289  if (isset($td[ord($s[$i]) ])) $s2[$i] = $td[ord($s[$i]) ];
290  }
291  return $s2;
292  }
293  /**
294  * rename file name which comes from windows zip
295  * @param $ldir directory to decode
296  * @return void
297  */
299  {
300  $handle = opendir($ldir);
301  while (false !== ($file = readdir($handle))) {
302  if ($file[0] != ".") {
303  $afile = "$ldir/$file";
304 
305  if (is_file($afile)) {
306  rename($afile, "$ldir/" . WNGBdecode($file));
307  } else if (is_dir($afile)) {
308  WNGBDirRename($afile);
309  }
310  }
311  }
312 
313  closedir($handle);
314  rename($ldir, WNGBdecode($ldir));
315  }
316 ?>
← centre documentaire © anakeen - published under CC License - Dynacase