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