/**
* @file include/text.php
*/
+
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;
require_once "mod/proxy.php";
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
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"
$callers = debug_backtrace();
$logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
- datetime_convert('UTC', 'UTC', 'now', 'Y-m-d\TH:i:s\Z'),
+ DateTimeFormat::utcNow(DateTimeFormat::ATOM),
$process_id,
$LOGGER_LEVELS[$level],
basename($callers[0]['file']),
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_") {
$callers = debug_backtrace();
$logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
- datetime_convert(),
+ DateTimeFormat::utcNow(),
$process_id,
basename($callers[0]['file']),
$callers[0]['line'],
* 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() {
}
}
+/**
+ * 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 == ''
|| $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;
}
/**
// 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[] = "#<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
- $prefix = "#";
- } elseif ($tag["type"] == TERM_MENTION) {
- $mentions[] = "@<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
- $prefix = "@";
+ if ($tag["type"] == TERM_HASHTAG) {
+ if ($orig_tag != $tag["url"]) {
+ $item['body'] = str_replace($orig_tag, $tag["url"], $item['body']);
}
- $tags[] = $prefix."<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
+
+ $hashtags[] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
+ $prefix = "#";
+ } elseif ($tag["type"] == TERM_MENTION) {
+ $mentions[] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
+ $prefix = "@";
}
- dba::close($taglist);
+
+ $tags[] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
}
+ dba::close($taglist);
$item['tags'] = $tags;
$item['hashtags'] = $hashtags;
$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 = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
- $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
- }
+ $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 = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
+ $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
}
+
if ($as != '') {
$s .= '<div class="body-attach">'.$as.'<div class="clear"></div></div>';
}
* @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);
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) . ']');
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),
$lang = 'javascript';
}
+ if ($lang === 'bash') {
+ $lang = 'sh';
+ }
+
// @TODO: Replace Text_Highlighter_Renderer_Html by scrivo/highlight.php
// Autoload the library to make constants available