sugarcrm_dev /include/utils.php

Language PHP Lines 4326
MD5 Hash 88daed9f46940b94fdfe45feae42cb08
Repository https://github.com/mikmagic/sugarcrm_dev.git 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
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
<?php
/*********************************************************************************
 * SugarCRM Community Edition is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License along with
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/

/*********************************************************************************

 * Description:  Includes generic helper functions used throughout the application.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 ********************************************************************************/
require_once('include/SugarObjects/SugarConfig.php');
require_once('include/utils/security_utils.php');



function make_sugar_config(&$sugar_config)
{
	/* used to convert non-array config.php file to array format */
	global $admin_export_only;
	global $cache_dir;
	global $calculate_response_time;
	global $create_default_user;
	global $dateFormats;
	global $dbconfig;
	global $dbconfigoption;
	global $default_action;
	global $default_charset;
	global $default_currency_name;
	global $default_currency_symbol;
	global $default_currency_iso4217;
	global $defaultDateFormat;
	global $default_language;
	global $default_module;
	global $default_password;
	global $default_permission_mode;
	global $default_theme;
	global $defaultTimeFormat;
	global $default_user_is_admin;
	global $default_user_name;
	global $disable_export;
	global $disable_persistent_connections;
	global $display_email_template_variable_chooser;
	global $display_inbound_email_buttons;
	global $history_max_viewed;
	global $host_name;
	global $import_dir;
	global $languages;
	global $list_max_entries_per_page;
	global $lock_default_user_name;
	global $log_memory_usage;
	global $requireAccounts;
	global $RSS_CACHE_TIME;
	global $session_dir;
	global $site_URL;
	global $site_url;
	global $sugar_version;
	global $timeFormats;
	global $tmp_dir;
	global $translation_string_prefix;
	global $unique_key;
	global $upload_badext;
	global $upload_dir;
	global $upload_maxsize;
	global $import_max_execution_time;
	global $list_max_entries_per_subpanel;
	global $passwordsetting;

	// assumes the following variables must be set:
	// $dbconfig, $dbconfigoption, $cache_dir, $import_dir, $session_dir, $site_URL, $tmp_dir, $upload_dir

	$sugar_config = array (
	'admin_export_only' => empty($admin_export_only) ? false : $admin_export_only,
	'export_delimiter' => empty($export_delimiter) ? ',' : $export_delimiter,
	'cache_dir' => empty($cache_dir) ? 'cache/' : $cache_dir,
	'calculate_response_time' => empty($calculate_response_time) ? true : $calculate_response_time,
	'create_default_user' => empty($create_default_user) ? false : $create_default_user,
	'chartEngine' => 'Jit',
	'date_formats' => empty($dateFormats) ? array(
	'Y-m-d'=>'2010-12-23',
	'd-m-Y' => '23-12-2010',
	'm-d-Y'=>'12-23-2010',
	'Y/m/d'=>'2010/12/23',
	'd/m/Y' => '23/12/2010',
	'm/d/Y'=>'12/23/2010',
	'Y.m.d' => '2010.12.23',
	'd.m.Y' => '23.12.2010',
	'm.d.Y' => '12.23.2010'
		) : $dateFormats,
	'dbconfig' => $dbconfig,  // this must be set!!
	'dbconfigoption' => $dbconfigoption,  // this must be set!!
	'default_action' => empty($default_action) ? 'index' : $default_action,
	'default_charset' => empty($default_charset) ? 'UTF-8' : $default_charset,
	'default_currency_name' => empty($default_currency_name) ? 'US Dollar' : $default_currency_name,
	'default_currency_symbol' => empty($default_currency_symbol) ? '$' : $default_currency_symbol,
	'default_currency_iso4217' => empty($default_currency_iso4217) ? '$' : $default_currency_iso4217,
	'default_date_format' => empty($defaultDateFormat) ? 'm/d/Y' : $defaultDateFormat,
	'default_export_charset' => 'UTF-8',
	'default_language' => empty($default_language) ? 'en_us' : $default_language,
	'default_module' => empty($default_module) ? 'Home' : $default_module,
	'default_password' => empty($default_password) ? '' : $default_password,
	'default_permissions' => array (
		'dir_mode' => 02770,
		'file_mode' => 0660,
		'chown' => '',
		'chgrp' => '',
	),
    'default_theme' => empty($default_theme) ? 'Sugar5' : $default_theme,
    'default_time_format' => empty($defaultTimeFormat) ? 'h:ia' : $defaultTimeFormat,
	'default_user_is_admin' => empty($default_user_is_admin) ? false : $default_user_is_admin,
	'default_user_name' => empty($default_user_name) ? '' : $default_user_name,
	'disable_export' => empty($disable_export) ? false : $disable_export,
    'disable_persistent_connections' => empty($disable_persistent_connections) ? false : $disable_persistent_connections,
    'display_email_template_variable_chooser' => empty($display_email_template_variable_chooser) ? false : $display_email_template_variable_chooser,
	'display_inbound_email_buttons' => empty($display_inbound_email_buttons) ? false : $display_inbound_email_buttons,
	'history_max_viewed' => empty($history_max_viewed) ? 50 : $history_max_viewed,
	'host_name' => empty($host_name) ? 'localhost' : $host_name,
	'import_dir' => $import_dir,  // this must be set!!
	'import_max_records_per_file' => 100,
	'languages' => empty($languages) ? array('en_us' => 'English (US)') : $languages,
	'list_max_entries_per_page' => empty($list_max_entries_per_page) ? 20 : $list_max_entries_per_page,
	'list_max_entries_per_subpanel' => empty($list_max_entries_per_subpanel) ? 10 : $list_max_entries_per_subpanel,
	'lock_default_user_name' => empty($lock_default_user_name) ? false : $lock_default_user_name,
	'log_memory_usage' => empty($log_memory_usage) ? false : $log_memory_usage,
    'portal_view' => 'single_user',
	'resource_management' => array (
	    'special_query_limit' => 50000,
	    'special_query_modules' => array('Reports', 'Export', 'Import', 'Administration', 'Sync'),
	    'default_limit' => 1000,
    ),
	'require_accounts' => empty($requireAccounts) ? true : $requireAccounts,
	'rss_cache_time' => empty($RSS_CACHE_TIME) ? '10800' : $RSS_CACHE_TIME,
	'session_dir' => $session_dir,  // this must be set!!
	'site_url' => empty($site_URL) ? $site_url : $site_URL,  // this must be set!!
	'showDetailData' => true, // if true, read-only ACL fields will still appear on EditViews as non-editable
	'showThemePicker' => true,
	'sugar_version' => empty($sugar_version) ? 'unknown' : $sugar_version,
	'time_formats' => empty($timeFormats) ? array (
	'H:i'=>'23:00', 'h:ia'=>'11:00 pm', 'h:iA'=>'11:00PM',
	'H.i'=>'23.00', 'h.ia'=>'11.00 pm', 'h.iA'=>'11.00PM' ) : $timeFormats,
	'tmp_dir' => $tmp_dir,  // this must be set!!
	'translation_string_prefix' => empty($translation_string_prefix) ? false : $translation_string_prefix,
	'unique_key' => empty($unique_key) ? md5(create_guid()) : $unique_key,
	'upload_badext' => empty($upload_badext) ? array (
	'php', 'php3', 'php4', 'php5', 'pl', 'cgi', 'py',
	'asp', 'cfm', 'js', 'vbs', 'html', 'htm' ) : $upload_badext,
	'upload_dir' => $upload_dir,  // this must be set!!
	'upload_maxsize' => empty($upload_maxsize) ? 3000000 : $upload_maxsize,
	'import_max_execution_time' => empty($import_max_execution_time) ? 3600 : $import_max_execution_time,
	'lock_homepage' => false,
	'lock_subpanels' => false,
	'max_dashlets_homepage' => 15,
	'dashlet_display_row_options' => array('1','3','5','10'),
	'default_max_tabs' => empty($max_tabs) ? '7' : $max_tabs,
	'default_subpanel_tabs' =>  empty($subpanel_tabs) ? true : $subpanel_tabs,
	'default_subpanel_links' => empty($subpanel_links) ? false : $subpanel_links,
	'default_swap_last_viewed' => empty($swap_last_viewed) ? false : $swap_last_viewed,
	'default_swap_shortcuts' => empty($swap_shortcuts) ? false : $swap_shortcuts,
	'default_navigation_paradigm' => empty($navigation_paradigm) ? 'gm' : $navigation_paradigm,
    'default_call_status' => 'Planned',
	'js_lang_version' => 1,
	'passwordsetting' => empty($passwordsetting) ? array (
	    'SystemGeneratedPasswordON' => '',
	    'generatepasswordtmpl' => '',
	    'lostpasswordtmpl' => '',
	    'forgotpasswordON' => true,
    	'linkexpiration' => '1',
    	'linkexpirationtime' => '30',
    	'linkexpirationtype' => '1',
	    'systexpiration' => '0',
	    'systexpirationtime' => '',
	    'systexpirationtype' => '0',
	    'systexpirationlogin' => '',
		) : $passwordsetting,
	);
}

function get_sugar_config_defaults() {
	global $locale;
	/**
	 * used for getting base values for array style config.php.  used by the
	 * installer and to fill in new entries on upgrades.  see also:
	 * sugar_config_union
	 */

	$sugar_config_defaults = array (
	'admin_export_only' => false,
	'export_delimiter' => ',',
	'cache_dir' => 'cache/',
	'calculate_response_time' => true,
	'create_default_user' => false,
 	'chartEngine' => 'Jit',
	'date_formats' => array (
	'Y-m-d' => '2010-12-23', 'm-d-Y' => '12-23-2010', 'd-m-Y' => '23-12-2010',
	'Y/m/d' => '2010/12/23', 'm/d/Y' => '12/23/2010', 'd/m/Y' => '23/12/2010',
	'Y.m.d' => '2010.12.23', 'd.m.Y' => '23.12.2010', 'm.d.Y' => '12.23.2010',),
	'dbconfigoption' => array (
	'persistent' => true,
	'autofree' => false,
	'debug' => 0,
	'seqname_format' => '%s_seq',
	'portability' => 0,
	'ssl' => false ),
	'default_action' => 'index',
	'default_charset' => return_session_value_or_default('default_charset',
	'UTF-8'),
	'default_currency_name' => return_session_value_or_default('default_currency_name', 'US Dollar'),
	'default_currency_symbol' => return_session_value_or_default('default_currency_symbol', '$'),
	'default_currency_iso4217' => return_session_value_or_default('default_currency_iso4217', 'USD'),
	'default_currency_significant_digits' => return_session_value_or_default('default_currency_significant_digits', 2),
	'default_number_grouping_seperator' => return_session_value_or_default('default_number_grouping_seperator', ','),
	'default_decimal_seperator' => return_session_value_or_default('default_decimal_seperator', '.'),
	'default_date_format' => 'm/d/Y',
	'default_export_charset' => 'UTF-8',
	'default_language' => return_session_value_or_default('default_language',
	'en_us'),
	'default_module' => 'Home',
	'default_password' => '',
	'default_permissions' => array (
		'dir_mode' => 02770,
		'file_mode' => 0660,
		'user' => '',
		'group' => '',
	),
	'default_theme' => return_session_value_or_default('site_default_theme', 'Sugar5'),
	'default_time_format' => 'h:ia',
	'default_user_is_admin' => false,
	'default_user_name' => '',
	'disable_export' => false,
	'disable_persistent_connections' =>
	return_session_value_or_default('disable_persistent_connections',
	'false'),
    'display_email_template_variable_chooser' => false,
	'display_inbound_email_buttons' => false,
	'dump_slow_queries' => false,
	'email_default_editor' => 'html',
	'email_default_client' => 'sugar',
	'email_default_delete_attachments' => true,
	'history_max_viewed' => 50,
	'installer_locked' => true,
	'import_max_records_per_file' => 100,
	'languages' => array('en_us' => 'English (US)'),
	'large_scale_test' => false,
	'list_max_entries_per_page' => 20,
	'list_max_entries_per_subpanel' => 10,
	'lock_default_user_name' => false,
	'log_memory_usage' => false,
	'portal_view' => 'single_user',
    'resource_management' => array (
	    'special_query_limit' => 50000,
	    'special_query_modules' => array('Reports', 'Export', 'Import', 'Administration', 'Sync'),
	    'default_limit' => 1000,
    ),
	'require_accounts' => true,
	'rss_cache_time' => return_session_value_or_default('rss_cache_time',
	'10800'),
	'save_query' => 'all',
	'showDetailData' => true, // if true, read-only ACL fields will still appear on EditViews as non-editable
	'showThemePicker' => true,
	'slow_query_time_msec' => '100',
    'sugarbeet' => true,
    'time_formats' => array (
	'H:i'=>'23:00', 'h:ia'=>'11:00pm', 'h:iA'=>'11:00PM',
	'H.i'=>'23.00', 'h.ia'=>'11.00pm', 'h.iA'=>'11.00PM' ),
    'tracker_max_display_length' => 15,
	'translation_string_prefix' =>
	return_session_value_or_default('translation_string_prefix', false),
	'upload_badext' => array (
	'php', 'php3', 'php4', 'php5', 'pl', 'cgi', 'py',
	'asp', 'cfm', 'js', 'vbs', 'html', 'htm' ),
	'upload_maxsize' => 3000000,
	'import_max_execution_time' => 3600,
//	'use_php_code_json' => returnPhpJsonStatus(),
	'verify_client_ip' => true,
	'js_custom_version' => '',
	'js_lang_version' => 1,
	'default_number_grouping_seperator' => ',',
	'default_decimal_seperator' => '.',
	'lock_homepage' => false,
	'lock_subpanels' => false,
	'max_dashlets_homepage' => '15',
	'default_max_tabs' => '7',
	'dashlet_display_row_options' => array('1','3','5','10'),
	'default_subpanel_tabs' =>  true,
	'default_subpanel_links' => false,
	'default_swap_last_viewed' => false,
	'default_swap_shortcuts' => false,
	'default_navigation_paradigm' => 'gm',
	'admin_access_control' => false,
  	'use_common_ml_dir'	=> false,
  	'common_ml_dir' => '',
	'vcal_time' => '2',
	'passwordsetting' => empty($passwordsetting) ? array (
	    'SystemGeneratedPasswordON' => '',
	    'generatepasswordtmpl' => '',
	    'lostpasswordtmpl' => '',
	    'forgotpasswordON' => false,
    	'linkexpiration' => '1',
    	'linkexpirationtime' => '30',
    	'linkexpirationtype' => '1',
	    'systexpiration' => '0',
	    'systexpirationtime' => '',
	    'systexpirationtype' => '0',
	    'systexpirationlogin' => '',
		) : $passwordsetting,
	'use_real_names' => true,
	);

	if(!is_object($locale)) {
		$locale = new Localization();
	}

	$sugar_config_defaults['default_currencies'] = $locale->getDefaultCurrencies();

	$sugar_config_defaults = sugarArrayMerge($locale->getLocaleConfigDefaults(), $sugar_config_defaults);
	return( $sugar_config_defaults );
}

/**
 * @deprecated use SugarView::getMenu() instead
 */
function load_menu($path){
	global $module_menu;

	if(file_exists($path . 'Menu.php'))
	{
		require($path . 'Menu.php');
	}
	if(file_exists('custom/' . $path . 'Ext/Menus/menu.ext.php'))
	{
		require('custom/' . $path . 'Ext/Menus/menu.ext.php');
	}
	if(file_exists('custom/application/Ext/Menus/menu.ext.php'))
	{
		require('custom/application/Ext/Menus/menu.ext.php');
	}
	return $module_menu;
}

/**
 * get_notify_template_file
 * This function will return the location of the email notifications template to use
 *
 * @return string relative file path to email notifications template file
 */
function get_notify_template_file($language){
	/*
	 * Order of operation:
	 * 1) custom version of specified language
	 * 2) stock version of specified language
	 * 3) custom version of en_us template
	 * 4) stock en_us template
	 */

	// set $file to the base code template so it's set if none of the conditions pass
	$file = "include/language/en_us.notify_template.html";

	if(file_exists("custom/include/language/{$language}.notify_template.html")){
		$file = "custom/include/language/{$language}.notify_template.html";
	}
	else if(file_exists("include/language/{$language}.notify_template.html")){
		$file = "include/language/{$language}.notify_template.html";
	}
	else if(file_exists("custom/include/language/en_us.notify_template.html")){
		$file = "custom/include/language/en_us.notify_template.html";
	}

	return $file;
}

function sugar_config_union( $default, $override ){
	// a little different then array_merge and array_merge_recursive.  we want
	// the second array to override the first array if the same value exists,
	// otherwise merge the unique keys.  it handles arrays of arrays recursively
	// might be suitable for a generic array_union
	if( !is_array( $override ) ){
		$override = array();
	}
	foreach( $default as $key => $value ){
		if( !array_key_exists($key, $override) ){
			$override[$key] = $value;
		}
		else if( is_array( $key ) ){
			$override[$key] = sugar_config_union( $value, $override[$key] );
		}
	}
	return( $override );
}

function make_not_writable( $file ){
	// Returns true if the given file/dir has been made not writable
	$ret_val = false;
	if( is_file($file) || is_dir($file) ){
		if( !is_writable($file) ){
			$ret_val = true;
		}
		else {
			$original_fileperms = fileperms($file);

			// take away writable permissions
			$new_fileperms = $original_fileperms & ~0x0092;
			@sugar_chmod($file, $new_fileperms);

			if( !is_writable($file) ){
				$ret_val = true;
			}
		}
	}
	return $ret_val;
}


/** This function returns the name of the person.
 * It currently returns "first last".  It should not put the space if either name is not available.
 * It should not return errors if either name is not available.
 * If no names are present, it will return ""
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function return_name($row, $first_column, $last_column)
{
	$first_name = "";
	$last_name = "";
	$full_name = "";

	if(isset($row[$first_column]))
	{
		$first_name = stripslashes($row[$first_column]);
	}

	if(isset($row[$last_column]))
	{
		$last_name = stripslashes($row[$last_column]);
	}

	$full_name = $first_name;

	// If we have a first name and we have a last name
	if($full_name != "" && $last_name != "")
	{
		// append a space, then the last name
		$full_name .= " ".$last_name;
	}
	// If we have no first name, but we have a last name
	else if($last_name != "")
	{
		// append the last name without the space.
		$full_name .= $last_name;
	}

	return $full_name;
}


function get_languages()
{
	global $sugar_config;
	$lang = $sugar_config['languages'];
    if(!empty($sugar_config['disabled_languages'])){
        foreach(explode(',', $sugar_config['disabled_languages']) as $disable) {
            unset($lang[$disable]);
        }
    }
	return $lang;
}

function get_all_languages()
{
	global $sugar_config;
	return $sugar_config['languages'];
}


function get_language_display($key)
{
	global $sugar_config;
	return $sugar_config['languages'][$key];
}

function get_assigned_user_name($assigned_user_id, $is_group = '') {
	static $saved_user_list = null;

	if(empty($saved_user_list)) {
		$saved_user_list = get_user_array(false, '', '', false, null, $is_group);
	}

	if(isset($saved_user_list[$assigned_user_id])) {
		return $saved_user_list[$assigned_user_id];
	}

	return '';
}

/**
 * retrieves the user_name column value (login)
 * @param string id GUID of user
 * @return string
 */
function get_user_name($id) {
	global $db;

	if(empty($db))
	$db = DBManagerFactory::getInstance();

	$q = "SELECT user_name FROM users WHERE id='{$id}'";
	$r = $db->query($q);
	$a = $db->fetchByAssoc($r);

	return (empty($a)) ? '' : $a['user_name'];
}


//TODO Update to use global cache
function get_user_array($add_blank=true, $status="Active", $assigned_user="", $use_real_name=false, $user_name_begins = null, $is_group=' AND portal_only=0 ', $from_cache = true) {
	global $locale;
	global $sugar_config;

	if(empty($locale)) {

		$locale = new Localization();
	}
	if($from_cache)
		$user_array = get_register_value('user_array', $add_blank. $status . $assigned_user);

	if(!isset($user_array)) {
		$db = DBManagerFactory::getInstance();
		$temp_result = Array();
		// Including deleted users for now.
		if (empty($status)) {
			$query = "SELECT id, first_name, last_name, user_name from users WHERE 1=1".$is_group;
		}
		else {
			$query = "SELECT id, first_name, last_name, user_name from users WHERE status='$status'".$is_group;
		}

		if (!empty($user_name_begins)) {
			$query .= " AND user_name LIKE '$user_name_begins%' ";
		}
		if (!empty($assigned_user)) {
			$query .= " OR id='$assigned_user'";
		}
		$query = $query.' ORDER BY user_name ASC';
		$GLOBALS['log']->debug("get_user_array query: $query");
		$result = $db->query($query, true, "Error filling in user array: ");

		if ($add_blank==true) {
			// Add in a blank row
			$temp_result[''] = '';
		}

		// Get the id and the name.
		while($row = $db->fetchByAssoc($result)) {
			if($use_real_name == true || showFullName()) {
				if(isset($row['last_name'])) { // cn: we will ALWAYS have both first_name and last_name (empty value if blank in db)
					$temp_result[$row['id']] = $locale->getLocaleFormattedName($row['first_name'],$row['last_name']);
				} else {
					$temp_result[$row['id']] = $row['user_name'];
				}
			} else {
				$temp_result[$row['id']] = $row['user_name'];
			}
		}

		$user_array = $temp_result;
		if($from_cache)
			set_register_value('user_array', $add_blank. $status . $assigned_user, $temp_result);
	}


	return $user_array;
}


/**
 * uses a different query to return a list of users than get_user_array()
 * @param args string where clause entry
 * @return array Array of Users' details that match passed criteria
 */
function getUserArrayFromFullName($args, $hide_portal_users = false) {
	global $locale;
	$db = DBManagerFactory::getInstance();

	$argArray = array();
	if(strpos($args, " ")) {
		$argArray = explode(" ", $args);
	} else {
		$argArray[] = $args;
	}

	$inClause = '';
	foreach($argArray as $arg) {
		if(!empty($inClause)) {
			$inClause .= ' OR ';
		}
		if(empty($arg))
		continue;

		$inClause .= "(first_name LIKE '{$arg}%' OR last_name LIKE '{$arg}%')";
	}

	$query  = "SELECT id, first_name, last_name, user_name FROM users WHERE status='Active' AND deleted=0 AND ";
	if ( $hide_portal_users ) {
	    $query .= " portal_only=0 AND ";
	}
	$query .= $inClause;
	$query .= " ORDER BY last_name ASC";

	$r = $db->query($query);
	$ret = array();
	while($a = $db->fetchByAssoc($r)) {
		$ret[$a['id']] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name']);
	}

	return $ret;
}

/**
 *
 * based on user pref then system pref
 */
function showFullName() {
	global $sugar_config;
	global $current_user;
	static $showFullName = null;

	if (is_null($showFullName)) {
		$sysPref = !empty($sugar_config['use_real_names']);
		$userPref = (is_object($current_user)) ? $current_user->getPreference('use_real_names') : null;

		if($userPref != null) {
			$showFullName = ($userPref == 'on');
		} else {
			$showFullName = $sysPref;
		}
	}

	return $showFullName;
}

function clean($string, $maxLength)
{
	$string = substr($string, 0, $maxLength);
	return escapeshellcmd($string);
}

/**
 * Copy the specified request variable to the member variable of the specified object.
 * Do no copy if the member variable is already set.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function safe_map($request_var, & $focus, $always_copy = false)
{
	safe_map_named($request_var, $focus, $request_var, $always_copy);
}

/**
 * Copy the specified request variable to the member variable of the specified object.
 * Do no copy if the member variable is already set.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function safe_map_named($request_var, & $focus, $member_var, $always_copy)
{
	if (isset($_REQUEST[$request_var]) && ($always_copy || is_null($focus->$member_var))) {
		$GLOBALS['log']->debug("safe map named called assigning '{$_REQUEST[$request_var]}' to $member_var");
		$focus->$member_var = $_REQUEST[$request_var];
	}
}

/**
 * This function retrieves an application language file and returns the array of strings included in the $app_list_strings var.
 *
 * @param string $language specific language to load
 * @return array lang strings
 */
function return_app_list_strings_language($language)
{
	global $app_list_strings;
	global $sugar_config;

	$cache_key = 'app_list_strings.'.$language;

	// Check for cached value
	$cache_entry = sugar_cache_retrieve($cache_key);
	if(!empty($cache_entry))
	{
		return $cache_entry;
	}

	$default_language = $sugar_config['default_language'];
	$temp_app_list_strings = $app_list_strings;

	$langs = array();
	if ($language != 'en_us') {
	    $langs[] = 'en_us';
	}
	if ($default_language != 'en_us' && $language != $default_language) {
	    $langs[] = $default_language;
	}
	$langs[] = $language;

	$app_list_strings_array = array();

	foreach ( $langs as $lang ) {
	    $app_list_strings = array();
	    if(file_exists("include/language/$lang.lang.php")) {
            include("include/language/$lang.lang.php");
            $GLOBALS['log']->info("Found language file: $lang.lang.php");
        }
        if(file_exists("include/language/$lang.lang.override.php")) {
            include("include/language/$lang.lang.override.php");
            $GLOBALS['log']->info("Found override language file: $lang.lang.override.php");
        }
        if(file_exists("include/language/$lang.lang.php.override")) {
            include("include/language/$lang.lang.php.override");
            $GLOBALS['log']->info("Found override language file: $lang.lang.php.override");
        }

        $app_list_strings_array[] = $app_list_strings;
    }

    $app_list_strings = array();
    foreach ( $app_list_strings_array as $app_list_strings_item ) {
        $app_list_strings = sugarArrayMerge($app_list_strings, $app_list_strings_item);
    }

    foreach ( $langs as $lang ) {
        if(file_exists("custom/application/Ext/Language/$lang.lang.ext.php")) {
            $app_list_strings = _mergeCustomAppListStrings("custom/application/Ext/Language/$lang.lang.ext.php" , $app_list_strings);
            $GLOBALS['log']->info("Found extended language file: $lang.lang.ext.php");
        }
        if(file_exists("custom/include/language/$lang.lang.php")) {
            include("custom/include/language/$lang.lang.php");
            $GLOBALS['log']->info("Found custom language file: $lang.lang.php");
        }
    }

	if(!isset($app_list_strings)) {
		$GLOBALS['log']->fatal("Unable to load the application language file for the selected language ($language) or the default language ($default_language) or the en_us language");
		return null;
	}

	$return_value = $app_list_strings;
	$app_list_strings = $temp_app_list_strings;

	sugar_cache_put($cache_key, $return_value);

	return $return_value;
}

/**
* The dropdown items in custom language files is $app_list_strings['$key']['$second_key'] = $value not
* $GLOBALS['app_list_strings']['$key'] = $value, so we have to delete the original ones in app_list_strings and relace it with the custom ones.
 * @param file string the language that you want include,
 * @param app_list_strings array the golbal strings
 * @return array
 */
 //jchi 25347
function _mergeCustomAppListStrings($file , $app_list_strings){
	$app_list_strings_original = $app_list_strings;
	unset($app_list_strings);
	include($file);
	if(!isset($app_list_strings) || !is_array($app_list_strings)){
		return $app_list_strings_original;
	}
	//Bug 25347: We should not merge custom dropdown fields unless they relate to parent fields or the module list.
	foreach($app_list_strings as $key=>$value)
	{
		$exemptDropdowns = array("moduleList", "parent_type_display", "record_type_display", "record_type_display_notes");
		if (!in_array($key, $exemptDropdowns) && array_key_exists($key, $app_list_strings_original))
		{
	   		unset($app_list_strings_original["$key"]);
		}
   }
   $app_list_strings = sugarArrayMergeRecursive($app_list_strings_original , $app_list_strings);
   return $app_list_strings;
}

/**
 * This function retrieves an application language file and returns the array of strings included.
 *
 * @param string $language specific language to load
 * @return array lang strings
 */
function return_application_language($language)
{
	global $app_strings, $sugar_config;

	$cache_key = 'app_strings.'.$language;

	// Check for cached value
	$cache_entry = sugar_cache_retrieve($cache_key);
	if(!empty($cache_entry))
	{
		return $cache_entry;
	}

	$temp_app_strings = $app_strings;
	$default_language = $sugar_config['default_language'];

	$langs = array();
	if ($language != 'en_us') {
	    $langs[] = 'en_us';
	}
	if ($default_language != 'en_us' && $language != $default_language) {
	    $langs[] = $default_language;
	}

	$langs[] = $language;

	$app_strings_array = array();

	foreach ( $langs as $lang ) {
	    $app_strings = array();
	    if(file_exists("include/language/$lang.lang.php")) {
            include("include/language/$lang.lang.php");
            $GLOBALS['log']->info("Found language file: $lang.lang.php");
        }
        if(file_exists("include/language/$lang.lang.override.php")) {
            include("include/language/$lang.lang.override.php");
            $GLOBALS['log']->info("Found override language file: $lang.lang.override.php");
        }
        if(file_exists("include/language/$lang.lang.php.override")) {
            include("include/language/$lang.lang.php.override");
            $GLOBALS['log']->info("Found override language file: $lang.lang.php.override");
        }
        if(file_exists("custom/application/Ext/Language/$lang.lang.ext.php")) {
            include("custom/application/Ext/Language/$lang.lang.ext.php");
            $GLOBALS['log']->info("Found extended language file: $lang.lang.ext.php");
        }
        if(file_exists("custom/include/language/$lang.lang.php")) {
            include("custom/include/language/$lang.lang.php");
            $GLOBALS['log']->info("Found custom language file: $lang.lang.php");
        }
        $app_strings_array[] = $app_strings;
	}

	$app_strings = array();
    foreach ( $app_strings_array as $app_strings_item ) {
        $app_strings = sugarArrayMerge($app_strings, $app_strings_item);
    }

	if(!isset($app_strings)) {
		$GLOBALS['log']->fatal("Unable to load the application language strings");
		return null;
	}

	// If we are in debug mode for translating, turn on the prefix now!
	if($sugar_config['translation_string_prefix']) {
		foreach($app_strings as $entry_key=>$entry_value) {
			$app_strings[$entry_key] = $language.' '.$entry_value;
		}
	}
	if(isset($_SESSION['show_deleted'])) {
		$app_strings['LBL_DELETE_BUTTON'] = $app_strings['LBL_UNDELETE_BUTTON'];
		$app_strings['LBL_DELETE_BUTTON_LABEL'] = $app_strings['LBL_UNDELETE_BUTTON_LABEL'];
		$app_strings['LBL_DELETE_BUTTON_TITLE'] = $app_strings['LBL_UNDELETE_BUTTON_TITLE'];
		$app_strings['LBL_DELETE'] = $app_strings['LBL_UNDELETE'];
	}

	$app_strings['LBL_ALT_HOT_KEY'] = get_alt_hot_key();

	$return_value = $app_strings;
	$app_strings = $temp_app_strings;

	sugar_cache_put($cache_key, $return_value);

	return $return_value;
}

/**
 * This function retrieves a module's language file and returns the array of strings included.
 *
 * @param string $language specific language to load
 * @param string $module module name to load strings for
 * @param bool $refresh optional, true if you want to rebuild the language strings
 * @return array lang strings
 */
function return_module_language($language, $module, $refresh=false)
{
	global $mod_strings;
	global $sugar_config;
	global $currentModule;

	// Jenny - Bug 8119: Need to check if $module is not empty
	if (empty($module)) {
		$stack  = debug_backtrace();
		$GLOBALS['log']->warn("Variable module is not in return_module_language ". var_export($stack, true));
		return array();
	}

	$cache_key = LanguageManager::getLanguageCacheKey($module, $language);
	// Check for cached value
	$cache_entry = sugar_cache_retrieve($cache_key);
	if(!empty($cache_entry))
	{
		return $cache_entry;
	}

	// Store the current mod strings for later
	$temp_mod_strings = $mod_strings;
	$loaded_mod_strings = array();
	$language_used = $language;
	$default_language = $sugar_config['default_language'];

	if(empty($language)) {
		$language = $default_language;
	}

	// Bug 21559 - So we can get all the strings defined in the template, refresh
	// the vardefs file if the cached language file doesn't exist.
    if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$language.'.lang.php')
			&& !empty($GLOBALS['beanList'][$module])){
		$object = $GLOBALS['beanList'][$module];
		if ($object == 'aCase') {
            $object = 'Case';
		}
		VardefManager::refreshVardefs($module,$object);
	}

	$loaded_mod_strings = LanguageManager::loadModuleLanguage($module, $language,$refresh);

	// cn: bug 6048 - merge en_us with requested language
	if($language != $sugar_config['default_language'])
        $loaded_mod_strings = sugarArrayMerge(
            LanguageManager::loadModuleLanguage($module, $sugar_config['default_language'],$refresh),
                $loaded_mod_strings
            );

    // Load in en_us strings by default
    if($language != 'en_us' && $sugar_config['default_language'] != 'en_us')
        $loaded_mod_strings = sugarArrayMerge(
            LanguageManager::loadModuleLanguage($module, 'en_us', $refresh),
                $loaded_mod_strings
            );

	// If we are in debug mode for translating, turn on the prefix now!
	if($sugar_config['translation_string_prefix']) {
		foreach($loaded_mod_strings as $entry_key=>$entry_value) {
			$loaded_mod_strings[$entry_key] = $language_used.' '.$entry_value;
		}
	}

	$return_value = $loaded_mod_strings;
	if(!isset($mod_strings)){
		$mod_strings = $return_value;
	}
	else
		$mod_strings = $temp_mod_strings;

    sugar_cache_put($cache_key, $return_value);
	return $return_value;
}


/** This function retrieves an application language file and returns the array of strings included in the $mod_list_strings var.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 * If you are using the current language, do not call this function unless you are loading it for the first time */
function return_mod_list_strings_language($language,$module) {
	global $mod_list_strings;
	global $sugar_config;
	global $currentModule;

	$cache_key = "mod_list_str_lang.".$language.$module;

	// Check for cached value
	$cache_entry = sugar_cache_retrieve($cache_key);
	if(!empty($cache_entry))
	{
		return $cache_entry;
	}

	$language_used = $language;
	$temp_mod_list_strings = $mod_list_strings;
	$default_language = $sugar_config['default_language'];

	if($currentModule == $module && isset($mod_list_strings) && $mod_list_strings != null) {
		return $mod_list_strings;
	}

	// cn: bug 6351 - include en_us if file langpack not available
	// cn: bug 6048 - merge en_us with requested language
	include("modules/$module/language/en_us.lang.php");
	$en_mod_list_strings = array();
	if($language_used != $default_language)
	$en_mod_list_strings = $mod_list_strings;

	if(file_exists("modules/$module/language/$language.lang.php")) {
		include("modules/$module/language/$language.lang.php");
	}

	if(file_exists("modules/$module/language/$language.lang.override.php")){
		include("modules/$module/language/$language.lang.override.php");
	}

	if(file_exists("modules/$module/language/$language.lang.php.override")){
		echo 'Please Change:<br>' . "modules/$module/language/$language.lang.php.override" . '<br>to<br>' . 'Please Change:<br>' . "modules/$module/language/$language.lang.override.php";
		include("modules/$module/language/$language.lang.php.override");
	}

	// cn: bug 6048 - merge en_us with requested language
	$mod_list_strings = sugarArrayMerge($en_mod_list_strings, $mod_list_strings);

	// if we still don't have a language pack, then log an error
	if(!isset($mod_list_strings)) {
		$GLOBALS['log']->fatal("Unable to load the application list language file for the selected language($language) or the default language($default_language) for module({$module})");
		return null;
	}

	$return_value = $mod_list_strings;
	$mod_list_strings = $temp_mod_list_strings;

	sugar_cache_put($cache_key, $return_value);
	return $return_value;
}


/** This function retrieves a theme's language file and returns the array of strings included.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function return_theme_language($language, $theme)
{
	global $mod_strings, $sugar_config, $currentModule;

	$language_used = $language;
	$default_language = $sugar_config['default_language'];

	include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php");
	if(file_exists(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php")){
		include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php");
	}
	if(file_exists(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override")){
		echo 'Please Change:<br>' . SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override" . '<br>to<br>' . 'Please Change:<br>' . SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php";
		include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override");
	}
	if(!isset($theme_strings))
	{
		$GLOBALS['log']->warn("Unable to find the theme file for language: ".$language." and theme: ".$theme);
		require(SugarThemeRegistry::get($theme)->getFilePath()."/language/$default_language.lang.php");
		$language_used = $default_language;
	}

	if(!isset($theme_strings))
	{
		$GLOBALS['log']->fatal("Unable to load the theme($theme) language file for the selected language($language) or the default language($default_language)");
		return null;
	}

	// If we are in debug mode for translating, turn on the prefix now!
	if($sugar_config['translation_string_prefix'])
	{
		foreach($theme_strings as $entry_key=>$entry_value)
		{
			$theme_strings[$entry_key] = $language_used.' '.$entry_value;
		}
	}

	return $theme_strings;
}



/** If the session variable is defined and is not equal to "" then return it.  Otherwise, return the default value.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function return_session_value_or_default($varname, $default)
{
	if(isset($_SESSION[$varname]) && $_SESSION[$varname] != "")
	{
		return $_SESSION[$varname];
	}

	return $default;
}

/**
 * Creates an array of where restrictions.  These are used to construct a where SQL statement on the query
 * It looks for the variable in the $_REQUEST array.  If it is set and is not "" it will create a where clause out of it.
 * @param &$where_clauses - The array to append the clause to
 * @param $variable_name - The name of the variable to look for an add to the where clause if found
 * @param $SQL_name - [Optional] If specified, this is the SQL column name that is used.  If not specified, the $variable_name is used as the SQL_name.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function append_where_clause(&$where_clauses, $variable_name, $SQL_name = null)
{
	if($SQL_name == null)
	{
		$SQL_name = $variable_name;
	}

	if(isset($_REQUEST[$variable_name]) && $_REQUEST[$variable_name] != "")
	{
		array_push($where_clauses, "$SQL_name like '".$GLOBALS['db']->quote($_REQUEST[$variable_name])."%'");
	}
}

/**
 * Generate the appropriate SQL based on the where clauses.
 * @param $where_clauses - An Array of individual where clauses stored as strings
 * @returns string where_clause - The final SQL where clause to be executed.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function generate_where_statement($where_clauses)
{
	$where = "";
	foreach($where_clauses as $clause)
	{
		if($where != "")
		$where .= " and ";
		$where .= $clause;
	}

	$GLOBALS['log']->info("Here is the where clause for the list view: $where");
	return $where;
}

/**
 * determines if a passed string matches the criteria for a Sugar GUID
 * @param string $guid
 * @return bool False on failure
 */
function is_guid($guid) {
	if(strlen($guid) != 36) {
		return false;
	}

	if(preg_match("/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/i", $guid)) {
		return true;
	}

	return true;;
}


/**
 * A temporary method of generating GUIDs of the correct format for our DB.
 * @return String contianing a GUID in the format: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
 *
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function create_guid()
{
	$microTime = microtime();
	list($a_dec, $a_sec) = explode(" ", $microTime);

	$dec_hex = dechex($a_dec* 1000000);
	$sec_hex = dechex($a_sec);

	ensure_length($dec_hex, 5);
	ensure_length($sec_hex, 6);

	$guid = "";
	$guid .= $dec_hex;
	$guid .= create_guid_section(3);
	$guid .= '-';
	$guid .= create_guid_section(4);
	$guid .= '-';
	$guid .= create_guid_section(4);
	$guid .= '-';
	$guid .= create_guid_section(4);
	$guid .= '-';
	$guid .= $sec_hex;
	$guid .= create_guid_section(6);

	return $guid;

}

function create_guid_section($characters)
{
	$return = "";
	for($i=0; $i<$characters; $i++)
	{
		$return .= dechex(mt_rand(0,15));
	}
	return $return;
}

function ensure_length(&$string, $length)
{
	$strlen = strlen($string);
	if($strlen < $length)
	{
		$string = str_pad($string,$length,"0");
	}
	else if($strlen > $length)
	{
		$string = substr($string, 0, $length);
	}
}

function microtime_diff($a, $b) {
	list($a_dec, $a_sec) = explode(" ", $a);
	list($b_dec, $b_sec) = explode(" ", $b);
	return $b_sec - $a_sec + $b_dec - $a_dec;
}

// check if Studio is displayed.
function displayStudioForCurrentUser()
{
    global $current_user;
    if ( $current_user->isAdmin() ) {
        return true;
    }



	return true;

}

function displayWorkflowForCurrentUser()
{
    $_SESSION['display_workflow_for_user'] = false;
    return false;
}

// return an array with all modules where the user is an admin.
function get_admin_modules_for_user($user) {
    $GLOBALS['log']->deprecated("get_admin_modules_for_user() is deprecated as of 6.2.2 and may disappear in the future, use Users->getDeveloperModules() instead");

    if(!isset($user)){
        $modules = array();
        return $modules;
    }

    return($user->getDeveloperModules());
    
}

 function get_workflow_admin_modules_for_user($user){
    if (isset($_SESSION['get_workflow_admin_modules_for_user'])) {
        return $_SESSION['get_workflow_admin_modules_for_user'];
    }

    global $moduleList;
    $workflow_mod_list = array();
    foreach($moduleList as $module){
		$workflow_mod_list[$module] = $module;
	}

	// This list is taken from teh previous version of workflow_utils.php
    $workflow_mod_list['Tasks'] = "Tasks";
    $workflow_mod_list['Calls'] = "Calls";
    $workflow_mod_list['Meetings'] = "Meetings";
    $workflow_mod_list['Notes'] = "Notes";
    $workflow_mod_list['ProjectTask'] = "Project Tasks";
	$workflow_mod_list['Leads'] = "Leads";
	$workflow_mod_list['Opportunities'] = "Opportunities";
	// End of list

    $workflow_admin_modules = array();
    if(empty($user)) {
    	return $workflow_admin_modules;
    }
    $actions = ACLAction::getUserActions($user->id);
    //check for ForecastSchedule because it doesn't exist in $workflow_mod_list
    if (isset($actions['ForecastSchedule']['module']['admin']['aclaccess']) && ($actions['ForecastSchedule']['module']['admin']['aclaccess']==ACL_ALLOW_DEV ||
        $actions['ForecastSchedule']['module']['admin']['aclaccess']==ACL_ALLOW_ADMIN_DEV)) {
        $workflow_admin_modules['Forecasts'] = 'Forecasts';
    }
    foreach ($workflow_mod_list as $key=>$val) {
        if(!in_array($val, $workflow_admin_modules) && ($val!='iFrames' && $val!='Feeds' && $val!='Home' && $val!='Dashboard'
            && $val!='Calendar' && $val!='Activities' && $val!='Reports') &&
           ($user->isDeveloperForModule($key))) {
                $workflow_admin_modules[$key] = $val;
        }
    }
    $_SESSION['get_workflow_admin_modules_for_user'] = $workflow_admin_modules;
    return ($workflow_admin_modules);
}

// Check if user is admin for at least one module.
function is_admin_for_any_module($user) {
    if (!isset($user)){
        return false;
    }
    if($user->isAdmin()) {
        return true;
    }
    return false;
}


// Check if user is admin for a specific module.
function is_admin_for_module($user,$module) {
    if (!isset($user)) {
        return false;
    }
    if ($user->isAdmin()) {
        return true;
    }
    return false;
}


/**
 * Check if user id belongs to a system admin.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function is_admin($user) {
    if(empty($user)) {
        return false;
    }
    
	return $user->isAdmin();
}

/**
 * Return the display name for a theme if it exists.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 *
 * @deprecated use SugarThemeRegistry::get($theme)->name instead
 */
function get_theme_display($theme)
{
	return SugarThemeRegistry::get($theme)->name;
}

/**
 * Return an array of directory names.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 *
 * @deprecated use SugarThemeRegistry::availableThemes() instead.
 */
function get_themes()
{
    return SugarThemeRegistry::availableThemes();
}

/**
 * THIS FUNCTION IS DEPRECATED AND SHOULD NOT BE USED; USE get_select_options_with_id()
 * Create HTML to display select options in a dropdown list.  To be used inside
 * of a select statement in a form.
 * param $option_list - the array of strings to that contains the option list
 * param $selected - the string which contains the default value
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function get_select_options ($option_list, $selected) {
	return get_select_options_with_id($option_list, $selected);
}

/**
 * Create HTML to display select options in a dropdown list.  To be used inside
 * of a select statement in a form.   This method expects the option list to have keys and values.  The keys are the ids.  The values are the display strings.
 * param $option_list - the array of strings to that contains the option list
 * param $selected - the string which contains the default value
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function get_select_options_with_id ($option_list, $selected_key) {
	return get_select_options_with_id_separate_key($option_list, $option_list, $selected_key);
}


/**
 * Create HTML to display select options in a dropdown list.  To be used inside
 * of a select statement in a form.   This method expects the option list to have keys and values.  The keys are the ids.  The values are the display strings.
 * param $label_list - the array of strings to that contains the option list
 * param $key_list - the array of strings to that contains the values list
 * param $selected - the string which contains the default value
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function get_select_options_with_id_separate_key ($label_list, $key_list, $selected_key, $massupdate=false) {
	global $app_strings;
	$select_options = "";

	//for setting null selection values to human readable --None--
	$pattern = "/'0?'></";
	$replacement = "''>".$app_strings['LBL_NONE']."<";

	if (empty($key_list)) $key_list = array();
	//create the type dropdown domain and set the selected value if $opp value already exists
	foreach ($key_list as $option_key=>$option_value) {

		$selected_string = '';
		// the system is evaluating $selected_key == 0 || '' to true.  Be very careful when changing this.  Test all cases.
		// The bug was only happening with one of the users in the drop down.  It was being replaced by none.
		if (($option_key != '' && $selected_key == $option_key) || ($selected_key == '' && $option_key == '' && !$massupdate) || (is_array($selected_key) &&  in_array($option_key, $selected_key)))
		{
			$selected_string = 'selected ';
		}

		$html_value = $option_key;

		$select_options .= "\n<OPTION ".$selected_string."value='$html_value'>$label_list[$option_key]</OPTION>";
	}
	$select_options = preg_replace($pattern, $replacement, $select_options);
	return $select_options;
}


/**
 * Call this method instead of die().
 * Then we call the die method with the error message that is passed in.
 */
function sugar_die($error_message)
{
	global $focus;
	sugar_cleanup();
	die($error_message);
}


/**
 * Create javascript to clear values of all elements in a form.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function get_clear_form_js () {
	$the_script = <<<EOQ
<script type="text/javascript" language="JavaScript">
function clear_form(form) {
	var newLoc = 'index.php?action=' + form.action.value + '&module=' + form.module.value + '&query=true&clear_query=true';
	if(typeof(form.advanced) != 'undefined'){
		newLoc += '&advanced=' + form.advanced.value;
	}
	document.location.href= newLoc;
}
</script>
EOQ;

	return $the_script;
}

/**
 * Create javascript to set the cursor focus to specific field in a form
 * when the screen is rendered.  The field name is currently hardcoded into the
 * the function.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function get_set_focus_js () {
	//TODO Clint 5/20 - Make this function more generic so that it can take in the target form and field names as variables
	$the_script = <<<EOQ
<script type="text/javascript" language="JavaScript">
<!-- Begin
function set_focus() {
	if (document.forms.length > 0) {
		for (i = 0; i < document.forms.length; i++) {
			for (j = 0; j < document.forms[i].elements.length; j++) {
				var field = document.forms[i].elements[j];
				if ((field.type == "text" || field.type == "textarea" || field.type == "password") &&
						!field.disabled && (field.name == "first_name" || field.name == "name" || field.name == "user_name" || field.name=="document_name")) {
					field.focus();
                    if (field.type == "text") {
                        field.select();
                    }
					break;
	    		}
			}
      	}
   	}
}
//  End -->
</script>
EOQ;

	return $the_script;
}

/**
 * Very cool algorithm for sorting multi-dimensional arrays.  Found at http://us2.php.net/manual/en/function.array-multisort.php
 * Syntax: $new_array = array_csort($array [, 'col1' [, SORT_FLAG [, SORT_FLAG]]]...);
 * Explanation: $array is the array you want to sort, 'col1' is the name of the column
 * you want to sort, SORT_FLAGS are : SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING
 * you can repeat the 'col',FLAG,FLAG, as often you want, the highest prioritiy is given to
 * the first - so the array is sorted by the last given column first, then the one before ...
 * Example: $array = array_csort($array,'town','age',SORT_DESC,'name');
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function array_csort() {
	$args = func_get_args();
	$marray = array_shift($args);
	$i = 0;

	$msortline = "return(array_multisort(";
	foreach ($args as $arg) {
		$i++;
		if (is_string($arg)) {
			foreach ($marray as $row) {
				$sortarr[$i][] = $row[$arg];
			}
		} else {
			$sortarr[$i] = $arg;
		}
		$msortline .= "\$sortarr[".$i."],";
	}
	$msortline .= "\$marray));";

	eval($msortline);
	return $marray;
}

/**
 * Converts localized date format string to jscalendar format
 * Example: $array = array_csort($array,'town','age',SORT_DESC,'name');
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
function parse_calendardate($local_format) {
	preg_match('/\(?([^-]{1})[^-]*-([^-]{1})[^-]*-([^-]{1})[^-]*\)/', $local_format, $matches);
	$calendar_format = "%" . $matches[1] . "-%" . $matches[2] . "-%" . $matches[3];
	return str_replace(array("y", "ᅣ1�7", "a", "j"), array("Y", "Y", "Y", "d"), $calendar_format);
}





function translate($string, $mod='', $selectedValue=''){
	//$test_start = microtime();
	//static $mod_strings_results = array();
	if(!empty($mod)){
		global $current_language;
		//Bug 31275
		if(isset($_REQUEST['login_language'])){
		    $current_language = ($_REQUEST['login_language'] == $current_language)? $current_language : $_REQUEST['login_language'];
		}
		$mod_strings = return_module_language($current_language, $mod);

	}else{
		global $mod_strings;
	}

	$returnValue = '';
	global $app_strings, $app_list_strings;

	if(isset($mod_strings[$string]))
	$returnValue = $mod_strings[$string];
	else if(isset($app_strings[$string]))
	$returnValue = $app_strings[$string];
	else if(isset($app_list_strings[$string]))
	$returnValue = $app_list_strings[$string];
	else if(isset($app_list_strings['moduleList']) && isset($app_list_strings['moduleList'][$string]))
	$returnValue = $app_list_strings['moduleList'][$string];


	//$test_end = microtime();
	//
	//    $mod_strings_results[$mod] = microtime_diff($test_start,$test_end);
	//
	//    echo("translate results:");
	//    $total_time = 0;
	//    $total_strings = 0;
	//    foreach($mod_strings_results as $key=>$value)
	//    {
	//        echo("Module $key \t\t time $value \t\t<br>");
	//        $total_time += $value;
	//    }
	//
	//    echo("Total time: $total_time<br>");



	if(empty($returnValue)){
		return $string;
	}

	if(is_array($returnValue) && ! empty($selectedValue) && isset($returnValue[$selectedValue]) ){
		return $returnValue[$selectedValue];
	}

	return $returnValue;
}

function unTranslateNum($num) {
        static $dec_sep;
        static $num_grp_sep;
        global $current_user, $sugar_config;

        if($dec_sep == null) {
            $user_dec_sep = $current_user->getPreference('dec_sep');
            $dec_sep = (empty($user_dec_sep) ? $sugar_config['default_decimal_seperator'] : $user_dec_sep);
        }
        if($num_grp_sep == null) {
            $user_num_grp_sep = $current_user->getPreference('num_grp_sep');
            $num_grp_sep = (empty($user_num_grp_sep) ? $sugar_config['default_number_grouping_seperator'] : $user_num_grp_sep);
        }

        $num = preg_replace("'" . preg_quote($num_grp_sep) . "'", '', $num);
        $num = preg_replace("'" . preg_quote($dec_sep) . "'", '.', $num);
        return $num;

}

function add_http($url) {
	if(!preg_match("@://@i", $url)) {
		$scheme = "http";
		if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
			$scheme = 'https';
		}

		return "{$scheme}://{$url}";
	}

	return $url;
}

/**
 * returns a default array of XSS tags to clean
 * @return array
 */
function getDefaultXssTags() {
	$tmp = array(
	"applet" => "applet",
	"base" => "base",
	"embed" => "embed",
	"form" => "form",
	"frame" => "frame",
	"frameset" => "frameset",
	"iframe" => "iframe",
	"import" => "\?import",
	"layer" => "layer",
	"link" => "link",
	"object" => "object",
	"script" => "script",
	"xmp" => "xmp",
	);

	$ret = base64_encode(serialize($tmp));

	return $ret;
}

/**
 * Remove potential xss vectors from strings
 * @param string str String to search for XSS attack vectors
 * @param bool cleanImg Flag to allow <img> tags to survive - only used by InboundEmail for inline images.
 * @return string
 */
function remove_xss($str, $cleanImg=true)
{
    $potentials = clean_xss($str, $cleanImg);
    if(is_array($potentials) && !empty($potentials)) {
        foreach($potentials as $bad) {
            $str = str_replace($bad, "", $str);
        }
    }
    return $str;
}

/**
 * Detects typical XSS attack patterns
 * @param string str String to search for XSS attack vectors
 * @param bool cleanImg Flag to allow <img> tags to survive - only used by InboundEmail for inline images.
 * @return array Array of matches, empty on clean string
 */
function clean_xss($str, $cleanImg=true) {
	global $sugar_config;

	if(empty($sugar_config['email_xss']))
	$sugar_config['email_xss'] = getDefaultXssTags();

	$arr = unserialize(base64_decode($sugar_config['email_xss']));

	$regex = '';
	foreach($arr as $v) {
		if(!empty($regex)) {
			$regex .= "|";
		}
		$regex .= $v;
	}

	$tag_regex        = "#<({$regex})[^>]*>?#sim";

	// cn: bug 13079 - "on\w" matched too many non-events (cONTact, strONG, etc.)
	$jsEvents  = "onblur|onfocus|oncontextmenu|onresize|onscroll|onunload|ondblclick|onclick|";
	$jsEvents .= "onmouseup|onmouseover|onmousedown|onmouseenter|onmouseleave|onmousemove|onload|onchange|";
	$jsEvents .= "onreset|onselect|onsubmit|onkeydown|onkeypress|onkeyup|onabort|onerror|ondragdrop";

	$attribute_regex	= "#<.+({$jsEvents})[^=>]*=[^>]*>#sim";
	$javascript_regex	= '@<[^/>][^>]+(expression\(|j\W*a\W*v\W*a|v\W*b\W*s\W*c\W*r|&#|/\*|\*/)[^>]*>@sim';
	$imgsrc_regex		= '#<[^>]+src[^=]*=([^>]*?http(s)?://[^>]*)>#sim';
	$css_url			= '#url\(.*\.\w+\)#';


	$str = str_replace("\t", "", $str);

	$matches = array_merge(
	xss_check_pattern($tag_regex, $str),
	xss_check_pattern($javascript_regex, $str)
	);


    $jsMatches = xss_check_pattern($attribute_regex, $str);
    if(!empty($jsMatches)){
        preg_match_all($attribute_regex, $str, $newMatches, PREG_PATTERN_ORDER);
        if(!empty($newMatches[0][0])){
            $matches2 = array_merge(xss_check_pattern("#({$jsEvents})#sim", $newMatches[0][0]));
            $matches = array_merge($matches, $matches2);
        }
    }

	if($cleanImg) {
		$matches = array_merge($matches,
		xss_check_pattern($imgsrc_regex, $str)
		);
	}

	// cn: bug 13498 - custom white-list of allowed domains that vet remote images
	preg_match_all($css_url, $str, $cssUrlMatches, PREG_PATTERN_ORDER);

	if(isset($sugar_config['security_trusted_domains']) && !empty($sugar_config['security_trusted_domains']) && is_array($sugar_config['security_trusted_domains'])) {
		if(is_array($cssUrlMatches) && count($cssUrlMatches) > 0) {
			// normalize whitelist
			foreach($sugar_config['security_trusted_domains'] as $k => $v) {
				$sugar_config['security_trusted_domains'][$k] = strtolower($v);
			}

			foreach($cssUrlMatches[0] as $match) {
				$domain = strtolower(substr(strstr($match, "://"), 3));
				$baseUrl = substr($domain, 0, strpos($domain, "/"));

				if(!in_array($baseUrl, $sugar_config['security_trusted_domains'])) {
					$matches[] = $match;
				}
			}
		}
	} else {
		$matches = array_merge($matches, $cssUrlMatches[0]);
	}

	return $matches;
}

/**
 * Helper function used by clean_xss() to parse for known-bad vectors
 * @param string pattern Regex pattern to use
 * @param string str String to parse for badness
 * @return array
 */
function xss_check_pattern($pattern, $str) {
	preg_match_all($pattern, $str, $matches, PREG_PATTERN_ORDER);
	return $matches[1];
}

/**
 * Designed to take a string passed in the URL as a parameter and clean all "bad" data from it
 *
 * @param string $str
 * @param string $filter which corresponds to a regular expression to use; choices are:
 * 		"STANDARD" ( default )
 * 		"STANDARDSPACE"
 * 		"FILE"
 * 		"NUMBER"
 * 		"SQL_COLUMN_LIST"
 * 		"PATH_NO_URL"
 * 		"SAFED_GET"
 * 		"UNIFIED_SEARCH"
 * 		"AUTO_INCREMENT"
 * 		"ALPHANUM"
 * @param boolean $dieOnBadData true (default) if you want to die if bad data if found, false if not
 */
function clean_string($str, $filter = "STANDARD", $dieOnBadData = true) 
{
	global  $sugar_config;

	$filters = Array(
	"STANDARD"        => '#[^A-Z0-9\-_\.\@]#i',
	"STANDARDSPACE"   => '#[^A-Z0-9\-_\.\@\ ]#i',
	"FILE"            => '#[^A-Z0-9\-_\.]#i',
	"NUMBER"          => '#[^0-9\-]#i',
	"SQL_COLUMN_LIST" => '#[^A-Z0-9\(\),_\.]#i',
	"PATH_NO_URL"     => '#://#i',
	"SAFED_GET"		  => '#[^A-Z0-9\@\=\&\?\.\/\-_~]#i', /* range of allowed characters in a GET string */
	"UNIFIED_SEARCH"	=> "#[\\x00]#", /* cn: bug 3356 & 9236 - MBCS search strings */
	"AUTO_INCREMENT"	=> '#[^0-9\-,\ ]#i',
	"ALPHANUM"        => '#[^A-Z0-9\-]#i',
	);

	if (preg_match($filters[$filter], $str)) {
		if (isset($GLOBALS['log']) && is_object($GLOBALS['log'])) {
			$GLOBALS['log']->fatal("SECURITY: bad data passed in; string: {$str}");
		}
		if ( $dieOnBadData ) {
			die("Bad data passed in; <a href=\"{$sugar_config['site_url']}\">Return to Home</a>");
		}
		return false;
	}
	else {
		return $str;
	}
}

function clean_special_arguments() {
	if(isset($_SERVER['PHP_SELF'])) {
		if (!empty($_SERVER['PHP_SELF'])) clean_string($_SERVER['PHP_SELF'], 'SAFED_GET');
	}
	if (!empty($_REQUEST) && !empty($_REQUEST['login_theme'])) clean_string($_REQUEST['login_theme'], "STANDARD");
	if (!empty($_REQUEST) && !empty($_REQUEST['login_module'])) clean_string($_REQUEST['login_module'], "STANDARD");
	if (!empty($_REQUEST) && !empty($_REQUEST['login_action'])) clean_string($_REQUEST['login_action'], "STANDARD");
	if (!empty($_REQUEST) && !empty($_REQUEST['ck_login_theme_20'])) clean_string($_REQUEST['ck_login_theme_20'], "STANDARD");
	if (!empty($_SESSION) && !empty($_SESSION['authenticated_user_theme'])) clean_string($_SESSION['authenticated_user_theme'], "STANDARD");
	if (!empty($_REQUEST) && !empty($_REQUEST['module_name'])) clean_string($_REQUEST['module_name'], "STANDARD");
	if (!empty($_REQUEST) && !empty($_REQUEST['module'])) clean_string($_REQUEST['module'], "STANDARD");
	if (!empty($_POST) && !empty($_POST['parent_type'])) clean_string($_POST['parent_type'], "STANDARD");
	if (!empty($_REQUEST) && !empty($_REQUEST['mod_lang'])) clean_string($_REQUEST['mod_lang'], "STANDARD");
	if (!empty($_SESSION) && !empty($_SESSION['authenticated_user_language'])) clean_string($_SESSION['authenticated_user_language'], "STANDARD");
	if (!empty($_SESSION) && !empty($_SESSION['dyn_layout_file'])) clean_string($_SESSION['dyn_layout_file'], "PATH_NO_URL");
	if (!empty($_GET) && !empty($_GET['from'])) clean_string($_GET['from']);
	if (!empty($_GET) && !empty($_GET['gmto'])) clean_string($_GET['gmto'], "NUMBER");
	if (!empty($_GET) && !empty($_GET['case_number'])) clean_string($_GET['case_number'], "AUTO_INCREMENT");
	if (!empty($_GET) && !empty($_GET['bug_number'])) clean_string($_GET['bug_number'], "AUTO_INCREMENT");
	if (!empty($_GET) && !empty($_GET['quote_num'])) clean_string($_GET['quote_num'], "AUTO_INCREMENT");
	clean_superglobals('stamp', 'ALPHANUM'); // for vcr controls
	clean_superglobals('offset', 'ALPHANUM');
	clean_superglobals('return_action');
	clean_superglobals('return_module');
	return TRUE;
}

/**
 * cleans the given key in superglobals $_GET, $_POST, $_REQUEST
 */
function clean_superglobals($key, $filter = 'STANDARD') {
	if(isset($_GET[$key])) clean_string($_GET[$key], $filter);
	if(isset($_POST[$key])) clean_string($_POST[$key], $filter);
	if(isset($_REQUEST[$key])) clean_string($_REQUEST[$key], $filter);
}

function set_superglobals($key, $val){
	$_GET[$key] = $val;
	$_POST[$key] = $val;
	$_REQUEST[$key] = $val;
}

// Works in conjunction with clean_string() to defeat SQL injection, file inclusion attacks, and XSS
function clean_incoming_data() {
	global $sugar_config;

	if (get_magic_quotes_gpc() == 1) {
		$req  = array_map("preprocess_param", $_REQUEST);
		$post = array_map("preprocess_param", $_POST);
		$get  = array_map("preprocess_param", $_GET);
	} else {

		$req  = array_map("securexss", $_REQUEST);
		$post = array_map("securexss", $_POST);
		$get  = array_map("securexss", $_GET);
	}

	// PHP cannot stomp out superglobals reliably
	foreach($post as $k => $v) { $_POST[$k] = $v; }
	foreach($get  as $k => $v) { $_GET[$k] = $v; }
	foreach($req  as $k => $v) {
		 $_REQUEST[$k] = $v;
		 //ensure the keys are safe as well
		 securexsskey($k);
	}
	// Any additional variables that need to be cleaned should be added here
	if (isset($_REQUEST['login_theme'])) clean_string($_REQUEST['login_theme']);
	if (isset($_REQUEST['login_module'])) clean_string($_REQUEST['login_module']);
	if (isset($_REQUEST['login_action'])) clean_string($_REQUEST['login_action']);
	if (isset($_REQUEST['login_language'])) clean_string($_REQUEST['login_language']);
	if (isset($_REQUEST['action'])) clean_string($_REQUEST['action']);
	if (isset($_REQUEST['module'])) clean_string($_REQUEST['module']);
	if (isset($_REQUEST['record'])) clean_string($_REQUEST['record'], 'STANDARDSPACE');
	if (isset($_SESSION['authenticated_user_theme'])) clean_string($_SESSION['authenticated_user_theme']);
	if (isset($_SESSION['authenticated_user_language'])) clean_string($_SESSION['authenticated_user_language']);
	if (isset($_REQUEST['language'])) clean_string($_REQUEST['language']);
	if (isset($sugar_config['default_theme'])) clean_string($sugar_config['default_theme']);
	if (isset($_REQUEST['offset'])) clean_string($_REQUEST['offset']);
	if (isset($_REQUEST['stamp'])) clean_string($_REQUEST['stamp']);

	if(isset($_REQUEST['lvso'])){
			set_superglobals('lvso', (strtolower($_REQUEST['lvso']) === 'desc')?'desc':'asc');
	}
	// Clean "offset" and "order_by" parameters in URL
	foreach ($_REQUEST as $key => $val) {
		if (str_end($key, "_offset")) {
			clean_string($_REQUEST[$key], "ALPHANUM"); // keep this ALPHANUM for disable_count_query
			set_superglobals($key, $_REQUEST[$key]);
		}
		elseif (str_end($key, "_ORDER_BY")) {
			clean_string($_REQUEST[$key], "SQL_COLUMN_LIST");
			set_superglobals($key, $_REQUEST[$key]);
		}
	}


	return 0;
}

// Returns TRUE if $str begins with $begin
function str_begin($str, $begin) {
	return (substr($str, 0, strlen($begin)) == $begin);
}

// Returns TRUE if $str ends with $end
function str_end($str, $end) {
	return (substr($str, strlen($str) - strlen($end)) == $end);
}

function securexss($value) {
	if(is_array($value)){
    	$new = array();
        foreach($value as $key=>$val){
       		$new[$key] = securexss($val);
        }
        return $new;
    }
	static $xss_cleanup=  array('"' =>'&quot;', "'" =>  '&#039;' , '<' =>'&lt;' , '>'=>'&gt;');
	$value = preg_replace(array('/javascript:/i', '/\0/'), array('java script:', ''), $value);
	$value = preg_replace('/javascript:/i', 'java script:', $value);
	return str_replace(array_keys($xss_cleanup), array_values($xss_cleanup), $value);
}

function securexsskey($value, $die=true){
	global $sugar_config;
	$matches = array();
	preg_match("/[\'\"\<\>]/", $value, $matches);
	if(!empty($matches)){
		if($die){
			die("Bad data passed in; <a href=\"{$sugar_config['site_url']}\">Return to Home</a>");
		}else{
			unset($_REQUEST[$value]);
			unset($_POST[$value]);
			unset($_GET[$value]);
		}
	}
}

function preprocess_param($value){
	if(is_string($value)){
		if(get_magic_quotes_gpc() == 1){
			$value = stripslashes($value);
		}

		$value = securexss($value);
	}


	return $value;


}

function set_register_value($category, $name, $value){
    return sugar_cache_put("{$category}:{$name}", $value);
}

function get_register_value($category,$name){
    return sugar_cache_retrieve("{$category}:{$name}");
}

// this function cleans id's when being imported
function convert_id($string)
{
	return preg_replace_callback( '|[^A-Za-z0-9\-]|',
	create_function(
	// single quotes are essential here,
	// or alternative escape all $ as \$
	'$matches',
	'return ord($matches[0]);'
         ) ,$string);
}

/**
 * @deprecated use SugarTheme::getImage()
 */
function get_image($image,$other_attributes,$width="",$height="")
{
    return SugarThemeRegistry::current()->getImage(basename($image),
        $other_attributes,
        empty($width) ? null : $width,
        empty($height) ? null : $height
        );
}
/**
 * @deprecated use SugarTheme::getImageURL()
 */
function getImagePath($image_name)
{
    return SugarThemeRegistry::current()->getImageURL($image_name);
}

function getWebPath($relative_path){
	//if it has  a :// then it isn't a relative path
	if(substr_count($relative_path, '://') > 0) return $relative_path;
	if(defined('TEMPLATE_URL'))$relative_path = SugarTemplateUtilities::getWebPath($relative_path);
	return $relative_path;
}

function getJSPath($relative_path, $additional_attrs=''){
	if(defined('TEMPLATE_URL'))$relative_path = SugarTemplateUtilities::getWebPath($relative_path);
	if(empty($GLOBALS['sugar_config']['js_custom_version']))$GLOBALS['sugar_config']['js_custom_version'] = 1;
	$js_version_key = isset($GLOBALS['js_version_key'])?$GLOBALS['js_version_key']:'';
	$path = $relative_path . '?s=' . $js_version_key . '&c=' . $GLOBALS['sugar_config']['js_custom_version'] ;
	if ( inDeveloperMode() ) $path .= '&developerMode='.mt_rand();
	if(!empty($additonal_attrs)) $path .= '&' . $additional_attrs;
	return $path;
}

function getSWFPath($relative_path, $additional_params=''){
	$path = $relative_path;
	if (!empty($additional_params)){
		$path .= '?' . $additional_params;
	}
	if (defined('TEMPLATE_URL')){
		$path = TEMPLATE_URL . '/' . $path;
	}
	return $path;
}





function getSQLDate($date_str)
{
	if (preg_match('/^(\d{1,2})-(\d{1,2})-(\d{4})$/',$date_str,$match))
	{
		if ( strlen($match[2]) == 1)
		{
			$match[2] = "0".$match[2];
		}
		if ( strlen($match[1]) == 1)
		{
			$match[1] = "0".$match[1];
		}
		return "{$match[3]}-{$match[1]}-{$match[2]}";
	}
	else if (preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/',$date_str,$match))
	{
		if ( strlen($match[2]) == 1)
		{
			$match[2] = "0".$match[2];
		}
		if ( strlen($match[1]) == 1)
		{
			$match[1] = "0".$match[1];
		}
		return "{$match[3]}-{$match[1]}-{$match[2]}";
	}
	else
	{
		return "";
	}
}

function clone_history(&$db, $from_id,$to_id, $to_type)
{
	global $timedate;
	$old_note_id=null;
	$old_filename=null;
	require_once('include/upload_file.php');
	$tables = array('calls'=>'Call', 'meetings'=>'Meeting', 'notes'=>'Note', 'tasks'=>'Task');

	$location=array('Email'=>"modules/Emails/Email.php",
	'Call'=>"modules/Calls/Call.php",
	'Meeting'=>"modules/Meetings/Meeting.php",
	'Note'=>"modules/Notes/Note.php",
	'Tasks'=>"modules/Tasks/Task.php",
	);


	foreach($tables as $table=>$bean_class)
	{

		if (!class_exists($bean_class))
		{
			require_once($location[$bean_class]);
		}

		$bProcessingNotes=false;
		if ($table=='notes')
		{
			$bProcessingNotes=true;
		}
		$query = "SELECT id FROM $table WHERE parent_id='$from_id'";
		$results = $db->query($query);
		while($row = $db->fetchByAssoc($results))
		{
			//retrieve existing record.
			$bean= new $bean_class();
			$bean->retrieve($row['id']);
			//process for new instance.
			if ($bProcessingNotes)
			{
				$old_note_id=$row['id'];
				$old_filename=$bean->filename;
			}
			$bean->id=null;
			$bean->parent_id=$to_id;
			$bean->parent_type=$to_type;
			if ($to_type=='Contacts' and in_array('contact_id',$bean->column_fields))
			{
				$bean->contact_id=$to_id;
			}
			$bean->update_date_modified = false;
            $bean->update_modified_by = false;
            if(isset($bean->date_modified))
                $bean->date_modified = $timedate->to_db($bean->date_modified);
            if(isset($bean->date_entered))
                $bean->date_entered = $timedate->to_db($bean->date_entered);
			//save
			$new_id=$bean->save();

			//duplicate the file now. for notes.
			if ($bProcessingNotes && !empty($old_filename))
			{
				UploadFile::duplicate_file($old_note_id,$new_id,$old_filename);
			}
			//reset the values needed for attachment duplication.
			$old_note_id=null;
			$old_filename=null;
		}
	}
}

function values_to_keys($array)
{
	$new_array = array();
	if(!is_array($array))
	{
		return $new_array;
	}
	foreach($array as $arr){
		$new_array[$arr] = $arr;
	}
	return $new_array;
}

function clone_relationship(&$db, $tables = array(), $from_column, $from_id, $to_id)
{
	foreach($tables as $table)
	{

		if ($table == 'emails_beans') {
			$query = "SELECT * FROM $table WHERE $from_column='$from_id' and bean_module='Leads'";
		} else {
			$query = "SELECT * FROM $table WHERE $from_column='$from_id'";
		}
		$results = $db->query($query);
		while($row = $db->fetchByAssoc($results))
		{
			$query = "INSERT INTO $table ";
			$names = '';
			$values = '';
			$row[$from_column] = $to_id;
			$row['id'] = create_guid();
			if ($table=='emails_beans') {
				$row['bean_module'] =='Contacts';
			}

			foreach($row as $name=>$value)
			{

				if(empty($names))
				{
					$names .= $name;
					$values .= "'$value'";
				} else
				{
					$names .= ', '. $name;
					$values .= ", '$value'";
				}
			}
			$query .= "($names)	VALUES ($values)";
			$db->query($query);
		}
	}
}

function get_unlinked_email_query($type, $bean) {
    global $current_user;

    $return_array['select']='SELECT emails.id ';
    $return_array['from']='FROM emails ';
    $return_array['where']="";
	$return_array['join'] = " JOIN (select distinct email_id from emails_email_addr_rel eear

	join email_addr_bean_rel eabr on eabr.bean_id ='$bean->id' and eabr.bean_module = '$bean->module_dir' and
	eabr.email_address_id = eear.email_address_id and eabr.deleted=0
	where eear.deleted=0 and eear.email_id not in
	(select eb.email_id from emails_beans eb where eb.bean_module ='$bean->module_dir' and eb.bean_id = '$bean->id')
	) derivedemails on derivedemails.email_id = emails.id";
    $return_array['join_tables'][0] = '';

	if (isset($type) and isset($type['return_as_array']) and $type['return_as_array']==true) {
		return $return_array;
	}

	return $return_array['select'] . $return_array['from'] . $return_array['where'];
} // fn

/**
 * Check to see if the number is empty or non-zero
 * @param $value
 * @return boolean
 **/
function number_empty($value)
{
	return empty($value) && $value != '0';
}

function get_bean_select_array($add_blank=true, $bean_name, $display_columns, $where='', $order_by='', $blank_is_none=false)
{
	global $beanFiles;
	require_once($beanFiles[$bean_name]);
	$focus = new $bean_name();
	$user_array = array();
	$user_array = get_register_value('select_array',$bean_name. $display_columns. $where . $order_by);
	if(!$user_array)
	{

		$db = DBManagerFactory::getInstance();
		$temp_result = Array();
		$query = "SELECT {$focus->table_name}.id, {$display_columns} as display from {$focus->table_name} ";
		$query .= "where ";
		if ( $where != '')
		{
			$query .= $where." AND ";
		}
		
		$query .=  " {$focus->table_name}.deleted=0";

		if ( $order_by != '')
		{
			$query .= " order by {$focus->table_name}.{$order_by}";
		}

		$GLOBALS['log']->debug("get_user_array query: $query");
		$result = $db->query($query, true, "Error filling in user array: ");

		if ($add_blank==true){
			// Add in a blank row
			if($blank_is_none == true) { // set 'blank row' to "--None--"
				global $app_strings;
				$temp_result[''] = $app_strings['LBL_NONE'];
			} else {
				$temp_result[''] = '';
			}
		}

		// Get the id and the name.
		while($row = $db->fetchByAssoc($result))
		{
			$temp_result[$row['id']] = $row['display'];
		}

		$user_array = $temp_result;
		set_register_value('select_array',$bean_name. $display_columns. $where . $order_by,$temp_result);
	}

	return $user_array;

}
/**
 *
 *
 * @param unknown_type $listArray
 */
// function parse_list_modules
// searches a list for items in a user's allowed tabs and returns an array that removes unallowed tabs from list
function parse_list_modules(&$listArray)
{
	global $modListHeader;
	$returnArray = array();

	foreach($listArray as $optionName => $optionVal)
	{
		if(array_key_exists($optionName, $modListHeader))
		{
			$returnArray[$optionName] = $optionVal;
		}

		// special case for projects
		if(array_key_exists('Project', $modListHeader))
		{
			$returnArray['ProjectTask'] = $listArray['ProjectTask'];
		}
	}
	$acldenied = ACLController::disabledModuleList($listArray,false);
	foreach($acldenied as $denied){
		unset($returnArray[$denied]);
	}
	asort($returnArray);

	return $returnArray;
}

function display_notice($msg = false){
	global $error_notice;
	//no error notice - lets just display the error to the user
	if(!isset($error_notice)){
		echo '<br>'.$msg . '<br>';
	}else{
		$error_notice .= $msg . '<br>';
	}
}

/* checks if it is a number that atleast has the plus at the beggining
 */
function skype_formatted($number){
	//kbrill - BUG #15375
	if(isset($_REQUEST['action']) && $_REQUEST['action']=="Popup") {
		return false;
	} else {
		return substr($number, 0, 1) == '+' || substr($number, 0, 2) == '00' || substr($number, 0, 3) == '011';
	}
//	return substr($number, 0, 1) == '+' || substr($number, 0, 2) == '00' || substr($number, 0, 2) == '011';
}

function format_skype($number) {
    return preg_replace('/[^\+0-9]/','',$number);
}

function insert_charset_header() {
	header('Content-Type: text/html; charset=UTF-8');
}

function getCurrentURL()
{
	$href = "http:";
	if(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
	{
		$href = 'https:';
	}

	$href.= "//".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
	return $href;
}

function javascript_escape($str) {
	$new_str = '';

	for($i = 0; $i < strlen($str); $i++) {

		if(ord(substr($str, $i, 1))==10){
			$new_str .= '\n';
		}elseif(ord(substr($str, $i, 1))==13){
			$new_str .= '\r';
		}
		else {
			$new_str .= $str{$i};
		}
	}

	$new_str = str_replace("'", "\\'", $new_str);

	return $new_str;
}

function js_escape($str, $keep=true){
	$str = html_entity_decode(str_replace("\\", "", $str), ENT_QUOTES);

	if($keep){
		$str = javascript_escape($str);
	}
	else {
		$str = str_replace("'", " ", $str);
		$str = str_replace('"', " ", $str);
	}

	return $str;

	//end function js_escape
}

function br2nl($str) {
	$regex = "#<[^>]+br.+?>#i";
	preg_match_all($regex, $str, $matches);

	foreach($matches[0] as $match) {
		$str = str_replace($match, "<br>", $str);
	}

	$brs = array('<br>','<br/>', '<br />');
	$str = str_replace("\r\n", "\n", $str); // make from windows-returns, *nix-returns
	$str = str_replace("\n\r", "\n", $str); // make from windows-returns, *nix-returns
	$str = str_replace("\r", "\n", $str); // make from windows-returns, *nix-returns
	$str = str_ireplace($brs, "\n", $str); // to retrieve it

	return $str;
}

/**
 * Private helper function for displaying the contents of a given variable.
 * This function is only intended to be used for SugarCRM internal development.
 * The ppd stands for Pre Print Die.
 */
function _ppd($mixed)
{
}


/**
 * Private helper function for displaying the contents of a given variable in
 * the Logger. This function is only intended to be used for SugarCRM internal
 * development. The pp stands for Pre Print.
 * @param $mixed var to print_r()
 * @param $die boolean end script flow
 * @param $displayStackTrace also show stack trace
 */
function _ppl($mixed, $die=false, $displayStackTrace=false, $loglevel="fatal") {
}

/**
 * private helper function to quickly show the major, direct, field attributes of a given bean.
 * The ppf stands for Pre[formatted] Print Focus [object]
 * @param object bean The focus bean
 */
function _ppf($bean, $die=false) {
}



/**
 * Private helper function for displaying the contents of a given variable.
 * This function is only intended to be used for SugarCRM internal development.
 * The pp stands for Pre Print.
 */
function _pp($mixed)
{
}

/**
 * Private helper function for displaying the contents of a given variable.
 * This function is only intended to be used for SugarCRM internal development.
 * The pp stands for Pre Print.
 */
function _pstack_trace($mixed=NULL)
{
}

/**
 * Private helper function for displaying the contents of a given variable.
 * This function is only intended to be used for SugarCRM internal development.
 * The pp stands for Pre Print Trace.
 */
function _ppt($mixed, $textOnly=false)
{
}

/**
 * Private helper function for displaying the contents of a given variable.
 * This function is only intended to be used for SugarCRM internal development.
 * The pp stands for Pre Print Trace Die.
 */
function _pptd($mixed)
{
}

/**
 * Private helper function for decoding javascript UTF8
 * This function is only intended to be used for SugarCRM internal development.
 */
function decodeJavascriptUTF8($str) {
}

/**
 * Will check if a given PHP version string is supported (tested on this ver),
 * unsupported (results unknown), or invalid (something will break on this
 * ver).  Do not pass in any pararameter to default to a check against the
 * current environment's PHP version.
 *
 * @return 1 implies supported, 0 implies unsupported, -1 implies invalid
 */
function check_php_version($sys_php_version = '') {
	$sys_php_version = empty($sys_php_version) ? constant('PHP_VERSION') : $sys_php_version;
	// versions below $min_considered_php_version considered invalid by default,
	// versions equal to or above this ver will be considered depending
	// on the rules that follow
	$min_considered_php_version = '5.2.1';

	// only the supported versions,
	// should be mutually exclusive with $invalid_php_versions
	$supported_php_versions = array (
	'5.2.1', '5.2.2', '5.2.3', '5.2.4', '5.2.5', '5.2.6', '5.2.8', '5.3.0'
	);
	//Find out what Database the system is using.
	global $sugar_config;
	$dbType = '';
	if (isset($_REQUEST['setup_db_type'])) {
		$dbType = $_REQUEST['setup_db_type'];
	} else if (isset ($sugar_config['dbconfig']) && isset ($sugar_config['dbconfig']['db_type'])) {
		$dbType = $sugar_config['dbconfig']['db_type'];
	}

	// invalid versions above the $min_considered_php_version,
	// should be mutually exclusive with $supported_php_versions

	// SugarCRM prohibits install on PHP 5.2.7 on all platforms
	$invalid_php_versions = array('5.2.7');

	// default unsupported
	$retval = 0;

	// versions below $min_considered_php_version are invalid
	if(1 == version_compare($sys_php_version, $min_considered_php_version, '<')) {
		$retval = -1;
	}

	// supported version check overrides default unsupported
	foreach($supported_php_versions as $ver) {
		if(1 == version_compare($sys_php_version, $ver, 'eq') || strpos($sys_php_version,$ver) !== false) {
			$retval = 1;
			break;
		}
	}

	// invalid version check overrides default unsupported
	foreach($invalid_php_versions as $ver) {
		if(1 == version_compare($sys_php_version, $ver, 'eq') && strpos($sys_php_version,$ver) !== false) {
			$retval = -1;
			break;
		}
	}

    //allow a redhat distro to install, regardless of version.  We are assuming the redhat naming convention is followed
    //and the php version contains 'rh' characters
    if(strpos($sys_php_version, 'rh') !== false) {
        $retval = 1;
    }

	return $retval;
}

/**
 * Will check if a given IIS version string is supported (tested on this ver),
 * unsupported (results unknown), or invalid (something will break on this
 * ver).
 *
 * @return 1 implies supported, 0 implies unsupported, -1 implies invalid
 */
function check_iis_version($sys_iis_version = '') {

	$server_software = $_SERVER["SERVER_SOFTWARE"];
	$iis_version = '';
	if(strpos($server_software,'Microsoft-IIS') !== false && preg_match_all("/^.*\/(\d+\.?\d*)$/",  $server_software, $out))
	    $iis_version = $out[1][0];

    $sys_iis_version = empty($sys_iis_version) ? $iis_version : $sys_iis_version;

    // versions below $min_considered_iis_version considered invalid by default,
    // versions equal to or above this ver will be considered depending
    // on the rules that follow
    $min_considered_iis_version = '6.0';

    // only the supported versions,
    // should be mutually exclusive with $invalid_iis_versions
    $supported_iis_versions = array ('6.0', '7.0',);
    $unsupported_iis_versions = array();
    $invalid_iis_versions = array('5.0',);

    // default unsupported
    $retval = 0;

    // versions below $min_considered_iis_version are invalid
    if(1 == version_compare($sys_iis_version, $min_considered_iis_version, '<')) {
        $retval = -1;
    }

    // supported version check overrides default unsupported
    foreach($supported_iis_versions as $ver) {
        if(1 == version_compare($sys_iis_version, $ver, 'eq') || strpos($sys_iis_version,$ver) !== false) {
            $retval = 1;
            break;
        }
    }

    // unsupported version check overrides default unsupported
    foreach($unsupported_iis_versions as $ver) {
        if(1 == version_compare($sys_iis_version, $ver, 'eq') && strpos($sys_iis_version,$ver) !== false) {
            $retval = 0;
            break;
        }
    }

    // invalid version check overrides default unsupported
    foreach($invalid_iis_versions as $ver) {
        if(1 == version_compare($sys_iis_version, $ver, 'eq') && strpos($sys_iis_version,$ver) !== false) {
            $retval = -1;
            break;
        }
    }

    return $retval;
}

function pre_login_check(){
	global $action, $login_error;
	if(!empty($action)&& $action == 'Login'){

		if(!empty($login_error)){
			$login_error = htmlentities($login_error);
			$login_error = str_replace(array("&lt;pre&gt;","&lt;/pre&gt;","\r\n", "\n"), "<br>", $login_error);
			$_SESSION['login_error'] = $login_error;
			echo '<script>
						function set_focus() {}
						if(document.getElementById("post_error")) {
							document.getElementById("post_error").innerHTML="'. $login_error. '";
							document.getElementById("cant_login").value=1;
							document.getElementById("login_button").disabled = true;
							document.getElementById("user_name").disabled = true;
							//document.getElementById("user_password").disabled = true;
						}
						</script>';
		}
	}
}



function sugar_cleanup($exit = false) {
	static $called = false;
	if($called)return;
	$called = true;
	set_include_path(realpath(dirname(__FILE__) . '/..') . PATH_SEPARATOR . get_include_path());
	chdir(realpath(dirname(__FILE__) . '/..'));
	global $sugar_config;
	require_once('include/utils/LogicHook.php');
	LogicHook::initialize();
	$GLOBALS['logic_hook']->call_custom_logic('', 'server_round_trip');

	//added this check to avoid errors during install.
	if (empty($sugar_config['dbconfig'])) {
		if ($exit) exit; else return;
	}

    if (!class_exists('Tracker', true)) {
		require_once 'modules/Trackers/Tracker.php';
	}
	Tracker::logPage();
	// Now write the cached tracker_queries
	if(!empty($GLOBALS['savePreferencesToDB']) && $GLOBALS['savePreferencesToDB']) {
	    if ( isset($GLOBALS['current_user']) && $GLOBALS['current_user'] instanceOf User )
	        $GLOBALS['current_user']->savePreferencesToDB();
	}

	//check to see if this is not an ajax call AND the user preference error flag is set
	if(
		(isset($_SESSION['USER_PREFRENCE_ERRORS']) && $_SESSION['USER_PREFRENCE_ERRORS'])
		&& ($_REQUEST['action']!='modulelistmenu' && $_REQUEST['action']!='DynamicAction')
		&& (empty($_REQUEST['to_pdf']) || !$_REQUEST['to_pdf'] )
		&& (empty($_REQUEST['sugar_body_only']) || !$_REQUEST['sugar_body_only'] )

	){
		global $app_strings;
		//this is not an ajax call and the user preference error flag is set, so reset the flag and print js to flash message
		$err_mess = $app_strings['ERROR_USER_PREFS'];
		$_SESSION['USER_PREFRENCE_ERRORS'] = false;
		echo "
		<script>
			ajaxStatus.flashStatus('$err_mess',7000);
		</script>";

	}

	pre_login_check();
	if(class_exists('DBManagerFactory')) {
		$db = DBManagerFactory::getInstance();
		$db->disconnect();
		if($exit) {
			exit;
		}
	}
}

register_shutdown_function('sugar_cleanup');


/*
 check_logic_hook - checks to see if your custom logic is in the logic file
 if not, it will add it. If the file isn't built yet, it will create the file

 */
function check_logic_hook_file($module_name, $event, $action_array){
	require_once('include/utils/logic_utils.php');
	$add_logic = false;

	if(file_exists("custom/modules/$module_name/logic_hooks.php")){

		$hook_array = get_hook_array($module_name);

		if(check_existing_element($hook_array, $event, $action_array)==true){
			//the hook at hand is present, so do nothing
		} else {
			$add_logic = true;

            $logic_count = 0;
            if(!empty($hook_array[$event]))
            {
			    $logic_count = count($hook_array[$event]);
            }
            
			if($action_array[0]==""){
				$action_array[0] = $logic_count  + 1;
			}
			$hook_array[$event][] = $action_array;

		}
		//end if the file exists already
	} else {
		$add_logic = true;
		if($action_array[0]==""){
			$action_array[0] = 1;
		}
		$hook_array = array();
		$hook_array[$event][] = $action_array;
		//end if else file exists already
	}
	if($add_logic == true){

		//reorder array by element[0]
		//$hook_array = reorder_array($hook_array, $event);
		//!!!Finish this above TODO

		$new_contents = replace_or_add_logic_type($hook_array);
		write_logic_file($module_name, $new_contents);

		//end if add_element is true
	}

	//end function check_logic_hook_file
}

function remove_logic_hook($module_name, $event, $action_array) {
    require_once('include/utils/logic_utils.php');
	$add_logic = false;

	if(file_exists("custom/modules/".$module_name."/logic_hooks.php")){
        // The file exists, let's make sure the hook is there
		$hook_array = get_hook_array($module_name);

		if(check_existing_element($hook_array, $event, $action_array)==true){
            // The hook is there, time to take it out.

            foreach ( $hook_array[$event] as $i => $hook ) {
                // We don't do a full comparison below just in case the filename changes
                if ( $hook[0] == $action_array[0]
                     && $hook[1] == $action_array[1]
                     && $hook[3] == $action_array[3]
                     && $hook[4] == $action_array[4] ) {
                    unset($hook_array[$event][$i]);
                }
            }

            $new_contents = replace_or_add_logic_type($hook_array);
            write_logic_file($module_name, $new_contents);

        }
    }
}

function display_stack_trace($textOnly=false){

	$stack  = debug_backtrace();

	echo "\n\n display_stack_trace caller, file: " . $stack[0]['file']. ' line#: ' .$stack[0]['line'];

	if(!$textOnly)
	echo '<br>';

	$first = true;
	$out = '';

	foreach($stack as $item) {
		$file  = '';
		$class = '';
		$line  = '';
		$function  = '';

		if(isset($item['file']))
		$file = $item['file'];
		if(isset($item['class']))
		$class = $item['class'];
		if(isset($item['line']))
		$line = $item['line'];
		if(isset($item['function']))
		$function = $item['function'];

		if(!$first) {
			if(!$textOnly) {
				$out .= '<font color="black"><b>';
			}

			$out .= $file;

			if(!$textOnly) {
				$out .= '</b></font><font color="blue">';
			}

			$out .= "[L:{$line}]";

			if(!$textOnly) {
				$out .= '</font><font color="red">';
			}

			$out .= "({$class}:{$function})";

			if(!$textOnly) {
				$out .= '</font><br>';
			} else {
				$out .= "\n";
			}
		} else {
			$first = false;
		}
	}

	echo $out;
}

function StackTraceErrorHandler($errno, $errstr, $errfile,$errline, $errcontext) {
	$error_msg = " $errstr occured in <b>$errfile</b> on line $errline [" . date("Y-m-d H:i:s") . ']';
	$halt_script = true;
	switch($errno){
		case 2048: return; //depricated we have lots of these ignore them
		case E_USER_NOTICE:
		case E_NOTICE:
		    if ( error_reporting() & E_NOTICE ) {
		        $halt_script = false;
		        $type = 'Notice';
		    }
		    else
		        return;
			break;
		case E_USER_WARNING:
		case E_COMPILE_WARNING:
		case E_CORE_WARNING:
		case E_WARNING:

			$halt_script = false;
			$type = "Warning";
			break;

		case E_USER_ERROR:
		case E_COMPILE_ERROR:
		case E_CORE_ERROR:
		case E_ERROR:

			$type = "Fatal Error";
			break;

		case E_PARSE:

			$type = "Parse Error";
			break;

		default:
			//don't know what it is might not be so bad
			$halt_script = false;
			$type = "Unknown Error ($errno)";
			break;
	}
	$error_msg = '<b>'.$type.'</b>:' . $error_msg;
	echo $error_msg;
	display_stack_trace();
	if($halt_script){
		exit -1;
	}



}


if(isset($sugar_config['stack_trace_errors']) && $sugar_config['stack_trace_errors']){

	set_error_handler('StackTraceErrorHandler');
}
function get_sub_cookies($name){
	$cookies = array();
	if(isset($_COOKIE[$name])){
		$subs = explode('#', $_COOKIE[$name]);
		foreach($subs as $cookie){
			if(!empty($cookie)){
				$cookie = explode('=', $cookie);

				$cookies[$cookie[0]] = $cookie[1];
			}
		}
	}
	return $cookies;

}


function mark_delete_components($sub_object_array, $run_second_level=false, $sub_sub_array=""){

	if(!empty($sub_object_array)){

		foreach($sub_object_array as $sub_object){

			//run_second level is set to true if you need to remove sub-sub components
			if($run_second_level==true){

				mark_delete_components($sub_object->get_linked_beans($sub_sub_array['rel_field'],$sub_sub_array['rel_module']));

				//end if run_second_level is true
			}
			$sub_object->mark_deleted($sub_object->id);
			//end foreach sub component
		}
		//end if this is not empty
	}

	//end function mark_delete_components
}

/**
 * For translating the php.ini memory values into bytes.  e.g. input value of '8M' will return 8388608.
 */
function return_bytes($val)
{
	$val = trim($val);
	$last = strtolower($val{strlen($val)-1});

	switch($last)
	{
		// The 'G' modifier is available since PHP 5.1.0
		case 'g':
			$val *= 1024;
		case 'm':
			$val *= 1024;
		case 'k':
			$val *= 1024;
	}

	return $val;
}

/**
 * Adds the href HTML tags around any URL in the $string
 */
function url2html($string) {
	//
	$return_string = preg_replace('/(\w+:\/\/)(\S+)/', ' <a href="\\1\\2" target="_new"  style="font-weight: normal;">\\1\\2</a>', $string);
	return $return_string;
}
// End customization by Julian

/**
 * tries to determine whether the Host machine is a Windows machine
 */
function is_windows() {
    static $is_windows = null;
    if (!isset($is_windows)) {
        $is_windows = strtoupper(substr(PHP_OS, 0, 3)) == 'WIN';
    }
    return $is_windows;
}

/**
 * equivalent for windows filesystem for PHP's is_writable()
 * @param string file Full path to the file/dir
 * @return bool true if writable
 */
function is_writable_windows($file) {
	if($file{strlen($file)-1}=='/') {
		return is_writable_windows($file.uniqid(mt_rand()).'.tmp');
	}

	// the assumption here is that Windows has an inherited permissions scheme
	// any file that is a descendant of an unwritable directory will inherit
	// that property and will trigger a failure below.
	if(is_dir($file)) {
		return true;
	}

	$file = str_replace("/", '\\', $file);

	if(file_exists($file)) {
		if (!($f = @sugar_fopen($file, 'r+')))
		return false;
		fclose($f);
		return true;
	}

	if(!($f = @sugar_fopen($file, 'w')))
	return false;
	fclose($f);
	unlink($file);
	return true;
}


/**
 * best guesses Timezone based on webserver's TZ settings
 */
function lookupTimezone($userOffset = 0)
{
    return TimeDate::guessTimezone($userOffset);
}

function convert_module_to_singular($module_array){
	global $beanList;

	foreach($module_array as $key => $value){
		if(!empty($beanList[$value])) $module_array[$key] = $beanList[$value];

		if($value=="Cases") {
			$module_array[$key] = "Case";
		}
		if($key=="projecttask"){
			$module_array['ProjectTask'] = "Project Task";
			unset($module_array[$key]);
		}
	}

	return $module_array;

	//end function convert_module_to_singular
}

/*
 * Given the bean_name which may be plural or singular return the singular
 * bean_name. This is important when you need to include files.
 */
function get_singular_bean_name($bean_name){
	global $beanFiles, $beanList;
	if(array_key_exists($bean_name, $beanList)){
		return $beanList[$bean_name];
	}
	else{
		return $bean_name;
	}
}

function get_label($label_tag, $temp_module_strings){
	global $app_strings;
	if(!empty($temp_module_strings[$label_tag])){

		$label_name = $temp_module_strings[$label_tag];
	} else {
		if(!empty($app_strings[$label_tag])){
			$label_name = $app_strings[$label_tag];
		} else {
			$label_name = $label_tag;
		}
	}
	return $label_name;

	//end function get_label
}


function search_filter_rel_info(& $focus, $tar_rel_module, $relationship_name){

	$rel_list = array();

	foreach($focus->relationship_fields as $rel_key => $rel_value){
		if($rel_value == $relationship_name){
			$temp_bean = get_module_info($tar_rel_module);
	//		echo $focus->$rel_key;
			$temp_bean->retrieve($focus->$rel_key);
			if($temp_bean->id!=""){

				$rel_list[] = $temp_bean;
				return $rel_list;
			}
		}
	}

	foreach($focus->field_defs as $field_name => $field_def){
		//Check if the relationship_name matches a "relate" field
		if(!empty($field_def['type']) && $field_def['type'] == 'relate'
		&& !empty($field_def['id_name']) && !empty($focus->field_defs[$field_def['id_name']])
		&& !empty($focus->field_defs[$field_def['id_name']]['relationship'])
		&& $focus->field_defs[$field_def['id_name']]['relationship'] == $relationship_name)
		{
			$temp_bean = get_module_info($tar_rel_module);
		//	echo $focus->$field_def['id_name'];
			$temp_bean->retrieve($focus->$field_def['id_name']);
			if($temp_bean->id!=""){

				$rel_list[] = $temp_bean;
				return $rel_list;
			}
		//Check if the relationship_name matches a "link" in a relate field
		} else if(!empty($rel_value['link']) && !empty($rel_value['id_name']) && $rel_value['link'] == $relationship_name){
			$temp_bean = get_module_info($tar_rel_module);
		//	echo $focus->$rel_value['id_name'];
			$temp_bean->retrieve($focus->$rel_value['id_name']);
			if($temp_bean->id!=""){

				$rel_list[] = $temp_bean;
				return $rel_list;
			}
		}
	}

	// special case for unlisted parent-type relationships
	if($focus->parent_type == $tar_rel_module && !empty($focus->parent_id)) {
		$temp_bean = get_module_info($tar_rel_module);
		$temp_bean->retrieve($focus->parent_id);
		if($temp_bean->id!=""){
			$rel_list[] = $temp_bean;
			return $rel_list;
		}
	}

	return $rel_list;

	//end function search_filter_rel_info
}

function get_module_info($module_name){
	global $beanList;
	global $dictionary;

	//Get dictionary and focus data for module
	$vardef_name = $beanList[$module_name];

	if($vardef_name=="aCase"){
		$class_name = "Case";
	} else {
		$class_name = $vardef_name;
	}

	if(!file_exists('modules/'. $module_name . '/'.$class_name.'.php')){
		return;
	}

	include_once('modules/'. $module_name . '/'.$class_name.'.php');

	$module_bean = new $vardef_name();
	return $module_bean;
	//end function get_module_table
}

/**
 * In order to have one place to obtain the proper object name. aCase for example causes issues throughout the application.
 *
 * @param string $moduleName
 */
function get_valid_bean_name($module_name){
	global $beanList;

	$vardef_name = $beanList[$module_name];
	if($vardef_name=="aCase"){
		$bean_name = "Case";
	} else {
		$bean_name = $vardef_name;
	}
	return $bean_name;
}



function  checkAuthUserStatus(){

	//authUserStatus();
}


/**
 * This function returns an array of phpinfo() results that can be parsed and
 * used to figure out what version we run, what modules are compiled in, etc.
 * @param	$level			int		info level constant (1,2,4,8...64);
 * @return	$returnInfo		array	array of info about the PHP environment
 * @author	original by "code at adspeed dot com" Fron php.net
 * @author	customized for Sugar by Chris N.
 */
function getPhpInfo($level=-1) {
	/**	Name (constant)		Value	Description
		INFO_GENERAL		1		The configuration line, php.ini location, build date, Web Server, System and more.
		INFO_CREDITS		2		PHP Credits. See also phpcredits().
		INFO_CONFIGURATION	4		Current Local and Master values for PHP directives. See also ini_get().
		INFO_MODULES		8		Loaded modules and their respective settings. See also get_loaded_extensions().
		INFO_ENVIRONMENT	16		Environment Variable information that's also available in $_ENV.
		INFO_VARIABLES		32		Shows all predefined variables from EGPCS (Environment, GET, POST, Cookie, Server).
		INFO_LICENSE		64		PHP License information. See also the license FAQ.
		INFO_ALL			-1		Shows all of the above. This is the default value.
	 */
	ob_start();
	phpinfo($level);
	$phpinfo = ob_get_contents();
	ob_end_clean();

	$phpinfo	= strip_tags($phpinfo,'<h1><h2><th><td>');
	$phpinfo	= preg_replace('/<th[^>]*>([^<]+)<\/th>/',"<info>\\1</info>",$phpinfo);
	$phpinfo	= preg_replace('/<td[^>]*>([^<]+)<\/td>/',"<info>\\1</info>",$phpinfo);
	$parsedInfo	= preg_split('/(<h.?>[^<]+<\/h.>)/', $phpinfo, -1, PREG_SPLIT_DELIM_CAPTURE);
	$match		= '';
	$version	= '';
	$returnInfo	= array();

	if(preg_match('/<h1 class\=\"p\">PHP Version ([^<]+)<\/h1>/', $phpinfo, $version)) {
		$returnInfo['PHP Version'] = $version[1];
	}


	for ($i=1; $i<count($parsedInfo); $i++) {
		if (preg_match('/<h.>([^<]+)<\/h.>/', $parsedInfo[$i], $match)) {
			$vName = trim($match[1]);
			$parsedInfo2 = explode("\n",$parsedInfo[$i+1]);

			foreach ($parsedInfo2 AS $vOne) {
				$vPat	= '<info>([^<]+)<\/info>';
				$vPat3	= "/$vPat\s*$vPat\s*$vPat/";
				$vPat2	= "/$vPat\s*$vPat/";

				if (preg_match($vPat3,$vOne,$match)) { // 3cols
					$returnInfo[$vName][trim($match[1])] = array(trim($match[2]),trim($match[3]));
				} elseif (preg_match($vPat2,$vOne,$match)) { // 2cols
					$returnInfo[$vName][trim($match[1])] = trim($match[2]);
				}
			}
		} elseif(true) {

		}
	}

	return $returnInfo;
}

/**
 * This function will take a string that has tokens like {0}, {1} and will replace
 * those tokens with the args provided
 * @param	$format string to format
 * @param	$args args to replace
 * @return	$result a formatted string
 */
function string_format($format, $args){
	$result = $format;
	for($i = 0; $i < count($args); $i++){
		$result = str_replace('{'.$i.'}', $args[$i], $result);
	}
	return $result;
}

/**
 * Generate a string for displaying a unique identifier that is composed
 * of a system_id and number.  This is use to allow us to generate quote
 * numbers using a DB auto-increment key from offline clients and still
 * have the number be unique (since it is modified by the system_id.
 *
 * @param	$num of bean
 * @param	$system_id from system
 * @return	$result a formatted string
 */
function format_number_display($num, $system_id){
	global $sugar_config;
	if(isset($num) && !empty($num)){
		$num=unformat_number($num);
		if(isset($system_id) && $system_id == 1){
			return sprintf("%d", $num);
		}
		else{
			return sprintf("%d-%d", $num, $system_id);
		}
	}
}
function checkLoginUserStatus(){
}

/**
 * This function will take a number and system_id and format
 * @param	$url URL containing host to append port
 * @param	$port the port number - if '' is passed, no change to url
 * @return	$resulturl the new URL with the port appended to the host
 */
function appendPortToHost($url, $port)
{
	$resulturl = $url;

	// if no port, don't change the url
	if($port != '')
	{
		$split = explode("/", $url);
		//check if it starts with http, in case they didn't include that in url
		if(str_begin($url, 'http'))
		{
			//third index ($split[2]) will be the host
			$split[2] .= ":".$port;
		}
		else // otherwise assumed to start with host name
		{
			//first index ($split[0]) will be the host
			$split[0] .= ":".$port;
		}

		$resulturl = implode("/", $split);
	}

	return $resulturl;
}

/**
 * Singleton to return JSON object
 * @return	JSON object
 */
function getJSONobj() {
	static $json = null;
	if(!isset($json)) {
		require_once('include/JSON.php');
		$json = new JSON(JSON_LOOSE_TYPE);
	}
	return $json;
}

require_once('include/utils/db_utils.php');
//check to see if custom utils exists
if(file_exists('custom/include/custom_utils.php')){
	include_once('custom/include/custom_utils.php');
}


/**
 * Set default php.ini settings for entry points
 */
function setPhpIniSettings() {
	// zlib module
	// Bug 37579 - Comment out force enabling zlib.output_compression, since it can cause problems on certain hosts
	/*
    if(function_exists('gzclose') && headers_sent() == false) {
		ini_set('zlib.output_compression', 1);
	}
	*/
	// mbstring module
	//nsingh: breaks zip/unzip functionality. Commenting out 4/23/08

	/*if(function_exists('mb_strlen')) {
		ini_set('mbstring.func_overload', 7);
		ini_set('mbstring.internal_encoding', 'UTF-8');
	}*/


	// http://us3.php.net/manual/en/ref.pcre.php#ini.pcre.backtrack-limit
	// starting with 5.2.0, backtrack_limit breaks JSON decoding
	$backtrack_limit = ini_get('pcre.backtrack_limit');
	if(!empty($backtrack_limit)) {
		ini_set('pcre.backtrack_limit', '-1');
	}

	// mssql only
	if(ini_get("mssql.charset")) {
		ini_set('mssql.charset', "UTF-8");
	}
}

/**
 * like array_merge() but will handle array elements that are themselves arrays;
 * PHP's version just overwrites the element with the new one.
 *
 * @internal Note that this function deviates from the internal array_merge()
 *           functions in that it does does not treat numeric keys differently
 *           than string keys.  Additionally, it deviates from
 *           array_merge_recursive() by not creating an array when like values
 *           found.
 *
 * @param array gimp the array whose values will be overloaded
 * @param array dom the array whose values will pwn the gimp's
 * @return array beaten gimp
 */
function sugarArrayMerge($gimp, $dom) {
	if(is_array($gimp) && is_array($dom)) {
		foreach($dom as $domKey => $domVal) {
			if(array_key_exists($domKey, $gimp)) {
				if(is_array($domVal)) {
					$tempArr = array();
                    foreach ( $domVal as $domArrKey => $domArrVal )
                        $tempArr[$domArrKey] = $domArrVal;
                    foreach ( $gimp[$domKey] as $gimpArrKey => $gimpArrVal )
                        if ( !array_key_exists($gimpArrKey, $tempArr) )
                            $tempArr[$gimpArrKey] = $gimpArrVal;
                    $gimp[$domKey] = $tempArr;
				} else {
					$gimp[$domKey] = $domVal;
				}
			} else {
				$gimp[$domKey] = $domVal;
			}
		}
	}
    // if the passed value for gimp isn't an array, then return the $dom
    elseif(is_array($dom))
        return $dom;

	return $gimp;
}

/**
 * Similiar to sugarArrayMerge except arrays of N depth are merged.
 *
 * @param array gimp the array whose values will be overloaded
 * @param array dom the array whose values will pwn the gimp's
 * @return array beaten gimp
 */
function sugarArrayMergeRecursive($gimp, $dom) {
	if(is_array($gimp) && is_array($dom)) {
		foreach($dom as $domKey => $domVal) {
			if(array_key_exists($domKey, $gimp)) {
				if(is_array($domVal) && is_array($gimp[$domKey])) {
					$gimp[$domKey] = sugarArrayMergeRecursive($gimp[$domKey], $domVal);
				} else {
					$gimp[$domKey] = $domVal;
				}
			} else {
				$gimp[$domKey] = $domVal;
			}
		}
	}
    // if the passed value for gimp isn't an array, then return the $dom
    elseif(is_array($dom))
        return $dom;

	return $gimp;
}

/**
 * finds the correctly working versions of PHP-JSON
 * @return bool True if NOT found or WRONG version
 */
function returnPhpJsonStatus() {
	if(function_exists('json_encode')) {
		$phpInfo = getPhpInfo(8);
        return version_compare($phpInfo['json']['json version'], '1.1.1', '<');
	}
	return true; // not found
}


/**
 * getTrackerSubstring
 *
 * Returns a [number]-char or less string for the Tracker to display in the header
 * based on the tracker_max_display_length setting in config.php.  If not set,
 * or invalid length, then defaults to 15 for COM editions, 30 for others.
 *
 * @param string name field for a given Object
 * @return string [number]-char formatted string if length of string exceeds the max allowed
 */
function getTrackerSubstring($name) {
	static $max_tracker_item_length;

	//Trim the name
	$name = html_entity_decode($name, ENT_QUOTES, 'UTF-8');
	$strlen = function_exists('mb_strlen') ? mb_strlen($name) : strlen($name);

	global $sugar_config;

	if(!isset($max_tracker_item_length)) {
		if(isset($sugar_config['tracker_max_display_length'])) {
	      $max_tracker_item_length = (is_int($sugar_config['tracker_max_display_length']) && $sugar_config['tracker_max_display_length'] > 0 && $sugar_config['tracker_max_display_length'] < 50) ? $sugar_config['tracker_max_display_length'] : 15;
		} else {
	      $max_tracker_item_length = 15;
		}
	}

	if($strlen > $max_tracker_item_length) {
		$chopped = function_exists('mb_substr') ? mb_substr($name, 0, $max_tracker_item_length, "UTF-8") : substr($name, 0, $max_tracker_item_length, "UTF-8");
	} else {
		$chopped = $name;
	}

	return $chopped;
}
function generate_search_where ($field_list=array(),$values=array(),&$bean,$add_custom_fields=false,$module='') {
	$where_clauses= array();
	$like_char='%';
	$table_name=$bean->object_name;
	foreach ($field_list[$module] as $field=>$parms) {
		if(isset($values[$field]) && $values[$field] != "") {
			$operator='like';
			if (!empty($parms['operator'])) {
				$operator=$parms['operator'];
			}
			if (is_array($values[$field])) {
				$operator='in';
				$field_value='';
				foreach ($values[$field] as $key => $val) {
					if ($val != ' ' and $val != '') {
						if (!empty($field_value)) {
							$field_value.=',';
						}
						$field_value .= "'".$GLOBALS['db']->quote($val)."'";
					}
				}
			} else {
				$field_value=$GLOBALS['db']->quote($values[$field]);
			}
			//set db_fields array.
			if (!isset($parms['db_field']) )  {
				$parms['db_field'] = array($field);
			}
			if (isset($parms['my_items']) and $parms['my_items'] == true) {
				global $current_user;
				$field_value = $GLOBALS['db']->quote($current_user->id);
				$operator='=';
			}

			$where='';
			$itr=0;
			if ($field_value != '') {

				foreach ($parms['db_field'] as $db_field) {
					if (strstr($db_field,'.')===false) {
						$db_field=$bean->table_name.".".$db_field;
					}
					if ($GLOBALS['db']->dbType=='oci8' &&  isset($parms['query_type']) && $parms['query_type']=='case_insensitive') {
						$db_field='upper('.$db_field.")";
						$field_value=strtoupper($field_value);
					}

					$itr++;
					if (!empty($where)) {
						$where .= " OR ";
					}
					switch (strtolower($operator)) {
						case 'like' :
							$where .=  $db_field . " like '".$field_value.$like_char."'";
							break;
						case 'in':
							$where .=  $db_field . " in (".$field_value.')';
							break;
						case '=':
							$where .=  $db_field . " = '".$field_value ."'";
							break;
					}
				}
			}
			if (!empty($where)) {
				if ($itr>1) {
					array_push($where_clauses, '( '.$where.' )');
				} else {
					array_push($where_clauses, $where);
				}
			}
		}
	}
	if ($add_custom_fields) {
		require_once('modules/DynamicFields/DynamicField.php');
		$bean->setupCustomFields($module);
		$bean->custom_fields->setWhereClauses($where_clauses);
	}
	return $where_clauses;
}

function add_quotes($str) {
	return "'{$str}'";
}

/**
 * This function will rebuild the config file
 * @param	$sugar_config
 * @param	$sugar_version
 * @return	bool true if successful
 */
function rebuildConfigFile($sugar_config, $sugar_version) {
	// add defaults to missing values of in-memory sugar_config
	$sugar_config = sugarArrayMerge(get_sugar_config_defaults(), $sugar_config );
	// need to override version with default no matter what
	$sugar_config['sugar_version'] = $sugar_version;

	ksort( $sugar_config );

	if( write_array_to_file( "sugar_config", $sugar_config, "config.php" ) ){
		return true;
	}
	else {
		return false;
	}
}

/**
 * getJavascriptSiteURL
 * This function returns a URL for the client javascript calls to access
 * the site.  It uses $_SERVER['HTTP_REFERER'] in the event that Proxy servers
 * are used to access the site.  Thus, the hostname in the URL returned may
 * not always match that of $sugar_config['site_url'].  Basically, the
 * assumption is that however the user accessed the website is how they
 * will continue to with subsequent javascript requests.  If the variable
 * $_SERVER['HTTP_REFERER'] is not found then we default to old algorithm.
 * @return $site_url The url used to refer to the website
 */
function getJavascriptSiteURL() {
	global $sugar_config;
	if(!empty($_SERVER['HTTP_REFERER'])) {
		$url = parse_url($_SERVER['HTTP_REFERER']);
		$replacement_url = $url['scheme']."://".$url['host'];
		if(!empty($url['port']))
		$replacement_url .= ':'.$url['port'];
		$site_url = preg_replace('/^http[s]?\:\/\/[^\/]+/',$replacement_url,$sugar_config['site_url']);
	} else {
		$site_url = preg_replace('/^http(s)?\:\/\/[^\/]+/',"http$1://".$_SERVER['HTTP_HOST'],$sugar_config['site_url']);
		if(!empty($_SERVER['SERVER_PORT']) &&$_SERVER['SERVER_PORT'] == '443') {
			$site_url = preg_replace('/^http\:/','https:',$site_url);
		}
	}
	$GLOBALS['log']->debug("getJavascriptSiteURL(), site_url=".  $site_url);
	return $site_url;
}

// works nicely with array_map() -- can be used to wrap single quotes around each element in an array
function add_squotes($str) {
	return "'" . $str . "'";
}


// recursive function to count the number of levels within an array
function array_depth($array, $depth_count=-1, $depth_array=array()){
	$depth_count++;
	if (is_array($array)){
		foreach ($array as $key => $value){
			$depth_array[] = array_depth($value, $depth_count);
		}
	}
	else{
		return $depth_count;
	}
	foreach ($depth_array as $value){
		$depth_count = $value > $depth_count ? $value : $depth_count;
	}
	return $depth_count;
}

/**
 * Creates a new Group User
 * @param string $name Name of Group User
 * @return string GUID of new Group User
 */
function createGroupUser($name) {


	$group = new User();
	$group->user_name	= $name;
	$group->last_name	= $name;
	$group->is_group	= 1;
	$group->deleted		= 0;
	$group->status		= 'Active'; // cn: bug 6711
	$group->setPreference('timezone', TimeDate::userTimezone());
	$group->save();

	return $group->id;
}

/*
 * Helper function to locate an icon file given only a name
 * Searches through the various paths for the file
 * @param string iconFileName   The filename of the icon
 * @return string Relative pathname of the located icon, or '' if not found
 */

function _getIcon($iconFileName)
{
    $iconPath = SugarThemeRegistry::current()->getImageURL("icon_{$iconFileName}.gif");
    //First try un-ucfirst-ing the icon name
    if ( empty($iconPath) )
        $iconPath = SugarThemeRegistry::current()->getImageURL(
            "icon_" . strtolower(substr($iconFileName,0,1)).substr($iconFileName,1) . ".gif");
    //Next try removing the icon prefix
    if ( empty($iconPath) )
        $iconPath = SugarThemeRegistry::current()->getImageURL("{$iconFileName}.gif");

  	return $iconPath;
}
/**
 * Function to grab the correct icon image for Studio
 * @param string $iconFileName Name of the icon file
 * @param string $altfilename Name of a fallback icon file (displayed if the imagefilename doesn't exist)
 * @param string $width Width of image
 * @param string $height Height of image
 * @param string $align Alignment of image
 * @return string $string <img> tag with corresponding image
 */

function getStudioIcon($iconFileName='', $altFileName='', $width='48', $height='48', $align='baseline' )
{
	global $app_strings, $theme;

    $iconPath = _getIcon($iconFileName);
 	if(empty($iconPath)){
 	    $iconPath = _getIcon($altFileName);
 	    if (empty($iconPath))
 	    {
            return $app_strings['LBL_NO_IMAGE'];
 	    }
 	}
	return '<img border="0" src="'.$iconPath.'" width="'.$width.'" height="'.$height.'" align="'.$align.'">';
}

/**
 * Function to grab the correct icon image for Dashlets Dialog
 * @param string $filename Location of the icon file
 * @param string $module Name of the module to fall back onto if file does not exist
 * @param string $width Width of image
 * @param string $height Height of image
 * @param string $align Alignment of image
 * @return string $string <img> tag with corresponding image
 */

function get_dashlets_dialog_icon($module='', $width='32', $height='32', $align='absmiddle'){
	global $app_strings, $theme;
 	$icon_path = _getIcon($module . "_32");
 	if (empty($icon_path))
 	{
 		$icon_path = _getIcon($module);
 	}
 	if(empty($icon_path)){
 		$icon = $app_strings['LBL_NO_IMAGE'];
 	}
	else{
		$icon = '<img border="0" src="'.$icon_path.'" width="'.$width.'" height="'.$height.'" align="'.$align.'">';
	}
	return $icon;
}

// works nicely to change UTF8 strings that are html entities - good for PDF conversions
function html_entity_decode_utf8($string)
{
    static $trans_tbl;
    // replace numeric entities
    //php will have issues with numbers with leading zeros, so do not include them in what we send to code2utf.
    $string = preg_replace('~&#x0*([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
    $string = preg_replace('~&#0*([0-9]+);~e', 'code2utf(\\1)', $string);
    // replace literal entities
    if (!isset($trans_tbl))
    {
        $trans_tbl = array();
        foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
            $trans_tbl[$key] = utf8_encode($val);
    }
    return strtr($string, $trans_tbl);
}

// Returns the utf string corresponding to the unicode value
function code2utf($num)
{
    if ($num < 128) return chr($num);
    if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
    if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
    if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
    return '';
}

function str_split_php4($string, $length = 1) {
	$string_length = strlen($string);
	$return = array();
	$cursor = 0;
	if ($length > $string_length) {
		// use the string_length as the string is shorter than the length
		$length = $string_length;
	}
	for ($cursor = 0; $cursor < $string_length; $cursor = $cursor + $length) {
		$return[] = substr($string, $cursor, $length);
	}
	return $return;
}

if (version_compare(phpversion(), '5.0.0', '<')) {
	function str_split($string, $length = 1) {
		return str_split_php4($string, $length);
	}
}

/*
 * Invoked when connected to mssql. checks if we have freetds version of mssql library.
 * the response is put into a global variable.
 */
function is_freetds() {

	$ret=false;
	if (isset($GLOBALS['mssql_library_version'])) {
		if ($GLOBALS['mssql_library_version']=='freetds') {
			$ret=true;
		} else {
			$ret=false;
		}
	} else {
		ob_start();
		phpinfo();
		$info=ob_get_contents();
		ob_end_clean();

		if (strpos($info,'FreeTDS') !== false) {
			$GLOBALS['mssql_library_version']='freetds';
			$ret=true;
		} else {
			$GLOBALS['mssql_library_version']='regular';
			$ret=false;
		}
	}
	return $ret;
}

/*
 *  stripos - Find position of first occurrence of a case-insensitive string
 *
 *  The function is being defined for systems with PHP version < 5.
 *
 */
if (!function_exists("stripos")){
	function stripos($haystack,$needle,$offset=0){
      return strpos(strtolower($haystack),strtolower($needle),$offset);
	}
}

/**
 * Chart dashlet helper function that returns the correct CSS file, dependent on the current theme.
 *
 * @todo this won't work completely right until we impliment css compression and combination
 *       for now, we'll just include the last css file found.
 *
 * @return chart.css file to use
 */
function chartStyle()
{
    return SugarThemeRegistry::current()->getCSSURL('chart.css');
}

/**
 * Chart dashlet helper functions that returns the correct XML color file for charts,
 * dependent on the current theme.
 *
 * @return sugarColors.xml to use
 */
function chartColors()
{
	if (SugarThemeRegistry::current()->getCSSURL('sugarColors.xml')=='')
	return SugarThemeRegistry::current()->getImageURL('sugarColors.xml');
    return SugarThemeRegistry::current()->getCSSURL('sugarColors.xml');
}
/* End Chart Dashlet helper functions */

/**
 * This function is designed to set up the php enviroment
 * for AJAX requests.
 */

function ajaxInit() {
	ini_set('display_errors', 'false');
}

/**
 * Returns an absolute path from the given path, determining if it is relative or absolute
 *
 * @param  string $path
 * @return string
 */
function getAbsolutePath(
    $path,
    $currentServer = false
    )
{
    $path = trim($path);

    // try to match absolute paths like \\server\share, /directory or c:\
    if ( ( substr($path,0,2) == '\\\\' )
            || ( $path[0] == '/' )
            || preg_match('/^[A-z]:/i',$path)
            || $currentServer )
        return $path;

    return getcwd().'/'.$path;
}

/**
 * Returns the bean object of the given module
 *
 * @deprecated use SugarModule::loadBean() instead
 * @param  string $module
 * @return object
 */
function loadBean(
    $module
    )
{
    return SugarModule::get($module)->loadBean();
}


/**
 * Returns true if the application is being accessed on a touch screen interface ( like an iPad )
 */
function isTouchScreen()
{
    $ua = empty($_SERVER['HTTP_USER_AGENT']) ? "undefined" : strtolower($_SERVER['HTTP_USER_AGENT']);

    // first check if we have forced use of the touch enhanced interface
    if ( isset($_COOKIE['touchscreen']) && $_COOKIE['touchscreen'] == '1' ) {
        return true;
    }

    // next check if we should use the touch interface with our device
    if ( strpos($ua, 'ipad') !== false ) {
        return true;
    }

    return false;
}

/**
 * Returns the shortcut keys to access the shortcut links.  Shortcut
 * keys vary depending on browser versions and operating systems.
 * @return String value of the shortcut keys
 */
function get_alt_hot_key() {
	$ua = '';
    if ( isset($_SERVER['HTTP_USER_AGENT']) )
        $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
	$isMac = strpos($ua, 'mac') !== false;
	$isLinux = strpos($ua, 'linux') !== false;

	if(!$isMac && !$isLinux && strpos($ua, 'mozilla') !== false) {
	   if(preg_match('/firefox\/(\d)?\./', $ua, $matches)) {
	   	  return $matches[1] < 2 ? 'Alt+' : 'Alt+Shift+';
	   }
	}
	return $isMac ? 'Ctrl+' : 'Alt+';
}

function can_start_session(){
	if(!empty($_GET['PHPSESSID'])) {
	   return true;
	}
	$session_id = session_id();
	return empty($session_id) ? true : false;
}

function load_link_class($properties){
	$class = 'Link';
	if(!empty($properties['link_class']) && !empty($properties['link_file'])){
    	require_once($properties['link_file']);
    	$class = $properties['link_class'];
    }
    return $class;
}


function inDeveloperMode()
{
    return isset($GLOBALS['sugar_config']['developerMode']) && $GLOBALS['sugar_config']['developerMode'];
}

/**
 * Filter the protocol list for inbound email accounts.
 *
 * @param array $protocol
 */
function filterInboundEmailPopSelection($protocol)
{
    if ( !isset($GLOBALS['sugar_config']['allow_pop_inbound']) || ! $GLOBALS['sugar_config']['allow_pop_inbound'] )
    {
        if( isset($protocol['pop3']) )
			unset($protocol['pop3']);
    }
    else
        $protocol['pop3'] = 'POP3';

    return $protocol;
}

/**
 * The function is used because currently we are not supporting mbstring.func_overload
 * For some user using mssql without FreeTDS, they may store multibyte charaters in varchar using latin_general collation. It cannot store so many mutilbyte characters, so we need to use strlen.
 * The varchar in MySQL, Orcale, and nvarchar in FreeTDS, we can store $length mutilbyte charaters in it. we need mb_substr to keep more info.
* @returns the substred strings.
 */
function sugar_substr($string, $length, $charset='UTF-8') {
    if($GLOBALS['db']->dbType == 'mssql' && empty($GLOBALS['db']->isFreeTDS)) {
        if(strlen($string) > $length) {
            $string = trim(substr(trim($string),0,$length));
        }
    }
    else {
        if(mb_strlen($string,$charset) > $length) {
            $string = trim(mb_substr(trim($string),0,$length,$charset));
        }
    }
    return $string;
}

/**
 * The function is used because on FastCGI enviroment, the ucfirst(Chinese Characters) will produce bad charcters.
 * This will work even without setting the mbstring.*encoding
 */
function sugar_ucfirst($string, $charset='UTF-8') {
    return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset) . mb_substr($string, 1, mb_strlen($string), $charset);
}


/**
 *
 */
function unencodeMultienum($string) {
	if (is_array($string))
	{
	   return $string;
	}
	if (substr($string, 0 ,1) == "^" && substr($string, -1) == "^") {
          $string = substr(substr($string, 1), 0, strlen($string) -2);
	}

	return explode('^,^', $string);
}

function encodeMultienumValue($arr) {
    if (!is_array($arr))
        return $arr;

    if (empty($arr))
        return "";

	$string = "^" . implode('^,^', $arr) . "^";

    return $string;
}

/**
 * create_export_query is used for export and massupdate
 * We haven't handle the these fields: $field['type'] == 'relate' && isset($field['link']
 * This function will correct the where clause and output necessary join condition for them
 * @param $module: the module name
 * @param $searchFields: searchFields which is got after $searchForm->populateFromArray()
 * @param $where: where clauses
 * @return $ret_array['where']: corrected where clause
 * @return $ret_array['join']: extra join condition
 */
function create_export_query_relate_link_patch($module, $searchFields, $where){
	if(file_exists('modules/'.$module.'/SearchForm.html')){
		$ret_array['where'] = $where;
		return $ret_array;
	}
	$seed = loadBean($module);
    foreach($seed->field_defs as $name=>$field)
	{

		if( $field['type'] == 'relate' && isset($field['link']) && !empty($searchFields[$name]['value']) ){
			$seed->load_relationship($field['link']);
			$params = array();
			if(empty($join_type))
			{
				$params['join_type'] = ' LEFT JOIN ';
			}
			else
			{
				$params['join_type'] = $join_type;
			}
			if(isset($data['join_name']))
			{
				$params['join_table_alias'] = $field['join_name'];
			}
			else
			{
				$params['join_table_alias']	= 'join_'.$field['name'];

			}
			if(isset($data['join_link_name']))
			{
				$params['join_table_link_alias'] = $field['join_link_name'];
			}
			else
			{
				$params['join_table_link_alias'] = 'join_link_'.$field['name'];
			}
			$join = $seed->$field['link']->getJoin($params, true);
			$join_table_alias = 'join_'.$field['name'];
			if(isset($field['db_concat_fields'])){
				$db_field = db_concat($join_table_alias, $field['db_concat_fields']);
				$where = preg_replace('/'.$field['name'].'/', $db_field, $where);
			}else{
				$where = preg_replace('/(^|[\s(])' . $field['name'] . '/', '${1}' . $join_table_alias . '.'.$field['rname'], $where);
			}
		}
	}
	$ret_array = array('where'=>$where, 'join'=>$join['join']);
	return $ret_array;
}

/**
  * We need to clear all the js cache files, including the js language files  in serval places in MB. So I extract them into a util function here.
  * @Depends on QuickRepairAndRebuild.php
  * @Relate bug 30642  ,23177
  */
function clearAllJsAndJsLangFilesWithoutOutput(){
		global $current_language , $mod_strings;
		$MBmodStrings = $mod_strings;
        $mod_strings = return_module_language ( $current_language, 'Administration' ) ;
        include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
        $repair = new RepairAndClear();
        $repair->module_list = array();
		$repair->show_output = false;
		$repair->clearJsLangFiles();
		$repair->clearJsFiles();
		$mod_strings = $MBmodStrings;
}

/**
 * This function will allow you to get a variable value from query string
 */
function getVariableFromQueryString($variable, $string){
	$matches = array();
	$number = preg_match("/{$variable}=([a-zA-Z0-9_-]+)[&]?/", $string, $matches);
	if($number){
		return $matches[1];
	}
	else{
		return false;
	}
}

/**
 * should_hide_iframes
 * This is a helper method to determine whether or not to show iframes (My Sites) related
 * information in the application.
 *
 * @return boolean flag indicating whether or not iframes module should be hidden
 */
function should_hide_iframes() {
   //Remove the MySites module
   if(file_exists('modules/iFrames/iFrame.php')) {
        if(!class_exists("iFrame")) {
                require_once('modules/iFrames/iFrame.php');
        }
        return false;
   }
   return true;
}

/**
 * Given a version such as 5.5.0RC1 return RC. If we have a version such as: 5.5 then return GA
 *
 * @param string $version
 * @return string RC, BETA, GA
 */
function getVersionStatus($version){
	if(preg_match('/^[\d\.]+?([a-zA-Z]+?)[\d]*?$/si', $version, $matches)) {
		return strtoupper($matches[1]);
	}else{
		return 'GA';
	}
}

/**
 * Return the numeric portion of a version. For example if passed 5.5.0RC1 then return 5.5. If given
 * 5.5.1RC1 then return 5.5.1
 *
 * @param string $version
 * @return version
 */
function getMajorMinorVersion($version){
	if(preg_match('/^([\d\.]+).*$/si', $version, $matches2)){
		$version = $matches2[1];
		$arr = explode('.', $version);
		if(count($arr) > 2){
			if($arr[2] == '0'){
				$version = substr($version, 0, 3);
			}
		}
	}
	return $version;
}

/**
 * Return string composed of seconds & microseconds of current time, without dots
 * @return string
 */
function sugar_microtime()
{
	$now = explode(' ', microtime());
	$unique_id = $now[1].str_replace('.', '', $now[0]);
	return $unique_id;
}

/**
 * Extract urls from a piece of text
 * @param  $string
 * @return array of urls found in $string
 */
function getUrls($string)
{
	$lines = explode("<br>", trim($string));
	$urls = array();
	foreach($lines as $line){
    	$regex = '/http?\:\/\/[^\" ]+/i';
    	preg_match_all($regex, $line, $matches);
    	foreach($matches[0] as $match){
    		$urls[] = $match;
    	}
	}
    return $urls;
}


/**
 * Sanitize image file from hostile content
 * @param string $path Image file
 * @param bool $jpeg  Accept only JPEGs?
 */
function verify_image_file($path, $jpeg = false)
{
	if(function_exists('imagepng') && function_exists('imagejpeg') && function_exists('imagecreatefromstring')) {
        $img = imagecreatefromstring(file_get_contents($path));
    	if(!$img) {
    	    return false;
    	}
    	$img_size = getimagesize($path);
		$filetype = $img_size['mime'];
		//if filetype is jpeg or if we are only allowing jpegs, create jpg image
        if($filetype == "image/jpeg" || $jpeg) {
            if(imagejpeg($img, $path)) {
                return true;
            }
        } elseif ($filetype == "image/png") { // else if the filetype is png, create png
        	imagealphablending($img, true);
        	imagesavealpha($img, true);
    	    if(imagepng($img, $path)) {
                return true;
    	    }
        } else {
        	return false;	
        }
	} else {
	    // check image manually
	    $fp = fopen($path, "r");
	    if(!$fp) return false;
	    $data = fread($fp, 4096);
	    fclose($fp);
	    if(preg_match("/<(html|!doctype|script|body|head|plaintext|table|img |pre(>| )|frameset|iframe|object|link|base|style|font|applet|meta|center|form|isindex)/i",
	         $data, $m)) {
	        $GLOBALS['log']->info("Found {$m[0]} in $path, not allowing upload");
	        return false;
	    }
	    return true;
	}
	return false;
}

/**
 * Verify uploaded image
 * Verifies that image has proper extension, MIME type and doesn't contain hostile contant
 * @param string $path  Image path
 * @param bool $jpeg_only  Accept only JPEGs?
 */
function verify_uploaded_image($path, $jpeg_only = false)
{
    $supportedExtensions = array('jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg');
    if(!$jpeg_only) {
        $supportedExtensions['png'] = 'image/png';
    }

    if(!file_exists($path) || !is_file($path)) {
	    return false;
	}

	$img_size = getimagesize($path);
	$filetype = $img_size['mime'];
	$ext = end(explode(".", $path));
	if(substr_count('..', $path) > 0 || $ext === $path || !in_array(strtolower($ext), array_keys($supportedExtensions)) ||
	    !in_array($filetype, array_values($supportedExtensions))) {
	        return false;
	}
    return verify_image_file($path, $jpeg_only);
}

/**
 * @param $input - the input string to sanitize
 * @param int $quotes - use quotes
 * @param string $charset - the default charset
 * @param bool $remove - strip tags or not
 * @return string - the sanitized string
 */
function sanitize($input, $quotes = ENT_QUOTES, $charset = 'UTF-8', $remove = false)
{
    return htmlentities($input, $quotes, $charset);
}
Back to Top