From 9ae8925069fe87ff35b138668ee91946304593bf Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 19 May 2024 18:08:19 +0000 Subject: [PATCH 1/9] Upload: Fix available file extension check --- js_upload/js_upload.php | 43 ++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/js_upload/js_upload.php b/js_upload/js_upload.php index 089cb7e2..786fc3c2 100644 --- a/js_upload/js_upload.php +++ b/js_upload/js_upload.php @@ -7,11 +7,11 @@ * Maintainer: Hypolite Petovan */ -use Friendica\App; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Renderer; use Friendica\DI; +use Friendica\Util\Images; use Friendica\Util\Strings; global $js_upload_jsonresponse; @@ -20,9 +20,9 @@ global $js_upload_result; function js_upload_install() { Hook::register('photo_upload_form', __FILE__, 'js_upload_form'); - Hook::register('photo_post_init', __FILE__, 'js_upload_post_init'); - Hook::register('photo_post_file', __FILE__, 'js_upload_post_file'); - Hook::register('photo_post_end', __FILE__, 'js_upload_post_end'); + Hook::register('photo_post_init', __FILE__, 'js_upload_post_init'); + Hook::register('photo_post_file', __FILE__, 'js_upload_post_file'); + Hook::register('photo_post_end', __FILE__, 'js_upload_post_end'); } function js_upload_form(array &$b) @@ -34,11 +34,11 @@ function js_upload_form(array &$b) $tpl = Renderer::getMarkupTemplate('js_upload.tpl', 'addon/js_upload'); $b['addon_text'] .= Renderer::replaceMacros($tpl, [ - '$upload_msg' => DI::l10n()->t('Select files for upload'), - '$drop_msg' => DI::l10n()->t('Drop files here to upload'), - '$cancel' => DI::l10n()->t('Cancel'), - '$failed' => DI::l10n()->t('Failed'), - '$post_url' => $b['post_url'], + '$upload_msg' => DI::l10n()->t('Select files for upload'), + '$drop_msg' => DI::l10n()->t('Drop files here to upload'), + '$cancel' => DI::l10n()->t('Cancel'), + '$failed' => DI::l10n()->t('Failed'), + '$post_url' => $b['post_url'], '$maximagesize' => Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')), ]); } @@ -48,7 +48,10 @@ function js_upload_post_init(array &$b) global $js_upload_result, $js_upload_jsonresponse; // list of valid extensions - $allowedExtensions = ['jpeg', 'gif', 'png', 'jpg']; + $allowedExtensions = []; + foreach (Images::IMAGETYPES as $type) { + $allowedExtensions[] = image_type_to_extension($type, false); + } // max file size in bytes $sizeLimit = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')); @@ -75,10 +78,9 @@ function js_upload_post_file(array &$b) $result = $js_upload_result; - $b['src'] = $result['path']; + $b['src'] = $result['path']; $b['filename'] = $result['filename']; $b['filesize'] = filesize($b['src']); - } function js_upload_post_end(int &$b) @@ -179,11 +181,11 @@ class qqUploadedFileForm class qqFileUploader { - private $allowedExtensions = []; - private $sizeLimit = 10485760; + private $allowedExtensions; + private $sizeLimit; private $file; - function __construct(array $allowedExtensions = [], $sizeLimit = 10485760) + function __construct(array $allowedExtensions = [], $sizeLimit) { $allowedExtensions = array_map('strtolower', $allowedExtensions); @@ -197,7 +199,6 @@ class qqFileUploader } else { $this->file = false; } - } /** @@ -216,11 +217,9 @@ class qqFileUploader } // if ($size > $this->sizeLimit) { - // return array('error' => DI::l10n()->t('Uploaded file is too large')); // } - $maximagesize = Strings::getBytesFromShorthand(DI::config()->get('system', 'maximagesize')); if (($maximagesize) && ($size > $maximagesize)) { @@ -241,14 +240,14 @@ class qqFileUploader if ($this->file->save()) { return [ - 'success' => true, - 'path' => $this->file->getPath(), + 'success' => true, + 'path' => $this->file->getPath(), 'filename' => $filename . '.' . $ext ]; } else { return [ - 'error' => DI::l10n()->t('Upload was cancelled, or server error encountered'), - 'path' => $this->file->getPath(), + 'error' => DI::l10n()->t('Upload was cancelled, or server error encountered'), + 'path' => $this->file->getPath(), 'filename' => $filename . '.' . $ext ]; } From 084a2a705761f4a21d6e720c267600b92750b700 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 27 May 2024 04:45:17 +0000 Subject: [PATCH 2/9] "zrl" functionility is moved --- forumdirectory/forumdirectory.php | 5 ++--- groupdirectory/groupdirectory.php | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/forumdirectory/forumdirectory.php b/forumdirectory/forumdirectory.php index 114da1da..9c99d63f 100644 --- a/forumdirectory/forumdirectory.php +++ b/forumdirectory/forumdirectory.php @@ -8,7 +8,6 @@ * Note: Please use Group Directory instead */ -use Friendica\App; use Friendica\Content\Nav; use Friendica\Content\Pager; use Friendica\Content\Widget; @@ -16,8 +15,8 @@ use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Model\Profile; use Friendica\Model\User; +use Friendica\Security\OpenWebAuth; global $forumdirectory_search; @@ -82,7 +81,7 @@ function forumdirectory_content() $gdirpath = ''; $dirurl = DI::config()->get('system', 'directory'); if (strlen($dirurl)) { - $gdirpath = Profile::zrl($dirurl, true); + $gdirpath = OpenWebAuth::zrl($dirurl, true); } $sql_extra = ''; diff --git a/groupdirectory/groupdirectory.php b/groupdirectory/groupdirectory.php index 6d562cdc..df4bbb33 100644 --- a/groupdirectory/groupdirectory.php +++ b/groupdirectory/groupdirectory.php @@ -13,8 +13,8 @@ use Friendica\Core\Hook; use Friendica\Core\Renderer; use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Model\Profile; use Friendica\Model\User; +use Friendica\Security\OpenWebAuth; global $groupdirectory_search; @@ -79,7 +79,7 @@ function groupdirectory_content() $gdirpath = ''; $dirurl = DI::config()->get('system', 'directory'); if (strlen($dirurl)) { - $gdirpath = Profile::zrl($dirurl, true); + $gdirpath = OpenWebAuth::zrl($dirurl, true); } $sql_extra = ''; From 39247ca28f84ec25b49e41482b7e5ea91bdf8b21 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 29 May 2024 06:07:48 +0000 Subject: [PATCH 3/9] Function renamed --- forumdirectory/forumdirectory.php | 2 +- groupdirectory/groupdirectory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/forumdirectory/forumdirectory.php b/forumdirectory/forumdirectory.php index 9c99d63f..9a155a00 100644 --- a/forumdirectory/forumdirectory.php +++ b/forumdirectory/forumdirectory.php @@ -81,7 +81,7 @@ function forumdirectory_content() $gdirpath = ''; $dirurl = DI::config()->get('system', 'directory'); if (strlen($dirurl)) { - $gdirpath = OpenWebAuth::zrl($dirurl, true); + $gdirpath = OpenWebAuth::getZrlUrl($dirurl, true); } $sql_extra = ''; diff --git a/groupdirectory/groupdirectory.php b/groupdirectory/groupdirectory.php index df4bbb33..615d7024 100644 --- a/groupdirectory/groupdirectory.php +++ b/groupdirectory/groupdirectory.php @@ -79,7 +79,7 @@ function groupdirectory_content() $gdirpath = ''; $dirurl = DI::config()->get('system', 'directory'); if (strlen($dirurl)) { - $gdirpath = OpenWebAuth::zrl($dirurl, true); + $gdirpath = OpenWebAuth::getZrlUrl($dirurl, true); } $sql_extra = ''; From 08b46e553645a2e02fd587f65fa76950fc6e1398 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sat, 22 Jun 2024 18:56:32 +0200 Subject: [PATCH 4/9] New addon providing additional statistics for moderation --- ratioed/ratioed.php | 264 ++++++++++++++++++++++++++++++++++ ratioed/templates/ratioed.tpl | 164 +++++++++++++++++++++ 2 files changed, 428 insertions(+) create mode 100644 ratioed/ratioed.php create mode 100644 ratioed/templates/ratioed.tpl diff --git a/ratioed/ratioed.php b/ratioed/ratioed.php new file mode 100644 index 00000000..3c88705c --- /dev/null +++ b/ratioed/ratioed.php @@ -0,0 +1,264 @@ + + */ + +use Friendica\Content\Pager; +use Friendica\Core\Hook; +use Friendica\Core\Logger; +use Friendica\Core\Renderer; +use Friendica\Database\DBA; +use Friendica\DI; +use Friendica\Model\User; +use Friendica\Module\Moderation\Users\Active; + +/** + * Sets up the addon hooks and updates data in the database if needed + */ +function ratioed_install() +{ + Hook::register('moderation_users_tabs', 'addon/ratioed/ratioed.php', 'ratioed_users_tabs'); + + Logger::info("ratioed: installed"); +} + +/** + * @brief Uninstallation hook for ratioed plugin + */ +function ratioed_uninstall() { + Hook::unregister('moderation_users_tabs', 'addon/ratioed/ratioed.php', 'ratioed_users_tabs'); + + Logger::info("ratioed: uninstalled"); +} + +/** + * This is a statement rather than an actual function definition. The simple + * existence of this method is checked to figure out if the addon offers a + * module. + */ +function ratioed_module() {} + +/** + * @brief Adds additional users tab to the moderation panel + * + * @param array $arr Parameters, including "tabs" which is the list to modify, and "selectedTab", which is the currently selected tab ID + */ +function ratioed_users_tabs(array &$arr) { + Logger::debug("ratioed: users tabs"); + + array_push($arr['tabs'], [ + 'label' => DI::l10n()->t('Behaviour'), + 'url' => 'ratioed', + 'sel' => $arr['selectedTab'] == 'ratioed' ? 'active' : '', + 'title' => DI::l10n()->t('Statistics about users behaviour'), + 'id' => 'admin-users-ratioed', + 'accesskey' => 'r', + ]); +} + +class Ratioed extends Friendica\Module\Moderation\Users\Active +{ + protected function content(array $request = []): string + { + Friendica\Module\Moderation\Users\Active::content(); + + $action = $this->parameters['action'] ?? ''; + $uid = $this->parameters['uid'] ?? 0; + + if ($uid) { + $user = User::getById($uid, ['username', 'blocked']); + if (!$user) { + $this->systemMessages->addNotice($this->t('User not found')); + $this->baseUrl->redirect('moderation/users'); + } + } + + switch ($action) { + case 'delete': + if ($this->session->getLocalUserId() != $uid) { + self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't'); + // delete user + User::remove($uid); + + $this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username'])); + } else { + $this->systemMessages->addNotice($this->t('You can\'t remove yourself')); + } + + $this->baseUrl->redirect('moderation/users/active'); + break; + case 'block': + self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't'); + User::block($uid); + $this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username'])); + $this->baseUrl->redirect('moderation/users/active'); + break; + } + $pager = new Pager($this->l10n, $this->args->getQueryString(), 100); + + $valid_orders = [ + 'name', + 'email', + 'register_date', + 'last-activity', + 'last-item', + 'page-flags', + ]; + + $order = 'last-item'; + $order_direction = '-'; + if (!empty($request['o'])) { + $new_order = $request['o']; + if ($new_order[0] === '-') { + $order_direction = '-'; + $new_order = substr($new_order, 1); + } + + if (in_array($new_order, $valid_orders)) { + $order = $new_order; + } + } + + $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'active', $order, ($order_direction == '-')); + + $users = array_map($this->setupUserCallback(), $users); + + $header_titles = [ + $this->t('Name'), + $this->t('Email'), + $this->t('Register date'), + $this->t('Last login'), + $this->t('Last public item'), + $this->t('Type'), + $this->t('Blocked by'), + $this->t('Comments last 24h'), + $this->t('Reactions last 24h'), + $this->t('Ratio last 24h'), + ]; + $field_names = [ + 'name', + 'email', + 'register_date', + 'login_date', + 'lastitem_date', + 'page_flags', + 'blocked_by', + 'comments', + 'reactions', + 'ratio', + ]; + $th_users = array_map(null, $header_titles, $valid_orders, $field_names); + + $count = $this->database->count('user', ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` != ?", 0]); + + $t = Renderer::getMarkupTemplate('ratioed.tpl', 'addon/ratioed'); + return self::getTabsHTML('ratioed') . Renderer::replaceMacros($t, [ + // strings // + '$title' => $this->t('Moderation'), + '$page' => $this->t('Behaviour'), + '$select_all' => $this->t('select all'), + '$delete' => $this->t('Delete'), + '$block' => $this->t('Block'), + '$blocked' => $this->t('User blocked'), + '$siteadmin' => $this->t('Site admin'), + '$accountexpired' => $this->t('Account expired'), + '$h_newuser' => $this->t('Create a new user'), + + '$th_users' => $th_users, + '$order_users' => $order, + '$order_direction_users' => $order_direction, + + '$confirm_delete_multi' => $this->t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'), + '$confirm_delete' => $this->t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'), + + '$form_security_token' => self::getFormSecurityToken('moderation_users_active'), + + // values // + '$baseurl' => $this->baseUrl, + '$query_string' => $this->args->getQueryString(), + + '$users' => $users, + '$count' => $count, + '$pager' => $pager->renderFull($count), + ]); + } + + protected function setupUserCallback(): \Closure + { + Logger::debug("ratioed: setupUserCallback"); + $parentCallback = parent::setupUserCallback(); + return function ($user) use ($parentCallback) { + $blocked_count = DBA::count('user-contact', ['uid' => $user['uid'], 'is-blocked' => 1]); + $user['blocked_by'] = $blocked_count; + + $self_contact_result = DBA::p('SELECT admin_contact.id AS user_contact_uid FROM contact AS admin_contact JOIN contact AS user_contact ON admin_contact.`uri-id` = user_contact.`uri-id` AND admin_contact.self = 0 AND user_contact.self = 1 WHERE user_contact.uid = ?', $user['uid']); + if (DBA::isResult($self_contact_result)) { + $self_contact_result_row = DBA::fetch($self_contact_result); + $user['user_contact_uid'] = $self_contact_result_row['user_contact_uid']; + } + else { + $user['user_contact_uid'] = NULL; + } + + if ($user['user_contact_uid']) { + $post_engagement_result = DBA::p('SELECT SUM(`comments`) AS `comment_count`, SUM(`activities`) AS `activities_count` FROM `post-engagement` WHERE `post-engagement`.created > DATE_SUB(now(), INTERVAL 1 DAY) AND `post-engagement`.`owner-id` = ?', $user['user_contact_uid']); + if (DBA::isResult($post_engagement_result)) { + $post_engagement_result_row = DBA::fetch($post_engagement_result); + $user['comments'] = $post_engagement_result_row['comment_count']; + $user['reactions'] = $post_engagement_result_row['activities_count']; + if ($user['reactions'] > 0) { + $user['ratio'] = number_format($user['comments'] / $user['reactions'], 1, '.', ''); + $user['ratioed'] = (float)($user['ratio']) >= 2.0; + } + else { + if ($user['comments'] == 0) { + $user['ratio'] = '0'; + $user['ratioed'] = false; + } + else { + $user['ratio'] = '∞'; + $user['ratioed'] = false; + } + } + } + else { + $user['comments'] = 'error'; + $user['reactions'] = 'error'; + $user['ratio'] = 'error'; + $user['ratioed'] = false; + } + } + else { + $user['comments'] = 'error'; + $user['reactions'] = 'error'; + $user['ratio'] = 'error'; + $user['ratioed'] = false; + } + + $user = $parentCallback($user); + Logger::debug("ratioed: setupUserCallback", [ + 'uid' => $user['uid'], + 'blocked_by' => $user['blocked_by'], + 'comments' => $user['comments'], + 'reactions' => $user['reactions'], + 'ratio' => $user['ratio'], + 'ratioed' => $user['ratioed'], + ]); + return $user; + }; + } +} + +/** + * @brief Displays the ratioed tab in the moderation panel + */ +function ratioed_content() { + Logger::debug("ratioed: content"); + + $ratioed = DI::getDice()->create(Ratioed::class, [$_SERVER]); + $httpException = DI::getDice()->create(Friendica\Module\Special\HTTPException::class); + $ratioed->run($httpException); +} diff --git a/ratioed/templates/ratioed.tpl b/ratioed/templates/ratioed.tpl new file mode 100644 index 00000000..0d81b0d7 --- /dev/null +++ b/ratioed/templates/ratioed.tpl @@ -0,0 +1,164 @@ + + + +
+

{{$title}} - {{$page}} ({{$count}})

+

+ {{$h_newuser}} +

+
+ + + + + + + {{foreach $th_users as $k=>$th}} + {{if $k < 2 || $order_users == $th.1 || ($k==5 && !in_array($order_users,[$th_users.2.1, $th_users.3.1, $th_users.4.1])) }} + + {{/if}} + {{/foreach}} + + + + + {{foreach $users as $u}} + + + + + + {{if $order_users == $th_users.2.1}} + + {{/if}} + + {{if $order_users == $th_users.3.1}} + + {{/if}} + + {{if $order_users == $th_users.4.1}} + + {{/if}} + + {{if !in_array($order_users,[$th_users.2.1, $th_users.3.1, $th_users.4.1]) }} + + {{/if}} + + + + + + + + + {{/foreach}} + +
+
+ + +
+
+ + {{if $order_users == $th.1}} + {{if $order_direction_users == "+"}} + ↓ + {{else}} + ↑ + {{/if}} + {{else}} + ↕ + {{/if}} + {{$th.0}} + +
+ {{if $u.is_deletable}} +
+ + +
+ {{else}} +   + {{/if}} +
{{$u.name}}{{$u.email}}{{$u.register_date}}{{$u.login_date}}{{$u.lastitem_date}} + + + {{if $u.page_flags_raw==0 && $u.account_type_raw > 0}} + + {{/if}} + {{if $u.is_admin}}{{/if}} + {{if $u.account_expired}}{{/if}} + + +
+ + {{$pager nofilter}} +
+
From 7d5446a778419d67ec4df778c836b59083d5f163 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sun, 23 Jun 2024 15:15:06 +0200 Subject: [PATCH 5/9] Ratioed: remove unnecessary uninstall function --- ratioed/ratioed.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/ratioed/ratioed.php b/ratioed/ratioed.php index 3c88705c..d8da9ac1 100644 --- a/ratioed/ratioed.php +++ b/ratioed/ratioed.php @@ -25,15 +25,6 @@ function ratioed_install() Logger::info("ratioed: installed"); } -/** - * @brief Uninstallation hook for ratioed plugin - */ -function ratioed_uninstall() { - Hook::unregister('moderation_users_tabs', 'addon/ratioed/ratioed.php', 'ratioed_users_tabs'); - - Logger::info("ratioed: uninstalled"); -} - /** * This is a statement rather than an actual function definition. The simple * existence of this method is checked to figure out if the addon offers a From 18e512cc8b0bd79a6be164e7fc06ec4a41f56dcd Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sun, 23 Jun 2024 15:24:43 +0200 Subject: [PATCH 6/9] Ratioed: move panel class into separate file --- ratioed/RatioedPanel.php | 206 +++++++++++++++++++++++++++++++++++++++ ratioed/ratioed.php | 201 +------------------------------------- 2 files changed, 208 insertions(+), 199 deletions(-) create mode 100644 ratioed/RatioedPanel.php diff --git a/ratioed/RatioedPanel.php b/ratioed/RatioedPanel.php new file mode 100644 index 00000000..16f9df6f --- /dev/null +++ b/ratioed/RatioedPanel.php @@ -0,0 +1,206 @@ +parameters['action'] ?? ''; + $uid = $this->parameters['uid'] ?? 0; + + if ($uid) { + $user = User::getById($uid, ['username', 'blocked']); + if (!$user) { + $this->systemMessages->addNotice($this->t('User not found')); + $this->baseUrl->redirect('moderation/users'); + } + } + + switch ($action) { + case 'delete': + if ($this->session->getLocalUserId() != $uid) { + self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't'); + // delete user + User::remove($uid); + + $this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username'])); + } else { + $this->systemMessages->addNotice($this->t('You can\'t remove yourself')); + } + + $this->baseUrl->redirect('moderation/users/active'); + break; + case 'block': + self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't'); + User::block($uid); + $this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username'])); + $this->baseUrl->redirect('moderation/users/active'); + break; + } + $pager = new Pager($this->l10n, $this->args->getQueryString(), 100); + + $valid_orders = [ + 'name', + 'email', + 'register_date', + 'last-activity', + 'last-item', + 'page-flags', + ]; + + $order = 'last-item'; + $order_direction = '-'; + if (!empty($request['o'])) { + $new_order = $request['o']; + if ($new_order[0] === '-') { + $order_direction = '-'; + $new_order = substr($new_order, 1); + } + + if (in_array($new_order, $valid_orders)) { + $order = $new_order; + } + } + + $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'active', $order, ($order_direction == '-')); + + $users = array_map($this->setupUserCallback(), $users); + + $header_titles = [ + $this->t('Name'), + $this->t('Email'), + $this->t('Register date'), + $this->t('Last login'), + $this->t('Last public item'), + $this->t('Type'), + $this->t('Blocked by'), + $this->t('Comments last 24h'), + $this->t('Reactions last 24h'), + $this->t('Ratio last 24h'), + ]; + $field_names = [ + 'name', + 'email', + 'register_date', + 'login_date', + 'lastitem_date', + 'page_flags', + 'blocked_by', + 'comments', + 'reactions', + 'ratio', + ]; + $th_users = array_map(null, $header_titles, $valid_orders, $field_names); + + $count = $this->database->count('user', ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` != ?", 0]); + + $t = Renderer::getMarkupTemplate('ratioed.tpl', 'addon/ratioed'); + return self::getTabsHTML('ratioed') . Renderer::replaceMacros($t, [ + // strings // + '$title' => $this->t('Moderation'), + '$page' => $this->t('Behaviour'), + '$select_all' => $this->t('select all'), + '$delete' => $this->t('Delete'), + '$block' => $this->t('Block'), + '$blocked' => $this->t('User blocked'), + '$siteadmin' => $this->t('Site admin'), + '$accountexpired' => $this->t('Account expired'), + '$h_newuser' => $this->t('Create a new user'), + + '$th_users' => $th_users, + '$order_users' => $order, + '$order_direction_users' => $order_direction, + + '$confirm_delete_multi' => $this->t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'), + '$confirm_delete' => $this->t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'), + + '$form_security_token' => self::getFormSecurityToken('moderation_users_active'), + + // values // + '$baseurl' => $this->baseUrl, + '$query_string' => $this->args->getQueryString(), + + '$users' => $users, + '$count' => $count, + '$pager' => $pager->renderFull($count), + ]); + } + + protected function setupUserCallback(): \Closure + { + Logger::debug("ratioed: setupUserCallback"); + $parentCallback = parent::setupUserCallback(); + return function ($user) use ($parentCallback) { + $blocked_count = DBA::count('user-contact', ['uid' => $user['uid'], 'is-blocked' => 1]); + $user['blocked_by'] = $blocked_count; + + $self_contact_result = DBA::p('SELECT admin_contact.id AS user_contact_uid FROM contact AS admin_contact JOIN contact AS user_contact ON admin_contact.`uri-id` = user_contact.`uri-id` AND admin_contact.self = 0 AND user_contact.self = 1 WHERE user_contact.uid = ?', $user['uid']); + if (DBA::isResult($self_contact_result)) { + $self_contact_result_row = DBA::fetch($self_contact_result); + $user['user_contact_uid'] = $self_contact_result_row['user_contact_uid']; + } + else { + $user['user_contact_uid'] = NULL; + } + + if ($user['user_contact_uid']) { + $post_engagement_result = DBA::p('SELECT SUM(`comments`) AS `comment_count`, SUM(`activities`) AS `activities_count` FROM `post-engagement` WHERE `post-engagement`.created > DATE_SUB(now(), INTERVAL 1 DAY) AND `post-engagement`.`owner-id` = ?', $user['user_contact_uid']); + if (DBA::isResult($post_engagement_result)) { + $post_engagement_result_row = DBA::fetch($post_engagement_result); + $user['comments'] = $post_engagement_result_row['comment_count']; + $user['reactions'] = $post_engagement_result_row['activities_count']; + if ($user['reactions'] > 0) { + $user['ratio'] = number_format($user['comments'] / $user['reactions'], 1, '.', ''); + $user['ratioed'] = (float)($user['ratio']) >= 2.0; + } + else { + if ($user['comments'] == 0) { + $user['ratio'] = '0'; + $user['ratioed'] = false; + } + else { + $user['ratio'] = '∞'; + $user['ratioed'] = false; + } + } + } + else { + $user['comments'] = 'error'; + $user['reactions'] = 'error'; + $user['ratio'] = 'error'; + $user['ratioed'] = false; + } + } + else { + $user['comments'] = 'error'; + $user['reactions'] = 'error'; + $user['ratio'] = 'error'; + $user['ratioed'] = false; + } + + $user = $parentCallback($user); + Logger::debug("ratioed: setupUserCallback", [ + 'uid' => $user['uid'], + 'blocked_by' => $user['blocked_by'], + 'comments' => $user['comments'], + 'reactions' => $user['reactions'], + 'ratio' => $user['ratio'], + 'ratioed' => $user['ratioed'], + ]); + return $user; + }; + } +} diff --git a/ratioed/ratioed.php b/ratioed/ratioed.php index d8da9ac1..8ae0d48c 100644 --- a/ratioed/ratioed.php +++ b/ratioed/ratioed.php @@ -6,14 +6,10 @@ * Author: Matthew Exon */ -use Friendica\Content\Pager; +use Friendica\Addon\ratioed\RatioedPanel; use Friendica\Core\Hook; use Friendica\Core\Logger; -use Friendica\Core\Renderer; -use Friendica\Database\DBA; use Friendica\DI; -use Friendica\Model\User; -use Friendica\Module\Moderation\Users\Active; /** * Sets up the addon hooks and updates data in the database if needed @@ -50,206 +46,13 @@ function ratioed_users_tabs(array &$arr) { ]); } -class Ratioed extends Friendica\Module\Moderation\Users\Active -{ - protected function content(array $request = []): string - { - Friendica\Module\Moderation\Users\Active::content(); - - $action = $this->parameters['action'] ?? ''; - $uid = $this->parameters['uid'] ?? 0; - - if ($uid) { - $user = User::getById($uid, ['username', 'blocked']); - if (!$user) { - $this->systemMessages->addNotice($this->t('User not found')); - $this->baseUrl->redirect('moderation/users'); - } - } - - switch ($action) { - case 'delete': - if ($this->session->getLocalUserId() != $uid) { - self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't'); - // delete user - User::remove($uid); - - $this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username'])); - } else { - $this->systemMessages->addNotice($this->t('You can\'t remove yourself')); - } - - $this->baseUrl->redirect('moderation/users/active'); - break; - case 'block': - self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't'); - User::block($uid); - $this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username'])); - $this->baseUrl->redirect('moderation/users/active'); - break; - } - $pager = new Pager($this->l10n, $this->args->getQueryString(), 100); - - $valid_orders = [ - 'name', - 'email', - 'register_date', - 'last-activity', - 'last-item', - 'page-flags', - ]; - - $order = 'last-item'; - $order_direction = '-'; - if (!empty($request['o'])) { - $new_order = $request['o']; - if ($new_order[0] === '-') { - $order_direction = '-'; - $new_order = substr($new_order, 1); - } - - if (in_array($new_order, $valid_orders)) { - $order = $new_order; - } - } - - $users = User::getList($pager->getStart(), $pager->getItemsPerPage(), 'active', $order, ($order_direction == '-')); - - $users = array_map($this->setupUserCallback(), $users); - - $header_titles = [ - $this->t('Name'), - $this->t('Email'), - $this->t('Register date'), - $this->t('Last login'), - $this->t('Last public item'), - $this->t('Type'), - $this->t('Blocked by'), - $this->t('Comments last 24h'), - $this->t('Reactions last 24h'), - $this->t('Ratio last 24h'), - ]; - $field_names = [ - 'name', - 'email', - 'register_date', - 'login_date', - 'lastitem_date', - 'page_flags', - 'blocked_by', - 'comments', - 'reactions', - 'ratio', - ]; - $th_users = array_map(null, $header_titles, $valid_orders, $field_names); - - $count = $this->database->count('user', ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `uid` != ?", 0]); - - $t = Renderer::getMarkupTemplate('ratioed.tpl', 'addon/ratioed'); - return self::getTabsHTML('ratioed') . Renderer::replaceMacros($t, [ - // strings // - '$title' => $this->t('Moderation'), - '$page' => $this->t('Behaviour'), - '$select_all' => $this->t('select all'), - '$delete' => $this->t('Delete'), - '$block' => $this->t('Block'), - '$blocked' => $this->t('User blocked'), - '$siteadmin' => $this->t('Site admin'), - '$accountexpired' => $this->t('Account expired'), - '$h_newuser' => $this->t('Create a new user'), - - '$th_users' => $th_users, - '$order_users' => $order, - '$order_direction_users' => $order_direction, - - '$confirm_delete_multi' => $this->t('Selected users will be deleted!\n\nEverything these users had posted on this site will be permanently deleted!\n\nAre you sure?'), - '$confirm_delete' => $this->t('The user {0} will be deleted!\n\nEverything this user has posted on this site will be permanently deleted!\n\nAre you sure?'), - - '$form_security_token' => self::getFormSecurityToken('moderation_users_active'), - - // values // - '$baseurl' => $this->baseUrl, - '$query_string' => $this->args->getQueryString(), - - '$users' => $users, - '$count' => $count, - '$pager' => $pager->renderFull($count), - ]); - } - - protected function setupUserCallback(): \Closure - { - Logger::debug("ratioed: setupUserCallback"); - $parentCallback = parent::setupUserCallback(); - return function ($user) use ($parentCallback) { - $blocked_count = DBA::count('user-contact', ['uid' => $user['uid'], 'is-blocked' => 1]); - $user['blocked_by'] = $blocked_count; - - $self_contact_result = DBA::p('SELECT admin_contact.id AS user_contact_uid FROM contact AS admin_contact JOIN contact AS user_contact ON admin_contact.`uri-id` = user_contact.`uri-id` AND admin_contact.self = 0 AND user_contact.self = 1 WHERE user_contact.uid = ?', $user['uid']); - if (DBA::isResult($self_contact_result)) { - $self_contact_result_row = DBA::fetch($self_contact_result); - $user['user_contact_uid'] = $self_contact_result_row['user_contact_uid']; - } - else { - $user['user_contact_uid'] = NULL; - } - - if ($user['user_contact_uid']) { - $post_engagement_result = DBA::p('SELECT SUM(`comments`) AS `comment_count`, SUM(`activities`) AS `activities_count` FROM `post-engagement` WHERE `post-engagement`.created > DATE_SUB(now(), INTERVAL 1 DAY) AND `post-engagement`.`owner-id` = ?', $user['user_contact_uid']); - if (DBA::isResult($post_engagement_result)) { - $post_engagement_result_row = DBA::fetch($post_engagement_result); - $user['comments'] = $post_engagement_result_row['comment_count']; - $user['reactions'] = $post_engagement_result_row['activities_count']; - if ($user['reactions'] > 0) { - $user['ratio'] = number_format($user['comments'] / $user['reactions'], 1, '.', ''); - $user['ratioed'] = (float)($user['ratio']) >= 2.0; - } - else { - if ($user['comments'] == 0) { - $user['ratio'] = '0'; - $user['ratioed'] = false; - } - else { - $user['ratio'] = '∞'; - $user['ratioed'] = false; - } - } - } - else { - $user['comments'] = 'error'; - $user['reactions'] = 'error'; - $user['ratio'] = 'error'; - $user['ratioed'] = false; - } - } - else { - $user['comments'] = 'error'; - $user['reactions'] = 'error'; - $user['ratio'] = 'error'; - $user['ratioed'] = false; - } - - $user = $parentCallback($user); - Logger::debug("ratioed: setupUserCallback", [ - 'uid' => $user['uid'], - 'blocked_by' => $user['blocked_by'], - 'comments' => $user['comments'], - 'reactions' => $user['reactions'], - 'ratio' => $user['ratio'], - 'ratioed' => $user['ratioed'], - ]); - return $user; - }; - } -} - /** * @brief Displays the ratioed tab in the moderation panel */ function ratioed_content() { Logger::debug("ratioed: content"); - $ratioed = DI::getDice()->create(Ratioed::class, [$_SERVER]); + $ratioed = DI::getDice()->create(RatioedPanel::class, [$_SERVER]); $httpException = DI::getDice()->create(Friendica\Module\Special\HTTPException::class); $ratioed->run($httpException); } From 5f27f72b0d18bbf3226f4850cb4aa6f2c36482fe Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sat, 29 Jun 2024 15:23:24 +0200 Subject: [PATCH 7/9] Streamline log lines --- mailstream/mailstream.php | 70 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/mailstream/mailstream.php b/mailstream/mailstream.php index b513cf36..c5cf8f3f 100644 --- a/mailstream/mailstream.php +++ b/mailstream/mailstream.php @@ -34,7 +34,7 @@ function mailstream_install() Hook::register('post_remote_end', 'addon/mailstream/mailstream.php', 'mailstream_post_hook'); Hook::register('mailstream_send_hook', 'addon/mailstream/mailstream.php', 'mailstream_send_hook'); - Logger::info("mailstream: installed"); + Logger::info("installed mailstream"); } /** @@ -44,7 +44,7 @@ function mailstream_check_version() { if (!is_null(DI::config()->get('mailstream', 'dbversion'))) { DI::config()->delete('mailstream', 'dbversion'); - Logger::info("mailstream_check_version: old version detected, reinstalling"); + Logger::info("old version detected, reinstalling"); mailstream_install(); Hook::loadHooks(); Hook::add( @@ -105,7 +105,7 @@ function mailstream_generate_id(string $uri): string $host = DI::baseUrl()->getHost(); $resource = hash('md5', $uri); $message_id = "<" . $resource . "@" . $host . ">"; - Logger::debug('mailstream: Generated message ID ' . $message_id . ' for URI ' . $uri); + Logger::debug('generated message ID', ['id' => $message_id, 'uri' => $uri]); return $message_id; } @@ -114,20 +114,20 @@ function mailstream_send_hook(array $data) $criteria = array('uid' => $data['uid'], 'contact-id' => $data['contact-id'], 'uri' => $data['uri']); $item = Post::selectFirst([], $criteria); if (empty($item)) { - Logger::error('mailstream_send_hook could not find item'); + Logger::error('could not find item'); return; } $user = User::getById($item['uid']); if (empty($user)) { - Logger::error('mailstream_send_hook could not fund user', ['uid' => $item['uid']]); + Logger::error('could not find user', ['uid' => $item['uid']]); return; } if (!mailstream_send($data['message_id'], $item, $user)) { - Logger::debug('mailstream_send_hook send failed, will retry', $data); + Logger::debug('send failed, will retry', $data); if (!Worker::defer()) { - Logger::error('mailstream_send_hook failed and could not defer', $data); + Logger::error('failed and could not defer', $data); } } } @@ -145,32 +145,32 @@ function mailstream_post_hook(array &$item) mailstream_check_version(); if (!DI::pConfig()->get($item['uid'], 'mailstream', 'enabled')) { - Logger::debug('mailstream: not enabled.', ['item' => $item['id'], ' uid ' => $item['uid']]); + Logger::debug('mailstream not enabled.', ['item' => $item['id'], 'uid' => $item['uid']]); return; } if (!$item['uid']) { - Logger::debug('mailstream: no uid for item ' . $item['id']); + Logger::debug('no uid', ['item' => $item['id']]); return; } if (!$item['contact-id']) { - Logger::debug('mailstream: no contact-id for item ' . $item['id']); + Logger::debug('no contact-id', ['item' => $item['id']]); return; } if (!$item['uri']) { - Logger::debug('mailstream: no uri for item ' . $item['id']); + Logger::debug('no uri', ['item' => $item['id']]); return; } if ($item['verb'] == Activity::ANNOUNCE) { - Logger::debug('mailstream: announce item ', ['item' => $item['id']]); + Logger::debug('ignoring announce', ['item' => $item['id']]); return; } if (DI::pConfig()->get($item['uid'], 'mailstream', 'nolikes')) { if ($item['verb'] == Activity::LIKE) { - Logger::debug('mailstream: like item ' . $item['id']); + Logger::debug('ignoring like', ['item' => $item['id']]); return; } if ($item['verb'] == Activity::DISLIKE) { - Logger::debug('mailstream: dislike item ' . $item['id']); + Logger::debug('ignoring dislike', ['item' => $item['id']]); return; } } @@ -221,7 +221,7 @@ function mailstream_do_images(array &$item, array &$attachments) try { $curlResult = DI::httpClient()->fetchFull($url, HttpClientAccept::DEFAULT, 0, $cookiejar); } catch (InvalidArgumentException $e) { - Logger::error('mailstream_do_images exception fetching url', ['url' => $url, 'item_id' => $item['id']]); + Logger::error('exception fetching url', ['url' => $url, 'item_id' => $item['id']]); continue; } $attachments[$url] = [ @@ -322,13 +322,12 @@ function mailstream_subject(array $item): string } $contact = Contact::selectFirst([], ['id' => $item['contact-id'], 'uid' => $item['uid']]); if (!DBA::isResult($contact)) { - Logger::error( - 'mailstream_subject no contact for item', - ['id' => $item['id'], - 'plink' => $item['plink'], - 'contact id' => $item['contact-id'], - 'uid' => $item['uid']] - ); + Logger::error('no contact', [ + 'item' => $item['id'], + 'plink' => $item['plink'], + 'contact id' => $item['contact-id'], + 'uid' => $item['uid'] + ]); return DI::l10n()->t("Friendica post"); } if ($contact['network'] === 'dfrn') { @@ -365,17 +364,16 @@ function mailstream_subject(array $item): string function mailstream_send(string $message_id, array $item, array $user): bool { if (!is_array($item)) { - Logger::error('mailstream_send item is empty', ['message_id' => $message_id]); + Logger::error('item is empty', ['message_id' => $message_id]); return false; } if (!$item['visible']) { - Logger::debug('mailstream_send item not yet visible', ['item uri' => $item['uri']]); + Logger::debug('item not yet visible', ['item uri' => $item['uri']]); return false; } if (!$message_id) { - Logger::error('mailstream_send no message ID supplied', ['item uri' => $item['uri'], - 'user email' => $user['email']]); + Logger::error('no message ID supplied', ['item uri' => $item['uri'], 'user email' => $user['email']]); return true; } @@ -432,13 +430,15 @@ function mailstream_send(string $message_id, array $item, array $user): bool if (!$mail->Send()) { throw new Exception($mail->ErrorInfo); } - Logger::debug('mailstream_send sent message', ['message ID' => $mail->MessageID, - 'subject' => $mail->Subject, - 'address' => $address]); + Logger::debug('sent message', [ + 'message ID' => $mail->MessageID, + 'subject' => $mail->Subject, + 'address' => $address + ]); } catch (phpmailerException $e) { - Logger::debug('mailstream_send PHPMailer exception sending message ' . $message_id . ': ' . $e->errorMessage()); + Logger::debug('PHPMailer exception sending message', ['id' => $message_id, 'error' => $e->errorMessage()]); } catch (Exception $e) { - Logger::debug('mailstream_send exception sending message ' . $message_id . ': ' . $e->getMessage()); + Logger::debug('exception sending message', ['id' => $message_id, 'error' => $e->getMessage()]); } return true; @@ -468,7 +468,7 @@ function mailstream_html_wrap(string &$text) function mailstream_convert_table_entries() { $ms_item_ids = DBA::selectToArray('mailstream_item', [], ['message-id', 'uri', 'uid', 'contact-id'], ["`mailstream_item`.`completed` IS NULL"]); - Logger::debug('mailstream_convert_table_entries processing ' . count($ms_item_ids) . ' items'); + Logger::debug('processing items', ['count' => count($ms_item_ids)]); foreach ($ms_item_ids as $ms_item_id) { $send_hook_data = array('uid' => $ms_item_id['uid'], 'contact-id' => $ms_item_id['contact-id'], @@ -476,10 +476,10 @@ function mailstream_convert_table_entries() 'message_id' => $ms_item_id['message-id'], 'tries' => 0); if (!$ms_item_id['message-id'] || !strlen($ms_item_id['message-id'])) { - Logger::info('mailstream_convert_table_entries: item has no message-id.', ['item' => $ms_item_id['id'], 'uri' => $ms_item_id['uri']]); - continue; + Logger::info('item has no message-id', ['item' => $ms_item_id['id'], 'uri' => $ms_item_id['uri']]); + continue; } - Logger::info('mailstream_convert_table_entries: convert item to workerqueue', $send_hook_data); + Logger::info('convert item to workerqueue', $send_hook_data); Hook::fork(Worker::PRIORITY_LOW, 'mailstream_send_hook', $send_hook_data); } DBA::e('DROP TABLE `mailstream_item`'); From f3db763c599cfeb830fdf95a8de5738c183be30b Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Tue, 9 Jul 2024 20:13:00 +0100 Subject: [PATCH 8/9] More comprehensible check for root user contact --- mailstream/mailstream.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mailstream/mailstream.php b/mailstream/mailstream.php index c5cf8f3f..b86c4e2a 100644 --- a/mailstream/mailstream.php +++ b/mailstream/mailstream.php @@ -144,12 +144,12 @@ function mailstream_post_hook(array &$item) { mailstream_check_version(); - if (!DI::pConfig()->get($item['uid'], 'mailstream', 'enabled')) { - Logger::debug('mailstream not enabled.', ['item' => $item['id'], 'uid' => $item['uid']]); + if ($item['uid'] === 0) { + Logger::debug('mailstream: root user, skipping item ' . $item['id']); return; } - if (!$item['uid']) { - Logger::debug('no uid', ['item' => $item['id']]); + if (!DI::pConfig()->get($item['uid'], 'mailstream', 'enabled')) { + Logger::debug('mailstream: not enabled.', ['item' => $item['id'], ' uid ' => $item['uid']]); return; } if (!$item['contact-id']) { From 589cf712cc92911d5288b1dfff06bd8c9d4cfb78 Mon Sep 17 00:00:00 2001 From: Matthew Exon Date: Sun, 14 Jul 2024 18:56:22 +0200 Subject: [PATCH 9/9] Remove old version conversion code --- mailstream/mailstream.php | 44 --------------------------------------- 1 file changed, 44 deletions(-) diff --git a/mailstream/mailstream.php b/mailstream/mailstream.php index b86c4e2a..8bce2529 100644 --- a/mailstream/mailstream.php +++ b/mailstream/mailstream.php @@ -37,25 +37,6 @@ function mailstream_install() Logger::info("installed mailstream"); } -/** - * Enforces that mailstream_install has set up the current version - */ -function mailstream_check_version() -{ - if (!is_null(DI::config()->get('mailstream', 'dbversion'))) { - DI::config()->delete('mailstream', 'dbversion'); - Logger::info("old version detected, reinstalling"); - mailstream_install(); - Hook::loadHooks(); - Hook::add( - 'mailstream_convert_table_entries', - 'addon/mailstream/mailstream.php', - 'mailstream_convert_table_entries' - ); - Hook::fork(Worker::PRIORITY_LOW, 'mailstream_convert_table_entries'); - } -} - /** * This is a statement rather than an actual function definition. The simple * existence of this method is checked to figure out if the addon offers a @@ -142,8 +123,6 @@ function mailstream_send_hook(array $data) */ function mailstream_post_hook(array &$item) { - mailstream_check_version(); - if ($item['uid'] === 0) { Logger::debug('mailstream: root user, skipping item ' . $item['id']); return; @@ -462,29 +441,6 @@ function mailstream_html_wrap(string &$text) return $text; } -/** - * Convert v1 mailstream table entries to v2 workerqueue items - */ -function mailstream_convert_table_entries() -{ - $ms_item_ids = DBA::selectToArray('mailstream_item', [], ['message-id', 'uri', 'uid', 'contact-id'], ["`mailstream_item`.`completed` IS NULL"]); - Logger::debug('processing items', ['count' => count($ms_item_ids)]); - foreach ($ms_item_ids as $ms_item_id) { - $send_hook_data = array('uid' => $ms_item_id['uid'], - 'contact-id' => $ms_item_id['contact-id'], - 'uri' => $ms_item_id['uri'], - 'message_id' => $ms_item_id['message-id'], - 'tries' => 0); - if (!$ms_item_id['message-id'] || !strlen($ms_item_id['message-id'])) { - Logger::info('item has no message-id', ['item' => $ms_item_id['id'], 'uri' => $ms_item_id['uri']]); - continue; - } - Logger::info('convert item to workerqueue', $send_hook_data); - Hook::fork(Worker::PRIORITY_LOW, 'mailstream_send_hook', $send_hook_data); - } - DBA::e('DROP TABLE `mailstream_item`'); -} - /** * Form for configuring mailstream features for a user *