]> git.mxchange.org Git - friendica.git/blobdiff - include/text.php
Merge pull request #317 from CatoTH/master
[friendica.git] / include / text.php
index 6d557ed84e8c62706cca664776d0e71337137055..d6a9ef5d3a03a33dbd8b2008e50392ac9cfb1c06 100644 (file)
@@ -80,6 +80,9 @@ function escape_tags($string) {
 if(! function_exists('autoname')) {
 function autoname($len) {
 
+       if($len <= 0)
+               return '';
+
        $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u'); 
        if(mt_rand(0,5) == 4)
                $vowels[] = 'y';
@@ -205,7 +208,6 @@ function hex2bin($s) {
                return '';
 
        if(! ctype_xdigit($s)) {
-               logger('hex2bin: illegal input: ' . print_r(debug_backtrace(), true));
                return($s);
        }
 
@@ -226,6 +228,9 @@ if(! function_exists('paginate')) {
 function paginate(&$a) {
        $o = '';
        $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string);
+
+//     $stripped = preg_replace('/&zrl=(.*?)([\?&]|$)/ism','',$stripped);
+
        $stripped = str_replace('q=','',$stripped);
        $stripped = trim($stripped,'/');
        $pagenum = $a->pager['page'];
@@ -553,7 +558,7 @@ function contact_block() {
 
        if((! is_array($a->profile)) || ($a->profile['hide-friends']))
                return $o;
-       $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0",
+       $r = q("SELECT COUNT(*) AS `total` FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0 AND `archive` = 0",
                        intval($a->profile['uid'])
        );
        if(count($r)) {
@@ -564,7 +569,7 @@ function contact_block() {
                $micropro = Null;
                
        } else {
-               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0 ORDER BY RAND() LIMIT %d",
+               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `blocked` = 0 and `pending` = 0 AND `hidden` = 0 AND `archive` = 0 ORDER BY RAND() LIMIT %d",
                                intval($a->profile['uid']),
                                intval($shown)
                );
@@ -610,6 +615,8 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) {
                        $url = $redirect_url;
                        $sparkle = ' sparkle';
                }
+               else
+                       $url = zrl($url);
        }
        $click = ((x($contact,'click')) ? ' onclick="' . $contact['click'] . '" ' : '');
        if($click)
@@ -639,7 +646,7 @@ 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" value="' . $s .'" />';
+       $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') . '" />'; 
@@ -724,6 +731,8 @@ function smilies($s, $sample = false) {
                '\\o/', 
                'o.O', 
                'O.o', 
+               'o_O', 
+               'O_o', 
                ":'(", 
                ":-!", 
                ":-/", 
@@ -758,6 +767,8 @@ function smilies($s, $sample = false) {
                '<img src="' . $a->get_baseurl() . '/images/smiley-thumbsup.gif" alt="\\o/" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-Oo.gif" alt="o.O" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-Oo.gif" alt="O.o" />',
+               '<img src="' . $a->get_baseurl() . '/images/smiley-Oo.gif" alt="o_O" />',
+               '<img src="' . $a->get_baseurl() . '/images/smiley-Oo.gif" alt="O_o" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-cry.gif" alt=":\'(" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" />',
                '<img src="' . $a->get_baseurl() . '/images/smiley-undecided.gif" alt=":-/" />',
@@ -890,24 +901,30 @@ function prepare_body($item,$attach = false) {
                foreach($arr as $r) {
                        $matches = false;
                        $icon = '';
-                       $cnt = preg_match('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches);
+                       $cnt = preg_match_all('|\[attach\]href=\"(.*?)\" length=\"(.*?)\" type=\"(.*?)\" title=\"(.*?)\"\[\/attach\]|',$r,$matches, PREG_SET_ORDER);
                        if($cnt) {
-                               $icontype = strtolower(substr($matches[3],0,strpos($matches[3],'/')));
-                               switch($icontype) {
-                                       case 'video':
-                                       case 'audio':
-                                       case 'image':
-                                       case 'text':
-                                               $icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
-                                               break;
-                                       default:
-                                               $icon = '<div class="attachtype icon s22 type-unkn"></div>';
-                                               break;
+                               foreach($matches as $mtch) {
+                                       $icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
+                                       switch($icontype) {
+                                               case 'video':
+                                               case 'audio':
+                                               case 'image':
+                                               case 'text':
+                                                       $icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
+                                                       break;
+                                               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'])
+                                               $the_url = $a->get_baseurl() . '/redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
+                                       else
+                                               $the_url = $mtch[1];
+
+                                       $s .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
                                }
-                               $title = ((strlen(trim($matches[4]))) ? escape_tags(trim($matches[4])) : escape_tags($matches[1]));
-                               $title .= ' ' . $matches[2] . ' ' . t('bytes');
-
-                               $s .= '<a href="' . strip_tags($matches[1]) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
                        }
                }
                $s .= '<div class="clear"></div></div>';
@@ -919,7 +936,8 @@ function prepare_body($item,$attach = false) {
                foreach($matches as $mtch) {
                        if(strlen($x))
                                $x .= ',';
-                       $x .= file_tag_decode($mtch[1]);
+                       $x .= xmlify(file_tag_decode($mtch[1])) 
+                               . ((local_user() == $item['uid']) ? ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&cat=' . xmlify(file_tag_decode($mtch[1])) . '" title="' . t('remove') . '" >' . t('[remove]') . '</a>' : '');
                }
                if(strlen($x))
                        $s .= '<div class="categorytags"><span>' . t('Categories:') . ' </span>' . $x . '</div>'; 
@@ -934,7 +952,7 @@ function prepare_body($item,$attach = false) {
                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>';
+                       $x .= xmlify(file_tag_decode($mtch[1])) . ' <a href="' . $a->get_baseurl() . '/filerm/' . $item['id'] . '?f=&term=' . xmlify(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>'; 
@@ -1057,10 +1075,12 @@ function unamp($s) {
 if(! function_exists('lang_selector')) {
 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();" >';
+       
        $langs = glob('view/*/strings.php');
+       
+       $lang_options = array();
+       $selected = "";
+       
        if(is_array($langs) && count($langs)) {
                $langs[] = '';
                if(! in_array('view/en/strings.php',$langs))
@@ -1068,17 +1088,22 @@ function lang_selector() {
                asort($langs);
                foreach($langs as $l) {
                        if($l == '') {
-                               $default_selected = ((! x($_SESSION,'language')) ? ' selected="selected" ' : '');
-                               $o .= '<option value="" ' . $default_selected . '>' . t('default') . '</option>';
+                               $lang_options[""] = t('default');
                                continue;
                        }
                        $ll = substr($l,5);
                        $ll = substr($ll,0,strrpos($ll,'/'));
-                       $selected = (($ll === $lang && (x($_SESSION, 'language'))) ? ' selected="selected" ' : '');
-                       $o .= '<option value="' . $ll . '"' . $selected . '>' . $ll . '</option>';
+                       $selected = (($ll === $lang && (x($_SESSION, 'language'))) ? $ll : $selected);
+                       $lang_options[$ll]=$ll;
                }
        }
-       $o .= '</select></form></div>';
+
+       $tpl = get_markup_template("lang_selector.tpl");        
+       $o = replace_macros($tpl, array(
+               '$title' => t('Select an alternate language'),
+               '$langs' => array($lang_options, $selected),
+               
+       ));
        return $o;
 }}
 
@@ -1305,13 +1330,126 @@ function file_tag_decode($s) {
 }
 
 function file_tag_file_query($table,$s,$type = 'file') {
+
        if($type == 'file')
-               $str = preg_quote( '[' . file_tag_encode($s) . ']' );
+               $str = preg_quote( '[' . str_replace('%','%%',file_tag_encode($s)) . ']' );
        else
-               $str = preg_quote( '<' . file_tag_encode($s) . '>' );
+               $str = preg_quote( '<' . str_replace('%','%%',file_tag_encode($s)) . '>' );
        return " AND " . (($table) ? dbesc($table) . '.' : '') . "file regexp '" . dbesc($str) . "' ";
 }
 
+// ex. given music,video return <music><video> or [music][video]
+function file_tag_list_to_file($list,$type = 'file') {
+        $tag_list = '';
+        if(strlen($list)) {
+                $list_array = explode(",",$list);
+                if($type == 'file') {
+                       $lbracket = '[';
+                       $rbracket = ']';
+               }
+                else {
+                       $lbracket = '<';
+                       $rbracket = '>';
+               }
+
+                foreach($list_array as $item) {
+                 if(strlen($item)) {
+                               $tag_list .= $lbracket . file_tag_encode(trim($item))  . $rbracket;
+                       }
+                }
+       }
+        return $tag_list;
+}
+
+// ex. given <music><video>[friends], return music,video or friends
+function file_tag_file_to_list($file,$type = 'file') {
+        $matches = false;
+        $list = '';
+        if($type == 'file') {
+                $cnt = preg_match_all('/\[(.*?)\]/',$file,$matches,PREG_SET_ORDER);
+       }
+        else {
+                $cnt = preg_match_all('/<(.*?)>/',$file,$matches,PREG_SET_ORDER);
+       }
+       if($cnt) {
+               foreach($matches as $mtch) {
+                       if(strlen($list))
+                               $list .= ',';
+                       $list .= file_tag_decode($mtch[1]);
+               }
+       }
+
+        return $list;
+}
+
+function file_tag_update_pconfig($uid,$file_old,$file_new,$type = 'file') {
+        // $file_old - categories previously associated with an item
+        // $file_new - new list of categories for an item
+
+       if(! intval($uid))
+               return false;
+
+        if($file_old == $file_new)
+               return true;
+
+       $saved = get_pconfig($uid,'system','filetags');
+        if(strlen($saved)) {
+                if($type == 'file') {
+                       $lbracket = '[';
+                       $rbracket = ']';
+               }
+                else {
+                       $lbracket = '<';
+                       $rbracket = '>';
+               }
+
+                $filetags_updated = $saved;
+
+               // check for new tags to be added as filetags in pconfig
+                $new_tags = array();
+                $check_new_tags = explode(",",file_tag_file_to_list($file_new,$type));
+
+               foreach($check_new_tags as $tag) {
+                       if(! stristr($saved,$lbracket . file_tag_encode($tag) . $rbracket))
+                               $new_tags[] = $tag;
+               }
+
+               $filetags_updated .= file_tag_list_to_file(implode(",",$new_tags),$type);
+
+               // check for deleted tags to be removed from filetags in pconfig
+                $deleted_tags = array();
+                $check_deleted_tags = explode(",",file_tag_file_to_list($file_old,$type));
+
+               foreach($check_deleted_tags as $tag) {
+                       if(! stristr($file_new,$lbracket . file_tag_encode($tag) . $rbracket))
+                               $deleted_tags[] = $tag;
+               }
+
+                foreach($deleted_tags as $key => $tag) {
+                       $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]);
+                       }
+                       else {
+                               $filetags_updated = str_replace($lbracket . file_tag_encode($tag) . $rbracket,'',$filetags_updated);
+                       }
+               }
+
+                if($saved != $filetags_updated) {
+                       set_pconfig($uid,'system','filetags', $filetags_updated);
+                }
+               return true;
+       }
+        else
+                if(strlen($file_new)) {
+                       set_pconfig($uid,'system','filetags', $file_new);
+                }
+               return true;
+}
+
 function file_tag_save_file($uid,$item,$file) {
        $result = false;
        if(! intval($uid))
@@ -1335,12 +1473,16 @@ function file_tag_save_file($uid,$item,$file) {
        return true;
 }
 
-function file_tag_unsave_file($uid,$item,$file) {
+function file_tag_unsave_file($uid,$item,$file,$cat = false) {
        $result = false;
        if(! intval($uid))
                return false;
 
-       $pattern = '[' . file_tag_encode($file) . ']' ;
+       if($cat == true)
+               $pattern = '<' . file_tag_encode($file) . '>' ;
+       else
+               $pattern = '[' . file_tag_encode($file) . ']' ;
+
 
        $r = q("select file from item where id = %d and uid = %d limit 1",
                intval($item),
@@ -1355,13 +1497,14 @@ function file_tag_unsave_file($uid,$item,$file) {
                intval($uid)
        );
 
-       $r = q("select file from item where uid = %d " . file_tag_file_query('item',$file),
+       $r = q("select file from item where uid = %d and deleted = 0 " . file_tag_file_query('item',$file,(($cat) ? 'category' : 'file')),
                intval($uid)
        );
 
        if(! count($r)) {
                $saved = get_pconfig($uid,'system','filetags');
                set_pconfig($uid,'system','filetags',str_replace($pattern,'',$saved));
+
        }
        return true;
 }
@@ -1382,3 +1525,14 @@ function undo_post_tagging($s) {
        return $s;
 }
 
+function fix_mce_lf($s) {
+       $s = str_replace("\r\n","\n",$s);
+       $s = str_replace("\n\n","\n",$s);
+       return $s;
+}
+
+
+function protect_sprintf($s) {
+       return(str_replace('%','%%',$s));
+}
+