X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Ftext.php;h=10f626458665c7beb19e2d23d970f924387e7857;hb=4da9395075e13553d200ced03ffc104743d9bc98;hp=67ce7e65e83df258ae041296b951478453066ca6;hpb=bfc2bda9b606b424e4dfc1053af1ecea116fb1e2;p=friendica.git diff --git a/include/text.php b/include/text.php index 67ce7e65e8..10f6264586 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\Contact; use Friendica\Model\Event; use Friendica\Model\Item; use Friendica\Model\Profile; @@ -151,7 +152,7 @@ function autoname($len) { 'nd','ng','nk','nt','rn','rp','rt']; $noend = ['bl', 'br', 'cl','cr','dr','fl','fr','gl','gr', - 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh']; + 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh','q']; $start = mt_rand(0,2); if ($start == 0) { @@ -177,14 +178,13 @@ function autoname($len) { $word = substr($word,0,$len); foreach ($noend as $noe) { - if ((strlen($word) > 2) && (substr($word, -2) == $noe)) { - $word = substr($word, 0, -1); + $noelen = strlen($noe); + if ((strlen($word) > $noelen) && (substr($word, -$noelen) == $noe)) { + $word = autoname($len); break; } } - if (substr($word, -1) == 'q') { - $word = substr($word, 0, -1); - } + return $word; } @@ -452,7 +452,7 @@ function perms2str($p) { if (is_array($p)) { $tmp = $p; } else { - $tmp = explode(',',$p); + $tmp = explode(',', $p); } if (is_array($tmp)) { @@ -462,33 +462,6 @@ function perms2str($p) { return $ret; } - -/** - * generate a guaranteed unique (for this domain) item ID for ATOM - * safe from birthday paradox - * - * @param string $hostname - * @param int $uid - * @return string - */ -function item_new_uri($hostname, $uid, $guid = "") { - - do { - if ($guid == "") { - $hash = get_guid(32); - } else { - $hash = $guid; - $guid = ""; - } - - $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash; - - $dups = dba::exists('item', ['uri' => $uri]); - } while ($dups == true); - - return $uri; -} - /** * @deprecated * wrapper to load a view template, checking for alternate @@ -513,7 +486,7 @@ function load_view_file($s) { return $content; } - $theme = current_theme(); + $theme = $a->getCurrentTheme(); if (file_exists("$d/theme/$theme/$b")) { $stamp1 = microtime(true); @@ -637,7 +610,7 @@ function logger($msg, $level = 0) { // turn off logger in install mode if ( - $a->module == 'install' + $a->mode == App::MODE_INSTALL || !dba::$connected ) { return; @@ -709,7 +682,7 @@ function dlogger($msg, $level = 0) { // turn off logger in install mode if ( - $a->module == 'install' + $a->mode == App::MODE_INSTALL || !dba::$connected ) { return; @@ -961,13 +934,9 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) { $redir = false; if ($redirect) { - $redirect_url = 'redir/' . $contact['id']; - if (local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === NETWORK_DFRN)) { - $redir = true; - $url = $redirect_url; + $url = Contact::magicLink($contact['url']); + if (strpos($url, 'redir/') === 0) { $sparkle = ' sparkle'; - } else { - $url = Profile::zrl($url); } } @@ -1015,7 +984,7 @@ function search($s, $id = 'search-box', $url = 'search', $save = false, $aside = '$action_url' => $url, '$search_label' => L10n::t('Search'), '$save_label' => $save_label, - '$savedsearch' => Feature::isEnabled(local_user(),'savedsearch'), + '$savedsearch' => local_user() && Feature::isEnabled(local_user(),'savedsearch'), '$search_hint' => L10n::t('@name, !forum, #tags, content'), '$mode' => $mode ]; @@ -1183,12 +1152,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,9 +1166,19 @@ 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); + Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]], + ['id' => $item["id"]]); } } @@ -1214,24 +1189,20 @@ 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); - $searchpath = System::baseUrl() . "/search?tag="; - - $tags = []; - $hashtags = []; - $mentions = []; - // In order to provide theme developers more possibilities, event items // are treated differently. if ($item['object-type'] === ACTIVITY_OBJ_EVENT && isset($item['event-id'])) { @@ -1239,37 +1210,27 @@ function prepare_body(&$item, $attach = false, $preview = false) { return $ev; } - $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)); + $tags = \Friendica\Model\Term::populateTagsFromItem($item); - while ($tag = dba::fetch($taglist)) { - if ($tag["url"] == "") { - $tag["url"] = $searchpath . strtolower($tag["term"]); - } - - $orig_tag = $tag["url"]; + $item['tags'] = $tags['tags']; + $item['hashtags'] = $tags['hashtags']; + $item['mentions'] = $tags['mentions']; - $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 = "@"; + // Compile eventual content filter reasons + $filter_reasons = []; + if (!$is_preview && public_contact() != $item['author-id']) { + 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']); } - $tags[] = $prefix . "" . $tag["term"] . ""; + $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); } - dba::close($taglist); - - $item['tags'] = $tags; - $item['hashtags'] = $hashtags; - $item['mentions'] = $mentions; // Update the cached values if there is no "zrl=..." on the links. $update = (!local_user() && !remote_user() && ($item["uid"] == 0)); @@ -1282,9 +1243,15 @@ 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); if (! $attach) { // Replace the blockquotes with quotes that are used in mails. @@ -1300,11 +1267,7 @@ function prepare_body(&$item, $attach = false, $preview = false) { foreach ($matches as $mtch) { $mime = $mtch[3]; - if ((local_user() == $item['uid']) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) { - $the_url = 'redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1]; - } else { - $the_url = $mtch[1]; - } + $the_url = Contact::magicLinkById($item['author-id'], $mtch[1]); if (strpos($mime, 'video') !== false) { if (!$vhead) { @@ -1393,10 +1356,36 @@ 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); + $s = apply_content_filter($s, $filter_reasons); - return $prep_arr['html']; + $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)) { + $tpl = get_markup_template('wall/content_filter.tpl'); + $html = replace_macros($tpl, [ + '$reasons' => $reasons, + '$rnd' => random_string(8), + '$openclose' => L10n::t('Click to open/close'), + '$html' => $html + ]); + } + + return $html; } /** @@ -1636,10 +1625,11 @@ function bb_translate_video($s) { $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER); if ($r) { foreach ($matches as $mtch) { - if ((stristr($mtch[1],'youtube')) || (stristr($mtch[1],'youtu.be'))) - $s = str_replace($mtch[0],'[youtube]' . $mtch[1] . '[/youtube]',$s); - elseif (stristr($mtch[1],'vimeo')) - $s = str_replace($mtch[0],'[vimeo]' . $mtch[1] . '[/vimeo]',$s); + if ((stristr($mtch[1], 'youtube')) || (stristr($mtch[1], 'youtu.be'))) { + $s = str_replace($mtch[0], '[youtube]' . $mtch[1] . '[/youtube]', $s); + } elseif (stristr($mtch[1], 'vimeo')) { + $s = str_replace($mtch[0], '[vimeo]' . $mtch[1] . '[/vimeo]', $s); + } } } return $s; @@ -1757,7 +1747,7 @@ function file_tag_file_query($table,$s,$type = 'file') { } // ex. given music,video return