From 41dc6cea2b05971c63de7fe870d115667ccc081d Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 11 Oct 2021 04:33:10 +0000 Subject: [PATCH 1/3] Replace the legacy function --- src/Content/Widget.php | 32 +++++------------- src/Module/Contact.php | 39 ++++++--------------- src/Module/Search/Acl.php | 6 ++-- view/theme/frio/theme.php | 71 --------------------------------------- 4 files changed, 22 insertions(+), 126 deletions(-) diff --git a/src/Content/Widget.php b/src/Content/Widget.php index 653c99624b..3f97e9f259 100644 --- a/src/Content/Widget.php +++ b/src/Content/Widget.php @@ -92,11 +92,13 @@ class Widget /** * Return unavailable networks as array + * + * @return array Unsupported networks */ - public static function unavailableNetworksAsArray() + public static function unavailableNetworks() { // Always hide content from these networks - $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET]; + $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::ZOT]; if (!Addon::isEnabled("discourse")) { $networks[] = Protocol::DISCOURSE; @@ -128,24 +130,6 @@ class Widget return $networks; } - /** - * Return unavailable networks - */ - public static function unavailableNetworks() - { - $networks = self::unavailableNetworksAsArray(); - - if (!sizeof($networks)) { - return ""; - } - - $network_filter = implode("','", $networks); - - $network_filter = "AND `network` NOT IN ('$network_filter')"; - - return $network_filter; - } - /** * Display a generic filter widget based on a list of options * @@ -274,10 +258,12 @@ class Widget return ''; } - $extra_sql = self::unavailableNetworks(); + $networks = self::unavailableNetworks(); + $extra_sql = " AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"; + $sql_values = array_merge([local_user()], $networks); $r = DBA::p("SELECT `network` FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql GROUP BY `network` ORDER BY `network`", - local_user() + $sql_values ); $nets = array(); @@ -525,7 +511,7 @@ class Widget /** * Display the account types sidebar * The account type value is added as a parameter to the url - * + * * @param string $base Basepath * @param int $accounttype Acount type * @return string diff --git a/src/Module/Contact.php b/src/Module/Contact.php index c3075febed..2cd575df90 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -664,42 +664,25 @@ class Contact extends BaseModule } if ($group) { - $sql_extra = " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)"; + $sql_extra .= " AND EXISTS(SELECT `id` FROM `group_member` WHERE `gid` = ? AND `contact`.`id` = `contact-id`)"; $sql_values[] = $group; } - $total = 0; - $stmt = DBA::p("SELECT COUNT(*) AS `total` - FROM `contact` - WHERE `uid` = ? - AND `self` = 0 - AND NOT `deleted` - $sql_extra - " . Widget::unavailableNetworks(), - $sql_values - ); - if (DBA::isResult($stmt)) { - $total = DBA::fetch($stmt)['total']; - } - DBA::close($stmt); + $networks = Widget::unavailableNetworks(); + $sql_extra .= " AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"; + $sql_values = array_merge($sql_values, $networks); + + $condition = ["`uid` = ? AND NOT `self` AND NOT `deleted`" . $sql_extra]; + $condition = array_merge($condition, $sql_values); + + $total = DBA::count('contact', $condition); $pager = new Pager(DI::l10n(), DI::args()->getQueryString()); - $sql_values[] = $pager->getStart(); - $sql_values[] = $pager->getItemsPerPage(); - $contacts = []; - $stmt = DBA::p("SELECT * - FROM `contact` - WHERE `uid` = ? - AND `self` = 0 - AND NOT `deleted` - $sql_extra - ORDER BY `name` ASC - LIMIT ?, ?", - $sql_values - ); + $stmt = DBA::select('contact', [], $condition, ['order' => ['name'], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]); + while ($contact = DBA::fetch($stmt)) { $contact['blocked'] = Model\Contact\User::isBlocked($contact['id'], local_user()); $contact['readonly'] = Model\Contact\User::isIgnored($contact['id'], local_user()); diff --git a/src/Module/Search/Acl.php b/src/Module/Search/Acl.php index f0e9450be6..33d9e32c18 100644 --- a/src/Module/Search/Acl.php +++ b/src/Module/Search/Acl.php @@ -130,10 +130,8 @@ class Acl extends BaseModule $group_count = DBA::count('group', $condition_group); } - $networks = Widget::unavailableNetworksAsArray(); - if (!empty($networks)) { - $condition = DBA::mergeConditions($condition, array_merge(["NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"], $networks)); - } + $networks = Widget::unavailableNetworks(); + $condition = DBA::mergeConditions($condition, array_merge(["NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"], $networks)); switch ($type) { case self::TYPE_MENTION_CONTACT_GROUP: diff --git a/view/theme/frio/theme.php b/view/theme/frio/theme.php index 079b3c6d5c..961eb2a5eb 100644 --- a/view/theme/frio/theme.php +++ b/view/theme/frio/theme.php @@ -55,7 +55,6 @@ function frio_install() Hook::register('item_photo_menu', 'view/theme/frio/theme.php', 'frio_item_photo_menu'); Hook::register('contact_photo_menu', 'view/theme/frio/theme.php', 'frio_contact_photo_menu'); Hook::register('nav_info', 'view/theme/frio/theme.php', 'frio_remote_nav'); - Hook::register('acl_lookup_end', 'view/theme/frio/theme.php', 'frio_acl_lookup'); Hook::register('display_item', 'view/theme/frio/theme.php', 'frio_display_item'); Logger::log('installed theme frio'); @@ -239,76 +238,6 @@ function frio_remote_nav(App $a, array &$nav_info) } } -/** - * Search for contacts - * - * This function search for a users contacts. The code is copied from contact search - * in /src/Module/Contact.php. With this function the contacts will permitted to acl_lookup() - * and can grabbed as json. For this we use the type="r". This is usful to to let js - * grab the contact data. - * We use this to give the data to textcomplete and have a filter function at the - * contact page. - * - * @todo Is this function still in use? - * - * @param App $a The app data @TODO Unused - * @param array $results The array with the originals from acl_lookup() - */ -function frio_acl_lookup(App $a, &$results) -{ - $nets = !empty($_GET['nets']) ? Strings::escapeTags(trim($_GET['nets'])) : ''; - - // we introduce a new search type, r should do the same query like it's - // done in /src/Module/Contact.php for connections - if ($results['type'] !== 'r') { - return; - } - - $sql_extra = ''; - if ($results['search']) { - $search_txt = DBA::escape(Strings::protectSprintf(preg_quote($results['search']))); - $sql_extra .= " AND (`attag` LIKE '%%" . $search_txt . "%%' OR `name` LIKE '%%" . $search_txt . "%%' OR `nick` LIKE '%%" . $search_txt . "%%') "; - } - - if ($nets) { - $sql_extra .= sprintf(" AND network = '%s' ", DBA::escape($nets)); - } - - $total = 0; - $r = DBA::fetchFirst("SELECT COUNT(*) AS `total` FROM `contact` - WHERE `uid` = ? AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra ", $_SESSION['uid']); - if (DBA::isResult($r)) { - $total = $r['total']; - } - - $sql_extra3 = Widget::unavailableNetworks(); - - $r = DBA::toArray(DBA::p("SELECT * FROM `contact` WHERE `uid` = ? AND NOT `self` AND NOT `deleted` AND NOT `pending` $sql_extra $sql_extra3 ORDER BY `name` ASC LIMIT ?, ? ", - $_SESSION['uid'], $results['start'], $results['count'] - )); - - $contacts = []; - - if (DBA::isResult($r)) { - foreach ($r as $rr) { - $contacts[] = Module\Contact::getContactTemplateVars($rr); - } - } - - $results['items'] = $contacts; - $results['tot'] = $total; -} - -/** - * Manipulate the data of the item - * - * At the moment we use this function to add some own stuff to the item menu - * - * @param App $a App $a The app data - * @param array $arr Array with the item and the item actions
- * 'item' => Array with item data
- * 'output' => Array with item actions
- */ function frio_display_item(App $a, &$arr) { // Add follow to the item menu From d3ebb3ac49479246bbfa026641022579efb57e75 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 11 Oct 2021 05:51:18 +0000 Subject: [PATCH 2/3] Added missing "break" --- src/Module/Search/Acl.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Module/Search/Acl.php b/src/Module/Search/Acl.php index 33d9e32c18..d0a1d86e99 100644 --- a/src/Module/Search/Acl.php +++ b/src/Module/Search/Acl.php @@ -138,10 +138,14 @@ class Acl extends BaseModule $condition = DBA::mergeConditions($condition, ["NOT `self` AND NOT `blocked` AND `notify` != ? AND NOT `network` IN (?, ?)", '', Protocol::OSTATUS, Protocol::STATUSNET ]); + break; + case self::TYPE_MENTION_CONTACT: $condition = DBA::mergeConditions($condition, ["NOT `self` AND NOT `blocked` AND `notify` != ? AND `network` != ?", '', Protocol::STATUSNET ]); + break; + case self::TYPE_MENTION_FORUM: $condition = DBA::mergeConditions($condition, ["NOT `self` AND NOT `blocked` AND `notify` != ? AND `contact-type` = ?", '', Contact::TYPE_COMMUNITY From 86947b09975c253f7fd8d8ad8ef8bae5fad1090c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 11 Oct 2021 06:25:04 +0000 Subject: [PATCH 3/3] Replace "p" with "select" --- src/Content/Widget.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Content/Widget.php b/src/Content/Widget.php index 3f97e9f259..daa34a8b00 100644 --- a/src/Content/Widget.php +++ b/src/Content/Widget.php @@ -259,12 +259,10 @@ class Widget } $networks = self::unavailableNetworks(); - $extra_sql = " AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"; - $sql_values = array_merge([local_user()], $networks); + $query = "`uid` = ? AND NOT `deleted` AND `network` != '' AND NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"; + $condition = array_merge([$query], array_merge([local_user()], $networks)); - $r = DBA::p("SELECT `network` FROM `contact` WHERE `uid` = ? AND NOT `deleted` AND `network` != '' $extra_sql GROUP BY `network` ORDER BY `network`", - $sql_values - ); + $r = DBA::select('contact', ['network'], $condition, ['group_by' => ['network'], 'order' => ['network']]); $nets = array(); while ($rr = DBA::fetch($r)) {