From c1e2f95484dc8fa11c4d7ae2c5d5394e34785ecd Mon Sep 17 00:00:00 2001 From: friendica Date: Tue, 26 Feb 2013 18:26:33 -0800 Subject: doc update --- include/text.php | 505 +++++++++++++++++++++++++++---------------------------- 1 file changed, 251 insertions(+), 254 deletions(-) (limited to 'include/text.php') diff --git a/include/text.php b/include/text.php index b6fa49f81..39110602e 100644 --- a/include/text.php +++ b/include/text.php @@ -4,13 +4,11 @@ // $s is the string requiring macro substitution. // $r is an array of key value pairs (search => replace) // 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. + require_once("include/template_processor.php"); -if(! function_exists('replace_macros')) { + function replace_macros($s,$r) { global $t; @@ -42,7 +40,7 @@ function replace_macros($s,$r) { // $tt = microtime() - $ts; // $a->page['debug'] .= "$tt
\n"; return $output; -}} +} // random string, there are 86 characters max in text mode, 128 for hex @@ -51,13 +49,13 @@ function replace_macros($s,$r) { define('RANDOM_STRING_HEX', 0x00 ); define('RANDOM_STRING_TEXT', 0x01 ); -if(! function_exists('random_string')) { + 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)); -}} +} /** * This is our primary input filter. @@ -75,30 +73,30 @@ function random_string($size = 64,$type = RANDOM_STRING_HEX) { * */ -if(! function_exists('notags')) { + function notags($string) { return(str_replace(array("<",">"), array('[',']'), $string)); // High-bit filter no longer used // return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string)); -}} +} // use this on "body" or "content" input where angle chars shouldn't be removed, // and allow them to be safely displayed. -if(! function_exists('escape_tags')) { + function escape_tags($string) { return(htmlspecialchars($string, ENT_COMPAT, 'UTF-8', false)); -}} +} // generate a string that's random, but usually pronounceable. // used to generate initial passwords -if(! function_exists('autoname')) { + function autoname($len) { if($len <= 0) @@ -167,13 +165,13 @@ function autoname($len) { if(substr($word,-1) == 'q') $word = substr($word,0,-1); return $word; -}} +} // escape text ($str) for XML transport // returns escaped text. -if(! function_exists('xmlify')) { + function xmlify($str) { $buffer = ''; @@ -210,21 +208,21 @@ function xmlify($str) { } $buffer = trim($buffer); return($buffer); -}} +} // undo an xmlify // pass xml escaped text ($s), returns unescaped text -if(! function_exists('unxmlify')) { + function unxmlify($s) { $ret = str_replace('&','&', $s); $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret); return $ret; -}} +} // convenience wrapper, reverse the operation "bin2hex" -if(! function_exists('hex2bin')) { + function hex2bin($s) { if(! (is_string($s) && strlen($s))) return ''; @@ -234,7 +232,7 @@ function hex2bin($s) { } return(pack("H*",$s)); -}} +} // Automatic pagination. // To use, get the count of total items. @@ -246,7 +244,7 @@ function hex2bin($s) { // will limit the results to the correct items for the current page. // The actual page handling is then accomplished at the application layer. -if(! function_exists('paginate')) { + function paginate(&$a) { $o = ''; $stripped = preg_replace('/(&page=[0-9]*)/','',$a->query_string); @@ -300,9 +298,9 @@ function paginate(&$a) { $o .= ''."\r\n"; } return $o; -}} +} + -if(! function_exists('alt_pager')) { function alt_pager(&$a, $i, $more = '', $less = '') { $o = ''; @@ -332,11 +330,11 @@ function alt_pager(&$a, $i, $more = '', $less = '') { $o .= ''."\r\n"; return $o; -}} +} // Turn user/group ACLs stored as angle bracketed text into arrays -if(! function_exists('expand_acl')) { + function expand_acl($s) { // turn string array of angle-bracketed elements into string array @@ -353,22 +351,22 @@ function expand_acl($s) { } } return $ret; -}} +} // Used to wrap ACL elements in angle brackets for storage -if(! function_exists('sanitise_acl')) { + function sanitise_acl(&$item) { if(strlen($item)) $item = '<' . notags(trim($item)) . '>'; else unset($item); -}} +} // Convert an ACL array to a storable string -if(! function_exists('perms2str')) { + function perms2str($p) { $ret = ''; @@ -382,12 +380,12 @@ function perms2str($p) { $ret = implode('',$tmp); } return $ret; -}} +} // generate a guaranteed unique (for this domain) item ID for ATOM // safe from birthday paradox -if(! function_exists('item_message_id')) { + function item_message_id() { do { @@ -402,12 +400,12 @@ function item_message_id() { $dups = true; } while($dups == true); return $uri; -}} +} // Generate a guaranteed unique photo ID. // safe from birthday paradox -if(! function_exists('photo_new_resource')) { + function photo_new_resource() { do { @@ -420,7 +418,7 @@ function photo_new_resource() { $found = true; } while($found == true); return $resource; -}} +} @@ -435,15 +433,14 @@ function photo_new_resource() { // pass the attribute string as $attr and the attribute you // are looking for as $s - returns true if found, otherwise false -if(! function_exists('attribute_contains')) { function attribute_contains($attr,$s) { $a = explode(' ', $attr); if(count($a) && in_array($s,$a)) return true; return false; -}} +} + -if(! function_exists('logger')) { function logger($msg,$level = 0) { // turn off logger in install mode global $a; @@ -460,7 +457,7 @@ function logger($msg,$level = 0) { @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND); return; -}} +} // This is a special logging facility for developers. It allows one to target specific things to trace/debug @@ -469,7 +466,7 @@ function logger($msg,$level = 0) { // If you find dlogger() calls in checked in code, you are free to remove them - so as to provide a noise-free // development environment which responds to events you are targetting personally. -if(! function_exists('dlogger')) { + function dlogger($msg,$level = 0) { // turn off logger in install mode global $a; @@ -486,7 +483,7 @@ function dlogger($msg,$level = 0) { @file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND); return; -}} +} function profiler($t1,$t2,$label) { @@ -495,12 +492,12 @@ function profiler($t1,$t2,$label) { } -if(! function_exists('activity_match')) { + function activity_match($haystack,$needle) { if(($haystack === $needle) || ((basename($needle) === $haystack) && strstr($needle,NAMESPACE_ACTIVITY_SCHEMA))) return true; return false; -}} +} // Pull out all #hashtags and @person tags from $s; @@ -511,7 +508,7 @@ function activity_match($haystack,$needle) { // Returns array of tags found, or empty array. -if(! function_exists('get_tags')) { + function get_tags($s) { $ret = array(); @@ -556,19 +553,19 @@ function get_tags($s) { } } return $ret; -}} +} // quick and dirty quoted_printable encoding -if(! function_exists('qp')) { + function qp($s) { return str_replace ("%","=",rawurlencode($s)); -}} +} + -if(! function_exists('get_mentions')) { function get_mentions($item,$tags) { $o = ''; @@ -582,9 +579,9 @@ function get_mentions($item,$tags) { } } return $o; -}} +} + -if(! function_exists('contact_block')) { function contact_block() { $o = ''; $a = get_app(); @@ -637,7 +634,7 @@ function contact_block() { call_hooks('contact_block_end', $arr); return $o; -}} +} function chanlink_hash($s) { @@ -661,7 +658,7 @@ function magiclink_url($observer,$myaddr,$url) { } -if(! function_exists('micropro')) { + function micropro($contact, $redirect = false, $class = '', $textmode = false) { if($contact['click']) @@ -677,11 +674,11 @@ function micropro($contact, $redirect = false, $class = '', $textmode = false) { '$name' => $contact['xchan_name'], '$title' => $contact['xchan_name'] . ' [' . $contact['xchan_addr'] . ']', )); -}} +} + -if(! function_exists('search')) { function search($s,$id='search-box',$url='/search',$save = false) { $a = get_app(); $o = '
'; @@ -692,9 +689,9 @@ function search($s,$id='search-box',$url='/search',$save = false) { $o .= ''; $o .= '
'; return $o; -}} +} + -if(! function_exists('valid_email')) { function valid_email($x){ if(get_config('system','disable_email_validation')) @@ -703,7 +700,7 @@ function valid_email($x){ if(preg_match('/^[_a-zA-Z0-9\-\+]+(\.[_a-zA-Z0-9\-\+]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/',$x)) return true; return false; -}} +} /** @@ -714,12 +711,12 @@ function valid_email($x){ * */ -if(! function_exists('linkify')) { + function linkify($s) { $s = preg_replace("/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\'\%\$\!\+]*)/", ' $1', $s); $s = preg_replace("/\<(.*?)(src|href)=(.*?)\&\;(.*?)\>/ism",'<$1$2=$3&$4>',$s); return($s); -}} +} function get_poke_verbs() { @@ -792,7 +789,7 @@ function get_mood_verbs() { * */ -if(! function_exists('smilies')) { + function smilies($s, $sample = false) { $a = get_app(); @@ -898,7 +895,7 @@ function smilies($s, $sample = false) { return $s; -}} +} function smile_encode($m) { return(str_replace($m[1],base64url_encode($m[1]),$m[0])); @@ -922,7 +919,7 @@ function preg_heart($x) { } -if(! function_exists('day_translate')) { + function day_translate($s) { $ret = str_replace(array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'), array( t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'), t('Sunday')), @@ -933,14 +930,14 @@ function day_translate($s) { $ret); return $ret; -}} +} + -if(! function_exists('normalise_link')) { function normalise_link($url) { $ret = str_replace(array('https:','//www.'), array('http:','//'), $url); return(rtrim($ret,'/')); -}} +} /** * @@ -953,18 +950,18 @@ function normalise_link($url) { * */ -if(! function_exists('link_compare')) { + function link_compare($a,$b) { if(strcasecmp(normalise_link($a),normalise_link($b)) === 0) return true; return false; -}} +} // 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(); @@ -1073,12 +1070,12 @@ function prepare_body($item,$attach = false) { call_hooks('prepare_body_final', $prep_arr); return $prep_arr['html']; -}} +} // Given a text string, convert from bbcode to html and add smilie icons. -if(! function_exists('prepare_text')) { + function prepare_text($text) { require_once('include/bbcode.php'); @@ -1089,14 +1086,14 @@ function prepare_text($text) { $s = smilies(bbcode($text)); return $s; -}} +} /** * return atom link elements for all of our hubs */ -if(! function_exists('feed_hublinks')) { + function feed_hublinks() { $hub = get_config('system','huburl'); @@ -1114,11 +1111,11 @@ function feed_hublinks() { } } return $hubxml; -}} +} /* return atom link elements for salmon endpoints */ -if(! function_exists('feed_salmonlinks')) { + function feed_salmonlinks($nick) { $a = get_app(); @@ -1130,9 +1127,9 @@ function feed_salmonlinks($nick) { $salmon .= ' ' . "\n" ; $salmon .= ' ' . "\n" ; return $salmon; -}} +} + -if(! function_exists('get_plink')) { function get_plink($item) { $a = get_app(); if (x($item,'plink') && ($item['private'] != 1)) { @@ -1144,17 +1141,17 @@ function get_plink($item) { else { return false; } -}} +} + -if(! function_exists('unamp')) { function unamp($s) { return str_replace('&', '&', $s); -}} +} + -if(! function_exists('lang_selector')) { function lang_selector() { global $a; @@ -1187,10 +1184,10 @@ function lang_selector() { )); return $o; -}} +} + -if(! function_exists('return_bytes')) { function return_bytes ($size_str) { switch (substr ($size_str, -1)) { @@ -1199,7 +1196,7 @@ function return_bytes ($size_str) { case 'G': case 'g': return (int)$size_str * 1073741824; default: return $size_str; } -}} +} function generate_user_guid() { $found = true; @@ -1253,62 +1250,62 @@ function base64url_decode($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); + +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; - } - } -} + /* + * 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 '
'; @@ -1357,7 +1354,7 @@ function array_xmlify($val){ function reltoabs($text, $base) { if (empty($base)) - return $text; + return $text; $base = rtrim($base,'/'); @@ -1434,36 +1431,36 @@ function term_query($table,$s,$type = TERM_UNKNOWN) { // ex. given music,video return