X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModule%2FGroup.php;h=3cd8166f1fadc2a37275b27b39787c29fc7ef917;hb=6dbbd081795fa1c8fe57db2248ac162efeeada88;hp=d9caac32daed9b102d4f48e0eeffead99971f554;hpb=fc2340d4afe6466c58e35afd64fe9010bf185b04;p=friendica.git diff --git a/src/Module/Group.php b/src/Module/Group.php index d9caac32da..3cd8166f1f 100644 --- a/src/Module/Group.php +++ b/src/Module/Group.php @@ -1,6 +1,6 @@ isAjax()) { - self::ajaxPost(); + $this->ajaxPost(); } if (!local_user()) { @@ -47,10 +44,10 @@ class Group extends BaseModule } // @TODO: Replace with parameter from router - if (($a->argc == 2) && ($a->argv[1] === 'new')) { + if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) { BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit'); - $name = Strings::escapeTags(trim($_POST['groupname'])); + $name = trim($request['groupname']); $r = Model\Group::create(local_user(), $name); if ($r) { $r = Model\Group::getIdByName(local_user(), $name); @@ -64,15 +61,15 @@ class Group extends BaseModule } // @TODO: Replace with parameter from router - if (($a->argc == 2) && intval($a->argv[1])) { + if ((DI::args()->getArgc() == 2) && intval(DI::args()->getArgv()[1])) { BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_edit'); - $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user()]); + $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => local_user()]); if (!DBA::isResult($group)) { notice(DI::l10n()->t('Group not found.')); DI::baseUrl()->redirect('contact'); } - $groupname = Strings::escapeTags(trim($_POST['groupname'])); + $groupname = trim($_POST['groupname']); if (strlen($groupname) && ($groupname != $group['name'])) { if (!Model\Group::update($group['id'], $groupname)) { notice(DI::l10n()->t('Group name was not changed.')); @@ -81,26 +78,32 @@ class Group extends BaseModule } } - public static function ajaxPost() + public function ajaxPost() { try { - $a = DI::app(); - if (!local_user()) { throw new \Exception(DI::l10n()->t('Permission denied.'), 403); } - // POST /group/123/add/123 - // POST /group/123/remove/123 - // @TODO: Replace with parameter from router - if ($a->argc == 4) { - list($group_id, $command, $contact_id) = array_slice($a->argv, 1); + if (isset($this->parameters['command'])) { + $group_id = $this->parameters['group']; + $contact_id = $this->parameters['contact']; if (!Model\Group::exists($group_id, local_user())) { throw new \Exception(DI::l10n()->t('Unknown group.'), 404); } - $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]); + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $cdata = Model\Contact::getPublicAndUserContactID($contact_id, local_user()); + if (empty($cdata['public'])) { + throw new \Exception(DI::l10n()->t('Contact not found.'), 404); + } + + if (empty($cdata['user'])) { + throw new \Exception(DI::l10n()->t('Invalid contact.'), 404); + } + + $contact = Model\Contact::getById($cdata['user'], ['deleted']); if (!DBA::isResult($contact)) { throw new \Exception(DI::l10n()->t('Contact not found.'), 404); } @@ -109,29 +112,27 @@ class Group extends BaseModule throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410); } - switch($command) { + switch($this->parameters['command']) { case 'add': - if (!Model\Group::addMember($group_id, $contact_id)) { + if (!Model\Group::addMember($group_id, $cdata['user'])) { throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500); } $message = DI::l10n()->t('Contact successfully added to group.'); break; case 'remove': - if (!Model\Group::removeMember($group_id, $contact_id)) { + if (!Model\Group::removeMember($group_id, $cdata['user'])) { throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500); } $message = DI::l10n()->t('Contact successfully removed from group.'); break; - default: - throw new \Exception(DI::l10n()->t('Unknown group command.'), 400); } } else { throw new \Exception(DI::l10n()->t('Bad request.'), 400); } - notice($message); + info($message); System::jsonExit(['status' => 'OK', 'message' => $message]); } catch (\Exception $e) { notice($e->getMessage()); @@ -139,7 +140,7 @@ class Group extends BaseModule } } - public static function content(array $parameters = []) + protected function content(array $request = []): string { $change = false; @@ -149,11 +150,11 @@ class Group extends BaseModule $a = DI::app(); - DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', (($a->argc > 1) ? $a->argv[1] : 'everyone')); + DI::page()['aside'] = Model\Group::sidebarWidget('contact', 'group', 'extended', ((DI::args()->getArgc() > 1) ? DI::args()->getArgv()[1] : 'everyone')); // With no group number provided we jump to the unassigned contacts as a starting point // @TODO: Replace with parameter from router - if ($a->argc == 1) { + if (DI::args()->getArgc() == 1) { DI::baseUrl()->redirect('group/none'); } @@ -172,7 +173,7 @@ class Group extends BaseModule ]; // @TODO: Replace with parameter from router - if (($a->argc == 2) && ($a->argv[1] === 'new')) { + if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) { return Renderer::replaceMacros($tpl, $context + [ '$title' => DI::l10n()->t('Create a group of contacts/friends.'), '$gname' => ['groupname', DI::l10n()->t('Group Name: '), '', ''], @@ -184,8 +185,8 @@ class Group extends BaseModule $nogroup = false; // @TODO: Replace with parameter from router - if (($a->argc == 2) && ($a->argv[1] === 'none') || - ($a->argc == 1) && ($a->argv[0] === 'nogroup')) { + if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'none') || + (DI::args()->getArgc() == 1) && (DI::args()->getArgv()[0] === 'nogroup')) { $id = -1; $nogroup = true; $group = [ @@ -205,17 +206,17 @@ class Group extends BaseModule } // @TODO: Replace with parameter from router - if (($a->argc == 3) && ($a->argv[1] === 'drop')) { + if ((DI::args()->getArgc() == 3) && (DI::args()->getArgv()[1] === 'drop')) { BaseModule::checkFormSecurityTokenRedirectOnError('/group', 'group_drop', 't'); // @TODO: Replace with parameter from router - if (intval($a->argv[2])) { - if (!Model\Group::exists($a->argv[2], local_user())) { + if (intval(DI::args()->getArgv()[2])) { + if (!Model\Group::exists(DI::args()->getArgv()[2], local_user())) { notice(DI::l10n()->t('Group not found.')); DI::baseUrl()->redirect('contact'); } - if (!Model\Group::remove($a->argv[2])) { + if (!Model\Group::remove(DI::args()->getArgv()[2])) { notice(DI::l10n()->t('Unable to remove group.')); } } @@ -223,17 +224,17 @@ class Group extends BaseModule } // @TODO: Replace with parameter from router - if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) { + if ((DI::args()->getArgc() > 2) && intval(DI::args()->getArgv()[1]) && intval(DI::args()->getArgv()[2])) { BaseModule::checkFormSecurityTokenForbiddenOnError('group_member_change', 't'); - if (DBA::exists('contact', ['id' => $a->argv[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) { - $change = intval($a->argv[2]); + if (DBA::exists('contact', ['id' => DI::args()->getArgv()[2], 'uid' => local_user(), 'self' => false, 'pending' => false, 'blocked' => false])) { + $change = intval(DI::args()->getArgv()[2]); } } // @TODO: Replace with parameter from router - if (($a->argc > 1) && intval($a->argv[1])) { - $group = DBA::selectFirst('group', ['id', 'name'], ['id' => $a->argv[1], 'uid' => local_user(), 'deleted' => false]); + if ((DI::args()->getArgc() > 1) && intval(DI::args()->getArgv()[1])) { + $group = DBA::selectFirst('group', ['id', 'name'], ['id' => DI::args()->getArgv()[1], 'uid' => local_user(), 'deleted' => false]); if (!DBA::isResult($group)) { notice(DI::l10n()->t('Group not found.')); DI::baseUrl()->redirect('contact'); @@ -319,7 +320,8 @@ class Group extends BaseModule $contacts = Model\Contact\Group::listUngrouped(local_user()); } else { $contacts_stmt = DBA::select('contact', [], - ['uid' => local_user(), 'pending' => false, 'blocked' => false, 'self' => false], + ['rel' => [Model\Contact::FOLLOWER, Model\Contact::FRIEND, Model\Contact::SHARING], + 'uid' => local_user(), 'pending' => false, 'blocked' => false, 'failed' => false, 'self' => false], ['order' => ['name']] ); $contacts = DBA::toArray($contacts_stmt);