Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
ExportDocument.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  const useAclDocumentType = ":useDocument";
12  const useAclAccountType = ":useAccount";
13 
14  protected $alreadyExported = array();
15  protected $lattr;
16  protected $prevfromid = - 1;
17  protected $familyName = '';
18  protected $csvEnclosure = '"';
19  protected $csvSeparator = ',';
20  protected $encoding = 'utf-8';
21  protected $verifyAttributeAccess = false;
22  protected $attributeGrants = array();
23  protected $noAccessText = \FormatCollection::noAccessText;
24  protected $exportAccountType = self::useAclAccountType;
25 
26  private $logicalName = [];
27 
28  private $logins = [];
29  /**
30  * Use when cannot access attribut value
31  * Due to visibility "I"
32  * @param string $noAccessText
33  */
34  public function setNoAccessText($noAccessText)
35  {
36  $this->noAccessText = $noAccessText;
37  }
38  /**
39  * If true, attribute with "I" visibility are not returned
40  * @param boolean $verifyAttributeAccess
41  */
42  public function setVerifyAttributeAccess($verifyAttributeAccess)
43  {
44  $this->verifyAttributeAccess = $verifyAttributeAccess;
45  }
46  /**
47  * @param string $encoding
48  */
49  public function setEncoding($encoding)
50  {
51  $this->encoding = $encoding;
52  }
53  /**
54  * @param string $csvSeparator
55  */
56  public function setCsvSeparator($csvSeparator)
57  {
58  $this->csvSeparator = $csvSeparator;
59  }
60  /**
61  * @param string $csvEnclosure
62  */
63  public function setCsvEnclosure($csvEnclosure)
64  {
65  $this->csvEnclosure = $csvEnclosure;
66  }
67 
68  public function reset()
69  {
70  $this->alreadyExported = array();
71  }
72  /**
73  * @return array
74  */
75  public function getTrans()
76  {
77  static $htmlTransMapping = false;
78  if (!$htmlTransMapping) {
79  // to invert HTML entities
80  $htmlTransMapping = get_html_translation_table(HTML_ENTITIES);
81  $htmlTransMapping = array_flip($htmlTransMapping);
82  $htmlTransMapping = array_map("utf8_encode", $htmlTransMapping);
83  }
84  return $htmlTransMapping;
85  }
86 
87  protected function getUserLogin($uid)
88  {
89  if (!isset($this->logins[$uid])) {
90  simpleQuery("", sprintf("select login from users where id=%d", $uid) , $login, true, true);
91  $this->logins[$uid] = $login ? $login : 0;
92  }
93  return $this->logins[$uid];
94  }
95  protected function getUserLogicalName($uid)
96  {
97  if (!isset($this->logicalName[$uid])) {
98  simpleQuery("", sprintf("select name from docread where id=(select fid from users where id = %d)", $uid) , $logicalName, true, true);
99  $this->logicalName[$uid] = $logicalName ? $logicalName : 0;
100  }
101  return $this->logicalName[$uid];
102  }
103  /**
104  * @param resource $fout
105  * @param string|int $docid
106  */
107  public function exportProfil($fout, $docid)
108  {
109  if (!$docid) return;
110  // import its profile
111  $doc = \new_Doc("", $docid); // needed to have special acls
112  $doc->acls[] = "viewacl";
113  $doc->acls[] = "modifyacl";
114  if ($doc->name != "") $name = $doc->name;
115  else $name = $doc->id;
116 
118  $q = new \QueryDb($dbaccess, "DocPerm");
119  $q->AddQuery(sprintf("docid=%d", $doc->profid));
120  $q->order_by = "userid";
121  $acls = $q->Query(0, 0, "TABLE");
122 
123  $tAcls = array();
124  if ($acls) {
125  foreach ($acls as $va) {
126  $up = $va["upacl"];
127  $uid = $va["userid"];
128 
129  if ($uid >= STARTIDVGROUP) {
130  $qvg = new \QueryDb($dbaccess, "VGroup");
131  $qvg->AddQuery(sprintf("num=%d", $uid));
132  $tvu = $qvg->Query(0, 1, "TABLE");
133  $uid = sprintf("attribute(%s)", $tvu[0]["id"]);
134  } else {
135  if ($this->exportAccountType === self::useAclDocumentType) {
136  $uln = $this->getUserLogicalName($uid);
137  if ($uln) {
138  if (preg_match('/^attribute\(.*\)$/', $uln)) {
139  $uid = sprintf("document(%s)", $uln);
140  } else {
141  $uid = $uln;
142  }
143  } else {
144  $uid = $this->getUserLogin($uid);
145  if ($uid) {
146  $uid = sprintf("account(%s)", $uid);
147  }
148  }
149  } else {
150  $uid = $this->getUserLogin($uid);
151  if (preg_match('/^attribute\(.*\)$/', $uid)) {
152  $uid = sprintf("account(%s)", $uid);
153  }
154  }
155  }
156  foreach ($doc->acls as $kAcl => $acl) {
157  $bup = ($doc->ControlUp($up, $acl) == "");
158  if ($uid && $bup) {
159  $tAcls[$kAcl . "-" . $uid] = ["uid" => $uid, "acl" => $acl];
160  }
161  }
162  }
163  }
164  // add extended Acls
165  if ($doc->extendedAcls) {
166  $extAcls = array_keys($doc->extendedAcls);
167  $aclCond = GetSqlCond($extAcls, "acl");
168  simpleQuery($dbaccess, sprintf("select * from docpermext where docid=%d and %s order by userid", $doc->profid, $aclCond) , $eAcls);
169 
170  foreach ($eAcls as $kAcl => $aAcl) {
171  $uid = $aAcl["userid"];
172  if ($uid >= STARTIDVGROUP) {
173  $qvg = new \QueryDb($dbaccess, "VGroup");
174  $qvg->AddQuery(sprintf("num=%d", $uid));
175  $tvu = $qvg->Query(0, 1, "TABLE");
176  $uid = sprintf("attribute(%s)", $tvu[0]["id"]);
177  } else {
178  $uid = $this->getUserLogin($uid);
179  if (preg_match('/^attribute\(.*\)$/', $uid)) {
180  $uid = sprintf("account(%s)", $uid);
181  }
182  }
183  if ($uid) {
184  $tAcls["e".$kAcl . "-" . $uid] = ["uid" => $uid, "acl" => $aAcl["acl"]];
185  }
186  }
187  }
188  if (count($tAcls) > 0) {
189  $data = array(
190  "PROFIL",
191  $name,
192  $this->exportAccountType,
193  ""
194  );
195  ksort($tAcls);
196  foreach ($tAcls as $ku => $oneAcl) {
197  //fputs_utf8($fout, ";" . $tpa[$ku] . "=" . $uid);
198  $data[] = sprintf("%s=%s", $oneAcl["acl"], $oneAcl["uid"]);
199  }
200  \Dcp\WriteCsv::fput($fout, $data);
201  }
202  }
203  /**
204  * @deprecated rename to csvExport
205  */
206  public function cvsExport(\Doc & $doc, &$ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat)
207  {
208  $this->csvExport($doc, $ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat);
209  }
210  public function csvExport(\Doc & $doc, &$ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat)
211  {
212 
213  if (!$doc->isAffected()) return;
214  if (in_array($doc->id, $this->alreadyExported)) return;
215  $this->alreadyExported[] = $doc->id;
216 
219  \Dcp\WriteCsv::$encoding = ($wutf8) ? "utf-8" : "iso8859-15";
220 
221  $efldid = '';
222  $dbaccess = $doc->dbaccess;
223  if ($this->prevfromid != $doc->fromid) {
224  if (($eformat != "I") && ($this->prevfromid > 0)) {
225  \Dcp\WriteCsv::fput($fout, array());
226  }
227  $adoc = $doc->getFamilyDocument();
228  if ($adoc->name != "") $this->familyName = $adoc->name;
229  else $this->familyName = $adoc->id;
230  if (!$this->familyName) return;
231  $this->lattr = $adoc->GetExportAttributes($wfile, $nopref);
232  $data = array();
233 
234  if ($eformat == "I") {
235  $data = array(
236  "//FAM",
237  $adoc->title . "(" . $this->familyName . ")",
238  "<specid>",
239  "<fldid>"
240  );
241  //fputs_utf8($fout, "//FAM;" . $adoc->title . "(" . $this->familyName . ");<specid>;<fldid>;");
242 
243  }
244  foreach ($this->lattr as $attr) {
245  $data[] = $attr->getLabel();
246  //fputs_utf8($fout, str_replace(SEPCHAR, ALTSEPCHAR, $attr->getLabel()) . SEPCHAR);
247 
248  }
249  WriteCsv::fput($fout, $data);
250  //fputs_utf8($fout, "\n");
251  if ($eformat == "I") {
252  $data = array(
253  "ORDER",
254  $this->familyName,
255  "",
256  ""
257  );
258  //fputs_utf8($fout, "ORDER;" . $this->familyName . ";;;");
259  foreach ($this->lattr as $attr) {
260  $data[] = $attr->id;
261  //fputs_utf8($fout, $attr->id . ";");
262 
263  }
264  WriteCsv::fput($fout, $data);
265  // fputs_utf8($fout, "\n");
266 
267  }
268  $this->prevfromid = $doc->fromid;
269  }
270  $docName = '';
271  if ($doc->name != "" && $doc->locked != - 1) {
272  $docName = $doc->name;
273  } else if ($wprof) {
274  if ($doc->locked != - 1) {
275  $err = $doc->setNameAuto(true);
276  $docName = $doc->name;
277  }
278  } else if ($wident) {
279  $docName = $doc->id;
280  }
281  $data = array();
282  if ($eformat == "I") {
283  $data = array(
284  "DOC",
285  $this->familyName,
286  $docName,
287  $efldid
288  );
289  }
290  // write values
291  foreach ($this->lattr as $attr) {
292  if ($this->verifyAttributeAccess && !\Dcp\VerifyAttributeAccess::isAttributeAccessGranted($doc, $attr)) {
293  $data[] = $this->noAccessText;
294  continue;
295  }
296 
297  if ($eformat == 'F') {
298  if ($this->csvEnclosure) {
299  $value = str_replace(array(
300  '<BR>',
301  '<br/>'
302  ) , array(
303  "\n",
304  "\\n"
305  ) , $doc->getHtmlAttrValue($attr->id, '', false, -1, false));
306  } else {
307  $value = str_replace(array(
308  '<BR>',
309  '<br/>'
310  ) , '\\n', $doc->getHtmlAttrValue($attr->id, '', false, -1, false));
311  }
312  } else {
313 
314  $value = $doc->getRawValue($attr->id);
315  }
316  // invert HTML entities
317  if (($attr->type == "image") || ($attr->type == "file")) {
318  $tfiles = $doc->vault_properties($attr);
319  $tf = array();
320  foreach ($tfiles as $f) {
321  $ldir = $doc->id . '-' . preg_replace('/[^a-zA-Z0-9_.-]/', '_', unaccent($doc->title)) . "_D";
322  $fname = $ldir . '/' . unaccent($f["name"]);
323  $tf[] = $fname;
324  $ef[$fname] = array(
325  "path" => $f["path"],
326  "ldir" => $ldir,
327  "fname" => unaccent($f["name"])
328  );
329  }
330  $value = implode("\n", $tf);
331  } else if ($attr->type == "docid" || $attr->type == "account" || $attr->type == "thesaurus") {
332  $docrevOption = $attr->getOption("docrev", "latest");
333  if ($value != "") {
334  if (strstr($value, "\n") || ($attr->getOption("multiple") == "yes")) {
335  $tid = $doc->rawValueToArray($value);
336  $tn = array();
337  foreach ($tid as $did) {
338  $brtid = explode("<BR>", $did);
339  $tnbr = array();
340  foreach ($brtid as $brid) {
341  $n = getNameFromId($dbaccess, $brid);
342  if ($n) {
343  if ($docrevOption === "latest") {
344  $tnbr[] = $n;
345  } else {
346  addWarningMsg(sprintf(_("Doc %s : Attribut \"%s\" reference revised identifier : cannot use logical name") , $doc->getTitle() , $attr->getLabel()));
347  $tnbr[] = $brid;
348  }
349  } else {
350  $tnbr[] = $brid;
351  }
352  }
353  $tn[] = implode('<BR>', $tnbr);
354  }
355  $value = implode("\n", $tn);
356  } else {
358  if ($n) {
359  if ($docrevOption === "latest") {
360  $value = $n;
361  } else {
362  addWarningMsg(sprintf(_("Doc %s : Attribut \"%s\" reference revised identifier : cannot use logical name") , $doc->getTitle() , $attr->getLabel()));
363  }
364  }
365  }
366  }
367  } else if ($attr->type == "htmltext") {
368  $value = $attr->prepareHtmltextForExport($value);
369  if ($wfile) {
370  $value = preg_replace_callback('/(<img.*?src=")(((?=.*docid=(.*?)&)(?=.*attrid=(.*?)&)(?=.*index=(-?[0-9]+)))|(file\/(.*?)\/[0-9]+\/(.*?)\/(-?[0-9]+))).*?"/', function ($matches) use (&$ef)
371  {
372  if (isset($matches[7])) {
373  $docid = $matches[8];
374  $attrid = $matches[9];
375  $index = $matches[10] == "-1" ? 0 : $matches[10];
376  } else {
377  $docid = $matches[4];
378  $index = $matches[6] == "-1" ? 0 : $matches[6];
379  $attrid = $matches[5];
380  }
381  $doc = \new_Doc(getDbAccess() , $docid);
382  $attr = $doc->getAttribute($attrid);
383  $tfiles = $doc->vault_properties($attr);
384  $f = $tfiles[$index];
385 
386  $ldir = $doc->id . '-' . preg_replace('/[^a-zA-Z0-9_.-]/', '_', unaccent($doc->title)) . "_D";
387  $fname = $ldir . '/' . unaccent($f["name"]);
388  $ef[$fname] = array(
389  "path" => $f["path"],
390  "ldir" => $ldir,
391  "fname" => unaccent($f["name"])
392  );
393  return $matches[1] . "file://" . $fname . '"';
394  }
395  , $value);
396  }
397  } else {
398  $trans = $this->getTrans();
399  $value = preg_replace_callback('/(\&[a-zA-Z0-9\#]+;)/s', function ($matches) use ($trans)
400  {
401  return strtr($matches[1], $trans);
402  }
403  , $value);
404  // invert HTML entities which ascii code like &#232;
405  $value = preg_replace_callback('/\&#([0-9]+);/s', function ($matches)
406  {
407  return chr($matches[1]);
408  }
409  , $value);
410  }
411  $data[] = $value;
412  }
413  \Dcp\WriteCsv::fput($fout, $data);
414  if ($wprof) {
415  $profid = ($doc->dprofid) ? $doc->dprofid : $doc->profid;
416  if ($profid == $doc->id) {
417  $this->exportProfil($fout, $doc->id);
418  } else if ($profid > 0) {
419  $name = getNameFromId($dbaccess, $profid);
420  $dname = $doc->name;
421  if (!$dname) $dname = $doc->id;
422  if (!$name) $name = $profid;
423  if (!isset($tdoc[$profid])) {
424  $tdoc[$profid] = true;
425  $pdoc = \new_Doc($dbaccess, $profid);
426  $this->csvExport($pdoc, $ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat);
427  }
428  $data = array(
429  "PROFIL",
430  $dname,
431  $name,
432  ""
433  );
434  \Dcp\WriteCsv::fput($fout, $data);
435  }
436  }
437  }
438  /**
439  * @param string $exportAccountType
440  * @throws Exception
441  */
442  public function setExportAccountType($exportAccountType)
443  {
444  $availables = [self::useAclAccountType, self::useAclDocumentType];
445  if (!in_array($exportAccountType, $availables)) {
446  throw new Exception("PRFL0300", $exportAccountType, implode(", ", $availables));
447  }
448  $this->exportAccountType = $exportAccountType;
449  }
450 }
vault_properties(NormalAttribute $attr)
Definition: Class.Doc.php:8224
setCsvSeparator($csvSeparator)
getHtmlAttrValue($attrid, $target="_self", $htmllink=2, $index=-1, $entities=true, $abstract=false)
Definition: Class.Doc.php:6489
setCsvEnclosure($csvEnclosure)
$tdoc
if($_POST["login"]=="")
Definition: chgpasswd.php:19
& getAttribute($idAttr, &$oa=null, $useMask=true)
Definition: Class.Doc.php:2152
csvExport(\Doc &$doc, &$ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat)
$ldir
Definition: resizeimg.php:145
static rawValueToArray($v)
Definition: Class.Doc.php:6228
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
setNoAccessText($noAccessText)
$csvEnclosure
const STARTIDVGROUP
static $separator
Definition: WriteCsv.php:16
getFamilyDocument()
Definition: Class.Doc.php:1482
addWarningMsg($msg)
Definition: Lib.Common.php:95
isAffected()
setEncoding($encoding)
$docid
Definition: cleanFamily.php:13
$login
Definition: dav.php:40
$csvSeparator
getTitle($id="-1", $def="", $latest=false)
Definition: Class.Doc.php:8715
exportProfil($fout, $docid)
getDbAccess()
Definition: Lib.Common.php:368
setVerifyAttributeAccess($verifyAttributeAccess)
setNameAuto($temporary=false)
Definition: Class.Doc.php:6937
new_Doc($dbaccess, $id= '', $latest=false)
cvsExport(\Doc &$doc, &$ef, $fout, $wprof, $wfile, $wident, $wutf8, $nopref, $eformat)
GetSqlCond($Table, $column, $integer=false)
getNameFromId($dbaccess, $id)
setExportAccountType($exportAccountType)
static $enclosure
Definition: WriteCsv.php:15
$dbaccess
Definition: checkVault.php:17
unaccent($s)
Definition: Lib.Util.php:569
static fput($handler, array $data)
Definition: WriteCsv.php:22
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
Definition: Lib.Common.php:484
static $encoding
Definition: WriteCsv.php:17
if($file) if($subject==""&&$file) if($subject=="") $err
getRawValue($idAttr, $def="")
Definition: Class.Doc.php:3117
$value
$data
← centre documentaire © anakeen