diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 2129e92bd..2496199bd 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -349,7 +349,7 @@ class Profile if ($visitor_is_followed || $visitor_is_following) { $wallmessage_link = $visitor_base_path . '/message/new/' . $profile_contact['id']; } elseif ($visitor_is_authenticated && !empty($profile['unkmail'])) { - $wallmessage_link = 'wallmessage/' . $profile['nickname']; + $wallmessage_link = 'profile/' . $profile['nickname'] . '/unkmail'; } } } diff --git a/src/Module/Profile/UnkMail.php b/src/Module/Profile/UnkMail.php new file mode 100644 index 000000000..32d92bb9e --- /dev/null +++ b/src/Module/Profile/UnkMail.php @@ -0,0 +1,166 @@ +. + * + */ + +namespace Friendica\Module\Profile; + +use Friendica\App; +use Friendica\Core\L10n; +use Friendica\Core\Renderer; +use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Database\Database; +use Friendica\Model\Mail; +use Friendica\Model\User; +use Friendica\Module\Response; +use Friendica\Navigation\SystemMessages; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Profiler; +use Friendica\Util\Strings; +use Psr\Log\LoggerInterface; + +/** + * Unknown Mail module + */ +class UnkMail extends \Friendica\BaseModule +{ + /** @var IHandleUserSessions */ + private $userSessions; + + /** @var SystemMessages */ + private $systemMessages; + + /** @var Database */ + private $database; + + /** @var App\Page */ + private $page; + + 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 = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->userSessions = $userSessions; + $this->systemMessages = $systemMessages; + $this->database = $database; + $this->page = $page; + } + + protected function post(array $request = []) + { + $replyto = $this->userSessions->getMyUrl(); + if (!$replyto) { + $this->systemMessages->addNotice($this->l10n->t('Permission denied.')); + return; + } + + $recipient = $this->parameters['nickname']; + $subject = trim($request['subject'] ?? ''); + $body = Strings::escapeHtml(trim($request['body'] ?? '')); + + if (!$body) { + $this->systemMessages->addNotice($this->l10n->t('Empty message body.')); + return; + } + + $user = User::getByNickname($recipient); + if (empty($user)) { + return; + } + + if (!$user['unkmail']) { + return; + } + + $total = $this->database->count('mail', ["`uid` = ? AND `created` > ? AND `unknown`", $user['uid'], DateTimeFormat::utc('now - 1 day')]); + if ($total > $user['cntunkmail']) { + return; + } + + $ret = Mail::sendWall($user, $body, $subject, $replyto); + + switch ($ret) { + case -1: + $this->systemMessages->addNotice($this->l10n->t('No recipient selected.')); + break; + case -2: + $this->systemMessages->addNotice($this->l10n->t('Unable to check your home location.')); + break; + case -3: + $this->systemMessages->addNotice($this->l10n->t('Message could not be sent.')); + break; + case -4: + $this->systemMessages->addNotice($this->l10n->t('Message collection failure.')); + break; + } + + $this->baseUrl->redirect('profile/' . $user['nickname']); + } + + protected function content(array $request = []): string + { + $returnUrl = 'profile/' . $this->parameters['nickname']; + + if (!$this->userSessions->getMyUrl()) { + $this->systemMessages->addNotice($this->l10n->t('Permission denied.')); + $this->baseUrl->redirect($returnUrl); + } + + $user = User::getByNickname($this->parameters['nickname']); + if (empty($user)) { + $this->systemMessages->addNotice($this->l10n->t('Recipient not found.')); + $this->baseUrl->redirect($returnUrl); + } + + if (!$user['unkmail']) { + $this->systemMessages->addNotice($this->l10n->t('Permission denied.')); + $this->baseUrl->redirect($returnUrl); + } + + $total = $this->database->count('mail', ["`uid` = ? AND `created` > ? AND `unknown`", $user['uid'], DateTimeFormat::utc('now - 1 day')]); + if ($total > $user['cntunkmail']) { + $this->systemMessages->addNotice($this->l10n->t('Number of daily wall messages for %s exceeded. Message failed.', $user['username'])); + $this->baseUrl->redirect($returnUrl); + } + + $tpl = Renderer::getMarkupTemplate('profile/unkmail-header.tpl'); + $this->page['htmlhead'] .= Renderer::replaceMacros($tpl, [ + '$baseurl' => $this->baseUrl->get(true), + '$nickname' => $user['nickname'], + '$linkurl' => $this->l10n->t('Please enter a link URL:') + ]); + + $tpl = Renderer::getMarkupTemplate('profile/unkmail.tpl'); + return Renderer::replaceMacros($tpl, [ + '$l10n' => [ + 'header' => $this->l10n->t('Send Private Message'), + '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']), + 'insert' => $this->l10n->t('Insert web link'), + 'wait' => $this->l10n->t('Please wait'), + 'submit' => $this->l10n->t('Submit'), + ], + + '$nickname' => $user['nickname'], + + '$to' => ['to' , $this->l10n->t('To') , $user['username'], '', '', 'disabled'], + '$subject' => ['subject', $this->l10n->t('Subject') , $request['subject'] ?? ''], + '$body' => ['body' , $this->l10n->t('Your message'), $request['body'] ?? ''], + ]); + } +} diff --git a/static/routes.config.php b/static/routes.config.php index 47176868f..9afc52e9f 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -38,6 +38,7 @@ $profileRoutes = [ '/contacts[/{type}]' => [Module\Profile\Contacts::class, [R::GET]], '/status[/{category}[/{date1}[/{date2}]]]' => [Module\Profile\Status::class, [R::GET]], '/media' => [Module\Profile\Media::class, [R::GET]], + '/unkmail' => [Module\Profile\UnkMail::class, [R::GET, R::POST]], ]; $apiRoutes = [ diff --git a/view/templates/profile/unkmail-header.tpl b/view/templates/profile/unkmail-header.tpl new file mode 100644 index 000000000..2c7d8a9ac --- /dev/null +++ b/view/templates/profile/unkmail-header.tpl @@ -0,0 +1,15 @@ + + diff --git a/view/templates/profile/unkmail.tpl b/view/templates/profile/unkmail.tpl new file mode 100644 index 000000000..7faa155f2 --- /dev/null +++ b/view/templates/profile/unkmail.tpl @@ -0,0 +1,26 @@ +
{{$l10n.subheader}}
+