X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Ftext.php;h=ee8a213ff659bfa3aa238e740ac51138346d25b3;hb=abd7b2666c1ba79d44db0e71b369433cf8c8e678;hp=9fae74252bd0f0b1c22762e291a64f5413b887da;hpb=c845415a99ebc348103815a7b2c55b15c75cdd24;p=friendica.git diff --git a/include/text.php b/include/text.php index 9fae74252b..ee8a213ff6 100644 --- a/include/text.php +++ b/include/text.php @@ -14,6 +14,7 @@ use Friendica\Core\L10n; use Friendica\Core\PConfig; use Friendica\Core\System; use Friendica\Database\DBM; +use Friendica\Model\Event; use Friendica\Model\Item; use Friendica\Model\Profile; use Friendica\Render\FriendicaSmarty; @@ -21,7 +22,6 @@ use Friendica\Util\DateTimeFormat; use Friendica\Util\Map; require_once "mod/proxy.php"; -require_once "include/event.php"; require_once "include/conversation.php"; /** @@ -1183,12 +1183,8 @@ 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', ''); + $rendered_html = defaults($item, 'rendered-html', ''); if ($rendered_hash == '' || $item["rendered-html"] == "" @@ -1201,6 +1197,16 @@ function put_item_in_cache(&$item, $update = false) $item["rendered-html"] = prepare_text($item["body"]); $item["rendered-hash"] = hash("md5", $item["body"]); + // Force an update if the generated values differ from the existing ones + if ($rendered_hash != $item["rendered-hash"]) { + $update = true; + } + + // Only compare the HTML when we forcefully ignore the cache + if (Config::get("system", "ignore_cache") && ($rendered_html != $item["rendered-html"])) { + $update = true; + } + if ($update && ($item["id"] > 0)) { dba::update('item', ['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]], ['id' => $item["id"]], false); @@ -1214,15 +1220,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); @@ -1235,42 +1243,58 @@ function prepare_body(&$item, $attach = false, $preview = false) { // In order to provide theme developers more possibilities, event items // are treated differently. if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) { - $ev = format_event_item($item); + $ev = Event::getItemHTML($item); return $ev; } - if (!Config::get('system','suppress_tags')) { - $taglist = dba::p("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = ? AND `oid` = ? AND `type` IN (?, ?) ORDER BY `tid`", - intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION)); + $taglist = dba::p("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = ? AND `oid` = ? AND `type` IN (?, ?) ORDER BY `tid`", + intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION)); - while ($tag = dba::fetch($taglist)) { - if ($tag["url"] == "") { - $tag["url"] = $searchpath.strtolower($tag["term"]); - } + while ($tag = dba::fetch($taglist)) { + if ($tag["url"] == "") { + $tag["url"] = $searchpath . strtolower($tag["term"]); + } - $orig_tag = $tag["url"]; + $orig_tag = $tag["url"]; - $tag["url"] = best_link_url($item, $sp, $tag["url"]); + $tag["url"] = best_link_url($item, $sp, $tag["url"]); - if ($tag["type"] == TERM_HASHTAG) { - if ($orig_tag != $tag["url"]) { - $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']); - } - $hashtags[] = "#".$tag["term"].""; - $prefix = "#"; - } elseif ($tag["type"] == TERM_MENTION) { - $mentions[] = "@".$tag["term"].""; - $prefix = "@"; + if ($tag["type"] == TERM_HASHTAG) { + if ($orig_tag != $tag["url"]) { + $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']); } - $tags[] = $prefix."".$tag["term"].""; + + $hashtags[] = "#" . $tag["term"] . ""; + $prefix = "#"; + } elseif ($tag["type"] == TERM_MENTION) { + $mentions[] = "@" . $tag["term"] . ""; + $prefix = "@"; } - dba::close($taglist); + + $tags[] = $prefix . "" . $tag["term"] . ""; } + dba::close($taglist); $item['tags'] = $tags; $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 +1306,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 +1425,39 @@ function prepare_body(&$item, $attach = false, $preview = false) { $s = preg_replace('|(]+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 $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 = ' +

' . + L10n::t('Click to open/close') . + '

+ '; + } - return $prep_arr['html']; + return $html; } /** @@ -2030,6 +2091,10 @@ function text_highlight($s, $lang) { $lang = 'javascript'; } + if ($lang === 'bash') { + $lang = 'sh'; + } + // @TODO: Replace Text_Highlighter_Renderer_Html by scrivo/highlight.php // Autoload the library to make constants available