This file is indexed.

/usr/share/phpgacl/gacl_api.class.php is in phpgacl 3.3.7-7.3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
<?php
/**
 * phpGACL - Generic Access Control List
 * Copyright (C) 2002,2003 Mike Benoit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For questions, help, comments, discussion, etc., please join the
 * phpGACL mailing list. http://sourceforge.net/mail/?group_id=57103
 *
 * You may contact the author of phpGACL by e-mail at:
 * ipso@snappymail.ca
 *
 * The latest version of phpGACL can be obtained from:
 * http://phpgacl.sourceforge.net/
 *
 * @package phpGACL
 *
 */

/*
 *
 * For examples, see example.php or the Administration interface,
 * as it makes use of nearly every API Call.
 *
 */
/**
 * gacl_api Extended API Class
 *
 * Class gacl_api should be used for applications that must interface directly with
 * phpGACL's data structures, objects, and rules.
 *
 * @package phpGACL
 * @author Mike Benoit <ipso@snappymail.ca>
 *
 */

class gacl_api extends gacl {

	/*
	 *
	 * Misc helper functions.
	 *
	 */

	/**
	 * showarray()
	 *
	 * Dump all contents of an array in HTML (kinda)
	 *
	 * @param array
	 *
	 */
	function showarray($array) {
		echo "<br><pre>\n";
		var_dump($array);
		echo "</pre><br>\n";
	}

	/**
	 * count_all()
	 *
	 * Recursively counts elements in an array and sub-arrays.
	 *
	 * This is different from count($arg, COUNT_RECURSIVE)
	 * in PHP >= 4.2.0, which includes sub-arrays in the count.
	 *
	 * @return int The returned count is a count of all scalar elements found.
	 *
	 * @param array Array to count
	 */
	function count_all($arg = NULL) {
		switch (TRUE) {
			case is_scalar($arg):
			case is_object($arg):
				// single object
				return 1;
			case is_array($arg):
				// call recursively for all elements of $arg
				$count = 0;
				foreach ($arg as $val) {
					$count += $this->count_all($val);
				}
				return $count;
		}
		return FALSE;
	}

	/**
	 * get_version()
	 *
	 * Grabs phpGACL version from the database.
	 *
	 * @return string Version of phpGACL
	 */
	function get_version() {
		$query = "select value from ".$this->_db_table_prefix."phpgacl where name = 'version'";
		$version = $this->db->GetOne($query);

		return $version;
	}

	/**
	 * get_schema_version()
	 *
	 * Grabs phpGACL schema version from the database.
	 *
	 * @return string Schema Version
	 */
	function get_schema_version() {
		$query = "select value from ".$this->_db_table_prefix."phpgacl where name = 'schema_version'";
		$version = $this->db->GetOne($query);

		return $version;
	}

	/*
	 *
	 * ACL
	 *
	 */

	/**
	 * consolidated_edit_acl()
	 *
	 * Add's an ACL but checks to see if it can consolidate it with another one first.
	 *
	 * This ONLY works with ACO's and ARO's. Groups, and AXO are excluded.
	 * As well this function is designed for handling ACLs with return values,
	 * and consolidating on the return_value, in hopes of keeping the ACL count to a minimum.
	 *
	 * A return value of false must _always_ be handled outside this function.
	 * As this function will remove AROs from ACLs and return false, in most cases
	 * you will need to a create a completely new ACL on a false return.
	 *
	 * @return bool Special boolean return value. See note.
	 *
	 * @param string ACO Section Value
	 * @param string ACO Value
	 * @param string ARO Section Value
	 * @param string ARO Value
	 * @param string Return Value of ACL
	 */
	function consolidated_edit_acl($aco_section_value, $aco_value, $aro_section_value, $aro_value, $return_value) {

		$this->debug_text("consolidated_edit_acl(): ACO Section Value: $aco_section_value ACO Value: $aco_value ARO Section Value: $aro_section_value ARO Value: $aro_value Return Value: $return_value");

		$acl_ids = array();

		if (empty($aco_section_value) ) {
			$this->debug_text("consolidated_edit_acl(): ACO Section Value ($aco_section_value) is empty, this is required!");
			return false;
		}

		if (empty($aco_value) ) {
			$this->debug_text("consolidated_edit_acl(): ACO Value ($aco_value) is empty, this is required!");
			return false;
		}

		if (empty($aro_section_value) ) {
			$this->debug_text("consolidated_edit_acl(): ARO Section Value ($aro_section_value) is empty, this is required!");
			return false;
		}

		if (empty($aro_value) ) {
			$this->debug_text("consolidated_edit_acl(): ARO Value ($aro_value) is empty, this is required!");
			return false;
		}

		if (empty($return_value) ) {
			$this->debug_text("consolidated_edit_acl(): Return Value ($return_value) is empty, this is required!");
			return false;
		}

		//See if a current ACL exists with the current objects, excluding return value
		$current_acl_ids = $this->search_acl($aco_section_value, $aco_value, $aro_section_value, $aro_value, FALSE, FALSE, FALSE, FALSE, FALSE);
		//showarray($current_acl_ids);

		if (is_array($current_acl_ids)) {
			$this->debug_text("add_consolidated_acl(): Found current ACL_IDs, counting ACOs");

			foreach ($current_acl_ids as $current_acl_id) {
				//Check to make sure these ACLs only have a single ACO mapped to them.
				$current_acl_array = &$this->get_acl($current_acl_id);

				//showarray($current_acl_array);
				$this->debug_text("add_consolidated_acl(): Current Count: ".$this->count_all($current_acl_array['aco'])."");

				if ( $this->count_all($current_acl_array['aco']) == 1) {
					$this->debug_text("add_consolidated_acl(): ACL ID: $current_acl_id has 1 ACO.");

					//Test to see if the return values match, if they do, no need removing or appending ARO. Just return true.
					if ($current_acl_array['return_value'] == $return_value) {
						$this->debug_text("add_consolidated_acl(): ACL ID: $current_acl_id has 1 ACO, and the same return value. No need to modify.");
						return true;
					}

					$acl_ids[] = $current_acl_id;
				}

			}
		}

		//showarray($acl_ids);
		$acl_ids_count = count($acl_ids);

		//If acl_id's turns up more then one ACL, lets remove the ARO from all of them in hopes to
		//eliminate any conflicts.
		if (is_array($acl_ids) AND $acl_ids_count > 0) {
			$this->debug_text("add_consolidated_acl(): Removing specified ARO from existing ACL.");

			foreach ($acl_ids as $acl_id) {
				//Remove ARO from current ACLs, so we don't create conflicting ACLs later on.
				if (!$this->shift_acl($acl_id, array($aro_section_value => array($aro_value)) ) ) {
					$this->debug_text("add_consolidated_acl(): Error removing specified ARO from ACL ID: $acl_id");
					return false;
				}
			}
		} else {
			$this->debug_text("add_consolidated_acl(): Didn't find any current ACLs with a single ACO. ");
		}
		//unset($acl_ids);
    $acl_ids = array();
		unset($acl_ids_count);

		//At this point there should be no conflicting ACLs, searching for an existing ACL with the new values.
		$new_acl_ids = $this->search_acl($aco_section_value, $aco_value, FALSE, FALSE, NULL, NULL, NULL, NULL, $return_value);
		$new_acl_count = count($new_acl_ids);
		//showarray($new_acl_ids);

		if (is_array($new_acl_ids)) {
			$this->debug_text("add_consolidated_acl(): Found new ACL_IDs, counting ACOs");

			foreach ($new_acl_ids as $new_acl_id) {
				//Check to make sure these ACLs only have a single ACO mapped to them.
				$new_acl_array = &$this->get_acl($new_acl_id);
				//showarray($new_acl_array);
				$this->debug_text("add_consolidated_acl(): New Count: ".$this->count_all($new_acl_array['aco'])."");
				if ( $this->count_all($new_acl_array['aco']) == 1) {

					$this->debug_text("add_consolidated_acl(): ACL ID: $new_acl_id has 1 ACO, append should be able to take place.");
					$acl_ids[] = $new_acl_id;
				}

			}
		}

		//showarray($acl_ids);
		$acl_ids_count = count($acl_ids);

		if (is_array($acl_ids) AND $acl_ids_count == 1) {
			$this->debug_text("add_consolidated_acl(): Appending specified ARO to existing ACL.");

			$acl_id=$acl_ids[0];

			if (!$this->append_acl($acl_id, array($aro_section_value => array($aro_value)) ) ) {
				$this->debug_text("add_consolidated_acl(): Error appending specified ARO to ACL ID: $acl_id");
				return false;
			}

			$this->debug_text("add_consolidated_acl(): Hot damn, ACL consolidated!");
			return true;
		} elseif($acl_ids_count > 1) {
			$this->debug_text("add_consolidated_acl(): Found more then one ACL with a single ACO. Possible conflicting ACLs.");
			return false;
		} elseif ($acl_ids_count == 0) {
			$this->debug_text("add_consolidated_acl(): No existing ACLs found, create a new one.");

			if (!$this->add_acl(	array( $aco_section_value => array($aco_value) ),
									array( $aro_section_value => array($aro_value) ),
									NULL,
									NULL,
									NULL,
									TRUE,
									TRUE,
									$return_value,
									NULL)
								) {
				$this->debug_text("add_consolidated_acl(): Error adding new ACL for ACO Section: $aco_section_value ACO Value: $aco_value Return Value: $return_value");
				return false;
			}

			$this->debug_text("add_consolidated_acl(): ADD_ACL() successfull, returning True.");
			return true;
		}

		$this->debug_text("add_consolidated_acl(): Returning false.");
		return false;
	}

	/**
	 * search_acl()
	 *
	 * Searches for ACL's with specified objects mapped to them.
	 *
	 * NULL values are included in the search, if you want to ignore
	 * for instance aro_groups use FALSE instead of NULL.
	 *
	 * @return array containing ACL IDs if search is successful
	 *
	 * @param string ACO Section Value
	 * @param string ACO Value
	 * @param string ARO Section Value
	 * @param string ARO Value
	 * @param string ARO Group Name
	 * @param string AXO Section Value
	 * @param string AXO Value
	 * @param string AXO Group Name
	 * @param string Return Value
	 */
	function search_acl($aco_section_value=NULL, $aco_value=NULL, $aro_section_value=NULL, $aro_value=NULL, $aro_group_name=NULL, $axo_section_value=NULL, $axo_value=NULL, $axo_group_name=NULL, $return_value=NULL) {
		$this->debug_text("search_acl(): aco_section_value: $aco_section_value aco_value: $aco_value, aro_section_value: $aro_section_value, aro_value: $aro_value, aro_group_name: $aro_group_name, axo_section_value: $axo_section_value, axo_value: $axo_value, axo_group_name: $axo_group_name, return_value: $return_value");

		$query = '
				SELECT		a.id
				FROM		'. $this->_db_table_prefix .'acl a';

		$where_query = array();

		// ACO
		if ($aco_section_value !== FALSE AND $aco_value !== FALSE) {
			$query .= '
				LEFT JOIN	'. $this->_db_table_prefix .'aco_map ac ON a.id=ac.acl_id';

			if ($aco_section_value == NULL AND $aco_value == NULL) {
				$where_query[] = '(ac.section_value IS NULL AND ac.value IS NULL)';
			} else {
				$where_query[] = '(ac.section_value='. $this->db->quote($aco_section_value) .' AND ac.value='. $this->db->quote($aco_value) .')';
			}
		}

		// ARO
		if ($aro_section_value !== FALSE AND $aro_value !== FALSE) {
			$query .= '
				LEFT JOIN	'. $this->_db_table_prefix .'aro_map ar ON a.id=ar.acl_id';

			if ($aro_section_value == NULL AND $aro_value == NULL) {
				$where_query[] = '(ar.section_value IS NULL AND ar.value IS NULL)';
			} else {
				$where_query[] = '(ar.section_value='. $this->db->quote($aro_section_value) .' AND ar.value='. $this->db->quote($aro_value) .')';
			}
		}

		// AXO
		if ($axo_section_value !== FALSE AND $axo_value !== FALSE) {
			$query .= '
				LEFT JOIN	'. $this->_db_table_prefix .'axo_map ax ON a.id=ax.acl_id';

			if ($axo_section_value == NULL AND $axo_value == NULL) {
				$where_query[] = '(ax.section_value IS NULL AND ax.value IS NULL)';
			} else {
				$where_query[] = '(ax.section_value='. $this->db->quote($axo_section_value) .' AND ax.value='. $this->db->quote($axo_value) .')';
			}
		}

		// ARO Group
		if ($aro_group_name !== FALSE) {
			$query .= '
				LEFT JOIN	'. $this->_db_table_prefix .'aro_groups_map arg ON a.id=arg.acl_id
				LEFT JOIN	'. $this->_db_table_prefix .'aro_groups rg ON arg.group_id=rg.id';

			if ($aro_group_name == NULL) {
				$where_query[] = '(rg.name IS NULL)';
			} else {
				$where_query[] = '(rg.name='. $this->db->quote($aro_group_name) .')';
			}
		}

		// AXO Group
		if ($axo_group_name !== FALSE) {
			$query .= '
				LEFT JOIN	'. $this->_db_table_prefix .'axo_groups_map axg ON a.id=axg.acl_id
				LEFT JOIN	'. $this->_db_table_prefix .'axo_groups xg ON axg.group_id=xg.id';

			if ($axo_group_name == NULL) {
				$where_query[] = '(xg.name IS NULL)';
			} else {
				$where_query[] = '(xg.name='. $this->db->quote($axo_group_name) .')';
			}
		}
		if ($return_value != FALSE) {
			if ($return_value == NULL) {
				$where_query[] = '(a.return_value IS NULL)';
			} else {
				$where_query[] = '(a.return_value='. $this->db->quote($return_value) .')';
			}
		}

		if (count($where_query) > 0) {
			$query .= '
				WHERE		'. implode (' AND ', $where_query);
		}

		return $this->db->GetCol($query);
	}

	/**
	 * append_acl()
	 *
	 * Appends objects on to a specific ACL.
	 *
	 * @return bool TRUE if successful, FALSE otherwise.
	 *
	 * @param int ACL ID #
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 */
	function append_acl($acl_id, $aro_array=NULL, $aro_group_ids=NULL, $axo_array=NULL, $axo_group_ids=NULL, $aco_array=NULL) {
		$this->debug_text("append_acl(): ACL_ID: $acl_id");

		$update = 0;

		if (empty($acl_id)) {
			$this->debug_text("append_acl(): No ACL_ID specified! ACL_ID: $acl_id");
			return false;
		}

		//Grab ACL data.
		$acl_array = &$this->get_acl($acl_id);

		//Append each object type seperately.
		if (is_array($aro_array) AND count($aro_array) > 0) {
			$this->debug_text("append_acl(): Appending ARO's");

			while (list($aro_section_value,$aro_value_array) = @each($aro_array)) {
				foreach ($aro_value_array as $aro_value) {
					if ( count($acl_array['aro'][$aro_section_value]) != 0 ) {
						if (!in_array($aro_value, $acl_array['aro'][$aro_section_value])) {
							$this->debug_text("append_acl(): ARO Section Value: $aro_section_value ARO VALUE: $aro_value");
							$acl_array['aro'][$aro_section_value][] = $aro_value;
							$update=1;
						} else {
							$this->debug_text("append_acl(): Duplicate ARO, ignoring... ");
						}
					} else { //Array is empty so add this aro value.
						$acl_array['aro'][$aro_section_value][] = $aro_value;
						$update = 1;
					}
				}
			}
		}

		if (is_array($aro_group_ids) AND count($aro_group_ids) > 0) {
			$this->debug_text("append_acl(): Appending ARO_GROUP_ID's");

			while (list(,$aro_group_id) = @each($aro_group_ids)) {
				if (!is_array($acl_array['aro_groups']) OR !in_array($aro_group_id, $acl_array['aro_groups'])) {
					$this->debug_text("append_acl(): ARO Group ID: $aro_group_id");
					$acl_array['aro_groups'][] = $aro_group_id;
					$update = 1;
				} else {
					$this->debug_text("append_acl(): Duplicate ARO_Group_ID, ignoring... ");
				}
			}
		}

		if (is_array($axo_array) AND count($axo_array) > 0) {
			$this->debug_text("append_acl(): Appending AXO's");

			while (list($axo_section_value,$axo_value_array) = @each($axo_array)) {
				foreach ($axo_value_array as $axo_value) {
					if (!in_array($axo_value, $acl_array['axo'][$axo_section_value])) {
						$this->debug_text("append_acl(): AXO Section Value: $axo_section_value AXO VALUE: $axo_value");
						$acl_array['axo'][$axo_section_value][] = $axo_value;
						$update = 1;
					} else {
						$this->debug_text("append_acl(): Duplicate AXO, ignoring... ");
					}

				}
			}
		}

		if (is_array($axo_group_ids) AND count($axo_group_ids) > 0) {
			$this->debug_text("append_acl(): Appending AXO_GROUP_ID's");
			while (list(,$axo_group_id) = @each($axo_group_ids)) {
				if (!is_array($acl_array['axo_groups']) OR !in_array($axo_group_id, $acl_array['axo_groups'])) {
					$this->debug_text("append_acl(): AXO Group ID: $axo_group_id");
					$acl_array['axo_groups'][] = $axo_group_id;
					$update = 1;
				} else {
					$this->debug_text("append_acl(): Duplicate ARO_Group_ID, ignoring... ");
				}
			}
		}

		if (is_array($aco_array) AND count($aco_array) > 0) {
			$this->debug_text("append_acl(): Appending ACO's");

			while (list($aco_section_value,$aco_value_array) = @each($aco_array)) {
				foreach ($aco_value_array as $aco_value) {
					if (!in_array($aco_value, $acl_array['aco'][$aco_section_value])) {
						$this->debug_text("append_acl(): ACO Section Value: $aco_section_value ACO VALUE: $aco_value");
						$acl_array['aco'][$aco_section_value][] = $aco_value;
						$update = 1;
					} else {
						$this->debug_text("append_acl(): Duplicate ACO, ignoring... ");
					}
				}
			}
		}

		if ($update == 1) {
			$this->debug_text("append_acl(): Update flag set, updating ACL.");
			//function edit_acl($acl_id, $aco_array, $aro_array, $aro_group_ids=NULL, $axo_array=NULL, $axo_group_ids=NULL, $allow=1, $enabled=1, $return_value=NULL, $note=NULL) {
			return $this->edit_acl($acl_id, $acl_array['aco'], $acl_array['aro'], $acl_array['aro_groups'], $acl_array['axo'], $acl_array['axo_groups'], $acl_array['allow'], $acl_array['enabled'], $acl_array['return_value'], $acl_array['note']);
		}

		//Return true if everything is duplicate and no ACL id updated.
		$this->debug_text("append_acl(): Update flag not set, NOT updating ACL.");
		return true;
	}

	/**
	 * shift_acl()
	 *
	 * Opposite of append_acl(). Removes objects from a specific ACL. (named after PHP's array_shift())
	 *
	 * @return bool TRUE if successful, FALSE otherwise.
	 *
	 * @param int ACL ID #
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 */
	function shift_acl($acl_id, $aro_array=NULL, $aro_group_ids=NULL, $axo_array=NULL, $axo_group_ids=NULL, $aco_array=NULL) {
		$this->debug_text("shift_acl(): ACL_ID: $acl_id");

		$update = 0;

		if (empty($acl_id)) {
			$this->debug_text("shift_acl(): No ACL_ID specified! ACL_ID: $acl_id");
			return false;
		}

		//Grab ACL data.
		$acl_array = &$this->get_acl($acl_id);

		//showarray($acl_array);
		//Remove each object type seperately.
		if (is_array($aro_array) AND count($aro_array) > 0) {
			$this->debug_text("shift_acl(): Removing ARO's");

			while (list($aro_section_value,$aro_value_array) = @each($aro_array)) {
				foreach ($aro_value_array as $aro_value) {
					$this->debug_text("shift_acl(): ARO Section Value: $aro_section_value ARO VALUE: $aro_value");

					//Only search if aro array contains data.
					if ( count($acl_array['aro'][$aro_section_value]) != 0 ) {
						$aro_key = array_search($aro_value, $acl_array['aro'][$aro_section_value]);

						if ($aro_key !== FALSE) {
							$this->debug_text("shift_acl(): Removing ARO. ($aro_key)");
							unset($acl_array['aro'][$aro_section_value][$aro_key]);
							$update = 1;
						} else {
							$this->debug_text("shift_acl(): ARO doesn't exist, can't remove it.");
						}
					}

				}
			}
		}

		if (is_array($aro_group_ids) AND count($aro_group_ids) > 0) {
			$this->debug_text("shift_acl(): Removing ARO_GROUP_ID's");

			while (list(,$aro_group_id) = @each($aro_group_ids)) {
				$this->debug_text("shift_acl(): ARO Group ID: $aro_group_id");
				$aro_group_key = array_search($aro_group_id, $acl_array['aro_groups']);

				if ($aro_group_key !== FALSE) {
					$this->debug_text("shift_acl(): Removing ARO Group. ($aro_group_key)");
					unset($acl_array['aro_groups'][$aro_group_key]);
					$update = 1;
				} else {
					$this->debug_text("shift_acl(): ARO Group doesn't exist, can't remove it.");
				}
			}
		}

		if (is_array($axo_array) AND count($axo_array) > 0) {
			$this->debug_text("shift_acl(): Removing AXO's");

			while (list($axo_section_value,$axo_value_array) = @each($axo_array)) {
				foreach ($axo_value_array as $axo_value) {
					$this->debug_text("shift_acl(): AXO Section Value: $axo_section_value AXO VALUE: $axo_value");
					$axo_key = array_search($axo_value, $acl_array['axo'][$axo_section_value]);

					if ($axo_key !== FALSE) {
						$this->debug_text("shift_acl(): Removing AXO. ($axo_key)");
						unset($acl_array['axo'][$axo_section_value][$axo_key]);
						$update = 1;
					} else {
						$this->debug_text("shift_acl(): AXO doesn't exist, can't remove it.");
					}
				}
			}
		}

		if (is_array($axo_group_ids) AND count($axo_group_ids) > 0) {
			$this->debug_text("shift_acl(): Removing AXO_GROUP_ID's");

			while (list(,$axo_group_id) = @each($axo_group_ids)) {
				$this->debug_text("shift_acl(): AXO Group ID: $axo_group_id");
				$axo_group_key = array_search($axo_group_id, $acl_array['axo_groups']);

				if ($axo_group_key !== FALSE) {
					$this->debug_text("shift_acl(): Removing AXO Group. ($axo_group_key)");
					unset($acl_array['axo_groups'][$axo_group_key]);
					$update = 1;
				} else {
					$this->debug_text("shift_acl(): AXO Group doesn't exist, can't remove it.");
				}
			}
		}

		if (is_array($aco_array) AND count($aco_array) > 0) {
			$this->debug_text("shift_acl(): Removing ACO's");

			while (list($aco_section_value,$aco_value_array) = @each($aco_array)) {
				foreach ($aco_value_array as $aco_value) {
					$this->debug_text("shift_acl(): ACO Section Value: $aco_section_value ACO VALUE: $aco_value");
					$aco_key = array_search($aco_value, $acl_array['aco'][$aco_section_value]);

					if ($aco_key !== FALSE) {
						$this->debug_text("shift_acl(): Removing ACO. ($aco_key)");
						unset($acl_array['aco'][$aco_section_value][$aco_key]);
						$update = 1;
					} else {
						$this->debug_text("shift_acl(): ACO doesn't exist, can't remove it.");
					}
				}
			}
		}

		if ($update == 1) {
			//We know something was changed, so lets see if no ACO's or no ARO's are left assigned to this ACL, if so, delete the ACL completely.
			//$this->showarray($acl_array);
			$this->debug_text("shift_acl(): ACOs: ". $this->count_all($acl_array['aco']) ." AROs: ".$this->count_all($acl_array['aro'])."");

			if ( $this->count_all($acl_array['aco']) == 0
					OR ( $this->count_all($acl_array['aro']) == 0
						AND ( $this->count_all($acl_array['axo']) == 0 OR $acl_array['axo'] == FALSE)
						AND (count($acl_array['aro_groups']) == 0 OR $acl_array['aro_groups'] == FALSE)
						AND (count($acl_array['axo_groups']) == 0 OR $acl_array['axo_groups'] == FALSE)
						) ) {
				$this->debug_text("shift_acl(): No ACOs or ( AROs AND AXOs AND ARO Groups AND AXO Groups) left assigned to this ACL (ID: $acl_id), deleting ACL.");

				return $this->del_acl($acl_id);
			}

			$this->debug_text("shift_acl(): Update flag set, updating ACL.");

			return $this->edit_acl($acl_id, $acl_array['aco'], $acl_array['aro'], $acl_array['aro_groups'], $acl_array['axo'], $acl_array['axo_groups'], $acl_array['allow'], $acl_array['enabled'], $acl_array['return_value'], $acl_array['note']);
		}

		//Return true if everything is duplicate and no ACL id updated.
		$this->debug_text("shift_acl(): Update flag not set, NOT updating ACL.");
		return true;
	}

	/**
	 * get_acl()
	 *
	 * Grabs ACL data.
	 *
	 * @return bool FALSE if not found, or Associative Array with the following items:
	 *
	 *	- 'aco' => Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 *	- 'aro' => Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 *	- 'axo' => Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 *	- 'aro_groups' => Array of Group IDs
	 *	- 'axo_groups' => Array of Group IDs
	 *	- 'acl_id' => int ACL ID #
	 *	- 'allow' => int Allow flag
	 *	- 'enabled' => int Enabled flag
	 *	- 'return_value' => string Return Value
	 *	- 'note' => string Note
	 *
	 * @param int ACL ID #
	 */
	function get_acl($acl_id) {

		$this->debug_text("get_acl(): ACL_ID: $acl_id");

		if (empty($acl_id)) {
			$this->debug_text("get_acl(): No ACL_ID specified! ACL_ID: $acl_id");
			return false;
		}

		//Grab ACL information
		$query = "select id, allow, enabled, return_value, note from ".$this->_db_table_prefix."acl where id = ".$acl_id."";
		$acl_row = $this->db->GetRow($query);

		// return false if not found
		if (!$acl_row) {
			$this->debug_text("get_acl(): No ACL found for that ID! ACL_ID: $acl_id");
			return false;
		}

		list($retarr['acl_id'], $retarr['allow'], $retarr['enabled'], $retarr['return_value'], $retarr['note']) = $acl_row;

		//Grab selected ACO's
		$query = "select distinct a.section_value, a.value, c.name, b.name from ".$this->_db_table_prefix."aco_map a, ".$this->_db_table_prefix."aco b, ".$this->_db_table_prefix."aco_sections c
							where ( a.section_value=b.section_value AND a.value = b.value) AND b.section_value=c.value AND a.acl_id = $acl_id";
		$rs = $this->db->Execute($query);
		$rows = $rs->GetRows();

		$retarr['aco'] = array();
		while (list(,$row) = @each($rows)) {
			list($section_value, $value, $section, $aco) = $row;
			$this->debug_text("Section Value: $section_value Value: $value Section: $section ACO: $aco");

			$retarr['aco'][$section_value][] = $value;

		}
		//showarray($aco);

		//Grab selected ARO's
		$query = "select distinct a.section_value, a.value, c.name, b.name from ".$this->_db_table_prefix."aro_map a, ".$this->_db_table_prefix."aro b, ".$this->_db_table_prefix."aro_sections c
							where ( a.section_value=b.section_value AND a.value = b.value) AND b.section_value=c.value AND a.acl_id = $acl_id";
		$rs = $this->db->Execute($query);
		$rows = $rs->GetRows();

		$retarr['aro'] = array();
		while (list(,$row) = @each($rows)) {
			list($section_value, $value, $section, $aro) = $row;
			$this->debug_text("Section Value: $section_value Value: $value Section: $section ARO: $aro");

			$retarr['aro'][$section_value][] = $value;

		}
		//showarray($options_aro);

		//Grab selected AXO's
		$query = "select distinct a.section_value, a.value, c.name, b.name from ".$this->_db_table_prefix."axo_map a, ".$this->_db_table_prefix."axo b, ".$this->_db_table_prefix."axo_sections c
							where ( a.section_value=b.section_value AND a.value = b.value) AND b.section_value=c.value AND a.acl_id = $acl_id";
		$rs = $this->db->Execute($query);
		$rows = $rs->GetRows();

		$retarr['axo'] = array();
		while (list(,$row) = @each($rows)) {
			list($section_value, $value, $section, $axo) = $row;
			$this->debug_text("Section Value: $section_value Value: $value Section: $section AXO: $axo");

			$retarr['axo'][$section_value][] = $value;

		}
		//showarray($options_aro);

		//Grab selected ARO groups.
		$retarr['aro_groups'] = array();
		$query = "select distinct group_id from ".$this->_db_table_prefix."aro_groups_map where  acl_id = $acl_id";
		$retarr['aro_groups'] = $this->db->GetCol($query);
		//showarray($selected_groups);

		//Grab selected AXO groups.
		$retarr['axo_groups'] = array();
		$query = "select distinct group_id from ".$this->_db_table_prefix."axo_groups_map where  acl_id = $acl_id";
		$retarr['axo_groups'] = $this->db->GetCol($query);
		//showarray($selected_groups);

		return $retarr;
	}

	/**
	 * is_conflicting_acl()
	 *
	 * Checks for conflicts when adding a specific ACL.
	 *
	 * @return bool Returns true if conflict is found.
	 *
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Array of ACL IDs to ignore from the result set.
	 *
	 */
	function is_conflicting_acl($aco_array, $aro_array, $aro_group_ids=NULL, $axo_array=NULL, $axo_group_ids=NULL, $ignore_acl_ids=NULL) {
		//Check for potential conflicts. Ignore groups, as groups will almost always have "conflicting" ACLs.
		//Thats part of inheritance.

		if (!is_array($aco_array)) {
			$this->debug_text('is_conflicting_acl(): Invalid ACO Array.');
			return FALSE;
		}

		if (!is_array($aro_array)) {
			$this->debug_text('is_conflicting_acl(): Invalid ARO Array.');
			return FALSE;
		}

		$query  = '
			SELECT		a.id
			FROM		'. $this->_db_table_prefix .'acl a
			LEFT JOIN	'. $this->_db_table_prefix .'aco_map ac ON ac.acl_id=a.id
			LEFT JOIN	'. $this->_db_table_prefix .'aro_map ar ON ar.acl_id=a.id
			LEFT JOIN	'. $this->_db_table_prefix .'axo_map ax ON ax.acl_id=a.id
			LEFT JOIN	'. $this->_db_table_prefix .'axo_groups_map axg ON axg.acl_id=a.id
			LEFT JOIN	'. $this->_db_table_prefix .'axo_groups xg ON xg.id=axg.group_id
			';

		//ACO
		foreach ($aco_array as $aco_section_value => $aco_value_array) {
			$this->debug_text("is_conflicting_acl(): ACO Section Value: $aco_section_value ACO VALUE: $aco_value_array");
			//showarray($aco_array);

			if (!is_array($aco_value_array)) {
				$this->debug_text('is_conflicting_acl(): Invalid Format for ACO Array item. Skipping...');
				continue;
				// return TRUE;
			}
			//Move the below line in to the LEFT JOIN above for PostgreSQL sake.
			//'ac1' => 'ac.acl_id=a.id',
			$where_query = array(
				'ac2' => '(ac.section_value='. $this->db->quote($aco_section_value) .' AND ac.value IN (\''. implode ('\',\'', $aco_value_array) .'\'))'
			);

			//ARO
			foreach ($aro_array as $aro_section_value => $aro_value_array) {
				$this->debug_text("is_conflicting_acl(): ARO Section Value: $aro_section_value ARO VALUE: $aro_value_array");

				if (!is_array($aro_value_array))
				{
					$this->debug_text('is_conflicting_acl(): Invalid Format for ARO Array item. Skipping...');
					continue;
					// return TRUE;
				}

				$this->debug_text("is_conflicting_acl(): Search: ACO Section: $aco_section_value ACO Value: $aco_value_array ARO Section: $aro_section_value ARO Value: $aro_value_array");

				//Move the below line in to the LEFT JOIN above for PostgreSQL sake.
				//$where_query['ar1'] = 'ar.acl_id=a.id';
				$where_query['ar2'] = '(ar.section_value='. $this->db->quote($aro_section_value) .' AND ar.value IN (\''. implode ('\',\'', $aro_value_array) .'\'))';

				if (is_array($axo_array) AND count($axo_array) > 0) {
					foreach ($axo_array as $axo_section_value => $axo_value_array) {
						$this->debug_text("is_conflicting_acl(): AXO Section Value: $axo_section_value AXO VALUE: $axo_value_array");

						if (!is_array($axo_value_array)) {
							$this->debug_text('is_conflicting_acl(): Invalid Format for AXO Array item. Skipping...');
							continue;
							// return TRUE;
						}

						$this->debug_text("is_conflicting_acl(): Search: ACO Section: $aco_section_value ACO Value: $aco_value_array ARO Section: $aro_section_value ARO Value: $aro_value_array AXO Section: $axo_section_value AXO Value: $axo_value_array");

						//$where_query['ax1'] = 'ax.acl_id=x.id';
						$where_query['ax1'] = 'ax.acl_id=a.id';
						$where_query['ax2'] = '(ax.section_value='. $this->db->quote($axo_section_value) .' AND ax.value IN (\''. implode ('\',\'', $axo_value_array) .'\'))';

						$where  = 'WHERE ' . implode(' AND ', $where_query);

						$conflict_result = $this->db->GetCol($query . $where);

						if (is_array($conflict_result) AND !empty($conflict_result)) {
							// showarray($conflict_result);

							if (is_array($ignore_acl_ids)) {
								$conflict_result = array_diff($conflict_result, $ignore_acl_ids);
							}

							if (count($conflict_result) > 0) {
								$conflicting_acls_str = implode(',', $conflict_result);
								$this->debug_text("is_conflicting_acl(): Conflict FOUND!!! ACL_IDS: ($conflicting_acls_str)");
								return TRUE;
							}
						}
					}
				} else {
					$where_query['ax1'] = '(ax.section_value IS NULL AND ax.value IS NULL)';
					$where_query['ax2'] = 'xg.name IS NULL';

					$where  = 'WHERE ' . implode(' AND ', $where_query);

					$conflict_result = $this->db->GetCol($query . $where);

					if (is_array($conflict_result) AND !empty($conflict_result)) {
						// showarray($conflict_result);

						if (is_array($ignore_acl_ids)) {
							$conflict_result = array_diff($conflict_result, $ignore_acl_ids);
						}

						if (count($conflict_result) > 0) {
							$conflicting_acls_str = implode(',', $conflict_result);
							$this->debug_text("is_conflicting_acl(): Conflict FOUND!!! ACL_IDS: ($conflicting_acls_str)");
							return TRUE;
						}
					}
				}
			}
		}

		$this->debug_text('is_conflicting_acl(): No conflicting ACL found.');
		return FALSE;
	}

	/**
	 * add_acl()
	 *
	 * Add's an ACL. ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays.
	 *
	 * @return bool Return ACL ID of new ACL if successful, FALSE otherewise.
	 *
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param int Allow flag
	 * @param int Enabled flag
	 * @param string Return Value
	 * @param string Note
	 * @param string ACL Section Value
	 * @param int ACL ID # Specific Request

	 */
	function add_acl($aco_array, $aro_array, $aro_group_ids=NULL, $axo_array=NULL, $axo_group_ids=NULL, $allow=1, $enabled=1, $return_value=NULL, $note=NULL, $section_value=NULL, $acl_id=FALSE ) {

		$this->debug_text("add_acl():");

		if (count($aco_array) == 0) {
			$this->debug_text("Must select at least one Access Control Object");
			return false;
		}

		if (count($aro_array) == 0 AND count($aro_group_ids) == 0) {
			$this->debug_text("Must select at least one Access Request Object or Group");
			return false;
		}

		if (empty($allow)) {
			$allow=0;
		}

		if (empty($enabled)) {
			$enabled=0;
		}

		if (!empty($section_value)
			AND !$this->get_object_section_section_id(NULL, $section_value, 'ACL')) {
			$this->debug_text("add_acl(): Section Value: $section_value DOES NOT exist in the database.");
			return false;
		}

		//Unique the group arrays. Later one we unique ACO/ARO/AXO arrays.
		if (is_array($aro_group_ids)) {
			$aro_group_ids = array_unique($aro_group_ids);
		}
		if (is_array($axo_group_ids)) {
			$axo_group_ids = array_unique($axo_group_ids);
		}

		//Check for conflicting ACLs.
		if ($this->is_conflicting_acl($aco_array,$aro_array,$aro_group_ids,$axo_array,$axo_group_ids,array($acl_id))) {
			$this->debug_text("add_acl(): Detected possible ACL conflict, not adding ACL!");
			return false;
		}

		//Edit ACL if acl_id is set. This is simply if we're being called by edit_acl().
		if ($this->get_acl($acl_id) == FALSE) {
			if ( empty($section_value) ) {
				$section_value='system';
				if( !$this->get_object_section_section_id(NULL, $section_value, 'ACL') ) {
					// Use the acl section with the lowest order value.
					$acl_sections_table = $this->_db_table_prefix .'acl_sections';
					$acl_section_order_value = $this->db->GetOne("SELECT min(order_value) from $acl_sections_table");

					$query = "
						SELECT value
						FROM $acl_sections_table
						WHERE order_value = $acl_section_order_value
					";
					$section_value = $this->db->GetOne($query);

					if ( empty($section_value) ) {
						$this->debug_text("add_acl(): No valid acl section found.");
						return false;
					} else {
						$this->debug_text("add_acl(): Using default section value: $section_value.");
					}
				}
			}

			//ACL not specified, so create acl_id
			if (empty($acl_id)) {
				//Create ACL row first, so we have the acl_id
				$acl_id = $this->db->GenID($this->_db_table_prefix.'acl_seq',10);

				//Double check the ACL ID was generated.
				if (empty($acl_id)) {
					$this->debug_text("add_acl(): ACL_ID generation failed!");
					return false;
				}
			}

			//Begin transaction _after_ GenID. Because on the first run, if GenID has to create the sequence,
			//the transaction will fail.
			$this->db->BeginTrans();

			$query = 'INSERT INTO '.$this->_db_table_prefix.'acl (id,section_value,allow,enabled,return_value,note,updated_date) VALUES('. $acl_id .','. $this->db->quote($section_value) .','. $allow .','. $enabled .','. $this->db->quote($return_value) .', '. $this->db->quote($note) .','. time() .')';
			$result = $this->db->Execute($query);
		} else {
			$section_sql = '';
			if ( !empty($section_value) ) {
				$section_sql = 'section_value='. $this->db->quote ($section_value) .',';
			}

			$this->db->BeginTrans();

			//Update ACL row, and remove all mappings so they can be re-inserted.
			$query  = '
				UPDATE	'. $this->_db_table_prefix .'acl
				SET             ' . $section_sql . '
						allow='. $allow .',
						enabled='. $enabled .',
						return_value='. $this->db->quote($return_value) .',
						note='. $this->db->quote($note) .',
						updated_date='. time() .'
				WHERE	id='. $acl_id;
			$result = $this->db->Execute($query);

			if ($result) {
				$this->debug_text("Update completed without error, delete mappings...");
				//Delete all mappings so they can be re-inserted.
				foreach (array('aco_map', 'aro_map', 'axo_map', 'aro_groups_map', 'axo_groups_map') as $map) {
					$query = 'DELETE FROM '. $this->_db_table_prefix . $map .' WHERE acl_id='. $acl_id;
					$rs = $this->db->Execute($query);

					if (!is_object($rs))
					{
						$this->debug_db('add_acl');
						$this->db->RollBackTrans();
						return FALSE;
					}
				}
			}
		}

		if (!is_object($result)) {
			$this->debug_db('add_acl');
			$this->db->RollBackTrans();
			return false;
		}

		$this->debug_text("Insert or Update completed without error, insert new mappings.");
		// Insert ACO/ARO/AXO mappings
		foreach (array('aco', 'aro', 'axo') as $map) {
			$map_array = ${$map .'_array'};

			if (!is_array ($map_array)) {
				continue;
			}

			foreach ($map_array as $section_value => $value_array) {
				$this->debug_text ('Insert: '. strtoupper($map) .' Section Value: '. $section_value .' '. strtoupper($map) .' VALUE: '. $value_array);
				// $this->showarray ($aco_value_array);

				if (!is_array($value_array)) {
					$this->debug_text ('add_acl (): Invalid Format for '. strtoupper ($map) .' Array item. Skipping...');
					continue;
					// return true;
				}

				$value_array = array_unique($value_array);

				foreach ($value_array as $value) {
					$object_id = &$this->get_object_id($section_value, $value, $map);

					if (empty($object_id))
					{
						$this->debug_text('add_acl(): '. strtoupper($map) . " Object Section Value: $section_value Value: $value DOES NOT exist in the database. Skipping...");
						$this->db->RollBackTrans();
						return false;
					}

					$query  = 'INSERT INTO '. $this->_db_table_prefix . $map .'_map (acl_id,section_value,value) VALUES ('. $acl_id .', '. $this->db->quote($section_value) .', '. $this->db->quote($value) .')';
					$rs = $this->db->Execute($query);

					if (!is_object($rs))
					{
						$this->debug_db('add_acl');
						$this->db->RollBackTrans();
						return false;
					}
				}
			}
		}

		// Insert ARO/AXO GROUP mappings
		foreach (array('aro', 'axo') as $map) {
			$map_group_ids = ${$map .'_group_ids'};

			if (!is_array($map_group_ids)) {
				continue;
			}

			foreach ($map_group_ids as $group_id) {
				$this->debug_text ('Insert: '. strtoupper($map) .' GROUP ID: '. $group_id);

				$group_data = &$this->get_group_data($group_id, $map);

				if (empty($group_data)) {
					$this->debug_text('add_acl(): '. strtoupper($map) . " Group: $group_id DOES NOT exist in the database. Skipping...");
					$this->db->RollBackTrans();
					return false;
				}

				$query  = 'INSERT INTO '. $this->_db_table_prefix . $map .'_groups_map (acl_id,group_id) VALUES ('. $acl_id .', '. $group_id .')';
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					$this->debug_db('add_acl');
					$this->db->RollBackTrans();
					return false;
				}
			}
		}

		$this->db->CommitTrans();

		if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) {
			//Expire all cache.
			$this->Cache_Lite->clean('default');
		}

		//Return only the ID in the first row.
		return $acl_id;
	}

	/**
	 * edit_acl()
	 *
	 * Edit's an ACL, ACO_IDS, ARO_IDS, GROUP_IDS must all be arrays.
	 *
	 * @return bool Return TRUE if successful, FALSE otherewise.
	 *
	 * @param int ACL ID # to edit
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 * @param array Array of Group IDs
	 * @param int Allow flag
	 * @param int Enabled flag
	 * @param string Return Value
	 * @param string Note
	 * @param string ACL Section Value
	 */
	function edit_acl($acl_id, $aco_array, $aro_array, $aro_group_ids=NULL, $axo_array=NULL, $axo_group_ids=NULL, $allow=1, $enabled=1, $return_value=NULL, $note=NULL, $section_value=NULL) {

		$this->debug_text("edit_acl():");

		if (empty($acl_id) ) {
			$this->debug_text("edit_acl(): Must specify a single ACL_ID to edit");
			return false;
		}
		if (count($aco_array) == 0) {
			$this->debug_text("edit_acl(): Must select at least one Access Control Object");
			return false;
		}

		if (count($aro_array) == 0 AND count($aro_group_ids) == 0) {
			$this->debug_text("edit_acl(): Must select at least one Access Request Object or Group");
			return false;
		}

		if (empty($allow)) {
			$allow=0;
		}

		if (empty($enabled)) {
			$enabled=0;
		}

		//if ($this->add_acl($aco_array, $aro_array, $group_ids, $allow, $enabled, $acl_id)) {
		if ($this->add_acl($aco_array, $aro_array, $aro_group_ids, $axo_array, $axo_group_ids, $allow, $enabled, $return_value, $note, $section_value, $acl_id)) {
			return true;
		} else {
			$this->debug_text("edit_acl(): error in add_acl()");
			return false;
		}
	}

	/**
	 * del_acl()
	 *
	 * Deletes a given ACL
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise.
	 *
	 * @param int ACL ID # to delete
	 */
	function del_acl($acl_id) {

		$this->debug_text("del_acl(): ID: $acl_id");

		if (empty($acl_id) ) {
			$this->debug_text("del_acl(): ACL_ID ($acl_id) is empty, this is required");
			return false;
		}

		$this->db->BeginTrans();

		// Delete all mappings to the ACL first
		foreach (array('aco_map', 'aro_map', 'axo_map', 'aro_groups_map', 'axo_groups_map') as $map) {
			$query  = 'DELETE FROM '. $this->_db_table_prefix . $map .' WHERE acl_id='. $acl_id;
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('del_acl');
				$this->db->RollBackTrans();
				return false;
			}
		}

		// Delete the ACL
		$query  = 'DELETE FROM '. $this->_db_table_prefix .'acl WHERE id='. $acl_id;
		$this->debug_text('delete query: '. $query);
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('del_acl');
			$this->db->RollBackTrans();
			return false;
		}

		$this->debug_text("del_acl(): deleted ACL ID: $acl_id");
		$this->db->CommitTrans();

		if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) {
			//Expire all cache.
			$this->Cache_Lite->clean('default');
		}

		return TRUE;
	}


	/*
	 *
	 * Groups
	 *
	 */

	/**
	 * sort_groups()
	 *
	 * Grabs all the groups from the database doing preliminary grouping by parent
	 *
	 * @return array Returns 2-Dimensional array: $array[<parent_id>][<group_id>] = <group_name>
	 *
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function sort_groups($group_type='ARO') {

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		//Grab all groups from the database.
		$query  = 'SELECT id, parent_id, name FROM '. $table .' ORDER BY parent_id, name';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('sort_groups');
			return false;
		}

		/*
		 * Save groups in an array sorted by parent. Should be make it easier for later on.
		 */
		$sorted_groups = array();

		while ($row = $rs->FetchRow()) {
			$id = &$row[0];
			$parent_id = &$row[1];
			$name = &$row[2];

			$sorted_groups[$parent_id][$id] = $name;
		}

		return $sorted_groups;
	}

	/**
	 * format_groups()
	 *
	 * Takes the array returned by sort_groups() and formats for human
	 * consumption. Recursively calls itself to produce the desired output.
	 *
	 * @return array Array of formatted text, ordered by group id, formatted according to $type
	 *
	 * @param array Output from gacl_api->sorted_groups($group_type)
	 * @param array Output type desired, either 'TEXT', 'HTML', or 'ARRAY'
	 * @param int Root of tree to produce
	 * @param int Current level of depth
	 * @param array Pass the current formatted groups object for appending via recursion.
	 */
	function format_groups($sorted_groups, $type='TEXT', $root_id=0, $level=0, $formatted_groups=NULL) {
		if ( !is_array ($sorted_groups) ) {
			return FALSE;
		}

		if ( !is_array ($formatted_groups) ) {
			$formatted_groups = array ();
		}

		//$this->showarray($formatted_groups);

		//while (list($id,$name) = @each($sorted_groups[$root_id])) {
		if (isset($sorted_groups[$root_id])) {
			//$last_id = end( array_keys($sorted_groups[$root_id]));
			//PHP5 compatibility
			$keys = array_keys($sorted_groups[$root_id]);
			$last_id = end($keys);
			unset($keys);

			foreach ($sorted_groups[$root_id] as $id => $name) {
				switch (strtoupper($type)) {
					case 'TEXT':
						/*
						 * Formatting optimized for TEXT (combo box) output.
						 */

						if ( is_numeric($level) ) {
							$level = str_repeat('&nbsp;&nbsp; ', $level);
						}

						if ( strlen($level) >= 8 ) {
							if ( $id == $last_id ) {
								$spacing = substr($level, 0, -8) .'\'- ';
								$level = substr($level, 0, -8) .'&nbsp;&nbsp; ';
							} else {
								$spacing = substr($level, 0, -8) .'|- ';
							}
						} else {
							$spacing = $level;
						}

						$next = $level .'|&nbsp; ';
						$text = $spacing.$name;
						break;
					case 'HTML':
						/*
						 * Formatting optimized for HTML (tables) output.
						 */
						$width= $level * 20;
						$spacing = "<img src=\"s.gif\" width=\"$width\">";
						$next = $level + 1;
						$text = $spacing." ".$name;
						break;
					case 'ARRAY':
						$next = $level;
						$text = $name;
						break;
					default:
						return FALSE;
				}

				$formatted_groups[$id] = $text;
				/*
				 * Recurse if we can.
				 */

				//if (isset($sorted_groups[$id]) AND count($sorted_groups[$id]) > 0) {
				if (isset($sorted_groups[$id]) ) {
					//$this->debug_text("format_groups(): Recursing! Level: $level");
					$formatted_groups = $this->format_groups($sorted_groups, $type, $id, $next, $formatted_groups);
				} else {
					//$this->debug_text("format_groups(): Found last branch!");
				}
			}
		}

		//$this->debug_text("format_groups(): Returning final array.");

		return $formatted_groups;
	}

	/**
	 * get_group_id()
	 *
	 * Gets the group_id given the name or value.
	 *
	 * Will only return one group id, so if there are duplicate names, it will return false.
	 *
	 * @return int Returns Group ID if found and Group ID is unique in database, otherwise, returns FALSE
	 *
	 * @param string Group Value
	 * @param string Group Name
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function get_group_id($value = NULL, $name = NULL, $group_type = 'ARO') {

		$this->debug_text("get_group_id(): Value: $value, Name: $name, Type: $group_type" );

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		$name = trim($name);
		$value = trim($value);

		if (empty($name) AND empty($value) ) {
			$this->debug_text("get_group_id(): name and value, at least one is required");
			return false;
		}

		$query = 'SELECT id FROM '. $table .' WHERE ';
		if ( !empty($value) ) {
		  $query .= ' value='. $this->db->quote($value);
		} else {
		  $query .= ' name='. $this->db->quote($name);
		}
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_group_id');
			return false;
		}

		$row_count = $rs->RecordCount();

		if ($row_count > 1) {
			$this->debug_text("get_group_id(): Returned $row_count rows, can only return one. Please make your names unique.");
			return false;
		}

		if ($row_count == 0) {
			$this->debug_text("get_group_id(): Returned $row_count rows");
			return false;
		}

		$row = $rs->FetchRow();

		//Return the ID.
		return $row[0];
	}

	/**
	 * get_group_children()
	 *
	 * Gets a groups child IDs
	 *
	 * @return array Array of Child ID's of the referenced group
	 *
	 * @param int Group ID #
	 * @param int Group Type, either 'ARO' or 'AXO'
	 * @param string Either 'RECURSE' or 'NO_RECURSE', to recurse while fetching group children.
	 */
	function get_group_children($group_id, $group_type = 'ARO', $recurse = 'NO_RECURSE') {
		$this->debug_text("get_group_children(): Group_ID: $group_id Group Type: $group_type Recurse: $recurse");

		switch (strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'aro_groups';
		}

		if (empty($group_id)) {
			$this->debug_text("get_group_children(): ID ($group_id) is empty, this is required");
			return FALSE;
		}

		$query  = '
				SELECT		g1.id
				FROM		'. $table .' g1';

		//FIXME-mikeb: Why is group_id in quotes?
		switch (strtoupper($recurse)) {
			case 'RECURSE':
				$query .= '
				LEFT JOIN 	'. $table .' g2 ON g2.lft<g1.lft AND g2.rgt>g1.rgt
				WHERE		g2.id='. $group_id;
				break;
			default:
				$query .= '
				WHERE		g1.parent_id='. $group_id;
		}

		$query .= '
				ORDER BY	g1.value';

		return $this->db->GetCol($query);
	}

	/**
	 * get_group_data()
	 *
	 * Gets the group data given the GROUP_ID.
	 *
	 * @return array Returns numerically indexed array with the following columns:
	 *	- array[0] = (int) Group ID #
	 *	- array[1] = (int) Parent Group ID #
	 *	- array[2] = (string) Group Value
	 *	- array[3] = (string) Group Name
	 *	- array[4] = (int) lft MPTT Value
	 *	- array[5] = (int) rgt MPTT Value
	 *
	 * @param int Group ID #
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function get_group_data($group_id, $group_type = 'ARO') {

		$this->debug_text("get_group_data(): Group_ID: $group_id Group Type: $group_type");

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		if (empty($group_id) ) {
			$this->debug_text("get_group_data(): ID ($group_id) is empty, this is required");
			return false;
		}

		$query  = 'SELECT id, parent_id, value, name, lft, rgt FROM '. $table .' WHERE id='. $group_id;
		//$rs = $this->db->Execute($query);
		$row = $this->db->GetRow($query);

		if ($row) {
			return $row;
		}

		$this->debug_text("get_object_data(): Group does not exist.");
		return false;
	}

	/**
	 * get_group_parent_id()
	 *
	 * Grabs the parent_id of a given group
	 *
	 * @return int Parent ID of the Group
	 *
	 * @param int Group ID #
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function get_group_parent_id($id, $group_type='ARO') {

		$this->debug_text("get_group_parent_id(): ID: $id Group Type: $group_type");

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		if (empty($id) ) {
			$this->debug_text("get_group_parent_id(): ID ($id) is empty, this is required");
			return false;
		}

		$query = 'SELECT parent_id FROM '. $table .' WHERE id='. $id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_group_parent_id');
			return false;
		}

		$row_count = $rs->RecordCount();

		if ($row_count > 1) {
			$this->debug_text("get_group_parent_id(): Returned $row_count rows, can only return one. Please make your names unique.");
			return false;
		}

		if ($row_count == 0) {
			$this->debug_text("get_group_parent_id(): Returned $row_count rows");
			return false;
		}

		$row = $rs->FetchRow();

		//Return the ID.
		return $row[0];
	}


	/**
	 * get_root_group_id ()
	 *
	 * Grabs the id of the root group for the specified tree
	 *
	 * @return int Root Group ID #
	 *
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function get_root_group_id($group_type='ARO') {

		$this->debug_text('get_root_group_id(): Group Type: '. $group_type);

		switch (strtolower($group_type)) {
			case 'axo':
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			case 'aro':
				$table = $this->_db_table_prefix .'aro_groups';
				break;
			default:
				$this->debug_text('get_root_group_id(): Invalid Group Type: '. $group_type);
				return FALSE;
		}

		$query = 'SELECT id FROM '. $table .' WHERE parent_id=0';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_root_group_id');
			return FALSE;
		}

		$row_count = $rs->RecordCount();

		switch ($row_count) {
			case 1:
				$row = $rs->FetchRow();
				// Return the ID.
				return $row[0];
			case 0:
				$this->debug_text('get_root_group_id(): Returned 0 rows, you do not have a root group defined yet.');
				return FALSE;
		}

		$this->debug_text('get_root_group_id(): Returned '. $row_count .' rows, can only return one. Your tree is very broken.');
		return FALSE;
	}

	/*======================================================================*\
		Function:	map_path_to_root()
		Purpose:	Maps a unique path to root to a specific group. Each group can only have
						one path to root.
	\*======================================================================*/
	/** REMOVED **/
	/*======================================================================*\
		Function:	put_path_to_root()
		Purpose:	Writes the unique path to root to the database. There should really only be
						one path to root for each level "deep" the groups go. If the groups are branched
						10 levels deep, there should only be 10 unique path to roots. These of course
						overlap each other more and more the closer to the root/trunk they get.
	\*======================================================================*/
	/** REMOVED **/
	/*======================================================================*\
		Function:	clean_path_to_root()
		Purpose:	Cleans up any paths that are not being used.
	\*======================================================================*/
	/** REMOVED **/
	/*======================================================================*\
		Function:	get_path_to_root()
		Purpose:	Generates the path to root for a given group.
	\*======================================================================*/
	/** REMOVED **/

	/**
	 * add_group()
	 *
	 * Inserts a group, defaults to be on the "root" branch.
	 *
	 * Since v3.3.x you can only create one group with Parent_ID=0
	 * So, its a good idea to create a "Virtual Root" group with Parent_ID=0
	 * Then assign other groups to that.
	 *
	 * @return int New Group ID # if successful, FALSE if otherwise.
	 *
	 * @param string Group Value
	 * @param string Group Name
	 * @param int Parent Group ID #
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function add_group($value, $name, $parent_id=0, $group_type='ARO') {

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		$this->debug_text("add_group(): Name: $name Value: $value Parent ID: $parent_id Group Type: $group_type");

		$name = trim($name);
		$value = trim($value);

		if ( $name == '' ) {
			$this->debug_text("add_group(): name ($name) OR parent id ($parent_id) is empty, this is required");
			return false;
		}

		//This has to be outside the transaction, because the first time it is run, it will say the sequence
		//doesn't exist. Then try to create it, but the transaction will already by aborted by then.
		$insert_id = $this->db->GenID($this->_db_table_prefix.$group_type.'_groups_id_seq',10);
		if ( $value === '' ) {
			$value = $insert_id;
		}

		$this->db->BeginTrans();

		// special case for root group
		if ($parent_id == 0) {
			// check a root group is not already defined
			$query = 'SELECT id FROM '. $table .' WHERE parent_id=0';
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('add_group');
				$this->db->RollBackTrans();
				return FALSE;
			}

			if ($rs->RowCount() > 0) {
				$this->debug_text('add_group (): A root group already exists.');
				$this->db->RollBackTrans();
				return FALSE;
			}

			$parent_lft = 0;
			$parent_rgt = 1;
		} else {
			if (empty($parent_id)) {
				$this->debug_text("add_group (): parent id ($parent_id) is empty, this is required");
				$this->db->RollbackTrans();
				return FALSE;
			}

			// grab parent details from database
			$query = 'SELECT id, lft, rgt FROM '. $table .' WHERE id='. $parent_id;
			$row = $this->db->GetRow($query);

			if (!is_array($row)) {
				$this->debug_db('add_group');
				$this->db->RollBackTrans();
				return FALSE;
			}

			if (empty($row)) {
				$this->debug_text('add_group (): Parent ID: '. $parent_id .' not found.');
				$this->db->RollBackTrans();
				return FALSE;
			}

			$parent_lft = &$row[1];
			$parent_rgt = &$row[2];

			// make room for the new group
			$query  = 'UPDATE '. $table .' SET rgt=rgt+2 WHERE rgt>='. $parent_rgt;
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('add_group');
				$this->db->RollBackTrans();
				return FALSE;
			}

			$query  = 'UPDATE '. $table .' SET lft=lft+2 WHERE lft>'. $parent_rgt;
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('add_group');
				$this->db->RollBackTrans();
				return FALSE;
			}
		}

		$query = 'INSERT INTO '. $table .' (id,parent_id,name,value,lft,rgt) VALUES ('. $insert_id .','. $parent_id .','. $this->db->quote($name) .','. $this->db->quote($value) .','. $parent_rgt .','. ($parent_rgt + 1) .')';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('add_group');
			$this->db->RollBackTrans();
			return FALSE;
		}

		$this->db->CommitTrans();

		$this->debug_text('add_group (): Added group as ID: '. $insert_id);
		return $insert_id;
	}

	/**
	 * get_group_objects()
	 *
	 * Gets all objects assigned to a group.
	 *
	 * If $option == 'RECURSE' it will get all objects in child groups as well.
	 * defaults to omit child groups.
	 *
	 * @return array Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]

	 *
	 * @param int Group ID #
	 * @param string Group Type, either 'ARO' or 'AXO'
	 * @param string Option, either 'RECURSE' or 'NO_RECURSE'
	 */
	function get_group_objects($group_id, $group_type='ARO', $option='NO_RECURSE') {

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$object_table = $this->_db_table_prefix .'axo';
				$group_table = $this->_db_table_prefix .'axo_groups';
				$map_table = $this->_db_table_prefix .'groups_axo_map';
				break;
			default:
				$group_type = 'aro';
				$object_table = $this->_db_table_prefix .'aro';
				$group_table = $this->_db_table_prefix .'aro_groups';
				$map_table = $this->_db_table_prefix .'groups_aro_map';
				break;
		}

		$this->debug_text("get_group_objects(): Group ID: $group_id");

		if (empty($group_id)) {
			$this->debug_text("get_group_objects(): Group ID:  ($group_id) is empty, this is required");
			return false;
		}

		$query  = '
				SELECT		o.section_value,o.value';

		if ($option == 'RECURSE') {
			$query .= '
				FROM		'. $group_table .' g2
				JOIN		'. $group_table .' g1 ON g1.lft>=g2.lft AND g1.rgt<=g2.rgt
				JOIN		'. $map_table .' gm ON gm.group_id=g1.id
				JOIN		'. $object_table .' o ON o.id=gm.'. $group_type .'_id
				WHERE		g2.id='. $group_id;
		} else {
			$query .= '
				FROM		'. $map_table .' gm
				JOIN		'. $object_table .' o ON o.id=gm.'. $group_type .'_id
				WHERE		gm.group_id='. $group_id;
		}

		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_group_objects');
			return false;
		}

		$this->debug_text("get_group_objects(): Got group objects, formatting array.");

		$retarr = array();

		//format return array.
		while ($row = $rs->FetchRow()) {
			$section = &$row[0];
			$value = &$row[1];

			$retarr[$section][] = $value;
		}

		return $retarr;
	}

	/**
	 * add_group_object()
	 *
	 * Assigns an Object to a group
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise.
	 *
	 * @param int Group ID #
	 * @param string Object Section Value
	 * @param string Object Value
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function add_group_object($group_id, $object_section_value, $object_value, $group_type='ARO') {

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'groups_axo_map';
				$object_table = $this->_db_table_prefix .'axo';
				$group_table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'groups_aro_map';
				$object_table = $this->_db_table_prefix .'aro';
				$group_table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		$this->debug_text("add_group_object(): Group ID: $group_id Section Value: $object_section_value Value: $object_value Group Type: $group_type");

		$object_section_value = trim($object_section_value);
		$object_value = trim($object_value);

		if (empty($group_id) OR empty($object_value) OR empty($object_section_value)) {
			$this->debug_text("add_group_object(): Group ID: ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is empty, this is required");
			return false;
		}

		// test to see if object & group exist and if object is already a member
		$query  = '
				SELECT		o.id AS id,g.id AS group_id,gm.group_id AS member
				FROM		'. $object_table .' o
				LEFT JOIN	'. $group_table .' g ON g.id='. $group_id .'
				LEFT JOIN	'. $table .' gm ON (gm.'. $group_type .'_id=o.id AND gm.group_id=g.id)
				WHERE		(o.section_value='. $this->db->quote($object_section_value) .' AND o.value='. $this->db->quote($object_value) .')';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('add_group_object');
			return FALSE;
		}

		if ($rs->RecordCount() != 1) {
			$this->debug_text('add_group_object(): Value ('. $object_value .') OR Section value ('. $object_section_value .') is invalid. Does this object exist?');
			return FALSE;
		}

		$row = $rs->FetchRow();

		if ($row[1] != $group_id) {
			$this->debug_text('add_group_object(): Group ID ('. $group_id .') is invalid. Does this group exist?');
			return FALSE;
		}

		//Group_ID == Member
		if ($row[1] == $row[2]) {
			$this->debug_text('add_group_object(): Object: ('. $object_section_value .' -> '. $object_value .') is already a member of Group: ('. $group_id .')');
			//Object is already assigned to group. Return true.
			return TRUE;
		}

		$object_id = $row[0];

		$query = 'INSERT INTO '. $table .' (group_id,'. $group_type .'_id) VALUES ('. $group_id .','. $object_id .')';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('add_group_object');
			return FALSE;
		}

		$this->debug_text('add_group_object(): Added Object: '. $object_id .' to Group ID: '. $group_id);

		if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) {
			//Expire all cache.
			$this->Cache_Lite->clean('default');
		}

		return TRUE;
	}

	/**
	 * del_group_object()
	 *
	 * Removes an Object from a group.
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise
	 *
	 * @param int Group ID #
	 * @param string Object Section Value
	 * @param string Object Value
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function del_group_object($group_id, $object_section_value, $object_value, $group_type='ARO') {

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'groups_axo_map';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'groups_aro_map';
				break;
		}

		$this->debug_text("del_group_object(): Group ID: $group_id Section value: $object_section_value Value: $object_value");

		$object_section_value = trim($object_section_value);
		$object_value = trim($object_value);

		if (empty($group_id) OR empty($object_value) OR empty($object_section_value)) {
			$this->debug_text("del_group_object(): Group ID:  ($group_id) OR Section value: $object_section_value OR Value ($object_value) is empty, this is required");
			return false;
		}

 		if (!$object_id = $this->get_object_id($object_section_value, $object_value, $group_type)) {
			$this->debug_text ("del_group_object (): Group ID ($group_id) OR Value ($object_value) OR Section value ($object_section_value) is invalid. Does this object exist?");
			return FALSE;
		}

		$query = 'DELETE FROM '. $table .' WHERE group_id='. $group_id .' AND '. $group_type .'_id='. $object_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('del_group_object');
			return false;
		}

		$this->debug_text("del_group_object(): Deleted Value: $object_value to Group ID: $group_id assignment");

		if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) {
			//Expire all cache.
			$this->Cache_Lite->clean('default');
		}

		return true;
	}

	/**
	 * edit_group()
	 *
	 * Edits a group
	 *
	 * @returns bool Returns TRUE if successful, FALSE otherwise
	 *
	 * @param int Group ID #
	 * @param string Group Value
	 * @param string Group Name
	 * @param int Parent ID #
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function edit_group($group_id, $value=NULL, $name=NULL, $parent_id=NULL, $group_type='ARO') {
		$this->debug_text("edit_group(): ID: $group_id Name: $name Value: $value Parent ID: $parent_id Group Type: $group_type");

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		if (empty($group_id) ) {
			$this->debug_text('edit_group(): Group ID ('. $group_id .') is empty, this is required');
			return FALSE;
		}

		if ( !is_array($curr = $this->get_group_data($group_id, $group_type)) ) {
			$this->debug_text('edit_group(): Invalid Group ID: '. $group_id);
			return FALSE;
		}

		$name = trim($name);

		// don't set name if it is unchanged
		if ($name == $curr[3]) {
			unset($name);
		}

		// don't set parent_id if it is unchanged
		if ($parent_id == $curr[1]) {
			unset($parent_id);
		}

		if (!empty($parent_id)) {
			if ($group_id == $parent_id) {
				$this->debug_text('edit_group(): Groups can\'t be a parent to themselves. Incest is bad. ;)');
				return FALSE;
			}

			//Make sure we don't re-parent to our own children.
			//Grab all children of this group_id.
			$children_ids = $this->get_group_children($group_id, $group_type, 'RECURSE');
			if (is_array($children_ids)) {
				if (@in_array($parent_id, $children_ids) ) {
					$this->debug_text('edit_group(): Groups can\'t be re-parented to their own children, this would be incestuous!');
					return FALSE;
				}
			}
			unset($children_ids);

			// make sure parent exists
			if (!$this->get_group_data($parent_id, $group_type)) {
				$this->debug_text('edit_group(): Parent Group ('. $parent_id .') doesn\'t exist');
				return FALSE;
			}
		}

		$set = array();

		// update name if it is specified.
		if (!empty($name)) {
			$set[] = 'name='. $this->db->quote($name);
		}

		// update parent_id if it is specified.
		if (!empty($parent_id)) {
			$set[] = 'parent_id='. $parent_id;
		}

		// update value if it is specified.
		if (!empty($value)) {
			$set[] = 'value='. $this->db->quote($value);
		}

		if (empty($set)) {
			$this->debug_text('edit_group(): Nothing to update.');
			return FALSE;
		}

		$this->db->BeginTrans();

		$query  = 'UPDATE '. $table .' SET '. implode(',', $set) .' WHERE id='. $group_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('edit_group');
			$this->db->RollbackTrans();
			return FALSE;
		}

		$this->debug_text('edit_group(): Modified group ID: '. $group_id);

		// rebuild group tree if parent_id has changed
		if (!empty($parent_id)) {
			if (!$this->_rebuild_tree($table, $this->get_root_group_id($group_type))) {
				$this->db->RollbackTrans();
				return FALSE;
			}
		}

		$this->db->CommitTrans();

		if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) {
			// Expire all cache.
			$this->Cache_Lite->clean('default');
		}

		return TRUE;
	}

	/**
	 * rebuild_tree ()
	 *
	 * rebuilds the group tree for the given type
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise
	 *
	 * @param string Group Type, either 'ARO' or 'AXO'
	 * @param int Group ID #
	 * @param int Left value of Group
	 */
	function rebuild_tree($group_type = 'ARO', $group_id = NULL, $left = 1) {
		$this->debug_text("rebuild_tree(): Group Type: $group_type Group ID: $group_id Left: $left");

		switch (strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'axo_groups';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'aro_groups';
				break;
		}

		if (!isset($group_id)) {
			if ($group_id = $this->get_root_group_id($group_type)) {
				$left = 1;
				$this->debug_text('rebuild_tree(): No Group ID Specified, using Root Group ID: '. $group_id);
			} else {
				$this->debug_text('rebuild_tree(): A Root group could not be found, are there any groups defined?');
				return FALSE;
			}
		}

		$this->db->BeginTrans();
		$rebuilt = $this->_rebuild_tree($table, $group_id, $left);

		if ($rebuilt === FALSE) {
			$this->debug_text('rebuild_tree(): Error rebuilding tree!');
			$this->db->RollBackTrans();
			return FALSE;
		}

		$this->db->CommitTrans();
		$this->debug_text('rebuild_tree(): Tree rebuilt.');
		return TRUE;
	}
	/**
	 * _rebuild_tree ()
	 *
	 * Utility recursive function called by rebuild_tree()
	 *
	 * @return int Returns right value of this node + 1
	 *
	 * @param string Table name of group type
	 * @param int Group ID #
	 * @param int Left value of Group
	 */
	function _rebuild_tree($table, $group_id, $left = 1) {
		$this->debug_text("_rebuild_tree(): Table: $table Group ID: $group_id Left: $left");

		// get all children of this node
		$query = 'SELECT id FROM '. $table .' WHERE parent_id='. $group_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('_rebuild_tree');
			return FALSE;
		}

		// the right value of this node is the left value + 1
		$right = $left + 1;

		while ($row = $rs->FetchRow()) {
			// recursive execution of this function for each
			// child of this node
			// $right is the current right value, which is
			// incremented by the rebuild_tree function
			$right = $this->_rebuild_tree($table, $row[0], $right);

			if ($right === FALSE) {
				return FALSE;
			}
		}

		// we've got the left value, and now that we've processed
		// the children of this node we also know the right value
		$query  = 'UPDATE '. $table .' SET lft='. $left .', rgt='. $right .' WHERE id='. $group_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('_rebuild_tree');
			return FALSE;
		}

		// return the right value of this node + 1
		return $right + 1;
	}

	/**
	 * del_group()
	 *
	 * deletes a given group
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise.
	 *
	 * @param int Group ID #
	 * @param bool If TRUE, child groups of this group will be reparented to the current group's parent.
	 * @param string Group Type, either 'ARO' or 'AXO'
	 */
	function del_group($group_id, $reparent_children=TRUE, $group_type='ARO') {

		switch(strtolower(trim($group_type))) {
			case 'axo':
				$group_type = 'axo';
				$table = $this->_db_table_prefix .'axo_groups';
				$groups_map_table = $this->_db_table_prefix .'axo_groups_map';
				$groups_object_map_table = $this->_db_table_prefix .'groups_axo_map';
				break;
			default:
				$group_type = 'aro';
				$table = $this->_db_table_prefix .'aro_groups';
				$groups_map_table = $this->_db_table_prefix .'aro_groups_map';
				$groups_object_map_table = $this->_db_table_prefix .'groups_aro_map';
				break;
		}

		$this->debug_text("del_group(): ID: $group_id Reparent Children: $reparent_children Group Type: $group_type");

		if (empty($group_id) ) {
			$this->debug_text("del_group(): Group ID ($group_id) is empty, this is required");
			return false;
		}

		// Get details of this group
		$query = 'SELECT id, parent_id, name, lft, rgt FROM '. $table .' WHERE id='. $group_id;
		$group_details = $this->db->GetRow($query);

		if (!is_array($group_details)) {
			$this->debug_db('del_group');
			return false;
		}

		$parent_id = $group_details[1];

		$left = $group_details[3];
		$right = $group_details[4];

		$this->db->BeginTrans();

		// grab list of all children
		$children_ids = $this->get_group_children($group_id, $group_type, 'RECURSE');

		// prevent deletion of root group & reparent of children if it has more than one immediate child
		if ($parent_id == 0) {
			$query = 'SELECT count(*) FROM '. $table .' WHERE parent_id='. $group_id;
			$child_count = $this->db->GetOne($query);

			if (($child_count > 1) AND $reparent_children) {
				$this->debug_text ('del_group (): You cannot delete the root group and reparent children, this would create multiple root groups.');
				$this->db->RollbackTrans();
				return FALSE;
			}
		}

		$success = FALSE;

		/*
		 * Handle children here.
		 */
		switch (TRUE) {
			// there are no child groups, just delete group
			case !is_array($children_ids):
			case count($children_ids) == 0:
				// remove acl maps
				$query = 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// remove group object maps
				$query = 'DELETE FROM '. $groups_object_map_table .' WHERE group_id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// remove group
				$query = 'DELETE FROM '. $table .' WHERE id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// move all groups right of deleted group left by width of deleted group
				$query = 'UPDATE '. $table .' SET lft=lft-'. ($right-$left+1) .' WHERE lft>'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				$query = 'UPDATE '. $table .' SET rgt=rgt-'. ($right-$left+1) .' WHERE rgt>'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				$success = TRUE;
				break;
			case $reparent_children == TRUE:
				// remove acl maps
				$query = 'DELETE FROM '. $groups_map_table .' WHERE group_id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// remove group object maps
				$query = 'DELETE FROM '. $groups_object_map_table .' WHERE group_id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// remove group
				$query = 'DELETE FROM '. $table .' WHERE id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// set parent of immediate children to parent group
				$query = 'UPDATE '. $table .' SET parent_id='. $parent_id .' WHERE parent_id='. $group_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// move all children left by 1
				$query = 'UPDATE '. $table .' SET lft=lft-1, rgt=rgt-1 WHERE lft>'. $left .' AND rgt<'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// move all groups right of deleted group left by 2
				$query = 'UPDATE '. $table .' SET lft=lft-2 WHERE lft>'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				$query = 'UPDATE '. $table .' SET rgt=rgt-2 WHERE rgt>'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				$success = TRUE;
				break;
			default:
				// make list of group and all children
				$group_ids = $children_ids;
				$group_ids[] = $group_id;

				// remove acl maps
				$query = 'DELETE FROM '. $groups_map_table .' WHERE group_id IN ('. implode (',', $group_ids) .')';
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// remove group object maps
				$query = 'DELETE FROM '. $groups_object_map_table .' WHERE group_id IN ('. implode (',', $group_ids) .')';
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// remove groups
				$query = 'DELETE FROM '. $table .' WHERE id IN ('. implode (',', $group_ids) .')';
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				// move all groups right of deleted group left by width of deleted group
				$query = 'UPDATE '. $table .' SET lft=lft-'. ($right - $left + 1) .' WHERE lft>'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				$query = 'UPDATE '. $table .' SET rgt=rgt-'. ($right - $left + 1) .' WHERE rgt>'. $right;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					break;
				}

				$success = TRUE;
		}

		// if the delete failed, rollback the trans and return false
		if (!$success) {

			$this->debug_db('del_group');
			$this->db->RollBackTrans();
			return false;
		}

		$this->debug_text("del_group(): deleted group ID: $group_id");
		$this->db->CommitTrans();

		if ($this->_caching == TRUE AND $this->_force_cache_expire == TRUE) {
			//Expire all cache.
			$this->Cache_Lite->clean('default');
		}

		return true;

	}


	/*
	 *
	 * Objects (ACO/ARO/AXO)
	 *
	 */

	/**
	 * get_object()
	 *
	 * Grabs all Objects's in the database, or specific to a section_value
	 *
	 * @return ADORecordSet  Returns recordset directly, with object ID only selected:
	 *
	 * @param string Filter to this section value
	 * @param int Returns hidden objects if 1, leaves them out otherwise.
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO', or 'ACL'
	 */
	function get_object($section_value = null, $return_hidden=1, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				break;
			case 'acl':
				$object_type = 'acl';
				$table = $this->_db_table_prefix .'acl';
				break;
			default:
				$this->debug_text('get_object(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("get_object(): Section Value: $section_value Object Type: $object_type");

		$query = 'SELECT id FROM '. $table;

		$where = array();

		if (!empty($section_value)) {
			$where[] = 'section_value='. $this->db->quote($section_value);
		}

		if ($return_hidden==0 AND $object_type != 'acl') {
			$where[] = 'hidden=0';
		}

		if (!empty($where)) {
			$query .= ' WHERE '. implode(' AND ', $where);
		}

		$rs = $this->db->GetCol($query);

		if (!is_array($rs)) {
			$this->debug_db('get_object');
			return false;
		}

		// Return Object IDs
		return $rs;
	}
	/**
	 * get_ungrouped_objects()
	 *
	 * Grabs ID's of all Objects (ARO's and AXO's only) in the database not assigned to a Group.
	 *
	 * This function is useful for applications that synchronize user databases with an outside source.
	 * If syncrhonization doesn't automatically place users in an appropriate group, this function can
	 * quickly identify them so that they can be assigned to the correct group.
	 *
	 * @return array Returns an array of object ID's
	 *
	 * @param int Returns hidden objects if 1, does not if 0.
	 * @param string Object Type, either 'ARO' or 'AXO' (groupable types)
	 */

	function get_ungrouped_objects($return_hidden=1, $object_type=NULL) {

		   switch(strtolower(trim($object_type))) {
				   case 'aro':
						   $object_type = 'aro';
						   $table = $this->_db_table_prefix .'aro';
						   break;
				   case 'axo':
						   $object_type = 'axo';
						   $table = $this->_db_table_prefix .'axo';
						   break;
				   default:
						   $this->debug_text('get_ungrouped_objects(): Invalid Object Type: '. $object_type);
						   return FALSE;
		   }

		   $this->debug_text("get_ungrouped_objects(): Object Type: $object_type");

			$query = 'SELECT id FROM '. $table. ' a
							LEFT JOIN ' . $this->_db_table_prefix. 'groups_'.$object_type.'_map b ON a.id = b.'. $object_type .'_id';

		   $where = array();
		   $where[] = 'b.group_id IS NULL';

		   if ($return_hidden==0) {
				   $where[] = 'a.hidden=0';
		   }

		   if (!empty($where)) {
				   $query .= ' WHERE '. implode(' AND ', $where);
		   }

		   $rs = $this->db->Execute($query);

		   if (!is_object($rs)) {
				   $this->debug_db('get_ungrouped_objects');
				   return false;
		   }

		   while(!$rs->EOF) {
				   $retarr[] = $rs->fields[0];
				   $rs->MoveNext();
		   }

		   // Return Array of object IDS
		   return $retarr;
	}


	/**
	 * get_objects ()
	 *
	 * Grabs all Objects in the database, or specific to a section_value
	 *
	 * @return array Returns objects in format suitable for add_acl and is_conflicting_acl
	 *	- i.e. Associative array, item={Section Value}, key={Array of Object Values} i.e. ["<Section Value>" => ["<Value 1>", "<Value 2>", "<Value 3>"], ...]
	 *
	 * @param string Filter for section value
	 * @param int Returns hidden objects if 1, does not if 0
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO'
	 */
	function get_objects($section_value = NULL, $return_hidden = 1, $object_type = NULL) {
		switch (strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				break;
			default:
				$this->debug_text('get_objects(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("get_objects(): Section Value: $section_value Object Type: $object_type");

		$query = 'SELECT section_value,value FROM '. $table;

		$where = array();

		if (!empty($section_value)) {
			$where[] = 'section_value='. $this->db->quote($section_value);
		}

		if ($return_hidden==0) {
			$where[] = 'hidden=0';
		}

		if (!empty($where)) {
			$query .= ' WHERE '. implode(' AND ', $where);
		}

		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_objects');
			return FALSE;
		}

		$retarr = array();

		while ($row = $rs->FetchRow()) {
			$retarr[$row[0]][] = $row[1];
		}

		// Return objects
		return $retarr;
	}

	/**
	 * get_object_data()
	 *
	 * Gets all data pertaining to a specific Object.
	 *
	 * @return array Returns 2-Dimensional array of rows with columns = ( section_value, value, order_value, name, hidden )
	 *
	 * @param int Object ID #
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO'
	 */
	function get_object_data($object_id, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				break;
			default:
				$this->debug_text('get_object_data(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("get_object_data(): Object ID: $object_id Object Type: $object_type");

		if (empty($object_id) ) {
			$this->debug_text("get_object_data(): Object ID ($object_id) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("get_object_data(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$query  = 'SELECT section_value,value,order_value,name,hidden FROM '. $table .' WHERE id='. $object_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_object_data');
			return false;
		}

		if ($rs->RecordCount() < 1) {
			$this->debug_text('get_object_data(): Returned  '. $row_count .' rows');
			return FALSE;
		}

		// Return all objects
		return $rs->GetRows();
	}

	/**
	 * get_object_id()
	 *
	 * Gets the object_id given the section_value AND value of the object.
	 *
	 * @return int Object ID #
	 *
	 * @param string Object Section Value
	 * @param string Object Value
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO'
	 */
	function get_object_id($section_value, $value, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				break;
			default:
				$this->debug_text('get_object_id(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("get_object_id(): Section Value: $section_value Value: $value Object Type: $object_type");

		$section_value = trim($section_value);
		$value = trim($value);

		if (empty($section_value) AND empty($value) ) {
			$this->debug_text("get_object_id(): Section Value ($value) AND value ($value) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("get_object_id(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$query = 'SELECT id FROM '. $table .' WHERE section_value='. $this->db->quote($section_value) .' AND value='. $this->db->quote($value);
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_object_id');
			return false;
		}

		$row_count = $rs->RecordCount();

		if ($row_count > 1) {
			$this->debug_text("get_object_id(): Returned $row_count rows, can only return one. This should never happen, the database may be missing a unique key.");
			return false;
		}

		if ($row_count == 0) {
			$this->debug_text("get_object_id(): Returned $row_count rows");
			return false;
		}

		$row = $rs->FetchRow();

		//Return the ID.
		return $row[0];
	}

	/**
	 * get_object_section_value()
	 *
	 * Gets the object_section_value given object id
	 *
	 * @return string Object Section Value
	 *
	 * @param int Object ID #
	 * @param string Object Type, either 'ACO', 'ARO', or 'AXO'
	 */
	function get_object_section_value($object_id, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				break;
			default:
				$this->debug_text('get_object_section_value(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("get_object_section_value(): Object ID: $object_id Object Type: $object_type");

		if (empty($object_id) ) {
			$this->debug_text("get_object_section_value(): Object ID ($object_id) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("get_object_section_value(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$query = 'SELECT section_value FROM '. $table .' WHERE id='. $object_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_object_section_value');
			return false;
		}

		$row_count = $rs->RecordCount();

		if ($row_count > 1) {
			$this->debug_text("get_object_section_value(): Returned $row_count rows, can only return one.");
			return false;
		}

		if ($row_count == 0) {
			$this->debug_text("get_object_section_value(): Returned $row_count rows");
			return false;
		}

		$row = $rs->FetchRow();

		//Return the ID.
		return $row[0];
	}

	/**
	 * get_object_groups()
	 *
	 * Gets all groups an object is a member of.
	 *
	 * If $option == 'RECURSE' it will get all ancestor groups.
	 * defaults to only get direct parents.
	 *
	 * @return array Array of Group ID #'s, or FALSE if Failed
	 *
	 * @param int Object ID #
	 * @param string Object Type, either 'ARO' or 'AXO'
	 * @param string Option, either 'RECURSE', or 'NO_RECURSE'
	 */
	function get_object_groups($object_id, $object_type = 'ARO', $option = 'NO_RECURSE') {
		$this->debug_text('get_object_groups(): Object ID: '. $object_id .' Object Type: '. $object_type .' Option: '. $option);

		switch(strtolower(trim($object_type))) {
			case 'axo':
				$object_type = 'axo';
				$group_table = $this->_db_table_prefix .'axo_groups';
				$map_table = $this->_db_table_prefix .'groups_axo_map';
				break;
			case 'aro':
				$object_type = 'aro';
				$group_table = $this->_db_table_prefix .'aro_groups';
				$map_table = $this->_db_table_prefix .'groups_aro_map';
				break;
			default:
				$this->debug_text('get_object_groups(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		if (empty($object_id)) {
			$this->debug_text('get_object_groups(): Object ID: ('. $object_id .') is empty, this is required');
			return FALSE;
		}

		if (strtoupper($option) == 'RECURSE') {
		    $query = '
				SELECT		DISTINCT g.id AS group_id
				FROM		'. $map_table .' gm
				LEFT JOIN	'. $group_table .' g1 ON g1.id=gm.group_id
				LEFT JOIN	'. $group_table .' g ON g.lft<=g1.lft AND g.rgt>=g1.rgt';
		} else {
		    $query = '
		    	SELECT		gm.group_id
		    	FROM		'. $map_table .' gm';
		}

		$query .= '
				WHERE		gm.'. $object_type .'_id='. $object_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_object_groups');
			return FALSE;
		}

		$retarr = array();

		while ($row = $rs->FetchRow()) {
			$retarr[] = $row[0];
		}

		return $retarr;
	}

	/**
	 * add_object()
	 *
	 * Inserts a new object
	 *
	 * @return int Returns the ID # of the new object if successful, FALSE otherwise
	 *
	 * @param string Object Section Value
	 * @param string Object Name
	 * @param string Object Value
	 * @param int Display Order
	 * @param int Hidden Flag, either 1 to hide, or 0 to show.
	 * @param string Object Type, either 'ACO', 'ARO', or 'AXO'
	 */
	function add_object($section_value, $name, $value=0, $order=0, $hidden=0, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				$object_sections_table = $this->_db_table_prefix .'aco_sections';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				$object_sections_table = $this->_db_table_prefix .'aro_sections';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				$object_sections_table = $this->_db_table_prefix .'axo_sections';
				break;
			default:
				$this->debug_text('add_object(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("add_object(): Section Value: $section_value Value: $value Order: $order Name: $name Object Type: $object_type");

		$section_value = trim($section_value);
		$name = trim($name);
		$value = trim($value);
		$order = trim($order);
		$hidden = intval($hidden);

		if ($order == NULL OR $order == '') {
			$order = 0;
		}

		if (empty($name) OR empty($section_value) ) {
			$this->debug_text("add_object(): name ($name) OR section value ($section_value) is empty, this is required");
			return false;
		}

		if (strlen($name) >= 255 OR strlen($value) >= 230 ) {
			$this->debug_text("add_object(): name ($name) OR value ($value) is too long.");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("add_object(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		// Test to see if the section is invalid or object already exists.
		$query  = '
			SELECT		CASE WHEN o.id IS NULL THEN 0 ELSE 1 END AS object_exists
			FROM		'. $object_sections_table .' s
			LEFT JOIN	'. $table .' o ON (s.value=o.section_value AND o.value='. $this->db->quote($value) .')
			WHERE		s.value='. $this->db->quote($section_value);
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('add_object');
			return FALSE;
		}

		if ($rs->RecordCount() != 1) {
			// Section is invalid
			$this->debug_text("add_object(): Section Value: $section_value Object Type ($object_type) does not exist, this is required");
			return false;
		}

		$row = $rs->FetchRow();

		if ($row[0] == 1) {
			//Object is already created.
			return true;
		}

		$insert_id = $this->db->GenID($this->_db_table_prefix.$object_type.'_seq',10);
		$query = 'INSERT INTO '. $table .' (id,section_value,value,order_value,name,hidden) VALUES('. $insert_id .','. $this->db->quote($section_value) .','. $this->db->quote($value) .','. $order .','. $this->db->quote($name) .','. $hidden .')';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('add_object');
			return false;
		}

		$this->debug_text("add_object(): Added object as ID: $insert_id");
		return $insert_id;
	}

	/**
	 * edit_object()
	 *
	 * Edits a given Object
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise
	 *
	 * @param int Object ID #
	 * @param string Object Section Value
	 * @param string Object Name
	 * @param string Object Value
	 * @param int Display Order
	 * @param int Hidden Flag, either 1 to hide, or 0 to show
	 * @param string Object Type, either 'ACO', 'ARO', or 'AXO'
	 */
	function edit_object($object_id, $section_value, $name, $value=0, $order=0, $hidden=0, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				$object_map_table = $this->_db_table_prefix .'aco_map';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				$object_map_table = $this->_db_table_prefix .'aro_map';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				$object_map_table = $this->_db_table_prefix .'axo_map';
				break;
		}

		$this->debug_text("edit_object(): ID: $object_id Section Value: $section_value Value: $value Order: $order Name: $name Object Type: $object_type");

		$section_value = trim($section_value);
		$name = trim($name);
		$value = trim($value);
		$order = trim($order);
		$hidden = intval($hidden);

		if (empty($object_id) OR empty($section_value) ) {
			$this->debug_text("edit_object(): Object ID ($object_id) OR Section Value ($section_value) is empty, this is required");
			return false;
		}

		if (empty($name) ) {
			$this->debug_text("edit_object(): name ($name) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("edit_object(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$this->db->BeginTrans();

		//Get old value incase it changed, before we do the update.
		$query = 'SELECT value, section_value FROM '. $table .' WHERE id='. $object_id;
		$old = $this->db->GetRow($query);

		$query  = '
			UPDATE	'. $table .'
			SET		section_value='. $this->db->quote($section_value) .',
					value='. $this->db->quote($value) .',
					order_value='. $this->db->quote($order) .',
					name='. $this->db->quote($name) .',
					hidden='. $hidden .'
			WHERE	id='. $object_id;
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('edit_object');
			$this->db->RollbackTrans();
			return false;
		}

		$this->debug_text('edit_object(): Modified '. strtoupper($object_type) .' ID: '. $object_id);

		if ($old[0] != $value OR $old[1] != $section_value) {
			$this->debug_text("edit_object(): Value OR Section Value Changed, update other tables.");

			$query  = '
				UPDATE	'. $object_map_table .'
				SET		value='. $this->db->quote($value) .',
						section_value='. $this->db->quote($section_value) .'
				WHERE	section_value='. $this->db->quote($old[1]) .'
					AND	value='. $this->db->quote($old[0]);
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('edit_object');
				$this->db->RollbackTrans();
				return FALSE;
			}

			$this->debug_text ('edit_object(): Modified Map Value: '. $value .' Section Value: '. $section_value);
		}

		$this->db->CommitTrans();

		return TRUE;
	}

	/**
	 * del_object()
	 *
	 * Deletes a given Object and, if instructed to do so, erase all referencing objects
	 *
	 * ERASE feature by: Martino Piccinato
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise.
	 *
	 * @param int Object ID #
	 * @param string Object Type, either 'ACO', 'ARO', or 'AXO'
	 * @param bool Erases all referencing objects if TRUE, leaves them alone otherwise.
	 */
	function del_object($object_id, $object_type=NULL, $erase=FALSE) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				$object_map_table = $this->_db_table_prefix .'aco_map';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				$object_map_table = $this->_db_table_prefix .'aro_map';
				$groups_map_table = $this->_db_table_prefix .'aro_groups_map';
				$object_group_table = $this->_db_table_prefix .'groups_aro_map';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				$object_map_table = $this->_db_table_prefix .'axo_map';
				$groups_map_table = $this->_db_table_prefix .'axo_groups_map';
				$object_group_table = $this->_db_table_prefix .'groups_axo_map';
				break;
			default:
				$this->debug_text('del_object(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("del_object(): ID: $object_id Object Type: $object_type, Erase all referencing objects: $erase");

		if (empty($object_id) ) {
			$this->debug_text("del_object(): Object ID ($object_id) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("del_object(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$this->db->BeginTrans();

		// Get Object section_value/value (needed to look for referencing objects)
		$query = 'SELECT section_value,value FROM '. $table .' WHERE id='. $object_id;
		$object = $this->db->GetRow($query);

		if (empty($object)) {
			$this->debug_text('del_object(): The specified object ('. strtoupper($object_type) .' ID: '. $object_id .') could not be found.');
			$this->db->RollbackTrans();
			return FALSE;
		}

		$section_value = $object[0];
		$value = $object[1];

		// Get ids of acl referencing the Object (if any)
		$query = "SELECT acl_id FROM $object_map_table WHERE value='$value' AND section_value='$section_value'";
		$acl_ids = $this->db->GetCol($query);

		if ($erase) {
			// We were asked to erase all acl referencing it

			$this->debug_text("del_object(): Erase was set to TRUE, delete all referencing objects");

			if ($object_type == "aro" OR $object_type == "axo") {
				// The object can be referenced in groups_X_map tables
				// in the future this branching may become useless because
				// ACO might me "groupable" too

				// Get rid of groups_map referencing the Object
				$query = 'DELETE FROM '. $object_group_table .' WHERE '. $object_type .'_id='. $object_id;
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					$this->debug_db('edit_object');
					$this->db->RollBackTrans();
					return false;
				}
			}

			if (!empty($acl_ids)) {
				//There are acls actually referencing the object

				if ($object_type == 'aco') {
					// I know it's extremely dangerous but
					// if asked to really erase an ACO
					// we should delete all acl referencing it
					// (and relative maps)

					// Do this below this branching
					// where it uses $orphan_acl_ids as
					// the array of the "orphaned" acl
					// in this case all referenced acl are
					// orhpaned acl

					$orphan_acl_ids = $acl_ids;
				} else {
					// The object is not an ACO and might be referenced
					// in still valid acls regarding also other object.
					// In these cases the acl MUST NOT be deleted

					// Get rid of $object_id map referencing erased objects
					$query = "DELETE FROM $object_map_table WHERE section_value='$section_value' AND value='$value'";
					$this->db->Execute($query);

					if (!is_object($rs)) {
						$this->debug_db('edit_object');
						$this->db->RollBackTrans();
						return false;
					}

					// Find the "orphaned" acl. I mean acl referencing the erased Object (map)
					// not referenced anymore by other objects

					$sql_acl_ids = implode(",", $acl_ids);

					$query = '
						SELECT		a.id
						FROM		'. $this->_db_table_prefix .'acl a
						LEFT JOIN	'. $object_map_table .' b ON a.id=b.acl_id
						LEFT JOIN	'. $groups_map_table .' c ON a.id=c.acl_id
						WHERE		b.value IS NULL
							AND		b.section_value IS NULL
							AND		c.group_id IS NULL
							AND		a.id in ('. $sql_acl_ids .')';
					$orphan_acl_ids = $this->db->GetCol($query);

				} // End of else section of "if ($object_type == "aco")"

				if ($orphan_acl_ids) {
					// If there are orphaned acls get rid of them

					foreach ($orphan_acl_ids as $acl) {
						$this->del_acl($acl);
					}
				}

			} // End of if ($acl_ids)

			// Finally delete the Object itself
			$query = "DELETE FROM $table WHERE id='$object_id'";
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('edit_object');
				$this->db->RollBackTrans();
				return false;
			}

			$this->db->CommitTrans();
			return true;

		} // End of "if ($erase)"

		$groups_ids = FALSE;

		if ($object_type == 'axo' OR $object_type == 'aro') {
			// If the object is "groupable" (may become unnecessary,
			// see above

			// Get id of groups where the object is assigned:
			// you must explicitly remove the object from its groups before
			// deleting it (don't know if this is really needed, anyway it's safer ;-)

			$query = 'SELECT group_id FROM '. $object_group_table .' WHERE '. $object_type .'_id='. $object_id;
			$groups_ids = $this->db->GetCol($query);
		}

		if ( ( isset($acl_ids) AND !empty($acl_ids) ) OR ( isset($groups_ids) AND !empty($groups_ids) ) ) {
			// The Object is referenced somewhere (group or acl), can't delete it

			$this->debug_text("del_object(): Can't delete the object as it is being referenced by GROUPs (".@implode($groups_ids).") or ACLs (".@implode($acl_ids,",").")");
			$this->db->RollBackTrans();
			return false;
		} else {
			// The Object is NOT referenced anywhere, delete it

			$query = "DELETE FROM $table WHERE id='$object_id'";
			$rs = $this->db->Execute($query);

			if ( !is_object($rs) ) {
				$this->debug_db('edit_object');
				$this->db->RollBackTrans();
				return false;
			}

			$this->db->CommitTrans();
			return true;
		}

		$this->db->RollbackTrans();
		return false;
	}

	/*
	 *
	 * Object Sections
	 *
	 */

	/**
	 * get_object_section_section_id()
	 *
	 * Gets the object_section_id given the name AND/OR value of the section.
	 *
	 * Will only return one section id, so if there are duplicate names it will return false.
	 *
	 * @return int Object Section ID if the object section is found AND is unique, or FALSE otherwise.
	 *
	 * @param string Object Name
	 * @param string Object Value
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO', or 'ACL'
	 *
	 */
	function get_object_section_section_id($name = NULL, $value = NULL, $object_type = NULL) {
		$this->debug_text("get_object_section_section_id(): Value: $value Name: $name Object Type: $object_type");

		switch(strtolower(trim($object_type))) {
			case 'aco':
			case 'aro':
			case 'axo':
			case 'acl':
				$object_type = strtolower(trim($object_type));
				$table = $this->_db_table_prefix . $object_type;
				$object_sections_table = $this->_db_table_prefix . $object_type .'_sections';
				break;
			default:
				$this->debug_text('get_object_section_section_id(): Invalid Object Type ('. $object_type . ')');
				return FALSE;
		}

		$name = trim($name);
		$value = trim($value);

		if (empty($name) AND empty($value) ) {
			$this->debug_text('get_object_section_section_id(): Both Name ('. $name .') and Value ('. $value .') are empty, you must specify at least one.');
			return FALSE;
		}

		$query = 'SELECT id FROM '. $object_sections_table;
		$where = ' WHERE ';

		// limit by value if specified
		if (!empty($value)) {
			$query .= $where .'value='. $this->db->quote($value);
			$where = ' AND ';
		}

		// only use name if asked, this is SLOW
		if (!empty($name)) {
			$query .= $where .'name='. $this->db->quote($name);
		}

		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('get_object_section_section_id');
			return FALSE;
		}

		$row_count = $rs->RecordCount();

		// If only one row is returned
		if ($row_count == 1) {
			// Return only the ID in the first row.
			$row = $rs->FetchRow();
			return $row[0];
		}

		// If more than one row is returned
		// should only ever occur when using name as values are unique.
		if ($row_count > 1) {
			$this->debug_text('get_object_section_section_id(): Returned '. $row_count .' rows, can only return one. Please search by value not name, or make your names unique.');
			return FALSE;
		}

		// No rows returned, no matching section found
		$this->debug_text('get_object_section_section_id(): Returned '. $row_count .' rows, no matching section found.');
		return FALSE;
	}

	/**
	 * add_object_section()
	 *
	 * Inserts an object Section
	 *
	 * @return int Object Section ID of new section
	 *
	 * @param string Object Name
	 * @param string Object Value
	 * @param int Display Order
	 * @param int Hidden flag, hides section if 1, shows section if 0
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO', or 'ACL'
	 */
	function add_object_section($name, $value=0, $order=0, $hidden=0, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$object_sections_table = $this->_db_table_prefix .'aco_sections';
				break;
			case 'aro':
				$object_type = 'aro';
				$object_sections_table = $this->_db_table_prefix .'aro_sections';
				break;
			case 'axo':
				$object_type = 'axo';
				$object_sections_table = $this->_db_table_prefix .'axo_sections';
				break;
			case 'acl':
				$object_type = 'acl';
				$object_sections_table = $this->_db_table_prefix .'acl_sections';
				break;
		}

		$this->debug_text("add_object_section(): Value: $value Order: $order Name: $name Object Type: $object_type");

		$name = trim($name);
		$value = trim($value);
		$order = trim($order);
		$hidden = intval($hidden);

		if ($order == NULL OR $order == '') {
			$order = 0;
		}

		if (empty($name) ) {
			$this->debug_text("add_object_section(): name ($name) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("add_object_section(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$insert_id = $this->db->GenID($this->_db_table_prefix.$object_type.'_sections_seq',10);
		$query = 'insert into '. $object_sections_table .' (id,value,order_value,name,hidden) VALUES( '. $insert_id .', '. $this->db->quote($value) .', '. $order .', '. $this->db->quote($name) .', '. $hidden .')';
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('add_object_section');
			return false;
		} else {
			$this->debug_text("add_object_section(): Added object_section as ID: $insert_id");
			return $insert_id;
		}
	}

	/**
	 * edit_object_section()
	 *
	 * Edits a given Object Section
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise
	 *
	 * @param int Object Section ID #
	 * @param string Object Section Name
	 * @param string Object Section Value
	 * @param int Display Order
	 * @param int Hidden Flag, hide object section if 1, show if 0
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO', or 'ACL'
	 */
	function edit_object_section($object_section_id, $name, $value=0, $order=0, $hidden=0, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco';
				$object_sections_table = $this->_db_table_prefix .'aco_sections';
				$object_map_table = $this->_db_table_prefix .'aco_map';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro';
				$object_sections_table = $this->_db_table_prefix .'aro_sections';
				$object_map_table = $this->_db_table_prefix .'aro_map';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo';
				$object_sections_table = $this->_db_table_prefix .'axo_sections';
				$object_map_table = $this->_db_table_prefix .'axo_map';
				break;
			case 'acl':
				$object_type = 'acl';
				$table = $this->_db_table_prefix .'acl';
				$object_sections_table = $this->_db_table_prefix .'acl_sections';
				break;
			default:
				$this->debug_text('edit_object_section(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("edit_object_section(): ID: $object_section_id Value: $value Order: $order Name: $name Object Type: $object_type");

		$name = trim($name);
		$value = trim($value);
		$order = trim($order);
		$hidden = intval($hidden);

		if (empty($object_section_id) ) {
			$this->debug_text("edit_object_section(): Section ID ($object_section_id) is empty, this is required");
			return false;
		}

		if (empty($name) ) {
			$this->debug_text("edit_object_section(): name ($name) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("edit_object_section(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$this->db->BeginTrans();

		//Get old value incase it changed, before we do the update.
		$query = "select value from $object_sections_table where id=$object_section_id";
		$old_value = $this->db->GetOne($query);

		$query = "update $object_sections_table set
																value='$value',
																order_value='$order',
																name='$name',
																hidden=$hidden
													where   id=$object_section_id";
		$rs = $this->db->Execute($query);

		if (!is_object($rs)) {
			$this->debug_db('edit_object_section');

			$this->db->RollbackTrans();

			return false;
		} else {
			$this->debug_text("edit_object_section(): Modified aco_section ID: $object_section_id");

			if ($old_value != $value) {
				$this->debug_text("edit_object_section(): Value Changed, update other tables.");

				$query = "update $table set
																section_value='$value'
													where section_value = '$old_value'";
				$rs = $this->db->Execute($query);

				if (!is_object($rs)) {
					$this->debug_db('edit_object_section');

					$this->db->RollbackTrans();

					return false;
				} else {
					if (!empty($object_map_table)) {
						$query = "update $object_map_table set
																		section_value='$value'
															where section_value = '$old_value'";
						$rs = $this->db->Execute($query);

						if ( !is_object($rs) ) {
							$this->debug_db('edit_object_section');

							$this->db->RollbackTrans();

							return false;
						} else {
							$this->debug_text("edit_object_section(): Modified ojbect_map value: $value");

							$this->db->CommitTrans();
							return true;
						}
					} else {
						//ACL sections, have no mapping table. Return true.

						$this->db->CommitTrans();

						return true;
					}
				}
			}

      $this->db->CommitTrans();
			return true;
		}
	}

	/**
	 * del_object_section()
	 *
	 * Deletes a given Object Section and, if explicitly asked, all the section objects
	 *
	 * ERASE feature by: Martino Piccinato
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise
	 *
	 * @param int Object Section ID # to delete
	 * @param string Object Type, either 'ACO', 'ARO', 'AXO', or 'ACL'
	 * @param bool Erases all section objects assigned to the section
	 */
	function del_object_section($object_section_id, $object_type=NULL, $erase=FALSE) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$object_sections_table = $this->_db_table_prefix .'aco_sections';
				break;
			case 'aro':
				$object_type = 'aro';
				$object_sections_table = $this->_db_table_prefix .'aro_sections';
				break;
			case 'axo':
				$object_type = 'axo';
				$object_sections_table = $this->_db_table_prefix .'axo_sections';
				break;
			case 'acl':
				$object_type = 'acl';
				$object_sections_table = $this->_db_table_prefix .'acl_sections';
				break;
		}

		$this->debug_text("del_object_section(): ID: $object_section_id Object Type: $object_type, Erase all: $erase");

		if (empty($object_section_id) ) {
			$this->debug_text("del_object_section(): Section ID ($object_section_id) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("del_object_section(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		// Get the value of the section
		$query="SELECT value FROM $object_sections_table WHERE id='$object_section_id'";
		$section_value = $this->db->GetOne($query);

		// Get all objects ids in the section
		$object_ids = $this->get_object($section_value, 1, $object_type);

		if($erase) {
			// Delete all objects in the section and for
			// each object delete the referencing object
			// (see del_object method)
			if (is_array($object_ids)) {
					foreach ($object_ids as $id) {
						if ( $object_type === 'acl' ) {
							$this->del_acl($id);
						} else {
							$this->del_object($id, $object_type, TRUE);
						}
					}
			}
		}

		if($object_ids AND !$erase) {
			// There are objects in the section and we
			// were not asked to erase them: don't delete it

			$this->debug_text("del_object_section(): Could not delete the section ($section_value) as it is not empty.");

			return false;

		} else {
			// The section is empty (or emptied by this method)

			$query = "DELETE FROM $object_sections_table where id='$object_section_id'";
			$rs = $this->db->Execute($query);

			if (!is_object($rs)) {
				$this->debug_db('del_object_section');
				return false;
			} else {
				$this->debug_text("del_object_section(): deleted section ID: $object_section_id Value: $section_value");
				return true;
			}

		}

		return false;
	}

	/**
	 * get_section_data()
	 *
	 * Gets the section data given the Section Value
	 *
	 * @return array Returns numerically indexed array with the following columns:
	 *	- array[0] = (int) Section ID #
	 *	- array[1] = (string) Section Value
	 *	- array[2] = (int) Section Order
	 *	- array[3] = (string) Section Name
	 *	- array[4] = (int) Section Hidden?
	 * @param string Section Value
	 * @param string Object Type, either 'ACO', 'ARO', or 'AXO'
	 */
	function get_section_data($section_value, $object_type=NULL) {

		switch(strtolower(trim($object_type))) {
			case 'aco':
				$object_type = 'aco';
				$table = $this->_db_table_prefix .'aco_sections';
				break;
			case 'aro':
				$object_type = 'aro';
				$table = $this->_db_table_prefix .'aro_sections';
				break;
			case 'axo':
				$object_type = 'axo';
				$table = $this->_db_table_prefix .'axo_sections';
				break;
			default:
				$this->debug_text('get_section_data(): Invalid Object Type: '. $object_type);
				return FALSE;
		}

		$this->debug_text("get_section_data(): Section Value: $section_value Object Type: $object_type");

		if (empty($section_value) ) {
			$this->debug_text("get_section_data(): Section Value ($section_value) is empty, this is required");
			return false;
		}

		if (empty($object_type) ) {
			$this->debug_text("get_section_data(): Object Type ($object_type) is empty, this is required");
			return false;
		}

		$query = "SELECT id, value, order_value, name, hidden FROM '. $table .' WHERE value='$section_value'";
		$row = $this->db->GetRow($query);

		if ($row) {
			return $row;
		}

		$this->debug_text("get_section_data(): Section does not exist.");
		return false;
	}

	/**
	 * clear_database()
	 *
	 * Deletes all data from the phpGACL tables. USE WITH CAUTION.
	 *
	 * @return bool Returns TRUE if successful, FALSE otherwise
	 *
	 */
        function clear_database(){

			$tablesToClear = array(
					$this->_db_table_prefix.'acl',
					$this->_db_table_prefix.'aco',
					$this->_db_table_prefix.'aco_map',
					$this->_db_table_prefix.'aco_sections',
					$this->_db_table_prefix.'aro',
					$this->_db_table_prefix.'aro_groups',
					$this->_db_table_prefix.'aro_groups_map',
					$this->_db_table_prefix.'aro_map',
					$this->_db_table_prefix.'aro_sections',
					$this->_db_table_prefix.'axo',
					$this->_db_table_prefix.'axo_groups',
					$this->_db_table_prefix.'axo_groups_map',
					$this->_db_table_prefix.'axo_map',
					$this->_db_table_prefix.'axo_sections',
					$this->_db_table_prefix.'groups_aro_map',
					$this->_db_table_prefix.'groups_axo_map'
					);

			// Get all the table names and loop
			$tableNames = $this->db->MetaTables('TABLES');
			$query = array();
			foreach ($tableNames as $key => $value){
					if (in_array($value, $tablesToClear) ) {
							$query[] = 'TRUNCATE TABLE '.$value.';';
					}
			}

			// Loop the queries and return.
			foreach ($query as $key => $value){
					$result = $this->db->Execute($value);
			}

			return TRUE;
        }

}
?>