You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

213 lines
7.6 KiB

  1. <?php
  2. /**
  3. * Action sendemail for DokuWiki plugin bureaucracy
  4. */
  5. class helper_plugin_bureaucracy_actionmail extends helper_plugin_bureaucracy_action {
  6. protected $_mail_html = '';
  7. protected $_mail_text = '';
  8. protected $subject = '';
  9. protected $replyto = array();
  10. protected $mailtemplate = '';
  11. /**
  12. * Build a nice email from the submitted data and send it
  13. *
  14. * @param helper_plugin_bureaucracy_field[] $fields
  15. * @param string $thanks
  16. * @param array $argv
  17. * @return string thanks message
  18. * @throws Exception mailing failed
  19. */
  20. public function run($fields, $thanks, $argv) {
  21. global $ID;
  22. global $conf;
  23. $mail = new Mailer();
  24. $this->prepareNamespacetemplateReplacements();
  25. $this->prepareDateTimereplacements();
  26. $this->prepareLanguagePlaceholder();
  27. $this->prepareNoincludeReplacement();
  28. $this->prepareFieldReplacements($fields);
  29. $evdata = [
  30. 'fields' => $fields,
  31. 'values' => &$this->values
  32. ];
  33. $event = new Doku_Event('PLUGIN_BUREAUCRACY_EMAIL_SEND', $evdata);
  34. if($event->advise_before()) {
  35. //set default subject
  36. $this->subject = sprintf($this->getLang('mailsubject'), $ID);
  37. //build html&text table, collect replyto and subject
  38. list($table_html, $table_text) = $this->processFieldsBuildTable($fields, $mail);
  39. //Body
  40. if($this->mailtemplate) {
  41. //show template
  42. $this->patterns['__tablehtml__'] = '/@TABLEHTML@/';
  43. $this->patterns['__tabletext__'] = '/@TABLETEXT@/';
  44. $this->values['__tablehtml__'] = $table_html;
  45. $this->values['__tabletext__'] = $table_text;
  46. list($this->_mail_html, $this->_mail_text) = $this->getContent();
  47. } else {
  48. //show simpel listing
  49. $this->_mail_html .= sprintf($this->getLang('mailintro')."<br><br>", dformat());
  50. $this->_mail_html .= $table_html;
  51. $this->_mail_text .= sprintf($this->getLang('mailintro')."\n\n", dformat());
  52. $this->_mail_text .= $table_text;
  53. }
  54. $mail->setBody($this->_mail_text,null,null,$this->_mail_html);
  55. // Reply-to
  56. if(!empty($this->replyto)) {
  57. $replyto = $mail->cleanAddress($this->replyto);
  58. $mail->setHeader('Reply-To', $replyto, false);
  59. }
  60. // To
  61. $to = $this->replace(implode(',',$argv)); // get recipient address(es)
  62. $to = $mail->cleanAddress($to);
  63. $mail->to($to);
  64. // From
  65. $mail->from($conf['mailfrom']);
  66. // Subject
  67. $this->subject = $this->replace($this->subject);
  68. $mail->subject($this->subject);
  69. if(!$mail->send()) {
  70. throw new Exception($this->getLang('e_mail'));
  71. }
  72. }
  73. $event->advise_after();
  74. return '<p>' . $thanks . '</p>';
  75. }
  76. /**
  77. * Create html and plain table of the field
  78. * and collect values for subject and replyto
  79. *
  80. * @param helper_plugin_bureaucracy_field[] $fields
  81. * @param Mailer $mail
  82. * @return array of html and text table
  83. */
  84. protected function processFieldsBuildTable($fields, $mail) {
  85. global $ID;
  86. $table_html = '<table>';
  87. $table_text = '';
  88. foreach($fields as $field) {
  89. $html = $text = '';
  90. $value = $field->getParam('value');
  91. $label = $field->getParam('label');
  92. switch($field->getFieldType()) {
  93. case 'fieldset':
  94. if(!empty($field->depends_on)) {
  95. //print fieldset only if depend condition is true
  96. foreach($fields as $field_tmp) {
  97. if($field_tmp->getParam('label') === $field->depends_on[0] && $field_tmp->getParam('value') === $field->depends_on[1] ) {
  98. list($html, $text) = $this->mail_buildRow($label);
  99. }
  100. }
  101. } else {
  102. list($html, $text) = $this->mail_buildRow($label);
  103. }
  104. break;
  105. case 'file':
  106. if($value === null || $label === null) break; //print attachment only if field was visible
  107. $file = $field->getParam('file');
  108. if(!$file['size']) {
  109. $message = $this->getLang('attachmentMailEmpty');
  110. } else if($file['size'] > $this->getConf('maxEmailAttachmentSize')) {
  111. $message = $file['name'] . ' ' . $this->getLang('attachmentMailToLarge');
  112. msg(sprintf($this->getLang('attachmentMailToLarge_userinfo'), hsc($file['name']), filesize_h($this->getConf('maxEmailAttachmentSize'))), 2);
  113. } else {
  114. $message = $file['name'];
  115. $mail->attachFile($file['tmp_name'], $file['type'], $file['name']);
  116. }
  117. list($html, $text) = $this->mail_buildRow($label, $message);
  118. break;
  119. case 'subject':
  120. $this->subject = $label;
  121. break;
  122. case 'usemailtemplate':
  123. if (!is_null($field->getParam('template')) ) {
  124. $this->mailtemplate = $this->replace($field->getParam('template'));
  125. resolve_pageid(getNS($ID), $this->mailtemplate, $ignored);
  126. }
  127. break;
  128. default:
  129. if($value === null || $label === null) break;
  130. if(is_array($value)) $value = implode(', ', $value);
  131. list($html, $text) = $this->mail_buildRow($label, $value);
  132. if(!is_null($field->getParam('replyto'))) {
  133. $this->replyto[] = $value;
  134. }
  135. }
  136. $table_html .= $html;
  137. $table_text .= $text;
  138. }
  139. $table_html .= '</table>';
  140. return array($table_html, $table_text);
  141. }
  142. /**
  143. * Build a row
  144. *
  145. * @param $column1
  146. * @param null $column2
  147. * @return array of html and text row
  148. */
  149. protected function mail_buildRow($column1,$column2=null) {
  150. if($column2 === null) {
  151. $html = '<tr><td colspan="2"><u>'.hsc($column1).'<u></td></tr>';
  152. $text = "\n=====".$column1.'=====';
  153. } else {
  154. $html = '<tr><td><b>'.hsc($column1).'<b></td><td>'.hsc($column2).'</td></tr>';
  155. $text = "\n $column1 \t\t $column2";
  156. }
  157. return array($html, $text);
  158. }
  159. /**
  160. * Parse mail template in html and text, and perform replacements
  161. *
  162. * @return array html and text content
  163. */
  164. protected function getContent() {
  165. $content = rawWiki($this->mailtemplate);
  166. $html = '';
  167. $text = '';
  168. if(preg_match_all('#<code\b(.*?)>(.*?)</code>#is', $content, $matches)) {
  169. foreach($matches[1] as $index => $codeoptions) {
  170. list($syntax,) = explode(' ', trim($codeoptions), 2);
  171. if($syntax == 'html') {
  172. $html = $matches[2][$index];
  173. }
  174. if($syntax == 'text' || $syntax == '') {
  175. $text = $matches[2][$index];
  176. }
  177. }
  178. }
  179. return array(
  180. $this->replace($html),
  181. $this->replace($text)
  182. );
  183. }
  184. }
  185. // vim:ts=4:sw=4:et:enc=utf-8: