]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Merge commit 'upstream/master'
[friendica.git] / include / text.php
old mode 100755 (executable)
new mode 100644 (file)
index 0f7d2f7..d34fd7f
@@ -14,7 +14,13 @@ if(! function_exists('replace_macros')) {
 function replace_macros($s,$r) {
        global $t;
        
-       return $t->replace($s,$r);
+       //$ts = microtime();
+       $r =  $t->replace($s,$r);
+       //$tt = microtime() - $ts;
+       
+       //$a = get_app();
+       //$a->page['debug'] .= "$tt <br>\n";
+       return $r;
 
 }}
 
@@ -731,7 +737,10 @@ function smilies($s, $sample = false) {
                ':headdesk',
                '~friendika', 
                '~friendica', 
-               'Diaspora*' 
+//             'Diaspora*' 
+               ':beard',
+               ':whitebeard'
+
        );
 
        $icons = array(
@@ -772,8 +781,9 @@ function smilies($s, $sample = false) {
                '<img src="' . $a->get_baseurl() . '/images/smiley-bangheaddesk.gif" alt=":headdesk" />',
                '<a href="http://project.friendika.com">~friendika <img src="' . $a->get_baseurl() . '/images/friendika-16.png" alt="~friendika" /></a>',
                '<a href="http://friendica.com">~friendica <img src="' . $a->get_baseurl() . '/images/friendica-16.png" alt="~friendica" /></a>',
-               '<a href="http://diasporafoundation.org">Diaspora<img src="' . $a->get_baseurl() . '/images/diaspora.png" alt="Diaspora*" /></a>',
-
+//             '<a href="http://diasporafoundation.org">Diaspora<img src="' . $a->get_baseurl() . '/images/diaspora.png" alt="Diaspora*" /></a>',
+               '<img src="' . $a->get_baseurl() . '/images/smiley-beard.png" alt=":beard" />',
+               '<img src="' . $a->get_baseurl() . '/images/smiley-whitebeard.png" alt=":whitebeard" />'
        );
 
        $params = array('texts' => $texts, 'icons' => $icons, 'string' => $s);
@@ -786,6 +796,7 @@ function smilies($s, $sample = false) {
                }
        }
        else {
+               $params['string'] = preg_replace_callback('/&lt;(3+)/','preg_heart',$params['string']);
                $s = str_replace($params['texts'],$params['icons'],$params['string']);
        }
 
@@ -804,7 +815,18 @@ function smile_decode($m) {
        return(str_replace($m[1],base64url_decode($m[1]),$m[0]));
 }
 
+// expand <3333 to the correct number of hearts
 
+function preg_heart($x) {
+       $a = get_app();
+       if(strlen($x[1]) == 1)
+               return $x[0];
+       $t = '';
+       for($cnt = 0; $cnt < strlen($x[1]); $cnt ++)
+               $t .= '<img src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />';
+       $r =  str_replace($x[0],$t,$x[0]);
+       return $r;
+}
 
 
 if(! function_exists('day_translate')) {
@@ -852,16 +874,30 @@ function link_compare($a,$b) {
 if(! function_exists('prepare_body')) {
 function prepare_body($item,$attach = false) {
 
+       $a = get_app();
        call_hooks('prepare_body_init', $item); 
 
-       $s = prepare_text($item['body']);
+       $cache = get_config('system','itemcache');
+
+       if (($cache != '')) {
+               $cachefile = $cache."/".$item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']);
+
+               if (file_exists($cachefile))
+                       $s = file_get_contents($cachefile);
+               else {
+                       $s = prepare_text($item['body']);
+                       file_put_contents($cachefile, $s);
+               }
+       } else
+               $s = prepare_text($item['body']);
 
        $prep_arr = array('item' => $item, 'html' => $s);
        call_hooks('prepare_body', $prep_arr);
        $s = $prep_arr['html'];
 
-       if(! $attach)
+       if(! $attach) {
                return $s;
+       }
 
        $arr = explode(',',$item['attach']);
        if(count($arr)) {
@@ -891,10 +927,37 @@ function prepare_body($item,$attach = false) {
                }
                $s .= '<div class="clear"></div></div>';
        }
+       $matches = false;
+       $cnt = preg_match_all('/<(.*?)>/',$item['file'],$matches,PREG_SET_ORDER);
+       if($cnt) {
+//             logger('prepare_text: categories: ' . print_r($matches,true), LOGGER_DEBUG);
+               foreach($matches as $mtch) {
+                       if(strlen($x))
+                               $x .= ',';
+                       $x .= file_tag_decode($mtch[1]);
+               }
+               if(strlen($x))
+                       $s .= '<div class="categorytags"><span>' . t('Categories:') . ' </span>' . $x . '</div>'; 
 
 
+       }
+       $matches = false;
+       $x = '';
+       $cnt = preg_match_all('/\[(.*?)\]/',$item['file'],$matches,PREG_SET_ORDER);
+       if($cnt) {
+//             logger('prepare_text: filed_under: ' . print_r($matches,true), LOGGER_DEBUG);
+               foreach($matches as $mtch) {
+                       if(strlen($x))
+                               $x .= '&nbsp;&nbsp;&nbsp;';
+                       $x .= file_tag_decode($mtch[1]). ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&term=' . file_tag_decode($mtch[1]) . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>';
+               }
+               if(strlen($x) && (local_user() == $item['uid']))
+                       $s .= '<div class="filesavetags"><span>' . t('Filed under:') . ' </span>' . $x . '</div>'; 
+       }
+
        $prep_arr = array('item' => $item, 'html' => $s);
        call_hooks('prepare_body_final', $prep_arr);
+
        return $prep_arr['html'];
 }}
 
@@ -996,7 +1059,7 @@ function lang_selector() {
                        }
                        $ll = substr($l,5);
                        $ll = substr($ll,0,strrpos($ll,'/'));
-                       $selected = (($ll === $lang && (x($_SESSION['language']))) ? ' selected="selected" ' : '');
+                       $selected = (($ll === $lang && (x($_SESSION, 'language'))) ? ' selected="selected" ' : '');
                        $o .= '<option value="' . $ll . '"' . $selected . '>' . $ll . '</option>';
                }
        }
@@ -1201,3 +1264,89 @@ function reltoabs($text, $base)
   return $text;
 }
 
+function item_post_type($item) {
+       if(intval($item['event-id']))
+               return t('event');
+       if(strlen($item['resource-id']))
+               return t('photo');
+       if(strlen($item['verb']) && $item['verb'] !== ACTIVITY_POST)
+               return t('activity');
+       if($item['id'] != $item['parent'])
+               return t('comment');
+       return t('post');
+}
+
+// post categories and "save to file" use the same item.file table for storage.
+// We will differentiate the different uses by wrapping categories in angle brackets
+// and save to file categories in square brackets.
+// To do this we need to escape these characters if they appear in our tag. 
+
+function file_tag_encode($s) {
+       return str_replace(array('<','>','[',']'),array('%3c','%3e','%5b','%5d'),$s);
+}
+
+function file_tag_decode($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( '[' . file_tag_encode($s) . ']' );
+       else
+               $str = preg_quote( '<' . file_tag_encode($s) . '>' );
+       return " AND " . (($table) ? dbesc($table) . '.' : '') . "file regexp '" . dbesc($str) . "' ";
+}
+
+function file_tag_save_file($uid,$item,$file) {
+       $result = false;
+       if(! intval($uid))
+               return false;
+       $r = q("select file from item where id = %d and uid = %d limit 1",
+               intval($item),
+               intval($uid)
+       );
+       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",
+                               dbesc($r[0]['file'] . '[' . file_tag_encode($file) . ']'),
+                               intval($item),
+                               intval($uid)
+                       );
+               $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) . ']');
+       }
+       return true;
+}
+
+function file_tag_unsave_file($uid,$item,$file) {
+       $result = false;
+       if(! intval($uid))
+               return false;
+
+       $pattern = '[' . file_tag_encode($file) . ']' ;
+
+       $r = q("select file from item where id = %d and uid = %d limit 1",
+               intval($item),
+               intval($uid)
+       );
+       if(! count($r))
+               return false;
+
+       q("update item set file = '%s' where id = %d and uid = %d limit 1",
+               dbesc(str_replace($pattern,'',$r[0]['file'])),
+               intval($item),
+               intval($uid)
+       );
+
+       $r = q("select file from item where uid = %d " . file_tag_file_query('item',$file),
+               intval($uid)
+       );
+
+       if(! count($r)) {
+               $saved = get_pconfig($uid,'system','filetags');
+               set_pconfig($uid,'system','filetags',str_replace($pattern,'',$saved));
+       }
+       return true;
+}
+