]> git.mxchange.org Git - friendica.git/blob - src/Module/Group.php
Merge pull request #13090 from abanink/feature-openwebauth
[friendica.git] / src / Module / Group.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;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Renderer;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model;
30
31 class Group extends BaseModule
32 {
33         protected function post(array $request = [])
34         {
35                 if (DI::mode()->isAjax()) {
36                         $this->ajaxPost();
37                 }
38
39                 if (!DI::userSession()->getLocalUserId()) {
40                         DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
41                         DI::baseUrl()->redirect();
42                 }
43
44                 // @TODO: Replace with parameter from router
45                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
46                         BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
47
48                         $name = trim($request['groupname']);
49                         $r = Model\Group::create(DI::userSession()->getLocalUserId(), $name);
50                         if ($r) {
51                                 $r = Model\Group::getIdByName(DI::userSession()->getLocalUserId(), $name);
52                                 if ($r) {
53                                         DI::baseUrl()->redirect('group/' . $r);
54                                 }
55                         } else {
56                                 DI::sysmsg()->addNotice(DI::l10n()->t('Could not create group.'));
57                         }
58                         DI::baseUrl()->redirect('group');
59                 }
60
61                 // @TODO: Replace with parameter from router
62                 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
63                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
64
65                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId()]);
66                         if (!DBA::isResult($group)) {
67                                 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
68                                 DI::baseUrl()->redirect('contact');
69                         }
70                         $groupname = trim($_POST['groupname']);
71                         if (strlen($groupname) && ($groupname != $group['name'])) {
72                                 if (!Model\Group::update($group['id'], $groupname)) {
73                                         DI::sysmsg()->addNotice(DI::l10n()->t('Group name was not changed.'));
74                                 }
75                         }
76                 }
77         }
78
79         public function ajaxPost()
80         {
81                 try {
82                         if (!DI::userSession()->getLocalUserId()) {
83                                 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
84                         }
85
86                         if (isset($this->parameters['command'])) {
87                                 $group_id = $this->parameters['group'];
88                                 $contact_id = $this->parameters['contact'];
89
90                                 if (!Model\Group::exists($group_id, DI::userSession()->getLocalUserId())) {
91                                         throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
92                                 }
93
94                                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
95                                 $cdata = Model\Contact::getPublicAndUserContactID($contact_id, DI::userSession()->getLocalUserId());
96                                 if (empty($cdata['public'])) {
97                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
98                                 }
99
100                                 if (empty($cdata['user'])) {
101                                         throw new \Exception(DI::l10n()->t('Invalid contact.'), 404);
102                                 }
103
104                                 $contact = Model\Contact::getById($cdata['user'], ['deleted']);
105                                 if (!DBA::isResult($contact)) {
106                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
107                                 }
108
109                                 if ($contact['deleted']) {
110                                         throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
111                                 }
112
113                                 switch($this->parameters['command']) {
114                                         case 'add':
115                                                 if (!Model\Group::addMember($group_id, $cdata['user'])) {
116                                                         throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
117                                                 }
118
119                                                 $message = DI::l10n()->t('Contact successfully added to group.');
120                                                 break;
121                                         case 'remove':
122                                                 if (!Model\Group::removeMember($group_id, $cdata['user'])) {
123                                                         throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
124                                                 }
125
126                                                 $message = DI::l10n()->t('Contact successfully removed from group.');
127                                                 break;
128                                 }
129                         } else {
130                                 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
131                         }
132
133                         DI::sysmsg()->addInfo($message);
134                         System::jsonExit(['status' => 'OK', 'message' => $message]);
135                 } catch (\Exception $e) {
136                         DI::sysmsg()->addNotice($e->getMessage());
137                         System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
138                 }
139         }
140
141         protected function content(array $request = []): string
142         {
143                 $change = false;
144
145                 if (!DI::userSession()->getLocalUserId()) {
146                         throw new \Friendica\Network\HTTPException\ForbiddenException();
147                 }
148
149                 $a = DI::app();
150
151                 DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone'));
152
153                 // With no group number provided we jump to the unassigned contacts as a starting point
154                 // @TODO: Replace with parameter from router
155                 if (DI::args()->getArgc() == 1) {
156                         DI::baseUrl()->redirect('group/none');
157                 }
158
159                 // Switch to text mode interface if we have more than 'n' contacts or group members
160                 $switchtotext = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'groupedit_image_limit');
161                 if (is_null($switchtotext)) {
162                         $switchtotext = DI::config()->get('system', 'groupedit_image_limit', 200);
163                 }
164
165                 $tpl = Renderer::getMarkupTemplate('group_edit.tpl');
166
167
168                 $context = [
169                         '$submit' => DI::l10n()->t('Save Group'),
170                         '$submit_filter' => DI::l10n()->t('Filter'),
171                 ];
172
173                 // @TODO: Replace with parameter from router
174                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
175                         return Renderer::replaceMacros($tpl, $context + [
176                                 '$title' => DI::l10n()->t('Create a group of contacts/friends.'),
177                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''],
178                                 '$gid' => 'new',
179                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_edit"),
180                         ]);
181                 }
182
183                 $nogroup = false;
184
185                 // @TODO: Replace with parameter from router
186                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'none') ||
187                         (DI::args()->getArgc() == 1) && (DI::args()->getArgv()[0] === 'nogroup')) {
188                         $id = -1;
189                         $nogroup = true;
190                         $group = [
191                                 'id' => $id,
192                                 'name' => DI::l10n()->t('Contacts not in any group'),
193                         ];
194
195                         $members = [];
196                         $preselected = [];
197
198                         $context = $context + [
199                                 '$title' => $group['name'],
200                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
201                                 '$gid' => $id,
202                                 '$editable' => 0,
203                         ];
204                 }
205
206                 // @TODO: Replace with parameter from router
207                 if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop')) {
208                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't');
209
210                         // @TODO: Replace with parameter from router
211                         if (intval(DI::args()->getArgv()[2])) {
212                                 if (!Model\Group::exists(DI::args()->getArgv()[2], DI::userSession()->getLocalUserId())) {
213                                         DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
214                                         DI::baseUrl()->redirect('contact');
215                                 }
216
217                                 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
218                                         DI::sysmsg()->addNotice(DI::l10n()->t('Unable to remove group.'));
219                                 }
220                         }
221                         DI::baseUrl()->redirect('group');
222                 }
223
224                 // @TODO: Replace with parameter from router
225                 if ((DI::args()->getArgc() > 2) && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) {
226                         BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't');
227
228                         if (DBA::exists('contact', ['id' => DI::args()->getArgv()[2], 'uid' => DI::userSession()->getLocalUserId(), 'self' => false, 'pending' => false, 'blocked' => false])) {
229                                 $change = intval(DI::args()->getArgv()[2]);
230                         }
231                 }
232
233                 // @TODO: Replace with parameter from router
234                 if ((DI::args()->getArgc() > 1) && intval(DI::args()->getArgv()[1])) {
235                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => DI::userSession()->getLocalUserId(), 'deleted' => false]);
236                         if (!DBA::isResult($group)) {
237                                 DI::sysmsg()->addNotice(DI::l10n()->t('Group not found.'));
238                                 DI::baseUrl()->redirect('contact');
239                         }
240
241                         $members = Model\Contact\Group::getById($group['id']);
242                         $preselected = [];
243
244                         if (count($members)) {
245                                 foreach ($members as $member) {
246                                         $preselected[] = $member['id'];
247                                 }
248                         }
249
250                         if ($change) {
251                                 if (in_array($change, $preselected)) {
252                                         Model\Group::removeMember($group['id'], $change);
253                                 } else {
254                                         Model\Group::addMember($group['id'], $change);
255                                 }
256
257                                 $members = Model\Contact\Group::getById($group['id']);
258                                 $preselected = [];
259                                 if (count($members)) {
260                                         foreach ($members as $member) {
261                                                 $preselected[] = $member['id'];
262                                         }
263                                 }
264                         }
265
266                         $drop_tpl = Renderer::getMarkupTemplate('group_drop.tpl');
267                         $drop_txt = Renderer::replaceMacros($drop_tpl, [
268                                 '$id' => $group['id'],
269                                 '$delete' => DI::l10n()->t('Delete Group'),
270                                 '$form_security_token' => BaseModule::getFormSecurityToken("group_drop"),
271                         ]);
272
273                         $context = $context + [
274                                 '$title' => $group['name'],
275                                 '$gname' => ['groupname', DI::l10n()->t('Group Name: '), $group['name'], ''],
276                                 '$gid' => $group['id'],
277                                 '$drop' => $drop_txt,
278                                 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
279                                 '$edit_name' => DI::l10n()->t('Edit Group Name'),
280                                 '$editable' => 1,
281                         ];
282                 }
283
284                 if (!isset($group)) {
285                         throw new \Friendica\Network\HTTPException\BadRequestException();
286                 }
287
288                 $groupeditor = [
289                         'label_members' => DI::l10n()->t('Members'),
290                         'members' => [],
291                         'label_contacts' => DI::l10n()->t('All Contacts'),
292                         'group_is_empty' => DI::l10n()->t('Group is empty'),
293                         'contacts' => [],
294                 ];
295
296                 $sec_token = addslashes(BaseModule::getFormSecurityToken('group_member_change'));
297
298                 // Format the data of the group members
299                 foreach ($members as $member) {
300                         if ($member['url']) {
301                                 $entry = Contact::getContactTemplateVars($member);
302                                 $entry['label'] = 'members';
303                                 $entry['photo_menu'] = '';
304                                 $entry['change_member'] = [
305                                         'title'     => DI::l10n()->t("Remove contact from group"),
306                                         'gid'       => $group['id'],
307                                         'cid'       => $member['id'],
308                                         'sec_token' => $sec_token
309                                 ];
310
311                                 $groupeditor['members'][] = $entry;
312                         } else {
313                                 Model\Group::removeMember($group['id'], $member['id']);
314                         }
315                 }
316
317                 if ($nogroup) {
318                         $contacts = Model\Contact\Group::listUngrouped(DI::userSession()->getLocalUserId());
319                 } else {
320                         $contacts_stmt = DBA::select('contact', [],
321                                 ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING],
322                                 'uid' => DI::userSession()->getLocalUserId(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false],
323                                 ['order' => ['name']]
324                         );
325                         $contacts = DBA::toArray($contacts_stmt);
326                         $context['$desc'] = DI::l10n()->t('Click on a contact to add or remove.');
327                 }
328
329                 if (DBA::isResult($contacts)) {
330                         // Format the data of the contacts who aren't in the contact group
331                         foreach ($contacts as $member) {
332                                 if (!in_array($member['id'], $preselected)) {
333                                         $entry = Contact::getContactTemplateVars($member);
334                                         $entry['label'] = 'contacts';
335                                         if (!$nogroup)
336                                                 $entry['photo_menu'] = [];
337
338                                         if (!$nogroup) {
339                                                 $entry['change_member'] = [
340                                                         'title'     => DI::l10n()->t("Add contact to group"),
341                                                         'gid'       => $group['id'],
342                                                         'cid'       => $member['id'],
343                                                         'sec_token' => $sec_token
344                                                 ];
345                                         }
346
347                                         $groupeditor['contacts'][] = $entry;
348                                 }
349                         }
350                 }
351
352                 $context['$groupeditor'] = $groupeditor;
353
354                 // If there are to many contacts we could provide an alternative view mode
355                 $total = count($groupeditor['members']) + count($groupeditor['contacts']);
356                 $context['$shortmode'] = (($switchtotext && ($total > $switchtotext)) ? true : false);
357
358                 if ($change) {
359                         $tpl = Renderer::getMarkupTemplate('groupeditor.tpl');
360                         echo Renderer::replaceMacros($tpl, $context);
361                         System::exit();
362                 }
363
364                 return Renderer::replaceMacros($tpl, $context);
365         }
366 }