]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / include / text.php
index 3b04c8663703a3f1f87a542d6493c85a6e46bf7a..66d4f17ea66ac5b20ef9c486f207d23dfa65e68a 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
 use Friendica\App;
+use Friendica\Core\System;
 
 require_once "include/template_processor.php";
 require_once "include/friendica_smarty.php";
@@ -24,7 +25,7 @@ function replace_macros($s, $r) {
        $a = get_app();
 
        // pass $baseurl to all templates
-       $r['$baseurl'] = App::get_baseurl();
+       $r['$baseurl'] = System::baseUrl();
 
        $t = $a->template_engine();
        try {
@@ -293,7 +294,7 @@ function paginate_data(App $a, $count = null) {
        $stripped = trim($stripped, '/');
        $pagenum = $a->pager['page'];
 
-       if (($a->page_offset != '') AND !preg_match('/[?&].offset=/', $stripped)) {
+       if (($a->page_offset != '') && !preg_match('/[?&].offset=/', $stripped)) {
                $stripped .= '&offset=' . urlencode($a->page_offset);
        }
 
@@ -488,8 +489,6 @@ if (! function_exists('item_new_uri')) {
 function item_new_uri($hostname, $uid, $guid = "") {
 
        do {
-               $dups = false;
-
                if ($guid == "") {
                        $hash = get_guid(32);
                } else {
@@ -499,11 +498,7 @@ function item_new_uri($hostname, $uid, $guid = "") {
 
                $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
 
-               $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1",
-                       dbesc($uri));
-               if (dbm::is_result($r)) {
-                       $dups = true;
-               }
+               $dups = dba::exists('item', array('uri' => $uri));
        } while ($dups == true);
 
        return $uri;
@@ -753,7 +748,7 @@ function logger($msg, $level = 0) {
 
        $callers = debug_backtrace();
        $logline = sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n",
-                       datetime_convert(),
+                       datetime_convert('UTC', 'UTC', 'now', 'Y-m-d\TH:i:s\Z'),
                        $process_id,
                        $LOGGER_LEVELS[$level],
                        basename($callers[0]['file']),
@@ -1276,8 +1271,8 @@ function redir_private_images($a, &$item)
 
 function put_item_in_cache(&$item, $update = false) {
 
-       if (($item["rendered-hash"] != hash("md5", $item["body"])) OR ($item["rendered-hash"] == "") OR
-               ($item["rendered-html"] == "") OR get_config("system", "ignore_cache")) {
+       if (($item["rendered-hash"] != hash("md5", $item["body"])) || ($item["rendered-hash"] == "") ||
+               ($item["rendered-html"] == "") || get_config("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.
@@ -1290,9 +1285,9 @@ function put_item_in_cache(&$item, $update = false) {
                $item["rendered-hash"] = hash("md5", $item["body"]);
                $item["body"] = $body;
 
-               if ($update AND ($item["id"] != 0)) {
-                       q("UPDATE `item` SET `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d",
-                               dbesc($item["rendered-html"]), dbesc($item["rendered-hash"]), intval($item["id"]));
+               if ($update && ($item["id"] > 0)) {
+                       dba::update('item', array('rendered-html' => $item["rendered-html"], 'rendered-hash' => $item["rendered-hash"]),
+                                       array('id' => $item["id"]), false);
                }
        }
 }
@@ -1317,23 +1312,29 @@ function prepare_body(&$item, $attach = false, $preview = false) {
        $a = get_app();
        call_hooks('prepare_body_init', $item);
 
-       $searchpath = z_root() . "/search?tag=";
+       $searchpath = System::baseUrl() . "/search?tag=";
 
        $tags = array();
        $hashtags = array();
        $mentions = array();
 
        if (!get_config('system','suppress_tags')) {
-               $taglist = q("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d) ORDER BY `tid`",
+               $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));
 
-               foreach ($taglist as $tag) {
-
+               while ($tag = dba::fetch($taglist)) {
                        if ($tag["url"] == "") {
                                $tag["url"] = $searchpath.strtolower($tag["term"]);
                        }
 
+                       $orig_tag = $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) {
@@ -1342,6 +1343,7 @@ function prepare_body(&$item, $attach = false, $preview = false) {
                        }
                        $tags[] = $prefix."<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
                }
+               dba::close($taglist);
        }
 
        $item['tags'] = $tags;
@@ -1352,8 +1354,9 @@ function prepare_body(&$item, $attach = false, $preview = false) {
        $update = (!local_user() and !remote_user() and ($item["uid"] == 0));
 
        // Or update it if the current viewer is the intented viewer
-       if (($item["uid"] == local_user()) AND ($item["uid"] != 0))
+       if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
                $update = true;
+       }
 
        put_item_in_cache($item, $update);
        $s = $item["rendered-html"];
@@ -1371,9 +1374,8 @@ function prepare_body(&$item, $attach = false, $preview = false) {
 
        $as = '';
        $vhead = false;
-       $arr = explode('[/attach],',$item['attach']);
+       $arr = explode('[/attach],', $item['attach']);
        if (count($arr)) {
-               $as .= '<div class="body-attach">';
                foreach ($arr as $r) {
                        $matches = false;
                        $icon = '';
@@ -1382,69 +1384,58 @@ 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))
+                                       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
+                                       } 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'), array(
-                                                               '$baseurl' => z_root(),
+                                                               '$baseurl' => System::baseUrl(),
                                                        ));
                                                        $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), array(
-                                                               '$baseurl' => z_root(),
+                                                               '$baseurl' => System::baseUrl(),
                                                        ));
                                                }
 
                                                $id = end(explode('/', $the_url));
                                                $as .= replace_macros(get_markup_template('video_top.tpl'), array(
-                                                       '$video'        => array(
-                                                               'id'       => $id,
-                                                               'title'         => t('View Video'),
-                                                               'src'           => $the_url,
-                                                               'mime'          => $mime,
+                                                       '$video' => array(
+                                                               'id'     => $id,
+                                                               'title'  => t('View Video'),
+                                                               'src'    => $the_url,
+                                                               'mime'   => $mime,
                                                        ),
                                                ));
                                        }
 
-                                       $filetype = strtolower(substr( $mime, 0, strpos($mime,'/') ));
+                                       $filetype = strtolower(substr($mime, 0, strpos($mime,'/')));
                                        if ($filetype) {
-                                               $filesubtype = strtolower(substr( $mime, strpos($mime,'/') + 1 ));
+                                               $filesubtype = strtolower(substr($mime, strpos($mime,'/') + 1));
                                                $filesubtype = str_replace('.', '-', $filesubtype);
                                        } else {
                                                $filetype = 'unkn';
                                                $filesubtype = 'unkn';
                                        }
 
-                                       $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
-                                       /*$icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
-                                       switch($icontype) {
-                                               case 'video':
-                                               case 'audio':
-                                               case 'image':
-                                               case 'text':
-                                                       $icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
-                                                       break;
-                                               default:
-                                                       $icon = '<div class="attachtype icon s22 type-unkn"></div>';
-                                                       break;
-                                       }*/
-
                                        $title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]));
                                        $title .= ' ' . $mtch[2] . ' ' . 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>';
                                }
                        }
                }
-               $as .= '<div class="clear"></div></div>';
        }
-       $s = $s . $as;
+       if ($as != '') {
+               $s .= '<div class="body-attach">'.$as.'<div class="clear"></div></div>';
+       }
 
        // map
-       if (strpos($s, '<div class="map">') !== false && $item['coord']) {
+       if (strpos($s, '<div class="map">') !== false && x($item, 'coord')) {
                $x = generate_map(trim($item['coord']));
                if ($x) {
                        $s = preg_replace('/\<div class\=\"map\"\>/','$0' . $x,$s);
@@ -1670,7 +1661,7 @@ function generate_user_guid() {
                if (! dbm::is_result($x)) {
                        $found = false;
                }
-       } while ($found == true );
+       } while ($found == true);
 
        return $guid;
 }
@@ -1934,14 +1925,13 @@ function file_tag_list_to_file($list,$type = 'file') {
                if ($type == 'file') {
                        $lbracket = '[';
                        $rbracket = ']';
-               }
-               else {
+               } else {
                        $lbracket = '<';
                        $rbracket = '>';
                }
 
                foreach ($list_array as $item) {
-                 if (strlen($item)) {
+                       if (strlen($item)) {
                                $tag_list .= $lbracket . file_tag_encode(trim($item))  . $rbracket;
                        }
                }
@@ -1960,8 +1950,9 @@ function file_tag_file_to_list($file,$type = 'file') {
        }
        if ($cnt) {
                foreach ($matches as $mtch) {
-                       if (strlen($list))
+                       if (strlen($list)) {
                                $list .= ',';
+                       }
                        $list .= file_tag_decode($mtch[1]);
                }
        }