]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Fix tests (#5400)
[friendica.git] / include / text.php
index f145c03e5306d7f129cfd710d9e2f58e4b9aa2ee..6aa0c9d2dd43208e33464c913d79253b5a04cf4c 100644 (file)
@@ -462,113 +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
- * languages before falling back to the default
- *
- * @global string $lang
- * @global App $a
- * @param string $s view name
- * @return string
- */
-function load_view_file($s) {
-       global $lang, $a;
-       if (! isset($lang)) {
-               $lang = 'en';
-       }
-       $b = basename($s);
-       $d = dirname($s);
-       if (file_exists("$d/$lang/$b")) {
-               $stamp1 = microtime(true);
-               $content = file_get_contents("$d/$lang/$b");
-               $a->save_timestamp($stamp1, "file");
-               return $content;
-       }
-
-       $theme = $a->getCurrentTheme();
-
-       if (file_exists("$d/theme/$theme/$b")) {
-               $stamp1 = microtime(true);
-               $content = file_get_contents("$d/theme/$theme/$b");
-               $a->save_timestamp($stamp1, "file");
-               return $content;
-       }
-
-       $stamp1 = microtime(true);
-       $content = file_get_contents($s);
-       $a->save_timestamp($stamp1, "file");
-       return $content;
-}
-
-
-/**
- * load a view template, checking for alternate
- * languages before falling back to the default
- *
- * @global string $lang
- * @param string $s view path
- * @return string
- */
-function get_intltext_template($s) {
-       global $lang;
-
-       $a = get_app();
-       $engine = '';
-       if ($a->theme['template_engine'] === 'smarty3') {
-               $engine = "/smarty3";
-       }
-
-       if (! isset($lang)) {
-               $lang = 'en';
-       }
-
-       if (file_exists("view/lang/$lang$engine/$s")) {
-               $stamp1 = microtime(true);
-               $content = file_get_contents("view/lang/$lang$engine/$s");
-               $a->save_timestamp($stamp1, "file");
-               return $content;
-       } elseif (file_exists("view/lang/en$engine/$s")) {
-               $stamp1 = microtime(true);
-               $content = file_get_contents("view/lang/en$engine/$s");
-               $a->save_timestamp($stamp1, "file");
-               return $content;
-       } else {
-               $stamp1 = microtime(true);
-               $content = file_get_contents("view$engine/$s");
-               $a->save_timestamp($stamp1, "file");
-               return $content;
-       }
-}
-
-
 /**
  * load template $s
  *
@@ -626,7 +519,6 @@ $LOGGER_LEVELS = [];
  * LOGGER_DATA
  * LOGGER_ALL
  *
- * @global App $a
  * @global array $LOGGER_LEVELS
  * @param string $msg
  * @param int $level
@@ -635,21 +527,13 @@ function logger($msg, $level = 0) {
        $a = get_app();
        global $LOGGER_LEVELS;
 
-       // turn off logger in install mode
-       if (
-               $a->mode == App::MODE_INSTALL
-               || !dba::$connected
-       ) {
-               return;
-       }
-
-       $debugging = Config::get('system','debugging');
-       $logfile   = Config::get('system','logfile');
-       $loglevel = intval(Config::get('system','loglevel'));
+       $debugging = Config::get('system', 'debugging');
+       $logfile   = Config::get('system', 'logfile');
+       $loglevel = intval(Config::get('system', 'loglevel'));
 
        if (
-               ! $debugging
-               || ! $logfile
+               !$debugging
+               || !$logfile
                || $level > $loglevel
        ) {
                return;
@@ -670,13 +554,20 @@ function logger($msg, $level = 0) {
        }
 
        $callers = debug_backtrace();
+
+       if (count($callers) > 1) {
+               $function = $callers[1]['function'];
+       } else {
+               $function = '';
+       }
+
        $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
                        DateTimeFormat::utcNow(DateTimeFormat::ATOM),
                        $process_id,
                        $LOGGER_LEVELS[$level],
                        basename($callers[0]['file']),
                        $callers[0]['line'],
-                       $callers[1]['function'],
+                       $function,
                        $msg
                );
 
@@ -698,25 +589,15 @@ function logger($msg, $level = 0) {
  * LOGGER_DATA
  * LOGGER_ALL
  *
- * @global App $a
  * @global array $LOGGER_LEVELS
  * @param string $msg
  * @param int $level
  */
-
 function dlogger($msg, $level = 0) {
        $a = get_app();
 
-       // turn off logger in install mode
-       if (
-               $a->mode == App::MODE_INSTALL
-               || !dba::$connected
-       ) {
-               return;
-       }
-
        $logfile = Config::get('system', 'dlogfile');
-       if (! $logfile) {
+       if (!$logfile) {
                return;
        }
 
@@ -736,7 +617,7 @@ function dlogger($msg, $level = 0) {
        $process_id = session_id();
 
        if ($process_id == '') {
-               $process_id = get_app()->process_id;
+               $process_id = $a->process_id;
        }
 
        $callers = debug_backtrace();
@@ -1011,7 +892,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
                ];
@@ -1157,7 +1038,7 @@ function redir_private_images($a, &$item)
                                continue;
                        }
 
-                       if ((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
+                       if ((local_user() == $item['uid']) && ($item['private'] == 1) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
                                $img_url = 'redir?f=1&quiet=1&url=' . urlencode($mtch[1]) . '&conurl=' . urlencode($item['author-link']);
                                $item['body'] = str_replace($mtch[0], '[img]' . $img_url . '[/img]', $item['body']);
                        }
@@ -1183,7 +1064,7 @@ function put_item_in_cache(&$item, $update = false)
        $rendered_html = defaults($item, 'rendered-html', '');
 
        if ($rendered_hash == ''
-               || $item["rendered-html"] == ""
+               || $rendered_html == ""
                || $rendered_hash != hash("md5", $item["body"])
                || Config::get("system", "ignore_cache")
        ) {
@@ -1193,6 +1074,12 @@ function put_item_in_cache(&$item, $update = false)
                $item["rendered-html"] = prepare_text($item["body"]);
                $item["rendered-hash"] = hash("md5", $item["body"]);
 
+               $hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
+               Addon::callHooks('put_item_in_cache', $hook_data);
+               $item['rendered-html'] = $hook_data['rendered-html'];
+               $item['rendered-hash'] = $hook_data['rendered-hash'];
+               unset($hook_data);
+
                // Force an update if the generated values differ from the existing ones
                if ($rendered_hash != $item["rendered-hash"]) {
                        $update = true;
@@ -1203,9 +1090,9 @@ function put_item_in_cache(&$item, $update = false)
                        $update = true;
                }
 
-               if ($update && ($item["id"] > 0)) {
-                       dba::update('item', ['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
-                                       ['id' => $item["id"]], false);
+               if ($update && !empty($item["id"])) {
+                       Item::update(['rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]],
+                                       ['id' => $item["id"]]);
                }
        }
 
@@ -1280,9 +1167,7 @@ function prepare_body(array &$item, $attach = false, $is_preview = false)
        $s = $hook_data['html'];
        unset($hook_data);
 
-       $s = apply_content_filter($s, $filter_reasons);
-
-       if (! $attach) {
+       if (!$attach) {
                // Replace the blockquotes with quotes that are used in mails.
                $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
                $s = str_replace(['<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'], [$mailquote, $mailquote, $mailquote], $s);
@@ -1309,7 +1194,8 @@ function prepare_body(array &$item, $attach = false, $is_preview = false)
                                ]);
                        }
 
-                       $id = end(explode('/', $the_url));
+                       $url_parts = explode('/', $the_url);
+                       $id = end($url_parts);
                        $as .= replace_macros(get_markup_template('video_top.tpl'), [
                                '$video' => [
                                        'id'     => $id,
@@ -1385,6 +1271,8 @@ function prepare_body(array &$item, $attach = false, $is_preview = false)
                $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
        }
 
+       $s = apply_content_filter($s, $filter_reasons);
+
        $hook_data = ['item' => $item, 'html' => $s];
        Addon::callHooks('prepare_body_final', $hook_data);
 
@@ -1576,11 +1464,11 @@ function return_bytes($size_str) {
 function generate_user_guid() {
        $found = true;
        do {
-               $guid = get_guid(32);
+               $guid = System::createGUID(32);
                $x = q("SELECT `uid` FROM `user` WHERE `guid` = '%s' LIMIT 1",
                        dbesc($guid)
                );
-               if (! DBM::is_result($x)) {
+               if (!DBM::is_result($x)) {
                        $found = false;
                }
        } while ($found == true);
@@ -1622,7 +1510,7 @@ function base64url_decode($s) {
  *  // Uncomment if you find you need it.
  *
  *     $l = strlen($s);
- *     if (! strpos($s,'=')) {
+ *     if (!strpos($s,'=')) {
  *             $m = $l % 4;
  *             if ($m == 2)
  *                     $s .= '==';
@@ -1737,11 +1625,11 @@ function reltoabs($text, $base) {
  * @return string
  */
 function item_post_type($item) {
-       if (intval($item['event-id'])) {
+       if (!empty($item['event-id'])) {
                return L10n::t('event');
-       } elseif (strlen($item['resource-id'])) {
+       } elseif (!empty($item['resource-id'])) {
                return L10n::t('photo');
-       } elseif (strlen($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
+       } elseif (!empty($item['verb']) && $item['verb'] !== ACTIVITY_POST) {
                return L10n::t('activity');
        } elseif ($item['id'] != $item['parent']) {
                return L10n::t('comment');
@@ -1845,7 +1733,7 @@ function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
                $check_new_tags = explode(",",file_tag_file_to_list($file_new,$type));
 
                foreach ($check_new_tags as $tag) {
-                       if (! stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket)) {
+                       if (!stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket)) {
                                $new_tags[] = $tag;
                        }
                }
@@ -1857,7 +1745,7 @@ function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
                $check_deleted_tags = explode(",",file_tag_file_to_list($file_old,$type));
 
                foreach ($check_deleted_tags as $tag) {
-                       if (! stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket)) {
+                       if (!stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket)) {
                                $deleted_tags[] = $tag;
                        }
                }
@@ -1886,20 +1774,17 @@ function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
        return true;
 }
 
-function file_tag_save_file($uid, $item, $file)
+function file_tag_save_file($uid, $item_id, $file)
 {
-       if (! intval($uid)) {
+       if (!intval($uid)) {
                return false;
        }
 
-       $r = q("SELECT `file` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-               intval($item),
-               intval($uid)
-       );
-       if (DBM::is_result($r)) {
-               if (!stristr($r[0]['file'],'[' . file_tag_encode($file) . ']')) {
-                       $fields = ['file' => $r[0]['file'] . '[' . file_tag_encode($file) . ']'];
-                       Item::update($fields, ['id' => $item]);
+       $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
+       if (DBM::is_result($item)) {
+               if (!stristr($item['file'],'[' . file_tag_encode($file) . ']')) {
+                       $fields = ['file' => $item['file'] . '[' . file_tag_encode($file) . ']'];
+                       Item::update($fields, ['id' => $item_id]);
                }
                $saved = PConfig::get($uid, 'system', 'filetags');
                if (!strlen($saved) || !stristr($saved, '[' . file_tag_encode($file) . ']')) {
@@ -1910,9 +1795,9 @@ function file_tag_save_file($uid, $item, $file)
        return true;
 }
 
-function file_tag_unsave_file($uid, $item, $file, $cat = false)
+function file_tag_unsave_file($uid, $item_id, $file, $cat = false)
 {
-       if (! intval($uid)) {
+       if (!intval($uid)) {
                return false;
        }
 
@@ -1924,16 +1809,13 @@ function file_tag_unsave_file($uid, $item, $file, $cat = false)
                $termtype = TERM_FILE;
        }
 
-       $r = q("SELECT `file` FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-               intval($item),
-               intval($uid)
-       );
-       if (! DBM::is_result($r)) {
+       $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
+       if (!DBM::is_result($item)) {
                return false;
        }
 
-       $fields = ['file' => str_replace($pattern,'',$r[0]['file'])];
-       Item::update($fields, ['id' => $item]);
+       $fields = ['file' => str_replace($pattern,'',$item['file'])];
+       Item::update($fields, ['id' => $item_id]);
 
        $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
                dbesc($file),
@@ -1997,6 +1879,7 @@ function is_a_date_arg($s) {
  */
 function deindent($text, $chr = "[\t ]", $count = NULL) {
        $lines = explode("\n", $text);
+
        if (is_null($count)) {
                $m = [];
                $k = 0;