3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module\Profile;
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;
41 class UnkMail extends \Friendica\BaseModule
43 /** @var IHandleUserSessions */
44 private $userSessions;
46 /** @var SystemMessages */
47 private $systemMessages;
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 = [])
57 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
59 $this->userSessions = $userSessions;
60 $this->systemMessages = $systemMessages;
61 $this->database = $database;
65 protected function post(array $request = [])
67 $replyto = $this->userSessions->getMyUrl();
69 $this->systemMessages->addNotice($this->l10n->t('Permission denied.'));
73 $recipient = $this->parameters['nickname'];
74 $subject = trim($request['subject'] ?? '');
75 $body = Strings::escapeHtml(trim($request['body'] ?? ''));
78 $this->systemMessages->addNotice($this->l10n->t('Empty message body.'));
82 $user = User::getByNickname($recipient);
87 if (!$user['unkmail']) {
91 $total = $this->database->count('mail', ["`uid` = ? AND `created` > ? AND `unknown`", $user['uid'], DateTimeFormat::utc('now - 1 day')]);
92 if ($total > $user['cntunkmail']) {
96 $ret = Mail::sendWall($user, $body, $subject, $replyto);
100 $this->systemMessages->addNotice($this->l10n->t('No recipient selected.'));
103 $this->systemMessages->addNotice($this->l10n->t('Unable to check your home location.'));
106 $this->systemMessages->addNotice($this->l10n->t('Message could not be sent.'));
109 $this->systemMessages->addNotice($this->l10n->t('Message collection failure.'));
113 $this->baseUrl->redirect('profile/' . $user['nickname']);
116 protected function content(array $request = []): string
118 $returnUrl = 'profile/' . $this->parameters['nickname'];
120 if (!$this->userSessions->getMyUrl()) {
121 $this->systemMessages->addNotice($this->l10n->t('Permission denied.'));
122 $this->baseUrl->redirect($returnUrl);
125 $user = User::getByNickname($this->parameters['nickname']);
127 $this->systemMessages->addNotice($this->l10n->t('Recipient not found.'));
128 $this->baseUrl->redirect($returnUrl);
131 if (!$user['unkmail']) {
132 $this->systemMessages->addNotice($this->l10n->t('Permission denied.'));
133 $this->baseUrl->redirect($returnUrl);
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);
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:')
148 $tpl = Renderer::getMarkupTemplate('profile/unkmail.tpl');
149 return Renderer::replaceMacros($tpl, [
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'),
158 '$nickname' => $user['nickname'],
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'] ?? ''],