]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
moved formatBytes() to /include/text.php
[friendica.git] / include / text.php
index b20afd93360e771c3e51b3ec979d71b42f271ee8..829e37c2ce97f8088bbd1e2a480454ff681f7c8d 100644 (file)
@@ -2,6 +2,9 @@
 
 require_once("include/template_processor.php");
 require_once("include/friendica_smarty.php");
+require_once("include/map.php");
+require_once("mod/proxy.php");
+
 
 if(! function_exists('replace_macros')) {
 /**
@@ -217,7 +220,7 @@ function xmlify($str) {
        $buffer = mb_ereg_replace("<", "&lt;", $buffer);
        $buffer = mb_ereg_replace(">", "&gt;", $buffer);
        */
-       $buffer = htmlspecialchars($str, ENT_QUOTES);
+       $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
        $buffer = trim($buffer);
 
        return($buffer);
@@ -269,23 +272,23 @@ if(! function_exists('paginate_data')) {
  * @return Array data for pagination template
  */
 function paginate_data(&$a, $count=null) {
-       $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
+       $stripped = preg_replace('/([&?]page=[0-9]*)/','',$a->query_string);
 
        $stripped = str_replace('q=','',$stripped);
        $stripped = trim($stripped,'/');
        $pagenum = $a->pager['page'];
 
-       if (($a->page_offset != "") AND !strstr($stripped, "&offset="))
+       if (($a->page_offset != "") AND !preg_match('/[?&].offset=/', $stripped))
                $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="") {
+               if (!strpos($url, "?")) {
+                       if ($pos = strpos($url, "&"))
+                               $url = substr($url, 0, $pos)."?".substr($url, $pos + 1);
+               }
 
                $d[$name] = array('url'=>$url, 'text'=>$text, 'class'=>$class);
        }
@@ -384,6 +387,18 @@ function alt_pager(&$a, $i) {
 
 }}
 
+if(! function_exists('scroll_loader')) {
+/**
+ * Loader for infinite scrolling
+ * @return string html for loader
+ */
+function scroll_loader() {
+       $tpl = get_markup_template("scroll_loader.tpl");
+       return replace_macros($tpl, array(
+               'wait' => t('Loading more entries...'),
+               'end' => t('The end')
+       ));
+}}
 
 if(! function_exists('expand_acl')) {
 /**
@@ -739,6 +754,9 @@ if(! function_exists('get_tags')) {
 function get_tags($s) {
        $ret = array();
 
+       // Convert hashtag links to hashtags
+       $s = preg_replace("/#\[url\=([^\[\]]*)\](.*?)\[\/url\]/ism", "#$2", $s);
+
        // ignore anything in a code block
        $s = preg_replace('/\[code\](.*?)\[\/code\]/sm','',$s);
 
@@ -817,10 +835,16 @@ function get_mentions($item) {
        foreach($arr as $x) {
                $matches = null;
                if(preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
-                       $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
                        $o .= "\t\t" . '<link rel="ostatus:attention" href="' . $matches[1] . '" />' . "\r\n";
+                       $o .= "\t\t" . '<link rel="mentioned" href="' . $matches[1] . '" />' . "\r\n";
                }
        }
+
+       if (!$item['private']) {
+                       $o .= "\t\t".'<link rel="ostatus:attention" href="http://activityschema.org/collection/public"/>'."\r\n";
+                       $o .= "\t\t".'<link rel="mentioned" href="http://activityschema.org/collection/public"/>'."\r\n";
+       }
+
        return $o;
 }}
 
@@ -904,7 +928,7 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
        if($redirect) {
                $a = get_app();
                $redirect_url = $a->get_baseurl() . '/redir/' . $contact['id'];
-               if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === 'dfrn')) {
+               if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === NETWORK_DFRN)) {
                        $redir = true;
                        $url = $redirect_url;
                        $sparkle = ' sparkle';
@@ -928,7 +952,7 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
                        . (($click) ? ' fakelink' : '') . '" '
                        . (($redir) ? ' target="redir" ' : '')
                        . (($url) ? ' href="' . $url . '"' : '') . $click . ' ><img class="contact-block-img' . $class . $sparkle . '" src="'
-                       . $contact['micro'] . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
+                       . proxy_url($contact['micro']) . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name']
                        . '" /></a></div>' . "\r\n";
        }
 }}
@@ -942,19 +966,18 @@ if(! function_exists('search')) {
  * @param string $s search query
  * @param string $id html id
  * @param string $url search url
- * @param boolean $save show save search button
- * @return string html for search box #FIXME: remove html
+ * @param boolean $savedsearch show save search button
  */
 function search($s,$id='search-box',$url='/search',$save = false) {
        $a = get_app();
-       $o  = '<div id="' . $id . '">';
-       $o .= '<form action="' . $a->get_baseurl((stristr($url,'network')) ? true : false) . $url . '" method="get" >';
-       $o .= '<input type="text" name="search" id="search-text" placeholder="' . t('Search') . '" value="' . $s .'" />';
-       $o .= '<input type="submit" name="submit" id="search-submit" value="' . t('Search') . '" />';
-       if($save)
-               $o .= '<input type="submit" name="save" id="search-save" value="' . t('Save') . '" />';
-       $o .= '</form></div>';
-       return $o;
+        return replace_macros(get_markup_template('searchbox.tpl'), array(
+               '$s' => $s,
+               '$id' => $id,
+               '$action_url' => $a->get_baseurl((stristr($url,'network')) ? true : false) . $url,
+               '$search_label' => t('Search'),
+               '$save_label' => t('Save'),
+               '$savedsearch' => feature_enabled(local_user(),'savedsearch'),
+       ));
 }}
 
 if(! function_exists('valid_email')) {
@@ -1109,14 +1132,15 @@ function smilies($s, $sample = false) {
                ':like',
                ':dislike',
                 '~friendica',
-                'red#'
+                'red#',
+               'red#matrix'
 
        );
 
        $icons = array(
-               '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />',
-               '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="</3" />',
-               '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="<\\3" />',
+               '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="&lt;3" />',
+               '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="&lt;/3" />',
+               '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="&lt;\\3" />',
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-smile.gif" alt=":-)" />',
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-wink.gif" alt=";-)" />',
                '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-frown.gif" alt=":-(" />',
@@ -1147,7 +1171,8 @@ 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://redmatrix.me/">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/rm-16.png" alt="red" />matrix</a>',
+               '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $a->get_baseurl() . '/images/rm-16.png" alt="red" />matrix</a>'
        );
 
        $params = array('texts' => $texts, 'icons' => $icons, 'string' => $s);
@@ -1192,7 +1217,7 @@ function preg_heart($x) {
                return $x[0];
        $t = '';
        for($cnt = 0; $cnt < strlen($x[1]); $cnt ++)
-               $t .= '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />';
+               $t .= '<img class="smiley" src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="&lt;3" />';
        $r =  str_replace($x[0],$t,$x[0]);
        return $r;
 }
@@ -1278,6 +1303,28 @@ 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")) {
+
+               // The function "redir_private_images" changes the body.
+               // I'm not sure if we should store it permanently, so we save the old value.
+               $body = $item["body"];
+
+               $a = get_app();
+               redir_private_images($a, $item);
+
+               $item["rendered-html"] = prepare_text($item["body"]);
+               $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"]));
+               }
+       }
+}
 
 // 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
@@ -1329,29 +1376,8 @@ function prepare_body(&$item,$attach = false, $preview = false) {
        $item['hashtags'] = $hashtags;
        $item['mentions'] = $mentions;
 
-
-       //$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)) {
-                       $stamp1 = microtime(true);
-                       $s = file_get_contents($cachefile);
-                       $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 {
-               redir_private_images($a, $item);
-               $s = prepare_text($item['body']);
-       }
+       put_item_in_cache($item, true);
+       $s = $item["rendered-html"];
 
        require_once("mod/proxy.php");
        $s = proxy_parse_html($s);
@@ -1442,6 +1468,14 @@ function prepare_body(&$item,$attach = false, $preview = false) {
        }
        $s = $s . $as;
 
+       // map
+       if(strpos($s,'<div class="map">') !== false && $item['coord']) {
+               $x = generate_map(trim($item['coord']));
+               if($x) {
+                       $s = preg_replace('/\<div class\=\"map\"\>/','$0' . $x,$s);
+               }
+       }               
+
 
        // Look for spoiler
        $spoilersearch = '<blockquote class="spoiler">';
@@ -2248,3 +2282,15 @@ function deindent($text, $chr="[\t ]", $count=NULL) {
 
        return implode("\n", $lines);
 }
+
+function formatBytes($bytes, $precision = 2) { 
+        $units = array('B', 'KB', 'MB', 'GB', 'TB'); 
+
+       $bytes = max($bytes, 0); 
+       $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
+       $pow = min($pow, count($units) - 1); 
+
+       $bytes /= pow(1024, $pow);
+
+       return round($bytes, $precision) . ' ' . $units[$pow]; 
+}