]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Vier: The change from blue to black in links is now faded as well.
[friendica.git] / include / text.php
index 4212e23ca4dc8684817a6409dd2db5be9bef8662..9dc90ac50ae19723f5803fd1e44e645f1dea9819 100644 (file)
@@ -6,22 +6,42 @@
 // 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.   
+// depending on the order in which they were declared in the array.
 
 require_once("include/template_processor.php");
+require_once("include/friendica_smarty.php");
 
-if(! function_exists('replace_macros')) {  
+if(! function_exists('replace_macros')) {
 function replace_macros($s,$r) {
        global $t;
-       
-       //$ts = microtime();
-       $r =  $t->replace($s,$r);
-       //$tt = microtime() - $ts;
-       
-       //$a = get_app();
-       //$a->page['debug'] .= "$tt <br>\n";
-       return template_unescape($r);
 
+       $stamp1 = microtime(true);
+
+       $a = get_app();
+
+       if($a->theme['template_engine'] === 'smarty3') {
+               $template = '';
+               if(gettype($s) === 'string') {
+                       $template = $s;
+                       $s = new FriendicaSmarty();
+               }
+               foreach($r as $key=>$value) {
+                       if($key[0] === '$') {
+                               $key = substr($key, 1);
+                       }
+                       $s->assign($key, $value);
+               }
+               $output = $s->parsed($template);
+       }
+       else {
+               $r =  $t->replace($s,$r);
+
+               $output = template_unescape($r);
+       }
+       $a = get_app();
+       $a->save_timestamp($stamp1, "rendering");
+
+       return $output;
 }}
 
 
@@ -154,10 +174,11 @@ function autoname($len) {
 
 if(! function_exists('xmlify')) {
 function xmlify($str) {
-       $buffer = '';
+/*     $buffer = '';
        
-       for($x = 0; $x < mb_strlen($str); $x ++) {
-               $char = $str[$x];
+       $len = mb_strlen($str);
+       for($x = 0; $x < $len; $x ++) {
+               $char = mb_substr($str,$x,1);
         
                switch( $char ) {
 
@@ -185,7 +206,14 @@ function xmlify($str) {
                                $buffer .= $char;
                                break;
                }       
-       }
+       }*/
+
+       $buffer = mb_ereg_replace("&", "&amp;", $str);
+       $buffer = mb_ereg_replace("'", "&apos;", $buffer);
+       $buffer = mb_ereg_replace("\"", "&quot;", $buffer);
+       $buffer = mb_ereg_replace("<", "&lt;", $buffer);
+       $buffer = mb_ereg_replace(">", "&gt;", $buffer);
+
        $buffer = trim($buffer);
        return($buffer);
 }}
@@ -195,8 +223,13 @@ function xmlify($str) {
 
 if(! function_exists('unxmlify')) {
 function unxmlify($s) {
-       $ret = str_replace('&amp;','&', $s);
-       $ret = str_replace(array('&lt;','&gt;','&quot;','&apos;'),array('<','>','"',"'"),$ret);
+//     $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('&apos;', "'", $ret);
+       $ret = mb_ereg_replace('&quot;', '"', $ret);
+       $ret = mb_ereg_replace('&lt;', "<", $ret);
+       $ret = mb_ereg_replace('&gt;', ">", $ret);
        return $ret;    
 }}
 
@@ -406,44 +439,101 @@ function load_view_file($s) {
                $lang = 'en';
        $b = basename($s);
        $d = dirname($s);
-       if(file_exists("$d/$lang/$b"))
-               return file_get_contents("$d/$lang/$b");
-       
+       if(file_exists("$d/$lang/$b")) {
+               $stamp1 = microtime(true);
+               $content = file_get_contents("$d/$lang/$b");
+               $a->save_timestamp($stamp1, "file");
+               return $content;
+       }
+
        $theme = current_theme();
 
-       if(file_exists("$d/theme/$theme/$b"))
-               return file_get_contents("$d/theme/$theme/$b");
-                       
-       return file_get_contents($s);
+       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;
 }}
 
 if(! function_exists('get_intltext_template')) {
 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/$s"))
-               return file_get_contents("view/$lang/$s");
-       elseif(file_exists("view/en/$s"))
-               return file_get_contents("view/en/$s");
-       else
-               return file_get_contents("view/$s");
+       if(file_exists("view/$lang$engine/$s")) {
+               $stamp1 = microtime(true);
+               $content = file_get_contents("view/$lang$engine/$s");
+               $a->save_timestamp($stamp1, "file");
+               return $content;
+       } elseif(file_exists("view/en$engine/$s")) {
+               $stamp1 = microtime(true);
+               $content = file_get_contents("view/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;
+       }
 }}
 
 if(! function_exists('get_markup_template')) {
-function get_markup_template($s) {
-       $a=get_app();
+function get_markup_template($s, $root = '') {
+       $stamp1 = microtime(true);
+
+       $a = get_app();
+
+       if($a->theme['template_engine'] === 'smarty3') {
+               $template_file = get_template_file($a, 'smarty3/' . $s, $root);
+
+               $template = new FriendicaSmarty();
+               $template->filename = $template_file;
+               $a->save_timestamp($stamp1, "rendering");
+
+               return $template;
+       }
+       else {
+               $template_file = get_template_file($a, $s, $root);
+               $a->save_timestamp($stamp1, "rendering");
+
+               $stamp1 = microtime(true);
+               $content = file_get_contents($template_file);
+               $a->save_timestamp($stamp1, "file");
+               return $content;
+
+       }
+}}
+
+if(! function_exists("get_template_file")) {
+function get_template_file($a, $filename, $root = '') {
        $theme = current_theme();
-       
-       if(file_exists("view/theme/$theme/$s"))
-               return file_get_contents("view/theme/$theme/$s");
-       elseif (x($a->theme_info,"extends") && file_exists("view/theme/".$a->theme_info["extends"]."/$s"))
-               return file_get_contents("view/theme/".$a->theme_info["extends"]."/$s");
+
+       // Make sure $root ends with a slash /
+       if($root !== '' && $root[strlen($root)-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";
        else
-               return file_get_contents("view/$s");
+               $template_file = "{$root}view/$filename";
 
+       return $template_file;
 }}
 
 
@@ -481,8 +571,10 @@ function logger($msg,$level = 0) {
 
        if((! $debugging) || (! $logfile) || ($level > $loglevel))
                return;
-       
+
+       $stamp1 = microtime(true);
        @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
+       $a->save_timestamp($stamp1, "file");
        return;
 }}
 
@@ -508,11 +600,12 @@ function get_tags($s) {
        $ret = array();
 
        // ignore anything in a code block
-
        $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
 
-       // ignore anything in a bbtag
+       // Force line feeds at bbtags
+       $s = str_replace(array("[", "]"), array("\n[", "]\n"), $s);
 
+       // ignore anything in a bbtag
        $s = preg_replace('/\[(.*?)\]/sm','',$s);
 
        // Match full names against @tags including the space between first and last
@@ -954,27 +1047,62 @@ function link_compare($a,$b) {
        return false;
 }}
 
+
+// Find any non-embedded images in private items and add redir links to them
+
+if(! function_exists('redir_private_images')) {
+function redir_private_images($a, &$item) {
+
+       $matches = false;
+       $cnt = preg_match_all('|\[img\](http[^\[]*?/photo/[a-fA-F0-9]+?(-[0-9]\.[\w]+?)?)\[\/img\]|', $item['body'], $matches, PREG_SET_ORDER);
+       if($cnt) {
+               //logger("redir_private_images: matches = " . print_r($matches, true));
+               foreach($matches as $mtch) {
+                       if(strpos($mtch[1], '/redir') !== false)
+                               continue;
+
+                       if((local_user() == $item['uid']) && ($item['private'] != 0) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN)) {
+                               //logger("redir_private_images: redir");
+                               $img_url = $a->get_baseurl() . '/redir?f=1&quiet=1&url=' . $mtch[1] . '&conurl=' . $item['author-link'];
+                               $item['body'] = str_replace($mtch[0], "[img]".$img_url."[/img]", $item['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
 
-
 if(! function_exists('prepare_body')) {
 function prepare_body($item,$attach = false) {
 
        $a = get_app();
        call_hooks('prepare_body_init', $item);
 
-       $cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
+       //$cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
+       $cachefile = get_cachefile($item["guid"]."-".hash("md5", $item['body']));
 
        if (($cachefile != '')) {
-               if (file_exists($cachefile))
+               if (file_exists($cachefile)) {
+                       $stamp1 = microtime(true);
                        $s = file_get_contents($cachefile);
-               else {
+                       $a->save_timestamp($stamp1, "file");
+               } else {
+                       redir_private_images($a, $item);
                        $s = prepare_text($item['body']);
+
+                       $stamp1 = microtime(true);
                        file_put_contents($cachefile, $s);
+                       $a->save_timestamp($stamp1, "file");
+
+                       logger('prepare_body: put item '.$item["id"].' into cachefile '.$cachefile);
                }
-       } else
+       } else {
+               redir_private_images($a, $item);
                $s = prepare_text($item['body']);
+       }
 
 
        $prep_arr = array('item' => $item, 'html' => $s);
@@ -988,16 +1116,27 @@ function prepare_body($item,$attach = false) {
                return $s;
        }
 
-       $arr = explode(',',$item['attach']);
+       $arr = explode('[/attach],',$item['attach']);
        if(count($arr)) {
                $s .= '<div class="body-attach">';
                foreach($arr as $r) {
                        $matches = false;
                        $icon = '';
-                       $cnt = preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$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) {
-                                       $icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
+                                       $filetype = strtolower(substr( $mtch[3], 0, strpos($mtch[3],'/') ));
+                                       if($filetype) {
+                                               $filesubtype = strtolower(substr( $mtch[3], strpos($mtch[3],'/') + 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':
@@ -1008,7 +1147,8 @@ function prepare_body($item,$attach = false) {
                                                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');
                                        if((local_user() == $item['uid']) && ($item['contact-id'] != $a->contact['id']) && ($item['network'] == NETWORK_DFRN))