]> git.mxchange.org Git - friendica.git/blob - src/Module/Group.php
Some more "escapeTags" removed
[friendica.git] / src / Module / Group.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\Renderer;
26 use Friendica\Core\System;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Model;
30
31 require_once 'boot.php';
32
33 class Group extends BaseModule
34 {
35         public static function post(array $parameters = [])
36         {
37                 $a = DI::app();
38
39                 if (DI::mode()->isAjax()) {
40                         self::ajaxPost();
41                 }
42
43                 if (!local_user()) {
44                         notice(DI::l10n()->t('Permission denied.'));
45                         DI::baseUrl()->redirect();
46                 }
47
48                 // @TODO: Replace with parameter from router
49                 if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
50                         BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
51
52                         $name = trim($_POST['groupname']);
53                         $r = Model\Group::create(local_user(), $name);
54                         if ($r) {
55                                 $r = Model\Group::getIdByName(local_user(), $name);
56                                 if ($r) {
57                                         DI::baseUrl()->redirect('group/' . $r);
58                                 }
59                         } else {
60                                 notice(DI::l10n()->t('Could not create group.'));
61                         }
62                         DI::baseUrl()->redirect('group');
63                 }
64
65                 // @TODO: Replace with parameter from router
66                 if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) {
67                         BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit');
68
69                         $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => local_user()]);
70                         if (!DBA::isResult($group)) {
71                                 notice(DI::l10n()->t('Group not found.'));
72                                 DI::baseUrl()->redirect('contact');
73                         }
74                         $groupname = trim($_POST['groupname']);
75                         if (strlen($groupname) && ($groupname != $group['name'])) {
76                                 if (!Model\Group::update($group['id'], $groupname)) {
77                                         notice(DI::l10n()->t('Group name was not changed.'));
78                                 }
79                         }
80                 }
81         }
82
83         public static function ajaxPost()
84         {
85                 try {
86                         $a = DI::app();
87
88                         if (!local_user()) {
89                                 throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
90                         }
91
92                         // POST /group/123/add/123
93                         // POST /group/123/remove/123
94                         // @TODO: Replace with parameter from router
95                         if (DI::args()->getArgc() == 4) {
96                                 list($group_id, $command, $contact_id) = array_slice(DI::args()->getArgv(), 1);
97
98                                 if (!Model\Group::exists($group_id, local_user())) {
99                                         throw new \Exception(DI::l10n()->t('Unknown group.'), 404);
100                                 }
101
102                                 $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]);
103                                 if (!DBA::isResult($contact)) {
104                                         throw new \Exception(DI::l10n()->t('Contact not found.'), 404);
105                                 }
106
107                                 if ($contact['deleted']) {
108                                         throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
109                                 }
110
111                                 switch($command) {
112                                         case 'add':
113                                                 if (!Model\Group::addMember($group_id, $contact_id)) {
114                                                         throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500);
115                                                 }
116
117                                                 $message = DI::l10n()->t('Contact successfully added to group.');
118                                                 break;
119                                         case 'remove':
120                                                 if (!Model\Group::removeMember($group_id, $contact_id)) {
121                                                         throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500);
122                                                 }
123
124                                                 $message = DI::l10n()->t('Contact successfully removed from group.');
125                                                 break;
126                                         default:
127                                                 throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
128                                 }
129                         } else {
130                                 throw new \Exception(DI::l10n()->t('Bad request.'), 400);
131                         }
132
133                         info($message);
134                         System::jsonExit(['status' => 'OK', 'message' => $message]);
135                 } catch (\Exception $e) {
136                         notice($e->getMessage());
137                         System::jsonError($e->getCode(), ['status' => 'error', 'message' => $e->getMessage()]);
138                 }
139         }
140
141         public static function content(array $parameters = [])
142         {
143                 $change = false;
144
145                 if (!local_user()) {
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(local_user(), '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], local_user())) {
213                                         notice(DI::l10n()->t('Group not found.'));
214                                         DI::baseUrl()->redirect('contact');
215                                 }
216
217                                 if (!Model\Group::remove(DI::args()->getArgv()[2])) {
218                                         notice(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' => local_user(), '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' => local_user(), 'deleted' => false]);
236                         if (!DBA::isResult($group)) {
237                                 notice(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(local_user());
319                 } else {
320                         $contacts_stmt = DBA::select('contact', [],
321                                 ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING],
322                                 'uid' => local_user(), '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                         exit();
362                 }
363
364                 return Renderer::replaceMacros($tpl, $context);
365         }
366 }