]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Merge pull request #3788 from Alkarex/denmark_regions
[friendica.git] / include / text.php
index d1ccb3c96cd9e354e41829f45d8636eed355a16a..8f0ff6e73e584153fce9fa13a318181bb55495e8 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);
        }
 
@@ -425,9 +426,9 @@ function expand_acl($s) {
        $ret = array();
 
        if (strlen($s)) {
-               $t = str_replace('<','',$s);
-               $a = explode('>',$t);
-               foreach($a as $aa) {
+               $t = str_replace('<', '', $s);
+               $a = explode('>', $t);
+               foreach ($a as $aa) {
                        if (intval($aa)) {
                                $ret[] = intval($aa);
                        }
@@ -442,10 +443,11 @@ if (! function_exists('sanitise_acl')) {
  * @param string $item
  */
 function sanitise_acl(&$item) {
-       if (intval($item))
+       if (intval($item)) {
                $item = '<' . intval(notags(trim($item))) . '>';
-       else
+       } else {
                unset($item);
+       }
 }}
 
 
@@ -461,14 +463,15 @@ if (! function_exists('perms2str')) {
  */
 function perms2str($p) {
        $ret = '';
-       if (is_array($p))
+       if (is_array($p)) {
                $tmp = $p;
-       else
+       } else {
                $tmp = explode(',',$p);
+       }
 
        if (is_array($tmp)) {
-               array_walk($tmp,'sanitise_acl');
-               $ret = implode('',$tmp);
+               array_walk($tmp, 'sanitise_acl');
+               $ret = implode('', $tmp);
        }
        return $ret;
 }}
@@ -486,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 {
@@ -497,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;
@@ -652,14 +649,14 @@ function get_template_file($a, $filename, $root = '') {
        $theme = current_theme();
 
        // Make sure $root ends with a slash /
-       if ($root !== '' && $root[strlen($root) - 1] !== '/') {
+       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("{$root}view/theme/{$a->theme_info[\"extends\"]}/$filename")) {
-               $template_file = "{$root}view/theme/{$a->theme_info[\"extends\"]}/$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 {
@@ -670,11 +667,6 @@ function get_template_file($a, $filename, $root = '') {
 }}
 
 
-
-
-
-
-
 if (! function_exists('attribute_contains')) {
 /**
  *  for html,xml parsing - let's say you've got
@@ -710,20 +702,18 @@ $LOGGER_LEVELS = array();
  * LOGGER_ALL
  *
  * @global App $a
- * @global dba $db
  * @global array $LOGGER_LEVELS
  * @param string $msg
  * @param int $level
  */
 function logger($msg, $level = 0) {
        $a = get_app();
-       global $db;
        global $LOGGER_LEVELS;
 
        // turn off logger in install mode
        if (
                $a->module == 'install'
-               || ! ($db && $db->connected)
+               || !dba::$connected
        ) {
                return;
        }
@@ -756,7 +746,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']),
@@ -784,7 +774,6 @@ function logger($msg, $level = 0) {
  * LOGGER_ALL
  *
  * @global App $a
- * @global dba $db
  * @global array $LOGGER_LEVELS
  * @param string $msg
  * @param int $level
@@ -792,12 +781,11 @@ function logger($msg, $level = 0) {
 
 function dlogger($msg, $level = 0) {
        $a = get_app();
-       global $db;
 
        // turn off logger in install mode
        if (
                $a->module == 'install'
-               || ! ($db && $db->connected)
+               || !dba::$connected
        ) {
                return;
        }
@@ -1203,12 +1191,11 @@ function get_mood_verbs() {
        return $arr;
 }
 
-if (! function_exists('day_translate')) {
 /**
- * Translate days and months names
- *
- * @param string $s
- * @return string
+ * @brief Translate days and months names.
+ * 
+ * @param string $s String with day or month name.
+ * @return string Translated string.
  */
 function day_translate($s) {
        $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
@@ -1220,8 +1207,23 @@ function day_translate($s) {
                $ret);
 
        return $ret;
-}}
+}
 
+/**
+ * @brief Translate short days and months names.
+ * 
+ * @param string $s String with short day or month name.
+ * @return string Translated string.
+ */
+function day_short_translate($s) {
+       $ret = str_replace(array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'),
+               array(t('Mon'), t('Tue'), t('Wed'), t('Thu'), t('Fri'), t('Sat'), t('Sund')),
+               $s);
+       $ret = str_replace(array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov','Dec'),
+               array(t('Jan'), t('Feb'), t('Mar'), t('Apr'), t('May'), ('Jun'), t('Jul'), t('Aug'), t('Sep'), t('Oct'), t('Nov'), t('Dec')),
+               $ret);
+       return $ret;
+}
 
 if (! function_exists('normalise_link')) {
 /**
@@ -1249,7 +1251,7 @@ if (! function_exists('link_compare')) {
  * @return boolean True if the URLs match, otherwise False
  *
  */
-function link_compare($a,$b) {
+function link_compare($a, $b) {
        return (strcasecmp(normalise_link($a), normalise_link($b)) === 0);
 }}
 
@@ -1279,8 +1281,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.
@@ -1293,20 +1295,16 @@ 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);
                }
        }
 }
 
-// Given an item array, convert the body element from bbcode to html and add smilie icons.
-// If attach is true, also add icons for item attachments
-
-if (! function_exists('prepare_body')) {
 /**
- * Given an item array, convert the body element from bbcode to html and add smilie icons.
- * If attach is true, also add icons for item attachments
+ * @brief Given an item array, convert the body element from bbcode to html and add smilie icons.
+ * If attach is true, also add icons for item attachments.
  *
  * @param array $item
  * @param boolean $attach
@@ -1315,28 +1313,41 @@ if (! function_exists('prepare_body')) {
  * @hook prepare_body ('item'=>item array, 'html'=>body string) after first bbcode to html
  * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
  */
-function prepare_body(&$item,$attach = false, $preview = false) {
+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();
+       $tags = array();
        $hashtags = array();
        $mentions = array();
 
+       // 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);
+               return $ev;
+       }
+
        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) {
@@ -1345,18 +1356,20 @@ 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;
        $item['hashtags'] = $hashtags;
        $item['mentions'] = $mentions;
 
-       // Update the cached values if there is no "zrl=..." on the links
-       $update = (!local_user() and !remote_user() and ($item["uid"] == 0));
+       // Update the cached values if there is no "zrl=..." on the links.
+       $update = (!local_user() && !remote_user() && ($item["uid"] == 0));
 
-       // Or update it if the current viewer is the intented viewer
-       if (($item["uid"] == local_user()) AND ($item["uid"] != 0))
+       // Or update it if the current viewer is the intented viewer.
+       if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
                $update = true;
+       }
 
        put_item_in_cache($item, $update);
        $s = $item["rendered-html"];
@@ -1366,7 +1379,7 @@ function prepare_body(&$item,$attach = false, $preview = false) {
        $s = $prep_arr['html'];
 
        if (! $attach) {
-               // Replace the blockquotes with quotes that are used in mails
+               // 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(array('<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'), array($mailquote, $mailquote, $mailquote), $s);
                return $s;
@@ -1374,92 +1387,79 @@ 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 = '';
-                       $cnt = preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"|',$r,$matches, PREG_SET_ORDER);
+                       $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))
+                                       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 {
+                                       } 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']) {
+       // Map.
+       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);
+                       $s = preg_replace('/\<div class\=\"map\"\>/', '$0' . $x, $s);
                }
        }
 
 
-       // Look for spoiler
+       // Look for spoiler.
        $spoilersearch = '<blockquote class="spoiler">';
 
-       // Remove line breaks before the spoiler
+       // Remove line breaks before the spoiler.
        while ((strpos($s, "\n" . $spoilersearch) !== false)) {
                $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
        }
@@ -1475,7 +1475,7 @@ function prepare_body(&$item,$attach = false, $preview = false) {
                $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
        }
 
-       // Look for quote with author
+       // Look for quote with author.
        $authorsearch = '<blockquote class="author">';
 
        while ((strpos($s, $authorsearch) !== false)) {
@@ -1486,7 +1486,7 @@ function prepare_body(&$item,$attach = false, $preview = false) {
                $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
        }
 
-       // replace friendica image url size with theme preference
+       // Replace friendica image url size with theme preference.
        if (x($a->theme_info, 'item_image_size')){
                $ps = $a->theme_info['item_image_size'];
                $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "$1-" . $ps, $s);
@@ -1496,15 +1496,13 @@ function prepare_body(&$item,$attach = false, $preview = false) {
        call_hooks('prepare_body_final', $prep_arr);
 
        return $prep_arr['html'];
-}}
-
+}
 
-if (! function_exists('prepare_text')) {
 /**
- * Given a text string, convert from bbcode to html and add smilie icons.
+ * @brief Given a text string, convert from bbcode to html and add smilie icons.
  *
- * @param string $text
- * @return string
+ * @param string $text String with bbcode.
+ * @return string Formattet HTML.
  */
 function prepare_text($text) {
 
@@ -1517,9 +1515,7 @@ function prepare_text($text) {
        }
 
        return trim($s);
-}}
-
-
+}
 
 /**
  * return array with details for categories and folders for an item
@@ -1554,8 +1550,9 @@ function get_cats_and_terms($item) {
        $categories = array();
        $folders = array();
 
-       $matches = false; $first = true;
-       $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
+       $matches = false;
+       $first = true;
+       $cnt = preg_match_all('/<(.*?)>/', $item['file'], $matches, PREG_SET_ORDER);
        if ($cnt) {
                foreach ($matches as $mtch) {
                        $categories[] = array(
@@ -1574,8 +1571,9 @@ function get_cats_and_terms($item) {
        }
 
        if (local_user() == $item['uid']) {
-               $matches = false; $first = true;
-               $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
+               $matches = false;
+               $first = true;
+               $cnt = preg_match_all('/\[(.*?)\]/', $item['file'], $matches, PREG_SET_ORDER);
                if ($cnt) {
                        foreach ($matches as $mtch) {
                                $folders[] = array(
@@ -1630,9 +1628,7 @@ function get_plink($item) {
                $ret = array();
        }
 
-       //if (x($item,'plink') && ($item['private'] != 1))
-
-       return($ret);
+       return $ret;
 }}
 
 if (! function_exists('unamp')) {
@@ -1674,7 +1670,7 @@ function generate_user_guid() {
                if (! dbm::is_result($x)) {
                        $found = false;
                }
-       } while ($found == true );
+       } while ($found == true);
 
        return $guid;
 }
@@ -1917,15 +1913,16 @@ function file_tag_encode($s) {
 }
 
 function file_tag_decode($s) {
-       return str_replace(array('%3c','%3e','%5b','%5d'),array('<','>','[',']'),$s);
+       return str_replace(array('%3c', '%3e', '%5b', '%5d'), array('<', '>', '[', ']'), $s);
 }
 
 function file_tag_file_query($table,$s,$type = 'file') {
 
-       if ($type == 'file')
-               $str = preg_quote( '[' . str_replace('%','%%',file_tag_encode($s)) . ']' );
-       else
-               $str = preg_quote( '<' . str_replace('%','%%',file_tag_encode($s)) . '>' );
+       if ($type == 'file') {
+               $str = preg_quote( '[' . str_replace('%', '%%', file_tag_encode($s)) . ']' );
+       } else {
+               $str = preg_quote( '<' . str_replace('%', '%%', file_tag_encode($s)) . '>' );
+       }
        return " AND " . (($table) ? dbesc($table) . '.' : '') . "file regexp '" . dbesc($str) . "' ";
 }
 
@@ -1937,14 +1934,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;
                        }
                }
@@ -1957,15 +1953,15 @@ function file_tag_file_to_list($file,$type = 'file') {
        $matches = false;
        $list = '';
        if ($type == 'file') {
-               $cnt = preg_match_all('/\[(.*?)\]/',$file,$matches,PREG_SET_ORDER);
-       }
-       else {
-               $cnt = preg_match_all('/<(.*?)>/',$file,$matches,PREG_SET_ORDER);
+               $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
+       } else {
+               $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
        }
        if ($cnt) {
                foreach ($matches as $mtch) {
-                       if (strlen($list))
+                       if (strlen($list)) {
                                $list .= ',';
+                       }
                        $list .= file_tag_decode($mtch[1]);
                }
        }
@@ -1973,7 +1969,7 @@ function file_tag_file_to_list($file,$type = 'file') {
        return $list;
 }
 
-function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
+function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') {
        // $file_old - categories previously associated with an item
        // $file_new - new list of categories for an item
 
@@ -2025,10 +2021,6 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
                                intval($termtype),
                                intval($uid));
 
-                       //$r = q("select file from item where uid = %d " . file_tag_file_query('item',$tag,$type),
-                       //      intval($uid)
-                       //);
-
                        if (dbm::is_result($r)) {
                                unset($deleted_tags[$key]);
                        }
@@ -2038,19 +2030,19 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
                }
 
                if ($saved != $filetags_updated) {
-                       set_pconfig($uid,'system','filetags', $filetags_updated);
+                       set_pconfig($uid, 'system', 'filetags', $filetags_updated);
                }
                return true;
        }
        else
                if (strlen($file_new)) {
-                       set_pconfig($uid,'system','filetags', $file_new);
+                       set_pconfig($uid, 'system', 'filetags', $file_new);
                }
                return true;
 }
 
-function file_tag_save_file($uid,$item,$file) {
-       require_once("include/files.php");
+function file_tag_save_file($uid, $item, $file) {
+       require_once "include/files.php";
 
        $result = false;
        if (! intval($uid))
@@ -2060,25 +2052,27 @@ function file_tag_save_file($uid,$item,$file) {
                intval($uid)
        );
        if (dbm::is_result($r)) {
-               if (! stristr($r[0]['file'],'[' . file_tag_encode($file) . ']'))
+               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)
                        );
+               }
 
                create_files_from_item($item);
 
                $saved = get_pconfig($uid,'system','filetags');
-               if ((! strlen($saved)) || (! stristr($saved,'[' . file_tag_encode($file) . ']')))
-                       set_pconfig($uid,'system','filetags',$saved . '[' . file_tag_encode($file) . ']');
+               if ((! strlen($saved)) || (! stristr($saved, '[' . file_tag_encode($file) . ']'))) {
+                       set_pconfig($uid, 'system', 'filetags', $saved . '[' . file_tag_encode($file) . ']');
+               }
                info( t('Item filed') );
        }
        return true;
 }
 
-function file_tag_unsave_file($uid,$item,$file,$cat = false) {
-       require_once("include/files.php");
+function file_tag_unsave_file($uid, $item, $file, $cat = false) {
+       require_once "include/files.php";
 
        $result = false;
        if (! intval($uid))
@@ -2115,25 +2109,22 @@ function file_tag_unsave_file($uid,$item,$file,$cat = false) {
                intval($termtype),
                intval($uid));
 
-       //$r = q("select file from item where uid = %d and deleted = 0 " . file_tag_file_query('item',$file,(($cat) ? 'category' : 'file')),
-       //);
-
        if (! dbm::is_result($r)) {
                $saved = get_pconfig($uid,'system','filetags');
-               set_pconfig($uid,'system','filetags',str_replace($pattern,'',$saved));
+               set_pconfig($uid, 'system', 'filetags', str_replace($pattern, '', $saved));
        }
 
        return true;
 }
 
 function normalise_openid($s) {
-       return trim(str_replace(array('http://','https://'),array('',''),$s),'/');
+       return trim(str_replace(array('http://', 'https://'), array('', ''), $s), '/');
 }
 
 
 function undo_post_tagging($s) {
        $matches = null;
-       $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism',$s,$matches,PREG_SET_ORDER);
+       $cnt = preg_match_all('/([!#@])\[url=(.*?)\](.*?)\[\/url\]/ism', $s, $matches, PREG_SET_ORDER);
        if ($cnt) {
                foreach ($matches as $mtch) {
                        $s = str_replace($mtch[0], $mtch[1] . $mtch[3],$s);
@@ -2174,7 +2165,7 @@ function deindent($text, $chr = "[\t ]", $count = NULL) {
                preg_match("|^" . $chr . "*|", $lines[$k], $m);
                $count = strlen($m[0]);
        }
-       for ($k=0; $k < count($lines); $k++) {
+       for ($k = 0; $k < count($lines); $k++) {
                $lines[$k] = preg_replace("|^" . $chr . "{" . $count . "}|", "", $lines[$k]);
        }
 
@@ -2204,11 +2195,12 @@ function formatBytes($bytes, $precision = 2) {
  */
 function format_network_name($network, $url = 0) {
        if ($network != "") {
-               require_once('include/contact_selectors.php');
-               if ($url != "")
+               require_once 'include/contact_selectors.php';
+               if ($url != "") {
                        $network_name = '<a href="'.$url.'">'.network_to_name($network, $url)."</a>";
-               else
+               } else {
                        $network_name = network_to_name($network);
+               }
 
                return $network_name;
        }