quan-li-cua-hang-bang-dia /source code/includes/common/KT_functions.inc.php

Language PHP Lines 1859
MD5 Hash a3d70b70523554fe7f410fddb5c0e967 Estimated Cost $37,630 (why?)
Repository http://quan-li-cua-hang-bang-dia.googlecode.com/svn/trunk/ View Raw File
   1
   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
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
<?php
/*
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2007 Adobe Systems Incorporated
 * All Rights Reserved
 * 
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it. If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe.
 */

/*
	Copyright (c) InterAKT Online 2000-2006. All rights reserved.
*/

/**
 * Try to set certain _SERVER vars if not setted from _ENV vars; 
 * @return nothing;
 */
function KT_setServerVariables() {
	if (!isset($_SERVER['QUERY_STRING']) && isset($_ENV['QUERY_STRING'])) {
		$_SERVER['QUERY_STRING'] = $_ENV['QUERY_STRING'];
	}
	if (!isset($_SERVER['QUERY_STRING'])) {
		$_SERVER['QUERY_STRING'] = '';
	}
	if (!isset($_SERVER['PHP_SELF']) && isset($_ENV['PHP_SELF'])) {
		$_SERVER['PHP_SELF'] = $_ENV['PHP_SELF'];
	}
	if (!isset($_SERVER['REQUEST_URI']) && isset($_ENV['REQUEST_URI'])) {
		$_SERVER['REQUEST_URI'] = $_ENV['REQUEST_URI'];
	}
	if (!isset($_SERVER['REQUEST_URI'])) {
		$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'].(isset($_SERVER['QUERY_STRING'])?"?".$_SERVER['QUERY_STRING']:"");
	}
	if (!isset($_SERVER['SERVER_NAME']) && isset($_ENV['SERVER_NAME'])) {
		$_SERVER['SERVER_NAME'] = $_ENV['SERVER_NAME'];
	}
	if (!isset($_SERVER['HTTP_HOST']) && isset($_ENV['HTTP_HOST'])) {
		$_SERVER['HTTP_HOST'] = $_ENV['HTTP_HOST'];
	}
	if (!isset($_SERVER['HTTP_HOST']) && isset($_SERVER['SERVER_NAME'])) {
		$_SERVER['HTTP_HOST'] = $_SERVER['SERVER_NAME'];
	}
	if (!isset($_SERVER['HTTPS']) && isset($_ENV['HTTPS'])) {
		$_SERVER['HTTPS'] = $_ENV['HTTPS'];
	}
	if (!isset($_SERVER['HTTP_REFERER']) && isset($_ENV['HTTP_REFERER'])) {
		$_SERVER['HTTP_REFERER'] = $_ENV['HTTP_REFERER'];
	}
	if (!isset($_SERVER['HTTP_USER_AGENT']) && isset($_ENV['HTTP_USER_AGENT'])) {
		$_SERVER['HTTP_USER_AGENT'] = $_ENV['HTTP_USER_AGENT'];
	}
	if (!isset($_SERVER['REMOTE_ADDR']) && isset($_ENV['REMOTE_ADDR'])) {
		$_SERVER['REMOTE_ADDR'] = $_ENV['REMOTE_ADDR'];
	}
	if (!isset($_SERVER['SCRIPT_FILENAME']) && isset($_ENV['SCRIPT_FILENAME'])) {
		$_SERVER['SCRIPT_FILENAME'] = $_ENV['SCRIPT_FILENAME'];
	}
	if (!isset($_SERVER['PATH_TRANSLATED']) && isset($_ENV['PATH_TRANSLATED'])) {
		$_SERVER['PATH_TRANSLATED'] = $_ENV['PATH_TRANSLATED'];
	}
	if (!isset($_SERVER['PATH_TRANSLATED']) && isset($_SERVER['ORIG_PATH_TRANSLATED'])) {
		$_SERVER['PATH_TRANSLATED'] = $_SERVER['ORIG_PATH_TRANSLATED'];
	}
	if (!isset($_SERVER['PATH_TRANSLATED']) && isset($_SERVER['DOCUMENT_ROOT']) && isset($_SERVER['PHP_SELF'])) {
		$_SERVER['PATH_TRANSLATED'] = $_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF'];
		$_SERVER['PATH_TRANSLATED'] = str_replace("\\", "/", $_SERVER['PATH_TRANSLATED']);
		$_SERVER['PATH_TRANSLATED'] = str_replace('//', '/', $_SERVER['PATH_TRANSLATED']);
	}
	if (!isset($_SERVER['PATH_TRANSLATED']) && isset($_SERVER['SCRIPT_FILENAME'])) {
		$_SERVER['PATH_TRANSLATED'] = $_SERVER['SCRIPT_FILENAME'];
	}
	if (!isset($_SERVER['SERVER_PROTOCOL']) && isset($_ENV['SERVER_PROTOCOL'])) {
		$_SERVER['SERVER_PROTOCOL'] = $_ENV['SERVER_PROTOCOL'];
	}
	if (!isset($GLOBALS['HTTP_SERVER_VARS'])) {
		$GLOBALS['HTTP_SERVER_VARS'] = &$_SERVER;
	}
	if (!isset($GLOBALS['HTTP_GET_VARS'])) {
		$GLOBALS['HTTP_GET_VARS'] = &$_GET;
	}
	if (!isset($GLOBALS['HTTP_POST_VARS'])) {
		$GLOBALS['HTTP_POST_VARS'] = &$_POST;
	}
	if (!isset($GLOBALS['HTTP_COOKIE_VARS'])) {
		$GLOBALS['HTTP_COOKIE_VARS'] = &$_COOKIE;
	}
	if (!isset($GLOBALS['HTTP_SESSION_VARS'])) {
		$GLOBALS['HTTP_SESSION_VARS'] = &$_SESSION;
	}
	if (!isset($GLOBALS['HTTP_ENV_VARS'])) {
		$GLOBALS['HTTP_ENV_VARS'] = &$_ENV;
	}
}

/**
 * Try to transform a relative path in an absolut one; 
 * @param string $pageUrl the url of the calling script;
 * @param string $templateUrl the url of the template url;
 * @param string $relUrl the url to transform in absolute if case;
 * @param boolean $makeAbsolut flag to transform or not in absolut url;
 * @return string return the absolute url;
 */
function KT_Rel2AbsUrl($pageUrl, $templateUrl, $relUrl, $makeAbsolut) {
	$server = KT_getServerName();
	if (!$makeAbsolut) {
		$pageUrl = str_replace($server, '', $pageUrl);
		$templateUrl = str_replace($server, '', $templateUrl);
		$templateUrl = str_replace($pageUrl, '', $templateUrl);
		$server = '';
	} 
	if (substr($relUrl,0,1) == "/") {
		return $server.$relUrl;
	}
	if (strpos($relUrl,"://") !== false) {
		return $relUrl;
	}

	$arrTemplateUrl = explode('/', $templateUrl);
	array_pop($arrTemplateUrl);
	if (strpos($templateUrl,"://") !== false) {
		$ret = implode('/', $arrTemplateUrl) . (count($arrTemplateUrl)>0?'/':'') . $relUrl;
	} else {
		$arrPageUrl = explode('/', $pageUrl);
		array_pop($arrPageUrl);
		$ret = implode('/', $arrPageUrl) . '/' . implode('/', $arrTemplateUrl) . (count($arrTemplateUrl)>0?'/':'') . $relUrl;
	}
	
	$ret = KT_CanonizeRelPath($ret);
	return $ret;
}

/**
 * Try to canonize a path (replace ../ and ./ ); 
 * @param string $relPath the path to be canonize;
 * @return string return the canonized path;
 */
function KT_CanonizeRelPath($relPath) {
	if (strpos($relPath, "..") !== false || strpos($relPath, "/.") !== false) {
		$parts = explode('/',$relPath);
		$newParts = array();
		for($i=0;$i<count($parts);$i++) {
			if ($parts[$i] == '..') {
				if (count($newParts) > 0 && $newParts[count($newParts) - 1] != '..') {
					array_pop($newParts);
				} else {
					$newParts[] = $parts[$i];
				}
			} elseif ($parts[$i] != '.') {
				$newParts[] = $parts[$i];
			}
		}
		$ret = implode('/',$newParts);
	} else {
		$ret = $relPath;
	}
	return $ret;
}

/**
 * Transforms path containing \ to path with / (URI paths) 
 * Only for PRO version	 
 * @param string path to be transformed;
 * @param boolean indicates if path is file or folder
 * @return string;
 * @access public
 */
function KT_TransformToUrlPath($path, $is_folder = true) {
	$path = str_replace(array('\\', '/'), '/', $path);
	if ($path != '' && $is_folder && substr($path, -1, 1) != '/') {
		$path .= '/';
	}
	return $path;
}

/**
 * Try to transform the relative url of any <a>, <link>, <img> in an absolute one; 
 * The absolute path is considered to be the path to the file in which the function is
 * called. 
 * @param string $templateUrl the url of the file in which is the text;
 * @param string $text the text to be parsed;
 * @param boolean $makeAbsolut flag to transform or not in absolut url;
 * @return string return the text with absolute paths in;
 */
/**
* Known bugs:
* 	1. URL contains \" or \'
*	2. URL contains inside "simple quotes" or 'double quotes' will be removed
*/
function KT_transformsPaths($templateUrl, $text, $makeAbsolut) {
	if ($templateUrl == "./" ) {
		$templateUrl = "";
	}
	
	$scriptUrl = KT_getUri();
	preg_match_all('/<(a|img|link|script|form|iframe|embed|applet)([^>]*)>/ims',$text, $matches);
	foreach($matches[2] as $key=>$value) {
		preg_match('/\s(href|src|action|codebase|archive)\s*=\s*(("((\\\"|[^"])+)")|(\'((\\\\\'|[^\'])+)\')|([^\s]+))?/ims' ,$value, $mmatch);
		if (isset($mmatch[2])) {
			if (preg_match("/\s*mailto\s*:/ims", $mmatch[2])) {
				continue;
			}
			$mydelim = '';
			if (substr($mmatch[2], 0, 1) == '\'' || substr($mmatch[2], 0, 1) == '"') {
				$mydelim = substr($mmatch[2], 0, 1);
				$mmatch[2] = substr($mmatch[2], 1, -1);
			}
			if (!($mmatch[1] == 'href' && strpos($mmatch[2], 'javascript:') === 0)
			&& !($mmatch[1] == 'href' && strpos($mmatch[2], '#') === 0)
			&& !($mmatch[1] == 'href' && strpos($mmatch[2], 'mailto:') === 0)
			&& !($mmatch[1] == 'action' && $mmatch[2] == "")) {
				$text = preg_replace("/".$mmatch[1]."\s*=\s*".preg_quote($mydelim . $mmatch[2] . $mydelim, "/")."/ims", $mmatch[1].'="'.KT_Rel2AbsUrl($scriptUrl, $templateUrl, $mmatch[2], $makeAbsolut).'"', $text);
			}
		}
	}
	preg_match_all('/<input([^>]*)>/ims',$text, $matches);
	foreach($matches[1] as $key=>$value) {
		$sub_text = preg_replace('/\svalue\s*=\s*(("((\\\")+|[^"]+)")|(\'((\\\\\')+|[^\']+)\')|([^\s]+))?/ims', '', $value);
		preg_match('/\ssrc\s*=\s*(("((\\\"|[^"])+)")|(\'((\\\\\'|[^\'])+)\')|([^\s]+))?/ims', $sub_text, $mmatch);
		if (isset($mmatch[1])) {
			$mydelim = '';
			if (substr($mmatch[1], 0, 1) == '\'' || substr($mmatch[1], 0, 1) == '"') {
				$mydelim = substr($mmatch[1], 0, 1);
				$mmatch[1] = substr($mmatch[1], 1, -1);
			}
			$text = preg_replace("/src\s*=\s*".preg_quote($mydelim . $mmatch[1] . $mydelim, "/")."/ims", 'src="'.KT_Rel2AbsUrl($scriptUrl, $templateUrl, $mmatch[1], $makeAbsolut).'"', $text);
		}
	}
	preg_match_all('/<param\s+name=(\'|")(movie|src|fileName)(\'|")([^>]*)>/ims',$text, $matches);
	foreach($matches[4] as $key=>$value) {
		preg_match('/\s(value)\s*=\s*(("((\\\"|[^"])+)")|(\'((\\\\\'|[^\'])+)\')|([^\s]+))?/ims' ,$value, $mmatch);
		if (isset($mmatch[2])) {
			$mydelim = '';
			if (substr($mmatch[2], 0, 1) == '\'' || substr($mmatch[2], 0, 1) == '"') {
				$mydelim = substr($mmatch[2], 0, 1);
				$mmatch[2] = substr($mmatch[2], 1, -1);
			}
			$text = preg_replace("/".$mmatch[1]."\s*=\s*".preg_quote($mydelim . $mmatch[2] . $mydelim, "/")."/ims", $mmatch[1].'="'.KT_Rel2AbsUrl($scriptUrl, $templateUrl, $mmatch[2], $makeAbsolut).'"', $text);
		}
	}
	if (preg_match("/UNI_navigateCancel/i", $text)) {
		preg_match_all("/UNI_navigateCancel\(event, '([\.\/]*includes\/nxt\/back.php)'\)/ims",$text, $matches);
		foreach($matches[1] as $key=>$value){
			$text = str_replace($value, KT_Rel2AbsUrl($scriptUrl, $templateUrl, $value, $makeAbsolut), $text);
		}
	}
	if (preg_match("/NEXT_ROOT=/i", $text)) {
		preg_match_all("/NEXT_ROOT=\"([^\"]*)\"/ims",$text, $matches);
		foreach($matches[1] as $key=>$value){
			$text = str_replace($value, KT_Rel2AbsUrl($scriptUrl, $templateUrl, $value, $makeAbsolut), $text);
		}
	}
	if (preg_match('/\$MXW_relPath\s=\s/i', $text)) {
		preg_match_all('/\$MXW_relPath\s=\s\'([^\']*)\';/is',$text, $matches);
		foreach($matches[1] as $key=>$value){
			$text = str_replace('$MXW_relPath = \'' . $value . '\';', '$MXW_relPath = \'' . KT_Rel2AbsUrl($scriptUrl, $templateUrl, $value, $makeAbsolut) . '\';', $text);
		}
	}
	if (preg_match("/\"path\": \".*includes\/ktm\/\",/i", $text)) {
		preg_match_all("/\"path\": \"(.*includes\/ktm\/)\",/ims",$text, $matches);
		foreach($matches[1] as $key=>$value){
			$text = preg_replace("/\"path\": \"" . preg_quote($value,"/") ."\"/ims", "\"path\": \"". KT_Rel2AbsUrl($scriptUrl, $templateUrl, $value, $makeAbsolut). "\"", $text);
		}
	}
	
	// make absolute URLs for AC_FL_RunContent
	if(preg_match_all('/AC_FL_RunContent\((.*)\);/Uims', $text, $matches)) {
		foreach($matches[1] as $key=>$value) {
			$arr = explode(',', $value);

			// check for , in the file name
			$offset = 0;
			if(substr($arr[11], 0, 1) !== "'") {
				$arr[10] = $arr[10] . "," . $arr[11];
				unset($arr[11]);
				$offset++;
			}
			if (isset($arr[17]) && substr($arr[17 + $offset], 0, 1) !== "'") {
				$arr[16 + $offset] = $arr[16 + $offset] . "," . $arr[17 + $offset];
				unset($arr[17 + $offset]);
			}

			$arr[10] = trim($arr[10]);
			$arr[10] = "'" . KT_Rel2AbsUrl($scriptUrl, $templateUrl, substr($arr[10], 1, strlen($arr[10]) - 2), $makeAbsolut) . "'";

			$arr[16 + $offset] = trim($arr[16 + $offset]);
			$arr[16 + $offset] = "'" . KT_Rel2AbsUrl($scriptUrl, $templateUrl, substr($arr[16 + $offset], 1, strlen($arr[16 + $offset]) - 2), $makeAbsolut) . "'";
			
			$text = str_replace($value, implode(',', $arr), $text);
		}
	}
	
	return $text;
}

/**
 * Return the server name on which the page reside. 
 * @return string return the server name (ex. http://server.com/ );
 */
function KT_getServerName() {
	$protocol = 'http';
	if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') {
		$protocol = 'https';
	}
	$host = $_SERVER['HTTP_HOST'];
	$baseUrl = $protocol . '://' . $host;
	if (substr($baseUrl, -1)=='/') {
		$baseUrl = substr($baseUrl, 0, strlen($baseUrl)-1);
	}
	return $baseUrl;
}

/**
 * Return the current page name and the query string if exists. 
 * @return string current page and query string (ex. /dir/index.php );
 */
function KT_getPHP_SELF() {
	KT_setServerVariables();
	$script = $_SERVER['REQUEST_URI'];
	if (strpos($script, '?') !== false) {
		$pos = strpos($script, '?');
		$script = substr($script, 0, $pos);
	}
	if (substr($script, -1) == '/' && substr($_SERVER['PHP_SELF'], -1) != '/') {
		$file = basename($_SERVER['PHP_SELF']);
		$script .= $file;
	}
	return $script;
}

/**
 * Return the URL of the page in which the script is called. 
 * @return string return the URL (ex. http://server.com/dir/papa.php );
 */
function KT_getUri() {
	$script = KT_getPHP_SELF();
	if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '' && $_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) {
		$script = substr($script, 0, strlen($script) - strlen($_SERVER['PATH_INFO']));
	}
	return KT_getServerName() . $script;
}

/**
 * Return the folder of the page in which the script is called. 
 * @return string return the folder (ex. http://mysite/admin/ );
 */
function KT_getUriFolder() {
	$ret = KT_getUri();
	if (substr($ret,-1,1) != '/') {
		$ret = dirname($ret).'/';
	}
	return $ret;
}

/**
 * Return the URL of the page in which the script is called and the query string if exists. 
 * @return string return the URL (ex. http://server.com/dir/papa.php?mod=return );
 */
function KT_getFullUri() {
	$ret = KT_getUri();
	if (strpos($_SERVER['REQUEST_URI'], '?') !== false) {
		$pos = strpos($_SERVER['REQUEST_URI'], '?');
		$query_string = substr($_SERVER['REQUEST_URI'], $pos);
		if (trim($query_string) != '') {
			$ret = $ret . $query_string;
		}
	}
	return $ret;
}

/**
 * Adding or replacing a param/value in the string. 
 * @param string $qstring the query string wich will be the subject (ex. ?myvar=1&myvar2=a or myvar=3&myvar2=v);
 * @param string $paramName the name of the variable wich will be strip from or will have a new value (ex. myvar or othervar);
 * @param string $paramValue if is not null this will be the value of the $paramName, if not the $paramName will be strip off (ex. myvalue or null);
 * @return string return the URL (ex. ?myvar=myvalue&myvar2=a or ?myvar2=v );
 */
function KT_addReplaceParam($qstring, $paramName, $paramValue=null) {

	// extract the URI if any
	if (strpos($qstring, "?") !== false) {
		$uri = preg_replace("/\?.*$/", "?", $qstring);
		$qstring = preg_replace("/^.*\?/", "", $qstring);
	} else {
		if (strpos($qstring, "=") !== false) {
			$uri = "";
		} else {
			$uri = $qstring;
			if ($paramValue !== null) {
				$uri .= "?";
			}
			$qstring = "";
		}
	}

	// the list of parameters
	$arr = explode('&',$qstring);

	// remove $paramName from the list
	foreach($arr as $key=>$value) {
		$tmpArr = explode('=',$value);
		if (urldecode($tmpArr[0]) == $paramName) {
			unset($arr[$key]);
			break;
		} else {
			if (strpos($paramName, "/") === 0) {
				if (preg_match($paramName, urldecode($tmpArr[0]))) {
					unset($arr[$key]);
					break;
				}
			}
		}
	}

	// add $paramName to the list
	if ($paramValue !== null) {
		$arr[] = rawurlencode($paramName).'='.rawurlencode($paramValue);
	}

	$ret = implode('&',$arr);
	$ret = preg_replace("/^&/", "", $ret);

	// if no parameters, remove the trailing ? from the URI
	if ($ret == '') {
		$uri = preg_replace("/\?$/", "", $uri);
	}

	// merge the URI with the new list
	$ret = $uri . $ret;

	return $ret;
}

/**
 * Make the redirect on server side; 
 * @param string $url the URL to which is makeing the redirect;
 * @return nothing;
 */
function KT_redir($url) {
	$protocol = "http://";
	$server_name = $_SERVER["HTTP_HOST"];
	if ($server_name != '') {
		$protocol = "http://";
		if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == "on")) {
			$protocol = "https://";
		}
		if (preg_match("#^/#", $url)) {
			$url = $protocol.$server_name.$url;
		} else if (!preg_match("#^[a-z]+://#", $url)) {
			$script = KT_getPHP_SELF();
			if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '' && $_SERVER['PATH_INFO'] != $_SERVER['PHP_SELF']) {
				$script = substr($script, 0, strlen($script) - strlen($_SERVER['PATH_INFO']));
			}
			$url = $protocol.$server_name.(preg_replace("#/[^/]*$#", "/", $script)).$url;
                }
                session_write_close();
		$url = str_replace(" ","%20",$url);
		if (KT_is_ajax_request()) {
			header("Kt_location: ".$url);
			echo "Redirecting to: " . $url;
		} else {
			header("Location: ".$url);
		}
	}
	exit;
}

/**
 * Return $GLOBALS['KT_REL_PATH'] in front of the URL if the URL is not absolute and $GLOBALS['KT_REL_PATH'] is setted; 
 * @param string $url the URL string (ex. admin/shop/);
 * @return nothing (ex. ../../admin/shop/);
 */
function KT_makeIncludedURL($url) {
	$ret = $url;
	if (isset($GLOBALS['KT_REL_PATH'])) {
		if (!preg_match("#^/#", $ret) && !preg_match("#^[a-z]+://#", $ret)) {
			$ret = $GLOBALS['KT_REL_PATH'] . $ret;
		}
	}
	return $ret;
}

/**
 * Escape the " > < from a string with the html entities; 
 * @param string $val the value to be escaped;
 * @return string the escaped value;
 */
function KT_escapeAttribute($val) {
	$val = str_replace(array('"',"<",">"), array("&quot;","&lt;","&gt;"), $val);
	return $val;
}

/**
 * Apply addcslashes php function on the argument for the charlist: \t\r\n\'\\ ; 
 * @param string $val the value to be escaped;
 * @return string the escaped value;
 */
function KT_escapeJS($val) {
	$val = addcslashes($val, "\t\r\n\'\\");
	return $val;
}

/**
 * Return the real path of the folder where includes folder resides ; 
 * @return string the siteRoot path (ex. var/html/www/mysite/);
 */
function KT_getSiteRoot() {
	$siteroot = dirname(realpath(__FILE__)) . '/../..';
	$siteroot = str_replace(DIRECTORY_SEPARATOR, '/', $siteroot);
	$siteroot = KT_CanonizeRelPath($siteroot);
	return $siteroot;
}

/**
 * Start the session if it is not started and put in session the values from _SESSION[md5($siteroot)] ; 
 * @return nothing;
 */
function KT_session_start() {
	if (!session_id()) {
		@session_start();
		if (!session_id()) {
			die('Your session is incorrectly defined and cannot be started. Check your php.ini configuration.');
		}
	}
	
        $siteroot = md5(KT_getSiteRoot());
        if (isset($_SESSION['KT_lastsiteroot'])) {
          $lastsiteroot = $_SESSION['KT_lastsiteroot'];
          if (isset($_SESSION[$lastsiteroot]) && is_array($_SESSION[$lastsiteroot])) {
            foreach ($_SESSION[$lastsiteroot] as $key => $value) {
              unset($_SESSION[$key]);
            }
          }
        }

	if ( isset($_SESSION[$siteroot]) && is_array($_SESSION[$siteroot]) ) {
		foreach ($_SESSION[$siteroot] as $key => $value) {
			$_SESSION[$key] = $value;
		}
        }
        $_SESSION['KT_lastsiteroot'] = $siteroot;
}

/**
 * Put in the $_SESSION[$siteroot][$var] key the $_SESSION[$var] value ; 
 * @param string $var the name of the variable to be setted from _SESSION[$var] to _SESSION[$siteroot][$var]; 
 * @return nothing;
 */
function KT_setSessionVar($var) {
	$siteroot = md5(KT_getSiteRoot());
	if ( !isset($_SESSION[$siteroot]) ) {
		$_SESSION[$siteroot] = array();
	}
	if ( isset($_SESSION[$var]) ) {
		$_SESSION[$siteroot][$var] = $_SESSION[$var];
	}
}

/**
 * Unset the $_SESSION[$siteroot][$var] and $_SESSION[$var] ; 
 * @param string $var the name of the variable to be unseted; 
 * @return nothing;
 */
function KT_unsetSessionVar($var) {
	$siteroot = md5(KT_getSiteRoot());
	if ( isset($_SESSION[$siteroot]) ) {
		if ( isset($_SESSION[$siteroot][$var]) ) {
			unset($_SESSION[$siteroot][$var]);
		}
		if ( count($_SESSION[$siteroot]) == 0 ) {
			unset($_SESSION[$siteroot]);
		}
	}
}

/**
 * Extracts parts of a file (dirname, basename, filename, extension)
 * @param string $fileName The file name
 * @return array returns associative array containing the dirname, basename, filename, extension;
 */
function KT_pathinfo($fileName) {
	if ($fileName != "") {
		$path_info = pathinfo($fileName);
		if (!isset($path_info['extension'])) {
			$path_info['extension'] = "";
		}
		$extSize = strlen($path_info['extension']);
		if ($extSize != 0) {
			$extSize = $extSize + 1;
		}
		$path_info['filename'] = substr($path_info['basename'], 0, strlen($path_info['basename']) - $extSize);
	} else {
		$path_info = array('dirname' => '', 'basename' => '','extension' => '', 'filename' => '');
	}
	return $path_info;
}

/**
 * Transforms a path into its absolute value
 *   Path separator is always "/" and for folders, always adds the trailing "/"
 *   If folder/file does not exist, the "pathName" will be added to the current folder path
 * @param string $pathName The path to be translated
 * @param boolean $isFolder Specifies if the path is a folder or a file
 * @return string return the absolute path
 */
function KT_realpath($pathName, $isFolder = true) {
	$isAbsolute = false;
	if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {
		if (substr($pathName,1,1) == ":") {
			$isAbsolute = true;
		}
	} else {
		if (substr($pathName,0,1) == "/") {
			$isAbsolute = true;
		}
	}
	if ($isAbsolute) {
		$realPath = $pathName;
	} else {
		$realPath = realpath($pathName);
		if ($realPath === false) {
			$realPath = realpath('.') . DIRECTORY_SEPARATOR;
			$realPath .= $pathName;
		}
	}
	$realPath = str_replace('\\\\', '\\', $realPath);
	$realPath = str_replace('\\', '/', $realPath);
	$realPath = str_replace('//', '/', $realPath);
	if ($isFolder) {
		if (substr($realPath, strlen($realPath)-1) != '/') {
			$realPath .= '/';
		}
	}
	$realPath = str_replace("/./", "/", $realPath);
	$realPath = str_replace("/./", "/", $realPath);
	$realPath = KT_CanonizeRelPath($realPath);
	return $realPath;
}

/**
 * Parse the string for values like {...} and return an array with all the conted from {}; 
 * @param string $string the string to be searched; 
 * @return array $replacements;
 */
function KT_getReplacementsFromMessage(&$string) {
	$replacements = array();

	if (preg_match_all('/\{([\w\d\.\s\(\)]+)\}/', $string, $matches)) {
		if (isset($matches[1]) && is_array($matches[1])) {
			$replacements = $matches[1];
		}
	}

	return $replacements;
}

/**
* Function KT_DynamicData replace all the dynamic data with their values;
* @param string $expression The expression to be evaluated
* @param object or null $tNG The tNG context in which the expression is evaluated
* @param string $escapeMethod The string escape method for the evaluated values (rawurlencode and SQL)
* @param booolean $useSavedData Weather to use the current tNG data or the saved values
* @param array $extraParams Extra expression parameters passed when for evaluation (of form $key => $value; any encounter of key will be replaced with its value)
* @return string the string with the dynamic data replaced with their values;
*/
function KT_DynamicData($expression, $tNG, $escapeMethod = '', $useSavedData = false, $extraParams = array(), $errorIfNotFound = true) {
	$PB = '{';
	$PE = '}';

    if (!is_string($expression)){
        return $expression;
    }
	
	// DynamicData functions - use this to define more functions
	KT_getInternalTimeFormat();
	$date_now = KT_convertDate(date('Y-m-d'), "yyyy-mm-dd", $GLOBALS['KT_screen_date_format']);
	$date_dt_now = KT_convertDate(date('Y-m-d H:i:s'), "yyyy-mm-dd HH:ii:ss", $GLOBALS['KT_screen_date_format'].' ' .$GLOBALS['KT_screen_time_format_internal']);
	$date_t_now = KT_convertDate(date('H:i:s'), "HH:ii:ss", $GLOBALS['KT_screen_time_format_internal']);
	$dynamicDataFunctions = array(
		'NOW()' => $date_now,
		'now()' => $date_now,
		'NOW' => $date_now,
		'now' => $date_now,
		'NOW_DT()' => $date_dt_now,
		'now_dt()' => $date_dt_now,
		'NOW_DT' => $date_dt_now,
		'now_dt' => $date_dt_now,
		'NOW_T()' => $date_t_now,
		'now_t()' => $date_t_now,
		'NOW_T' => $date_t_now,
		'now_t' => $date_t_now,
		'KT_REFERRER' => isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'',
		'kt_referrer' => isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'',
		'KT_CSV_LINE' => isset($GLOBALS['KT_CSV_LINE'])?$GLOBALS['KT_CSV_LINE']:'',
		'KT_XML_LINE' => isset($GLOBALS['KT_XML_LINE'])?$GLOBALS['KT_XML_LINE']:''
	);

	$placeholdersArr = KT_getReplacementsFromMessage($expression);

	$replacementsArr = array();

	switch ($escapeMethod) {
		case 'rawurlencode' :
			break;
		case 'expression' :
			break;
		case 'SQL' :
			if (!isset($tNG)) {
				$escapeMethod = false;
			}
			break;
		default :
			$escapeMethod = false;
			break;
	}

	if ($useSavedData !== true) {
		$useSavedData = false;
	}

	foreach ($placeholdersArr as $key => $placeholder) {
		if (array_key_exists($placeholder, $extraParams)) {
			// extra params have priority 1
			$placeholderType = 'tng_ddextra';
			$placeholderName = $placeholder;
		} else {
			// functions have priority 2
			if (array_key_exists($placeholder, $dynamicDataFunctions)) {
				$placeholderType = 'tNG_DDfunction';
				$placeholderName = $placeholder;
			}	else {
				$ptpos = strpos($placeholder, '.');
				if (!$ptpos) {
					// tng field
					if (isset($tNG)) {
						// attached to a tng, replace field with value
						$placeholderType = 'tNG_tNGfield';
						$placeholderName = $placeholder;
					} else {
						// no tng, leave as is
						$placeholderType = 'tNG_tNGfieldLater';
						$placeholderName = $placeholder;
					}
				} else {
					$placeholderType = substr($placeholder, 0, $ptpos);
					$placeholderName = substr($placeholder, $ptpos + 1);
				}
			}
		}
		$placeholder = $PB . $placeholder . $PE;
		switch (strtolower($placeholderType)) {
			case 'tng_ddfunction' :
				$replacementsArr[$placeholder] = $dynamicDataFunctions[$placeholderName];
				break;
			case 'tng_ddextra' :
				$replacementsArr[$placeholder] = $extraParams[$placeholderName];
				break;
			case 'tng_tngfield' :
				if ($useSavedData) {
					$placeholderValue = $tNG->getSavedValue($placeholderName);
				} else {
					if (isset($tNG->columns[$placeholderName]) || $placeholderName == $tNG->getPrimaryKey()) {
						$placeholderValue = $tNG->getColumnValue($placeholderName);
						$placeholderType = $tNG->getColumnType($placeholderName);
					} else {
						if ($errorIfNotFound == true) {
							die('KT_DynamicData:<br />Column ' . $placeholderName . ' is not part of the current transaction.');
						} else {
							$placeholderValue = $placeholder;
						}
					}
					if ($escapeMethod == 'SQL') {
						$placeholderValue = KT_escapeForSql($placeholderValue, $placeholderType);
					}
				}
				$replacementsArr[$placeholder] = $placeholderValue;
				break;
			case 'tng_tngfieldlater':
				break;
			case 'get':
				$myPlaceholderName = $placeholderName;
				if (isset($tNG)) {
					if (isset($tNG->multipleIdx)) {
						$myPlaceholderName .= "_".$tNG->multipleIdx;
					}
				}
				$replacementsArr[$placeholder] = KT_getRealValue("GET",$myPlaceholderName);
				if (!isset($replacementsArr[$placeholder])) {
					$replacementsArr[$placeholder] = KT_getRealValue("GET",$placeholderName);
				}
				break;
			case 'post':
				$myPlaceholderName = $placeholderName;
				if (isset($tNG)) {
					if (isset($tNG->multipleIdx)) {
						$myPlaceholderName .= "_".$tNG->multipleIdx;
					}
				}
				$replacementsArr[$placeholder] = KT_getRealValue("POST",$myPlaceholderName);
				if (!isset($replacementsArr[$placeholder])) {
					$replacementsArr[$placeholder] = KT_getRealValue("POST",$placeholderName);
				}
				break;
			case 'cookie':
				$replacementsArr[$placeholder] = KT_getRealValue("COOKIE",$placeholderName);
				break;
			case 'session':
				KT_session_start();
				$replacementsArr[$placeholder] = KT_getRealValue("SESSION",$placeholderName);
				break;
			case 'globals':
				$replacementsArr[$placeholder] = KT_getRealValue("GLOBALS",$placeholderName);
				break;
			case 'request':
				$replacementsArr[$placeholder] = KT_getRealValue("GLOBALS",$placeholderName);
				break;
			case 'server':
				$replacementsArr[$placeholder] = KT_getRealValue("SERVER",$placeholderName);
				break;
			case 'application':
				// CF only
				break;
			case 'csv':
				$replacementsArr[$placeholder] = KT_getRealValue("CSV",$placeholderName);
				break;	
			default :
				// recordset
				if (isset($GLOBALS[$placeholderType])) {
					$rs = $GLOBALS[$placeholderType];
					if (is_resource($rs)) {
						$placeholderValue = $GLOBALS["row_".$placeholderType][$placeholderName];
					} elseif (is_object($rs)) {
						$placeholderValue = $rs->Fields($placeholderName);
					} else {
						break;
					}
				} else {
					$placeholderValue = $placeholder;
				}
				$replacementsArr[$placeholder] = $placeholderValue;
				break;
		}
	}

	reset($replacementsArr);
	if ($escapeMethod == 'rawurlencode') {
		if (
			!array_key_exists ("{kt_login_redirect}", $replacementsArr) && 
			!array_key_exists ("{kt_referrer}", $replacementsArr) && 
			!array_key_exists ("{KT_REFERRER}", $replacementsArr)
			) {
			$replacementsArr = array_map($escapeMethod, $replacementsArr);
		}
	} elseif ($escapeMethod == 'expression') {
		$replacementsArr = array_map('KT_escapeExpression', $replacementsArr);
	}
	$newexpression = str_replace(array_keys($replacementsArr), array_values($replacementsArr), $expression);
	/*if ($escapeMethod == 'expression') {
		echo $newexpression."\n<br/>\n";
	}*/
	return $newexpression;
}

/**
 * Wrapper for stripslashes; it is used as callbackfunction; 
 * @param string $value the string to be searched; 
 * @param string $key the string to be searched; 
 * @return nothing;
 */
function KT_stripslashes($value, $key) {
	$value = stripslashes($value);
}

/**
 * Compiles a method and a reference into a value
 * Ex: method=GET and reference=test, return value=$_GET['test']
 * @param string $method The method (GET, POST, etc)
 * @param string $reference The reference (variable name)
 * @return object unknown The compiled value
 *         the return has stripped slashes if necessary
 * @access public
 */
function KT_getRealValue($method, $reference) {
	$needStrip = false;
	$ret = null;
	switch($method) {
		case 'GET':
			if (isset($_GET[$reference])) {
				$ret = $_GET[$reference];
			}
			$needStrip = true;
			break;
		case 'POST':
			if (isset($_POST[$reference])) {
				$ret = $_POST[$reference];
			}
			$needStrip = true;
			break;
		case 'COOKIE':
			if (isset($_COOKIE[$reference])) {
				$ret = $_COOKIE[$reference];
			}
			$needStrip = true;
			break;
		case 'SESSION':
			if (isset($_SESSION[$reference])) {
				$ret = $_SESSION[$reference];
			}
			break;
		case 'GLOBALS':
			if (isset($GLOBALS[$reference])) {
				$ret = $GLOBALS[$reference];
			}
			break;
		case 'SERVER':
			if (isset($_SERVER[$reference])) {
				$ret = $_SERVER[$reference];
			}
			break;
		case 'FILES':
			if (isset($_FILES[$reference])) {
				$ret = @$_FILES[$reference]['name'];
			}
			break;
		case 'VALUE':
			$ret = $reference;
			break;
		case 'CURRVAL':
			$ret = null;
			break;
		case 'CSV':
			if (isset($GLOBALS['KT_CSV'][$reference])) {
				$ret = $GLOBALS['KT_CSV'][$reference];
			}
			break;
		case 'XML':
			if (isset($GLOBALS['KT_XML'][$reference])) {
				$ret = $GLOBALS['KT_XML'][$reference];
			}
			break;
		default:
			die('KT_getRealValue:<br />Unknown method: '.$method.'.');
			break;
	}
	if ($needStrip && !is_null($ret)) {
		if (get_magic_quotes_gpc() || (isset($GLOBALS['KT_serverModel']) && $GLOBALS['KT_serverModel'] == 'adodb')) {
			if (is_array($ret)) {
				array_walk($ret, 'KT_stripslashes');
			} else {
				$ret = stripslashes($ret);
			}
		}
	}
	return $ret;
}



/**
 * Converts a date/time/datetime from one format to another
 * @param string $date The date
 * @param string $inFmt The input format (the format of the input date)
 * @param string $outFmt The output format (the format of the output date)
 * @return string the new date
 */
function KT_convertDate($date, $inFmt, $outFmt) {
	if (($inFmt == '') || ($outFmt == '') || ($inFmt == $outFmt)) {
		return $date;
	}
	
	if (!isset($date) || $date == '') {
		return;
	}
	
	if (strpos($inFmt, '%') !== false) {
		$inFmt = KT_format2newDF($inFmt);
	}
	
	if (strpos($outFmt, '%') !== false) {
		$outFmt = KT_format2newDF($outFmt);
	}
	
	$inFmtRule = KT_format2rule($inFmt);
	$outFmtRule = KT_format2rule($outFmt);
	$dateArr = KT_applyDate2rule($date, $inFmtRule);
	$outRule = KT_format2outRule($outFmt);
	$outdate = KT_applyOutRule2date($dateArr, $outFmtRule, $outRule);

	return $outdate;
}

/**
 * Convert a date format from an old type date format %Y-%m-%d into new ones; 
 * @param string $dateformat the string to be parsed (ex. %Y-%m-%d ); 
 * @return string $dateformat (ex. yyyy-mm-dd);
 */
function KT_format2newDF($dateformat) {
	$dateformat = str_replace('%Y', 'yyyy', $dateformat);
	$dateformat = str_replace('%m', 'mm', $dateformat);
	$dateformat = str_replace('%d', 'dd', $dateformat);
	$dateformat = str_replace('%H', 'HH', $dateformat);
	$dateformat = str_replace('%M', 'ii', $dateformat);
	$dateformat = str_replace('%S', 'ss', $dateformat);
	return $dateformat;
}

/**
 * Splits a date format into a chunked representation
 * @param string $format The format to be precessed
 * @return array the format in a chunked form (with chunks position and length)
 */
function KT_format2rule($format) {
	$rule = array();

	$rulePieces = preg_split('/([-\/\[\]\(\)\s\*\|\+\.:=,])/', $format);
	$count = count($rulePieces);

	for ($i=0; $i<$count; $i++) {
		$rulePiece = $rulePieces[$i];
		switch ($rulePiece) {
			case 'yyyy':
			case 'yy':
			case 'y':
				$rule['y']['piece'] = $i;
				$rule['y']['len'] = strlen($rulePiece);
				break;
			case 'mm':
			case 'm':
				$rule['m']['piece'] = $i;
				$rule['m']['len'] = strlen($rulePiece);
				break;
			case 'dd':
			case 'd':
				$rule['d']['piece'] = $i;
				$rule['d']['len'] = strlen($rulePiece);
				break;
			case 'HH':
			case 'H':
				$rule['H']['piece'] = $i;
				$rule['H']['len'] = strlen($rulePiece);
				break;
			case 'hh':
			case 'h':
				$rule['h']['piece'] = $i;
				$rule['h']['len'] = strlen($rulePiece);
				break;
			case 'ii':
			case 'i':
				$rule['i']['piece'] = $i;
				$rule['i']['len'] = strlen($rulePiece);
				break;
			case 'ss':
			case 's':
				$rule['s']['piece'] = $i;
				$rule['s']['len'] = strlen($rulePiece);
				break;
			case 'tt':
			case 't':
				$rule['t']['piece'] = $i;
				$rule['t']['len'] = strlen($rulePiece);
				break;
		}
	}
	return $rule;
}

/**
 * Splits a date into a chunked representation;
 * @param string $date The date to be precessed;
 * @param array $rule Associative array containing the date chunks order ('y'=> 1, 'm' => 3, etc);
 * @return array the date in a chunked form, containig yyyy, mm, dd, HH, ii and ss;
 */
function KT_applyDate2rule($date, $rule) {
	$dateArr = array();

	$dateArr['y'] = '';
	$dateArr['m'] = '';
	$dateArr['d'] = '';
	$dateArr['H'] = '00';
	$dateArr['i'] = '00';
	$dateArr['s'] = '00';

	$datePieces = preg_split('/([-\/\[\]\(\)\s\*\|\+\.:=,])/', $date, -1, PREG_SPLIT_NO_EMPTY);
	if (is_array($datePieces) && count($datePieces)>0) {
		foreach ($rule as $ruleKey => $ruleValue) {
			$index = $ruleValue['piece'];
			if (isset($datePieces[$index])) {
				$dateArr[$ruleKey] = $datePieces[$index];
			}
		}
	}

	$ruleKeys = array_keys($rule);
	if (in_array('h', $ruleKeys) && isset($dateArr['h'])) {
		$dateArr['H'] = $dateArr['h'];
		unset($dateArr['h']);
	}
	if (in_array('t', $ruleKeys)) {
		$value = isset($dateArr['t'])?$dateArr['t']:'A';
		if (strtoupper(substr($value, 0, 1)) == 'P') {
			if ($dateArr['H'] < 12) {
				$dateArr['H'] = $dateArr['H'] + 12;
			}
		} else {
			if ($dateArr['H'] == 12) {
				$dateArr['H'] = 0;
			}
		}
		unset($dateArr['t']);
	}

	foreach (array('y', 'm', 'd', 'H', 'i', 's') as $key => $piece) {
		if (strlen($dateArr[$piece]) == 1) {
			$dateArr[$piece] = '0' . $dateArr[$piece];
		}
	}

	if (strlen($dateArr['y']) == 2) {
		if ($dateArr['y'] < 70) {
			$dateArr['y'] = '20' . $dateArr['y'];
		} else {
			$dateArr['y'] = '19' . $dateArr['y'];
		}
	}

	return $dateArr;
}

/**
 * Removes extra chars from a date format, in order to obtain a parsable definition;
 * @param string $format The format to be stripped (ex. yyyy-mm-dd);
 * @return string $format (ex. y-m-d);
 */
function KT_format2outRule($format) {
	$format = str_replace('yyyy', 'y', $format);
	$format = str_replace('yy', 'y', $format);
	$format = str_replace('mm', 'm', $format);
	$format = str_replace('dd', 'd', $format);
	
	$format = str_replace('hh', 'h', $format);
	$format = str_replace('HH', 'H', $format);
	$format = str_replace('ii', 'i', $format);
	$format = str_replace('ss', 's', $format);
	$format = str_replace('tt', 't', $format);

	return $format;
}

/**
 * Processes a date array in a usable format;
 * @param array $dateArr Associative array containing date chunks ('y'=>'2004', 'm'=>'5', etc.);
 * @param array $formatRule Associative array containing the output date formatting rules ('y'=> 2 chars, 'm' => 1 char, etc);
 * @param string $outStringRule Defines the output date format;
 * @return string the date in the $outStringRule format;
 */
function KT_applyOutRule2date(&$dateArr, &$formatRule, &$outStringRule) {
	$date = '';

	$dateArrKeys = array_keys($dateArr);
	$formatRuleKeys = array_keys($formatRule);

	$preparedKeys = array_diff($formatRuleKeys, $dateArr);
	if (count($preparedKeys) > 0) {
		if (in_array('h', $preparedKeys)) {
			$value = $dateArr['H'];
			$dateArr['h'] = $value;
			if ($value == 0) {
				$dateArr['h'] = 12;
			}
			$dateArr['t'] = 'AM';
			if ($value > 12 && $value < 24) {
				$dateArr['h'] = $value - 12;
				$dateArr['t'] = 'PM';
			}
		}
		if (in_array('t', $preparedKeys)) {
			$value = $dateArr['H'];
			$dateArr['t'] = 'AM';
			if ($value > 11) {
				$dateArr['t'] = 'PM';
			}
		}
	}


	$formatRuleKeys = array_keys($formatRule);
	$count = count($formatRuleKeys);
	for ($i=0; $i<$count; $i++) {
		$key = $formatRuleKeys[$i];
		$len = $formatRule[$key]['len'];
		$value = $dateArr[$key];

		// convert from less digits to more
		// only for HH
		if (strlen($value) < $len) {
			if ($key == 'H') {
				$dateArr[$key] = '0' . $value;
			}
		}

		// convert from more digits to less
		if (strlen($value) > $len) {
			switch ($key) {
				case 'm':
				case 'd':
				case 'i':
				case 'h':
				case 'H':
				case 's':
					if (substr($value, 0, 1) == '0') {
						$dateArr[$key] = substr($value, 1);
					}
					break;
				case 'y':
					if (strlen($value) == 4) {
						$value = substr($value, 2);
					}
					if ($len == 1 && substr($value, 0, 1) == '0') {
						$value = substr($value, 1);
					}
					$dateArr[$key] = $value;
					break;
				case 't':
					$dateArr[$key] = substr($value, 0, 1);
					break;
			}
		}
	}

	$date = str_replace(array_keys($dateArr), array_values($dateArr), $outStringRule);
	$date = trim(preg_replace('/[-\/\[\]\(\)\s\*\|\+\.:=,]{2,}/', '', $date));

	return $date;
}

/**
 * Validates a date array
 * @param $dateArr the date array
 * @return boolean the date is valid or not
 */
function KT_isValidDate(&$dateArr, $fullDateTime = true) {
	if ($fullDateTime == true) {
		if (!isset($dateArr['y'])) {
			return false;
		}
		if (isset($dateArr['m'])) {
			if ($dateArr['m'] < 1 || $dateArr['m'] > 12) {
				return false;
			}
		} else {
			return false;
		}
	
		$maxday = KT_getDaysOfMonth($dateArr['m'], $dateArr['y']);
	
		if (isset($dateArr['d'])) {
			if ($dateArr['d'] < 1 || $dateArr['d'] > $maxday) {
				return false;
			}
		} else {
			return false;
		}
	}	
	
	if (isset($dateArr['H'])) {
		if ($dateArr['H'] < 0 || $dateArr['H'] > 23) {
			return false;
		}
	} else {
		return false;
	}
	if (isset($dateArr['i'])) {
		if ($dateArr['i'] < 0 || $dateArr['i'] > 59) {
			return false;
		}
	} else {
		return false;
	}
	if (isset($dateArr['s'])) {
		if ($dateArr['s'] < 0 || $dateArr['s'] > 59) {
			return false;
		}
	} else {
		return false;
	}

	return true;
}

/**
 * Return the last day of the given month for the given year; 
 * @param string $month the month in a numeric format (ex. 01 ); 
 * @param int $year the value of the year (ex. 2004) 
 * @return string the last day of the month (ex. 31 );
 */
function KT_getDaysOfMonth($month, $year) {
	$maxday = 1;
	
	switch (intval($month)) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			$maxday = 31;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			$maxday = 30;
			break;
		case 2:
			$maxday = 28;
			if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0)) {
				$maxday = 29;
			}
			break;
	}
	return $maxday;
}

/**
 * Compares 2 date arrays
 * @param array $dateArr1
 * @param array $dateArr2
 * @return integer -1, 1 or 0
 */
function KT_compareDates(&$dateArr1, &$dateArr2) {
	$time1 = $dateArr1['y'] . $dateArr1['m'] . $dateArr1['d'] . $dateArr1['H'] . $dateArr1['i'] . $dateArr1['s'];
	$time2 = $dateArr2['y'] . $dateArr2['m'] . $dateArr2['d'] . $dateArr2['H'] . $dateArr2['i'] . $dateArr2['s'];

	if (floatval($time1) > floatval($time2)) {
		return -1;
	}
	if (floatval($time1) < floatval($time2)) {
		return 1;
	}
	return 0;
}

/**
 * Strips empty values from time expressions
 * @param string $date - datetime expression
 * @return new datetime without 0's
 */
function KT_stripTime($date) {
	if ( strstr($date, ' ') && strstr($date, ':') && (strpos($date, ' ') < strpos($date, ':')) ) {
		$dateArr = explode(' ', $date);
		$timeArr = explode(':', $dateArr[1]);
		for ($i = count($timeArr) - 1; $i >=0; $i--) {
			if ($timeArr[$i] != '0' && $timeArr[$i] != '00') {
				break;
			} else {
				unset($timeArr[$i]);
			}
		}
		// remove time when [time format is hh:mm:ss tt] and [time value is 12 A(M)]
		if ( count($timeArr) == 1 && trim($timeArr[0]) == '12'  &&  count($dateArr) == 3 && (strtolower(trim($dateArr[2])) == 'a' || strtolower(trim($dateArr[2])) == 'am')  ) {
			unset($timeArr[0]);
			unset($dateArr[2]);		
		}
		$dateArr[1] = implode(':', $timeArr);
		if ($dateArr[1] == '') {
			unset($dateArr[1]);
		}
		$date = implode(' ', $dateArr);
	}
	return $date;
}

/**
 * Expands time expressions to full screen format
 * @param string $date - datetime expression
 * @return new datetime with full time part
 */
function KT_expandTime($date) {
	$dateArr = explode(' ', $date);
	$timeArr = explode(':', $dateArr[1]);
	
	$hour = $timeArr[0];
	$min = '00';
	$sec = '00';
	if ( isset($timeArr[1]) ) {
		$min = $timeArr[1];
	}
	if ( isset($timeArr[2]) ) {
		$sec = $timeArr[2];
	}
	if ( isset($dateArr[2]) ) {
		if ( preg_match('/p/i', $dateArr[2]) && $hour < 12 ) {
			$hour += 12;
		}
		if ( preg_match('/a/i', $dateArr[2]) && $hour == 12 ) {
			$hour = '00';
		}
	}
	
	$date = $dateArr[0] . ' ' . KT_convertDate($hour . ':' . $min . ':' . $sec, 'HH:ii:ss', $GLOBALS['KT_screen_time_format_internal']);
	return $date;
}


/**
 * Wrapper for KT_convertDate
 * For internal use
 * @param string $date The date in input format
 * @param string $toScreen if $toScreen == 'toscreen' the date will be outputed in screen format; otherwise in database format
 * @return string the date in the new format
 */
function KT_convertDateCall($date, $toScreen = '') {
	if (!isset($GLOBALS['KT_screen_time_format_internal'])) {
		KT_getInternalTimeFormat();
	}

	if (!preg_match('/^([\d-\/\[\]\(\)\s\*\|\+\.:=,]|a|p|am|pm)+$/i', $date)) {
		return $date;
	}
	$date = str_replace('1900-01-01 ', '', $date);
	if (preg_match('/^\d+$/',$date)) {
		if (strlen(trim($date)) == 14) {
			$newDate = substr($date,0,4).'-'.substr($date,4,2).'-'.substr($date,6,2);
			$newDate .= ' '.substr($date,8,2).':'.substr($date,10,2).':'.substr($date,12,2);
			$date = $newDate;
			$from = 'yyyy-mm-dd HH:ii:ss';
			$to = $GLOBALS['KT_screen_date_format'] . ' ' . $GLOBALS['KT_screen_time_format_internal'];
		} else {
			return $date;
		}
	}
	
	if ( !preg_match('/^(\d+[-\/\[\]\(\)\s\*\|\+\.=,]\d+[-\/\[\]\(\)\s\*\|\+\.=,]\d+)+/i', $date) && 
		!preg_match('/^\d+:\d+(:\d+|\s+a|\s+p|\s+am|\s+pm)/i', $date) 
		) {
		return $date;
	}
	
	if ( $toScreen == '' && preg_match('/^\d+[-\/\[\]\(\)\s\*\|\+\.=,]\d+[-\/\[\]\(\)\s\*\|\+\.=,]\d+\s+\d+/i', $date) ) {
		$date = KT_expandTime($date);
	}
	
	if ( strstr($date, ' ') && strstr($date, ':') && (strpos($date, ' ') < strpos($date, ':')) ) {
		$from = $GLOBALS['KT_screen_date_format'] . ' ' . $GLOBALS['KT_screen_time_format_internal'];
		$to = $GLOBALS['KT_db_date_format'] . ' ' . $GLOBALS['KT_db_time_format_internal'];
	} elseif (strstr($date, ':')) {
		$from = $GLOBALS['KT_screen_time_format_internal'];
		$to = $GLOBALS['KT_db_time_format_internal'];
	} else {
		$from = $GLOBALS['KT_screen_date_format'];
		$to = $GLOBALS['KT_db_date_format'];
	}
	if ($toScreen == 'toscreen') {
		$tmp = $from;
		$from = $to;
		$to = $tmp;
	}
	$date = KT_convertDate($date, $from, $to);
	if ($toScreen == 'toscreen') {
		$date = KT_stripTime($date);
	}
	return $date;
}

/**
 * Converts mm to ii in time formats and sets globals
 * For internal use
 * maybe must be called from tng?
 * @return nothing
 */
function KT_getInternalTimeFormat() {
	$GLOBALS['KT_db_date_format'] = preg_replace("/[yY]/","y",$GLOBALS['KT_db_date_format']);
	$GLOBALS['KT_db_date_format'] = preg_replace("/[mM]/","m",$GLOBALS['KT_db_date_format']);
	$GLOBALS['KT_db_date_format'] = preg_replace("/[dD]/","d",$GLOBALS['KT_db_date_format']);
	$GLOBALS['KT_screen_date_format'] = preg_replace("/[yY]/","y",$GLOBALS['KT_screen_date_format']);
	$GLOBALS['KT_screen_date_format'] = preg_replace("/[mM]/","m",$GLOBALS['KT_screen_date_format']);
	$GLOBALS['KT_screen_date_format'] = preg_replace("/[dD]/","d",$GLOBALS['KT_screen_date_format']);
	
	$GLOBALS['KT_screen_time_format_internal'] = str_replace('m', 'i', $GLOBALS['KT_screen_time_format']);
	$GLOBALS['KT_db_time_format_internal'] = str_replace('m', 'i', $GLOBALS['KT_db_time_format']);
}

/**
 * Converts a date/time/datetime from screen format to database format
 * For internal use
 * @param string $date The date in screen format
 * returns string the date in database format
 */
function KT_formatDate2DB($date) {
	return KT_convertDateCall($date);
}

/**
 * Converts a date/time/datetime from database format to screen format
 * Used for date display
 * @param string $date The date in database format
 * @return string the date in screen format
 */
function KT_formatDate($date) {
	return KT_convertDateCall($date, 'toscreen');
}

/**
 * Escape the expression for \ and '; 
 * @param string $expr the expression to be escaped; 
 * @return string escaped expression if not null or null;
 */
function KT_escapeExpression($expr) {
	if ($expr !== null) {
		$expr = str_replace(array('\\','\''),array('\\\\','\\\''),$expr);
		$expr = "'".$expr."'";
	} else {
		$expr = "null";
	}
	return $expr;
}

/**
 * Return a strip string (without html tags and no longer than $maxchars); 
 * @param string $value the string wich will be parsed; 
 * @param int $maxChars -1 or bigger then 0; 
 * @return string the last day of the month (ex. 31 );
 */
function KT_FormatForList($value, $maxChars) {
	$isBigger = false;
	$value = preg_replace("/<head[^>]*>[\w\W]*?<\/head>[\n\r]*/i", '', $value);
	$value = preg_replace("/<link[^>]*>[\n\r]*/i", '', $value);
	$value = preg_replace("/<script[^>]*>[\w\W]*?<\/script>[\n\r]*/i", '', $value);
	$value = preg_replace("/<style[^>]*>[\w\W]*?<\/style>[\n\r]*/i", '', $value);
	$value = strip_tags($value);
	if ($maxChars != -1) {
		if (strlen(trim($value)) > $maxChars) {
			$value = substr($value, 0, $maxChars);
			$isBigger = true;
		}
	}
	$value = str_replace(array("<", ">"), array("&lt;", "&gt;"), $value);
	if ($value == "") {
		$value = "&nbsp;";
	}
	if ($isBigger) {
		$value .= "...";
	}
	return $value;
}


/**
 * Set the database type and server model in $GLOBALS and return the database type; 
 * @param object $connection the connection object; 
 * @return string the type of the database;
 */
function KT_setDbType($connection) {
	if (!isset($GLOBALS['KT_dataDbType'])) {
		$cname = strtolower($connection->databaseType);
		$GLOBALS['KT_dataDbType'] = $cname;
	}
	if (!isset($GLOBALS['KT_serverModel'])) {
		$GLOBALS['KT_serverModel'] = 'adodb';
		if (isset($connection->servermodel) && $connection->servermodel == 'mysql') {
			$GLOBALS['KT_serverModel'] = 'mysql';
		}
	}
	return $GLOBALS['KT_dataDbType'];
}

/**
 * Escapes a value against a specific type to be used in the transaction SQL
 * Ex: value=ab'b and type=STRING, result=ab\'b (escapes slashes)
 * @param object unknown $colValue The value to prepare
 * @param string $colType The type (STRING_TYPE, NUMERIC_TYPE, etc)
 * @param bool $forFakeRs if it should return values for an SQL query, or for an fake recordset
 * @return object unknown The escaped value
 * @access public
 */
function KT_escapeForSql($colValue, $colType, $forFakeRs = false) {
	$type2empty = array(
		'STRING_TYPE'=>'null',
		'NUMERIC_TYPE' => 'null',
		'DOUBLE_TYPE' => 'null',
		'DATE_TYPE' => 'null',
		'DATE_ACCESS_TYPE'=>'null',
		'FILE_TYPE'=>'null',
		'CHECKBOX_YN_TYPE'=>"'N'",
		'CHECKBOX_1_0_TYPE'=>'0',
		'CHECKBOX_-1_0_TYPE'=>'0',
		'CHECKBOX_TF_TYPE'=>"'f'"
	);
	$type2quote = array(
		'STRING_TYPE'=>'\'',
		'NUMERIC_TYPE' => '',
		'DOUBLE_TYPE' => '',
		'DATE_TYPE' => '\'',
		'DATE_ACCESS_TYPE'=>'#',
		'FILE_TYPE'=>'\'',
		'CHECKBOX_YN_TYPE'=>'\'',
		'CHECKBOX_1_0_TYPE'=>'',
		'CHECKBOX_-1_0_TYPE'=>'',
		'CHECKBOX_TF_TYPE'=>'\'',
	);
	// fake rs empty values
	if ($forFakeRs) {
		$type2empty = array(
			'STRING_TYPE'=>'',
			'NUMERIC_TYPE' => '',
			'DOUBLE_TYPE' => '',
			'DATE_TYPE' => '',
			'DATE_ACCESS_TYPE'=>'',
			'FILE_TYPE'=>'',
			'CHECKBOX_YN_TYPE'=>'N',
			'CHECKBOX_1_0_TYPE'=>'0',
			'CHECKBOX_-1_0_TYPE'=>'0',
			'CHECKBOX_TF_TYPE'=>'f'
		);
	}
	
	if (is_null($colValue) || $colValue === '') {
		$tmValue = $type2empty[$colType];
	} else {
		if ($colType == "NUMERIC_TYPE") {
			$colValue = intval($colValue);
		} elseif ($colType == "DOUBLE_TYPE") {
			$colValue = str_replace(',', '.', $colValue);
			$colValue = floatval($colValue);
		}
		if ($forFakeRs) {
			$tmValue = $colValue;
		} else {
			$quote = $type2quote[$colType];
			$tmValue = $quote . str_replace(array("'", "\\"), array("''", "\\\\"), $colValue) . $quote;
		}
	}
	return $tmValue;
}

/**
 * Escapes a value of a field name to be used in the transaction SQL
 * Ex: First Name gets translated into `First Name`
 * @param string $colName The DataBase field name
 * @return string The escaped field name
 * @access public
 */
function KT_escapeFieldName($colName) {
	return $colName;
	$startq = '"';
	$endq = '"';

	// if colname contains ., return as is
	if (preg_match('/\./', $colName)) {
		return $colName;
	}

	if (isset($GLOBALS['KT_dataDbType'])) {
		if (strpos($GLOBALS['KT_dataDbType'],'mysql') !== FALSE) {
			$startq = '`';
			$endq = '`';
		}
		
		// if name is of the form `name`, quote it
		if (preg_match('/^`(.+)`$/', $colName, $matches)) {
			return $startq . $matches[1] . $endq;
		}
		
		return $startq . $colName . $endq;
	}
	return $startq . $colName . $endq;
}

/**
 * Add the URL to the $_SESSION['KT_backArr'] array if this array not exist;
 * If the array $_SESSION['KT_backArr'] exist check if the last entry is identical with the argument; 
 * If this 2 url are identical the old one is replaced with the new one, if not the new one is append it.
 * @param string $newUrl the url to be add to the array; 
 * @return nothing;
 */
function KT_SessionKtBack($newUrl) {
	KT_session_start();
	$newUrl = KT_addReplaceParam($newUrl, 'KT_ajax_request');
	if (!isset($_SESSION['KT_backArr'])) {
		$_SESSION['KT_backArr'] = array();
		array_push($_SESSION['KT_backArr'],$newUrl);
	} elseif (count($_SESSION['KT_backArr'])>0) {
		$oldUrl = array_pop($_SESSION['KT_backArr']);
		
		$toldUrl = KT_addReplaceParam($oldUrl, '/^pageNum_.*$/i');
		$toldUrl = KT_addReplaceParam($toldUrl, '/^totalRows_.*$/i');
		$toldUrl = KT_addReplaceParam($toldUrl, '/^sorter_.*$/i');
		$toldUrl = KT_addReplaceParam($toldUrl, '/^show_all_.*$/i');
		$toldUrl = KT_addReplaceParam($toldUrl, '/^show_filter_.*$/i');
		$toldUrl = KT_addReplaceParam($toldUrl, '/^reset_filter_.*$/i');
		$toldUrl = KT_addReplaceParam($toldUrl, 'isFlash');
		$toldUrl = KT_addReplaceParam($toldUrl, session_name());

		$tnewUrl = KT_addReplaceParam($newUrl, '/^pageNum_.*$/i');
		$tnewUrl = KT_addReplaceParam($tnewUrl, '/^totalRows_.*$/i');
		$tnewUrl = KT_addReplaceParam($tnewUrl, '/^sorter_.*$/i');
		$tnewUrl = KT_addReplaceParam($tnewUrl, '/^show_all_.*$/i');
		$tnewUrl = KT_addReplaceParam($tnewUrl, '/^show_filter_.*$/i');
		$tnewUrl = KT_addReplaceParam($tnewUrl, '/^reset_filter_.*$/i');
		$tnewUrl = KT_addReplaceParam($tnewUrl, 'isFlash');
		$tnewUrl = KT_addReplaceParam($tnewUrl, session_name());
		
		
		if ($tnewUrl != $toldUrl) {
			array_push($_SESSION['KT_backArr'],$oldUrl);
		}
		array_push($_SESSION['KT_backArr'],$newUrl);
	} else {
		array_push($_SESSION['KT_backArr'],$newUrl);
	}
}

/**
 * Set the the permissions for folder/file setted in GLOBALS['KT_folder_mode']/GLOBALS['KT_file_mode']; 
 * @fileName string $fileName the path; 
 * @isFolder boolean $isFolder;
 * @return boolean true is ;
 */
function KT_setFilePermissions($fileName, $isFolder = false) {
	$ret = false;
	$tmp_perms = "";
	if ($isFolder == true && isset($GLOBALS['KT_folder_mode'])) {
		$tmp_perms = $GLOBALS['KT_folder_mode'];
	} elseif (isset($GLOBALS['KT_file_mode'])){
		$tmp_perms = $GLOBALS['KT_file_mode'];
	}
	if ($tmp_perms != "") {
		if ($fileName != '' && file_exists($fileName)) {
			@clearstatcache();
			$old_perms = @fileperms($fileName);
			if ($old_perms !== false) {
				$old_perms = decoct($old_perms);
				$old_perms = substr($old_perms,-3,3);
				$old_perms = octdec($old_perms);
				$new_perms = $old_perms | octdec($tmp_perms);
				$ret = @chmod($fileName, $new_perms);
			} else {
				$ret = false;
			}
		}
	}
	return $ret;
}

/**
 * return the type of request this page has been used
 * @return boolean true if the request is and Ajax Request
 */
function KT_is_ajax_request() {
	return isset($_GET['KT_ajax_request']);
}

/**
 * Leave just ASCII chars (alpha-numeric) and _ . , ; () [] 
 * @param string string to be parsed;
 * @param string if equal folder let /;
 * @return string;
 * @access public
 */
function KT_replaceSpecialChars($text, $what) {
	if ($what == 'folder') {
		return preg_replace("/[^\/0-9a-z\.,;\-_\(\)\[\]\s]/i", "_", $text);
	} else {
		return preg_replace("/[^0-9a-z\.,;\-_\(\)\[\]\s]/i", "_", $text);
	}
}

/**
 * overwrite the Pragma: no-cache header which might have been sent by the server
 * To mark a response as "never expires," an origin server sends an Expires date 
 * @param int number of seconds;
 * @return nothing;
 * @access public
 */
function KT_sendExpireHeader($seconds) {
	//overwrite the Pragma: no-cache header which might have been sent by the server
	header('Pragma: cache', true);

	//	To mark a response as "never expires," an origin server sends an Expires date 
	//approximately one year from the time the response is sent.
	//	HTTP/1.1 servers SHOULD NOT send Expires dates more than one year in the future.
	//	the Date format is: Fri, 16 Jun 2006 10:09:26 GMT
	header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time() - $seconds), true);
	header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + $seconds), true);

	//Expires header is ignored by HTTP/1.1-compliant clients
	header('Cache-Control: max-age=' . $seconds, true);
}

/**
 * Remove denied tags.
 * @param string text to be cleaned
 * @param string denied tags
 * @param string allowed tags
 * @return cleaned text;
 * @access public
 */
function KT_cleanContent($text, $denied, $allowed) {
	$arrDenied = explode(',', trim($denied));
	$arrAllowed = explode(',', trim($allowed));
	array_walk($arrDenied, 'trim');
	array_walk($arrAllowed, 'trim');
	
	if (trim($denied)!='' && count($arrDenied) > 0) {
		preg_match_all('/<(\w+)[^>]*>/', $text, $contentTags);
		if (isset($contentTags[1])) {
			foreach($contentTags[1] as $k => $tag) {
				if (in_array(strtolower($tag), $arrDenied)) {
					$text = preg_replace('/<\/?' . $tag . '[^>]*>/i', '', $text);
				}
			}
		}	
	} else if (trim($allowed) && count($arrAllowed) > 0) {
		$allowed_tags_list = '<' . implode('>,<', $arrAllowed) . '>';
		$text = strip_tags($text, $allowed_tags_list);		
	}	
	// remove script;
	if (in_array('script', $arrDenied) || (count($arrAllowed)>0 && !in_array('script', $arrAllowed))) {
		while(preg_match_all("/<[^\/][\w\W]*?((on[a-z]+\s*=\s*\"[\w\W]*?\")|(on[a-z]+\s*=\s*'[\w\W]*?')|(on[a-z]+\s*=\s*[^\s]*))[\w\W]*?>/ims", $text, $m)) {
			for ($i=0; $i<count($m[0]); $i++) {
				$re = str_replace($m[1][$i], '', $m[0][$i]);
				$text = str_replace($m[0][$i], $re, $text);
			}
		}
		if (preg_match_all("/<[^>]+>/ims", $text, $m)) {
			for ($i=0; $i<count($m[0]); $i++) {
				while (preg_match("/(\W\w+\s*=\s*\"([^\"]*j\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t\s*:[^\"]*)\"+)|(\W\w+\s*=\s*'([^']*j\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t\s*:[^']*)'+)|(\W\w+\s*=\s*j\s*a\s*v\s*a\s*s\s*c\s*r\s*i\s*p\s*t\s*[^\s>]*)/ims", $m[0][$i], $mm)) {
					$re = str_replace($mm[count($m)-1], '', $m[0][$i]);
					$text = str_replace($m[0][$i], $re, $text);
					$m[0][$i] = $re;
				}				
			}
		}
		// style
		if (preg_match_all("/<\s*style[\w\W]*>([\s\S]*)<\/style>/ims", $text, $m)) {
			for ($i=0; $i<count($m[0]); $i++) {
				if (preg_match_all("/:\s*(expression)\s*\(/ims", $m[0][$i], $mm)) {
					$re = str_replace($mm[1], '', $m[0][$i]);
					$text = str_replace($m[0][$i], $re, $text);
				}
			}
		}
		//inline style
		if (preg_match_all("/<(.*)style\s*=[^>]+>/ims", $text, $m)) {
			for ($i=0; $i<count($m[0]); $i++) {
				if (preg_match_all("/:\s*(expression)\s*\(/ims", $m[0][$i], $mm)) {
					$re = str_replace($mm[1], '', $m[0][$i]);
					$text = str_replace($m[0][$i], $re, $text);
				}
			}
		}
	}

	return $text;
}
?>
Back to Top