friendica-addons/blockem/blockem.php

231 lines
5.5 KiB
PHP
Raw Normal View History

2012-01-10 09:16:09 +00:00
<?php
/**
* Name: blockem
* Description: Allows users to hide content by collapsing posts and replies.
2012-01-10 09:16:09 +00:00
* Version: 1.0
* Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
* Author: Roland Haeder <https://f.haeder.net/roland>
2023-01-08 19:11:35 +00:00
* Status: unsupported
2012-01-10 09:16:09 +00:00
*/
use Friendica\App;
use Friendica\Core\Hook;
use Friendica\Core\Renderer;
use Friendica\DI;
use Friendica\Util\Strings;
2021-07-25 17:28:07 +00:00
global $blockem_words;
2018-01-22 19:03:11 +00:00
function blockem_install()
{
Hook::register('prepare_body_content_filter', 'addon/blockem/blockem.php', 'blockem_prepare_body_content_filter');
Hook::register('display_item' , 'addon/blockem/blockem.php', 'blockem_display_item');
Hook::register('addon_settings' , 'addon/blockem/blockem.php', 'blockem_addon_settings');
Hook::register('addon_settings_post' , 'addon/blockem/blockem.php', 'blockem_addon_settings_post');
Hook::register('conversation_start' , 'addon/blockem/blockem.php', 'blockem_conversation_start');
Hook::register('item_photo_menu' , 'addon/blockem/blockem.php', 'blockem_item_photo_menu');
Hook::register('enotify_store' , 'addon/blockem/blockem.php', 'blockem_enotify_store');
2012-01-10 09:16:09 +00:00
}
function blockem_addon_settings(App $a, array &$data)
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2012-01-10 09:16:09 +00:00
return;
}
2012-01-10 09:16:09 +00:00
2022-10-20 21:51:49 +00:00
$words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'blockem', 'words', '');
2012-01-10 09:16:09 +00:00
$t = Renderer::getMarkupTemplate('settings.tpl', 'addon/blockem/');
$html = Renderer::replaceMacros($t, [
'$info' => DI::l10n()->t("Hides user's content by collapsing posts. Also replaces their avatar with generic image."),
'$words' => ['blockem-words', DI::l10n()->t('Comma separated profile URLS:'), $words],
]);
2012-01-10 09:16:09 +00:00
$data = [
'addon' => 'blockem',
'title' => DI::l10n()->t('Blockem'),
'html' => $html,
];
2012-01-10 09:16:09 +00:00
}
function blockem_addon_settings_post(App $a, array &$b)
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2012-01-10 09:16:09 +00:00
return;
}
2012-01-10 09:16:09 +00:00
if (!empty($_POST['blockem-submit'])) {
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'blockem', 'words', trim($_POST['blockem-words']));
2012-01-10 09:16:09 +00:00
}
}
function blockem_enotify_store(App $a, array &$b)
{
$words = DI::pConfig()->get($b['uid'], 'blockem', 'words');
2013-05-09 08:18:03 +00:00
if ($words) {
$arr = explode(',', $words);
} else {
2013-05-09 08:18:03 +00:00
return;
}
$found = false;
if (count($arr)) {
foreach ($arr as $word) {
if (!strlen(trim($word))) {
2013-05-09 08:18:03 +00:00
continue;
}
if (Strings::compareLink($b['url'], $word)) {
2013-05-09 08:18:03 +00:00
$found = true;
break;
}
}
}
if ($found) {
// empty out the fields
$b = [];
2013-05-09 08:18:03 +00:00
}
}
function blockem_prepare_body_content_filter(App $a, array &$hook_data)
2018-04-01 06:31:15 +00:00
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2012-01-10 09:16:09 +00:00
return;
}
2018-04-01 06:31:15 +00:00
$profiles_string = null;
2022-10-20 21:51:49 +00:00
if (DI::userSession()->getLocalUserId()) {
$profiles_string = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'blockem', 'words');
2012-01-10 09:16:09 +00:00
}
2018-04-01 06:31:15 +00:00
if ($profiles_string) {
$profiles_array = explode(',', $profiles_string);
} else {
2012-01-10 09:16:09 +00:00
return;
}
$found = false;
2018-04-01 06:31:15 +00:00
foreach ($profiles_array as $word) {
if (Strings::compareLink($hook_data['item']['author-link'], trim($word))) {
2018-04-01 06:31:15 +00:00
$found = true;
break;
2012-01-10 09:16:09 +00:00
}
}
2018-04-01 06:31:15 +00:00
if ($found) {
$hook_data['filter_reasons'][] = DI::l10n()->t('Filtered user: %s', $hook_data['item']['author-name']);
2012-01-10 09:16:09 +00:00
}
}
2012-01-11 00:54:47 +00:00
function blockem_display_item(App $a, array &$b = null)
{
if (!empty($b['output']['body']) && strstr($b['output']['body'], 'id="blockem-wrap-')) {
$b['output']['thumb'] = DI::baseUrl()->get() . "/images/person-80.jpg";
}
2012-01-11 00:54:47 +00:00
}
function blockem_conversation_start(App $a, array &$b)
{
2021-07-25 17:28:07 +00:00
global $blockem_words;
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2012-01-12 01:36:20 +00:00
return;
}
2012-01-12 01:36:20 +00:00
2022-10-20 21:51:49 +00:00
$words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'blockem', 'words');
if ($words) {
2021-07-25 17:28:07 +00:00
$blockem_words = explode(',', $words);
2012-01-12 01:36:20 +00:00
}
DI::page()['htmlhead'] .= <<< EOT
2012-01-12 01:36:20 +00:00
<script>
function blockemBlock(author) {
$.get('blockem?block=' +author, function(data) {
location.reload(true);
});
}
function blockemUnblock(author) {
$.get('blockem?unblock=' +author, function(data) {
location.reload(true);
});
}
</script>
EOT;
}
function blockem_item_photo_menu(App $a, array &$b)
{
2021-07-25 17:28:07 +00:00
global $blockem_words;
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId() || $b['item']['self']) {
2012-01-12 01:52:53 +00:00
return;
}
2012-01-12 01:52:53 +00:00
2012-01-12 01:36:20 +00:00
$blocked = false;
$author = $b['item']['author-link'];
2021-07-25 17:28:07 +00:00
if (!empty($blockem_words)) {
foreach($blockem_words as $bloke) {
if (Strings::compareLink($bloke,$author)) {
2012-01-12 01:36:20 +00:00
$blocked = true;
break;
}
}
}
if ($blocked) {
$b['menu'][DI::l10n()->t('Unblock Author')] = 'javascript:blockemUnblock(\'' . $author . '\');';
} else {
$b['menu'][DI::l10n()->t('Block Author')] = 'javascript:blockemBlock(\'' . $author . '\');';
}
2012-01-12 01:36:20 +00:00
}
/**
* 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 blockem_module() {}
2012-01-12 01:36:20 +00:00
function blockem_init(App $a)
{
2022-10-20 21:51:49 +00:00
if (!DI::userSession()->getLocalUserId()) {
2012-01-12 01:36:20 +00:00
return;
}
2012-01-12 01:36:20 +00:00
2022-10-20 21:51:49 +00:00
$words = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'blockem', 'words');
2012-01-12 01:36:20 +00:00
if (array_key_exists('block', $_GET) && $_GET['block']) {
if (strlen($words)) {
2012-01-12 01:36:20 +00:00
$words .= ',';
}
2012-01-12 01:36:20 +00:00
$words .= trim($_GET['block']);
}
if (array_key_exists('unblock', $_GET) && $_GET['unblock']) {
2012-01-12 01:36:20 +00:00
$arr = explode(',',$words);
2018-01-15 13:15:33 +00:00
$newarr = [];
2012-01-12 01:36:20 +00:00
if (count($arr)) {
foreach ($arr as $x) {
if (!Strings::compareLink(trim($x), trim($_GET['unblock']))) {
2012-01-12 01:36:20 +00:00
$newarr[] = $x;
}
2012-01-12 01:36:20 +00:00
}
}
$words = implode(',', $newarr);
2012-01-12 01:36:20 +00:00
}
2022-10-20 21:51:49 +00:00
DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'blockem', 'words', $words);
2018-12-26 05:39:53 +00:00
exit();
}