Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Method.MailTemplate.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  * Mail template document
9  *
10  * @author Anakeen 2009
11  * @version $Id: Method.MailTemplate.php,v 1.11 2009/01/16 12:47:38 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  * @begin-method-ignore
20  * this part will be deleted when construct document class until end-method-ignore
21  */
22 class _MAILTEMPLATE extends Doc
23 {
24  /*
25  * @end-method-ignore
26  */
27  public $ifiles = array();
28  public $sendercopy = true;
29 
30  function preEdition()
31  {
32  global $action;
33 
34  if ($mailfamily = $this->getValue("tmail_family", getHttpVars("TMAIL_FAMILY"))) {
35  $action->parent->AddJsRef("?app=FDL&action=FCKDOCATTR&famid=" . $mailfamily);
36  }
37  }
38  /**
39  * send document by email using this template
40  * @return string error - empty if no error -
41  */
42  function sendDocument(&$doc, $keys = array())
43  {
44  global $action;
45 
46  include_once ("FDL/sendmail.php");
47  include_once ("FDL/Lib.Vault.php");
48  if ($doc->isAffected()) {
49  $this->keys = $keys;
50 
51  $multi_mix = new Fdl_Mail_mimePart('', array(
52  'content_type' => 'multipart/mixed'
53  ));
54  $multi_rel = $multi_mix->addSubpart('', array(
55  'content_type' => 'multipart/related'
56  ));
57 
58  $tdest = $this->getAValues("tmail_dest");
59 
60  $dest = array(
61  "to" => array() ,
62  "cc" => array() ,
63  "bcc" => array() ,
64  "from" => array()
65  );
66  $from = trim($this->getValue("tmail_from"));
67  if ($from) {
68  $tdest[] = array(
69  "tmail_copymode" => "from",
70  "tmail_desttype" => $this->getValue("tmail_fromtype") ,
71  "tmail_recip" => $from
72  );
73  }
74 
75  if ($doc->wid) {
76  $wdoc = new_doc($this->dbaccess, $doc->wid);
77  }
78 
79  foreach ($tdest as $k => $v) {
80  $toccbcc = $v["tmail_copymode"];
81  $type = $v["tmail_desttype"];
82  $mail = '';
83  switch ($type) {
84  case 'F': // fixed address
85  $mail = $v["tmail_recip"];
86  break;
87 
88  case 'A': // text attribute
89  $aid = strtok($v["tmail_recip"], " ");
90  $mail = $doc->getRValue($aid);
91  break;
92 
93  case 'WA': // workflow text attribute
94  if ($wdoc) {
95  $aid = strtok($v["tmail_recip"], " ");
96  $mail = $wdoc->getRValue($aid);
97  }
98  break;
99 
100  case 'E': // text parameter
101  $aid = strtok($v["tmail_recip"], " ");
102  $mail = $doc->getparamValue($aid);
103  break;
104 
105  case 'WE': // workflow text parameter
106  if ($wdoc) {
107  $aid = strtok($v["tmail_recip"], " ");
108  $mail = $wdoc->getparamValue($aid);
109  }
110  break;
111 
112  case 'D': // user relations
113 
114  case 'WD': // user relations
115  if ($type == 'D') $udoc = & $doc;
116  elseif ($wdoc) $udoc = & $wdoc;
117  if ($udoc) {
118  $aid = strtok($v["tmail_recip"], " ");
119  $vdocid = $udoc->getValue($aid); // for array of users
120  $vdocid = str_replace('<BR>', "\n", $vdocid);
121  if (strpos($vdocid, "\n")) {
122  $tvdoc = $this->_val2array($vdocid);
123  $tmail = array();
124  foreach ($tvdoc as $docid) {
125  $umail = $udoc->getDocValue($docid, 'us_mail', '');
126  if (!$umail) $umail = $udoc->getDocValue($docid, 'grp_mail', '');
127  if ($umail) $tmail[] = $umail;
128  }
129  $mail = implode(",", $tmail);
130  } else {
131  if (strpos($aid, ':')) $mail = $udoc->getRValue($aid);
132  else {
133  $mail = $udoc->getRValue($aid . ':us_mail');
134  if (!$mail) $mail = $udoc->getRValue($aid . ':grp_mail');
135  }
136  }
137  }
138  break;
139 
140  case 'P':
141  $aid = strtok($v["tmail_recip"], " ");
142  $mail = getParam($aid);
143  break;
144  }
145  if ($mail) $dest[$toccbcc][] = str_replace(array(
146  "\n",
147  "\r"
148  ) , array(
149  ",",
150  ""
151  ) , $mail);
152  }
153 
154  $subject = $this->generateMailInstance($doc, $this->getValue("tmail_subject"));
155  $subject = str_replace(array(
156  "\n",
157  "\r",
158  "<BR>"
159  ) , array(
160  " ",
161  " ",
162  ", "
163  ) , html_entity_decode($subject, ENT_COMPAT, "UTF-8"));
164  $pfout = $this->generateMailInstance($doc, $this->getValue("tmail_body"));
165 
166  if ($this->sendercopy && getParam("FDL_BCC") == "yes") {
167  $umail = getMailAddr($this->userid);
168  if ($umail != "") $dest['bcc'][] = $umail;
169  }
170 
171  $to = implode(',', array_filter($dest['to'], create_function('$v', 'return!preg_match("/^\s*$/", $v);')));
172  $cc = implode(',', array_filter($dest['cc'], create_function('$v', 'return!preg_match("/^\s*$/", $v);')));
173  $bcc = implode(',', array_filter($dest['bcc'], create_function('$v', 'return!preg_match("/^\s*$/", $v);')));
174  $from = trim(implode(',', array_filter($dest['from'], create_function('$v', 'return!preg_match("/^\s*$/", $v);'))));
175 
176  if ($from == "") $from = getMailAddr($action->user->id);
177  if ($from == "") $from = getParam('SMTP_FROM');
178  if ($from == "") $from = $action->user->login . '@' . $_SERVER["HTTP_HOST"];
179 
180  if (trim($to . $cc . $bcc) == "") return ""; //nobody to send data
181  $multi_rel->addSubpart($pfout, array(
182  'content_type' => 'text/html',
183  'charset' => 'UTF-8',
184  'encoding' => 'quoted-printable'
185  ));
186  // ---------------------------
187  // add inserted image
188  foreach ($this->ifiles as $k => $v) {
189  if (file_exists($pubdir . "/$v")) {
190 
191  $multi_rel->addSubpart('', array(
192  'body_file' => $v,
193  'content_type' => sprintf("image/%s", fileextension($v)) ,
194  'charset' => 'UTF-8',
195  'filename' => $k,
196  'dfilename' => $k,
197  'encoding' => 'base64',
198  'name_encoding' => 'quoted-printable',
199  'filename_encoding' => 'quoted-printable',
200  'disposition' => 'inline',
201  'cid' => $k
202  ));
203  }
204  }
205  //send attachment
206  $ta = $this->getTValue("tmail_attach");
207  foreach ($ta as $k => $v) {
208  $vf = $doc->getRValue(strtok($v, " "));
209  if ($vf) {
210  $tvf = $this->_val2array($vf);
211  foreach ($tvf as $vf) {
212  if ($vf) {
213  $fileinfo = $this->getFileInfo($vf);
214  if ($fileinfo["path"]) {
215 
216  $multi_mix->addSubpart('', array(
217  'body_file' => $fileinfo['path'],
218  'content_type' => $fileinfo['mime_s'],
219  'charset' => 'UTF-8',
220  'filename' => $fileinfo['name'],
221  'dfilename' => $fileinfo['name'],
222  'encoding' => 'base64',
223  'name_encoding' => 'quoted-printable',
224  'filename_encoding' => 'quoted-printable',
225  'disposition' => 'attachment'
226  ));
227  }
228  }
229  }
230  }
231  }
232 
233  $err = sendmail($to, $from, $cc, $bcc, $subject, $multi_mix);
234 
235  $savecopy = $this->getValue("tmail_savecopy") == "yes";
236  if (($err == "") && $savecopy) createSentMessage($to, $from, $cc, $bcc, $subject, $multi_mix, $doc);
237  $recip = "";
238  if ($to) $recip.= sprintf(_("sendmailto %s") , $to);
239  if ($cc) $recip.= ' ' . sprintf(_("sendmailcc %s") , $cc);
240  if ($bcc) $recip.= ' ' . sprintf(_("sendmailbcc %s") , $bcc);
241 
242  if ($err == "") {
243  $doc->addComment(sprintf(_("send mail %s with template %s") , $recip, $this->title));
244  addWarningMsg(sprintf(_("send mail %s") , $recip));
245  } else {
246  $doc->addComment(sprintf(_("cannot send mail %s with template %s : %s") , $recip, $this->title, $err) , HISTO_ERROR);
247  addWarningMsg(sprintf(_("cannot send mail %s") , $err));
248  }
249  return $err;
250  }
251  }
252 
253  function generateMailInstance(&$doc, $tpl)
254  {
255  global $action;
256  $tpl = str_replace("&#x5B;", "[", $tpl); // replace [ convverted in Doc::setValue()
257  $doc->lay = new Layout("", $action, $tpl);
258 
259  $ulink = ($this->getValue("tmail_ulink") == "yes");
260  $doc->viewdefaultcard("mail", $ulink, false, true);
261  foreach ($this->keys as $k => $v) $doc->lay->set($k, $v);
262  $body = $doc->lay->gen();
263  $body = preg_replace(array(
264  "/SRC=\"([^\"]+)\"/e",
265  "/src=\"([^\"]+)\"/e"
266  ) , "\$this->srcfile('\\1')", $body);
267  return $body;
268  }
269 
270  function specRefresh2()
271  {
272  $err = $this->senddocument(new_doc($this->dbaccess, 24470, true));
273  return $err;
274  }
275 
276  function srcfile($src)
277  {
278  $vext = array(
279  "gif",
280  "png",
281  "jpg",
282  "jpeg",
283  "bmp"
284  );
285 
286  if (substr($src, 0, 3) == "cid") return "src=\"$src\"";
287  if (substr($src, 0, 4) == "http") {
288  $chopped_src = '';
289  // Detect HTTP URLs pointing to myself
290  foreach (array(
291  'CORE_URLINDEX',
292  'CORE_PUBURL'
293  ) as $url) {
294  $url = $this->getParam($url);
295  if (strlen($url) <= 0) {
296  continue;
297  }
298  if (strcmp(substr($src, 0, strlen($url)) , $url) == 0) {
299  // Chop the URL base part, and leave only the args/vars
300  $chopped_src = substr($src, strlen($url));
301  break;
302  }
303  }
304  if ($chopped_src == '') {
305  return sprintf('src="%s"', $src);
306  }
307  $src = $chopped_src;
308  }
309  $cid = $src;
310 
311  if (preg_match("/.*app=FDL.*action=EXPORTFILE.*vid=([0-9]*)/", $src, $reg)) {
312  $info = vault_properties($reg[1]);
313  $src = $info->path;
314  $cid = "cid" . $reg[1] . '.' . fileextension($info->path);
315  }
316 
317  if (!in_array(strtolower(fileextension($src)) , $vext)) return "";
318 
319  $this->ifiles[$cid] = $src;
320  return "src=\"cid:$cid\"";
321  }
322  /**
323  * @begin-method-ignore
324  * this part will be deleted when construct document class until end-method-ignore
325  */
326  }
327  /*
328  * @end-method-ignore
329  */
330 ?>
← centre documentaire © anakeen - published under CC License - Dynacase