]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Merge branch '3.6-rc' of https://github.com/Andi-K/friendica into 3.6-rc
[friendica.git] / include / text.php
index def479ce337ffb50a3e929c663cd1eaf7bb6cc6d..9fae74252bd0f0b1c22762e291a64f5413b887da 100644 (file)
@@ -7,18 +7,21 @@ 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\Item;
 use Friendica\Model\Profile;
-use Friendica\Model\Term;
+use Friendica\Render\FriendicaSmarty;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
-use Friendica\Util\Temporal;
 
 require_once "mod/proxy.php";
+require_once "include/event.php";
 require_once "include/conversation.php";
 
 /**
@@ -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"
@@ -723,7 +671,7 @@ function logger($msg, $level = 0) {
 
        $callers = debug_backtrace();
        $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
-                       Temporal::convert('now', 'UTC', 'UTC', Temporal::ATOM),
+                       DateTimeFormat::utcNow(DateTimeFormat::ATOM),
                        $process_id,
                        $LOGGER_LEVELS[$level],
                        basename($callers[0]['file']),
@@ -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_") {
@@ -789,7 +741,7 @@ function dlogger($msg, $level = 0) {
 
        $callers = debug_backtrace();
        $logline = sprintf("%s@\t%s:\t%s:\t%s\t%s\t%s\n",
-                       Temporal::convert(),
+                       DateTimeFormat::utcNow(),
                        $process_id,
                        basename($callers[0]['file']),
                        $callers[0]['line'],
@@ -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;
 }
 
 /**
@@ -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 = '<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>';
        }
@@ -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),