]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Merge branch 'master' into groups
[friendica.git] / include / text.php
index 0641689d5091d3ac049fac54af66b300c9d7a7dc..08803cc9b7be5b54bb7f4b9ceb8b6e980e520939 100644 (file)
@@ -19,11 +19,18 @@ function replace_macros($s,$r) {
 }}
 
 
-// random hex string, 64 chars max
+// random string, there are 86 characters max in text mode, 128 for hex
+// output is urlsafe
+
+define('RANDOM_STRING_HEX',  0x00 );
+define('RANDOM_STRING_TEXT', 0x01 );
 
 if(! function_exists('random_string')) {
-function random_string($size = 64) {
-       return(substr(hash('sha256',uniqid(rand(),true)),0,$size));
+function random_string($size = 64,$type = RANDOM_STRING_HEX) {
+       // generate a bit of entropy and run it through the whirlpool
+       $s = hash('whirlpool', (string) rand() . uniqid(rand(),true) . (string) rand(),(($type == RANDOM_STRING_TEXT) ? true : false));
+       $s = (($type == RANDOM_STRING_TEXT) ? str_replace("\n","",base64url_encode($s,true)) : $s);
+       return(substr($s,0,$size));
 }}
 
 /**
@@ -140,7 +147,7 @@ if(! function_exists('xmlify')) {
 function xmlify($str) {
        $buffer = '';
        
-       for($x = 0; $x < strlen($str); $x ++) {
+       for($x = 0; $x < mb_strlen($str); $x ++) {
                $char = $str[$x];
         
                switch( $char ) {
@@ -188,6 +195,9 @@ function unxmlify($s) {
 
 if(! function_exists('hex2bin')) {
 function hex2bin($s) {
+       if(! (is_string($s) && strlen($s)))
+               return '';
+
        if(! ctype_xdigit($s)) {
                logger('hex2bin: illegal input: ' . print_r(debug_backtrace(), true));
                return($s);
@@ -381,11 +391,13 @@ function get_intltext_template($s) {
 
 if(! function_exists('get_markup_template')) {
 function get_markup_template($s) {
-
+       $a=get_app();
        $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");
        else
                return file_get_contents("view/$s");
 
@@ -453,7 +465,7 @@ function get_tags($s) {
        // Match full names against @tags including the space between first and last
        // We will look these up afterward to see if they are full names or not recognisable.
 
-       if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \x0D\x0A,:?]+)([ \x0D\x0A,:?]|$)/',$s,$match)) {
+       if(preg_match_all('/(@[^ \x0D\x0A,:?]+ [^ \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
@@ -469,19 +481,18 @@ 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
                                continue;
                        }
+                       if(substr($mtch,-1,1) === '.')
+                               $mtch = substr($mtch,0,-1);
                        // ignore strictly numeric tags like #1
                        if((strpos($mtch,'#') === 0) && ctype_digit(substr($mtch,1)))
                                continue;
-                       if(substr($mtch,-1,1) === '.')
-                               $ret[] = substr($mtch,0,-1);
-                       else
-                               $ret[] = $mtch;
+                       $ret[] = $mtch;
                }
        }
        return $ret;
@@ -532,22 +543,30 @@ function contact_block() {
                $total = intval($r[0]['total']);
        }
        if(! $total) {
-               $o .= '<h4 class="contact-h4">' . t('No contacts') . '</h4>';
-               return $o;
-       }
-       $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT %d",
-                       intval($a->profile['uid']),
-                       intval($shown)
-       );
-       if(count($r)) {
-               $o .= '<h4 class="contact-h4">' .  sprintf( tt('%d Contact','%d Contacts', $total),$total) . '</h4><div id="contact-block">';
-               foreach($r as $rr) {
-                       $o .= micropro($rr,true,'mpfriend');
-               }
-               $o .= '</div><div id="contact-block-end"></div>';
-               $o .=  '<div id="viewcontacts"><a id="viewcontacts-link" href="viewcontacts/' . $a->profile['nickname'] . '">' . t('View Contacts') . '</a></div>';
+               $contacts = t('No contacts');
+               $micropro = Null;
                
+       } else {
+               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 ORDER BY RAND() LIMIT %d",
+                               intval($a->profile['uid']),
+                               intval($shown)
+               );
+               if(count($r)) {
+                       $contacts = sprintf( tt('%d Contact','%d Contacts', $total),$total);
+                       $micropro = Array();
+                       foreach($r as $rr) {
+                               $micropro[] = micropro($rr,true,'mpfriend');
+                       }
+               }
        }
+       
+       $tpl = get_markup_template('contact_block.tpl');
+       $o = replace_macros($tpl, array(
+               '$contacts' => $contacts,
+               '$nickname' => $a->profile['nickname'],
+               '$viewcontacts' => t('View Contacts'),
+               '$micropro' => $micropro,
+       ));
 
        $arr = array('contacts' => $r, 'output' => $o);
 
@@ -564,11 +583,13 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
 
        $url = $contact['url'];
        $sparkle = '';
+       $redir = false;
 
        if($redirect) {
                $a = get_app();
                $redirect_url = $a->get_baseurl() . '/redir/' . $contact['id'];
                if(local_user() && ($contact['uid'] == local_user()) && ($contact['network'] === 'dfrn')) {
+                       $redir = true;
                        $url = $redirect_url;
                        $sparkle = ' sparkle';
                }
@@ -579,6 +600,7 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
        if($textmode) {
                return '<div class="contact-block-textdiv' . $class . '"><a class="contact-block-link' . $class . $sparkle 
                        . (($click) ? ' fakelink' : '') . '" '
+                       . (($redir) ? ' target="redir" ' : '')
                        . (($url) ? ' href="' . $url . '"' : '') . $click
                        . '" title="' . $contact['name'] . ' [' . $contact['url'] . ']" alt="' . $contact['name'] 
                        . '" >'. $contact['name'] . '</a></div>' . "\r\n";
@@ -586,6 +608,7 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
        else {
                return '<div class="contact-block-div' . $class . '"><a class="contact-block-link' . $class . $sparkle 
                        . (($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'] 
                        . '" /></a></div>' . "\r\n";
@@ -595,12 +618,14 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
 
 
 if(! function_exists('search')) {
-function search($s,$id='search-box',$url='/search') {
+function search($s,$id='search-box',$url='/search',$save = false) {
        $a = get_app();
        $o  = '<div id="' . $id . '">';
        $o .= '<form action="' . $a->get_baseurl() . $url . '" method="get" >';
        $o .= '<input type="text" name="search" id="search-text" 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;
 }}
@@ -613,32 +638,6 @@ function valid_email($x){
 }}
 
 
-if(! function_exists('aes_decrypt')) {
-function aes_decrypt($val,$ky)
-{
-    $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
-    for($a=0;$a<strlen($ky);$a++)
-      $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
-    $mode = MCRYPT_MODE_ECB;
-    $enc = MCRYPT_RIJNDAEL_128;
-    $dec = @mcrypt_decrypt($enc, $key, $val, $mode, @mcrypt_create_iv( @mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM ) );
-    return rtrim($dec,(( ord(substr($dec,strlen($dec)-1,1))>=0 and ord(substr($dec, strlen($dec)-1,1))<=16)? chr(ord( substr($dec,strlen($dec)-1,1))):null));
-}}
-
-
-if(! function_exists('aes_encrypt')) {
-function aes_encrypt($val,$ky)
-{
-    $key="\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
-    for($a=0;$a<strlen($ky);$a++)
-      $key[$a%16]=chr(ord($key[$a%16]) ^ ord($ky[$a]));
-    $mode=MCRYPT_MODE_ECB;
-    $enc=MCRYPT_RIJNDAEL_128;
-    $val=str_pad($val, (16*(floor(strlen($val) / 16)+(strlen($val) % 16==0?2:1))), chr(16-(strlen($val) % 16)));
-    return mcrypt_encrypt($enc, $key, $val, $mode, mcrypt_create_iv( mcrypt_get_iv_size($enc, $mode), MCRYPT_DEV_URANDOM));
-}} 
-
-
 /**
  *
  * Function: linkify
@@ -649,7 +648,8 @@ function aes_encrypt($val,$ky)
 
 if(! function_exists('linkify')) {
 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="external-link">$1</a>', $s);
+       $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&amp\;(.*?)\>/ism",'<$1$2=$3&$4>',$s);
        return($s);
 }}
 
@@ -670,9 +670,9 @@ if(! function_exists('smilies')) {
 function smilies($s) {
        $a = get_app();
 
-       return str_replace(
+       $s = str_replace(
        array( '&lt;3', '&lt;/3', '&lt;\\3', ':-)', ':)', ';-)', ':-(', ':(', ':-P', ':P', ':-"', ':-x', ':-X', ':-D', '8-|', '8-O', 
-               '~friendika', 'Diaspora*' ),
+               '~friendika', '~friendica', 'Diaspora*' ),
        array(
                '<img src="' . $a->get_baseurl() . '/images/smiley-heart.gif" alt="<3" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-brokenheart.gif" alt="</3" />',
@@ -691,9 +691,14 @@ function smilies($s) {
                '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" />',
                '<a href="http://project.friendika.com">~friendika <img src="' . $a->get_baseurl() . '/images/friendika-16.png" alt="~friendika" /></a>',
-               '<a href="http://joindiaspora.com">Diaspora<img src="' . $a->get_baseurl() . '/images/diaspora.png" alt="Diaspora*" /></a>',
+               '<a href="http://friendica.com">~friendica <img src="' . $a->get_baseurl() . '/images/friendika-16.png" alt="~friendica" /></a>',
+               '<a href="http://diasporafoundation.org">Diaspora<img src="' . $a->get_baseurl() . '/images/diaspora.png" alt="Diaspora*" /></a>',
 
        ), $s);
+
+       call_hooks('smilie', $s);
+       return $s;
+
 }}
 
 
@@ -743,7 +748,14 @@ function link_compare($a,$b) {
 if(! function_exists('prepare_body')) {
 function prepare_body($item,$attach = false) {
 
+       call_hooks('prepare_body_init', $item); 
+
        $s = prepare_text($item['body']);
+
+       $prep_arr = array('item' => $item, 'html' => $s);
+       call_hooks('prepare_body', $prep_arr);
+       $s = $prep_arr['html'];
+
        if(! $attach)
                return $s;
 
@@ -775,7 +787,11 @@ function prepare_body($item,$attach = false) {
                }
                $s .= '<div class="clear"></div></div>';
        }
-       return $s;
+
+
+       $prep_arr = array('item' => $item, 'html' => $s);
+       call_hooks('prepare_body_final', $prep_arr);
+       return $prep_arr['html'];
 }}
 
 
@@ -835,9 +851,14 @@ function feed_salmonlinks($nick) {
 if(! function_exists('get_plink')) {
 function get_plink($item) {
        $a = get_app(); 
-       $plink = (((x($item,'plink')) && (! $item['private'])) ? '<div class="wall-item-links-wrapper"><a href="' 
-                       . $item['plink'] . '" title="' . t('link to source') . '" target="external-link" class="icon remote-link"></a></div>' : '');
-       return $plink;
+       if (x($item,'plink') && (! $item['private'])){
+               return array(
+                       'href' => $item['plink'],
+                       'title' => t('link to source'),
+               );
+       } else {
+               return false;
+       }
 }}
 
 if(! function_exists('unamp')) {
@@ -853,16 +874,22 @@ function lang_selector() {
        global $lang;
        $o = '<div id="lang-select-icon" class="icon language" title="' . t('Select an alternate language') . '" onclick="openClose(\'language-selector\');" ></div>';
        $o .= '<div id="language-selector" style="display: none;" >';
-       $o .= '<form action="" method="post" ><select name="system_language" onchange="this.form.submit();" >';
+       $o .= '<form action="#" method="post" ><select name="system_language" onchange="this.form.submit();" >';
        $langs = glob('view/*/strings.php');
        if(is_array($langs) && count($langs)) {
+               $langs[] = '';
                if(! in_array('view/en/strings.php',$langs))
                        $langs[] = 'view/en/';
                asort($langs);
                foreach($langs as $l) {
+                       if($l == '') {
+                               $default_selected = ((! x($_SESSION,'language')) ? ' selected="selected" ' : '');
+                               $o .= '<option value="" ' . $default_selected . '>' . t('default') . '</option>';
+                               continue;
+                       }
                        $ll = substr($l,5);
                        $ll = substr($ll,0,strrpos($ll,'/'));
-                       $selected = (($ll === $lang) ? ' selected="selected" ' : '');
+                       $selected = (($ll === $lang && (x($_SESSION['language']))) ? ' selected="selected" ' : '');
                        $o .= '<option value="' . $ll . '"' . $selected . '>' . $ll . '</option>';
                }
        }
@@ -896,20 +923,6 @@ function generate_user_guid() {
 }
 
 
-function pkcs5_pad ($text, $blocksize)
-{
-    $pad = $blocksize - (strlen($text) % $blocksize);
-    return $text . str_repeat(chr($pad), $pad);
-}
-
-function pkcs5_unpad($text)
-{
-    $pad = ord($text{strlen($text)-1});
-    if ($pad > strlen($text)) return false;
-    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
-    return substr($text, 0, -1 * $pad);
-} 
-
 
 function base64url_encode($s, $strip_padding = false) {
 
@@ -941,3 +954,104 @@ function base64url_decode($s) {
 
        return base64_decode(strtr($s,'-_','+/'));
 }
+
+
+if (!function_exists('str_getcsv')) {
+    function str_getcsv($input, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = '\n') {
+        if (is_string($input) && !empty($input)) {
+            $output = array();
+            $tmp    = preg_split("/".$eol."/",$input);
+            if (is_array($tmp) && !empty($tmp)) {
+                while (list($line_num, $line) = each($tmp)) {
+                    if (preg_match("/".$escape.$enclosure."/",$line)) {
+                        while ($strlen = strlen($line)) {
+                            $pos_delimiter       = strpos($line,$delimiter);
+                            $pos_enclosure_start = strpos($line,$enclosure);
+                            if (
+                                is_int($pos_delimiter) && is_int($pos_enclosure_start)
+                                && ($pos_enclosure_start < $pos_delimiter)
+                                ) {
+                                $enclosed_str = substr($line,1);
+                                $pos_enclosure_end = strpos($enclosed_str,$enclosure);
+                                $enclosed_str = substr($enclosed_str,0,$pos_enclosure_end);
+                                $output[$line_num][] = $enclosed_str;
+                                $offset = $pos_enclosure_end+3;
+                            } else {
+                                if (empty($pos_delimiter) && empty($pos_enclosure_start)) {
+                                    $output[$line_num][] = substr($line,0);
+                                    $offset = strlen($line);
+                                } else {
+                                    $output[$line_num][] = substr($line,0,$pos_delimiter);
+                                    $offset = (
+                                                !empty($pos_enclosure_start)
+                                                && ($pos_enclosure_start < $pos_delimiter)
+                                                )
+                                                ?$pos_enclosure_start
+                                                :$pos_delimiter+1;
+                                }
+                            }
+                            $line = substr($line,$offset);
+                        }
+                    } else {
+                        $line = preg_split("/".$delimiter."/",$line);
+   
+                        /*
+                         * Validating against pesky extra line breaks creating false rows.
+                         */
+                        if (is_array($line) && !empty($line[0])) {
+                            $output[$line_num] = $line;
+                        } 
+                    }
+                }
+                return $output;
+            } else {
+                return false;
+            }
+        } else {
+            return false;
+        }
+    }
+} 
+
+function cleardiv() {
+       return '<div class="clear"></div>';
+}
+
+
+function bb_translate_video($s) {
+
+       $matches = null;
+       $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER);
+       if($r) {
+               foreach($matches as $mtch) {
+                       if((stristr($mtch[1],'youtube')) || (stristr($mtch[1],'youtu.be')))
+                               $s = str_replace($mtch[0],'[youtube]' . $mtch[1] . '[/youtube]',$s);
+                       elseif(stristr($mtch[1],'vimeo'))
+                               $s = str_replace($mtch[0],'[vimeo]' . $mtch[1] . '[/vimeo]',$s);
+               }
+       }
+       return $s;      
+}
+
+function html2bb_video($s) {
+
+       $s = preg_replace('#<object[^>]+>(.*?)https+://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+)(.*?)</object>#ism',
+                       '[youtube]$2[/youtube]', $s);
+
+       $s = preg_replace('#<iframe[^>](.*?)https+://www.youtube.com/embed/([A-Za-z0-9\-_=]+)(.*?)</iframe>#ism',
+                       '[youtube]$2[/youtube]', $s);
+
+       $s = preg_replace('#<iframe[^>](.*?)https+://player.vimeo.com/video/([0-9]+)(.*?)</iframe>#ism',
+                       '[vimeo]$2[/vimeo]', $s);
+
+       return $s;
+}
+
+/**
+ * apply xmlify() to all values of array $val, recursively
+ */
+function array_xmlify($val){
+       if (is_bool($val)) return $val?"true":"false";
+       if (is_array($val)) return array_map('array_xmlify', $val);
+       return xmlify((string) $val);
+}