Update copyright
[friendica.git/.git] / src / Module / FriendSuggest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Worker;
28 use Friendica\DI;
29 use Friendica\Model\Contact as ContactModel;
30 use Friendica\Network\HTTPException\ForbiddenException;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\Strings;
34 use Friendica\Worker\Delivery;
35
36 /**
37  * Suggest friends to a known contact
38  */
39 class FriendSuggest extends BaseModule
40 {
41         public static function init(array $parameters = [])
42         {
43                 if (!local_user()) {
44                         throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
45                 }
46         }
47
48         public static function post(array $parameters = [])
49         {
50                 $cid = intval($parameters['contact']);
51
52                 // We do query the "uid" as well to ensure that it is our contact
53                 if (!DI::dba()->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
54                         throw new NotFoundException(DI::l10n()->t('Contact not found.'));
55                 }
56
57                 $suggest_contact_id = intval($_POST['suggest']);
58                 if (empty($suggest_contact_id)) {
59                         return;
60                 }
61
62                 // We do query the "uid" as well to ensure that it is our contact
63                 $contact = DI::dba()->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
64                 if (empty($contact)) {
65                         notice(DI::l10n()->t('Suggested contact not found.'));
66                         return;
67                 }
68
69                 $note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
70
71                 $suggest = DI::fsuggest()->insert([
72                         'uid'     => local_user(),
73                         'cid'     => $cid,
74                         'name'    => $contact['name'],
75                         'url'     => $contact['url'],
76                         'request' => $contact['request'],
77                         'photo'   => $contact['avatar'],
78                         'note'    => $note,
79                         'created' => DateTimeFormat::utcNow()
80                 ]);
81
82                 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
83
84                 info(DI::l10n()->t('Friend suggestion sent.'));
85         }
86
87         public static function content(array $parameters = [])
88         {
89                 $cid = intval($parameters['contact']);
90
91                 $contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
92                 if (empty($contact)) {
93                         notice(DI::l10n()->t('Contact not found.'));
94                         DI::baseUrl()->redirect();
95                 }
96
97                 $contacts = ContactModel::selectToArray(['id', 'name'], [
98                         '`uid` = ? 
99                         AND `id` != ? 
100                         AND `network` = ? 
101                         AND NOT `self` 
102                         AND NOT `blocked` 
103                         AND NOT `pending` 
104                         AND NOT `archive` 
105                         AND NOT `deleted` 
106                         AND `notify` != ""',
107                         local_user(),
108                         $cid,
109                         Protocol::DFRN,
110                 ]);
111
112                 $formattedContacts = [];
113
114                 foreach ($contacts as $contact) {
115                         $formattedContacts[$contact['id']] = $contact['name'];
116                 }
117
118                 $tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
119                 return Renderer::replaceMacros($tpl, [
120                         '$contact_id'      => $cid,
121                         '$fsuggest_title'  => DI::l10n()->t('Suggest Friends'),
122                         '$fsuggest_select' => [
123                                 'suggest',
124                                 DI::l10n()->t('Suggest a friend for %s', $contact['name']),
125                                 '',
126                                 '',
127                                 $formattedContacts,
128                         ],
129                         '$submit'          => DI::l10n()->t('Submit'),
130                 ]);
131         }
132 }