]> git.mxchange.org Git - friendica.git/blob - src/Util/EMailer/NotifyMailBuilder.php
Merge pull request #11519 from MrPetovan/task/11511-console-domain-move
[friendica.git] / src / Util / EMailer / NotifyMailBuilder.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util\EMailer;
23
24 use Exception;
25 use Friendica\App\BaseURL;
26 use Friendica\Content\Text\BBCode;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Renderer;
30 use Friendica\Network\HTTPException\InternalServerErrorException;
31 use Psr\Log\LoggerInterface;
32
33 /**
34  * Builder for notification emails (notification, source, links, ...)
35  */
36 class NotifyMailBuilder extends MailBuilder
37 {
38         /** @var string */
39         protected $subject;
40         /** @var string */
41         protected $preamble;
42         /** @var string */
43         protected $body;
44
45         /** @var string */
46         protected $siteAdmin;
47
48         /** @var bool */
49         private $contentAllowed = false;
50         /** @var string */
51         private $title = '';
52         /** @var array Details to print a photo:
53          * - image
54          * - link
55          * - name
56          */
57         private $photo = [
58                 'image' => null,
59                 'link'  => null,
60                 'name'  => null,
61         ];
62         /** @var array HTML/Plain version of the Site Link:
63          * - html
64          * - text
65          */
66         private $siteLink = [
67                 'html' => '',
68                 'text' => '',
69         ];
70         /** @var string The item link */
71         private $itemLink = '';
72
73         public function __construct(L10n $l10n, BaseURL $baseUrl, IManageConfigValues $config, LoggerInterface $logger, string $siteEmailAddress, string $siteName)
74         {
75                 parent::__construct($l10n, $baseUrl, $config, $logger);
76
77                 if ($this->config->get('config', 'admin_name')) {
78                         $this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
79                 } else {
80                         $this->siteAdmin = $l10n->t('%s Administrator', $siteName);
81                 }
82
83                 // Set the system wide site address/name as sender (default for system mails)
84                 $this->withSender($siteName, $siteEmailAddress, $siteEmailAddress);
85
86                 // check whether sending post content in email notifications is allowed
87                 $this->contentAllowed = !$this->config->get('system', 'enotify_no_content', false);
88         }
89
90         /**
91          * Adds a notification (in fact a more detailed message)
92          *
93          * @param string      $subject
94          * @param string      $preamble
95          * @param string      $title
96          * @param string|null $body
97          *
98          * @return static
99          */
100         public function withNotification(string $subject, string $preamble, string $title, string $body = null)
101         {
102                 if (!isset($body)) {
103                         $body = $preamble;
104                 }
105
106                 $this->title = stripslashes($title);
107                 $this->subject  = $subject;
108                 $this->preamble = $preamble;
109                 $this->body     = $body;
110
111                 return $this;
112         }
113
114         /**
115          * Adds a photo of the source of the notify
116          *
117          * @param string $image The image link to the photo
118          * @param string $link  The link to the source
119          * @param string $name  The name of the source
120          *
121          * @return static
122          */
123         public function withPhoto(string $image, string $link, string $name)
124         {
125                 $this->photo = [
126                         'image' => $image ?? '',
127                         'link'  => $link ?? '',
128                         'name'  => $name ?? '',
129                 ];
130
131                 return $this;
132         }
133
134         /**
135          * Adds a sitelink to the notification
136          *
137          * @param string $text The text version of the site link
138          * @param string $html The html version of the site link
139          *
140          * @return static
141          */
142         public function withSiteLink(string $text, string $html = '')
143         {
144                 $this->siteLink = [
145                         'text' => $text,
146                         'html' => $html,
147                 ];
148
149                 return $this;
150         }
151
152         /**
153          * Adds a link to the item of the notification
154          *
155          * @param string $link The text version of the item link
156          *
157          * @return static
158          */
159         public function withItemLink(string $link)
160         {
161                 $this->itemLink = $link;
162
163                 return $this;
164         }
165
166         /**
167          * {@inheritDoc}
168          */
169         protected function getSubject()
170         {
171                 return $this->subject;
172         }
173
174         /**
175          * {@inheritDoc}
176          *
177          * @throws InternalServerErrorException
178          * @throws Exception
179          */
180         protected function getHtmlMessage()
181         {
182                 $htmlVersion = BBCode::convert($this->body);
183
184                 // load the template for private message notifications
185                 $tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
186                 return Renderer::replaceMacros($tpl, [
187                         '$preamble'        => str_replace("\n", "<br>\n", $this->preamble),
188                         '$source_name'     => $this->photo['name'],
189                         '$source_link'     => $this->photo['link'],
190                         '$source_photo'    => $this->photo['image'],
191                         '$hsitelink'       => $this->siteLink['html'],
192                         '$hitemlink'       => sprintf('<a href="%s">%s</a>', $this->itemLink, $this->itemLink),
193                         '$thanks'          => $this->l10n->t('thanks'),
194                         '$site_admin'      => $this->siteAdmin,
195                         '$title'           => $this->title,
196                         '$htmlversion'     => $htmlVersion,
197                         '$content_allowed' => $this->contentAllowed,
198                 ]);
199
200         }
201
202         /**
203          * {@inheritDoc}
204          *
205          * @throws Exception
206          */
207         protected function getPlaintextMessage()
208         {
209                 $textVersion = BBCode::toPlaintext($this->body);
210
211                 // load the template for private message notifications
212                 $tpl = Renderer::getMarkupTemplate('email/notify/text.tpl');
213                 return Renderer::replaceMacros($tpl, [
214                         '$preamble'        => $this->preamble,
215                         '$tsitelink'       => $this->siteLink['text'],
216                         '$titemlink'       => $this->itemLink,
217                         '$thanks'          => $this->l10n->t('thanks'),
218                         '$site_admin'      => $this->siteAdmin,
219                         '$title'           => $this->title,
220                         '$textversion'     => $textVersion,
221                         '$content_allowed' => $this->contentAllowed,
222                 ]);
223         }
224 }