text.lib.php 26 KB

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