123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836 |
- <?php
- define('EXERCISE_NUMBER_OF_DECIMALS', 2);
- function api_html_to_text($string)
- {
-
- $string = preg_replace('/<br[^>]*>/i', "\n", $string);
- $string = preg_replace('/<\/?(div|p|h[1-6]|table|ol|ul|blockquote)[^>]*>/i', "\n", $string);
- $string = preg_replace('/<\/(tr|li)[^>]*>/i', "\n", $string);
- $string = preg_replace('/<\/(td|th)[^>]*>/i', "\t", $string);
- $string = strip_tags($string);
-
- $string = str_replace(array("\r\n", "\n\r", "\r"), "\n", $string);
- $string = preg_replace('/\s*\n/', "\n", $string);
- $string = preg_replace('/\n+/', "\n", $string);
- return trim($string);
- }
- function api_detect_encoding_html($string)
- {
- if (@preg_match('/<head.*(<meta[^>]*content=[^>]*>).*<\/head>/si', $string, $matches)) {
- if (@preg_match('/<meta[^>]*charset=(.*)["\';][^>]*>/si', $matches[1], $matches)) {
- return api_refine_encoding_id(trim($matches[1]));
- }
- }
- return api_detect_encoding(api_html_to_text($string));
- }
- function api_set_encoding_html(&$string, $encoding)
- {
- $old_encoding = api_detect_encoding_html($string);
- if (@preg_match('/(.*<head.*)(<meta[^>]*content=[^>]*>)(.*<\/head>.*)/si', $string, $matches)) {
- $meta = $matches[2];
- if (@preg_match("/(<meta[^>]*charset=)(.*)([\"';][^>]*>)/si", $meta, $matches1)) {
- $meta = $matches1[1].$encoding.$matches1[3];
- $string = $matches[1].$meta.$matches[3];
- } else {
- $string = $matches[1].'<meta http-equiv="Content-Type" content="text/html; charset='.$encoding.'"/>'.$matches[3];
- }
- } else {
- $count = 1;
- $string = str_ireplace('</head>', '<meta http-equiv="Content-Type" content="text/html; charset='.$encoding.'"/></head>', $string, $count);
- }
- $string = api_convert_encoding($string, $encoding, $old_encoding);
- }
- function api_get_title_html(&$string, $output_encoding = null, $input_encoding = null)
- {
- if (@preg_match('/<head.+<title[^>]*>(.*)<\/title>/msi', $string, $matches)) {
- if (empty($output_encoding)) {
- $output_encoding = api_get_system_encoding();
- }
- if (empty($input_encoding)) {
- $input_encoding = api_detect_encoding_html($string);
- }
- return trim(@preg_replace('/\s+/', ' ', api_html_entity_decode(api_convert_encoding($matches[1], $output_encoding, $input_encoding), ENT_QUOTES, $output_encoding)));
- }
- return '';
- }
- define('_PCRE_XML_ENCODING', '/<\?xml.*encoding=[\'"](.*?)[\'"].*\?>/m');
- function api_detect_encoding_xml($string, $default_encoding = null) {
- if (preg_match(_PCRE_XML_ENCODING, $string, $matches)) {
- return api_refine_encoding_id($matches[1]);
- }
- if (api_is_valid_utf8($string)) {
- return 'UTF-8';
- }
- if (empty($default_encoding)) {
- $default_encoding = _api_mb_internal_encoding();
- }
- return api_refine_encoding_id($default_encoding);
- }
- function api_convert_encoding_xml($string, $to_encoding, $from_encoding = null) {
- return _api_convert_encoding_xml($string, $to_encoding, $from_encoding);
- }
- function api_utf8_encode_xml($string, $from_encoding = null) {
- return _api_convert_encoding_xml($string, 'UTF-8', $from_encoding);
- }
- function api_utf8_decode_xml($string, $to_encoding = 'UTF-8') {
- return _api_convert_encoding_xml($string, $to_encoding, 'UTF-8');
- }
- function _api_convert_encoding_xml(&$string, $to_encoding, $from_encoding) {
- if (empty($from_encoding)) {
- $from_encoding = api_detect_encoding_xml($string);
- }
- $to_encoding = api_refine_encoding_id($to_encoding);
- if (!preg_match('/<\?xml.*\?>/m', $string, $matches)) {
- return api_convert_encoding('<?xml version="1.0" encoding="'.$to_encoding.'"?>'."\n".$string, $to_encoding, $from_encoding);
- }
- if (!preg_match(_PCRE_XML_ENCODING, $string)) {
- if (strpos($matches[0], 'standalone') !== false) {
-
- $replace = str_replace('standalone', ' encoding="'.$to_encoding.'" standalone', $matches[0]);
- } else {
- $replace = str_replace('?>', ' encoding="'.$to_encoding.'"?>', $matches[0]);
- }
- return api_convert_encoding(str_replace($matches[0], $replace, $string), $to_encoding, $from_encoding);
- }
- global $_api_encoding;
- $_api_encoding = api_refine_encoding_id($to_encoding);
- return api_convert_encoding(preg_replace_callback(_PCRE_XML_ENCODING, '_api_convert_encoding_xml_callback', $string), $to_encoding, $from_encoding);
- }
- function _api_convert_encoding_xml_callback($matches) {
- global $_api_encoding;
- return str_replace($matches[1], $_api_encoding, $matches[0]);
- }
- function api_contains_asciimathml($html) {
- if (!preg_match_all('/<span[^>]*class\s*=\s*[\'"](.*?)[\'"][^>]*>/mi', $html, $matches)) {
- return false;
- }
- foreach ($matches[1] as $string) {
- $string = ' '.str_replace(',', ' ', $string).' ';
- if (preg_match('/\sAM\s/m', $string)) {
- return true;
- }
- }
- return false;
- }
- function api_contains_asciisvg($html)
- {
- if (!preg_match_all('/<embed([^>]*?)>/mi', $html, $matches)) {
- return false;
- }
- foreach ($matches[1] as $string) {
- $string = ' '.str_replace(',', ' ', $string).' ';
- if (preg_match('/sscr\s*=\s*[\'"](.*?)[\'"]/m', $string)) {
- return true;
- }
- }
- return false;
- }
- function api_camel_case_to_underscore($string)
- {
- return strtolower(preg_replace('/([a-z])([A-Z])/', "$1_$2", $string));
- }
- function api_underscore_to_camel_case($string, $capitalise_first_char = true)
- {
- if ($capitalise_first_char) {
- $string = ucfirst($string);
- }
- return preg_replace_callback('/_([a-z])/', '_api_camelize', $string);
- }
- function _api_camelize($match)
- {
- return strtoupper($match[1]);
- }
- function api_trunc_str($text, $length = 30, $suffix = '...', $middle = false, $encoding = null)
- {
- if (empty($encoding)) {
- $encoding = api_get_system_encoding();
- }
- $text_length = api_strlen($text, $encoding);
- if ($text_length <= $length) {
- return $text;
- }
- if ($middle) {
- return rtrim(api_substr($text, 0, round($length / 2), $encoding)).$suffix.ltrim(api_substr($text, - round($length / 2), $text_length, $encoding));
- }
- return rtrim(api_substr($text, 0, $length, $encoding)).$suffix;
- }
- function domesticate($input)
- {
- $input = stripslashes($input);
- $input = str_replace("'", "''", $input);
- $input = str_replace('"', "''", $input);
- return ($input);
- }
- function _make_url_clickable_cb($matches) {
- $url = $matches[2];
- if (')' == $matches[3] && strpos($url, '(')) {
-
-
- $url .= $matches[3];
- $suffix = '';
- } else {
- $suffix = $matches[3];
- }
-
- while (substr_count($url, '(') < substr_count($url, ')')) {
- $suffix = strrchr($url, ')').$suffix;
- $url = substr($url, 0, strrpos($url, ')'));
- }
- $url = esc_url($url);
- if (empty($url))
- return $matches[0];
- return $matches[1]."<a href=\"$url\" rel=\"nofollow\">$url</a>".$suffix;
- }
- function esc_url($url, $protocols = null, $_context = 'display') {
-
- if ('' == $url)
- return $url;
- $url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
- $strip = array('%0d', '%0a', '%0D', '%0A');
- $url = _deep_replace($strip, $url);
- $url = str_replace(';//', '://', $url);
-
- if (strpos($url, ':') === false && !in_array($url[0], array('/', '#', '?')) &&
- !preg_match('/^[a-z0-9-]+?\.php/i', $url))
- $url = 'http://'.$url;
- return Security::remove_XSS($url);
-
-
- }
- function _deep_replace($search, $subject) {
- $subject = (string) $subject;
- $count = 1;
- while ($count) {
- $subject = str_replace($search, '', $subject, $count);
- }
- return $subject;
- }
- function _make_web_ftp_clickable_cb($matches) {
- $ret = '';
- $dest = $matches[2];
- $dest = 'http://'.$dest;
- $dest = esc_url($dest);
- if (empty($dest))
- return $matches[0];
-
- if (in_array(substr($dest, -1), array('.', ',', ';', ':', ')')) === true) {
- $ret = substr($dest, -1);
- $dest = substr($dest, 0, strlen($dest) - 1);
- }
- return $matches[1]."<a href=\"$dest\" rel=\"nofollow\">$dest</a>$ret";
- }
- function _make_email_clickable_cb($matches) {
- $email = $matches[2].'@'.$matches[3];
- return $matches[1]."<a href=\"mailto:$email\">$email</a>";
- }
- function make_clickable($text) {
- $r = '';
- $textarr = preg_split('/(<[^<>]+>)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
- $nested_code_pre = 0;
- foreach ($textarr as $piece) {
- if (preg_match('|^<code[\s>]|i', $piece) || preg_match('|^<pre[\s>]|i', $piece))
- $nested_code_pre++;
- elseif (('</code>' === strtolower($piece) || '</pre>' === strtolower($piece)) && $nested_code_pre)
- $nested_code_pre--;
- if ($nested_code_pre || empty($piece) || ($piece[0] === '<' && !preg_match('|^<\s*[\w]{1,20}+://|', $piece))) {
- $r .= $piece;
- continue;
- }
-
- if (10000 < strlen($piece)) {
-
- foreach (_split_str_by_whitespace($piece, 2100) as $chunk) {
- if (2101 < strlen($chunk)) {
- $r .= $chunk;
- } else {
- $r .= make_clickable($chunk);
- }
- }
- } else {
- $ret = " $piece ";
- $url_clickable = '~
- ([\\s(<.,;:!?]) # 1: Leading whitespace, or punctuation
- ( # 2: URL
- [\\w]{1,20}+:// # Scheme and hier-part prefix
- (?=\S{1,2000}\s) # Limit to URLs less than about 2000 characters long
- [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]*+ # Non-punctuation URL character
- (?: # Unroll the Loop: Only allow puctuation URL character if followed by a non-punctuation URL character
- [\'.,;:!?)] # Punctuation URL character
- [\\w\\x80-\\xff#%\\~/@\\[\\]*(+=&$-]++ # Non-punctuation URL character
- )*
- )
- (\)?) # 3: Trailing closing parenthesis (for parethesis balancing post processing)
- ~xS';
-
- $ret = preg_replace_callback($url_clickable, '_make_url_clickable_cb', $ret);
- $ret = preg_replace_callback('#([\s>])((www|ftp)\.[\w\\x80-\\xff\#$%&~/.\-;:=,?@\[\]+]+)#is', '_make_web_ftp_clickable_cb', $ret);
- $ret = preg_replace_callback('#([\s>])([.0-9a-z_+-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,})#i', '_make_email_clickable_cb', $ret);
- $ret = substr($ret, 1, -1);
- $r .= $ret;
- }
- }
-
- $r = preg_replace('#(<a([ \r\n\t]+[^>]+?>|>))<a [^>]+?>([^>]+?)</a></a>#i', "$1$3</a>", $r);
- return $r;
- }
- function _split_str_by_whitespace($string, $goal) {
- $chunks = array();
- $string_nullspace = strtr($string, "\r\n\t\v\f ", "\000\000\000\000\000\000");
- while ($goal < strlen($string_nullspace)) {
- $pos = strrpos(substr($string_nullspace, 0, $goal + 1), "\000");
- if (false === $pos) {
- $pos = strpos($string_nullspace, "\000", $goal + 1);
- if (false === $pos) {
- break;
- }
- }
- $chunks[] = substr($string, 0, $pos + 1);
- $string = substr($string, $pos + 1);
- $string_nullspace = substr($string_nullspace, $pos + 1);
- }
- if ($string) {
- $chunks[] = $string;
- }
- return $chunks;
- }
- function cut($text, $maxchar, $embed = false) {
- if (api_strlen($text) > $maxchar) {
- if ($embed) {
- return '<p title="'.$text.'">'.api_substr($text, 0, $maxchar).'...</p>';
- }
- return api_substr($text, 0, $maxchar).' ...';
- }
- return $text;
- }
- function float_format($number, $flag = 1) {
- if (is_numeric($number)) {
- if (!$number) {
- $result = ($flag == 2 ? '0.'.str_repeat('0', EXERCISE_NUMBER_OF_DECIMALS) : '0');
- } else {
- if (floor($number) == $number) {
- $result = number_format($number, ($flag == 2 ? EXERCISE_NUMBER_OF_DECIMALS : 0));
- } else {
- $result = number_format(round($number, 2), ($flag == 0 ? 0 : EXERCISE_NUMBER_OF_DECIMALS));
- }
- }
- return $result;
- }
- }
- function get_last_week() {
- $week = date('W');
- $year = date('Y');
- $lastweek = $week - 1;
- if ($lastweek == 0) {
- $week = 52;
- $year--;
- }
- $lastweek = sprintf("%02d", $lastweek);
- $arrdays = array();
- for ($i = 1; $i <= 7; $i++) {
- $arrdays[] = strtotime("$year"."W$lastweek"."$i");
- }
- return $arrdays;
- }
- function get_week_from_day($date) {
- if (!empty($date)) {
- $time = api_strtotime($date, 'UTC');
- return date('W', $time);
- } else {
- return date('W');
- }
- }
- function substrwords($text, $maxchar, $end = '...')
- {
- if (strlen($text) > $maxchar) {
- $words = explode(" ", $text);
- $output = '';
- $i = 0;
- while (1) {
- $length = (strlen($output) + strlen($words[$i]));
- if ($length > $maxchar) {
- break;
- } else {
- $output = $output." ".$words[$i];
- $i++;
- }
- }
- } else {
- $output = $text;
- return $output;
- }
- return $output.$end;
- }
- function implode_with_key($glue, $array)
- {
- if (!empty($array)) {
- $string = '';
- foreach ($array as $key => $value) {
- if (empty($value)) {
- $value = 'null';
- }
- $string .= $key." : ".$value." $glue ";
- }
- return $string;
- }
- return '';
- }
- function format_file_size($file_size)
- {
- $file_size = intval($file_size);
- if ($file_size >= 1073741824) {
- $file_size = (round($file_size / 1073741824 * 100) / 100).'G';
- } elseif ($file_size >= 1048576) {
- $file_size = (round($file_size / 1048576 * 100) / 100).'M';
- } elseif ($file_size >= 1024) {
- $file_size = (round($file_size / 1024 * 100) / 100).'k';
- } else {
- $file_size = $file_size.'B';
- }
- return $file_size;
- }
- function return_datetime_from_array($array)
- {
- $year = '0000';
- $month = $day = $hours = $minutes = $seconds = '00';
- if (isset($array['Y']) && (isset($array['F']) || isset($array['M'])) && isset($array['d']) && isset($array['H']) && isset($array['i'])) {
- $year = $array['Y'];
- $month = isset($array['F']) ? $array['F'] : $array['M'];
- if (intval($month) < 10) $month = '0'.$month;
- $day = $array['d'];
- if (intval($day) < 10) $day = '0'.$day;
- $hours = $array['H'];
- if (intval($hours) < 10) $hours = '0'.$hours;
- $minutes = $array['i'];
- if (intval($minutes) < 10) $minutes = '0'.$minutes;
- }
- if (checkdate($month, $day, $year)) {
- $datetime = $year.'-'.$month.'-'.$day.' '.$hours.':'.$minutes.':'.$seconds;
- }
- return $datetime;
- }
- function bracketsToArray($array)
- {
- return preg_split('/[\[\]]+/', $array, -1, PREG_SPLIT_NO_EMPTY);
- }
- function underScoreToCamelCase($string, $capitalizeFirstCharacter = true)
- {
- $str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
- if (!$capitalizeFirstCharacter) {
- $str[0] = strtolower($str[0]);
- }
- return $str;
- }
|