text.lib.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the text library for Chamilo.
  5. * It is loaded during the global initialization,
  6. * so the functions below are available everywhere.
  7. *
  8. * @package chamilo.library
  9. */
  10. define('EXERCISE_NUMBER_OF_DECIMALS', 2);
  11. /**
  12. * This function strips all html-tags found in the input string and outputs a pure text.
  13. * Mostly, the function is to be used before language or encoding detection of the input string.
  14. * @param string $string The input string with html-tags to be converted to plain text.
  15. * @return string The returned plain text as a result.
  16. */
  17. function api_html_to_text($string)
  18. {
  19. // These purifications have been found experimentally, for nice looking output.
  20. $string = preg_replace('/<br[^>]*>/i', "\n", $string);
  21. $string = preg_replace('/<\/?(div|p|h[1-6]|table|ol|ul|blockquote)[^>]*>/i', "\n", $string);
  22. $string = preg_replace('/<\/(tr|li)[^>]*>/i', "\n", $string);
  23. $string = preg_replace('/<\/(td|th)[^>]*>/i', "\t", $string);
  24. $string = strip_tags($string);
  25. // Line endings unification and cleaning.
  26. $string = str_replace(array("\r\n", "\n\r", "\r"), "\n", $string);
  27. $string = preg_replace('/\s*\n/', "\n", $string);
  28. $string = preg_replace('/\n+/', "\n", $string);
  29. return trim($string);
  30. }
  31. /**
  32. * Detects encoding of html-formatted text.
  33. * @param string $string The input html-formatted text.
  34. * @return string Returns the detected encoding.
  35. */
  36. function api_detect_encoding_html($string) {
  37. if (@preg_match('/<head.*(<meta[^>]*content=[^>]*>).*<\/head>/si', $string, $matches)) {
  38. if (@preg_match('/<meta[^>]*charset=(.*)["\';][^>]*>/si', $matches[1], $matches)) {
  39. return api_refine_encoding_id(trim($matches[1]));
  40. }
  41. }
  42. return api_detect_encoding(api_html_to_text($string));
  43. }
  44. /**
  45. * Converts the text of a html-document to a given encoding, the meta-tag is changed accordingly.
  46. * @param string $string The input full-html document.
  47. * @param string The new encoding value to be set.
  48. */
  49. function api_set_encoding_html(&$string, $encoding) {
  50. $old_encoding = api_detect_encoding_html($string);
  51. if (@preg_match('/(.*<head.*)(<meta[^>]*content=[^>]*>)(.*<\/head>.*)/si', $string, $matches)) {
  52. $meta = $matches[2];
  53. if (@preg_match("/(<meta[^>]*charset=)(.*)([\"';][^>]*>)/si", $meta, $matches1)) {
  54. $meta = $matches1[1] . $encoding . $matches1[3];
  55. $string = $matches[1] . $meta . $matches[3];
  56. } else {
  57. $string = $matches[1] . '<meta http-equiv="Content-Type" content="text/html; charset='.$encoding.'"/>' . $matches[3];
  58. }
  59. } else {
  60. $count = 1;
  61. $string = str_ireplace('</head>', '<meta http-equiv="Content-Type" content="text/html; charset='.$encoding.'"/></head>', $string, $count);
  62. }
  63. $string = api_convert_encoding($string, $encoding, $old_encoding);
  64. }
  65. /**
  66. * Returns the title of a html document.
  67. * @param string $string The contents of the input document.
  68. * @param string $input_encoding The encoding of the input document. If the value is not set, it is detected.
  69. * @param string $$output_encoding The encoding of the retrieved title. If the value is not set, the system encoding is assumend.
  70. * @return string The retrieved title, html-entities and extra-whitespace between the words are cleaned.
  71. */
  72. function api_get_title_html(&$string, $output_encoding = null, $input_encoding = null) {
  73. if (@preg_match('/<head.+<title[^>]*>(.*)<\/title>/msi', $string, $matches)) {
  74. if (empty($output_encoding)) {
  75. $output_encoding = api_get_system_encoding();
  76. }
  77. if (empty($input_encoding)) {
  78. $input_encoding = api_detect_encoding_html($string);
  79. }
  80. return trim(@preg_replace('/\s+/', ' ', api_html_entity_decode(api_convert_encoding($matches[1], $output_encoding, $input_encoding), ENT_QUOTES, $output_encoding)));
  81. }
  82. return '';
  83. }
  84. /* XML processing functions */
  85. // A regular expression for accessing declared encoding within xml-formatted text.
  86. // Published by Steve Minutillo,
  87. // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss/
  88. define('_PCRE_XML_ENCODING', '/<\?xml.*encoding=[\'"](.*?)[\'"].*\?>/m');
  89. /**
  90. * Detects encoding of xml-formatted text.
  91. * @param string $string The input xml-formatted text.
  92. * @param string $default_encoding This is the default encoding to be returned if there is no way the xml-text's encoding to be detected. If it not spesified, the system encoding is assumed then.
  93. * @return string Returns the detected encoding.
  94. * @todo The second parameter is to be eliminated. See api_detect_encoding_html().
  95. */
  96. function api_detect_encoding_xml($string, $default_encoding = null) {
  97. if (preg_match(_PCRE_XML_ENCODING, $string, $matches)) {
  98. return api_refine_encoding_id($matches[1]);
  99. }
  100. if (api_is_valid_utf8($string)) {
  101. return 'UTF-8';
  102. }
  103. if (empty($default_encoding)) {
  104. $default_encoding = _api_mb_internal_encoding();
  105. }
  106. return api_refine_encoding_id($default_encoding);
  107. }
  108. /**
  109. * Converts character encoding of a xml-formatted text. If inside the text the encoding is declared, it is modified accordingly.
  110. * @param string $string The text being converted.
  111. * @param string $to_encoding The encoding that text is being converted to.
  112. * @param string $from_encoding (optional) The encoding that text is being converted from. If it is omited, it is tried to be detected then.
  113. * @return string Returns the converted xml-text.
  114. */
  115. function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null) {
  116. return _api_convert_encoding_xml($string, $to_encoding, $from_encoding);
  117. }
  118. /**
  119. * Converts character encoding of a xml-formatted text into UTF-8. If inside the text the encoding is declared, it is set to UTF-8.
  120. * @param string $string The text being converted.
  121. * @param string $from_encoding (optional) The encoding that text is being converted from. If it is omited, it is tried to be detected then.
  122. * @return string Returns the converted xml-text.
  123. */
  124. function api_utf8_encode_xml($string, $from_encoding = null) {
  125. return _api_convert_encoding_xml($string, 'UTF-8', $from_encoding);
  126. }
  127. /**
  128. * Converts character encoding of a xml-formatted text from UTF-8 into a specified encoding. If inside the text the encoding is declared, it is modified accordingly.
  129. * @param string $string The text being converted.
  130. * @param string $to_encoding (optional) The encoding that text is being converted to. If it is omited, the platform character set is assumed.
  131. * @return string Returns the converted xml-text.
  132. */
  133. function api_utf8_decode_xml($string, $to_encoding = 'UTF-8') {
  134. return _api_convert_encoding_xml($string, $to_encoding, 'UTF-8');
  135. }
  136. /**
  137. * Converts character encoding of a xml-formatted text. If inside the text the encoding is declared, it is modified accordingly.
  138. * @param string $string The text being converted.
  139. * @param string $to_encoding The encoding that text is being converted to.
  140. * @param string $from_encoding (optional) The encoding that text is being converted from. If the value is empty, it is tried to be detected then.
  141. * @return string Returns the converted xml-text.
  142. */
  143. function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding) {
  144. if (empty($from_encoding)) {
  145. $from_encoding = api_detect_encoding_xml($string);
  146. }
  147. $to_encoding = api_refine_encoding_id($to_encoding);
  148. if (!preg_match('/<\?xml.*\?>/m', $string, $matches)) {
  149. return api_convert_encoding('<?xml version="1.0" encoding="'.$to_encoding.'"?>'."\n".$string, $to_encoding, $from_encoding);
  150. }
  151. if (!preg_match(_PCRE_XML_ENCODING, $string)) {
  152. if (strpos($matches[0], 'standalone') !== false) {
  153. // The encoding option should precede the standalone option, othewise DOMDocument fails to load the document.
  154. $replace = str_replace('standalone', ' encoding="'.$to_encoding.'" standalone' , $matches[0]);
  155. } else {
  156. $replace = str_replace('?>', ' encoding="'.$to_encoding.'"?>' , $matches[0]);
  157. }
  158. return api_convert_encoding(str_replace($matches[0], $replace, $string), $to_encoding, $from_encoding);
  159. }
  160. global $_api_encoding;
  161. $_api_encoding = api_refine_encoding_id($to_encoding);
  162. return api_convert_encoding(preg_replace_callback(_PCRE_XML_ENCODING, '_api_convert_encoding_xml_callback', $string), $to_encoding, $from_encoding);
  163. }
  164. /**
  165. * A callback for serving the function _api_convert_encoding_xml().
  166. * @param array $matches Input array of matches corresponding to the xml-declaration.
  167. * @return string Returns the xml-declaration with modified encoding.
  168. */
  169. function _api_convert_encoding_xml_callback($matches) {
  170. global $_api_encoding;
  171. return str_replace($matches[1], $_api_encoding, $matches[0]);
  172. }
  173. /* Functions for supporting ASCIIMathML mathematical formulas and ASCIIsvg maathematical graphics */
  174. /**
  175. * Dectects ASCIIMathML formula presence within a given html text.
  176. * @param string $html The input html text.
  177. * @return bool Returns TRUE when there is a formula found or FALSE otherwise.
  178. */
  179. function api_contains_asciimathml($html) {
  180. if (!preg_match_all('/<span[^>]*class\s*=\s*[\'"](.*?)[\'"][^>]*>/mi', $html, $matches)) {
  181. return false;
  182. }
  183. foreach ($matches[1] as $string) {
  184. $string = ' '.str_replace(',', ' ', $string).' ';
  185. if (preg_match('/\sAM\s/m', $string)) {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. /**
  192. * Dectects ASCIIsvg graphics presence within a given html text.
  193. * @param string $html The input html text.
  194. * @return bool Returns TRUE when there is a graph found or FALSE otherwise.
  195. */
  196. function api_contains_asciisvg($html) {
  197. if (!preg_match_all('/<embed([^>]*?)>/mi', $html, $matches)) {
  198. return false;
  199. }
  200. foreach ($matches[1] as $string) {
  201. $string = ' '.str_replace(',', ' ', $string).' ';
  202. if (preg_match('/sscr\s*=\s*[\'"](.*?)[\'"]/m', $string)) {
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. /* Miscellaneous text processing functions */
  209. /**
  210. * Convers a string from camel case into underscore.
  211. * Works correctly with ASCII strings only, implementation for human-language strings is not necessary.
  212. * @param string $string The input string (ASCII)
  213. * @return string The converted result string
  214. */
  215. function api_camel_case_to_underscore($string) {
  216. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $string));
  217. }
  218. /**
  219. * Converts a string with underscores into camel case.
  220. * Works correctly with ASCII strings only, implementation for human-language strings is not necessary.
  221. * @param string $string The input string (ASCII)
  222. * @param bool $capitalise_first_char (optional) If true (default), the function capitalises the first char in the result string.
  223. * @return string The converted result string
  224. */
  225. function api_underscore_to_camel_case($string, $capitalise_first_char = true) {
  226. if ($capitalise_first_char) {
  227. $string = ucfirst($string);
  228. }
  229. return preg_replace_callback('/_([a-z])/', '_api_camelize', $string);
  230. }
  231. // A function for internal use, only for this library.
  232. function _api_camelize($match) {
  233. return strtoupper($match[1]);
  234. }
  235. /**
  236. * Truncates a string.
  237. *
  238. * @author Brouckaert Olivier
  239. * @param string $text The text to truncate.
  240. * @param integer $length The approximate desired length. The length of the suffix below is to be added to have the total length of the result string.
  241. * @param string $suffix A suffix to be added as a replacement.
  242. * @param string $encoding (optional) The encoding to be used. If it is omitted, the platform character set will be used by default.
  243. * @param boolean $middle If this parameter is true, truncation is done in the middle of the string.
  244. * @return string Truncated string, decorated with the given suffix (replacement).
  245. */
  246. function api_trunc_str($text, $length = 30, $suffix = '...', $middle = false, $encoding = null) {
  247. if (empty($encoding)) {
  248. $encoding = api_get_system_encoding();
  249. }
  250. $text_length = api_strlen($text, $encoding);
  251. if ($text_length <= $length) {
  252. return $text;
  253. }
  254. if ($middle) {
  255. return rtrim(api_substr($text, 0, round($length / 2), $encoding)).$suffix.ltrim(api_substr($text, - round($length / 2), $text_length, $encoding));
  256. }
  257. return rtrim(api_substr($text, 0, $length, $encoding)).$suffix;
  258. }
  259. /**
  260. * Handling simple and double apostrofe in order that strings be stored properly in database
  261. *
  262. * @author Denes Nagy
  263. * @param string variable - the variable to be revised
  264. */
  265. function domesticate($input) {
  266. $input = stripslashes($input);
  267. $input = str_replace("'", "''", $input);
  268. $input = str_replace('"', "''", $input);
  269. return ($input);
  270. }
  271. /**
  272. * function make_clickable($string)
  273. *
  274. * @desc Completes url contained in the text with "<a href ...".
  275. * However the function simply returns the submitted text without any
  276. * transformation if it already contains some "<a href:" or "<img src=".
  277. * @param string $text text to be converted
  278. * @return text after conversion
  279. * @author Rewritten by Nathan Codding - Feb 6, 2001.
  280. * completed by Hugues Peeters - July 22, 2002
  281. *
  282. * Actually this function is taken from the PHP BB 1.4 script
  283. * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
  284. * to that URL
  285. * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
  286. * to http://www.xxxx.yyyy[/zzzz]
  287. * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
  288. * to that email address
  289. * - Only matches these 2 patterns either after a space, or at the beginning of a line
  290. *
  291. * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
  292. * have it require something like xxxx@yyyy.zzzz or such. We'll see.
  293. */
  294. /**
  295. * Callback to convert URI match to HTML A element.
  296. *
  297. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  298. * make_clickable()}.
  299. *
  300. * @since Wordpress 2.3.2
  301. * @access private
  302. *
  303. * @param array $matches Single Regex Match.
  304. * @return string HTML A element with URI address.
  305. */
  306. function _make_url_clickable_cb($matches) {
  307. $url = $matches[2];
  308. if ( ')' == $matches[3] && strpos( $url, '(' ) ) {
  309. // If the trailing character is a closing parethesis, and the URL has an opening parenthesis in it, add the closing parenthesis to the URL.
  310. // Then we can let the parenthesis balancer do its thing below.
  311. $url .= $matches[3];
  312. $suffix = '';
  313. } else {
  314. $suffix = $matches[3];
  315. }
  316. // Include parentheses in the URL only if paired
  317. while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
  318. $suffix = strrchr( $url, ')' ) . $suffix;
  319. $url = substr( $url, 0, strrpos( $url, ')' ) );
  320. }
  321. $url = esc_url($url);
  322. if ( empty($url) )
  323. return $matches[0];
  324. return $matches[1] . "<a href=\"$url\" rel=\"nofollow\">$url</a>" . $suffix;
  325. }
  326. /**
  327. * Checks and cleans a URL.
  328. *
  329. * A number of characters are removed from the URL. If the URL is for displaying
  330. * (the default behaviour) ampersands are also replaced. The 'clean_url' filter
  331. * is applied to the returned cleaned URL.
  332. *
  333. * @since wordpress 2.8.0
  334. * @uses wp_kses_bad_protocol() To only permit protocols in the URL set
  335. * via $protocols or the common ones set in the function.
  336. *
  337. * @param string $url The URL to be cleaned.
  338. * @param array $protocols Optional. An array of acceptable protocols.
  339. * Defaults to 'http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn' if not set.
  340. * @param string $_context Private. Use esc_url_raw() for database usage.
  341. * @return string The cleaned $url after the 'clean_url' filter is applied.
  342. */
  343. function esc_url( $url, $protocols = null, $_context = 'display' ) {
  344. //$original_url = $url;
  345. if ( '' == $url )
  346. return $url;
  347. $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
  348. $strip = array('%0d', '%0a', '%0D', '%0A');
  349. $url = _deep_replace($strip, $url);
  350. $url = str_replace(';//', '://', $url);
  351. /* If the URL doesn't appear to contain a scheme, we
  352. * presume it needs http:// appended (unless a relative
  353. * link starting with /, # or ? or a php file).
  354. */
  355. if ( strpos($url, ':') === false && ! in_array( $url[0], array( '/', '#', '?' ) ) &&
  356. ! preg_match('/^[a-z0-9-]+?\.php/i', $url) )
  357. $url = 'http://' . $url;
  358. return Security::remove_XSS($url);
  359. /*// Replace ampersands and single quotes only when displaying.
  360. if ( 'display' == $_context ) {
  361. $url = wp_kses_normalize_entities( $url );
  362. $url = str_replace( '&amp;', '&#038;', $url );
  363. $url = str_replace( "'", '&#039;', $url );
  364. }
  365. if ( '/' === $url[0] ) {
  366. $good_protocol_url = $url;
  367. } else {
  368. if ( ! is_array( $protocols ) )
  369. $protocols = wp_allowed_protocols();
  370. $good_protocol_url = wp_kses_bad_protocol( $url, $protocols );
  371. if ( strtolower( $good_protocol_url ) != strtolower( $url ) )
  372. return '';
  373. }
  374. /**
  375. * Filter a string cleaned and escaped for output as a URL.
  376. *
  377. * @since 2.3.0
  378. *
  379. * @param string $good_protocol_url The cleaned URL to be returned.
  380. * @param string $original_url The URL prior to cleaning.
  381. * @param string $_context If 'display', replace ampersands and single quotes only.
  382. */
  383. //return apply_filters( 'clean_url', $good_protocol_url, $original_url, $_context );98
  384. }
  385. /**
  386. * Perform a deep string replace operation to ensure the values in $search are no longer present
  387. *
  388. * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
  389. * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
  390. * str_replace would return
  391. *
  392. * @since wordpress 2.8.1
  393. * @access private
  394. *
  395. * @param string|array $search The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
  396. * @param string $subject The string being searched and replaced on, otherwise known as the haystack.
  397. * @return string The string with the replaced svalues.
  398. */
  399. function _deep_replace( $search, $subject ) {
  400. $subject = (string) $subject;
  401. $count = 1;
  402. while ( $count ) {
  403. $subject = str_replace( $search, '', $subject, $count );
  404. }
  405. return $subject;
  406. }
  407. /**
  408. * Callback to convert URL match to HTML A element.
  409. *
  410. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  411. * make_clickable()}.
  412. *
  413. * @since wordpress 2.3.2
  414. * @access private
  415. *
  416. * @param array $matches Single Regex Match.
  417. * @return string HTML A element with URL address.
  418. */
  419. function _make_web_ftp_clickable_cb($matches) {
  420. $ret = '';
  421. $dest = $matches[2];
  422. $dest = 'http://' . $dest;
  423. $dest = esc_url($dest);
  424. if ( empty($dest) )
  425. return $matches[0];
  426. // removed trailing [.,;:)] from URL
  427. if ( in_array( substr($dest, -1), array('.', ',', ';', ':', ')') ) === true ) {
  428. $ret = substr($dest, -1);
  429. $dest = substr($dest, 0, strlen($dest)-1);
  430. }
  431. return $matches[1] . "<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
  432. }
  433. /**
  434. * Callback to convert email address match to HTML A element.
  435. *
  436. * This function was backported from 2.5.0 to 2.3.2. Regex callback for {@link
  437. * make_clickable()}.
  438. *
  439. * @since wordpress 2.3.2
  440. * @access private
  441. *
  442. * @param array $matches Single Regex Match.
  443. * @return string HTML A element with email address.
  444. */
  445. function _make_email_clickable_cb($matches) {
  446. $email = $matches[2] . '@' . $matches[3];
  447. return $matches[1] . "<a href=\"mailto:$email\">$email</a>";
  448. }
  449. /**
  450. * Convert plaintext URI to HTML links.
  451. *
  452. * Converts URI, www and ftp, and email addresses. Finishes by fixing links
  453. * within links.
  454. *
  455. * @since wordpress 0.71
  456. *
  457. * @param string $text Content to convert URIs.
  458. * @return string Content with converted URIs.
  459. */
  460. function make_clickable( $text ) {
  461. $r = '';
  462. $textarr = preg_split( '/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE ); // split out HTML tags
  463. $nested_code_pre = 0; // Keep track of how many levels link is nested inside <pre> or <code>
  464. foreach ( $textarr as $piece ) {
  465. if ( preg_match( '|^<code[\s>]|i', $piece ) || preg_match( '|^<pre[\s>]|i', $piece ) )
  466. $nested_code_pre++;
  467. elseif ( ( '</code>' === strtolower( $piece ) || '</pre>' === strtolower( $piece ) ) && $nested_code_pre )
  468. $nested_code_pre--;
  469. if ( $nested_code_pre || empty( $piece ) || ( $piece[0] === '<' && ! preg_match( '|^<\s*[\w]{1,20}+://|', $piece ) ) ) {
  470. $r .= $piece;
  471. continue;
  472. }
  473. // Long strings might contain expensive edge cases ...
  474. if ( 10000 < strlen( $piece ) ) {
  475. // ... break it up
  476. foreach ( _split_str_by_whitespace( $piece, 2100 ) as $chunk ) { // 2100: Extra room for scheme and leading and trailing paretheses
  477. if ( 2101 < strlen( $chunk ) ) {
  478. $r .= $chunk; // Too big, no whitespace: bail.
  479. } else {
  480. $r .= make_clickable( $chunk );
  481. }
  482. }
  483. } else {
  484. $ret = " $piece "; // Pad with whitespace to simplify the regexes
  485. $url_clickable = '~
  486. ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
  487. ( # 2: URL
  488. [\\w]{1,20}+:// # Scheme and hier-part prefix
  489. (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
  490. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
  491. (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
  492. [\'.,;:!?)] # Punctuation URL character
  493. [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
  494. )*
  495. )
  496. (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
  497. ~xS'; // The regex is a non-anchored pattern and does not have a single fixed starting character.
  498. // Tell PCRE to spend more time optimizing since, when used on a page load, it will probably be used several times.
  499. $ret = preg_replace_callback( $url_clickable, '_make_url_clickable_cb', $ret );
  500. $ret = preg_replace_callback( '#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret );
  501. $ret = preg_replace_callback( '#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret );
  502. $ret = substr( $ret, 1, -1 ); // Remove our whitespace padding.
  503. $r .= $ret;
  504. }
  505. }
  506. // Cleanup of accidental links within links
  507. $r = preg_replace( '#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r );
  508. return $r;
  509. }
  510. /**
  511. * Breaks a string into chunks by splitting at whitespace characters.
  512. * The length of each returned chunk is as close to the specified length goal as possible,
  513. * with the caveat that each chunk includes its trailing delimiter.
  514. * Chunks longer than the goal are guaranteed to not have any inner whitespace.
  515. *
  516. * Joining the returned chunks with empty delimiters reconstructs the input string losslessly.
  517. *
  518. * Input string must have no null characters (or eventual transformations on output chunks must not care about null characters)
  519. *
  520. * <code>
  521. * _split_str_by_whitespace( "1234 67890 1234 67890a cd 1234 890 123456789 1234567890a 45678 1 3 5 7 90 ", 10 ) ==
  522. * array (
  523. * 0 => '1234 67890 ', // 11 characters: Perfect split
  524. * 1 => '1234 ', // 5 characters: '1234 67890a' was too long
  525. * 2 => '67890a cd ', // 10 characters: '67890a cd 1234' was too long
  526. * 3 => '1234 890 ', // 11 characters: Perfect split
  527. * 4 => '123456789 ', // 10 characters: '123456789 1234567890a' was too long
  528. * 5 => '1234567890a ', // 12 characters: Too long, but no inner whitespace on which to split
  529. * 6 => ' 45678 ', // 11 characters: Perfect split
  530. * 7 => '1 3 5 7 9', // 9 characters: End of $string
  531. * );
  532. * </code>
  533. *
  534. * @since wordpress 3.4.0
  535. * @access private
  536. *
  537. * @param string $string The string to split.
  538. * @param int $goal The desired chunk length.
  539. * @return array Numeric array of chunks.
  540. */
  541. function _split_str_by_whitespace( $string, $goal ) {
  542. $chunks = array();
  543. $string_nullspace = strtr( $string, "\r\n\t\v\f ", "\000\000\000\000\000\000" );
  544. while ( $goal < strlen( $string_nullspace ) ) {
  545. $pos = strrpos( substr( $string_nullspace, 0, $goal + 1 ), "\000" );
  546. if ( false === $pos ) {
  547. $pos = strpos( $string_nullspace, "\000", $goal + 1 );
  548. if ( false === $pos ) {
  549. break;
  550. }
  551. }
  552. $chunks[] = substr( $string, 0, $pos + 1 );
  553. $string = substr( $string, $pos + 1 );
  554. $string_nullspace = substr( $string_nullspace, $pos + 1 );
  555. }
  556. if ( $string ) {
  557. $chunks[] = $string;
  558. }
  559. return $chunks;
  560. }
  561. /**
  562. * This functions cuts a paragraph
  563. * i.e cut('Merry Xmas from Lima',13) = "Merry Xmas fr..."
  564. * @param string The text to "cut"
  565. * @param int Count of chars
  566. * @param bool Whether to embed in a <span title="...">...</span>
  567. * @return string
  568. * */
  569. function cut($text, $maxchar, $embed = false) {
  570. if (api_strlen($text) > $maxchar) {
  571. if ($embed) {
  572. return '<p title="'.$text.'">'.api_substr($text, 0, $maxchar).'...</p>';
  573. }
  574. return api_substr($text, 0, $maxchar).' ...';
  575. }
  576. return $text;
  577. }
  578. /**
  579. * Show a number as only integers if no decimals, but will show 2 decimals if exist.
  580. *
  581. * @param mixed Number to convert
  582. * @param int Decimal points 0=never, 1=if needed, 2=always
  583. * @return mixed An integer or a float depends on the parameter
  584. */
  585. function float_format($number, $flag = 1) {
  586. if (is_numeric($number)) {
  587. if (!$number) {
  588. $result = ($flag == 2 ? '0.'.str_repeat('0', EXERCISE_NUMBER_OF_DECIMALS) : '0');
  589. } else {
  590. if (floor($number) == $number) {
  591. $result = number_format($number, ($flag == 2 ? EXERCISE_NUMBER_OF_DECIMALS : 0));
  592. } else {
  593. $result = number_format(round($number, 2), ($flag == 0 ? 0 : EXERCISE_NUMBER_OF_DECIMALS));
  594. }
  595. }
  596. return $result;
  597. }
  598. }
  599. // TODO: To be checked for correct timezone management.
  600. /**
  601. * Function to obtain last week timestamps
  602. * @return array Times for every day inside week
  603. */
  604. function get_last_week() {
  605. $week = date('W');
  606. $year = date('Y');
  607. $lastweek = $week - 1;
  608. if ($lastweek == 0) {
  609. $week = 52;
  610. $year--;
  611. }
  612. $lastweek = sprintf("%02d", $lastweek);
  613. $arrdays = array();
  614. for ($i = 1; $i <= 7; $i++) {
  615. $arrdays[] = strtotime("$year"."W$lastweek"."$i");
  616. }
  617. return $arrdays;
  618. }
  619. /**
  620. * Gets the week from a day
  621. * @param string Date in UTC (2010-01-01 12:12:12)
  622. * @return int Returns an integer with the week number of the year
  623. */
  624. function get_week_from_day($date) {
  625. if (!empty($date)) {
  626. $time = api_strtotime($date,'UTC');
  627. return date('W', $time);
  628. } else {
  629. return date('W');
  630. }
  631. }
  632. /**
  633. * This function splits the string into words and then joins them back together again one by one.
  634. * Example: "Test example of a long string"
  635. * substrwords(5) = Test ... *
  636. * @param string
  637. * @param int the max number of character
  638. * @param string how the string will be end
  639. * @return a reduce string
  640. */
  641. function substrwords($text,$maxchar,$end='...')
  642. {
  643. if(strlen($text)>$maxchar)
  644. {
  645. $words=explode(" ",$text);
  646. $output = '';
  647. $i=0;
  648. while(1)
  649. {
  650. $length = (strlen($output)+strlen($words[$i]));
  651. if($length>$maxchar)
  652. {
  653. break;
  654. }
  655. else
  656. {
  657. $output = $output." ".$words[$i];
  658. $i++;
  659. };
  660. };
  661. }
  662. else
  663. {
  664. $output = $text;
  665. return $output;
  666. }
  667. return $output.$end;
  668. }
  669. function implode_with_key($glue, $array) {
  670. if (!empty($array)) {
  671. $string = '';
  672. foreach($array as $key => $value) {
  673. if (empty($value)) {
  674. $value = 'null';
  675. }
  676. $string .= $key." : ".$value." $glue ";
  677. }
  678. return $string;
  679. }
  680. return '';
  681. }
  682. /**
  683. * Transform the file size in a human readable format.
  684. *
  685. * @param int $file_size Size of the file in bytes
  686. * @return string A human readable representation of the file size
  687. */
  688. function format_file_size($file_size)
  689. {
  690. $file_size = intval($file_size);
  691. if ($file_size >= 1073741824) {
  692. $file_size = round($file_size / 1073741824 * 100) / 100 . 'G';
  693. } elseif($file_size >= 1048576) {
  694. $file_size = round($file_size / 1048576 * 100) / 100 . 'M';
  695. } elseif($file_size >= 1024) {
  696. $file_size = round($file_size / 1024 * 100) / 100 . 'k';
  697. } else {
  698. $file_size = $file_size . 'B';
  699. }
  700. return $file_size;
  701. }
  702. function return_datetime_from_array($array)
  703. {
  704. $year = '0000';
  705. $month = $day = $hours = $minutes = $seconds = '00';
  706. if (isset($array['Y']) && (isset($array['F']) || isset($array['M'])) && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
  707. $year = $array['Y'];
  708. $month = isset($array['F'])?$array['F']:$array['M'];
  709. if (intval($month) < 10 ) $month = '0'.$month;
  710. $day = $array['d'];
  711. if (intval($day) < 10 ) $day = '0'.$day;
  712. $hours = $array['H'];
  713. if (intval($hours) < 10 ) $hours = '0'.$hours;
  714. $minutes = $array['i'];
  715. if (intval($minutes) < 10 ) $minutes = '0'.$minutes;
  716. }
  717. if (checkdate($month,$day,$year)) {
  718. $datetime = $year.'-'.$month.'-'.$day.' '.$hours.':'.$minutes.':'.$seconds;
  719. }
  720. return $datetime;
  721. }
  722. /**
  723. * Converts an string CLEANYO[admin][amann,acostea]
  724. * into an array:
  725. *
  726. * array(
  727. * CLEANYO
  728. * admin
  729. * amann,acostea
  730. * )
  731. *
  732. * @param $array
  733. * @return array
  734. */
  735. function bracketsToArray($array)
  736. {
  737. return preg_split('/[\[\]]+/', $array, -1, PREG_SPLIT_NO_EMPTY);
  738. }
  739. /**
  740. * @param string $string
  741. * @param bool $capitalizeFirstCharacter
  742. * @return mixed
  743. */
  744. function underScoreToCamelCase($string, $capitalizeFirstCharacter = true)
  745. {
  746. $str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
  747. if (!$capitalizeFirstCharacter) {
  748. $str[0] = strtolower($str[0]);
  749. }
  750. return $str;
  751. }