diff --git a/include/api.php b/include/api.php index e5963fb8f..8a29c82dd 100644 --- a/include/api.php +++ b/include/api.php @@ -13,6 +13,7 @@ use Friendica\Core\NotificationsManager; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; use Friendica\Model\Photo; use Friendica\Model\User; use Friendica\Network\FKOAuth1; @@ -41,7 +42,6 @@ require_once 'include/html2bbcode.php'; require_once 'mod/wall_upload.php'; require_once 'mod/proxy.php'; require_once 'include/message.php'; -require_once 'include/group.php'; require_once 'include/like.php'; require_once 'include/plaintext.php'; @@ -4645,7 +4645,7 @@ function api_friendica_group_show($type) // loop through all groups and retrieve all members for adding data in the user array foreach ($r as $rr) { - $members = group_get_members($rr['id']); + $members = Contact::getByGroupId($rr['id']); $users = array(); if ($type == "xml") { @@ -4674,7 +4674,9 @@ function api_friendica_group_delete($type) { $a = get_app(); - if (api_user() === false) throw new ForbiddenException(); + if (api_user() === false) { + throw new ForbiddenException(); + } // params $user_info = api_get_user($a); @@ -4683,8 +4685,9 @@ function api_friendica_group_delete($type) $uid = $user_info['uid']; // error if no gid specified - if ($gid == 0 || $name == "") + if ($gid == 0 || $name == "") { throw new BadRequestException('gid or name not specified'); + } // get data of the specified group id $r = q( @@ -4693,8 +4696,9 @@ function api_friendica_group_delete($type) intval($gid) ); // error message if specified gid is not in database - if (!DBM::is_result($r)) + if (!DBM::is_result($r)) { throw new BadRequestException('gid not available'); + } // get data of the specified group id and group name $rname = q( @@ -4704,11 +4708,12 @@ function api_friendica_group_delete($type) dbesc($name) ); // error message if specified gid is not in database - if (!DBM::is_result($rname)) + if (!DBM::is_result($rname)) { throw new BadRequestException('wrong group name'); + } // delete group - $ret = group_rmv($uid, $name); + $ret = Group::removeByName($uid, $name); if ($ret) { // return success $success = array('success' => $ret, 'gid' => $gid, 'name' => $name, 'status' => 'deleted', 'wrong users' => array()); @@ -4759,9 +4764,9 @@ function api_friendica_group_create($type) $reactivate_group = true; // create group - $ret = group_add($uid, $name); + $ret = Group::create($uid, $name); if ($ret) { - $gid = group_byname($uid, $name); + $gid = Group::getIdByName($uid, $name); } else { throw new BadRequestException('other API error'); } @@ -4778,7 +4783,7 @@ function api_friendica_group_create($type) intval($uid) ); if (count($contact)) - $result = group_add_member($uid, $name, $cid, $gid); + $result = Group::create_member($uid, $name, $cid, $gid); else { $erroraddinguser = true; $errorusers[] = $cid; @@ -4817,14 +4822,14 @@ function api_friendica_group_update($type) throw new BadRequestException('gid not specified'); // remove members - $members = group_get_members($gid); + $members = Contact::getByGroupId($gid); foreach ($members as $member) { $cid = $member['id']; foreach ($users as $user) { $found = ($user['cid'] == $cid ? true : false); } if (!$found) { - $ret = group_rmv_member($uid, $name, $cid); + $ret = Group::removeMemberByName($uid, $name, $cid); } } @@ -4841,7 +4846,7 @@ function api_friendica_group_update($type) ); if (count($contact)) { - $result = group_add_member($uid, $name, $cid, $gid); + $result = Group::create_member($uid, $name, $cid, $gid); } else { $erroraddinguser = true; $errorusers[] = $cid; diff --git a/include/follow.php b/include/follow.php index e08136cab..c9e81f7b3 100644 --- a/include/follow.php +++ b/include/follow.php @@ -8,14 +8,14 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Model\User; use Friendica\Network\Probe; use Friendica\Protocol\Diaspora; use Friendica\Protocol\OStatus; use Friendica\Protocol\PortableContact; use Friendica\Protocol\Salmon; -require_once 'include/group.php'; - function update_contact($id) { /* Warning: Never ever fetch the public key via Probe::uri and write it into the contacts. @@ -244,10 +244,7 @@ function new_contact($uid, $url, $interactive = false, $network = '') { $contact_id = $r[0]['id']; $result['cid'] = $contact_id; - $def_gid = get_default_group($uid, $contact["network"]); - if (intval($def_gid)) { - group_add_member($uid, '', $contact_id, $def_gid); - } + Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id); // Update the avatar Contact::updateAvatar($ret['photo'], $uid, $contact_id); diff --git a/include/group.php b/include/group.php deleted file mode 100644 index 6e7348c4e..000000000 --- a/include/group.php +++ /dev/null @@ -1,396 +0,0 @@ -may apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL); - } - return true; - } - $r = dba::insert('group', array('uid' => $uid, 'name' => $name)); - $ret = $r; - } - return $ret; -} - - -function group_rmv($uid,$name) { - $ret = false; - if (x($uid) && x($name)) { - $r = q("SELECT id FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1", - intval($uid), - dbesc($name) - ); - if (DBM::is_result($r)) - $group_id = $r[0]['id']; - if (! $group_id) - return false; - - // remove group from default posting lists - $r = q("SELECT def_gid, allow_gid, deny_gid FROM user WHERE uid = %d LIMIT 1", - intval($uid) - ); - if ($r) { - $user_info = $r[0]; - $change = false; - - if ($user_info['def_gid'] == $group_id) { - $user_info['def_gid'] = 0; - $change = true; - } - if (strpos($user_info['allow_gid'], '<' . $group_id . '>') !== false) { - $user_info['allow_gid'] = str_replace('<' . $group_id . '>', '', $user_info['allow_gid']); - $change = true; - } - if (strpos($user_info['deny_gid'], '<' . $group_id . '>') !== false) { - $user_info['deny_gid'] = str_replace('<' . $group_id . '>', '', $user_info['deny_gid']); - $change = true; - } - - if ($change) { - q("UPDATE user SET def_gid = %d, allow_gid = '%s', deny_gid = '%s' WHERE uid = %d", - intval($user_info['def_gid']), - dbesc($user_info['allow_gid']), - dbesc($user_info['deny_gid']), - intval($uid) - ); - } - } - - // remove all members - dba::delete('group_member', array('uid' => $uid, 'pid' => $group_id)); - - // remove group - $r = q("UPDATE `group` SET `deleted` = 1 WHERE `uid` = %d AND `name` = '%s'", - intval($uid), - dbesc($name) - ); - - $ret = $r; - - } - - return $ret; -} - -function group_byname($uid,$name) { - if ((! $uid) || (! strlen($name))) - return false; - $r = q("SELECT * FROM `group` WHERE `uid` = %d AND `name` = '%s' LIMIT 1", - intval($uid), - dbesc($name) - ); - if (DBM::is_result($r)) - return $r[0]['id']; - return false; -} - -function group_rmv_member($uid, $name, $member) { - $gid = group_byname($uid, $name); - - if (!$gid) { - return false; - } - - if (!($uid && $gid && $member)) { - return false; - } - - $r = dba::delete('group_member', array('uid' => $uid, 'gid' => $gid, 'contact-id' => $member)); - return $r; -} - - -function group_add_member($uid,$name,$member,$gid = 0) { - if (! $gid) - $gid = group_byname($uid,$name); - if ((! $gid) || (! $uid) || (! $member)) - return false; - - $r = q("SELECT * FROM `group_member` WHERE `uid` = %d AND `gid` = %d AND `contact-id` = %d LIMIT 1", - intval($uid), - intval($gid), - intval($member) - ); - if (DBM::is_result($r)) - return true; // You might question this, but - // we indicate success because the group member was in fact created - // -- It was just created at another time - if (! DBM::is_result($r)) { - $r = dba::insert('group_member', array('uid' => $uid, 'gid' => $gid, 'contact-id' => $member)); - } - return $r; -} - -function group_get_members($gid) { - $ret = array(); - if (intval($gid)) { - $r = q("SELECT `group_member`.`contact-id`, `contact`.* FROM `group_member` - INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id` - WHERE `gid` = %d AND `group_member`.`uid` = %d AND - NOT `contact`.`self` AND NOT `contact`.`blocked` AND NOT `contact`.`pending` - ORDER BY `contact`.`name` ASC ", - intval($gid), - intval(local_user()) - ); - if (DBM::is_result($r)) - $ret = $r; - } - return $ret; -} - -function group_public_members($gid) { - $ret = 0; - if (intval($gid)) { - $r = q("SELECT `contact`.`id` AS `contact-id` FROM `group_member` - INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id` - WHERE `gid` = %d AND `group_member`.`uid` = %d - AND `contact`.`network` = '%s' AND `contact`.`notify` != '' ", - intval($gid), - intval(local_user()), - dbesc(NETWORK_OSTATUS) - ); - if (DBM::is_result($r)) - $ret = count($r); - } - return $ret; -} - - -function mini_group_select($uid,$gid = 0, $label = "") { - - $grps = array(); - $o = ''; - - $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC", - intval($uid) - ); - $grps[] = array('name' => '', 'id' => '0', 'selected' => ''); - if (DBM::is_result($r)) { - foreach ($r as $rr) { - $grps[] = array('name' => $rr['name'], 'id' => $rr['id'], 'selected' => (($gid == $rr['id']) ? 'true' : '')); - } - - } - logger('groups: ' . print_r($grps,true)); - - if ($label == "") - $label = t('Default privacy group for new contacts'); - - $o = replace_macros(get_markup_template('group_selection.tpl'), array( - '$label' => $label, - '$groups' => $grps - )); - return $o; -} - - -/** - * @brief Create group sidebar widget - * - * @param string $every - * @param string $each - * @param string $editmode - * 'standard' => include link 'Edit groups' - * 'extended' => include link 'Create new group' - * 'full' => include link 'Create new group' and provide for each group a link to edit this group - * @param int $group_id - * @param int $cid - * @return string - */ -function group_side($every="contacts",$each="group",$editmode = "standard", $group_id = 0, $cid = 0) { - - $o = ''; - - if (! local_user()) - return ''; - - $groups = array(); - - $groups[] = array( - 'text' => t('Everybody'), - 'id' => 0, - 'selected' => (($group_id == 0) ? 'group-selected' : ''), - 'href' => $every, - ); - - - - $r = q("SELECT * FROM `group` WHERE `deleted` = 0 AND `uid` = %d ORDER BY `name` ASC", - intval($_SESSION['uid']) - ); - $member_of = array(); - if ($cid) { - $member_of = groups_containing(local_user(),$cid); - } - - if (DBM::is_result($r)) { - foreach ($r as $rr) { - $selected = (($group_id == $rr['id']) ? ' group-selected' : ''); - - if ($editmode == "full") { - $groupedit = array( - 'href' => "group/".$rr['id'], - 'title' => t('edit'), - ); - } else { - $groupedit = null; - } - - $groups[] = array( - 'id' => $rr['id'], - 'cid' => $cid, - 'text' => $rr['name'], - 'selected' => $selected, - 'href' => $each."/".$rr['id'], - 'edit' => $groupedit, - 'ismember' => in_array($rr['id'],$member_of), - ); - } - } - - - $tpl = get_markup_template("group_side.tpl"); - $o = replace_macros($tpl, array( - '$title' => t('Groups'), - 'newgroup' => (($editmode == "extended") || ($editmode == "full") ? 1 : ''), - '$editgroupstext' => t('Edit groups'), - 'grouppage' => "group/", - '$edittext' => t('Edit group'), - '$createtext' => t('Create a new group'), - '$creategroup' => t('Group Name: '), - '$form_security_token' => get_form_security_token("group_edit"), - '$ungrouped' => (($every === 'contacts') ? t('Contacts not in any group') : ''), - '$groups' => $groups, - '$add' => t('add'), - )); - - - return $o; -} - -function expand_groups($a,$check_dead = false, $use_gcontact = false) { - if (! (is_array($a) && count($a))) - return array(); - $groups = implode(',', $a); - $groups = dbesc($groups); - - if ($use_gcontact) - $r = q("SELECT `gcontact`.`id` AS `contact-id` FROM `group_member` - INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id` - INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl` - WHERE `gid` IN ($groups)"); - else - $r = q("SELECT `contact-id` FROM `group_member` WHERE `gid` IN ( $groups )"); - - - $ret = array(); - if (DBM::is_result($r)) - foreach ($r as $rr) - $ret[] = $rr['contact-id']; - if ($check_dead && !$use_gcontact) { - require_once('include/acl_selectors.php'); - $ret = prune_deadguys($ret); - } - return $ret; -} - - -function member_of($c) { - - $r = q("SELECT `group`.`name`, `group`.`id` FROM `group` INNER JOIN `group_member` ON `group_member`.`gid` = `group`.`id` WHERE `group_member`.`contact-id` = %d AND `group`.`deleted` = 0 ORDER BY `group`.`name` ASC ", - intval($c) - ); - - return $r; - -} - -function groups_containing($uid,$c) { - - $r = q("SELECT `gid` FROM `group_member` WHERE `uid` = %d AND `group_member`.`contact-id` = %d ", - intval($uid), - intval($c) - ); - - $ret = array(); - if (DBM::is_result($r)) { - foreach ($r as $rr) { - $ret[] = $rr['gid']; - } - } - - return $ret; -} -/** - * @brief count unread group items - * - * Count unread items of each groups - * - * @return array - * 'id' => group id - * 'name' => group name - * 'count' => counted unseen group items - * - */ -function groups_count_unseen() { - - $r = q("SELECT `group`.`id`, `group`.`name`, - (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`) - WHERE `uid` = %d AND `unseen` AND - `contact-id` IN (SELECT `contact-id` FROM `group_member` - WHERE `group_member`.`gid` = `group`.`id` AND `group_member`.`uid` = %d)) AS `count` - FROM `group` WHERE `group`.`uid` = %d;", - intval(local_user()), - intval(local_user()), - intval(local_user()) - ); - - return $r; -} - -/** - * @brief Returns the default group for a given user and network - * - * @param int $uid User id - * @param string $network network name - * - * @return int group id - */ -function get_default_group($uid, $network = "") { - - $default_group = 0; - - if ($network == NETWORK_OSTATUS) - $default_group = PConfig::get($uid, "ostatus", "default_group"); - - if ($default_group != 0) - return $default_group; - - $g = q("SELECT `def_gid` FROM `user` WHERE `uid` = %d LIMIT 1", intval($uid)); - if ($g && intval($g[0]["def_gid"])) - $default_group = $g[0]["def_gid"]; - - return $default_group; -} diff --git a/include/items.php b/include/items.php index 0779ed91e..9f3f4ab30 100644 --- a/include/items.php +++ b/include/items.php @@ -12,6 +12,8 @@ use Friendica\Core\System; use Friendica\Database\DBM; use Friendica\Model\Contact; use Friendica\Model\GContact; +use Friendica\Model\Group; +use Friendica\Model\User; use Friendica\Object\Image; use Friendica\Protocol\DFRN; use Friendica\Protocol\OStatus; @@ -27,7 +29,6 @@ require_once 'include/plaintext.php'; require_once 'include/feed.php'; require_once 'mod/share.php'; require_once 'include/enotify.php'; -require_once 'include/group.php'; function construct_verb($item) { if ($item['verb']) { @@ -1720,11 +1721,7 @@ function new_follower($importer, $contact, $datarray, $item, $sharing = false) { 'hash' => $hash, 'datetime' => datetime_convert())); } - $def_gid = get_default_group($importer['uid'], $contact_record["network"]); - - if (intval($def_gid)) { - group_add_member($importer['uid'], '', $contact_record['id'], $def_gid); - } + Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']); if (($r[0]['notify-flags'] & NOTIFY_INTRO) && in_array($r[0]['page-flags'], array(PAGE_NORMAL))) { @@ -1958,9 +1955,9 @@ function compare_permissions($obj1, $obj2) { /// @TODO type-hint is array function enumerate_permissions($obj) { $allow_people = expand_acl($obj['allow_cid']); - $allow_groups = expand_groups(expand_acl($obj['allow_gid'])); + $allow_groups = Group::expand(expand_acl($obj['allow_gid'])); $deny_people = expand_acl($obj['deny_cid']); - $deny_groups = expand_groups(expand_acl($obj['deny_gid'])); + $deny_groups = Group::expand(expand_acl($obj['deny_gid'])); $recipients = array_unique(array_merge($allow_people, $allow_groups)); $deny = array_unique(array_merge($deny_people, $deny_groups)); $recipients = array_diff($recipients, $deny); diff --git a/mod/contactgroup.php b/mod/contactgroup.php index 0c2d6a0cb..96b65fd40 100644 --- a/mod/contactgroup.php +++ b/mod/contactgroup.php @@ -2,47 +2,48 @@ use Friendica\App; use Friendica\Database\DBM; +use Friendica\Model\Contact; +use Friendica\Model\Group; -require_once('include/group.php'); - -function contactgroup_content(App $a) { - if (! local_user()) { +function contactgroup_content(App $a) +{ + if (!local_user()) { killme(); } - if(($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) { + if (($a->argc > 2) && intval($a->argv[1]) && intval($a->argv[2])) { $r = q("SELECT `id` FROM `contact` WHERE `id` = %d AND `uid` = %d and `self` = 0 and `blocked` = 0 AND `pending` = 0 LIMIT 1", intval($a->argv[2]), intval(local_user()) ); - if (DBM::is_result($r)) + if (DBM::is_result($r)) { $change = intval($a->argv[2]); + } } - if(($a->argc > 1) && (intval($a->argv[1]))) { - + if (($a->argc > 1) && (intval($a->argv[1]))) { $r = q("SELECT * FROM `group` WHERE `id` = %d AND `uid` = %d AND `deleted` = 0 LIMIT 1", intval($a->argv[1]), intval(local_user()) ); - if (! DBM::is_result($r)) { + if (!DBM::is_result($r)) { killme(); } $group = $r[0]; - $members = group_get_members($group['id']); + $members = Contact::getByGroupId($group['id']); $preselected = array(); - if(count($members)) { - foreach($members as $member) + if (count($members)) { + foreach ($members as $member) { $preselected[] = $member['id']; + } } - if($change) { - if(in_array($change,$preselected)) { - group_rmv_member(local_user(),$group['name'],$change); - } - else { - group_add_member(local_user(),$group['name'],$change); + if ($change) { + if (in_array($change, $preselected)) { + Group::removeMember($group['id'], $change); + } else { + Group::addMember($group['id'], $change); } } } diff --git a/mod/contacts.php b/mod/contacts.php index 71ffcc2d6..fd1d6776c 100644 --- a/mod/contacts.php +++ b/mod/contacts.php @@ -8,6 +8,7 @@ use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; use Friendica\Model\GContact; +use Friendica\Model\Group; use Friendica\Network\Probe; require_once 'include/contact_selectors.php'; @@ -31,7 +32,6 @@ function contacts_init(App $a) { } } - require_once 'include/group.php'; require_once 'include/contact_widgets.php'; if ($_GET['nets'] == "all") { @@ -77,7 +77,7 @@ function contacts_init(App $a) { $findpeople_widget .= findpeople_widget(); } - $groups_widget .= group_side('contacts','group','full',0,$contact_id); + $groups_widget .= Group::sidebarWidget('contacts','group','full',0,$contact_id); $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array( '$vcard_widget' => $vcard_widget, diff --git a/mod/dfrn_confirm.php b/mod/dfrn_confirm.php index c999013ef..112ee34ab 100644 --- a/mod/dfrn_confirm.php +++ b/mod/dfrn_confirm.php @@ -25,11 +25,12 @@ use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Model\User; use Friendica\Network\Probe; use Friendica\Protocol\Diaspora; require_once 'include/enotify.php'; -require_once 'include/group.php'; function dfrn_confirm_post(App $a, $handsfree = null) { @@ -502,13 +503,10 @@ function dfrn_confirm_post(App $a, $handsfree = null) { } } - $def_gid = get_default_group($uid, $contact["network"]); - if($contact && intval($def_gid)) - group_add_member($uid, '', $contact['id'], $def_gid); + Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact['id']); // Let's send our user to the contact editor in case they want to // do anything special with this new friend. - if ($handsfree === null) { goaway(System::baseUrl() . '/contacts/' . intval($contact_id)); } else { diff --git a/mod/dfrn_request.php b/mod/dfrn_request.php index bff1464ae..ec6758656 100644 --- a/mod/dfrn_request.php +++ b/mod/dfrn_request.php @@ -16,10 +16,11 @@ use Friendica\Core\PConfig; use Friendica\Core\System; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; +use Friendica\Model\User; use Friendica\Network\Probe; require_once 'include/enotify.php'; -require_once 'include/group.php'; function dfrn_request_init(App $a) { @@ -190,12 +191,11 @@ function dfrn_request_post(App $a) { $parms['key'] // this was already escaped ); if (DBM::is_result($r)) { - $def_gid = get_default_group(local_user(), $r[0]["network"]); - if(intval($def_gid)) - group_add_member(local_user(), '', $r[0]['id'], $def_gid); + Group::addMember(User::getDefaultGroup($uid, $r[0]["network"]), $r[0]['id']); - if (isset($photo)) + if (isset($photo)) { Contact::updateAvatar($photo, local_user(), $r[0]["id"], true); + } $forwardurl = System::baseUrl()."/contacts/".$r[0]['id']; } else { diff --git a/mod/group.php b/mod/group.php index 4b64964cc..ba7c24c56 100644 --- a/mod/group.php +++ b/mod/group.php @@ -10,11 +10,12 @@ use Friendica\Core\Config; use Friendica\Core\PConfig; use Friendica\Core\System; use Friendica\Database\DBM; +use Friendica\Model\Contact; +use Friendica\Model\Group; function group_init(App $a) { if (local_user()) { - require_once 'include/group.php'; - $a->page['aside'] = group_side('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0)); + $a->page['aside'] = Group::sidebarWidget('contacts', 'group', 'extended', (($a->argc > 1) ? intval($a->argv[1]) : 0)); } } @@ -29,10 +30,10 @@ function group_post(App $a) { check_form_security_token_redirectOnErr('/group/new', 'group_edit'); $name = notags(trim($_POST['groupname'])); - $r = group_add(local_user(), $name); + $r = Group::create(local_user(), $name); if ($r) { info(t('Group created.') . EOL); - $r = group_byname(local_user(), $name); + $r = Group::getIdByName(local_user(), $name); if ($r) { goaway(System::baseUrl() . '/group/' . $r); } @@ -69,7 +70,7 @@ function group_post(App $a) { } } - $a->page['aside'] = group_side(); + $a->page['aside'] = Group::sidebarWidget(); } return; } @@ -118,7 +119,7 @@ function group_content(App $a) { $result = null; if (DBM::is_result($r)) { - $result = group_rmv(local_user(), $r[0]['name']); + $result = Group::removeByName(local_user(), $r[0]['name']); } if ($result) { @@ -158,7 +159,7 @@ function group_content(App $a) { } $group = $r[0]; - $members = group_get_members($group['id']); + $members = Contact::getByGroupId($group['id']); $preselected = array(); $entry = array(); $id = 0; @@ -171,12 +172,12 @@ function group_content(App $a) { if ($change) { if (in_array($change, $preselected)) { - group_rmv_member(local_user(), $group['name'], $change); + Group::removeMember($group['id'], $change); } else { - group_add_member(local_user(), $group['name'], $change); + Group::create_member(local_user(), $group['name'], $change); } - $members = group_get_members($group['id']); + $members = Contact::getByGroupId($group['id']); $preselected = array(); if (count($members)) { foreach ($members as $member) { @@ -233,7 +234,7 @@ function group_content(App $a) { $groupeditor['members'][] = $entry; } else { - group_rmv_member(local_user(), $group['name'], $member['id']); + Group::removeMember($group['id'], $member['id']); } } diff --git a/mod/network.php b/mod/network.php index 3ac13b24e..552625c2a 100644 --- a/mod/network.php +++ b/mod/network.php @@ -10,9 +10,9 @@ use Friendica\Core\Config; use Friendica\Core\PConfig; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; require_once 'include/conversation.php'; -require_once 'include/group.php'; require_once 'include/contact_widgets.php'; require_once 'include/items.php'; require_once 'include/acl_selectors.php'; @@ -157,7 +157,7 @@ function network_init(App $a) { $a->page['aside'] = ''; } - $a->page['aside'] .= (Feature::isEnabled(local_user(),'groups') ? group_side('network/0','network','standard',$group_id) : ''); + $a->page['aside'] .= (Feature::isEnabled(local_user(),'groups') ? Group::sidebarWidget('network/0','network','standard',$group_id) : ''); $a->page['aside'] .= (Feature::isEnabled(local_user(), 'forumlist_widget') ? ForumManager::widget(local_user(), $cid) : ''); $a->page['aside'] .= posted_date_widget('network',local_user(),false); $a->page['aside'] .= networks_widget('network',(x($_GET, 'nets') ? $_GET['nets'] : '')); @@ -567,7 +567,7 @@ function networkThreadedView(App $a, $update = 0) { $o .= $tabs; if ($group) { - if (($t = group_public_members($group)) && !PConfig::get(local_user(),'system','nowarn_insecure')) { + if (($t = Contact::getOStatusCountByGroupId($group)) && !PConfig::get(local_user(), 'system', 'nowarn_insecure')) { notice(sprintf(tt("Warning: This group contains %s member from a network that doesn't allow non public messages.", "Warning: This group contains %s members from a network that doesn't allow non public messages.", $t), $t).EOL); @@ -644,7 +644,7 @@ function networkThreadedView(App $a, $update = 0) { // NOTREACHED } - $contacts = expand_groups(array($group)); + $contacts = Group::expand(array($group)); if ((is_array($contacts)) && count($contacts)) { $contact_str_self = ""; diff --git a/mod/nogroup.php b/mod/nogroup.php index d7df8cb18..d80b6d3db 100644 --- a/mod/nogroup.php +++ b/mod/nogroup.php @@ -5,6 +5,7 @@ use Friendica\App; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; require_once 'include/contact_selectors.php'; @@ -14,14 +15,13 @@ function nogroup_init(App $a) return; } - require_once 'include/group.php'; require_once 'include/contact_widgets.php'; if (! x($a->page, 'aside')) { $a->page['aside'] = ''; } - $a->page['aside'] .= group_side('contacts', 'group', 'extended', 0, $contact_id); + $a->page['aside'] .= Group::sidebarWidget('contacts', 'group', 'extended'); } function nogroup_content(App $a) diff --git a/mod/ping.php b/mod/ping.php index 883129d14..930ed54ff 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -10,11 +10,11 @@ use Friendica\Core\System; use Friendica\Core\PConfig; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; use Friendica\Util\XML; require_once 'include/datetime.php'; require_once 'include/bbcode.php'; -require_once 'include/group.php'; require_once 'mod/proxy.php'; require_once 'include/enotify.php'; @@ -152,7 +152,7 @@ function ping_init(App $a) if ($network_count) { if (intval(Feature::isEnabled(local_user(), 'groups'))) { // Find out how unseen network posts are spread across groups - $group_counts = groups_count_unseen(); + $group_counts = Group::countUnseen(); if (DBM::is_result($group_counts)) { foreach ($group_counts as $group_count) { if ($group_count['count'] > 0) { diff --git a/mod/register.php b/mod/register.php index 4f6dedd24..74633acff 100644 --- a/mod/register.php +++ b/mod/register.php @@ -10,8 +10,8 @@ use Friendica\Model\User; require_once 'include/enotify.php'; require_once 'include/bbcode.php'; -if(! function_exists('register_post')) { -function register_post(App $a) { +function register_post(App $a) +{ check_form_security_token_redirectOnErr('/register', 'register'); global $lang; @@ -22,36 +22,34 @@ function register_post(App $a) { $arr = array('post' => $_POST); call_hooks('register_post', $arr); - $max_dailies = intval(Config::get('system','max_daily_registrations')); - if($max_dailies) { + $max_dailies = intval(Config::get('system', 'max_daily_registrations')); + if ($max_dailies) { $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day"); - if($r && $r[0]['total'] >= $max_dailies) { + if ($r && $r[0]['total'] >= $max_dailies) { return; } } - switch($a->config['register_policy']) { + switch ($a->config['register_policy']) { + case REGISTER_OPEN: + $blocked = 0; + $verified = 1; + break; + case REGISTER_APPROVE: + $blocked = 1; + $verified = 0; + break; - case REGISTER_OPEN: - $blocked = 0; - $verified = 1; - break; - - case REGISTER_APPROVE: - $blocked = 1; - $verified = 0; - break; - - default: - case REGISTER_CLOSED: - if((! x($_SESSION,'authenticated') && (! x($_SESSION,'administrator')))) { - notice( t('Permission denied.') . EOL ); - return; - } - $blocked = 1; - $verified = 0; - break; + default: + case REGISTER_CLOSED: + if ((!x($_SESSION, 'authenticated') && (!x($_SESSION, 'administrator')))) { + notice(t('Permission denied.') . EOL); + return; + } + $blocked = 1; + $verified = 0; + break; } @@ -63,59 +61,51 @@ function register_post(App $a) { $result = User::create($arr); - if(! $result['success']) { + if (!$result['success']) { notice($result['message']); return; } $user = $result['user']; - if($netpublish && $a->config['register_policy'] != REGISTER_APPROVE) { + if ($netpublish && $a->config['register_policy'] != REGISTER_APPROVE) { $url = System::baseUrl() . '/profile/' . $user['nickname']; Worker::add(PRIORITY_LOW, "Directory", $url); } - $using_invites = Config::get('system','invitation_only'); - $num_invites = Config::get('system','number_invites'); - $invite_id = ((x($_POST,'invite_id')) ? notags(trim($_POST['invite_id'])) : ''); + $using_invites = Config::get('system', 'invitation_only'); + $num_invites = Config::get('system', 'number_invites'); + $invite_id = ((x($_POST, 'invite_id')) ? notags(trim($_POST['invite_id'])) : ''); - - if( $a->config['register_policy'] == REGISTER_OPEN ) { - - if($using_invites && $invite_id) { + if ($a->config['register_policy'] == REGISTER_OPEN) { + if ($using_invites && $invite_id) { q("delete * from register where hash = '%s' limit 1", dbesc($invite_id)); - PConfig::set($user['uid'],'system','invites_remaining',$num_invites); + PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites); } // Only send a password mail when the password wasn't manually provided - if (!x($_POST,'password1') || !x($_POST,'confirm')) { + if (!x($_POST, 'password1') || !x($_POST, 'confirm')) { $res = User::sendRegisterOpenEmail( - $user['email'], - $a->config['sitename'], - System::baseUrl(), - $user['username'], - $result['password']); + $user['email'], $a->config['sitename'], System::baseUrl(), $user['username'], $result['password']); - if($res) { - info( t('Registration successful. Please check your email for further instructions.') . EOL ) ; + if ($res) { + info(t('Registration successful. Please check your email for further instructions.') . EOL); goaway(System::baseUrl()); } else { notice( - sprintf( - t('Failed to send email message. Here your accout details:
login: %s
password: %s

You can change your password after login.'), - $user['email'], - $result['password'] - ). EOL + t('Failed to send email message. Here your accout details:
login: %s
password: %s

You can change your password after login.', + $user['email'], + $result['password']) + . EOL ); } } else { - info( t('Registration successful.') . EOL ) ; + info(t('Registration successful.') . EOL); goaway(System::baseUrl()); } - } - elseif($a->config['register_policy'] == REGISTER_APPROVE) { - if(! strlen($a->config['admin_email'])) { - notice( t('Your registration can not be processed.') . EOL); + } elseif ($a->config['register_policy'] == REGISTER_APPROVE) { + if (!strlen($a->config['admin_email'])) { + notice(t('Your registration can not be processed.') . EOL); goaway(System::baseUrl()); } @@ -130,13 +120,13 @@ function register_post(App $a) { ); // invite system - if($using_invites && $invite_id) { - q("delete * from register where hash = '%s' limit 1", dbesc($invite_id)); - PConfig::set($user['uid'],'system','invites_remaining',$num_invites); + if ($using_invites && $invite_id) { + q("DELETE * FROM `register` WHERE `hash` = '%s' LIMIT 1", dbesc($invite_id)); + PConfig::set($user['uid'], 'system', 'invites_remaining', $num_invites); } // send email to admins - $admin_mail_list = "'".implode("','", array_map(dbesc, explode(",", str_replace(" ", "", $a->config['admin_email']))))."'"; + $admin_mail_list = "'" . implode("','", array_map(dbesc, explode(",", str_replace(" ", "", $a->config['admin_email'])))) . "'"; $adminlist = q("SELECT uid, language, email FROM user WHERE email IN (%s)", $admin_mail_list ); @@ -144,117 +134,105 @@ function register_post(App $a) { // send notification to admins foreach ($adminlist as $admin) { notification(array( - 'type' => NOTIFY_SYSTEM, - 'event' => 'SYSTEM_REGISTER_REQUEST', - 'source_name' => $user['username'], - 'source_mail' => $user['email'], - 'source_nick' => $user['nickname'], - 'source_link' => System::baseUrl()."/admin/users/", - 'link' => System::baseUrl()."/admin/users/", - 'source_photo' => System::baseUrl() . "/photo/avatar/".$user['uid'].".jpg", - 'to_email' => $admin['email'], - 'uid' => $admin['uid'], - 'language' => ($admin['language']?$admin['language']:'en'), + 'type' => NOTIFY_SYSTEM, + 'event' => 'SYSTEM_REGISTER_REQUEST', + 'source_name' => $user['username'], + 'source_mail' => $user['email'], + 'source_nick' => $user['nickname'], + 'source_link' => System::baseUrl() . "/admin/users/", + 'link' => System::baseUrl() . "/admin/users/", + 'source_photo' => System::baseUrl() . "/photo/avatar/" . $user['uid'] . ".jpg", + 'to_email' => $admin['email'], + 'uid' => $admin['uid'], + 'language' => $admin['language'] ? $admin['language'] : 'en', 'show_in_notification_page' => false )); } // send notification to the user, that the registration is pending User::sendRegisterPendingEmail( - $user['email'], - $a->config['sitename'], - $user['username']); + $user['email'], $a->config['sitename'], $user['username']); - info( t('Your registration is pending approval by the site owner.') . EOL ) ; + info(t('Your registration is pending approval by the site owner.') . EOL); goaway(System::baseUrl()); - - } return; -}} - - - - - - -if(! function_exists('register_content')) { -function register_content(App $a) { +} +function register_content(App $a) +{ // logged in users can register others (people/pages/groups) // even with closed registrations, unless specifically prohibited by site policy. // 'block_extended_register' blocks all registrations, period. + $block = Config::get('system', 'block_extended_register'); - $block = Config::get('system','block_extended_register'); - - if(local_user() && ($block)) { + if (local_user() && ($block)) { notice("Permission denied." . EOL); return; } - if((! local_user()) && ($a->config['register_policy'] == REGISTER_CLOSED)) { + if ((!local_user()) && ($a->config['register_policy'] == REGISTER_CLOSED)) { notice("Permission denied." . EOL); return; } - $max_dailies = intval(Config::get('system','max_daily_registrations')); - if($max_dailies) { + $max_dailies = intval(Config::get('system', 'max_daily_registrations')); + if ($max_dailies) { $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day"); - if($r && $r[0]['total'] >= $max_dailies) { + if ($r && $r[0]['total'] >= $max_dailies) { logger('max daily registrations exceeded.'); - notice( t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL); + notice(t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL); return; } } - if(x($_SESSION,'theme')) + if (x($_SESSION, 'theme')) { unset($_SESSION['theme']); - if(x($_SESSION,'mobile-theme')) - unset($_SESSION['mobile-theme']); - - - $username = ((x($_POST,'username')) ? $_POST['username'] : ((x($_GET,'username')) ? $_GET['username'] : '')); - $email = ((x($_POST,'email')) ? $_POST['email'] : ((x($_GET,'email')) ? $_GET['email'] : '')); - $openid_url = ((x($_POST,'openid_url')) ? $_POST['openid_url'] : ((x($_GET,'openid_url')) ? $_GET['openid_url'] : '')); - $nickname = ((x($_POST,'nickname')) ? $_POST['nickname'] : ((x($_GET,'nickname')) ? $_GET['nickname'] : '')); - $photo = ((x($_POST,'photo')) ? $_POST['photo'] : ((x($_GET,'photo')) ? hex2bin($_GET['photo']) : '')); - $invite_id = ((x($_POST,'invite_id')) ? $_POST['invite_id'] : ((x($_GET,'invite_id')) ? $_GET['invite_id'] : '')); - - $noid = Config::get('system','no_openid'); - - if($noid) { - $oidhtml = ''; - $fillwith = ''; - $fillext = ''; - $oidlabel = ''; } - else { - $oidhtml = ''; + if (x($_SESSION, 'mobile-theme')) { + unset($_SESSION['mobile-theme']); + } + + + $username = x($_REQUEST, 'username') ? $_REQUEST['username'] : ''; + $email = x($_REQUEST, 'email') ? $_REQUEST['email'] : ''; + $openid_url = x($_REQUEST, 'openid_url') ? $_REQUEST['openid_url'] : ''; + $nickname = x($_REQUEST, 'nickname') ? $_REQUEST['nickname'] : ''; + $photo = x($_REQUEST, 'photo') ? $_REQUEST['photo'] : ''; + $invite_id = x($_REQUEST, 'invite_id') ? $_REQUEST['invite_id'] : ''; + + $noid = Config::get('system', 'no_openid'); + + if ($noid) { + $oidhtml = ''; + $fillwith = ''; + $fillext = ''; + $oidlabel = ''; + } else { + $oidhtml = ''; $fillwith = t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'."); - $fillext = t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.'); + $fillext = t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.'); $oidlabel = t("Your OpenID \x28optional\x29: "); } // I set this and got even more fake names than before... - $realpeople = ''; // t('Members of this network prefer to communicate with real people who use their real names.'); - if(Config::get('system','publish_all')) { - $profile_publish_reg = ''; - } - else { + if (Config::get('system', 'publish_all')) { + $profile_publish = ''; + } else { $publish_tpl = get_markup_template("profile_publish.tpl"); - $profile_publish = replace_macros($publish_tpl,array( - '$instance' => 'reg', - '$pubdesc' => t('Include your profile in member directory?'), + $profile_publish = replace_macros($publish_tpl, array( + '$instance' => 'reg', + '$pubdesc' => t('Include your profile in member directory?'), '$yes_selected' => ' checked="checked" ', - '$no_selected' => '', - '$str_yes' => t('Yes'), - '$str_no' => t('No'), + '$no_selected' => '', + '$str_yes' => t('Yes'), + '$str_no' => t('No'), )); } - $r = q("SELECT count(*) AS `contacts` FROM `contact`"); + $r = q("SELECT COUNT(*) AS `contacts` FROM `contact`"); $passwords = !$r[0]["contacts"]; $license = ''; @@ -263,23 +241,21 @@ function register_content(App $a) { $arr = array('template' => $o); - call_hooks('register_form',$arr); + call_hooks('register_form', $arr); $o = $arr['template']; - $o = replace_macros($o, array( + $o = replace_macros($o, [ '$oidhtml' => $oidhtml, - '$invitations' => Config::get('system','invitation_only'), - '$permonly' => $a->config['register_policy'] == REGISTER_APPROVE, + '$invitations' => Config::get('system', 'invitation_only'), + '$permonly' => $a->config['register_policy'] == REGISTER_APPROVE, '$permonlybox' => array('permonlybox', t('Note for the admin'), '', t('Leave a message for the admin, why you want to join this node')), '$invite_desc' => t('Membership on this site is by invitation only.'), '$invite_label' => t('Your invitation ID: '), - '$invite_id' => $invite_id, + '$invite_id' => $invite_id, '$realpeople' => $realpeople, '$regtitle' => t('Registration'), - '$registertext' =>((x($a->config,'register_text')) - ? bbcode($a->config['register_text']) - : "" ), + '$registertext' => x($a->config, 'register_text') ? bbcode($a->config['register_text']) : "", '$fillwith' => $fillwith, '$fillext' => $fillext, '$oidlabel' => $oidlabel, @@ -289,7 +265,7 @@ function register_content(App $a) { '$passwords' => $passwords, '$password1' => array('password1', t('New Password:'), '', t('Leave empty for an auto generated password.')), '$password2' => array('confirm', t('Confirm:'), '', ''), - '$nickdesc' => str_replace('$sitename',$a->get_hostname(), t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be \'nickname@$sitename\'.')), + '$nickdesc' => t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be \'nickname@%s\'.', $a->get_hostname()), '$nicklabel' => t('Choose a nickname: '), '$photo' => $photo, '$publish' => $profile_publish, @@ -301,9 +277,7 @@ function register_content(App $a) { '$sitename' => $a->get_hostname(), '$importh' => t('Import'), '$importt' => t('Import your profile to this friendica instance'), - '$form_security_token' => get_form_security_token("register") - )); + '$form_security_token' => get_form_security_token("register") + ]); return $o; - -}} - +} diff --git a/mod/settings.php b/mod/settings.php index 5f14c8283..e3d650e08 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -10,11 +10,10 @@ use Friendica\Core\Config; use Friendica\Core\PConfig; use Friendica\Database\DBM; use Friendica\Model\GContact; +use Friendica\Model\Group; use Friendica\Model\User; use Friendica\Protocol\Email; -require_once 'include/group.php'; - function get_theme_config_file($theme) { $a = get_app(); $base_theme = $a->theme_info['extends']; @@ -834,7 +833,7 @@ function settings_content(App $a) { $default_group = PConfig::get(local_user(), 'ostatus', 'default_group'); $legacy_contact = PConfig::get(local_user(), 'ostatus', 'legacy_contact'); - $settings_connectors .= mini_group_select(local_user(), $default_group, t("Default group for OStatus contacts")); + $settings_connectors .= Group::displayGroupSelection(local_user(), $default_group, t("Default group for OStatus contacts")); /// @TODO Found to much different usage to test empty/non-empty strings (e.g. empty(), trim() == '') which is wanted? if ($legacy_contact != "") { @@ -1218,8 +1217,7 @@ function settings_content(App $a) { 'network_only' => array('expire_network_only', t("Only expire posts by others:"), $expire_network_only, '', array(t('No'), t('Yes'))), ); - require_once('include/group.php'); - $group_select = mini_group_select(local_user(), $a->user['def_gid']); + $group_select = Group::displayGroupSelection(local_user(), $a->user['def_gid']); // Private/public post links for the non-JS ACL form $private_post = 1; diff --git a/mod/update_display.php b/mod/update_display.php index 9dd3edf28..00109202e 100644 --- a/mod/update_display.php +++ b/mod/update_display.php @@ -5,25 +5,23 @@ use Friendica\App; use Friendica\Core\PConfig; -require_once("mod/display.php"); -require_once("include/group.php"); - -function update_display_content(App $a) { +require_once "mod/display.php"; +function update_display_content(App $a) +{ $profile_uid = intval($_GET["p"]); header("Content-type: text/html"); echo "\r\n"; echo "
"; - - $text = display_content($a,$profile_uid); + $text = display_content($a, $profile_uid); $pattern = "/]*) src=\"([^\"]*)\"/"; $replace = ""; + $replace = "
" . t("[Embedded content - reload page to view]") . "
"; $pattern = "/<\s*audio[^>]*>(.*?)<\s*\/\s*audio>/i"; $text = preg_replace($pattern, $replace, $text); $pattern = "/<\s*video[^>]*>(.*?)<\s*\/\s*video>/i"; diff --git a/mod/update_network.php b/mod/update_network.php index a6c817379..3a5741f6a 100644 --- a/mod/update_network.php +++ b/mod/update_network.php @@ -5,11 +5,10 @@ use Friendica\App; use Friendica\Core\PConfig; -require_once("mod/network.php"); -require_once("include/group.php"); - -function update_network_content(App $a) { +require_once "mod/network.php"; +function update_network_content(App $a) +{ $profile_uid = intval($_GET["p"]); header("Content-type: text/html"); @@ -27,7 +26,7 @@ function update_network_content(App $a) { $text = preg_replace($pattern, $replace, $text); if (PConfig::get(local_user(), "system", "bandwith_saver")) { - $replace = "
".t("[Embedded content - reload page to view]")."
"; + $replace = "
" . t("[Embedded content - reload page to view]") . "
"; $pattern = "/<\s*audio[^>]*>(.*?)<\s*\/\s*audio>/i"; $text = preg_replace($pattern, $replace, $text); $pattern = "/<\s*video[^>]*>(.*?)<\s*\/\s*video>/i"; diff --git a/src/Model/Contact.php b/src/Model/Contact.php index e790fd484..d7f27ddd5 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -27,6 +27,66 @@ require_once 'include/text.php'; */ class Contact extends BaseObject { + /** + * @brief Returns a list of contacts belonging in a group + * + * @param int $gid + * @return array + */ + public static function getByGroupId($gid) + { + $return = []; + if (intval($gid)) { + $stmt = dba::p('SELECT `group_member`.`contact-id`, `contact`.* + FROM `contact` + INNER JOIN `group_member` + ON `contact`.`id` = `group_member`.`contact-id` + WHERE `gid` = ? + AND `group_member`.`uid` = ? + AND NOT `contact`.`self` + AND NOT `contact`.`blocked` + AND NOT `contact`.`pending` + ORDER BY `contact`.`name` ASC', + $gid, + local_user() + ); + if (DBM::is_result($stmt)) { + $return = dba::inArray($stmt); + } + } + + return $return; + } + + /** + * @brief Returns the count of OStatus contacts in a group + * + * @param int $gid + * @return int + */ + public static function getOStatusCountByGroupId($gid) + { + $return = 0; + if (intval($gid)) { + $contacts = dba::fetch_first('SELECT COUNT(*) AS `count` + FROM `contact` + INNER JOIN `group_member` + ON `contact`.`id` = `group_member`.`contact-id` + WHERE `gid` = ? + AND `group_member`.`uid` = ? + AND `contact`.`network` = ? + AND `contact`.`notify` != ""', + $gid, + local_user(), + NETWORK_OSTATUS + ); + $return = $contacts['count']; + } + + return $return; + } + + /** * Creates the self-contact for the provided user id * diff --git a/src/Model/Group.php b/src/Model/Group.php new file mode 100644 index 000000000..4431bdc88 --- /dev/null +++ b/src/Model/Group.php @@ -0,0 +1,422 @@ + $gid], ['limit' => 1]); + if (DBM::is_result($group) && $group['deleted']) { + dba::update('group', ['deleted' => 0], ['gid' => $gid]); + notice(t('A deleted group with this name was revived. Existing item permissions may apply to this group and any future members. If this is not what you intended, please create another group with a different name.') . EOL); + } + return true; + } + + $return = dba::insert('group', ['uid' => $uid, 'name' => $name]); + } + return $return; + } + + /** + * @brief Get a list of group ids a contact belongs to + * + * @todo Get rid of $uid, the contact id already bears the information + * + * @param int $uid + * @param int $cid + * @return array + */ + private static function getByContactIdForUserId($uid, $cid) + { + $stmt = dba::p('SELECT `id` + FROM `group` + INNER JOIN `group_member` + ON `group_member`.`gid` = `group`.`id` + WHERE `group`.`uid` = ? + AND `group_member`.`contact-id` = ?', + $uid, + $cid + ); + + $return = []; + if (DBM::is_result($stmt)) { + while($group = dba::fetch($stmt)) { + $return[] = $group['id']; + } + } + + return $return; + } + + /** + * @brief count unread group items + * + * Count unread items of each groups of the local user + * + * @return array + * 'id' => group id + * 'name' => group name + * 'count' => counted unseen group items + */ + public static function countUnseen() + { + $stmt = dba::p("SELECT `group`.`id`, `group`.`name`, + (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`) + WHERE `uid` = ? + AND `unseen` + AND `contact-id` IN + (SELECT `contact-id` + FROM `group_member` + WHERE `group_member`.`gid` = `group`.`id` + AND `group_member`.`uid` = ?) + ) AS `count` + FROM `group` + WHERE `group`.`uid` = ?;", + local_user(), + local_user(), + local_user() + ); + + return dba::inArray($stmt); + } + + /** + * @brief Get the group id for a user/name couple + * + * Returns false if no group has been found. + * + * @param int $uid + * @param string $name + * @return int|boolean + */ + public static function getIdByName($uid, $name) + { + if (!$uid || !strlen($name)) { + return false; + } + + $group = dba::select('group', ['id'], ['uid' => $uid, 'name' => $name], ['limit' => 1]); + if (DBM::is_result($group)) { + return $group['id']; + } + + return false; + } + + /** + * @brief Mark a group as deleted + * + * @param type $gid + * @return boolean + */ + public static function remove($gid) { + if (! $gid) { + return false; + } + + // remove group from default posting lists + $user = dba::select('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $uid], ['limit' => 1]); + if (DBM::is_result($user)) { + $change = false; + + if ($user['def_gid'] == $gid) { + $user['def_gid'] = 0; + $change = true; + } + if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) { + $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']); + $change = true; + } + if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) { + $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']); + $change = true; + } + + if ($change) { + dba::update('user', $user, ['uid' => $uid]); + } + } + + // remove all members + dba::delete('group_member', ['gid' => $gid]); + + // remove group + $return = dba::update('group', ['deleted' => 1], ['id' => $gid]); + + return $return; + } + + /** + * @brief Mark a group as deleted based on its name + * + * @deprecated Use Group::remove instead + * + * @param type $uid + * @param type $name + * @return type + */ + public static function removeByName($uid, $name) { + $return = false; + if (x($uid) && x($name)) { + $gid = self::getIdByName($uid, $name); + + $return = self::remove($gid); + } + + return $return; + } + + /** + * @brief Adds a contact to a group + * + * @param int $gid + * @param int $cid + * @return boolean + */ + public static function addMember($gid, $cid) + { + if (!$gid || !$cid) { + return false; + } + + $row_exists = dba::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]); + if ($row_exists) { + // Row already existing, nothing to do + $return = true; + } else { + $return = dba::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]); + } + + return $return; + } + + /** + * @brief Removes a contact from a group + * + * @param int $gid + * @param int $cid + * @return boolean + */ + public static function removeMember($gid, $cid) + { + if (!$gid || !$cid) { + return false; + } + + $return = dba::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]); + + return $return; + } + + /** + * @brief Removes a contact from a group based on its name + * + * @deprecated Use Group::removeMember instead + * + * @param int $uid + * @param string $name + * @param int $cid + * @return boolean + */ + public static function removeMemberByName($uid, $name, $cid) + { + $gid = self::getIdByName($uid, $name); + + $return = self::removeMember($gid, $cid); + + return $return; + } + + /** + * @brief Returns the combined list of contact ids from a group id list + * + * @param array $group_ids + * @param boolean $check_dead + * @param boolean $use_gcontact + * @return array + */ + public static function expand($group_ids, $check_dead = false, $use_gcontact = false) + { + if (!is_array($group_ids) || !count($group_ids)) { + return []; + } + + $condition = '`gid` IN (' . substr(str_repeat("?, ", count($group_ids)), 0, -2) . ')'; + if ($use_gcontact) { + $sql = 'SELECT `gcontact`.`id` AS `contact-id` FROM `group_member` + INNER JOIN `contact` ON `contact`.`id` = `group_member`.`contact-id` + INNER JOIN `gcontact` ON `gcontact`.`nurl` = `contact`.`nurl` + WHERE ' . $condition; + $param_arr = array_merge([$sql], $group_ids); + $stmt = call_user_func_array('dba::p', $param_arr); + } else { + $condition_array = array_merge([$condition], $group_ids); + $stmt = dba::select('group_member', ['contact-id'], $condition_array); + } + + $return = []; + while($group_member = dba::fetch($stmt)) { + $return[] = $group_member['contact-id']; + } + + if ($check_dead && !$use_gcontact) { + require_once 'include/acl_selectors.php'; + $return = prune_deadguys($return); + } + return $return; + } + + /** + * @brief Returns a templated group selection list + * + * @param int $uid + * @param int $gid An optional pre-selected group + * @param string $label An optional label of the list + * @return string + */ + public static function displayGroupSelection($uid, $gid = 0, $label = '') + { + $o = ''; + + $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]); + + $display_groups = [ + [ + 'name' => '', + 'id' => '0', + 'selected' => '' + ] + ]; + while ($group = dba::fetch($stmt)) { + $display_groups[] = [ + 'name' => $group['name'], + 'id' => $group['id'], + 'selected' => $gid == $group['id'] ? 'true' : '' + ]; + } + logger('groups: ' . print_r($display_groups, true)); + + if ($label == '') { + $label = t('Default privacy group for new contacts'); + } + + $o = replace_macros(get_markup_template('group_selection.tpl'), array( + '$label' => $label, + '$groups' => $display_groups + )); + return $o; + } + + /** + * @brief Create group sidebar widget + * + * @param string $every + * @param string $each + * @param string $editmode + * 'standard' => include link 'Edit groups' + * 'extended' => include link 'Create new group' + * 'full' => include link 'Create new group' and provide for each group a link to edit this group + * @param int $group_id + * @param int $cid + * @return string + */ + public static function sidebarWidget($every = 'contacts', $each = 'group', $editmode = 'standard', $group_id = 0, $cid = 0) + { + $o = ''; + + if (!local_user()) { + return ''; + } + + $display_groups = [ + [ + 'text' => t('Everybody'), + 'id' => 0, + 'selected' => (($group_id == 0) ? 'group-selected' : ''), + 'href' => $every, + ] + ]; + + $stmt = dba::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]); + + $member_of = array(); + if ($cid) { + $member_of = self::getByContactIdForUserId(local_user(), $cid); + } + + while ($group = dba::fetch($stmt)) { + $selected = (($group_id == $group['id']) ? ' group-selected' : ''); + + if ($editmode == 'full') { + $groupedit = [ + 'href' => 'group/' . $group['id'], + 'title' => t('edit'), + ]; + } else { + $groupedit = null; + } + + $display_groups[] = [ + 'id' => $group['id'], + 'cid' => $cid, + 'text' => $group['name'], + 'href' => $each . '/' . $group['id'], + 'edit' => $groupedit, + 'selected' => $selected, + 'ismember' => in_array($group['id'], $member_of), + ]; + } + + $tpl = get_markup_template('group_side.tpl'); + $o = replace_macros($tpl, [ + '$add' => t('add'), + '$title' => t('Groups'), + '$groups' => $display_groups, + 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '', + 'grouppage' => 'group/', + '$edittext' => t('Edit group'), + '$ungrouped' => $every === 'contacts' ? t('Contacts not in any group') : '', + '$createtext' => t('Create a new group'), + '$creategroup' => t('Group Name: '), + '$editgroupstext' => t('Edit groups'), + '$form_security_token' => get_form_security_token('group_edit'), + ]); + + + return $o; + } +} diff --git a/src/Model/User.php b/src/Model/User.php index 54d2d3bc6..25aa2401e 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -8,10 +8,12 @@ namespace Friendica\Model; use Friendica\Core\Config; +use Friendica\Core\PConfig; use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; use Friendica\Model\Photo; use Friendica\Object\Image; use dba; @@ -19,7 +21,6 @@ use dba; require_once 'boot.php'; require_once 'include/crypto.php'; require_once 'include/enotify.php'; -require_once 'include/group.php'; require_once 'include/network.php'; require_once 'library/openid.php'; require_once 'include/pgettext.php'; @@ -30,6 +31,36 @@ require_once 'include/text.php'; */ class User { + /** + * @brief Returns the default group for a given user and network + * + * @param int $uid User id + * @param string $network network name + * + * @return int group id + */ + public static function getDefaultGroup($uid, $network = '') + { + $default_group = 0; + + if ($network == NETWORK_OSTATUS) { + $default_group = PConfig::get($uid, "ostatus", "default_group"); + } + + if ($default_group != 0) { + return $default_group; + } + + $user = dba::select('user', ['def_gid'], ['uid' => $uid], ['limit' => 1]); + + if (DBM::is_result($user)) { + $default_group = $user["def_gid"]; + } + + return $default_group; + } + + /** * @brief Authenticate a user with a clear text password * @@ -193,7 +224,6 @@ class User } // So now we are just looking for a space in the full name. - $loose_reg = Config::get('system', 'no_regfullname'); if (!$loose_reg) { $username = mb_convert_case($username, MB_CASE_TITLE, 'UTF-8'); @@ -202,7 +232,6 @@ class User } } - if (!allowed_email($email)) { $result['message'] .= t('Your email domain is not among those allowed on this site.') . EOL; } @@ -346,8 +375,7 @@ class User // Create a group with no members. This allows somebody to use it // right away as a default group for new contacts. - - group_add($newuid, t('Friends')); + Group::create($newuid, t('Friends')); $r = q("SELECT `id` FROM `group` WHERE `uid` = %d AND `name` = '%s'", intval($newuid), diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index d6f1078f4..b56f59dad 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -18,7 +18,9 @@ use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; use Friendica\Model\GContact; +use Friendica\Model\Group; use Friendica\Model\Profile; +use Friendica\Model\User; use Friendica\Network\Probe; use Friendica\Util\XML; @@ -27,7 +29,6 @@ use SimpleXMLElement; require_once 'include/items.php'; require_once 'include/bb2diaspora.php'; -require_once 'include/group.php'; require_once 'include/datetime.php'; require_once 'include/queue_fn.php'; @@ -37,7 +38,6 @@ require_once 'include/queue_fn.php'; */ class Diaspora { - /** * @brief Return a list of relay servers * @@ -2462,11 +2462,7 @@ class Diaspora logger("Author ".$author." was added as contact number ".$contact_record["id"].".", LOGGER_DEBUG); - $def_gid = get_default_group($importer['uid'], $ret["network"]); - - if (intval($def_gid)) { - group_add_member($importer["uid"], "", $contact_record["id"], $def_gid); - } + Group::addMember(User::getDefaultGroup($importer['uid'], $ret["network"]), $contact_record['id']); Contact::updateAvatar($ret["photo"], $importer['uid'], $contact_record["id"], true); diff --git a/src/Worker/Delivery.php b/src/Worker/Delivery.php index c20bb8d8f..9ed9a06c1 100644 --- a/src/Worker/Delivery.php +++ b/src/Worker/Delivery.php @@ -159,8 +159,6 @@ class Delivery { $public_message = true; if (!($mail || $fsuggest || $relocate)) { - require_once 'include/group.php'; - $parent = $items[0]; // This is IMPORTANT!!!! diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 18fff2afd..749478044 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -8,6 +8,7 @@ use Friendica\Core\Config; use Friendica\Core\Worker; use Friendica\Database\DBM; use Friendica\Model\Contact; +use Friendica\Model\Group; use Friendica\Network\Probe; use Friendica\Protocol\Diaspora; use Friendica\Protocol\OStatus; @@ -207,8 +208,6 @@ class Notifier { $slap = OStatus::salmon($target_item, $owner); - require_once 'include/group.php'; - $parent = $items[0]; $thr_parent = q("SELECT `network`, `author-link`, `owner-link` FROM `item` WHERE `uri` = '%s' AND `uid` = %d", @@ -349,9 +348,9 @@ class Notifier { } $allow_people = expand_acl($parent['allow_cid']); - $allow_groups = expand_groups(expand_acl($parent['allow_gid']),true); + $allow_groups = Group::expand(expand_acl($parent['allow_gid']),true); $deny_people = expand_acl($parent['deny_cid']); - $deny_groups = expand_groups(expand_acl($parent['deny_gid'])); + $deny_groups = Group::expand(expand_acl($parent['deny_gid'])); // if our parent is a public forum (forum_mode == 1), uplink to the origional author causing // a delivery fork. private groups (forum_mode == 2) do not uplink