text.lib.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. class Text
  11. {
  12. /**
  13. * This function strips all html-tags found in the input string and outputs a pure text.
  14. * Mostly, the function is to be used before language or encoding detection of the input string.
  15. * @param string $string The input string with html-tags to be converted to plain text.
  16. * @return string The returned plain text as a result.
  17. */
  18. static function api_html_to_text($string)
  19. {
  20. // These purifications have been found experimentally, for nice looking output.
  21. $string = preg_replace('/<br[^>]*>/i', "\n", $string);
  22. $string = preg_replace('/<\/?(div|p|h[1-6]|table|ol|ul|blockquote)[^>]*>/i', "\n", $string);
  23. $string = preg_replace('/<\/(tr|li)[^>]*>/i', "\n", $string);
  24. $string = preg_replace('/<\/(td|th)[^>]*>/i', "\t", $string);
  25. $string = strip_tags($string);
  26. // Line endings unification and cleaning.
  27. $string = str_replace(array("\r\n", "\n\r", "\r"), "\n", $string);
  28. $string = preg_replace('/\s*\n/', "\n", $string);
  29. $string = preg_replace('/\n+/', "\n", $string);
  30. return trim($string);
  31. }
  32. /**
  33. * Detects encoding of html-formatted text.
  34. * @param string $string The input html-formatted text.
  35. * @return string Returns the detected encoding.
  36. */
  37. static function api_detect_encoding_html($string)
  38. {
  39. if (@preg_match('/<head.*(<meta[^>]*content=[^>]*>).*<\/head>/si', $string, $matches)) {
  40. if (@preg_match('/<meta[^>]*charset=(.*)["\';][^>]*>/si', $matches[1], $matches)) {
  41. return api_refine_encoding_id(trim($matches[1]));
  42. }
  43. }
  44. return api_detect_encoding(self::api_html_to_text($string));
  45. }
  46. /**
  47. * Converts the text of a html-document to a given encoding, the meta-tag is changed accordingly.
  48. * @param string $string The input full-html document.
  49. * @param string The new encoding value to be set.
  50. */
  51. static function api_set_encoding_html(&$string, $encoding) {
  52. $old_encoding = self::api_detect_encoding_html($string);
  53. if (@preg_match('/(.*<head.*)(<meta[^>]*content=[^>]*>)(.*<\/head>.*)/si', $string, $matches)) {
  54. $meta = $matches[2];
  55. if (@preg_match("/(<meta[^>]*charset=)(.*)([\"';][^>]*>)/si", $meta, $matches1)) {
  56. $meta = $matches1[1] . $encoding . $matches1[3];
  57. $string = $matches[1] . $meta . $matches[3];
  58. } else {
  59. $string = $matches[1] . '<meta http-equiv="Content-Type" content="text/html; charset=' . $encoding . '"/>' . $matches[3];
  60. }
  61. } else {
  62. $count = 1;
  63. $string = str_ireplace('</head>', '<meta http-equiv="Content-Type" content="text/html; charset=' . $encoding . '"/></head>', $string, $count);
  64. }
  65. $string = api_convert_encoding($string, $encoding, $old_encoding);
  66. }
  67. /**
  68. * Returns the title of a html document.
  69. * @param string $string The contents of the input document.
  70. * @param string $input_encoding The encoding of the input document. If the value is not set, it is detected.
  71. * @param string $$output_encoding The encoding of the retrieved title. If the value is not set, the system encoding is assumend.
  72. * @return string The retrieved title, html-entities and extra-whitespace between the words are cleaned.
  73. */
  74. static function api_get_title_html(&$string, $output_encoding = null, $input_encoding = null)
  75. {
  76. if (@preg_match('/<head.+<title[^>]*>(.*)<\/title>/msi', $string, $matches)) {
  77. if (empty($output_encoding)) {
  78. $output_encoding = api_get_system_encoding();
  79. }
  80. if (empty($input_encoding)) {
  81. $input_encoding = self::api_detect_encoding_html($string);
  82. }
  83. return trim(@preg_replace('/\s+/', ' ', api_html_entity_decode(api_convert_encoding($matches[1], $output_encoding, $input_encoding), ENT_QUOTES, $output_encoding)));
  84. }
  85. return '';
  86. }
  87. /**
  88. * Detects encoding of xml-formatted text.
  89. * @param string $string The input xml-formatted text.
  90. * @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.
  91. * @return string Returns the detected encoding.
  92. * @todo The second parameter is to be eliminated. See api_detect_encoding_html().
  93. */
  94. static function api_detect_encoding_xml($string, $default_encoding = null) {
  95. if (preg_match(_PCRE_XML_ENCODING, $string, $matches)) {
  96. return api_refine_encoding_id($matches[1]);
  97. }
  98. if (api_is_valid_utf8($string)) {
  99. return 'UTF-8';
  100. }
  101. if (empty($default_encoding)) {
  102. $default_encoding = _api_mb_internal_encoding();
  103. }
  104. return api_refine_encoding_id($default_encoding);
  105. }
  106. /**
  107. * Converts character encoding of a xml-formatted text. If inside the text the encoding is declared, it is modified accordingly.
  108. * @param string $string The text being converted.
  109. * @param string $to_encoding The encoding that text is being converted to.
  110. * @param string $from_encoding (optional) The encoding that text is being converted from. If it is omited, it is tried to be detected then.
  111. * @return string Returns the converted xml-text.
  112. */
  113. static function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null) {
  114. return self::_api_convert_encoding_xml($string, $to_encoding, $from_encoding);
  115. }
  116. /**
  117. * 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.
  118. * @param string $string The text being converted.
  119. * @param string $from_encoding (optional) The encoding that text is being converted from. If it is omited, it is tried to be detected then.
  120. * @return string Returns the converted xml-text.
  121. */
  122. static function api_utf8_encode_xml($string, $from_encoding = null) {
  123. return self::_api_convert_encoding_xml($string, 'UTF-8', $from_encoding);
  124. }
  125. /**
  126. * 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.
  127. * @param string $string The text being converted.
  128. * @param string $to_encoding (optional) The encoding that text is being converted to. If it is omited, the platform character set is assumed.
  129. * @return string Returns the converted xml-text.
  130. */
  131. static function api_utf8_decode_xml($string, $to_encoding = null) {
  132. if (empty($to_encoding)) {
  133. $to_encoding = _api_mb_internal_encoding();
  134. }
  135. return self::_api_convert_encoding_xml($string, $to_encoding, 'UTF-8');
  136. }
  137. /**
  138. * Converts character encoding of a xml-formatted text. If inside the text the encoding is declared, it is modified accordingly.
  139. * @param string $string The text being converted.
  140. * @param string $to_encoding The encoding that text is being converted to.
  141. * @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.
  142. * @return string Returns the converted xml-text.
  143. */
  144. static function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding) {
  145. if (empty($from_encoding)) {
  146. $from_encoding = self::api_detect_encoding_xml($string);
  147. }
  148. $to_encoding = api_refine_encoding_id($to_encoding);
  149. if (!preg_match('/<\?xml.*\?>/m', $string, $matches)) {
  150. return api_convert_encoding('<?xml version="1.0" encoding="' . $to_encoding . '"?>' . "\n" . $string, $to_encoding, $from_encoding);
  151. }
  152. if (!preg_match(_PCRE_XML_ENCODING, $string)) {
  153. if (strpos($matches[0], 'standalone') !== false) {
  154. // The encoding option should precede the standalone option, othewise DOMDocument fails to load the document.
  155. $replace = str_replace('standalone', ' encoding="' . $to_encoding . '" standalone', $matches[0]);
  156. } else {
  157. $replace = str_replace('?>', ' encoding="' . $to_encoding . '"?>', $matches[0]);
  158. }
  159. return api_convert_encoding(str_replace($matches[0], $replace, $string), $to_encoding, $from_encoding);
  160. }
  161. global $_api_encoding;
  162. $_api_encoding = api_refine_encoding_id($to_encoding);
  163. return api_convert_encoding(preg_replace_callback(_PCRE_XML_ENCODING, array('Text', '_api_convert_encoding_xml_callback'), $string), $to_encoding, $from_encoding);
  164. }
  165. /**
  166. * A callback for serving the function _api_convert_encoding_xml().
  167. * @param array $matches Input array of matches corresponding to the xml-declaration.
  168. * @return string Returns the xml-declaration with modified encoding.
  169. */
  170. static function _api_convert_encoding_xml_callback($matches) {
  171. global $_api_encoding;
  172. return str_replace($matches[1], $_api_encoding, $matches[0]);
  173. }
  174. /* CSV processing functions */
  175. /**
  176. * Parses CSV data (one line) into an array. This function is not affected by the OS-locale settings.
  177. * @param string $string The input string.
  178. * @param string $delimiter (optional) The field delimiter, one character only. The default delimiter character is comma {,).
  179. * @param string $enclosure (optional) The field enclosure, one character only. The default enclosure character is quote (").
  180. * @param string $escape (optional) The escape character, one character only. The default escape character is backslash (\).
  181. * @return array Returns an array containing the fields read.
  182. * Note: In order this function to work correctly with UTF-8, limitation for the parameters $delimiter, $enclosure and $escape
  183. * should be kept. These parameters should be single ASCII characters only. Thus the implementation of this function is faster.
  184. * @link http://php.net/manual/en/function.str-getcsv.php (exists as of PHP 5 >= 5.3.0)
  185. */
  186. static function & api_str_getcsv(& $string, $delimiter = ',', $enclosure = '"', $escape = '\\') {
  187. $delimiter = (string) $delimiter;
  188. if (api_byte_count($delimiter) > 1) {
  189. $delimiter = $delimiter[1];
  190. }
  191. $enclosure = (string) $enclosure;
  192. if (api_byte_count($enclosure) > 1) {
  193. $enclosure = $enclosure[1];
  194. }
  195. $escape = (string) $escape;
  196. if (api_byte_count($escape) > 1) {
  197. $escape = $escape[1];
  198. }
  199. $str = (string) $string;
  200. $len = api_byte_count($str);
  201. $enclosed = false;
  202. $escaped = false;
  203. $value = '';
  204. $result = array();
  205. for ($i = 0; $i < $len; $i++) {
  206. $char = $str[$i];
  207. if ($char == $escape) {
  208. if (!$escaped) {
  209. $escaped = true;
  210. continue;
  211. }
  212. }
  213. $escaped = false;
  214. switch ($char) {
  215. case $enclosure:
  216. if ($enclosed && $str[$i + 1] == $enclosure) {
  217. $value .= $char;
  218. $i++;
  219. } else {
  220. $enclosed = !$enclosed;
  221. }
  222. break;
  223. case $delimiter:
  224. if (!$enclosed) {
  225. $result[] = $value;
  226. $value = '';
  227. } else {
  228. $value .= $char;
  229. }
  230. break;
  231. default:
  232. $value .= $char;
  233. break;
  234. }
  235. }
  236. if (!empty($value)) {
  237. $result[] = $value;
  238. }
  239. return $result;
  240. }
  241. /**
  242. * Reads a line from a file pointer and parses it for CSV fields. This function is not affected by the OS-locale settings.
  243. * @param resource $handle The file pointer, it must be valid and must point to a file successfully opened by fopen().
  244. * @param int $length (optional) Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).
  245. * If no length is specified, it will keep reading from the stream until it reaches the end of the line.
  246. * @param string $delimiter (optional) The field delimiter, one character only. The default delimiter character is comma {,).
  247. * @param string $enclosure (optional) The field enclosure, one character only. The default enclosure character is quote (").
  248. * @param string $escape (optional) The escape character, one character only. The default escape character is backslash (\).
  249. * @return array Returns an array containing the fields read.
  250. * Note: In order this function to work correctly with UTF-8, limitation for the parameters $delimiter, $enclosure and $escape
  251. * should be kept. These parameters should be single ASCII characters only.
  252. * @link http://php.net/manual/en/function.fgetcsv.php
  253. */
  254. static function api_fgetcsv($handle, $length = null, $delimiter = ',', $enclosure = '"', $escape = '\\') {
  255. if (($line = is_null($length) ? fgets($handle) : fgets($handle, $length)) !== false) {
  256. $line = rtrim($line, "\r\n");
  257. return self::api_str_getcsv($line, $delimiter, $enclosure, $escape);
  258. }
  259. return false;
  260. }
  261. /* Functions for supporting ASCIIMathML mathematical formulas and ASCIIsvg maathematical graphics */
  262. /**
  263. * Dectects ASCIIMathML formula presence within a given html text.
  264. * @param string $html The input html text.
  265. * @return bool Returns TRUE when there is a formula found or FALSE otherwise.
  266. */
  267. static function api_contains_asciimathml($html) {
  268. if (!preg_match_all('/<span[^>]*class\s*=\s*[\'"](.*?)[\'"][^>]*>/mi', $html, $matches)) {
  269. return false;
  270. }
  271. foreach ($matches[1] as $string) {
  272. $string = ' ' . str_replace(',', ' ', $string) . ' ';
  273. if (preg_match('/\sAM\s/m', $string)) {
  274. return true;
  275. }
  276. }
  277. return false;
  278. }
  279. /**
  280. * Dectects ASCIIsvg graphics presence within a given html text.
  281. * @param string $html The input html text.
  282. * @return bool Returns TRUE when there is a graph found or FALSE otherwise.
  283. */
  284. static function api_contains_asciisvg($html) {
  285. if (!preg_match_all('/<embed([^>]*?)>/mi', $html, $matches)) {
  286. return false;
  287. }
  288. foreach ($matches[1] as $string) {
  289. $string = ' ' . str_replace(',', ' ', $string) . ' ';
  290. if (preg_match('/sscr\s*=\s*[\'"](.*?)[\'"]/m', $string)) {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296. /* Miscellaneous text processing functions */
  297. /**
  298. * Convers a string from camel case into underscore.
  299. * Works correctly with ASCII strings only, implementation for human-language strings is not necessary.
  300. * @param string $string The input string (ASCII)
  301. * @return string The converted result string
  302. */
  303. static function api_camel_case_to_underscore($string) {
  304. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $string));
  305. }
  306. /**
  307. * Converts a string with underscores into camel case.
  308. * Works correctly with ASCII strings only, implementation for human-language strings is not necessary.
  309. * @param string $string The input string (ASCII)
  310. * @param bool $capitalise_first_char (optional) If true (default), the function capitalises the first char in the result string.
  311. * @return string The converted result string
  312. */
  313. static function api_underscore_to_camel_case($string, $capitalise_first_char = true) {
  314. if ($capitalise_first_char) {
  315. $string = ucfirst($string);
  316. }
  317. return preg_replace_callback('/_([a-z])/', array('Text', '_api_camelize'), $string);
  318. }
  319. // A function for internal use, only for this library.
  320. static function _api_camelize($match) {
  321. return strtoupper($match[1]);
  322. }
  323. /**
  324. * Truncates a string.
  325. *
  326. * @author Brouckaert Olivier
  327. * @param string $text The text to truncate.
  328. * @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.
  329. * @param string $suffix A suffix to be added as a replacement.
  330. * @param string $encoding (optional) The encoding to be used. If it is omitted, the platform character set will be used by default.
  331. * @param boolean $middle If this parameter is true, truncation is done in the middle of the string.
  332. * @return string Truncated string, decorated with the given suffix (replacement).
  333. */
  334. static function api_trunc_str($text, $length = 30, $suffix = '...', $middle = false, $encoding = null) {
  335. if (empty($encoding)) {
  336. $encoding = api_get_system_encoding();
  337. }
  338. $text_length = api_strlen($text, $encoding);
  339. if ($text_length <= $length) {
  340. return $text;
  341. }
  342. if ($middle) {
  343. return rtrim(api_substr($text, 0, round($length / 2), $encoding)) . $suffix . ltrim(api_substr($text, - round($length / 2), $text_length, $encoding));
  344. }
  345. return rtrim(api_substr($text, 0, $length, $encoding)) . $suffix;
  346. }
  347. /**
  348. * Handling simple and double apostrofe in order that strings be stored properly in database
  349. *
  350. * @author Denes Nagy
  351. * @param string variable - the variable to be revised
  352. */
  353. static function domesticate($input) {
  354. $input = stripslashes($input);
  355. $input = str_replace("'", "''", $input);
  356. $input = str_replace('"', "''", $input);
  357. return ($input);
  358. }
  359. /**
  360. * function make_clickable($string)
  361. *
  362. * @desc Completes url contained in the text with "<a href ...".
  363. * However the function simply returns the submitted text without any
  364. * transformation if it already contains some "<a href:" or "<img src=".
  365. * @param string $text text to be converted
  366. * @return text after conversion
  367. * See http://php.net/manual/fr/function.eregi-replace.php
  368. *
  369. * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
  370. * to that URL
  371. * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
  372. * to http://www.xxxx.yyyy[/zzzz]
  373. * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
  374. * to that email address
  375. * - Only matches these 2 patterns either after a space, or at the beginning of a line
  376. *
  377. */
  378. static function make_clickable($text) {
  379. $regex = '/(\S+@\S+\.\S+)/i';
  380. $replace = "<a href='mailto:$1'>$1</a>";
  381. $result = preg_replace($regex, $replace, $text);
  382. return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $result);
  383. }
  384. /**
  385. * This functions cuts a paragraph
  386. * i.e cut('Merry Xmas from Lima',13) = "Merry Xmas fr..."
  387. * @param string The text to "cut"
  388. * @param int Count of chars
  389. * @param bool Whether to embed in a <span title="...">...</span>
  390. * @return string
  391. * */
  392. static function cut($text, $maxchar, $embed = false) {
  393. if (api_strlen($text) > $maxchar) {
  394. if ($embed) {
  395. return '<span title="' . $text . '">' . api_substr($text, 0, $maxchar) . '...</span>';
  396. }
  397. return api_substr($text, 0, $maxchar) . ' ...';
  398. }
  399. return $text;
  400. }
  401. /**
  402. * Show a number as only integers if no decimals, but will show 2 decimals if exist.
  403. *
  404. * @param mixed Number to convert
  405. * @param int Decimal points 0=never, 1=if needed, 2=always
  406. * @return mixed An integer or a float depends on the parameter
  407. */
  408. static function float_format($number, $flag = 1) {
  409. if (is_numeric($number)) {
  410. if (!$number) {
  411. $result = ($flag == 2 ? '0.' . str_repeat('0', EXERCISE_NUMBER_OF_DECIMALS) : '0');
  412. } else {
  413. if (floor($number) == $number) {
  414. $result = number_format($number, ($flag == 2 ? EXERCISE_NUMBER_OF_DECIMALS : 0));
  415. } else {
  416. $result = number_format(round($number, 2), ($flag == 0 ? 0 : EXERCISE_NUMBER_OF_DECIMALS));
  417. }
  418. }
  419. return $result;
  420. }
  421. }
  422. // TODO: To be checked for correct timezone management.
  423. /**
  424. * Function to obtain last week timestamps
  425. * @return array Times for every day inside week
  426. */
  427. static function get_last_week() {
  428. $week = date('W');
  429. $year = date('Y');
  430. $lastweek = $week - 1;
  431. if ($lastweek == 0) {
  432. $week = 52;
  433. $year--;
  434. }
  435. $lastweek = sprintf("%02d", $lastweek);
  436. $arrdays = array();
  437. for ($i = 1; $i <= 7; $i++) {
  438. $arrdays[] = strtotime("$year" . "W$lastweek" . "$i");
  439. }
  440. return $arrdays;
  441. }
  442. /**
  443. * Gets the week from a day
  444. * @param string Date in UTC (2010-01-01 12:12:12)
  445. * @return int Returns an integer with the week number of the year
  446. */
  447. static function get_week_from_day($date) {
  448. if (!empty($date)) {
  449. $time = api_strtotime($date, 'UTC');
  450. return date('W', $time);
  451. } else {
  452. return date('W');
  453. }
  454. }
  455. /**
  456. * This function splits the string into words and then joins them back together again one by one.
  457. * Example: "Test example of a long string"
  458. * substrwords(5) = Test ... *
  459. * @param string
  460. * @param int the max number of character
  461. * @param string how the string will be end
  462. * @return a reduce string
  463. */
  464. static function substrwords($text, $maxchar, $end = '...') {
  465. if (strlen($text) > $maxchar) {
  466. $words = explode(" ", $text);
  467. $output = '';
  468. $i = 0;
  469. while (1) {
  470. $length = (strlen($output) + strlen($words[$i]));
  471. if ($length > $maxchar) {
  472. break;
  473. } else {
  474. $output = $output . " " . $words[$i];
  475. $i++;
  476. };
  477. };
  478. } else {
  479. $output = $text;
  480. return $output;
  481. }
  482. return $output . $end;
  483. }
  484. static function implode_with_key($glue, $array) {
  485. if (!empty($array)) {
  486. $string = '';
  487. foreach ($array as $key => $value) {
  488. if (empty($value)) {
  489. $value = 'null';
  490. }
  491. $string .= $key . " : " . $value . " $glue ";
  492. }
  493. return $string;
  494. }
  495. return '';
  496. }
  497. /**
  498. * function string2binary converts the string "true" or "false" to the boolean true false (0 or 1)
  499. * This is used for the Chamilo Config Settings as these store true or false as string
  500. * and the api_get_setting('course_create_active_tools') should be 0 or 1 (used for
  501. * the visibility of the tool)
  502. * @param string $variable
  503. * @author Patrick Cool, patrick.cool@ugent.be
  504. */
  505. static function string2binary($variable) {
  506. if ($variable == 'true') {
  507. return true;
  508. }
  509. if ($variable == 'false') {
  510. return false;
  511. }
  512. }
  513. /**
  514. * Transform the file size in a human readable format.
  515. *
  516. * @param int Size of the file in bytes
  517. * @return string A human readable representation of the file size
  518. */
  519. static function format_file_size($file_size) {
  520. $file_size = intval($file_size);
  521. if($file_size >= 1073741824) {
  522. $file_size = round($file_size / 1073741824 * 100) / 100 . 'G';
  523. } elseif($file_size >= 1048576) {
  524. $file_size = round($file_size / 1048576 * 100) / 100 . 'M';
  525. } elseif($file_size >= 1024) {
  526. $file_size = round($file_size / 1024 * 100) / 100 . 'k';
  527. } else {
  528. $file_size = $file_size . 'B';
  529. }
  530. return $file_size;
  531. }
  532. /**
  533. * @param $array
  534. * @return string
  535. */
  536. static function return_datetime_from_array($array)
  537. {
  538. $year = '0000';
  539. $month = $day = $hours = $minutes = $seconds = '00';
  540. if (isset($array['Y']) && (isset($array['F']) || isset($array['M'])) && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
  541. $year = $array['Y'];
  542. $month = isset($array['F'])?$array['F']:$array['M'];
  543. if (intval($month) < 10 ) $month = '0'.$month;
  544. $day = $array['d'];
  545. if (intval($day) < 10 ) $day = '0'.$day;
  546. $hours = $array['H'];
  547. if (intval($hours) < 10 ) $hours = '0'.$hours;
  548. $minutes = $array['i'];
  549. if (intval($minutes) < 10 ) $minutes = '0'.$minutes;
  550. }
  551. if (checkdate($month,$day,$year)) {
  552. $datetime = $year.'-'.$month.'-'.$day.' '.$hours.':'.$minutes.':'.$seconds;
  553. }
  554. return $datetime;
  555. }
  556. /**
  557. * Converts 2008-10-06 12:45:00 to -> array('prefix' => array(year'=>2008, 'month'=>10, etc...)
  558. * @param string
  559. * @param string
  560. * @param array
  561. */
  562. static function convert_date_to_array($date, $group)
  563. {
  564. $parts = explode(' ', $date);
  565. $date_parts = explode('-', $parts[0]);
  566. $date_parts_tmp = array();
  567. foreach ($date_parts as $item) {
  568. $date_parts_tmp[] = intval($item);
  569. }
  570. $time_parts = explode(':', $parts[1]);
  571. $time_parts_tmp = array();
  572. foreach ($time_parts as $item) {
  573. $time_parts_tmp[] = intval($item);
  574. }
  575. list($data[$group]['year'], $data[$group]['month'], $data[$group]['day']) = $date_parts_tmp;
  576. list($data[$group]['hour'], $data[$group]['minute']) = $time_parts_tmp;
  577. return $data;
  578. }
  579. /**
  580. * converts 1-9 to 01-09
  581. */
  582. static function two_digits($number)
  583. {
  584. $number = (int)$number;
  585. return ($number < 10) ? '0'.$number : $number;
  586. }
  587. /**
  588. * Converts an string CLEANYO[admin][amann,acostea]
  589. * into an array:
  590. *
  591. * array(
  592. * CLEANYO
  593. * admin
  594. * amann,acostea
  595. * )
  596. *
  597. * @param $array
  598. * @return array
  599. */
  600. function bracketsToArray($array)
  601. {
  602. return preg_split('/[\[\]]+/', $array, -1, PREG_SPLIT_NO_EMPTY);
  603. }
  604. }