Fixed fatal error
[friendica.git/.git] / mod / wallmessage.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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 use Friendica\App;
23 use Friendica\Core\Logger;
24 use Friendica\Core\Renderer;
25 use Friendica\Database\DBA;
26 use Friendica\DI;
27 use Friendica\Model\Mail;
28 use Friendica\Model\Profile;
29 use Friendica\Util\Strings;
30
31 function wallmessage_post(App $a) {
32
33         $replyto = Profile::getMyURL();
34         if (!$replyto) {
35                 notice(DI::l10n()->t('Permission denied.') . EOL);
36                 return;
37         }
38
39         $subject   = (!empty($_REQUEST['subject'])   ? Strings::escapeTags(trim($_REQUEST['subject']))   : '');
40         $body      = (!empty($_REQUEST['body'])      ? Strings::escapeHtml(trim($_REQUEST['body'])) : '');
41
42         $recipient = (($a->argc > 1) ? Strings::escapeTags($a->argv[1]) : '');
43         if ((! $recipient) || (! $body)) {
44                 return;
45         }
46
47         $r = q("select * from user where nickname = '%s' limit 1",
48                 DBA::escape($recipient)
49         );
50
51         if (! DBA::isResult($r)) {
52                 Logger::log('wallmessage: no recipient');
53                 return;
54         }
55
56         $user = $r[0];
57
58         if (! intval($user['unkmail'])) {
59                 notice(DI::l10n()->t('Permission denied.') . EOL);
60                 return;
61         }
62
63         $r = q("select count(*) as total from mail where uid = %d and created > UTC_TIMESTAMP() - INTERVAL 1 day and unknown = 1",
64                         intval($user['uid'])
65         );
66
67         if ($r[0]['total'] > $user['cntunkmail']) {
68                 notice(DI::l10n()->t('Number of daily wall messages for %s exceeded. Message failed.', $user['username']));
69                 return;
70         }
71
72         $ret = Mail::sendWall($user, $body, $subject, $replyto);
73
74         switch ($ret) {
75                 case -1:
76                         notice(DI::l10n()->t('No recipient selected.') . EOL);
77                         break;
78                 case -2:
79                         notice(DI::l10n()->t('Unable to check your home location.') . EOL);
80                         break;
81                 case -3:
82                         notice(DI::l10n()->t('Message could not be sent.') . EOL);
83                         break;
84                 case -4:
85                         notice(DI::l10n()->t('Message collection failure.') . EOL);
86                         break;
87                 default:
88                         info(DI::l10n()->t('Message sent.') . EOL);
89         }
90
91         DI::baseUrl()->redirect('profile/'.$user['nickname']);
92 }
93
94
95 function wallmessage_content(App $a) {
96
97         if (!Profile::getMyURL()) {
98                 notice(DI::l10n()->t('Permission denied.') . EOL);
99                 return;
100         }
101
102         $recipient = (($a->argc > 1) ? $a->argv[1] : '');
103
104         if (!$recipient) {
105                 notice(DI::l10n()->t('No recipient.') . EOL);
106                 return;
107         }
108
109         $r = q("select * from user where nickname = '%s' limit 1",
110                 DBA::escape($recipient)
111         );
112
113         if (! DBA::isResult($r)) {
114                 notice(DI::l10n()->t('No recipient.') . EOL);
115                 Logger::log('wallmessage: no recipient');
116                 return;
117         }
118
119         $user = $r[0];
120
121         if (!intval($user['unkmail'])) {
122                 notice(DI::l10n()->t('Permission denied.') . EOL);
123                 return;
124         }
125
126         $r = q("select count(*) as total from mail where uid = %d and created > UTC_TIMESTAMP() - INTERVAL 1 day and unknown = 1",
127                         intval($user['uid'])
128         );
129
130         if ($r[0]['total'] > $user['cntunkmail']) {
131                 notice(DI::l10n()->t('Number of daily wall messages for %s exceeded. Message failed.', $user['username']));
132                 return;
133         }
134
135         $tpl = Renderer::getMarkupTemplate('wallmsg-header.tpl');
136         DI::page()['htmlhead'] .= Renderer::replaceMacros($tpl, [
137                 '$baseurl' => DI::baseUrl()->get(true),
138                 '$nickname' => $user['nickname'],
139                 '$linkurl' => DI::l10n()->t('Please enter a link URL:')
140         ]);
141
142         $tpl = Renderer::getMarkupTemplate('wallmessage.tpl');
143         $o = Renderer::replaceMacros($tpl, [
144                 '$header'     => DI::l10n()->t('Send Private Message'),
145                 '$subheader'  => DI::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']),
146                 '$to'         => DI::l10n()->t('To:'),
147                 '$subject'    => DI::l10n()->t('Subject:'),
148                 '$recipname'  => $user['username'],
149                 '$nickname'   => $user['nickname'],
150                 '$subjtxt'    => $_REQUEST['subject'] ?? '',
151                 '$text'       => $_REQUEST['body'] ?? '',
152                 '$readonly'   => '',
153                 '$yourmessage'=> DI::l10n()->t('Your message:'),
154                 '$parent'     => '',
155                 '$upload'     => DI::l10n()->t('Upload photo'),
156                 '$insert'     => DI::l10n()->t('Insert web link'),
157                 '$wait'       => DI::l10n()->t('Please wait')
158         ]);
159
160         return $o;
161 }