1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22: 23: 24: 25: 26: 27: 28:
29:
30: 31: 32: 33: 34: 35:
36: function F_count_rows($dbtable, $where = '')
37: {
38: global $db;
39: require_once('../config/tce_config.php');
40: $numofrows = 0;
41: $sql = 'SELECT COUNT(*) AS numrows FROM '.$dbtable.' '.$where.'';
42: if ($r = F_db_query($sql, $db)) {
43: if ($m = F_db_fetch_array($r)) {
44: $numofrows = $m['numrows'];
45: }
46: } else {
47: F_display_db_error();
48: }
49: return($numofrows);
50: }
51:
52: 53: 54: 55: 56: 57:
58: function F_empty_to_null($str)
59: {
60: global $db;
61: require_once('../../shared/code/tce_db_dal.php');
62: if (strlen($str) > 0) {
63: return '\''.F_escape_sql($db, $str).'\'';
64: }
65: return 'NULL';
66: }
67:
68: 69: 70: 71: 72: 73:
74: function F_zero_to_null($num)
75: {
76: global $db;
77: require_once('../../shared/code/tce_db_dal.php');
78: if ($num == 0) {
79: return 'NULL';
80: }
81: return F_escape_sql($db, $num);
82: }
83:
84: 85: 86: 87: 88: 89:
90: function F_getBoolean($str)
91: {
92: if (is_bool($str)) {
93: return $str;
94: }
95: if (is_string($str) and ((strncasecmp($str, 't', 1) == 0) or (strncasecmp($str, '1', 1) == 0))) {
96: return true;
97: }
98: if (is_int($str) and ($str == 1)) {
99: return true;
100: }
101: return false;
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112: function F_check_unique($table, $where, $fieldname = false, $fieldid = false)
113: {
114: require_once('../config/tce_config.php');
115: global $l, $db;
116: $sqlc = 'SELECT * FROM '.$table.' WHERE '.$where.' LIMIT 1';
117: if ($rc = F_db_query($sqlc, $db)) {
118: if (($fieldname === false) and ($fieldid === false) and (F_count_rows($table, 'WHERE '.$where) > 0)) {
119: return false;
120: }
121: if ($mc = F_db_fetch_array($rc)) {
122: if ($mc[$fieldname] == $fieldid) {
123: return true;
124: }
125: } else {
126:
127: return true;
128: }
129: } else {
130: F_display_db_error();
131: }
132:
133: return false;
134: }
135:
136: 137: 138: 139: 140: 141:
142: function unhtmlentities($text_to_convert, $preserve_tagsign = false)
143: {
144: if ($preserve_tagsign) {
145: $text_to_convert = preg_replace('/\&([gl])t;/', '&\\1t;', $text_to_convert);
146: }
147: return @html_entity_decode($text_to_convert, ENT_NOQUOTES | ENT_XHTML, 'UTF-8');
148: }
149:
150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162:
163: function F_compact_string($string, $dquotes = false)
164: {
165: $repTable = array("\t" => ' ', "\n" => ' ', "\r" => ' ', "\0" => ' ', "\x0B" => ' ');
166: if ($dquotes) {
167: $repTable['"'] = '"';
168: }
169: return strtr($string, $repTable);
170: }
171:
172: 173: 174: 175: 176:
177: function F_replace_angulars($str)
178: {
179: $replaceTable = array('<' => '<', '>' => '>');
180: return strtr($str, $replaceTable);
181: }
182:
183: 184: 185: 186: 187: 188: 189:
190: function F_substr_utf8($str, $start = 0, $length)
191: {
192: $str .= '';
193: $bytelen = strlen($str);
194: $i = 0;
195: $j = 0;
196: $str_start = 0;
197: $str_end = $bytelen;
198: while ($i < $bytelen) {
199: if ($j == $start) {
200: $str_start = $i;
201: } elseif ($j == $length) {
202: $str_end = $i;
203: break;
204: }
205: $char = ord($str{$i});
206: if ($char <= 0x7F) {
207: $i += 1;
208: } elseif (($char >> 0x05) == 0x06) {
209: $i += 2;
210: } elseif (($char >> 0x04) == 0x0E) {
211: $i += 3;
212: } elseif (($char >> 0x03) == 0x1E) {
213: $i += 4;
214: } else {
215: $i += 1;
216: }
217: ++$j;
218: }
219: $str = substr($str, $str_start, $str_end);
220: return $str;
221: }
222:
223: 224: 225: 226: 227:
228: function F_text_to_xml($str)
229: {
230: $replaceTable = array("\0" => '', '&' => '&', '<' => '<', '>' => '>');
231: return strtr($str, $replaceTable);
232: }
233:
234: 235: 236: 237: 238:
239: function F_xml_to_text($str)
240: {
241: $replaceTable = array('&' => '&', '<' => '<', '>' => '>');
242: return strtr($str, $replaceTable);
243: }
244:
245: 246: 247: 248: 249:
250: function F_text_to_tsv($str)
251: {
252: $replaceTable = array("\0" => '', "\t" => '\t', "\n" => '\n', "\r" => '\r');
253: return strtr($str, $replaceTable);
254: }
255:
256: 257: 258: 259: 260:
261: function F_tsv_to_text($str)
262: {
263: $replaceTable = array('\t' => "\t", '\n' => "\n", '\r' => "\r");
264: return strtr($str, $replaceTable);
265: }
266:
267: 268: 269: 270: 271:
272: function showRequiredField($mode = 1)
273: {
274: global $l;
275: $str = '';
276: if ($mode == 2) {
277: $str = ' <acronym class="requiredonbox" title="'.$l['w_required'].'">+</acronym>';
278: } else {
279: $str = ' <acronym class="requiredoffbox" title="'.$l['w_not_required'].'">-</acronym>';
280: }
281: return $str;
282: }
283:
284: 285: 286: 287: 288:
289: function utrim($txt)
290: {
291: $txt = preg_replace('/\xA0/u', ' ', $txt);
292: $txt = preg_replace('/^([\s]+)/u', '', $txt);
293: $txt = preg_replace('/([\s]+)$/u', '', $txt);
294: return $txt;
295: }
296:
297: 298: 299: 300: 301: 302:
303: function getNormalizedIP($ip)
304: {
305: if (($ip == '0000:0000:0000:0000:0000:0000:0000:0001') or ($ip == '::1')) {
306:
307: $ip = '127.0.0.1';
308: }
309: $ip = strtolower($ip);
310:
311: if (($pos = strrpos($ip, '%')) !== false) {
312: $ip = substr($ip, 0, $pos);
313: }
314: if (($pos = strrpos($ip, '/')) !== false) {
315: $ip = substr($ip, 0, $pos);
316: }
317: $ip = preg_replace("/[^0-9a-f:\.]+/si", '', $ip);
318:
319: $is_ipv6 = (strpos($ip, ':') !== false);
320: $is_ipv4 = (strpos($ip, '.') !== false);
321: if ((!$is_ipv4) and (!$is_ipv6)) {
322: return false;
323: }
324: if ($is_ipv6 and $is_ipv4) {
325:
326: $ip = substr($ip, strrpos($ip, ':') + 1);
327: $is_ipv6 = false;
328: }
329: if ($is_ipv4) {
330:
331: $ip_parts = array_pad(explode('.', $ip), 4, 0);
332: if (count($ip_parts) > 4) {
333: return false;
334: }
335: for ($i = 0; $i < 4; ++$i) {
336: if ($ip_parts[$i] > 255) {
337: return false;
338: }
339: }
340: $part7 = base_convert(($ip_parts[0] * 256) + $ip_parts[1], 10, 16);
341: $part8 = base_convert(($ip_parts[2] * 256) + $ip_parts[3], 10, 16);
342: $ip = '::ffff:'.$part7.':'.$part8;
343: }
344:
345: if (strpos($ip, '::') !== false) {
346: $ip = str_replace('::', str_repeat(':0000', (8 - substr_count($ip, ':'))).':', $ip);
347: }
348: if (strpos($ip, ':') === 0) {
349: $ip = '0000'.$ip;
350: }
351:
352: $ip_parts = explode(':', $ip);
353: foreach ($ip_parts as $key => $num) {
354: $ip_parts[$key] = sprintf('%04s', $num);
355: }
356: $ip = implode(':', $ip_parts);
357: return $ip;
358: }
359:
360: 361: 362: 363: 364: 365:
366: function getIpAsInt($ip)
367: {
368: $ip = getNormalizedIP($ip);
369: $ip = str_replace(':', '', $ip);
370: return hexdec($ip);
371: }
372:
373: 374: 375: 376: 377: 378:
379: function getIpAsString($ip)
380: {
381: $ip = getIpAsInt($ip);
382: return sprintf('%.0f', $ip);
383: }
384:
385: 386: 387: 388: 389:
390: function F_formatFloat($num)
391: {
392: return sprintf('%.03f', round($num, 3));
393: }
394:
395: 396: 397: 398: 399: 400:
401: function F_formatPercentage($num, $ratio = true)
402: {
403: if ($ratio) {
404: $num = (100 * $num);
405: }
406: return '('.str_replace(' ', ' ', sprintf('% 3d', round($num))).'%)';
407: }
408:
409: 410: 411: 412: 413: 414:
415: function F_formatPdfPercentage($num, $ratio = true)
416: {
417: if ($ratio) {
418: $num = (100 * $num);
419: }
420: return '('.sprintf('% 3d', round($num)).'%)';
421: }
422:
423:
424: 425: 426: 427: 428: 429:
430: function F_formatXMLPercentage($num, $ratio = true)
431: {
432: if ($ratio) {
433: $num = (100 * $num);
434: }
435: return sprintf('%3d', round($num));
436: }
437:
438: 439: 440: 441: 442:
443: function F_getUTCoffset($timezone)
444: {
445: $user_timezone = new DateTimeZone($timezone);
446: $user_datetime = new DateTime('now', $user_timezone);
447: return $user_timezone->getOffset($user_datetime);
448: }
449:
450: 451: 452: 453: 454:
455: function F_db_getUTCoffset($timezone)
456: {
457: $time_offset = F_getUTCoffset($timezone);
458: $sign = ($time_offset >= 0)?'+':'-';
459: return $sign.gmdate('H:i', abs($time_offset));
460: }
461:
462: 463: 464: 465: 466: 467:
468: function getDataXML($data, $level = 1)
469: {
470: $xml = '';
471: $tb = str_repeat("\t", $level);
472: foreach ($data as $key => $value) {
473: $key = strtolower($key);
474: $key = preg_replace('/[^a-z0-9]+/', '_', $key);
475: if (is_numeric($key[0]) or ($key[0] == '_')) {
476: $key = 'item'.$key;
477: }
478: $xml .= $tb.'<'.$key.'>';
479: if (is_array($value)) {
480: $xml .= "\n".getDataXML($value, ($level + 1));
481: } else {
482: $xml .= F_text_to_xml($value);
483: }
484: $xml .= '</'.$key.'>'."\n";
485: }
486: return $xml;
487: }
488:
489: 490: 491: 492: 493: 494:
495: function getDataTSVHeader($data, $prefix = '')
496: {
497: $tsv = '';
498: foreach ($data as $key => $value) {
499: if (is_array($value)) {
500: $tsv .= getDataTSVHeader($value, $prefix.$key.'_');
501: } else {
502: $tsv .= "\t".$prefix.$key;
503: }
504: }
505: return $tsv;
506: }
507:
508: 509: 510: 511: 512:
513: function getDataTSV($data)
514: {
515: $tsv = '';
516: foreach ($data as $value) {
517: if (is_array($value)) {
518: $tsv .= getDataTSV($value);
519: } else {
520: $tsv .= "\t".F_text_to_tsv($value);
521: }
522: }
523: return $tsv;
524: }
525:
526: 527: 528: 529: 530:
531: function F_html_to_TSV($str)
532: {
533: $dollar_replacement = ":.dlr.:";
534:
535: $tags2textTable = array (
536: "'<br[^>]*?>'i" => ' ',
537: "'<table[^>]*?>'i" => "\n",
538: "'</table>'i" => "\n",
539: "'<tr[^>]*?>'i" => "\n",
540: "'<th[^>]*?>'i" => "\t",
541: "'<td[^>]*?>'i" => "\t",
542: "'<h[0-9][^>]*?>'i" => "\n\n",
543: "'</h[0-9]>'i" => "\n"
544: );
545: $str = str_replace(' ', ' ', $str);
546: $str = str_replace('→', '-', $str);
547: $str = str_replace('↓', '', $str);
548: $str = str_replace("\t", ' ', $str);
549: $str = preg_replace_callback('/colspan="([0-9]*)"/x', create_function('$matches', 'if ($matches[1] > 1) {return str_repeat("></td><td", ($matches[1] - 1));} return "";'), $str);
550: $str = str_replace("\r\n", "\n", $str);
551: $str = str_replace("\$", $dollar_replacement, $str);
552:
553: $str = str_replace("\n", '', $str);
554: $str = preg_replace(array_keys($tags2textTable), array_values($tags2textTable), $str);
555: $str = preg_replace("'<[^>]*?>'si", '', $str);
556:
557: $str = preg_replace("'[ \t\f]+[\r\n]'si", "\n", $str);
558: $str = preg_replace("'[\r\n][\r\n]+'si", "\n\n", $str);
559: $str = unhtmlentities($str, false);
560: $str = str_replace($dollar_replacement, "\$", $str);
561: $str = rtrim($str);
562: $str = ltrim($str, " \r\n\0\x0B");
563: $str = stripslashes($str);
564: return $str;
565: }
566:
567: 568: 569: 570: 571: 572: 573: 574: 575: 576:
577: function F_select_table_header_element($order_field, $orderdir, $title, $name, $current_order_field = '', $filter = '')
578: {
579: global $l;
580: require_once('../config/tce_config.php');
581: $ord = '';
582: if ($order_field == $current_order_field) {
583: if ($orderdir == 1) {
584: $ord = ' <acronym title="'.$l['w_ascent'].'">></acronym>';
585: } else {
586: $ord = ' <acronym title="'.$l['w_descent'].'"><</acronym>';
587: }
588: }
589: $str = '<th><a href="'.$_SERVER['SCRIPT_NAME'].'?'.$filter.'&firstrow=0&order_field='.$order_field.'&orderdir='.$orderdir.'" title="'.$title.'">'.$name.'</a>'.$ord.'</th>'."\n";
590: return $str;
591: }
592:
593: 594: 595: 596: 597:
598: function getContrastColor($color)
599: {
600: $r = hexdec(substr($color, 0, 2));
601: $g = hexdec(substr($color, 2, 2));
602: $b = hexdec(substr($color, 4, 2));
603:
604: $br = (((299 * $r) + (587 * $g) + (114 * $b)) / 1000);
605: if ($br < 128) {
606:
607: return 'ffffff';
608: }
609:
610: return '000000';
611: }
612:
613: 614: 615: 616: 617:
618: function F_isURL($str)
619: {
620: if ((preg_match('/^(ftp|http|https|mail|sftp|ssh|telnet|vnc)[:][\/][\/]/', $str) > 0) and (parse_url($str) !== false)) {
621: return true;
622: }
623: return false;
624: }
625:
626: 627: 628: 629: 630: 631: 632: 633:
634: function F_utf8_normalizer($str, $mode = 'NONE')
635: {
636: switch ($mode) {
637: case 'CUSTOM': {
638: if (function_exists('user_utf8_custom_normalizer')) {
639: return call_user_func('user_utf8_custom_normalizer', $str);
640: } else {
641: return $str;
642: }
643: break;
644: }
645: case 'C': {
646:
647: return normalizer_normalize($str, Normalizer::FORM_C);
648: break;
649: }
650: case 'D': {
651:
652: return normalizer_normalize($str, Normalizer::FORM_D);
653: break;
654: }
655: case 'KC': {
656:
657: return normalizer_normalize($str, Normalizer::FORM_KC);
658: break;
659: }
660: case 'KD': {
661:
662: return normalizer_normalize($str, Normalizer::FORM_KD);
663: break;
664: }
665: case 'NONE':
666: default: {
667: return $str;
668: break;
669: }
670: }
671: }
672:
673: 674: 675: 676: 677: 678: 679:
680: function bcdechex($dec)
681: {
682: $last = bcmod($dec, 16);
683: $remain = bcdiv(bcsub($dec, $last), 16);
684: if ($remain == 0) {
685: return strtoupper(dechex($last));
686: } else {
687: return bcdechex($remain).strtoupper(dechex($last));
688: }
689: }
690:
691:
692:
693:
694: