X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Ftext.php;h=311422c5751d9af55f2399298a1b4543f94e0c50;hb=4724c7de72cd7bf40af52f0ca07fdf8313b922f4;hp=73792e53ce5874785317fb27d783c494b406dda3;hpb=5e7285b9ba3236e3c5b6163df323eefebbc8b20e;p=friendica.git diff --git a/include/text.php b/include/text.php index 73792e53ce..311422c575 100644 --- a/include/text.php +++ b/include/text.php @@ -7,14 +7,17 @@ use Friendica\App; use Friendica\Content\ContactSelector; use Friendica\Content\Feature; use Friendica\Content\Smilies; +use Friendica\Content\Text\BBCode; use Friendica\Core\Addon; use Friendica\Core\Config; 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\Model\Term; +use Friendica\Render\FriendicaSmarty; use Friendica\Util\DateTimeFormat; use Friendica\Util\Map; @@ -486,31 +489,6 @@ function item_new_uri($hostname, $uid, $guid = "") { return $uri; } - -/** - * Generate a guaranteed unique photo ID. - * safe from birthday paradox - * - * @return string - */ -function photo_new_resource() { - - do { - $found = false; - $resource = hash('md5',uniqid(mt_rand(),true)); - $r = q("SELECT `id` FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", - dbesc($resource) - ); - - if (DBM::is_result($r)) { - $found = true; - } - } while ($found == true); - - return $resource; -} - - /** * @deprecated * wrapper to load a view template, checking for alternate @@ -615,36 +593,6 @@ function get_markup_template($s, $root = '') { return $template; } - -/** - * - * @param App $a - * @param string $filename - * @param string $root - * @return string - */ -function get_template_file($a, $filename, $root = '') { - $theme = current_theme(); - - // Make sure $root ends with a slash / - if ($root !== '' && substr($root, -1, 1) !== '/') { - $root = $root . '/'; - } - - if (file_exists("{$root}view/theme/$theme/$filename")) { - $template_file = "{$root}view/theme/$theme/$filename"; - } elseif (x($a->theme_info, "extends") && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info["extends"], $filename))) { - $template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info["extends"], $filename); - } elseif (file_exists("{$root}/$filename")) { - $template_file = "{$root}/$filename"; - } else { - $template_file = "{$root}view/$filename"; - } - - return $template_file; -} - - /** * for html,xml parsing - let's say you've got * an attribute foobar="class1 class2 class3" @@ -767,12 +715,16 @@ function dlogger($msg, $level = 0) { return; } - $logfile = Config::get('system','dlogfile'); - + $logfile = Config::get('system', 'dlogfile'); if (! $logfile) { return; } + $dlogip = Config::get('system', 'dlogip'); + if (!is_null($dlogip) && $_SERVER['REMOTE_ADDR'] != $dlogip) { + return; + } + if (count($LOGGER_LEVELS) == 0) { foreach (get_defined_constants() as $k => $v) { if (substr($k, 0, 7) == "LOGGER_") { @@ -1110,7 +1062,7 @@ function linkify($s) { * Load poke verbs * * @return array index is present tense verb - value is array containing past tense verb, translation of present, translation of past + * value is array containing past tense verb, translation of present, translation of past * @hook poke_verbs pokes array */ function get_poke_verbs() { @@ -1217,8 +1169,25 @@ function redir_private_images($a, &$item) } } +/** + * Sets the "rendered-html" field of the provided item + * + * Body is preserved to avoid side-effects as we modify it just-in-time for spoilers and private image links + * + * @param array $item + * @param bool $update + * + * @todo Remove reference, simply return "rendered-html" and "rendered-hash" + */ 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 == '' @@ -1226,22 +1195,19 @@ function put_item_in_cache(&$item, $update = false) || $rendered_hash != hash("md5", $item["body"]) || Config::get("system", "ignore_cache") ) { - // The function "redir_private_images" changes the body. - // I'm not sure if we should store it permanently, so we save the old value. - $body = $item["body"]; - $a = get_app(); redir_private_images($a, $item); $item["rendered-html"] = prepare_text($item["body"]); $item["rendered-hash"] = hash("md5", $item["body"]); - $item["body"] = $body; if ($update && ($item["id"] > 0)) { dba::update('item', ['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]], ['id' => $item["id"]], false); } } + + $item["body"] = $body; } /** @@ -1269,37 +1235,37 @@ 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; @@ -1329,62 +1295,55 @@ function prepare_body(&$item, $attach = false, $preview = false) { $as = ''; $vhead = false; - $arr = explode('[/attach],', $item['attach']); - if (count($arr)) { - foreach ($arr as $r) { - $matches = false; - $icon = ''; - $cnt = preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r ,$matches, PREG_SET_ORDER); - if ($cnt) { - 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]; - } - - if (strpos($mime, 'video') !== false) { - if (!$vhead) { - $vhead = true; - $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), [ - '$baseurl' => System::baseUrl(), - ]); - $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), [ - '$baseurl' => System::baseUrl(), - ]); - } - - $id = end(explode('/', $the_url)); - $as .= replace_macros(get_markup_template('video_top.tpl'), [ - '$video' => [ - 'id' => $id, - 'title' => L10n::t('View Video'), - 'src' => $the_url, - 'mime' => $mime, - ], - ]); - } - - $filetype = strtolower(substr($mime, 0, strpos($mime, '/'))); - if ($filetype) { - $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1)); - $filesubtype = str_replace('.', '-', $filesubtype); - } else { - $filetype = 'unkn'; - $filesubtype = 'unkn'; - } - - $title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1])); - $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes'); - - $icon = '
'; - $as .= '' . $icon . ''; - } + $matches = []; + preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\"(?: title=\"(.*?)\")?|', $item['attach'], $matches, PREG_SET_ORDER); + 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]; + } + + if (strpos($mime, 'video') !== false) { + if (!$vhead) { + $vhead = true; + $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), [ + '$baseurl' => System::baseUrl(), + ]); + $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), [ + '$baseurl' => System::baseUrl(), + ]); } + + $id = end(explode('/', $the_url)); + $as .= replace_macros(get_markup_template('video_top.tpl'), [ + '$video' => [ + 'id' => $id, + 'title' => L10n::t('View Video'), + 'src' => $the_url, + 'mime' => $mime, + ], + ]); + } + + $filetype = strtolower(substr($mime, 0, strpos($mime, '/'))); + if ($filetype) { + $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1)); + $filesubtype = str_replace('.', '-', $filesubtype); + } else { + $filetype = 'unkn'; + $filesubtype = 'unkn'; } + + $title = escape_tags(trim(!empty($mtch[4]) ? $mtch[4] : $mtch[1])); + $title .= ' ' . $mtch[2] . ' ' . L10n::t('bytes'); + + $icon = '
'; + $as .= '' . $icon . ''; } + if ($as != '') { $s .= '
'.$as.'
'; } @@ -1447,13 +1406,10 @@ function prepare_body(&$item, $attach = false, $preview = false) { * @return string Formattet HTML. */ function prepare_text($text) { - - require_once 'include/bbcode.php'; - if (stristr($text, '[nosmile]')) { - $s = bbcode($text); + $s = BBCode::convert($text); } else { - $s = Smilies::replace(bbcode($text)); + $s = Smilies::replace(BBCode::convert($text)); } return trim($s); @@ -1923,16 +1879,10 @@ function file_tag_save_file($uid, $item, $file) intval($uid) ); if (DBM::is_result($r)) { - if (! stristr($r[0]['file'],'[' . file_tag_encode($file) . ']')) { - q("UPDATE `item` SET `file` = '%s' WHERE `id` = %d AND `uid` = %d", - dbesc($r[0]['file'] . '[' . file_tag_encode($file) . ']'), - intval($item), - intval($uid) - ); + if (!stristr($r[0]['file'],'[' . file_tag_encode($file) . ']')) { + $fields = ['file' => $r[0]['file'] . '[' . file_tag_encode($file) . ']']; + Item::update($fields, ['id' => $item]); } - - Term::createFromItem($item); - $saved = PConfig::get($uid, 'system', 'filetags'); if (!strlen($saved) || !stristr($saved, '[' . file_tag_encode($file) . ']')) { PConfig::set($uid, 'system', 'filetags', $saved . '[' . file_tag_encode($file) . ']'); @@ -1964,13 +1914,8 @@ function file_tag_unsave_file($uid, $item, $file, $cat = false) return false; } - q("UPDATE `item` SET `file` = '%s' WHERE `id` = %d AND `uid` = %d", - dbesc(str_replace($pattern,'',$r[0]['file'])), - intval($item), - intval($uid) - ); - - Term::createFromItem($item); + $fields = ['file' => str_replace($pattern,'',$r[0]['file'])]; + Item::update($fields, ['id' => $item]); $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d", dbesc($file), @@ -2085,6 +2030,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