Merge pull request #4722 from MrPetovan/task/add-content_filter-hook

Add content_filter hook
This commit is contained in:
Michael Vogel 2018-04-06 06:42:52 +02:00 committed by GitHub
commit 05cff59f70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 6162 additions and 6062 deletions

View file

@ -756,7 +756,13 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
list($categories, $folders) = get_cats_and_terms($item);
$profile_name_e = $profile_name;
$item['title_e'] = $item['title'];
if (!empty($item['content-warning']) && PConfig::get(local_user(), 'system', 'disable_cw', false)) {
$title_e = ucfirst($item['content-warning']);
} else {
$title_e = $item['title'];
}
$body_e = $body;
$tags_e = $tags;
$hashtags_e = $hashtags;
@ -781,7 +787,7 @@ function conversation(App $a, $items, $mode, $update, $preview = false, $order =
'sparkle' => $sparkle,
'lock' => $lock,
'thumb' => System::removedBaseUrl(proxy_url($item['author-thumb'], false, PROXY_SIZE_THUMB)),
'title' => $item['title_e'],
'title' => $title_e,
'body' => $body_e,
'tags' => $tags_e,
'hashtags' => $hashtags_e,

View file

@ -1183,11 +1183,6 @@ function put_item_in_cache(&$item, $update = false)
{
$body = $item["body"];
// Add the content warning
if (!empty($item['content-warning'])) {
$item["body"] = $item['content-warning'] . '[spoiler]' . $item["body"] . '[/spoiler]';
}
$rendered_hash = defaults($item, 'rendered-hash', '');
if ($rendered_hash == ''
@ -1214,15 +1209,17 @@ function put_item_in_cache(&$item, $update = false)
* @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
* If attach is true, also add icons for item attachments.
*
* @param array $item
* @param array $item
* @param boolean $attach
* @param boolean $is_preview
* @return string item body html
* @hook prepare_body_init item array before any work
* @hook prepare_body ('item'=>item array, 'html'=>body string) after first bbcode to html
* @hook prepare_body_content_filter ('item'=>item array, 'filter_reasons'=>string array) before first bbcode to html
* @hook prepare_body ('item'=>item array, 'html'=>body string, 'is_preview'=>boolean, 'filter_reasons'=>string array) after first bbcode to html
* @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
*/
function prepare_body(&$item, $attach = false, $preview = false) {
function prepare_body(array &$item, $attach = false, $is_preview = false)
{
$a = get_app();
Addon::callHooks('prepare_body_init', $item);
@ -1271,6 +1268,22 @@ function prepare_body(&$item, $attach = false, $preview = false) {
$item['hashtags'] = $hashtags;
$item['mentions'] = $mentions;
// Compile eventual content filter reasons
$filter_reasons = [];
if (!$is_preview && !($item['self'] && local_user() == $item['uid'])) {
if (!empty($item['content-warning']) && (!local_user() || !PConfig::get(local_user(), 'system', 'disable_cw', false))) {
$filter_reasons[] = L10n::t('Content warning: %s', $item['content-warning']);
}
$hook_data = [
'item' => $item,
'filter_reasons' => $filter_reasons
];
Addon::callHooks('prepare_body_content_filter', $hook_data);
$filter_reasons = $hook_data['filter_reasons'];
unset($hook_data);
}
// Update the cached values if there is no "zrl=..." on the links.
$update = (!local_user() && !remote_user() && ($item["uid"] == 0));
@ -1282,9 +1295,17 @@ function prepare_body(&$item, $attach = false, $preview = false) {
put_item_in_cache($item, $update);
$s = $item["rendered-html"];
$prep_arr = ['item' => $item, 'html' => $s, 'preview' => $preview];
Addon::callHooks('prepare_body', $prep_arr);
$s = $prep_arr['html'];
$hook_data = [
'item' => $item,
'html' => $s,
'preview' => $is_preview,
'filter_reasons' => $filter_reasons
];
Addon::callHooks('prepare_body', $hook_data);
$s = $hook_data['html'];
unset($hook_data);
$s = apply_content_filter($s, $filter_reasons);
if (! $attach) {
// Replace the blockquotes with quotes that are used in mails.
@ -1393,10 +1414,39 @@ function prepare_body(&$item, $attach = false, $preview = false) {
$s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
}
$prep_arr = ['item' => $item, 'html' => $s];
Addon::callHooks('prepare_body_final', $prep_arr);
$hook_data = ['item' => $item, 'html' => $s];
Addon::callHooks('prepare_body_final', $hook_data);
return $prep_arr['html'];
return $hook_data['html'];
}
/**
* Given a HTML text and a set of filtering reasons, adds a content hiding header with the provided reasons
*
* Reasons are expected to have been translated already.
*
* @param string $html
* @param array $reasons
* @return string
*/
function apply_content_filter($html, array $reasons)
{
if (count($reasons)) {
$rnd = random_string(8);
$content_filter_html = '<ul class="content-filter-reasons">';
foreach ($reasons as $reason) {
$content_filter_html .= '<li>' . htmlspecialchars($reason) . '</li>' . PHP_EOL;
}
$content_filter_html .= '</ul>
<p><span id="content-filter-wrap-' . $rnd . '" class="fakelink content-filter-button" onclick=openClose(\'content-filter-' . $rnd . '\'); >' .
L10n::t('Click to open/close') .
'</span></p>
<div id="content-filter-' . $rnd . '" class="content-filter-content" style="display: none;">';
$html = $content_filter_html . $html . '</div>';
}
return $html;
}
/**