Changeset 646 for branches/1.1dev/lib
- Timestamp:
- Oct 25, 2018 12:32:47 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.1dev/lib/Utilities.inc.php
r423 r646 66 66 67 67 /** 68 * Returns text with appropriate html translations. 68 * Returns text with appropriate html translations. 69 69 * 70 70 * @param string $txt Text to clean. 71 * @param bool $preserve_html If set to true, oTxt will not translage <, >, ", or ' 71 * @param bool $preserve_html If set to true, oTxt will not translage <, >, ", or ' 72 72 * characters into HTML entities. This allows HTML to pass 73 73 * through unmunged. … … 90 90 $search['retain_left_angle'] = '/</'; 91 91 $replace['retain_left_angle'] = '<'; 92 92 93 93 $search['retain_right_angle'] = '/>/'; 94 94 $replace['retain_right_angle'] = '>'; 95 95 96 96 $search['retain_single_quote'] = '/'/'; 97 97 $replace['retain_single_quote'] = "'"; 98 98 99 99 $search['retain_double_quote'] = '/"/'; 100 100 $replace['retain_double_quote'] = '"'; … … 109 109 110 110 /** 111 * Returns text with stylistic modifications. 111 * Returns text with stylistic modifications. 112 112 * 113 113 * @param string $txt Text to clean. … … 131 131 $search['single_quotes'] = '/(^|[^\w=])(?:\'|'|‘)([^\']+?)(?:\'|'|’)([^\w]|$)/'; 132 132 $replace['single_quotes'] = '\\1‘\\2’\\3'; 133 133 134 134 // em--dashes become em—dashes 135 135 $search['em_dash'] = '/(\s*[^!<-])--([^>-]\s*)/'; 136 136 $replace['em_dash'] = '\\1—\\2'; 137 137 138 138 // & becomes & 139 139 $search['ampersand'] = '/&((?![\w\d#]{1,10};))/'; … … 141 141 142 142 return preg_replace($search, $replace, $txt); 143 } 144 145 /* 146 * Finds all URLs in text and hyperlinks them. 147 * 148 * @access public 149 * @param string $text Text to search for URLs. 150 * @param bool $strict True to only include URLs starting with a scheme (http:// ftp:// im://), or false to include URLs starting with 'www.'. 151 * @param mixed $length Number of characters to truncate URL, or NULL to disable truncating. 152 * @param string $delim Delimiter to append, indicate truncation. 153 * @return string Same input text, but URLs hyperlinked. 154 * @author Quinn Comendant <quinn@strangecode.com> 155 * @version 2.1 156 * @since 22 Mar 2015 23:29:04 157 */ 158 function hyperlinkTxt($text, $strict=false, $length=null, $delim='âŠ') 159 { 160 // A list of schemes we allow at the beginning of a URL. 161 $schemes = 'mailto:|tel:|skype:|callto:|facetime:|bitcoin:|geo:|magnet:\?|sip:|sms:|xmpp:|view-source:(?:https?://)?|[\w-]{2,}://'; 162 163 // Capture the full URL into the first match and only the first X characters into the second match. 164 // This will match URLs not preceded by " ' or = (URLs inside an attribute) or ` (Markdown quoted) or double-scheme (http://http://www.asdf.com) 165 // Valid URL characters: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;= 166 $regex = '@ 167 \b # Start with a word-boundary. 168 (?<!"|\'|=|>|`|\]\(|[:/]/) # Negative look-behind to exclude URLs already in <a> tag, <tags>beween</tags>, `Markdown quoted`, [Markdown](link), and avoid broken:/ and doubled://schemes:// 169 ( # Begin match 1 170 ( # Begin match 2 171 (?:%s) # URL starts with known scheme or www. if strict = false 172 [^\s/$.?#]+ # Any domain-valid characters 173 [^\s"`<>]{1,%s} # Match 2 is limited to a maximum of LENGTH valid URL characters 174 ) 175 [^\s"`<>]* # Match 1 continues with any further valid URL characters 176 ([^\P{Any}\sâŠ<>«»"ââ%s]) # Final character not a space or common end-of-sentence punctuation (.,:;?!, etc). Using double negation set, see http://stackoverflow.com/a/4786560/277303 177 ) 178 @Suxi 179 '; 180 $regex = sprintf($regex, 181 ($strict ? $schemes : $schemes . '|www\.'), // Strict=false adds "www." to the list of allowed start-of-URL. 182 ($length ? $length : ''), 183 ($strict ? '' : '?!.,:;)\'-') // Strict=false excludes some "URL-valid" characters from the last character of URL. (Hyphen must remain last character in this class.) 184 ); 185 186 // Use a callback function to decide when to append the delim. 187 // Also encode special chars with oTxt(). 188 return preg_replace_callback($regex, function ($m) use ($length, $delim) { 189 $url = $m[1]; 190 $truncated_url = $m[2] . $m[3]; 191 $absolute_url = preg_replace('!^www\.!', 'http://www.', $url); 192 if (is_null($length) || $url == $truncated_url) { 193 // If not truncating, or URL was not truncated. 194 // Remove http schemas, and any single trailing / to make the display URL. 195 $display_url = preg_replace(['!^https?://!', '!^([^/]+)/$!'], ['', '$1'], $url); 196 return sprintf('<a href="%s">%s</a>', oTxt($absolute_url), $display_url); 197 } else { 198 // Truncated URL. 199 // Remove http schemas, and any single trailing / to make the display URL. 200 $display_url = preg_replace(['!^https?://!', '!^([^/]+)/$!'], ['', '$1'], trim($truncated_url)); 201 return sprintf('<a href="%s">%s%s</a>', oTxt($absolute_url), $display_url, $delim); 202 } 203 }, $text); 143 204 } 144 205 … … 168 229 case 'middle' : 169 230 return preg_replace(array(sprintf('/^(.{%s}).{4,}(.{%s})$/sU', $part1, $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim . '$2', $delim), $str); 170 break; 231 break; 171 232 case 'end' : 172 233 return preg_replace(array(sprintf('/^(.{%s}).{4,}$/sU', $part1 + $part2), '/\s*\.{3,}\s*/sU'), array('$1' . $delim, $delim), $str); … … 174 235 } 175 236 } 176 237 177 238 /** 178 239 * Generates a hexadecimal html color based on provided word. … … 264 325 $units = array('B', 'KB', 'MB', 'GB', 'TB'); 265 326 $ii = count($units) - 1; 266 327 267 328 // Max unit 268 329 $unit = array_search((string) $unit, $units); … … 270 331 $unit = $ii; 271 332 } 272 333 273 334 // Loop 274 335 $i = 0; … … 277 338 $i++; 278 339 } 279 340 280 341 return sprintf($format, $size, $units[$i]); 281 342 } … … 287 348 * @param mixed $var The variable that is being set. 288 349 * @param mixed $default What to set it to if $val is not currently set. 289 * @return mixed The resulting value of $var. 350 * @return mixed The resulting value of $var. 290 351 */ 291 352 function setDefault(&$var, $default='') … … 332 393 */ 333 394 function urlEncodeArray($data, $prefix='', $_return=true) { 334 395 335 396 // Data is stored in static variable. 336 397 static $args; 337 398 338 399 if (is_array($data)) { 339 400 foreach ($data as $key => $val) { … … 348 409 $args[$prefix] = urlencode($data); 349 410 } 350 411 351 412 if ($_return) { 352 413 // This is not a recursive execution. All recursion is complete. … … 369 430 */ 370 431 function urlEncodeArrayToString($data, $prefix='') { 371 432 372 433 $array_args = urlEncodeArray($data, $prefix); 373 434 $url_args = ''; … … 381 442 382 443 /** 383 * An alternative to "shuffle()" that actually works. 444 * An alternative to "shuffle()" that actually works. 384 445 * 385 446 * @param array $array1 input array … … 393 454 return array(); 394 455 } 395 456 396 457 srand((double) microtime() * 10000000); 397 458 $rand = array_rand($array1); 398 459 399 460 $array2[] = $array1[$rand]; 400 461 unset ($array1[$rand]); 401 462 402 463 return $array2 + arrayRand($array1, $array2); 403 464 } … … 428 489 * Prints the word "checked" if a variable is set, and optionally matches 429 490 * the desired value, otherwise prints nothing, 430 * used for printing the word "checked" in a checkbox form input. 491 * used for printing the word "checked" in a checkbox form input. 431 492 * 432 493 * @param mixed $var the variable to compare … … 454 515 * prints the word "selected" if a variable is set, and optionally matches 455 516 * the desired value, otherwise prints nothing, 456 * otherwise prints nothing, used for printing the word "checked" in a 457 * select form input 517 * otherwise prints nothing, used for printing the word "checked" in a 518 * select form input 458 519 * 459 520 * @param mixed $var the variable to compare … … 500 561 * 501 562 * @param array $date String date to convert. 502 * @param array $format Date format to pass to date(). 563 * @param array $format Date format to pass to date(). 503 564 * Default produces MySQL datetime: 0000-00-00 00:00:00. 504 565 * … … 513 574 $sql_date = date($format, strtotime($date)); 514 575 } 515 576 516 577 return $sql_date; 517 578 } … … 528 589 { 529 590 static $magic_quotes_gpc; 530 591 531 592 if (!isset($magic_quotes_gpc)) { 532 593 $magic_quotes_gpc = get_magic_quotes_gpc(); 533 594 } 534 595 535 596 if ($magic_quotes_gpc) { 536 597 if (!is_array($var)) { … … 614 675 { 615 676 global $CFG; 616 677 617 678 if ('' == $val) { 618 679 logMsg(sprintf('Adding signature to empty string.', null), LOG_NOTICE, __FILE__, __LINE__); 619 680 } 620 681 621 682 if (!isset($key)) { 622 683 $key = $CFG->signing_key; … … 671 732 /** 672 733 * Translates text 673 * 734 * 674 735 * @access public 675 736 * @param string $text the text to be translated … … 679 740 return $text; 680 741 } 681 742 682 743 /** 683 744 * Translates text 684 * 745 * 685 746 * @access public 686 747 * @param string $text the text to be translated … … 690 751 return $text; 691 752 } 692 753 693 754 /** 694 755 * Translates text by domain 695 * 756 * 696 757 * @access public 697 758 * @param string $domain the language to translate the text into … … 702 763 return $text; 703 764 } 704 765 705 766 /** 706 767 * Translates text by domain and category 707 * 768 * 708 769 * @access public 709 770 * @param string $domain the language to translate the text into … … 715 776 return $text; 716 777 } 717 778 718 779 /** 719 780 * Binds the text domain 720 * 781 * 721 782 * @access public 722 783 * @param string $domain the language to translate the text into 723 * @param string 784 * @param string 724 785 * @return string translated text 725 786 */ … … 727 788 return $domain; 728 789 } 729 790 730 791 /** 731 792 * Sets the text domain 732 * 793 * 733 794 * @access public 734 795 * @param string $domain the language to translate the text into … … 742 803 /** 743 804 * Sends empty output to the browser and flushes the php buffer so the client 744 * will see data before the page is finished processing. 805 * will see data before the page is finished processing. 745 806 */ 746 807 function flushBuffer() {
Note: See TracChangeset
for help on using the changeset viewer.