]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Let an item be accssible via the GUI (additionally to uid and item id)
[friendica.git] / include / text.php
index ea36a2a01699d6b9794a611a6a2743721ddc49de..4d6aa8697ae650e57499d8dc5f24b6523084dc30 100644 (file)
@@ -1,13 +1,5 @@
 <?php
 
-// This is our template processor.
-// $s is the string requiring macro substitution.
-// $r is an array of key value pairs (search => replace)
-// returns substituted string.
-// WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other.
-// For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing, 
-// depending on the order in which they were declared in the array.
-
 require_once("include/template_processor.php");
 require_once("include/friendica_smarty.php");
 
@@ -21,7 +13,7 @@ if(! function_exists('replace_macros')) {
  * @return string substituted string
  */
 function replace_macros($s,$r) {
-       
+
        $stamp1 = microtime(true);
 
        $a = get_app();
@@ -55,7 +47,7 @@ function random_string($size = 64,$type = RANDOM_STRING_HEX) {
 
 if(! function_exists('notags')) {
 /**
- * This is our primary input filter. 
+ * This is our primary input filter.
  *
  * The high bit hack only involved some old IE browser, forget which (IE5/Mac?)
  * that had an XSS attack vector due to stripping the high-bit on an 8-bit character
@@ -218,14 +210,16 @@ function xmlify($str) {
                                break;
                }       
        }*/
-
+       /*
        $buffer = mb_ereg_replace("&", "&amp;", $str);
        $buffer = mb_ereg_replace("'", "&apos;", $buffer);
-       $buffer = mb_ereg_replace("\"", "&quot;", $buffer);
+       $buffer = mb_ereg_replace('"', "&quot;", $buffer);
        $buffer = mb_ereg_replace("<", "&lt;", $buffer);
        $buffer = mb_ereg_replace(">", "&gt;", $buffer);
-
+       */
+       $buffer = htmlspecialchars($str, ENT_QUOTES);
        $buffer = trim($buffer);
+       
        return($buffer);
 }}
 
@@ -238,11 +232,13 @@ if(! function_exists('unxmlify')) {
 function unxmlify($s) {
 //     $ret = str_replace('&amp;','&', $s);
 //     $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
-       $ret = mb_ereg_replace('&amp;', '&', $s);
+       /*$ret = mb_ereg_replace('&amp;', '&', $s);
        $ret = mb_ereg_replace('&apos;', "'", $ret);
        $ret = mb_ereg_replace('&quot;', '"', $ret);
        $ret = mb_ereg_replace('&lt;', "<", $ret);
        $ret = mb_ereg_replace('&gt;', ">", $ret);
+       */
+       $ret = htmlspecialchars_decode($s, ENT_QUOTES);
        return $ret;    
 }}
 
@@ -278,12 +274,19 @@ function paginate_data(&$a, $count=null) {
        $stripped = str_replace('q=','',$stripped);
        $stripped = trim($stripped,'/');
        $pagenum = $a->pager['page'];
-       $url = $a->get_baseurl() . '/' . $stripped;
 
+       if (($a->page_offset != "") AND !strstr($stripped, "&offset="))
+               $stripped .= "&offset=".urlencode($a->page_offset);
+       if (!strpos($stripped, "?")) {
+               if ($pos = strpos($stripped, "&"))
+                       $stripped = substr($stripped, 0, $pos)."?".substr($stripped, $pos + 1);
+       }
+
+       $url = $a->get_baseurl() . '/' . $stripped;
 
        $data = array();
-       function _l(&$d, $name, $url, $text, $class="") { 
-               
+       function _l(&$d, $name, $url, $text, $class="") {
+
                $d[$name] = array('url'=>$url, 'text'=>$text, 'class'=>$class); 
        }
 
@@ -301,7 +304,7 @@ function paginate_data(&$a, $count=null) {
 
                        _l($data, "first", $url."&page=1",  t('first'));
 
-                       
+
                        $numpages = $a->pager['total'] / $a->pager['itemspage'];
 
                        $numstart = 1;
@@ -332,11 +335,11 @@ function paginate_data(&$a, $count=null) {
 
                        $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
                        _l($data, "last", $url."&page=$lastpage", t('last'));
-                       
+
                        if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
                                _l($data, "next", $url."&page=".($a->pager['page'] + 1), t('next'));
 
-               }       
+               }
        }
        return $data;
 
@@ -359,7 +362,7 @@ if(! function_exists('paginate')) {
  * @return string html for pagination #FIXME remove html
  */
 function paginate(&$a) {
-       
+
        $data = paginate_data($a);
        $tpl = get_markup_template("paginate.tpl");
        return replace_macros($tpl, array("pager" => $data));
@@ -378,14 +381,14 @@ function alt_pager(&$a, $i) {
        $data = paginate_data($a, $i);
        $tpl = get_markup_template("paginate.tpl");
        return replace_macros($tpl, array('pager' => $data));
-       
+
 }}
 
 
 if(! function_exists('expand_acl')) {
 /**
  * Turn user/group ACLs stored as angle bracketed text into arrays
- * 
+ *
  * @param string $s
  * @return array
  */
@@ -475,9 +478,9 @@ if(! function_exists('photo_new_resource')) {
 /**
  * Generate a guaranteed unique photo ID.
  * safe from birthday paradox
- * 
+ *
  * @return string
- */    
+ */
 function photo_new_resource() {
 
        do {
@@ -498,7 +501,7 @@ if(! function_exists('load_view_file')) {
  * @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
@@ -650,6 +653,9 @@ function attribute_contains($attr,$s) {
 }}
 
 if(! function_exists('logger')) {
+/* setup int->string log level map */
+$LOGGER_LEVELS = array();
+       
 /**
  * log levels:
  * LOGGER_NORMAL (default)
@@ -667,9 +673,16 @@ function logger($msg,$level = 0) {
        // turn off logger in install mode
        global $a;
        global $db;
-
+       global $LOGGER_LEVELS;
+       
        if(($a->module == 'install') || (! ($db && $db->connected))) return;
 
+    if (count($LOGGER_LEVEL)==0){
+        foreach (get_defined_constants() as $k=>$v){
+            if (substr($k,0,7)=="LOGGER_") $LOGGER_LEVELS[$v] = substr($k,7,7);
+        }        
+    }
+    
        $debugging = get_config('system','debugging');
        $loglevel  = intval(get_config('system','loglevel'));
        $logfile   = get_config('system','logfile');
@@ -677,8 +690,19 @@ function logger($msg,$level = 0) {
        if((! $debugging) || (! $logfile) || ($level > $loglevel))
                return;
 
+       $callers = debug_backtrace(); 
+       $logline =  sprintf("%s@%s\t[%s]:%s:%s:%s\t%s\n", 
+                                datetime_convert(), 
+                                session_id(),
+                                $LOGGER_LEVELS[$level],
+                                basename($callers[0]['file']),
+                                $callers[0]['line'],
+                                $callers[1]['function'],
+                                $msg
+                               );
+       
        $stamp1 = microtime(true);
-       @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
+       @file_put_contents($logfile, $logline, FILE_APPEND);
        $a->save_timestamp($stamp1, "file");
        return;
 }}
@@ -742,7 +766,7 @@ function get_tags($s) {
        // Otherwise pull out single word tags. These can be @nickname, @first_last
        // and #hash tags.
 
-       if(preg_match_all('/([@#][^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/',$s,$match)) {
+       if(preg_match_all('/([!#@][^ \x0D\x0A,;:?]+)([ \x0D\x0A,;:?]|$)/',$s,$match)) {
                foreach($match[1] as $mtch) {
                        if(strstr($mtch,"]")) {
                                // we might be inside a bbcode color tag - leave it alone
@@ -957,7 +981,7 @@ if(! function_exists('linkify')) {
  * @param string $s
  */
 function linkify($s) {
-       $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="external-link">$1</a>', $s);
+       $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' <a href="$1" target="_blank">$1</a>', $s);
        $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
        return($s);
 }}
@@ -1122,7 +1146,7 @@ function smilies($s, $sample = false) {
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/like.gif" alt=":like" />',
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/dislike.gif" alt=":dislike" />',
                '<a href="http://friendica.com">~friendica <img class="smiley" src="' . $a->get_baseurl() . '/images/friendica-16.png" alt="~friendica" /></a>',
-               '<a href="http://friendica.com">red <img class="smiley" src="' . $a->get_baseurl() . '/images/rhash-16.png" alt="red" /></a>'
+               '<a href="http://redmatrix.me/">red <img class="smiley" src="' . $a->get_baseurl() . '/images/rhash-16.png" alt="red" /></a>'
        );
 
        $params = array('texts' => $texts, 'icons' => $icons, 'string' => $s);
@@ -1269,7 +1293,7 @@ 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) {
+function prepare_body(&$item,$attach = false, $preview = false) {
 
        $a = get_app();
        call_hooks('prepare_body_init', $item);
@@ -1290,13 +1314,13 @@ function prepare_body(&$item,$attach = false) {
                                $tag["url"] = $searchpath.strtolower($tag["term"]);
 
                        if ($tag["type"] == TERM_HASHTAG) {
-                               $hashtags[] = "#<a href=\"".$tag["url"]."\" target=\"external-link\">".$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=\"external-link\">".$tag["term"]."</a>";
+                               $mentions[] = "@<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
                                $prefix = "@";
                        }
-                       $tags[] = $prefix."<a href=\"".$tag["url"]."\" target=\"external-link\">".$tag["term"]."</a>";
+                       $tags[] = $prefix."<a href=\"".$tag["url"]."\" target=\"_blank\">".$tag["term"]."</a>";
                }
        }
 
@@ -1329,7 +1353,7 @@ function prepare_body(&$item,$attach = false) {
        }
 
 
-       $prep_arr = array('item' => $item, 'html' => $s);
+       $prep_arr = array('item' => $item, 'html' => $s, 'preview' => $preview);
        call_hooks('prepare_body', $prep_arr);
        $s = $prep_arr['html'];
 
@@ -1407,7 +1431,7 @@ function prepare_body(&$item,$attach = false) {
                                        $title = ((strlen(trim($mtch[4]))) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]));
                                        $title .= ' ' . $mtch[2] . ' ' . t('bytes');
 
-                                       $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
+                                       $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
                                }
                        }
                }
@@ -1601,16 +1625,30 @@ if(! function_exists('get_plink')) {
  * @return boolean|array False if item has not plink, otherwise array('href'=>plink url, 'title'=>translated title)
  */
 function get_plink($item) {
-       $a = get_app(); 
-       if (x($item,'plink') && ($item['private'] != 1)) {
-               return array(
-                       'href' => $item['plink'],
-                       'title' => t('link to source'),
-               );
-       } 
-       else {
-               return false;
-       }
+       $a = get_app();
+
+       if ($a->user['nickname'] != "") {
+               $ret = array(
+                               'href' => $a->get_baseurl()."/display/".$a->user['nickname']."/".$item['id'],
+                               'title' => t('link to source'),
+                       );
+               $ret["orig"] = $ret["href"];
+
+               if (x($item,'plink'))
+                       $ret["href"] = $item['plink'];
+
+       } elseif (x($item,'plink') && ($item['private'] != 1))
+               $ret = array(
+                               'href' => $item['plink'],
+                               'orig' => $item['plink'],
+                               'title' => t('link to source'),
+                       );
+       else
+               $ret = array();
+
+       //if (x($item,'plink') && ($item['private'] != 1))
+
+       return($ret);
 }}
 
 if(! function_exists('unamp')) {
@@ -2003,10 +2041,12 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
                 if($type == 'file') {
                        $lbracket = '[';
                        $rbracket = ']';
+                       $termtype = TERM_FILE;
                }
                 else {
                        $lbracket = '<';
                        $rbracket = '>';
+                       $termtype = TERM_CATEGORY;
                }
 
                 $filetags_updated = $saved;
@@ -2032,9 +2072,15 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
                }
 
                 foreach($deleted_tags as $key => $tag) {
-                       $r = q("select file from item where uid = %d " . file_tag_file_query('item',$tag,$type),
-                               intval($uid)
-                       );
+                       $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
+                               dbesc($tag),
+                               intval(TERM_OBJ_POST),
+                               intval($termtype),
+                               intval($uid));
+
+                       //$r = q("select file from item where uid = %d " . file_tag_file_query('item',$tag,$type),
+                       //      intval($uid)
+                       //);
 
                        if(count($r)) {
                                unset($deleted_tags[$key]);
@@ -2057,6 +2103,8 @@ function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
 }
 
 function file_tag_save_file($uid,$item,$file) {
+       require_once("include/files.php");
+
        $result = false;
        if(! intval($uid))
                return false;
@@ -2066,11 +2114,14 @@ function file_tag_save_file($uid,$item,$file) {
        );
        if(count($r)) {
                if(! stristr($r[0]['file'],'[' . file_tag_encode($file) . ']'))
-                       q("update item set file = '%s' where id = %d and uid = %d limit 1",
+                       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) . ']');
@@ -2080,14 +2131,19 @@ function file_tag_save_file($uid,$item,$file) {
 }
 
 function file_tag_unsave_file($uid,$item,$file,$cat = false) {
+       require_once("include/files.php");
+
        $result = false;
        if(! intval($uid))
                return false;
 
-       if($cat == true)
+       if($cat == true) {
                $pattern = '<' . file_tag_encode($file) . '>' ;
-       else
+               $termtype = TERM_CATEGORY;
+       } else {
                $pattern = '[' . file_tag_encode($file) . ']' ;
+               $termtype = TERM_FILE;
+       }
 
 
        $r = q("select file from item where id = %d and uid = %d limit 1",
@@ -2097,15 +2153,22 @@ function file_tag_unsave_file($uid,$item,$file,$cat = false) {
        if(! count($r))
                return false;
 
-       q("update item set file = '%s' where id = %d and uid = %d limit 1",
+       q("update item set file = '%s' where id = %d and uid = %d",
                dbesc(str_replace($pattern,'',$r[0]['file'])),
                intval($item),
                intval($uid)
        );
 
-       $r = q("select file from item where uid = %d and deleted = 0 " . file_tag_file_query('item',$file,(($cat) ? 'category' : 'file')),
-               intval($uid)
-       );
+       create_files_from_item($item);
+
+       $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
+               dbesc($file),
+               intval(TERM_OBJ_POST),
+               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(! count($r)) {
                $saved = get_pconfig($uid,'system','filetags');
@@ -2122,7 +2185,7 @@ function normalise_openid($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);