Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
mailcard.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  * Functions to send document by email
9  *
10  * @author Anakeen 2000
11  * @version $Id: mailcard.php,v 1.83 2008/12/16 15:52:35 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/Class.Doc.php");
20 include_once ("FDL/sendmail.php");
21 include_once ("Class.MailAccount.php");
22 // -----------------------------------
23 function mailcard(&$action)
24 {
25 
26  $docid = GetHttpVars("id");
27  $cr = GetHttpVars("cr"); // want a status
28  $dbaccess = $action->GetParam("FREEDOM_DB");
30  // control sending
31  $err = $doc->control('send');
32  if ($err != "") $action->exitError($err);
33 
34  $mailto = "";
35  $mailcc = "";
36  $mailbcc = "";
37  $mailfrom = GetHttpVars("_mail_from");
38 
39  foreach (array(
40  "plain",
41  "link"
42  ) as $format) {
43  $tmailto[$format] = array();
44  $tmailcc[$format] = array();
45  $tmailbcc[$format] = array();
46  }
47 
48  $tuid = array(); // list of user id to notify
49  $mt = GetHttpVars("to"); // simple arguments (can be use with wsh
50  if ($mt == "") {
51  $rtype = GetHttpVars("_mail_copymode", "");
52  $raddr = GetHttpVars("_mail_recip", "");
53  $idraddr = GetHttpVars("_mail_recipid", "");
54  $tformat = GetHttpVars("_mail_sendformat", "");
55  if ((is_array($raddr)) && (count($raddr) > 0)) {
56  foreach ($raddr as $k => $v) {
57  $v = trim($v);
58  if ($v != "") {
59  if ($tformat[$k] == "") $tformat[$k] = "plain";
60  switch ($rtype[$k]) {
61  case "cc":
62  $tmailcc[$tformat[$k]][$v] = $v;
63  break;
64 
65  case "bcc":
66  $tmailbcc[$tformat[$k]][$v] = $v;
67  break;
68 
69  default:
70  $tmailto[$tformat[$k]][$v] = $v;
71  if ($idraddr[$k] > 0) $tuid[] = $idraddr[$k];
72  break;
73  }
74  }
75  }
76  }
77  } else {
78  // other notation
79  $tmailto["plain"][0] = $mt;
80  $oldcc = GetHttpVars("cc");
81  if ($oldcc) $tmailcc["plain"][0] = $oldcc;
82  $oldbcc = GetHttpVars("bcc");
83  if ($oldbcc) $tmailbcc["plain"][0] = $oldbcc;
84  if ($mailfrom == "") $mailfrom = GetHttpVars("from");
85  }
86 
87  $sendedmail = false;
88  foreach (array(
89  "plain",
90  "link"
91  ) as $format) {
92 
93  $mailto = implode(",", $tmailto[$format]);
94  $mailcc = implode(",", $tmailcc[$format]);
95  $mailbcc = implode(",", $tmailbcc[$format]);
96  // correct trim --->
97  setHttpVar("_mail_to", $mailto);
98  setHttpVar("_mail_cc", $mailcc);
99  setHttpVar("_mail_bcc", $mailbcc);
100  setHttpVar("_mail_from", $mailfrom);
101  if ($format == "link") setHttpVar("_mail_format", "htmlnotif");
102  if (($mailto != "") || ($mailcc != "") || ($mailbcc != "")) {
104  $sendedmail = true;
105  }
106  }
107 
108  if ($cr == "Y") {
109  if ($err != "") $action->exitError(sprintf(_("the document %s has not be sended :\n %s") , $doc->title, $err));
110  elseif (!$sendedmail) $action->addWarningMsg(sprintf(_("the document %s has not been sended : no recipient") , $doc->title));
111  }
112 
113  foreach ($tuid as $uid) {
114  if ($uid > 0) {
115  $tu = getTDoc($dbaccess, $uid);
116  $wuid = getv($tu, "us_whatid");
117  // $err=$doc->addComment(_("document received for"),HISTO_NOTICE,"RCPTDOC",$wuid);
118  $err = $doc->addUTag($wuid, "TOVIEW");
119  }
120  }
121 
122  redirect($action, GetHttpVars("redirect_app", "FDL") , GetHttpVars("redirect_act", "FDL_CARD&latest=Y&refreshfld=Y&id=" . $doc->id) , $action->GetParam("CORE_STANDURL"));
123 }
124 // -----------------------------------
126 {
127 
128  $sendcopy = true;
129  $addfiles = array();
130  $userinfo = true;
131  $err = sendCard($action, GetHttpVars("id") , GetHttpVars("_mail_to", '') , GetHttpVars("_mail_cc", "") , GetHttpVars("_mail_subject") , GetHttpVars("zone") , GetHttpVars("ulink", "N") == "Y", GetHttpVars("_mail_cm", "") , GetHttpVars("_mail_from", "") , GetHttpVars("_mail_bcc", "") , GetHttpVars("_mail_format", "html") , $sendcopy, $addfiles, $userinfo, GetHttpVars("_mail_savecopy", "no") == "yes");
132 
133  if ($err != "") return $err;
134  // also change state sometime with confirmmail action
135  $state = GetHttpVars("state");
136 
137  if ($state != "") {
138 
139  $docid = GetHttpVars("id");
140 
141  $dbaccess = $action->GetParam("FREEDOM_DB");
143  if ($doc->wid > 0) {
144  if ($state != "-") {
145  $wdoc = new_Doc($dbaccess, $doc->wid);
146  $wdoc->Set($doc);
147  $err = $wdoc->ChangeState($state, _("email sended") , true);
148  if ($err != "") $action->addWarningMsg($err);
149  }
150  } else {
151  $action->AddLogMsg(sprintf(_("the document %s is not related to a workflow") , $doc->title));
152  }
153  }
154 }
155 // -----------------------------------
156 function sendCard(&$action, $docid, $to, $cc, $subject, $zonebodycard = "", // define mail layout
157 $ulink = false, // don't see hyperlink
158 $comment = "", $from = "", $bcc = "", $format = "html", // define view action
159 $sendercopy = true, // true : a copy is send to the sender according to the Freedom user parameter
160 $addfiles = array() , $userinfo = true, $savecopy = false)
161 {
162  // -----------------------------------
163  $viewonly = (GetHttpVars("viewonly", "N") == "Y");
164  if ((!$viewonly) && ($to == "") && ($cc == "") && ($bcc == "")) return _("mail dest is empty");
165  // -----------------------------------
166  global $ifiles;
167  global $tfiles;
168  global $tmpfile;
169  global $vf;
170  global $doc;
171  global $pubdir;
172  global $action;
173 
174  $ifiles = array();
175  $tfiles = array();
176  $tmpfile = array();
177  $mixed = true; // to see file as attachement
178  // set title
179  setHttpVar("id", $docid); // for view zone
180  if (GetHttpVars("_mail_format") == "") setHttpVar("_mail_format", $format);
181 
182  $dbaccess = $action->GetParam("FREEDOM_DB");
183  $doc = new_Doc($dbaccess, $docid);
184 
185  $ftitle = str_replace(array(
186  " ",
187  "/",
188  ")",
189  "("
190  ) , "_", $doc->title);
191  $ftitle = str_replace("'", "", $ftitle);
192  $ftitle = str_replace("\"", "", $ftitle);
193  $ftitle = str_replace("&", "", $ftitle);
194 
195  $to = str_replace("\"", "'", $to);
196  $from = str_replace("\"", "'", $from);
197  $cc = str_replace("\"", "'", $cc);
198  $bcc = str_replace("\"", "'", $bcc);
199 
201  $pubdir = $action->getParam("CORE_PUBDIR");
202  $szone = false;
203 
204  $multi_mix = new Fdl_Mail_mimePart('', array(
205  'content_type' => 'multipart/mixed'
206  ));
207  $multi_rel = $multi_mix->addSubpart('', array(
208  'content_type' => 'multipart/related'
209  ));
210 
211  if ($sendercopy && $action->getParam("FDL_BCC") == "yes") {
212  $umail = getMailAddr($action->user->id);
213  if ($umail != "") {
214  if ($bcc != "") $bcc = "$bcc,$umail";
215  else $bcc = "$umail";
216  }
217  }
218  if ($from == "") {
219  $from = getMailAddr($action->user->id);
220  if ($from == "") $from = getParam('SMTP_FROM');
221  if ($from == "") $from = $action->user->login . '@' . php_uname('n');
222  }
223 
224  if ($subject == "") $subject = $ftitle;
225  $subject = str_replace("\"", "'", $subject);
226 
227  $layout = "maildoc.xml"; // the default
228  if ($format == "htmlnotif") {
229  $layout = "mailnotification.xml";
230  $zonebodycard = "FDL:MAILNOTIFICATION:S";
231  }
232 
233  if ($zonebodycard == "") $zonebodycard = $doc->defaultmview;
234  if ($zonebodycard == "") $zonebodycard = $doc->defaultview;
235 
236  if (preg_match("/[A-Z]+:[^:]+:S/", $zonebodycard, $reg)) $szone = true; // the zonebodycard is a standalone zone ?
237  if (preg_match("/[A-Z]+:[^:]+:T/", $zonebodycard, $reg)) setHttpVar("dochead", "N"); // the zonebodycard without head ?
238  if (preg_match("/[A-Z]+:[^:]+:B/", $zonebodycard, $reg)) $binary = true;
239 
240  if ($binary) {
241 
242  $binfile = $doc->viewDoc($zonebodycard);
243  if (!is_file($binfile)) $err = $binfile;
244 
245  if ($err == "") {
246  $engine = $doc->getZoneTransform($zonebodycard);
247  if ($engine) {
248  include_once ("FDL/Lib.Vault.php");
249  $outfile = uniqid(getTmpDir() . "/conv") . ".$engine";
250  $err = convertFile($binfile, $engine, $outfile, $info);
251  if ($err == "") {
252  $mime = getSysMimeFile($outfile, basename($outfile));
253  $ext = getExtension($mime);
254  $binfile = $outfile;
255  }
256  } else {
257  $mime = getSysMimeFile($binfile, basename($binfile));
258  $ext = getExtension($mime);
259  if (!$ext) {
260  $tplfile = $doc->getZoneFile($zonebodycard);
261  $ext = getFileExtension($tplfile);
262  }
263  }
264 
265  $multi_mix->addSubpart('', array(
266  'body_file' => $binfile,
267  'content_type' => $mime,
268  'charset' => 'UTF-8',
269  'filename' => sprintf("%s.%s", $doc->title, $ext) ,
270  'dfilename' => sprintf("%s.%s", $doc->title, $ext) ,
271  'encoding' => 'base64',
272  'name_encoding' => 'quoted-printable',
273  'filename_encoding' => 'quoted-printable',
274  'disposition' => 'attachment'
275  ));
276 
277  $zonebodycard = "FDL:EMPTY";
278  }
279  }
280 
281  if ($err == "") {
282  setHttpVar("target", "mail");
283  if (preg_match("/html/", $format, $reg)) {
284 
285  if ($action->GetParam("CORE_URLINDEX") != "") {
286  $turl = parse_url($action->GetParam("CORE_URLINDEX"));
287  $url = $turl["scheme"] . '://' . $turl["host"];
288  if (isset($turl["port"])) $url.= ':' . $turl["port"];
289  if (isset($turl["path"])) $url.= $turl["path"] . "/";
290  $baseurl = $url;
291  $absurl = $action->GetParam("CORE_URLINDEX");
292  } else {
293  $absurl = $action->GetParam("CORE_ABSURL") . "/";
294  $baseurl = $action->GetParam("CORE_ABSURL");
295  }
296 
297  if ($szone) {
298 
299  $sgen = $doc->viewDoc($zonebodycard, "mail", $ulink, false, true);
300 
301  $doc->lay->Set("absurl", $absurl);
302  $doc->lay->Set("baseurl", $baseurl);
303  $sgen = $doc->lay->gen();
304  if ($comment != "") {
305  $comment = nl2br($comment);
306  $sgen = preg_replace("'<body([^>]*)>'i", "<body \\1><P>$comment<P><HR>", $sgen);
307  }
308  } else {
309  // contruct HTML mail
310  $docmail = new Layout(getLayoutFile("FDL", $layout) , $action);
311 
312  $docmail->Set("TITLE", $doc->title);
313  $docmail->Set("ID", $doc->id);
314  $docmail->Set("zone", $zonebodycard);
315  $docmail->Set("absurl", $absurl);
316  $docmail->Set("baseurl", $baseurl);
317  if ($comment != "") {
318  $docmail->setBlockData("COMMENT", array(
319  array(
320  "boo"
321  )
322  ));
323  $docmail->set("comment", nl2br($comment));
324  }
325 
326  $sgen = $docmail->gen();
327  }
328  if ($viewonly) {
329  echo $sgen;
330  exit;
331  }
332 
333  $sgen1 = preg_replace("/src=\"(FDL\/geticon[^\"]+)\"/ei", "imgvaultfile('\\1')", $sgen);
334 
335  $sgen1 = preg_replace(array(
336  "/SRC=\"([^\"]+)\"/e",
337  "/src=\"([^\"]+)\"/e"
338  ) , "srcfile('\\1')", $sgen1);
339 
340  $pfout = uniqid(getTmpDir() . "/" . $doc->id);
341  $fout = fopen($pfout, "w");
342 
343  fwrite($fout, $sgen1);
344 
345  fclose($fout);
346  }
347 
348  if (preg_match("/pdf/", $format, $reg)) {
349  // ---------------------------
350  // contruct PDF mail
351  if ($szone) {
352  $sgen = $doc->viewDoc($zonebodycard, "mail", false);
353  } else {
354 
355  $docmail2 = new Layout(getLayoutFile("FDL", $layout) , $action);
356 
357  $docmail2->Set("zone", $zonebodycard);
358  $docmail2->Set("TITLE", $doc->title);
359 
360  $sgen = $docmail2->gen();
361  }
362  $sgen2 = preg_replace("/src=\"([^\"]+)\"/ei", "realfile('\\1')", $sgen);
363 
364  $ppdf = uniqid(getTmpDir() . "/" . $doc->id) . ".pdf.html";
365  $fout = fopen($ppdf, "w");
366  $sgen2 = preg_replace('/\xE2\x82\xAC/', '&euro;', $sgen2);
367  fwrite($fout, $sgen2);
368  fclose($fout);
369  }
370  // ---------------------------
371  // contruct mail_mime object
372  $htmlPart = null;
373  if (preg_match("/html/", $format, $reg)) {
374  $htmlPart = $multi_rel->addSubpart('', array(
375  'body_file' => $pfout,
376  'content_type' => 'text/html',
377  'charset' => 'UTF-8',
378  'encoding' => 'quoted-printable',
379  'name_encoding' => 'quoted-printable',
380  'filename_encoding' => 'quoted-printable'
381  ));
382  } else if ($format == "pdf") {
383  $multi_rel->addSubpart($comment, array(
384  'content_type' => 'text/plain',
385  'charset' => 'UTF-8',
386  'encoding' => 'quoted-printable',
387  'name_encoding' => 'quoted-printable',
388  'filename_encoding' => 'quoted-printable'
389  ));
390  }
391 
392  if ($format != "pdf") {
393  // ---------------------------
394  // insert attached files
395  if (preg_match_all("/(href|src)=\"cid:([^\"]*)\"/i", $sgen, $match)) {
396  $tcids = $match[2]; // list of file references inserted in mail
397  $afiles = $doc->GetFileAttributes();
398  $taids = array_keys($afiles);
399  if (count($afiles) > 0) {
400  foreach ($tcids as $kf => $vaf) {
401  $tf = explode("+", $vaf);
402  if (count($tf) == 1) {
403  $aid = $tf[0];
404  $index = - 1;
405  } else {
406  $aid = $tf[0];
407  $index = $tf[1];
408  }
409  if (in_array($aid, $taids)) {
410  $tva = array();
411  $cidindex = "";
412  if ($afiles[$aid]->repeat) $va = $doc->getTValue($aid, "", $index);
413  else $va = $doc->getValue($aid);
414 
415  if ($va != "") {
416  list($mime, $vid) = explode("|", $va);
417 
418  if ($vid != "") {
419  if ($vf->Retrieve($vid, $info) == "") {
420 
421  $cidindex = $vaf;
422  if (($mixed) && ($afiles[$aid]->type != "image")) $cidindex = $info->name;
423  $multi_mix->addSubpart('', array(
424  'body_file' => $info->path,
425  'content_type' => $info->mime_s ? $info->mime_s : $mime,
426  'charset' => 'UTF-8',
427  'filename' => $info->name,
428  'dfilename' => $info->name,
429  'encoding' => 'base64',
430  'name_encoding' => 'quoted-printable',
431  'filename_encoding' => 'quoted-printable',
432  'disposition' => 'attachment',
433  'cid' => $cidindex
434  ));
435  }
436  }
437  }
438  }
439  }
440  }
441  }
442  // Remove <a href="cid:xxx"> links in HTML source, and regenerate attached file
443  if ($htmlPart !== null && $sgen1 != '' && $pfout != '') {
444  $htmlBody = preg_replace('|<a\s+(?:\w+="[^"]+")*\s*href="cid:[^"]+"[^>]*>([^<]*)</a>|si', '\1', $sgen1);
445  $fout = fopen($pfout, "w");
446  fwrite($fout, $htmlBody);
447  fclose($fout);
448  $htmlPart->setBodyFile($pfout);
449  }
450  // ---------------------------
451  // add icon image
452  if (preg_match("/html/", $format, $reg)) {
453  if (!$szone) {
454  $va = $doc->icon;
455  if ($va != "") {
456  list($mime, $vid) = explode("|", $va);
457 
458  if ($vid != "") {
459  if ($vf->Retrieve($vid, $info) == "") {
460  $multi_rel->addSubpart('', array(
461  'body_file' => $info->path,
462  'content_type' => $info->mime_s ? $info->mime_s : $mime,
463  'charset' => 'UTF-8',
464  'filename' => $info->name,
465  'dfilename' => $info->name,
466  'encoding' => 'base64',
467  'name_encoding' => 'quoted-printable',
468  'filename_encoding' => 'quoted-printable',
469  'disposition' => 'inline',
470  'cid' => 'icon'
471  ));
472  }
473  } else {
474  $icon = $doc->getIcon();
475  if (file_exists($pubdir . "/$icon")) {
476  $multi_rel->addSubpart('', array(
477  'body_file' => sprintf("%s/%s", $pubdir, $icon) ,
478  'content_type' => sprintf("image/%s", fileextension($icon)) ,
479  'charset' => 'UTF-8',
480  'filename' => "icon",
481  'dfilename' => "icon",
482  'encoding' => 'base64',
483  'name_encoding' => 'quoted-printable',
484  'filename_encoding' => 'quoted-printable',
485  'disposition' => 'inline',
486  'cid' => 'icon'
487  ));
488  }
489  }
490  }
491  }
492  }
493  // ---------------------------
494  // add inserted image
495  foreach ($ifiles as $k => $v) {
496  if (file_exists($pubdir . "/$v")) {
497 
498  $multi_rel->addSubpart('', array(
499  'body_file' => sprintf("%s/%s", $pubdir, $v) ,
500  'content_type' => sprintf("image/%s", fileextension($v)) ,
501  'charset' => 'UTF-8',
502  'filename' => basename($v) ,
503  'dfilename' => basename($v) ,
504  'encoding' => 'base64',
505  'name_encoding' => 'quoted-printable',
506  'filename_encoding' => 'quoted-printable',
507  'disposition' => 'inline',
508  'cid' => $v
509  ));
510  }
511  }
512 
513  foreach ($tfiles as $k => $v) {
514  if (file_exists($v)) {
515 
516  $multi_rel->addSubpart('', array(
517  'body_file' => $v,
518  'content_type' => trim(shell_exec(sprintf("file --mime -b %s", escapeshellarg($v)))) ,
519  'charset' => 'UTF-8',
520  'filename' => $k,
521  'dfilename' => $k,
522  'encoding' => 'base64',
523  'name_encoding' => 'quoted-printable',
524  'filename_encoding' => 'quoted-printable',
525  'disposition' => 'inline',
526  'cid' => $k
527  ));
528  }
529  }
530  // Other files,
531  if (count($addfiles) > 0) {
532  foreach ($addfiles as $kf => $vf) {
533  if (count($vf) == 3) {
534  $fview = $vf[0];
535  $fname = $vf[1];
536  $fmime = $vf[2];
537 
538  $fgen = $doc->viewDoc($fview, "mail");
539  $fpname = getTmpDir() . "/" . str_replace(array(
540  " ",
541  "/",
542  "(",
543  ")"
544  ) , "_", uniqid($doc->id) . $fname);
545  if ($fp = fopen($fpname, 'w')) {
546  fwrite($fp, $fgen);
547  fclose($fp);
548  }
549  $fpst = stat($fpname);
550  if (is_array($fpst) && $fpst["size"] > 0) {
551 
552  $multi_mix->addSubpart('', array(
553  'body_file' => $fpname,
554  'content_type' => $fmime,
555  'charset' => 'UTF-8',
556  'filename' => $fname,
557  'dfilename' => $fname,
558  'encoding' => 'base64',
559  'name_encoding' => 'quoted-printable',
560  'filename_encoding' => 'quoted-printable',
561  'disposition' => 'attachment',
562  'cid' => '$fname'
563  ));
564  }
565  }
566  }
567  }
568  }
569  if (preg_match("/pdf/", $format, $reg)) {
570  // try PDF
571  $fps = uniqid(getTmpDir() . "/" . $doc->id) . "ps";
572  $fpdf = uniqid(getTmpDir() . "/" . $doc->id) . "pdf";
573  $cmdpdf = sprintf("recode u8..l9 %s && html2ps -U -i 0.5 -b %s/ %s > %s && ps2pdf %s %s", escapeshellarg($ppdf) , // recode
574  escapeshellarg($pubdir) , escapeshellarg($ppdf) , escapeshellarg($fps) , // html2ps
575  escapeshellarg($fps) , escapeshellarg($fpdf) // ps2pdf
576  );
577  system(($cmdpdf) , $status);
578  if ($status == 0) {
579  $multi_mix->addSubpart('', array(
580  'body_file' => $fpdf,
581  'content_type' => 'application/pdf',
582  'charset' => 'UTF-8',
583  'filename' => sprintf("%s.pdf", $doc->title) ,
584  'dfilename' => sprintf("%s.pdf", $doc->title) ,
585  'encoding' => 'base64',
586  'name_encoding' => 'quoted-printable',
587  'filename_encoding' => 'quoted-printable',
588  'disposition' => 'attachment'
589  ));
590  } else {
591  $action->addlogmsg(sprintf(_("PDF conversion failed for %s") , $doc->title));
592  }
593  }
594 
595  $err = sendmail($to, $from, $cc, $bcc, $subject, $multi_mix);
596  if ($err == "") {
597  if ($savecopy) createSentMessage($to, $from, $cc, $bcc, $subject, $multi_mix, $doc);
598  if ($cc != "") $lsend = sprintf("%s and %s", $to, $cc);
599  else $lsend = $to;
600  $doc->addcomment(sprintf(_("sended to %s") , $lsend));
601  $action->addlogmsg(sprintf(_("sending %s to %s") , $doc->title, $lsend));
602  if ($userinfo) $action->addwarningmsg(sprintf(_("sending %s to %s") , $doc->title, $lsend));
603  } else {
604  $action->log->warning($err);
605  $action->addlogmsg(sprintf(_("%s cannot be sent") , $doc->title));
606  if ($userinfo) $action->addwarningmsg(sprintf(_("%s cannot be sent") , $doc->title));
607  if ($userinfo) $action->addwarningmsg($err);
608  }
609  // suppress temporaries files
610  if (isset($ftxt) && is_file($ftxt)) unlink($ftxt);
611  if (isset($fpdf) && is_file($fpdf)) unlink($fpdf);
612  if (isset($fps) && is_file($fps)) unlink($fps);
613  if (isset($pfout) && is_file($pfout)) unlink($pfout);
614  if (isset($ppdf) && is_file($ppdf)) unlink($ppdf);
615  if (isset($binfile) && is_file($binfile)) unlink($binfile);
616 
617  $tmpfile = array_merge($tmpfile, $tfiles);
618  foreach ($tmpfile as $k => $v) {
619  if (file_exists($v) && (substr($v, 0, 9) == getTmpDir() . "/")) unlink($v);
620  }
621  }
622 
623  return $err;
624 }
625 
626 function srcfile($src)
627 {
628  global $ifiles;
629  $vext = array(
630  "gif",
631  "png",
632  "jpg",
633  "jpeg",
634  "bmp"
635  );
636  if (substr($src, 0, 3) == "cid") return "src=\"$src\"";
637  if (substr($src, 0, 4) == "http") {
638  $chopped_src = '';
639  // Detect HTTP URLs pointing to myself
640  foreach (array(
641  'CORE_URLINDEX',
642  'CORE_PUBURL'
643  ) as $url) {
644  $url = getParam($url);
645  if (strlen($url) <= 0) {
646  continue;
647  }
648  if (strcmp(substr($src, 0, strlen($url)) , $url) == 0) {
649  // Chop the URL base part, and leave only the args/vars
650  $chopped_src = substr($src, strlen($url));
651  break;
652  }
653  }
654  if ($chopped_src == '') {
655  return sprintf('src="%s"', $src);
656  }
657  $src = $chopped_src;
658  }
659 
660  if (preg_match("/(.*)(app=FDL.*action=EXPORTFILE.*)$/", $src, $reg)) {
661  return imgvaultfile(str_replace('&amp;', '&', $reg[2]));
662  }
663 
664  if (!in_array(strtolower(fileextension($src)) , $vext)) return "";
665 
666  $ifiles[$src] = $src;
667  return "src=\"cid:$src\"";
668 }
669 function imgvaultfile($src)
670 {
671  global $tfiles;
672  $newfile = copyvault($src);
673  if ($newfile) {
674  $src = "img" . count($tfiles);
675  $tfiles[$src] = $newfile;
676  return "src=\"cid:$src\" ";
677  }
678  return "";
679 }
680 function copyvault($src)
681 {
682  global $action;
683 
684  include_once ('FDL/Lib.Vault.php');
685 
686  if (preg_match("/(.*)(app=FDL.*action=EXPORTFILE.*)docid=([^&]*)&/", $src, $reg)) {
687  $url = $action->getParam("CORE_OPENURL", $action->getParam("CORE_EXTERNURL"));
688  if (strstr($url, '?')) $url.= '&';
689  else $url.= '?';
690  $token = $action->user->getUserToken(3600, true, array(
691  "app" => "FDL",
692  "action" => "EXPORTFILE",
693  "docid" => $reg[3]
694  ));
695  $url.= "authtype=open&privateid=$token&";
696  $url.= $src;
697  $newfile = uniqid(getTmpDir() . "/img");
698  if (!copy($url, $newfile)) {
699  return "";
700  }
701  return $newfile;
702  }
703  if (preg_match("|^FDL/geticon\.php\?vaultid=(?P<vid>\d+)|", $src, $reg)) {
704  $info = vault_properties($reg['vid']);
705  $newfile = uniqid(getTmpDir() . "/img");
706  if (!copy($info->path, $newfile)) {
707  return "";
708  }
709  return $newfile;
710  }
711 
712  return "";
713 }
714 
715 function realfile($src)
716 {
717  global $vf;
718  global $doc;
719  global $pubdir;
720  global $tmpfile;
721  $f = false;
722  if ($src == "cid:icon") {
723  $va = $doc->icon;
724  } else {
725  if (substr($src, 0, 4) == "cid:") $va = $doc->getValue(substr($src, 4));
726  elseif (preg_match("/(.*)(app=FDL.*action=EXPORTFILE.*)$/", $src, $reg)) {
727  $va = copyvault(str_replace('&amp;', '&', $reg[2]));
728  $tmpfile[] = $va;
729  } else $va = $src;
730  }
731 
732  if ($va != "") {
733  list($mime, $vid) = explode("|", $va);
734 
735  if ($vid != "") {
736  if ($vf->Retrieve($vid, $info) == "") {
737  $f = $info->path;
738  }
739  } else {
740 
741  if (file_exists($pubdir . "/$va")) $f = $pubdir . "/$va";
742  elseif (file_exists($pubdir . "/Images/$va")) $f = $pubdir . "/Images/$va";
743  elseif ((substr($va, 0, 12) == getTmpDir() . '/img') && file_exists($va)) $f = $va;
744  }
745  }
746  // $mime=trim(`file -ib "$f"`);
747  // print "<br>[$mime][$f][$va]";
748  // if (substr($mime,0,5) != "image") $f="";
749  if ($f) return "src=\"$f\"";
750  return "";
751 }
752 ?>
← centre documentaire © anakeen - published under CC License - Dynacase