]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/UnkMail.php
spelling: parameter
[friendica.git] / src / Module / Profile / UnkMail.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Profile;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Session\Capability\IHandleUserSessions;
28 use Friendica\Database\Database;
29 use Friendica\Model\Mail;
30 use Friendica\Model\User;
31 use Friendica\Module\Response;
32 use Friendica\Navigation\SystemMessages;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Strings;
36 use Psr\Log\LoggerInterface;
37
38 /**
39  * Unknown Mail module
40  */
41 class UnkMail extends \Friendica\BaseModule
42 {
43         /** @var IHandleUserSessions */
44         private $userSessions;
45
46         /** @var SystemMessages */
47         private $systemMessages;
48
49         /** @var Database */
50         private $database;
51
52         /** @var App\Page */
53         private $page;
54
55         public function __construct(App\Page $page, Database $database, SystemMessages $systemMessages, IHandleUserSessions $userSessions, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
56         {
57                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
58
59                 $this->userSessions   = $userSessions;
60                 $this->systemMessages = $systemMessages;
61                 $this->database       = $database;
62                 $this->page           = $page;
63         }
64
65         protected function post(array $request = [])
66         {
67                 $replyto = $this->userSessions->getMyUrl();
68                 if (!$replyto) {
69                         $this->systemMessages->addNotice($this->l10n->t('Permission denied.'));
70                         return;
71                 }
72
73                 $recipient = $this->parameters['nickname'];
74                 $subject   = trim($request['subject'] ?? '');
75                 $body      = Strings::escapeHtml(trim($request['body'] ?? ''));
76
77                 if (!$body) {
78                         $this->systemMessages->addNotice($this->l10n->t('Empty message body.'));
79                         return;
80                 }
81
82                 $user = User::getByNickname($recipient);
83                 if (empty($user)) {
84                         return;
85                 }
86
87                 if (!$user['unkmail']) {
88                         return;
89                 }
90
91                 $total = $this->database->count('mail', ["`uid` = ? AND `created` > ? AND `unknown`", $user['uid'], DateTimeFormat::utc('now - 1 day')]);
92                 if ($total > $user['cntunkmail']) {
93                         return;
94                 }
95
96                 $ret = Mail::sendWall($user, $body, $subject, $replyto);
97
98                 switch ($ret) {
99                         case -1:
100                                 $this->systemMessages->addNotice($this->l10n->t('No recipient selected.'));
101                                 break;
102                         case -2:
103                                 $this->systemMessages->addNotice($this->l10n->t('Unable to check your home location.'));
104                                 break;
105                         case -3:
106                                 $this->systemMessages->addNotice($this->l10n->t('Message could not be sent.'));
107                                 break;
108                         case -4:
109                                 $this->systemMessages->addNotice($this->l10n->t('Message collection failure.'));
110                                 break;
111                 }
112
113                 $this->baseUrl->redirect('profile/' . $user['nickname']);
114         }
115
116         protected function content(array $request = []): string
117         {
118                 $returnUrl = 'profile/' . $this->parameters['nickname'];
119
120                 if (!$this->userSessions->getMyUrl()) {
121                         $this->systemMessages->addNotice($this->l10n->t('Permission denied.'));
122                         $this->baseUrl->redirect($returnUrl);
123                 }
124
125                 $user = User::getByNickname($this->parameters['nickname']);
126                 if (empty($user)) {
127                         $this->systemMessages->addNotice($this->l10n->t('Recipient not found.'));
128                         $this->baseUrl->redirect($returnUrl);
129                 }
130
131                 if (!$user['unkmail']) {
132                         $this->systemMessages->addNotice($this->l10n->t('Permission denied.'));
133                         $this->baseUrl->redirect($returnUrl);
134                 }
135
136                 $total = $this->database->count('mail', ["`uid` = ? AND `created` > ? AND `unknown`", $user['uid'], DateTimeFormat::utc('now - 1 day')]);
137                 if ($total > $user['cntunkmail']) {
138                         $this->systemMessages->addNotice($this->l10n->t('Number of daily wall messages for %s exceeded. Message failed.', $user['username']));
139                         $this->baseUrl->redirect($returnUrl);
140                 }
141
142                 $tpl = Renderer::getMarkupTemplate('profile/unkmail-header.tpl');
143                 $this->page['htmlhead'] .= Renderer::replaceMacros($tpl, [
144                         '$nickname' => $user['nickname'],
145                         '$linkurl'  => $this->l10n->t('Please enter a link URL:')
146                 ]);
147
148                 $tpl = Renderer::getMarkupTemplate('profile/unkmail.tpl');
149                 return Renderer::replaceMacros($tpl, [
150                         '$l10n' => [
151                                 'header'    => $this->l10n->t('Send Private Message'),
152                                 'subheader' => $this->l10n->t('If you wish for %s to respond, please check that the privacy settings on your site allow private mail from unknown senders.', $user['username']),
153                                 'insert'    => $this->l10n->t('Insert web link'),
154                                 'wait'      => $this->l10n->t('Please wait'),
155                                 'submit'    => $this->l10n->t('Submit'),
156                         ],
157
158                         '$nickname' => $user['nickname'],
159
160                         '$to'      => ['to'     , $this->l10n->t('To')          , $user['username'], '', '', 'disabled'],
161                         '$subject' => ['subject', $this->l10n->t('Subject')     , $request['subject'] ?? ''],
162                         '$body'    => ['body'   , $this->l10n->t('Your message'), $request['body'] ?? ''],
163                 ]);
164         }
165 }