1<?php
2
3namespace dokuwiki\Subscriptions;
4
5use Mailer;
6
7abstract class SubscriptionSender
8{
9    protected $mailer;
10
11    public function __construct(Mailer $mailer = null)
12    {
13        if (!$mailer instanceof \Mailer) {
14            $mailer = new Mailer();
15        }
16        $this->mailer = $mailer;
17    }
18
19    /**
20     * Get a valid message id for a certain $id and revision (or the current revision)
21     *
22     * @param string $id  The id of the page (or media file) the message id should be for
23     * @param string $rev The revision of the page, set to the current revision of the page $id if not set
24     *
25     * @return string
26     */
27    protected function getMessageID($id, $rev = null)
28    {
29        static $listid = null;
30        if (is_null($listid)) {
31            $server = parse_url(DOKU_URL, PHP_URL_HOST);
32            $listid = implode('.', array_reverse(explode('/', DOKU_BASE))) . $server;
33            $listid = urlencode($listid);
34            $listid = strtolower(trim($listid, '.'));
35        }
36
37        if (is_null($rev)) {
38            $rev = @filemtime(wikiFN($id));
39        }
40
41        return "<$id?rev=$rev@$listid>";
42    }
43
44    /**
45     * Helper function for sending a mail
46     *
47     * @param string $subscriber_mail The target mail address
48     * @param string $subject         The lang id of the mail subject (without the
49     *                                prefix “mail_”)
50     * @param string $context         The context of this mail, eg. page or namespace id
51     * @param string $template        The name of the mail template
52     * @param array  $trep            Predefined parameters used to parse the
53     *                                template (in text format)
54     * @param array  $hrep            Predefined parameters used to parse the
55     *                                template (in HTML format), null to default to $trep
56     * @param array  $headers         Additional mail headers in the form 'name' => 'value'
57     *
58     * @return bool
59     * @author Adrian Lang <lang@cosmocode.de>
60     *
61     */
62    protected function send($subscriber_mail, $subject, $context, $template, $trep, $hrep = null, $headers = [])
63    {
64        global $lang;
65        global $conf;
66
67        $text = rawLocale($template);
68        $subject = $lang['mail_' . $subject] . ' ' . $context;
69        $mail = $this->mailer;
70        $mail->bcc($subscriber_mail);
71        $mail->subject($subject);
72        $mail->setBody($text, $trep, $hrep);
73        if (in_array($template, ['subscr_list', 'subscr_digest'])) {
74            $mail->from($conf['mailfromnobody']);
75        }
76        if (isset($trep['SUBSCRIBE'])) {
77            $mail->setHeader('List-Unsubscribe', '<' . $trep['SUBSCRIBE'] . '>', false);
78        }
79
80        foreach ($headers as $header => $value) {
81            $mail->setHeader($header, $value);
82        }
83
84        return $mail->send();
85    }
86}
87