]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/NotifyMailBuilder.php
Fix content not allowed flag ...
[friendica.git] / src / Util / EMailer / NotifyMailBuilder.php
1 <?php
2
3 namespace Friendica\Util\EMailer;
4
5 use Exception;
6 use Friendica\App\BaseURL;
7 use Friendica\Content\Text\BBCode;
8 use Friendica\Core\Config\IConfig;
9 use Friendica\Core\L10n;
10 use Friendica\Core\Renderer;
11 use Friendica\Network\HTTPException\InternalServerErrorException;
12 use Psr\Log\LoggerInterface;
13
14 /**
15  * Builder for notification emails (notification, source, links, ...)
16  */
17 class NotifyMailBuilder extends MailBuilder
18 {
19         /** @var string */
20         protected $subject;
21         /** @var string */
22         protected $preamble;
23         /** @var string */
24         protected $body;
25
26         /** @var string */
27         protected $siteAdmin;
28
29         /** @var bool */
30         private $contentAllowed = false;
31         /** @var string */
32         private $title = '';
33         /** @var array Details to print a photo:
34          * - image
35          * - link
36          * - name
37          */
38         private $photo = [
39                 'image' => null,
40                 'link'  => null,
41                 'name'  => null,
42         ];
43         /** @var array HTML/Plain version of the Site Link:
44          * - html
45          * - text
46          */
47         private $siteLink = [
48                 'html' => '',
49                 'text' => '',
50         ];
51         /** @var string The item link */
52         private $itemLink = '';
53
54         public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config, LoggerInterface $logger, string $siteEmailAddress, string $siteName)
55         {
56                 parent::__construct($l10n, $baseUrl, $config, $logger);
57
58                 if ($this->config->get('config', 'admin_name')) {
59                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
60                 } else {
61                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
62                 }
63
64                 // Set the system wide site address/name as sender (default for system mails)
65                 $this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
66
67                 // check whether sending post content in email notifications is allowed
68                 $this->contentAllowed = !$this->config->get('system', 'enotify_no_content', false);
69         }
70
71         /**
72          * Adds a notification (in fact a more detailed message)
73          *
74          * @param string      $subject
75          * @param string      $preamble
76          * @param string      $title
77          * @param string|null $body
78          *
79          * @return static
80          */
81         public function withNotification(string $subject, string $preamble, string $title, string $body = null)
82         {
83                 if (!isset($body)) {
84                         $body = $preamble;
85                 }
86
87                 $this->title = stripslashes($title);
88                 $this->subject  = $subject;
89                 $this->preamble = $preamble;
90                 $this->body     = $body;
91
92                 return $this;
93         }
94
95         /**
96          * Adds a photo of the source of the notify
97          *
98          * @param string $image The image link to the photo
99          * @param string $link  The link to the source
100          * @param string $name  The name of the source
101          *
102          * @return static
103          */
104         public function withPhoto(string $image, string $link, string $name)
105         {
106                 $this->photo = [
107                         'image' => $image ?? '',
108                         'link'  => $link ?? '',
109                         'name'  => $name ?? '',
110                 ];
111
112                 return $this;
113         }
114
115         /**
116          * Adds a sitelink to the notification
117          *
118          * @param string $text The text version of the site link
119          * @param string $html The html version of the site link
120          *
121          * @return static
122          */
123         public function withSiteLink(string $text, string $html = '')
124         {
125                 $this->siteLink = [
126                         'text' => $text,
127                         'html' => $html,
128                 ];
129
130                 return $this;
131         }
132
133         /**
134          * Adds a link to the item of the notification
135          *
136          * @param string $link The text version of the item link
137          *
138          * @return static
139          */
140         public function withItemLink(string $link)
141         {
142                 $this->itemLink = $link;
143
144                 return $this;
145         }
146
147         /**
148          * {@inheritDoc}
149          */
150         protected function getSubject()
151         {
152                 return $this->subject;
153         }
154
155         /**
156          * {@inheritDoc}
157          *
158          * @throws InternalServerErrorException
159          * @throws Exception
160          */
161         protected function getHtmlMessage()
162         {
163                 $htmlVersion = BBCode::convert($this->body);
164
165                 // load the template for private message notifications
166                 $tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
167                 return Renderer::replaceMacros($tpl, [
168                         '$preamble'        => str_replace("\n", "<br>\n", $this->preamble),
169                         '$source_name'     => $this->photo['name'],
170                         '$source_link'     => $this->photo['link'],
171                         '$source_photo'    => $this->photo['image'],
172                         '$hsitelink'       => $this->siteLink['html'],
173                         '$hitemlink'       => sprintf('<a href="%s">%s</a>', $this->itemLink, $this->itemLink),
174                         '$thanks'          => $this->l10n->t('thanks'),
175                         '$site_admin'      => $this->siteAdmin,
176                         '$title'           => $this->title,
177                         '$htmlversion'     => $htmlVersion,
178                         '$content_allowed' => $this->contentAllowed,
179                 ]);
180
181         }
182
183         /**
184          * {@inheritDoc}
185          *
186          * @throws Exception
187          */
188         protected function getPlaintextMessage()
189         {
190                 $textVersion = BBCode::toPlaintext($this->body);
191
192                 // load the template for private message notifications
193                 $tpl = Renderer::getMarkupTemplate('email/notify/text.tpl');
194                 return Renderer::replaceMacros($tpl, [
195                         '$preamble'        => $this->preamble,
196                         '$tsitelink'       => $this->siteLink['text'],
197                         '$titemlink'       => $this->itemLink,
198                         '$thanks'          => $this->l10n->t('thanks'),
199                         '$site_admin'      => $this->siteAdmin,
200                         '$title'           => $this->title,
201                         '$textversion'     => $textVersion,
202                         '$content_allowed' => $this->contentAllowed,
203                 ]);
204         }
205 }