File: class_setupStep_Migrate.inc

package info (click to toggle)
fusiondirectory 1.0.8.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 28,984 kB
  • sloc: php: 74,645; xml: 3,645; perl: 1,555; pascal: 705; sh: 135; sql: 45; makefile: 19
file content (2834 lines) | stat: -rw-r--r-- 99,683 bytes parent folder | download | duplicates (2)
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
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2007  Fabian Hickert
  Copyright (C) 2011-2013  FusionDirectory

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/****************
 * FUNCTIONS

Step_Migrate                - Constructor.
update_strings              - Used to update the displayed step informations.
initialize_checks           - Initialize migration steps.
check_ldap_permissions      - Check if the used admin account has full access to the ldap database.
check_gosaAccounts          - Check if there are users without the required objectClasses.
migrate_gosaAccounts        - Migrate selected users to FusionDirectory user accounts.
check_organizationalUnits   - Check if there are departments, that are not visible for FusionDirectory
migrate_organizationalUnits - Migrate selected departments
check_administrativeAccount - Check if there is at least one acl entry available
checkBase                   - Check if there is a root object available

get_user_list               - Get list of available users
get_group_list              - Get list of groups

create_admin
create_admin_user

execute                     - Generate html output of this plugin
save_object                 - Save posts
array_to_ldif               - Create ldif output of an ldap result array

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

class Step_Migrate extends setup_step
{
  var $languages      = array();
  var $attributes     = array('valid_admin');
  var $header_image   = 'geticon.php?context=applications&icon=utilities-system-monitor&size=48';
  var $checks         = array();

  /* Department migration attributes */
  var $dep_migration_dialog = FALSE;
  var $deps_to_migrate      = array();
  var $show_details         = FALSE;

  /* Department migration attributes */
  var $users_migration_dialog = FALSE;
  var $users_to_migrate       = array();

  /* Create Acl attributes */
  var $acl_create_dialog    = FALSE;
  var $acl_create_selected  = ""; // Currently selected element, that should receive admin rights
  var $acl_create_changes   = ""; // Contains ldif information about changes
  var $acl_create_confirmed = FALSE;

  /* Checks initialised ? */
  var $checks_initialised = FALSE;

  /* Users outside to people ou */
  var $outside_users        = array();
  var $outside_users_dialog = FALSE;

  /* Users outside to groups ou */
  var $outside_groups        = array();
  var $outside_groups_dialog = FALSE;

  /* Device migration */
  var $device_dialog         = FALSE;
  var $device                = array();

  /* Service migration */
  var $service_dialog         = FALSE;
  var $service                = array();

  /* Group menus */
  var $menu_dialog           = FALSE;
  var $menu                  = array();

  /* check for multiple use of same uidNumber */
  var $check_uidNumbers        = array();
  var $check_uidNumbers_dialog = FALSE;

  /* check for multiple use of same gidNumber */
  var $check_gidNumbers        = array();
  var $check_gidNumbers_dialog = FALSE;

  var $group_list              = array();

  /* Migrable users */
  var $migrate_users = array();
  var $acl_migrate_dialog      = FALSE;
  var $migrate_acl_base_entry  = "";

  /* Root object classes */
  var $rootOC_migrate_dialog = FALSE;
  var $rootOC_details = array();

  /* One valid admin dn */
  var $valid_admin = FALSE;

  /* Defaults ACL roles */
  var $defaultRoles;

  function __construct()
  {
    $this->update_strings();
    $this->fill_defaultRoles();
  }

  function update_strings()
  {
    $this->s_title      = _("LDAP inspection");
    $this->s_title_long = _("LDAP inspection");
    $this->s_info       = _("Analyze your current LDAP for FusionDirectory compatibility");
  }

  function fill_defaultRoles()
  {
    $this->defaultRoles = array(
      array(
        'cn'              => 'manager',
        'description'     => _('Give all rights on users in the given branch'),
        'objectclass'     => array('top', 'gosaRole'),
        'gosaAclTemplate' => '0:user/password;cmdrw,user/user;cmdrw,user/posixAccount;cmdrw'
      ),
      array(
        'cn'              => 'editowninfos',
        'description'     => _('Allow users to edit their own information (main tab and posix use only on base)'),
        'objectclass'     => array('top', 'gosaRole'),
        'gosaAclTemplate' => '0:user/posixAccount;srw,user/user;srw'
      ),
      array(
        'cn'              => 'editowninfos',
        'description'     => _('Allow users to edit their own password (use only on base)'),
        'objectclass'     => array('top', 'gosaRole'),
        'gosaAclTemplate' => '0:user/password;srw'
      ),
    );
  }

  function initialize_checks()
  {
    $this->checks = array();
    $this->checks['root']['TITLE']      = _("Checking for root object");
    $this->checks['root']['STATUS']     = FALSE;
    $this->checks['root']['STATUS_MSG'] = "";
    $this->checks['root']['ERROR_MSG']  = "";
    $this->checkBase();

    $this->checks['rootOC']['TITLE']      = _("Inspecting object classes in root object");
    $this->checks['rootOC']['STATUS']     = FALSE;
    $this->checks['rootOC']['STATUS_MSG'] = "";
    $this->checks['rootOC']['ERROR_MSG']  = "";
    $this->checkBaseOC();

    $this->checks['permissions']['TITLE']       = _("Checking permission for LDAP database");
    $this->checks['permissions']['STATUS']      = FALSE;
    $this->checks['permissions']['STATUS_MSG']  = "";
    $this->checks['permissions']['ERROR_MSG']   = "";
    $this->check_ldap_permissions();

    $this->checks['deps_visible']['TITLE']      = _("Checking for invisible departments");
    $this->checks['deps_visible']['STATUS']     = FALSE;
    $this->checks['deps_visible']['STATUS_MSG'] = "";
    $this->checks['deps_visible']['ERROR_MSG']  = "";

    $this->checks['users_visible']['TITLE']       = _("Checking for invisible users");
    $this->checks['users_visible']['STATUS']      = FALSE;
    $this->checks['users_visible']['STATUS_MSG']  = "";
    $this->checks['users_visible']['ERROR_MSG']   = "";
    $this->check_gosaAccounts();

    $this->migrate_users = array();
    $this->checks['acls']['TITLE']      = _("Checking for super administrator");
    $this->checks['acls']['STATUS']     = FALSE;
    $this->checks['acls']['STATUS_MSG'] = "";
    $this->checks['acls']['ERROR_MSG']  = "";
    $this->check_administrativeAccount();

    $this->checks['default_acls']['TITLE']      = _("Checking for default ACL roles and groupes");
    $this->checks['default_acls']['STATUS']     = FALSE;
    $this->checks['default_acls']['STATUS_MSG'] = "";
    $this->checks['default_acls']['ERROR_MSG']  = "";
    $this->check_defaultACLs();

    $this->checks['outside_users']['TITLE']       = _("Checking for users outside the people tree");
    $this->checks['outside_users']['STATUS']      = FALSE;
    $this->checks['outside_users']['STATUS_MSG']  = "";
    $this->checks['outside_users']['ERROR_MSG']   = "";
    $this->search_outside_users();

    $this->checks['outside_groups']['TITLE']      = _("Checking for groups outside the groups tree");
    $this->checks['outside_groups']['STATUS']     = FALSE;
    $this->checks['outside_groups']['STATUS_MSG'] = "";
    $this->checks['outside_groups']['ERROR_MSG']  = "";
    $this->search_outside_groups();
    $this->check_organizationalUnits();

    $this->checks['uidNumber_usage']['TITLE']       = _("Checking for duplicated UID numbers");
    $this->checks['uidNumber_usage']['STATUS']      = FALSE;
    $this->checks['uidNumber_usage']['STATUS_MSG']  = "";
    $this->checks['uidNumber_usage']['ERROR_MSG']   = "";
    $this->check_uidNumber();

    $this->checks['gidNumber_usage']['TITLE']       = _("Checking for duplicate GID numbers");
    $this->checks['gidNumber_usage']['STATUS']      = FALSE;
    $this->checks['gidNumber_usage']['STATUS_MSG']  = "";
    $this->checks['gidNumber_usage']['ERROR_MSG']   = "";
    $this->check_gidNumber();

    $this->checks['old_style_devices']['TITLE']       = _("Checking for old style USB devices");
    $this->checks['old_style_devices']['STATUS']      = FALSE;
    $this->checks['old_style_devices']['STATUS_MSG']  = "";
    $this->checks['old_style_devices']['ERROR_MSG']   = "";
    $this->check_usb_devices();

    $this->checks['old_style_services']['TITLE']      = _("Checking for old services that have to be migrated");
    $this->checks['old_style_services']['STATUS']     = FALSE;
    $this->checks['old_style_services']['STATUS_MSG'] = "";
    $this->checks['old_style_services']['ERROR_MSG']  = "";
    $this->check_services();

    $this->checks['old_style_menus']['TITLE']       = _("Checking for old style application menus");
    $this->checks['old_style_menus']['STATUS']      = FALSE;
    $this->checks['old_style_menus']['STATUS_MSG']  = "";
    $this->checks['old_style_menus']['ERROR_MSG']   = "";
    $this->check_menus();
  }


  /* Check if there are uidNumbers which are used more than once */
  function check_uidNumber()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    $ldap->cd($cv['base']);
    $res = $ldap->search("(&(objectClass=posixAccount)(uidNumber=*))", array("dn","uidNumber"));
    if (!$res) {
      $this->checks['uidNumber_usage']['STATUS']      = FALSE;
      $this->checks['uidNumber_usage']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['uidNumber_usage']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
      return FALSE;
    }

    $this->check_uidNumbers = array();
    $tmp = array();
    while ($attrs = $ldap->fetch()) {
      $tmp[$attrs['uidNumber'][0]][] = $attrs;
    }

    foreach ($tmp as $entries) {
      if (count($entries) > 1) {
        foreach ($entries as $entry) {
          $this->check_uidNumbers[base64_encode($entry['dn'])] = $entry;
        }
      }
    }

    if ($this->check_uidNumbers) {
      $this->checks['uidNumber_usage']['STATUS']      = FALSE;
      $this->checks['uidNumber_usage']['STATUS_MSG']  = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['uidNumber_usage']['ERROR_MSG']   =
        sprintf(_("Found %s duplicate values for attribute 'uidNumber'."), count($this->check_uidNumbers));
      return FALSE;
    } else {
      $this->checks['uidNumber_usage']['STATUS']      = TRUE;
      $this->checks['uidNumber_usage']['STATUS_MSG']  = _("Ok");
      $this->checks['uidNumber_usage']['ERROR_MSG']   = "";
      return TRUE;
    }
  }


  /* Check if there are duplicated gidNumbers present in ldap */
  function check_gidNumber()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    $ldap->cd($cv['base']);
    $res = $ldap->search("(&(objectClass=posixGroup)(gidNumber=*))", array("dn","gidNumber"));
    if (!$res) {
      $this->checks['gidNumber_usage']['STATUS']      = FALSE;
      $this->checks['gidNumber_usage']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['gidNumber_usage']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
      return FALSE;
    }

    $this->check_gidNumbers = array();
    $tmp = array();
    while ($attrs = $ldap->fetch()) {
      $tmp[$attrs['gidNumber'][0]][] = $attrs;
    }

    foreach ($tmp as $entries) {
      if (count($entries) > 1) {
        foreach ($entries as $entry) {
          $this->check_gidNumbers[base64_encode($entry['dn'])] = $entry;
        }
      }
    }

    if ($this->check_gidNumbers) {
      $this->checks['gidNumber_usage']['STATUS']      = FALSE;
      $this->checks['gidNumber_usage']['STATUS_MSG']  = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['gidNumber_usage']['ERROR_MSG']   =
        sprintf(_("Found %s duplicate values for attribute 'gidNumber'."), count($this->check_gidNumbers));
      return FALSE;
    } else {
      $this->checks['gidNumber_usage']['STATUS']      = TRUE;
      $this->checks['gidNumber_usage']['STATUS_MSG']  = _("Ok");
      $this->checks['gidNumber_usage']['ERROR_MSG']   = "";
      return TRUE;
    }
  }

  /* Search for groups outside the group ou */
  function search_outside_groups()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    $group_ou = $cv['groupou'];
    $ldap->cd($cv['base']);

    /***********
     * Get all gosaDepartments to be able to
     *  validate correct ldap tree position of every single user
     ***********/
    $valid_deps = array();
    $valid_deps['/'] = $cv['base'];
    $ldap->search("(&(objectClass=gosaDepartment)(ou=*))", array("dn","ou"));
    while ($attrs = $ldap->fetch()) {
      $valid_deps[] = $attrs['dn'];
    }

    /***********
     * Get all groups
     ***********/
    $res = $ldap->search("(objectClass=posixGroup)", array("dn"));
    if (!$res) {
      $this->checks['outside_groups']['STATUS']     = FALSE;
      $this->checks['outside_groups']['STATUS_MSG'] = _("LDAP query failed");
      $this->checks['outside_groups']['ERROR_MSG']  = _("Possibly the 'root object' is missing.");
      return FALSE;
    }

    $this->outside_groups = array();
    $this->groups_list = array();;
    while ($attrs = $ldap->fetch()) {
      $group_db_base = preg_replace("/^[^,]+,".preg_quote($group_ou, '/')."+,/i", "", $attrs['dn']);

      /* Check if entry is not an addressbook only user
       *  and verify that he is in a valid department
       */
      if ( !preg_match("/".preg_quote("dc=addressbook,", '/')."/", $group_db_base) &&
          !in_array($group_db_base, $valid_deps)
        ) {
        $attrs['selected'] = FALSE;
        $attrs['ldif']     = "";
        $this->outside_groups[base64_encode($attrs['dn'])] = $attrs;
      }
      $this->group_list[] = $attrs['dn'];
    }

    if (count($this->outside_groups)) {
      $this->checks['outside_groups']['STATUS']     = FALSE;
      $this->checks['outside_groups']['STATUS_MSG'] = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['outside_groups']['ERROR_MSG']  =
        sprintf(_("Found %s groups outside the configured tree '%s'."), count($this->outside_groups), $group_ou);
      $this->checks['outside_groups']['ERROR_MSG']  .= "&nbsp;<input type='submit' name='outside_groups_dialog' value='"._("Move")."...'>";
      return FALSE;
    } else {
      $this->checks['outside_groups']['STATUS']     = TRUE;
      $this->checks['outside_groups']['STATUS_MSG'] = _("Ok");
      $this->checks['outside_groups']['ERROR_MSG']  = "";
      return TRUE;
    }
  }

  /* Search for users outside the people ou */
  function search_outside_users()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);

    /***********
     * Get all gosaDepartments to be able to
     *  validate correct ldap tree position of every single user
     ***********/
    $valid_deps = array();
    $valid_deps['/'] = $cv['base'];
    $ldap->search("(&(objectClass=gosaDepartment)(ou=*))", array("dn","ou"));
    while ($attrs = $ldap->fetch()) {
      $valid_deps[] = $attrs['dn'];
    }

    /***********
     * Search for all users
     ***********/
    $res = $ldap->search("(&(objectClass=gosaAccount)(!(uid=*$)))", array("dn"));
    if (!$res) {
      $this->checks['outside_users']['STATUS']      = FALSE;
      $this->checks['outside_users']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['outside_users']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
      return FALSE;
    }

    /***********
     * Check if returned users are within a valid GOsa department. (peopleou,gosaDepartment,base)
     ***********/
    $this->outside_users = array();
    $people_ou = trim($cv['peopleou']);
    if (!empty($people_ou)) {
      $people_ou = $people_ou.",";
    }

    while ($attrs = $ldap->fetch()) {
      $people_db_base = preg_replace("/^[^,]+,".preg_quote($people_ou, '/')."/i", "", $attrs['dn']);

      /* Check if entry is not an addressbook only user
       *  and verify that he is in a valid department
       */
      if ( !preg_match("/dc=addressbook,/", $people_db_base) &&
          !in_array($people_db_base, $valid_deps)
         ) {
        $attrs['selected'] = FALSE;
        $attrs['ldif']     = "";
        $this->outside_users[base64_encode($attrs['dn'])] = $attrs;
      }
    }

    if (count($this->outside_users)) {
      $this->checks['outside_users']['STATUS']      = FALSE;
      $this->checks['outside_users']['STATUS_MSG']  = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['outside_users']['ERROR_MSG']   =
        sprintf(_("Found %s user(s) outside the configured tree '%s'."), count($this->outside_users), $people_ou);
      $this->checks['outside_users']['ERROR_MSG']   .= "<input type='submit' name='outside_users_dialog' value='"._("Move")."...'>";
      return FALSE;
    } else {
      $this->checks['outside_users']['STATUS']      = TRUE;
      $this->checks['outside_users']['STATUS_MSG']  = _("Ok");
      $this->checks['outside_users']['ERROR_MSG']   = "";
      return TRUE;
    }
  }


  /* Check ldap accessibility
   * Create and remove a dummy object,
   *  to ensure that we have the necessary permissions
   */
  function check_ldap_permissions()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Create dummy entry */
    $name       = "GOsa_setup_text_entry_".session_id().rand(0, 999999);
    $dn         = "ou=".$name.",".$cv['base'];
    $testEntry  = array();

    $testEntry['objectClass'][] = "top";
    $testEntry['objectClass'][] = "organizationalUnit";
    $testEntry['objectClass'][] = "gosaDepartment";
    $testEntry['description']   = "Created by FusionDirectory setup, this object can be removed.";
    $testEntry['ou']            = $name;

    /* check if simple ldap cat will be successful */
    $res = $ldap->cat($cv['base']);
    if (!$res) {
      $this->checks['permissions']['STATUS']      = FALSE;
      $this->checks['permissions']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['permissions']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
      return FALSE;
    }

    /* Try to create dummy object */
    $ldap->cd ($dn);
    $res = $ldap->add($testEntry);
    $ldap->cat($dn);
    if (!$ldap->count()) {
      new log("view", "setup/".get_class($this), $dn, array(), $ldap->get_error());

      $this->checks['permissions']['STATUS']      = FALSE;
      $this->checks['permissions']['STATUS_MSG']  = _("Failed");
      $this->checks['permissions']['ERROR_MSG']   =
        sprintf(_("The specified user '%s' does not have full access to your ldap database."), $cv['admin']);
      return FALSE;
    }

    /* Try to remove created entry */
    $res = $ldap->rmDir($dn);
    $ldap->cat($dn);
    if ($ldap->count()) {
      new log("view", "setup/".get_class($this), $dn, array(), $ldap->get_error());
      $this->checks['permissions']['STATUS']      = FALSE;
      $this->checks['permissions']['STATUS_MSG']  = _("Failed");
      $this->checks['permissions']['ERROR_MSG']   =
        sprintf(_("The specified user '%s' does not have full access to your ldap database."), $cv['admin']);
      return FALSE;
    }

    /* Create & remove of dummy object was successful */
    $this->checks['permissions']['STATUS']      = TRUE;
    $this->checks['permissions']['STATUS_MSG']  = _("Ok");
    $this->checks['permissions']['ERROR_MSG']   = "";
    return TRUE;
  }


  /* Check if there are users which will
   *  be invisible for FusionDirectory
   */
  function check_gosaAccounts()
  {
    /* Remember old list of invisible users, to be able to set
     *  the 'html checked' status for the checkboxes again
     */
    $old    = $this->users_to_migrate;
    $this->users_to_migrate = array();

    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Get all invisible users */
    $ldap->cd($cv['base']);
    $res = $ldap->search("(&(|(objectClass=posixAccount)(&(objectClass=inetOrgPerson)(objectClass=organizationalPerson))(objectClass=OpenLDAPperson))(!(objectClass=gosaAccount))(!(&(objectClass=Account)(objectClass=sambaSamAccount)))(uid=*))", array("sn","givenName","cn","uid"));

    while ($attrs = $ldap->fetch()) {
      if (!preg_match("/,dc=addressbook,/", $attrs['dn'])) {
        $attrs['checked'] = FALSE;
        $attrs['before']  = "";
        $attrs['after']   = "";

        /* Set objects to selected, that were selected before reload */
        if (isset($old[base64_encode($attrs['dn'])])) {
          $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
        }
        $this->users_to_migrate[base64_encode($attrs['dn'])] = $attrs;
      }
    }

    /* No invisible */
    if (!$res) {
      $this->checks['users_visible']['STATUS']      = FALSE;
      $this->checks['users_visible']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['users_visible']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
    } elseif (count($this->users_to_migrate) == 0) {
      $this->checks['users_visible']['STATUS']      = TRUE;
      $this->checks['users_visible']['STATUS_MSG']  = _("Ok");
      $this->checks['users_visible']['ERROR_MSG']   = "";
    } else {
      $this->checks['users_visible']['STATUS']      = FALSE;
      $this->checks['users_visible']['STATUS_MSG']  = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['users_visible']['ERROR_MSG']   = sprintf(_("Found %s user(s) that will not be visible in FusionDirectory or which are incomplete."),
          count($this->users_to_migrate));
      $this->checks['users_visible']['ERROR_MSG']   .= "<input type='submit' name='users_visible_migrate' value='"._("Migrate")."...'>";
    }
  }


  /* Start user account migration */
  function migrate_gosaAccounts($only_ldif = FALSE)
  {
    $this->show_details = $only_ldif;

    /* Establish ldap connection */
    $ldap = $this->get_ldap_link();

    /* Add gosaAccount objectClass to the selected users */
    foreach ($this->users_to_migrate as $key => $dep) {
      if ($dep['checked']) {

        /* Get old objectClasses */
        $ldap->cat($dep['dn'], array("objectClass"));
        $attrs      = $ldap->fetch();

        /* Create new objectClass array */
        $new_attrs  = array();
        $new_attrs['objectClass'] = array("gosaAccount","inetOrgPerson","organizationalPerson","person");
        for ($i = 0; $i < $attrs['objectClass']['count']; $i++) {
          if (!in_array_ics($attrs['objectClass'][$i], $new_attrs['objectClass'])) {
            $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
          }
        }

        /* Set info attributes for current object,
         *  or write changes to the ldap database
         */
        if ($only_ldif) {
          $this->users_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
          $this->users_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
        } else {
          $ldap->cd($attrs['dn']);
          if (!$ldap->modify($new_attrs)) {
            msg_dialog::display(_("Migration error"), sprintf(_("Cannot migrate department '%s':")."<br><br><i>%s</i>", LDAP::fix($attrs['dn']), $ldap->get_error()), ERROR_DIALOG);
            return FALSE;
          }
        }
      }
    }
    return TRUE;
  }


  /* Check if there are invisible organizational Units */
  function check_organizationalUnits()
  {
    $old                    = $this->deps_to_migrate;
    $this->deps_to_migrate  = array();

    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Skip FusionDirectory internal departments */
    $skip_dns = array("/".$cv['peopleou']."/","/".$cv['groupou']."/","/".$cv['aclroleou']."/",
        "/^ou=people,/","/^ou=groups,/","/^ou=sudoers,/",
        "/(,|)ou=configs,/","/(,|)ou=systems,/","/(,|)ou=tokens,/",
        "/(,|)ou=apps,/","/(,|)ou=mime,/","/(,|)ou=devices/",
        "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
        "/(,|)ou=winstations,/","/^ou=hosts,/","/^ou=computers,/","/^ou=idmap,/","/^ou=Idmap,/","/(,|)ou=roles,/");

    /* Get all invisible departments */
    $ldap->cd($cv['base']);
    $res = $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))", array("ou","description","dn"));
    while ($attrs = $ldap->fetch()) {
      $attrs['checked'] = FALSE;
      $attrs['before']  = "";
      $attrs['after']   = "";

      /* Set objects to selected, that were selected before reload */
      if (isset($old[base64_encode($attrs['dn'])])) {
        $attrs['checked'] = $old[base64_encode($attrs['dn'])]['checked'];
      }
      $this->deps_to_migrate[base64_encode($attrs['dn'])] = $attrs;
    }

    /* Filter returned list of departments and ensure that
     *  FusionDirectory internal departments will not be listed
     */
    foreach ($this->deps_to_migrate as $key => $attrs) {
      $dn = $attrs['dn'];
      $skip = FALSE;

      /* Check if this object is an application release object
          e.g. groups-> application menus.
       */
      if (preg_match("/^.*,[ ]*cn=/", $dn)) {
        $cn_dn = preg_replace("/^.*,[ ]*cn=/", "cn=", $dn);
        if (in_array($cn_dn, $this->group_list)) {
          $skip = TRUE;
        }
      }

      foreach ($skip_dns as $skip_dn) {
        if (preg_match($skip_dn, $dn)) {
          $skip = TRUE;
        }
      }
      if ($skip) {
        unset($this->deps_to_migrate[$key]);
      }
    }

    /* If we have no invisible departments found
     *  tell the user that everything is ok
     */
    if (!$res) {
      $this->checks['deps_visible']['STATUS']     = FALSE;
      $this->checks['deps_visible']['STATUS_MSG'] = _("LDAP query failed");
      $this->checks['deps_visible']['ERROR_MSG']  = _("Possibly the 'root object' is missing.");
    } elseif (count($this->deps_to_migrate) == 0 ) {
      $this->checks['deps_visible']['STATUS']     = TRUE;
      $this->checks['deps_visible']['STATUS_MSG'] = _("Ok");
      $this->checks['deps_visible']['ERROR_MSG']  = "";
    } else {
      $this->checks['deps_visible']['STATUS']     = TRUE;
      $this->checks['deps_visible']['STATUS_MSG'] = '<font style="color:#FFA500">'._("Warning").'</font>';
      $this->checks['deps_visible']['ERROR_MSG']  = sprintf(_("Found %s department(s) that will not be visible in FusionDirectory."), count($this->deps_to_migrate));
      $this->checks['deps_visible']['ERROR_MSG']  .= "&nbsp;<input type='submit' name='deps_visible_migrate' value='"._("Migrate")."...'>";
    }
  }



  /* Start deparmtment migration */
  function migrate_organizationalUnits($only_ldif = FALSE)
  {
    $this->show_details = $only_ldif;

    /* Establish ldap connection */
    $ldap = $this->get_ldap_link();

    /* Add gosaDepartment objectClass to each selected entry */
    foreach ($this->deps_to_migrate as $key => $dep) {
      if ($dep['checked']) {

        /* Get current objectClasses */
        $ldap->cat($dep['dn'], array("objectClass","description"));
        $attrs      = $ldap->fetch();

        /* Create new objectClass attribute including gosaDepartment*/
        $new_attrs  = array();
        for ($i = 0; $i < $attrs['objectClass']['count']; $i++) {
          $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
        }
        $new_attrs['objectClass'][] = "gosaDepartment";

        /* Append description it is missing */
        if (!isset($attrs['description'])) {
          $new_attrs['description'][] = "GOsa department";
        }

        /* Depending on the parameter >only_diff< we save the changes as ldif
         *  or we write our changes directly to the ldap database
         */
        if ($only_ldif) {
          $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
          $this->deps_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
        } else {
          $ldap->cd($attrs['dn']);
          if (!$ldap->modify($new_attrs)) {
            msg_dialog::display(_("Migration error"), sprintf(_("Cannot migrate department '%s':")."<br><br><i>%s</i>", LDAP::fix($attrs['dn']), $ldap->get_error()), ERROR_DIALOG);
            return FALSE;
          }
        }
      }
    }
    return TRUE;
  }


  /* Check Acls if there is at least one object with acls defined */
  function check_administrativeAccount()
  {
    /* Reset settings */
    $FD_1_0_8_found = FALSE;
    $this->migrate_users = array();
    $this->acl_migrate_dialog = FALSE;
    $this->migrate_acl_base_entry  = "";
    $valid_admin = FALSE;

    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);
    $res = $ldap->cat($cv['base']);

    if (!$res) {
      $this->checks['acls']['STATUS']     = FALSE;
      $this->checks['acls']['STATUS_MSG'] = _("LDAP query failed");
      $this->checks['acls']['ERROR_MSG']  = _("Possibly the 'root object' is missing.");
    } else {
      $FD_1_0_8_found = FALSE; // GOsa 2.6 Account found
      $FD_1_0_7_found = FALSE; // GOsa 2.5 Account found, allow migration

      $attrs = $ldap->fetch();

      /* Collect a list of available FusionDirectory users and groups */
      $users = array();
      $ldap->search("(&(objectClass=gosaAccount)(objectClass=person)".
        "(objectClass=inetOrgPerson)(objectClass=organizationalPerson))", array("uid","dn"));
      while ($user_attrs = $ldap->fetch()) {
        $users[$user_attrs['dn']] = $user_attrs['uid'][0];
        $rusers[$user_attrs['uid'][0]] = $user_attrs['dn'];
      }
      $groups = array();
      $ldap->search("objectClass=posixGroup", array("cn","dn"));
      while ($group_attrs = $ldap->fetch()) {
        $groups[$group_attrs['dn']] = $group_attrs['cn'][0];
      }

      /* Check if a valid FusionDirectory 1.0.8 admin exists
          -> gosaAclEntry for an existing and accessible user.
       */
      $valid_users  = "";
      $valid_groups = "";
      if (isset($attrs['gosaAclEntry'])) {
        $acls = $attrs['gosaAclEntry'];
        for ($i = 0; $i < $acls['count']; $i++) {
          $acl = $acls[$i];
          $tmp = explode(":", $acl);

          if ($tmp[1] == "subtree") {
            /* Check if acl owner is a valid FusionDirectory user account */
            $ldap->cat(base64_decode($tmp[2]), array("gosaAclTemplate"), '(gosaAclTemplate=*:all;cmdrw)');
            if ($ldap->count()) {
              $members = explode(",", $tmp[3]);
              foreach ($members as $member) {
                $member = base64_decode($member);

                if (isset($users[$member])) {
                  if (!$valid_admin) {
                    $valid_admin = $member;
                  }
                  $valid_users    .= $users[$member].", ";
                  $FD_1_0_8_found = TRUE;
                }
                if (isset($groups[$member])) {
                  $ldap->cat($member);
                  $group_attrs = $ldap->fetch();
                  $val_users = "";
                  if (isset($group_attrs['memberUid'])) {
                    for ($e = 0; $e < $group_attrs['memberUid']['count']; $e ++) {
                      if (isset($rusers[$group_attrs['memberUid'][$e]])) {
                        if (!$valid_admin) {
                          $valid_admin = $rusers[$group_attrs['memberUid'][$e]];
                        }
                        $val_users .= $group_attrs['memberUid'][$e].", ";
                      }
                    }
                  }
                  if (!empty($val_users)) {
                    $valid_groups .= $groups[$member]."(<i>".trim($val_users, ", ")."</i>), ";
                    $FD_1_0_8_found  = TRUE;
                  }
                }
              }
            }
          }
        }
      }

      /* Try to find an old FD 1.0.7 administrative account that may be migrated */
      if (!$FD_1_0_8_found) {
        $valid_users  = "";
        $valid_groups = "";
        if (isset($attrs['gosaAclEntry'])) {
          $acls = $attrs['gosaAclEntry'];
          for ($i = 0; $i < $acls['count']; $i++) {
            $acl = $acls[$i];
            $tmp = explode(":", $acl);

            if ($tmp[1] == "psub") {
              $members = explode(",", $tmp[2]);
              foreach ($members as $member) {
                $member = base64_decode($member);
                if (isset($users[$member])) {
                  if (preg_match("/all;cmdrw/i", $tmp[3])) {
                    if (!$valid_admin) {
                      $valid_admin = $member;
                    }
                    $valid_users    .= $users[$member].", ";
                    $FD_1_0_7_found = TRUE;
                  }
                }
                if (isset($groups[$member])) {
                  if (preg_match("/all;cmdrw/i", $tmp[3])) {
                    $ldap->cat($member);
                    $group_attrs = $ldap->fetch();
                    $val_users = "";
                    if (isset($group_attrs['memberUid'])) {
                      for ($e = 0; $e < $group_attrs['memberUid']['count']; $e++) {
                        if (isset($rusers[$group_attrs['memberUid'][$e]])) {
                          if (!$valid_admin) {
                            $valid_admin = $rusers[$group_attrs['memberUid'][$e]];
                          }
                          $val_users .= $group_attrs['memberUid'][$e].", ";
                        }
                      }
                    }
                    if (!empty($val_users)) {
                      $valid_groups .= $groups[$member]."(<i>".trim($val_users, ", ")."</i>), ";
                      $FD_1_0_7_found  = TRUE;
                    }
                  }
                }
              }
            } elseif ($tmp[1] == "role") {
              /* Check if acl owner is a valid FusionDirectory user account */
              $ldap->cat(base64_decode($tmp[2]), array("gosaAclTemplate"));
              $ret = $ldap->fetch();

              if (isset($ret['gosaAclTemplate'])) {
                $cnt = $ret['gosaAclTemplate']['count'];
                for ($e = 0; $e < $cnt; $e++) {

                  $a_str = $ret['gosaAclTemplate'][$e];
                  if (preg_match("/^[0-9]*:psub:/", $a_str) && preg_match("/:all;cmdrw$/", $a_str)) {

                    $members = explode(",", $tmp[3]);
                    foreach ($members as $member) {
                      $member = base64_decode($member);

                      if (isset($users[$member])) {
                        if (!$valid_admin) {
                          $valid_admin = $member;
                        }
                        $valid_users    .= $users[$member].", ";
                        $FD_1_0_7_found = TRUE;
                      }
                      if (isset($groups[$member])) {
                        $ldap->cat($member);
                        $group_attrs = $ldap->fetch();
                        $val_users = "";
                        if (isset($group_attrs['memberUid'])) {
                          for ($e = 0; $e < $group_attrs['memberUid']['count']; $e ++) {
                            if (isset($rusers[$group_attrs['memberUid'][$e]])) {
                              if (!$valid_admin) {
                                $valid_admin = $rusers[$group_attrs['memberUid'][$e]];
                              }
                              $val_users .= $group_attrs['memberUid'][$e].", ";
                            }
                          }
                        }
                        if (!empty($val_users)) {
                          $valid_groups .= $groups[$member]."(<i>".trim($val_users, ", ")."</i>), ";
                          $FD_1_0_7_found  = TRUE;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }

      /* Print out results */
      if ($FD_1_0_7_found) {
        $str = "";
        if (!empty($valid_groups)) {
          $str .= "<i>".sprintf(_("FD 1.0.7 administrative accounts found: %s"), trim($valid_groups, ", "))."</i><br>";
        }
        $this->checks['acls']['STATUS']     = FALSE;
        $this->checks['acls']['STATUS_MSG'] = _("Failed");
        $this->checks['acls']['ERROR_MSG']  = $str;
        $this->checks['acls']['ERROR_MSG']  .= _("There is no valid FusionDirectory 1.0.8 administrator account inside your LDAP.")."&nbsp;";
        $this->checks['acls']['ERROR_MSG']  .= "<input type='submit' name='migrate_acls' value='"._("Migrate")."'>";
        $this->checks['acls']['ERROR_MSG']  .= "<input type='submit' name='create_acls' value='"._("Create")."'>";
      } elseif ($FD_1_0_8_found) {
        $str = "";
        if (!empty($valid_users)) {
          $str .= "<b>"._("Users")."</b>:&nbsp;".trim($valid_users, ", ")."<br>";
        }
        if (!empty($valid_groups)) {
          $str .= "<b>"._("Groups")."</b>:&nbsp;".trim($valid_groups, ", ")."<br>";
        }
        $this->checks['acls']['STATUS']     = TRUE;
        $this->checks['acls']['STATUS_MSG'] = _("Ok");
        $this->checks['acls']['ERROR_MSG']  = $str;
        $this->valid_admin = $valid_admin;
      } else {
        $this->checks['acls']['STATUS']     = FALSE;
        $this->checks['acls']['STATUS_MSG'] = _("Failed");
        $this->checks['acls']['ERROR_MSG']  = _("There is no FusionDirectory administrator account inside your LDAP.")."&nbsp;";
        $this->checks['acls']['ERROR_MSG']  .= "<input type='submit' name='create_acls' value='"._("Create")."'>";
      }
    }

    // Reload base OC
    $this->checkBaseOC();
    return $FD_1_0_8_found;
  }

  /* Check if default roles and groupes have been inserted */
  function check_defaultACLs()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);
    $res = $ldap->cat($cv['base']);

    if (!$res) {
      $this->checks['default_acls']['STATUS']     = FALSE;
      $this->checks['default_acls']['STATUS_MSG'] = _("LDAP query failed");
      $this->checks['default_acls']['ERROR_MSG']  = _("Possibly the 'root object' is missing.");
      return FALSE;
    }

    $existings = 0;
    foreach ($this->defaultRoles as $role) {
      $dn = 'cn='.$role['cn'].','.$cv['aclroleou'].",".$cv['base'];
      $ldap->cat($dn, array('dn'));
      if ($ldap->count() > 0) {
        $existings++;
      }
    }
    $this->checks['default_acls']['STATUS'] = ($existings == count($this->defaultRoles));
    if ($existings == 0) {
      $this->checks['default_acls']['STATUS_MSG'] = _('Default ACL roles have not been inserted');
    } elseif ($existings < count($this->defaultRoles)) {
      $this->checks['default_acls']['STATUS_MSG'] = _('Some default ACL roles are missing');
    } else {
      $this->checks['default_acls']['STATUS_MSG'] = _('Default ACL roles have been inserted');
    }
    if ($this->checks['default_acls']['STATUS'] === FALSE) {
      $this->checks['default_acls']['ERROR_MSG'] = '&nbsp;<input type="submit"
          name="root_add_defaultroles" value="'._('Migrate').'"/>';
    } else {
      $this->checks['default_acls']['ERROR_MSG'] = '';
    }
  }

  function insert_defaultRoles()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);

    foreach ($this->defaultRoles as $role) {
      $dn = 'cn='.$role['cn'].','.$cv['aclroleou'].",".$cv['base'];
      $ldap->cat($dn);
      if ($ldap->count() == 0) {
        $ldap->cd($dn);
        $ldap->add($role);
        if (!$ldap->success()) {
          msg_dialog::display(
            _("Migration error"),
            sprintf(
              _("Cannot add ACL role '%s':")."<br/><br/><i>%s</i>",
              LDAP::fix($dn), $ldap->get_error()
            ),
            ERROR_DIALOG
          );
          return FALSE;
        }
      }
    }
    return TRUE;
  }

  function create_admin($only_ldif = FALSE)
  {
    /* Reset '' */
    $this->acl_create_changes = "";

    /* Object that should receive admin acls */
    $dn = $this->acl_create_selected;

    /* Get collected configuration settings */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    $ldap->search("(&(objectClass=gosaRole)(gosaAclTemplate=*:all;cmdrw))", array('dn'));
    if ($attrs = $ldap->fetch()) {
      $roledn = $attrs['dn'];
    } else {
      $roledn = 'cn=admin,'.$cv['aclroleou'].",".$cv['base'];
      if (!$only_ldif) {
        $ldap->create_missing_trees($cv['aclroleou'].",".$cv['base']);
        $ldap->cd($roledn);
        $attrs_role = array(
          'cn'              => 'admin',
          'description'     => _('Give all rights on all objects'),
          'objectclass'     => array( 'top', 'gosaRole' ),
          'gosaAclTemplate' => '0:all;cmdrw'
        );
        $ldap->add($attrs_role);
        if (!$ldap->success()) {
          msg_dialog::display(_("Migration error"), sprintf(_("Cannot add ACL role '%s':")."<br><br><i>%s</i>", LDAP::fix($roledn), $ldap->get_error()), ERROR_DIALOG);
          return FALSE;
        }
      }
    }

    /* Get current base attributes */
    $ldap->cd($cv['base']);
    $ldap->cat($cv['base'], array("dn","objectClass","gosaAclEntry"));
    $attrs = $ldap->fetch();

    /* Add acls for the selcted user to the base */
    $attrs_new = array();
    $attrs_new['objectClass'] = $attrs['objectClass'];
    unset($attrs_new['objectClass']['count']);
    if (!in_array_ics('gosaAcl', $attrs_new['objectClass'])) {
      $attrs_new['objectClass'][] = 'gosaAcl';
    }

    $acl = "0:subtree:".base64_encode($roledn).':'.base64_encode($dn); //FIXME
    $attrs_new['gosaAclEntry'][] = $acl;
    if (isset($attrs['gosaAclEntry'])) {
      for ($i = 0; $i < $attrs['gosaAclEntry']['count']; $i ++) {

        $prio = preg_replace("/[:].*$/", "", $attrs['gosaAclEntry'][$i]);
        $rest = preg_replace("/^[^:]+/", "", $attrs['gosaAclEntry'][$i]);

        $data = ($prio + 1).$rest;
        $attrs_new['gosaAclEntry'][] = $data;
      }
    }

    if ($only_ldif) {
      $this->acl_create_changes = "\n".($ldap->fix($cv['base']))."\n";
      $this->acl_create_changes .= $this->array_to_ldif($attrs)."\n";
      $this->acl_create_changes .= "\n".($ldap->fix($cv['base']))."\n";
      $this->acl_create_changes .= $this->array_to_ldif($attrs_new);
    } else {
      $ldap->cd($cv['base']);
      $ldap->modify($attrs_new);
      if (!$ldap->success()) {
        msg_dialog::display(_("Migration error"), sprintf(_("Cannot add ACL for user '%s':")."<br><br><i>%s</i>", LDAP::fix($dn), $ldap->get_error()), ERROR_DIALOG);
        return FALSE;
      } else {
        return TRUE;
      }
    }
  }


  function create_admin_user()
  {
    $pw1 = $pw2 = "";
    $uid = "";

    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    if (isset($_POST['new_user_uid'])) {
      $uid = $_POST['new_user_uid'];
    }
    if (isset($_POST['new_user_password'])) {
      $pw1 = $_POST['new_user_password'];
    }
    if (isset($_POST['new_user_password2'])) {
      $pw2 = $_POST['new_user_password2'];
    }

    $ldap->cd($cv['base']);
    $ldap->search("(uid=".$uid.")");
    if ($ldap->count()) {
      msg_dialog::display(_("Input error"), msgPool::duplicated(_("Uid")), ERROR_DIALOG);
      return FALSE;
    }

    if (empty($pw1) || empty($pw2) | ($pw1 != $pw2)) {
      msg_dialog::display(_("Password error"), _("Provided passwords do not match!"), ERROR_DIALOG);
      return FALSE;
    }

    if (!tests::is_uid($uid) || empty($uid)) {
      msg_dialog::display(_("Input error"), _("Specify a valid user ID!"), ERROR_DIALOG);
      return FALSE;
    }

    /* Get current base attributes */
    $ldap->cd($cv['base']);

    $people_ou = trim($cv['peopleou']);
    if (!empty($people_ou)) {
      $people_ou = trim($people_ou).",";
    }

    if ($cv['peopledn'] == "cn") {
      $dn = "cn=System Administrator-".$uid.",".$people_ou.$cv['base'];
    } else {
      $dn = "uid=".$uid.",".$people_ou.$cv['base'];
    }

    $hash = passwordMethod::make_hash($pw2, $cv['encryption']);

    $new_user = array();

    $new_user['objectClass']  = array("top","person","gosaAccount","organizationalPerson","inetOrgPerson");
    $new_user['givenName']    = "System";
    $new_user['sn']           = "Administrator";
    $new_user['cn']           = "System Administrator-".$uid;
    $new_user['uid']          = $uid;
    $new_user['userPassword'] = $hash;

    $ldap->cd($cv['base']);

    $ldap->cat($dn, array("dn"));
    if ($ldap->count()) {
      msg_dialog::display(_("Error"), sprintf(_("Adding an administrative user failed: object '%s' already exists!"), LDAP::fix($dn)), ERROR_DIALOG);
      return FALSE;
    }

    $ldap->create_missing_trees(preg_replace("/^[^,]+,/", "", $dn));
    $ldap->cd($dn);
    $res = $ldap->add($new_user);
    $this->acl_create_selected = $dn;
    $this->create_admin();

    if (!$res) {
      msg_dialog::display(_("LDAP error"), $ldap->get_error(), ERROR_DIALOG);
      return FALSE;
    }

    $this->acl_create_dialog = FALSE;
    $this->check_administrativeAccount();
    return TRUE;
  }

  function migrate_outside_groups($perform = FALSE)
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);

    /* Check if there was a destination department posted */
    if (isset($_POST['move_group_to'])) {
      $destination_dep = $_POST['move_group_to'];
    } else {
      msg_dialog::display(_("LDAP error"), _("Cannot move users to the requested department!"), ERROR_DIALOG);
      return FALSE;
    }

    foreach ($this->outside_groups as $b_dn => $data) {
      $this->outside_groups[$b_dn]['ldif'] = "";
      if ($data['selected']) {
        $dn = base64_decode($b_dn);
        $d_dn = preg_replace("/,.*$/", ",".base64_decode($destination_dep), $dn);
        if (!$perform) {

          $this->outside_groups[$b_dn]['ldif'] = _("Group will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));

          /* Check if there are references to this object */
          $ldap->search("(&(member=".LDAP::prepare4filter($dn).")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))", array('dn'));
          $refs = "";
          while ($attrs = $ldap->fetch()) {
            $ref_dn = $attrs['dn'];
            $refs .= "<br />\t".$ref_dn;
          }
          if (!empty($refs)) {
            $this->outside_groups[$b_dn]['ldif'] .= "<br /><br /><i>"._("Updating following references too").":</i>".$refs;
          }

        } else {
          $this->move($dn, $d_dn);
        }
      }
    }
  }


  function migrate_outside_users($perform = FALSE)
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);

    /* Check if there was a destination department posted */
    if (isset($_POST['move_user_to'])) {
      $destination_dep = $_POST['move_user_to'];
    } else {
      msg_dialog::display(_("LDAP error"), _("Cannot move users to the requested department!"), ERROR_DIALOG);
      return FALSE;
    }

    foreach ($this->outside_users as $b_dn => $data) {
      $this->outside_users[$b_dn]['ldif'] = "";
      if ($data['selected']) {
        $dn = base64_decode($b_dn);
        $d_dn = preg_replace("/,.*$/", ",".base64_decode($destination_dep), $dn);
        if (!$perform) {
          $this->outside_users[$b_dn]['ldif'] = _("User will be moved from").":<br>\t".($ldap->fix($dn))."<br>"._("to").":<br>\t".($ldap->fix($d_dn));

          /* Check if there are references to this object */
          $ldap->search("(&(member=".LDAP::prepare4filter($dn).")(|(objectClass=gosaGroupOfNames)(objectClass=groupOfNames)))", array('dn'));
          $refs = "";
          while ($attrs = $ldap->fetch()) {
            $ref_dn = $attrs['dn'];
            $refs .= "<br />\t".$ref_dn;
          }
          if (!empty($refs)) {
            $this->outside_users[$b_dn]['ldif'] .= "<br /><br /><i>"._("The following references will be updated").":</i>".$refs;
          }

        } else {
          $this->move($dn, $d_dn);
        }
      }
    }
  }


  function execute()
  {
    /* Initialise checks if this is the first call */
    if (!$this->checks_initialised || isset($_POST['reload'])) {
      $this->initialize_checks();
      $this->checks_initialised = TRUE;
    }

    /*************
     * Groups outside the group ou
     *************/

    if (isset($_POST['outside_groups_dialog_cancel'])) {
      $this->outside_groups_dialog  = FALSE;
      $this->show_details           = FALSE;
      $this->dialog                 = FALSE;
    }

    if (isset($_POST['outside_groups_dialog_whats_done'])) {
      $this->show_details = TRUE;
      $this->migrate_outside_groups(FALSE);
    }

    if (isset($_POST['outside_groups_dialog_refresh'])) {
      $this->show_details = FALSE;
    }

    if (isset($_POST['outside_groups_dialog_perform'])) {
      $this->migrate_outside_groups(TRUE);
      $this->dialog                 = FALSE;
      $this->show_details           = FALSE;
      $this->outside_groups_dialog  = FALSE;
      $this->initialize_checks();
    }

    if (isset($_POST['outside_groups_dialog'])) {
      $this->outside_groups_dialog  = TRUE;
      $this->dialog                 = TRUE;
    }

    if ($this->outside_groups_dialog) {

      /* Fix displayed dn syntax */
      $tmp = $this->outside_groups;
      foreach ($tmp as $key => $data) {
        $tmp[$key]['dn'] = LDAP::fix($data['dn']);
      }

      $smarty = get_smarty();
      $smarty->assign("ous", $this->get_all_group_ous());
      $smarty->assign("method", "outside_groups");
      $smarty->assign("outside_groups", $tmp);
      $smarty->assign("group_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * User outside the people ou
     *************/

    if (isset($_POST['outside_users_dialog_cancel'])) {
      $this->outside_users_dialog = FALSE;
      $this->dialog               = FALSE;
      $this->show_details         = FALSE;
    }

    if (isset($_POST['outside_users_dialog_whats_done'])) {
      $this->show_details = TRUE;
      $this->migrate_outside_users(FALSE);
    }

    if (isset($_POST['outside_users_dialog_perform'])) {
      $this->migrate_outside_users(TRUE);
      $this->initialize_checks();
      $this->dialog               = FALSE;
      $this->show_details         = FALSE;
      $this->outside_users_dialog = FALSE;
    }

    if (isset($_POST['outside_users_dialog_refresh'])) {
      $this->show_details = FALSE;
    }

    if (isset($_POST['outside_users_dialog'])) {
      $this->outside_users_dialog = TRUE;
      $this->dialog               = TRUE;
    }

    if ($this->outside_users_dialog) {

      /* Fix displayed dn syntax */
      $tmp = $this->outside_users;
      foreach ($tmp as $key => $data) {
        $tmp[$key]['dn'] = LDAP::fix($data['dn']);
      }

      $smarty = get_smarty();
      $smarty->assign("ous", $this->get_all_people_ous());
      $smarty->assign("method", "outside_users");
      $smarty->assign("outside_users", $tmp);
      $smarty->assign("user_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * Root object check
     *************/

    if (isset($_POST['retry_root_create'])) {

      $state = $this->checks['root']['STATUS'];
      $this->checkBase(FALSE);
      if ($state != $this->checks['root']['STATUS']) {
        $this->initialize_checks();
      }
    }

    /*************
     * Root object class check
     *************/

    if (isset($_POST['root_add_objectclasses'])) {
      $this->rootOC_migrate_dialog  = TRUE;
      $this->dialog                 = TRUE;
    }
    if (isset($_POST['rootOC_dialog_cancel'])) {
      $this->rootOC_migrate_dialog  = FALSE;
      $this->dialog                 = FALSE;
    }
    if (isset($_POST['rootOC_migrate_start'])) {
      if ($this->checkBaseOC(FALSE)) {
        $this->checkBaseOC(); // Update overview info
        $this->dialog                 = FALSE;
        $this->rootOC_migrate_dialog  = FALSE;
      }
    }

    if ($this->rootOC_migrate_dialog) {
      $smarty = get_smarty();
      $smarty->assign("details", $this->rootOC_details);
      $smarty->assign("method", "rootOC_migrate_dialog");
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * Administrative Account -- Migrate/Create
     *************/

    if (isset($_POST['retry_acls'])) {
      $this->check_administrativeAccount();
    }

    /* Dialog handling */
    if (isset($_POST['create_acls'])) {
      $this->acl_create_dialog  = TRUE;
      $this->dialog             = TRUE;
    }

    if (isset($_POST['migrate_acls'])) {
      $this->acl_migrate_dialog = TRUE;
      $this->dialog             = TRUE;
    }

    if (isset($_POST['create_acls_cancel']) || isset($_POST['migrate_acls_cancel'])) {
      $this->acl_create_dialog  = FALSE;
      $this->acl_migrate_dialog = FALSE;
      $this->dialog             = FALSE;
      $this->show_details       = FALSE;
    }

    /* Account creation */
    if (isset($_POST['create_acls_create'])) {
      $this->create_admin(TRUE);
    }

    if (isset($_POST['create_admin_user'])) {
      if ($this->create_admin_user()) {
        $this->dialog       = FALSE;
        $this->show_details = FALSE;
      }
    }

    if (isset($_POST['root_add_defaultroles'])) {
      $this->insert_defaultRoles();
      $this->check_defaultACLs();
    }

    /* Add admin acls for the selected users to the ldap base */
    if ($this->acl_migrate_dialog && isset($_POST['migrate_admin_user'])) {

      /* Update ldap and reload check infos */
      $this->migrate_selected_admin_users();
      $this->dialog             = FALSE;
      $this->acl_migrate_dialog = FALSE;

    } elseif ($this->acl_migrate_dialog) {

      /* Display admin migration dialog */
      $this->migrate_users();
      $smarty = get_smarty();

      /* Do we have to display the changes */
      $details = isset($_POST['details']) && $_POST['details'];
      if (isset($_POST['migrate_acls_show_changes'])) {
        $details = TRUE;
      } elseif (isset($_POST['migrate_acls_hide_changes'])) {
        $details = FALSE;
      }

      $smarty->assign("migrate_acl_base_entry", $this->migrate_acl_base_entry);
      $smarty->assign("details", $details);
      $smarty->assign("method", "migrate_acls");
      $smarty->assign("migrateable_users", $this->migrate_users);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    if ($this->acl_create_dialog) {
      $smarty = get_smarty();
      $uid = "fd-admin";
      if (isset($_POST['new_user_uid'])) {
        $uid = $_POST['new_user_uid'];
      }
      $smarty->assign("new_user_uid", $uid);
      $smarty->assign("new_user_password", @$_POST['new_user_password']);
      $smarty->assign("new_user_password2", @$_POST['new_user_password2']);
      $smarty->assign("method", "create_acls");
      $smarty->assign("acl_create_selected", $this->acl_create_selected);
      $smarty->assign("what_will_be_done_now", $this->acl_create_changes);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * User Migration handling
     *************/

    /* Refresh list of deparments */
    if (isset($_POST['users_visible_migrate_refresh'])) {
      $this->check_gosaAccounts();
    }

    /* Open migration dialog */
    if (isset($_POST['users_visible_migrate'])) {
      $this->show_details           = FALSE;
      $this->users_migration_dialog = TRUE;
      $this->dialog                 = TRUE;
    }

    /* Close migration dialog */
    if (isset($_POST['users_visible_migrate_close'])) {
      $this->users_migration_dialog = FALSE;
      $this->dialog                 = FALSE;
      $this->show_details           = FALSE;
    }

    /* Start migration */
    if (isset($_POST['users_visible_migrate_migrate'])) {
      if ($this->migrate_gosaAccounts()) {
        $this->initialize_checks();
        $this->dialog                 = FALSE;
        $this->show_details           = FALSE;
        $this->users_migration_dialog = FALSE;
      }
    }

    /* Start migration */
    if (isset($_POST['users_visible_migrate_whatsdone'])) {
      $this->migrate_gosaAccounts(TRUE);
    }

    /* Display migration dialog */
    if ($this->users_migration_dialog) {

      /* Fix displayed dn syntax */
      $tmp = $this->users_to_migrate;
      foreach ($tmp as $key => $data) {
        $tmp[$key]['dn'] = LDAP::fix($data['dn']);
      }

      $smarty = get_smarty();
      $smarty->assign("users_to_migrate", $tmp);
      $smarty->assign("method", "migrate_users");
      $smarty->assign("user_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * Department Migration handling
     *************/

    /* Refresh list of deparments */
    if (isset($_POST['deps_visible_migrate_refresh'])) {
      $this->check_organizationalUnits();
      $this->show_details = FALSE;
    }

    /* Open migration dialog */
    if (isset($_POST['deps_visible_migrate'])) {
      $this->dep_migration_dialog = TRUE;
      $this->dialog               = TRUE;
    }

    /* Close migration dialog */
    if (isset($_POST['deps_visible_migrate_close'])) {
      $this->dep_migration_dialog = FALSE;
      $this->dialog               = FALSE;
      $this->show_details         = FALSE;
    }

    /* Start migration */
    if (isset($_POST['deps_visible_migrate_migrate'])) {
      if ($this->migrate_organizationalUnits()) {
        $this->check_organizationalUnits();
        $this->show_details         = FALSE;
        $this->dialog               = FALSE;
        $this->dep_migration_dialog = FALSE;
      }
    }

    /* Start migration */
    if (isset($_POST['deps_visible_migrate_whatsdone'])) {
      $this->migrate_organizationalUnits(TRUE);
    }

    /* Display migration dialog */
    if ($this->dep_migration_dialog) {
      $smarty = get_smarty();

      /* Fix displayed dn syntax */
      $tmp = $this->deps_to_migrate;
      foreach ($tmp as $key => $data) {
        $tmp[$key]['dn'] = LDAP::fix($data['dn']);
      }

      $smarty->assign("deps_to_migrate", $tmp);
      $smarty->assign("method", "migrate_deps");
      $smarty->assign("deps_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * Device migration
     *************/

    if ($this->device_dialog) {
      $this->check_device_posts();
    }

    if (isset($_POST['device_dialog_cancel'])) {
      $this->device_dialog  = FALSE;
      $this->show_details   = FALSE;
      $this->dialog         = FALSE;
    }

    if (isset($_POST['device_dialog_whats_done'])) {
      $this->show_details = TRUE;
    }

    if (isset($_POST['device_dialog_refresh'])) {
      $this->show_details = FALSE;
    }

    if (isset($_POST['migrate_devices'])) {
      $this->migrate_usb_devices();
    }

    if (isset($_POST['device_dialog'])) {
      $this->device_dialog  = TRUE;
      $this->dialog         = TRUE;
    }

    if ($this->device_dialog) {
      $smarty = get_smarty();
      $smarty->assign("method", "devices");
      $smarty->assign("devices", $this->device);
      $smarty->assign("device_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * Service migration
     *************/

    if ($this->service_dialog) {
      $this->check_service_posts();
    }

    if (isset($_POST['service_dialog_cancel'])) {
      $this->service_dialog = FALSE;
      $this->show_details   = FALSE;
      $this->dialog         = FALSE;
    }

    if (isset($_POST['service_dialog_whats_done'])) {
      $this->show_details = TRUE;
    }

    if (isset($_POST['service_dialog_refresh'])) {
      $this->show_details = FALSE;
    }

    if (isset($_POST['migrate_services'])) {
      $this->migrate_services();
    }

    if (isset($_POST['service_dialog'])) {
      $this->service_dialog = TRUE;
      $this->dialog         = TRUE;
    }

    if ($this->service_dialog) {
      $smarty = get_smarty();
      $smarty->assign("method", "services");
      $smarty->assign("services", $this->service);
      $smarty->assign("service_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    /*************
     * Menu migration
     *************/

    if ($this->menu_dialog) {
      $this->check_menu_posts();
    }

    if (isset($_POST['menu_dialog_cancel'])) {
      $this->menu_dialog  = FALSE;
      $this->show_details = FALSE;
      $this->dialog       = FALSE;
    }

    if (isset($_POST['menu_dialog_whats_done'])) {
      $this->show_details = TRUE;
    }

    if (isset($_POST['menu_dialog_refresh'])) {
      $this->show_details = FALSE;
    }

    if (isset($_POST['migrate_menus'])) {
      $this->migrate_menus();
    }

    if (isset($_POST['menu_dialog'])) {
      $this->menu_dialog  = TRUE;
      $this->dialog       = TRUE;
    }

    if ($this->menu_dialog) {
      $smarty = get_smarty();
      $smarty->assign("method", "menus");
      $smarty->assign("menus", $this->menu);
      $smarty->assign("menu_details", $this->show_details);
      return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
    }

    $smarty = get_smarty();
    $smarty->assign("checks", $this->checks);
    $smarty->assign("method", "default");
    return $smarty->fetch(get_template_path("setup_migrate.tpl", TRUE, dirname(__FILE__)));
  }


  function save_object()
  {
    $this->is_completed = TRUE;

    /* Capture all selected groups from outside_groups_dialog */
    if ($this->outside_groups_dialog) {
      foreach ($this->outside_groups as $dn => $data) {
        if (isset($_POST['select_group_'.$dn])) {
          $this->outside_groups[$dn]['selected'] = TRUE;
        } else {
          $this->outside_groups[$dn]['selected'] = FALSE;
        }
      }
    }

    /* Capture all selected users from outside_users_dialog */
    if ($this->outside_users_dialog) {
      foreach ($this->outside_users as $dn => $data) {
        if (isset($_POST['select_user_'.$dn])) {
          $this->outside_users[$dn]['selected'] = TRUE;
        } else {
          $this->outside_users[$dn]['selected'] = FALSE;
        }
      }
    }

    /* Get "create acl" dialog posts */
    if ($this->acl_create_dialog) {

      if (isset($_POST['create_acls_create_abort'])) {
        $this->acl_create_selected = "";
      }
    }

    /* Get selected departments */
    if ($this->dep_migration_dialog) {
      foreach ($this->deps_to_migrate as $id => $data) {
        if (isset($_POST['migrate_'.$id])) {
          $this->deps_to_migrate[$id]['checked'] = TRUE;
        } else {
          $this->deps_to_migrate[$id]['checked'] = FALSE;
        }
      }
    }

    /* Get selected users */
    if ($this->users_migration_dialog) {
      foreach ($this->users_to_migrate as $id => $data) {
        if (isset($_POST['migrate_'.$id])) {
          $this->users_to_migrate[$id]['checked'] = TRUE;
        } else {
          $this->users_to_migrate[$id]['checked'] = FALSE;
        }
      }
    }
  }


  /* Check if the root object exists.
   * If the parameter just_check is TRUE, then just check if the
   *  root object is missing and update the info messages.
   * If the Parameter is FALSE, try to create a new root object.
   */
  function checkBase($just_check = TRUE)
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Check if root object exists */
    $ldap->cd($cv['base']);
    $ldap->set_size_limit(1);
    $res = $ldap->search("(objectClass=*)");
    $ldap->set_size_limit(0);
    $err = ldap_errno($ldap->cid);

    if ( !$res ||
        $err == 0x20 || // LDAP_NO_SUCH_OBJECT
        $err == 0x40) { // LDAP_NAMING_VIOLATION

      /* Root object doesn't exists */
      if ($just_check) {
        $this->checks['root']['STATUS']     = FALSE;
        $this->checks['root']['STATUS_MSG'] = _("Failed");
        $this->checks['root']['ERROR_MSG']  = _("The LDAP root object is missing. It is required to use your LDAP service.").'&nbsp;';
        $this->checks['root']['ERROR_MSG']  .= "<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
        return FALSE;
      } else {

        /* Add root object */
        $ldap->cd($cv['base']);
        $res = $ldap->create_missing_trees($cv['base']);

        /* If adding failed, tell the user */
        if (!$res) {
          $this->checks['root']['STATUS']     = FALSE;
          $this->checks['root']['STATUS_MSG'] = _("Failed");
          $this->checks['root']['ERROR_MSG']  = _("Root object couldn't be created, you should try it on your own.");
          $this->checks['root']['ERROR_MSG']  .= "&nbsp;<input type='submit' name='retry_root_create' value='"._("Try to create root object")."'>";
          return $res;
        }
      }
    }

    /* Create & remove of dummy object was successful */
    $this->checks['root']['STATUS']     = TRUE;
    $this->checks['root']['STATUS_MSG'] = _("Ok");
  }


  /* Check if the root object includes the required object classes, e.g. gosaDepartment is required for ACLs.
   * If the parameter just_check is TRUE, then just check for the OCs.
   * If the Parameter is FALSE, try to add the required object classes.
   */
  function checkBaseOC($just_check = TRUE)
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Check if root object exists */
    $ldap->cd($cv['base']);
    $ldap->cat($cv['base']);
    if (!$ldap->count()) {
      $this->checks['rootOC']['STATUS']     = FALSE;
      $this->checks['rootOC']['STATUS_MSG'] = _("LDAP query failed");
      $this->checks['rootOC']['ERROR_MSG']  = _("Possibly the 'root object' is missing.");
      return;
    }

    $attrs = $ldap->fetch();

    /* Root object doesn't exists */
    if (!in_array("gosaDepartment", $attrs['objectClass'])) {
      if ($just_check) {

        $this->rootOC_details = array();
        $mods = array();

        /* Get list of possible container objects, to be able to detect naming
         *  attributes and missing attribute types.
         */
        if (!class_available("departmentManagement")) {
          $this->checks['rootOC']['STATUS']     = FALSE;
          $this->checks['rootOC']['STATUS_MSG'] = _("Failed");
          $this->checks['rootOC']['ERROR_MSG']  = sprintf(_("Missing FusionDirectory object class '%s'!"), "departmentManagement").
            "&nbsp;"._("Please check your installation.");
          return;
        }

        /* Create a minimal config object for objectType infos */
        global $config, $plist;
        $config = new config("");
        load_all_classes();
        $plist  = new pluglist($config, $ui);
        $config->loadPlist($plist);

        /* Try to detect base class type, e.g. is it a dcObject */
        $dep_types  = departmentManagement::getDepartmentTypes();
        $dep_type   = "";
        $attrs['objectClass'][] = 'gosaDepartment'; // This allow us to filter it as if it was already migrated
        foreach ($dep_types as $type) {
          if (objects::isOfType($attrs, $type)) {
            $dep_type = $type;
            break;
          }
        }
        $key = array_search('gosaDepartment', $attrs['objectClass']);
        unset($attrs['objectClass'][$key]);


        /* If no known base class was detect, abort with message */
        if (empty($dep_type)) {
          $this->checks['rootOC']['STATUS']     = FALSE;
          $this->checks['rootOC']['STATUS_MSG'] = _("Failed");
          $this->checks['rootOC']['ERROR_MSG']  =
            sprintf(_("Cannot handle the structural object type of your root object. Please try to add the object class '%s' manually."), "gosaDepartment");
          return;
        }
        $dep_infos = objects::infos($dep_type);

        /* Create 'current' and 'target' object properties, to be able to display
         *  a set of modifications required to create a valid FusionDirectory department.
         */
        $str = "dn: ".$cv['base']."\n";
        for ($i = 0; $i < $attrs['objectClass']['count']; $i++) {
          $str .= "objectClass: ".$attrs['objectClass'][$i]."\n";
        }
        $this->rootOC_details['current'] = $str;

        /* Create target infos */
        $str = "dn: ".$cv['base']."\n";
        for ($i = 0; $i < $attrs['objectClass']['count']; $i++) {
          $str .= "objectClass: ".$attrs['objectClass'][$i]."\n";
          $mods['objectClass'][] = $attrs['objectClass'][$i];
        }
        $mods['objectClass'][] = "gosaDepartment";

        $str .= "<b>objectClass: gosaDepartment</b>\n";

        /* Append attribute 'ou', it is required by gosaDepartment */
        if (!isset($attrs['ou'])) {
          $val = "GOsa";
          if (isset($attrs[$dep_infos['mainAttr']][0])) {
            $val = $attrs[$dep_infos['mainAttr']][0];
          }
          $str .= "<b>ou: ".$val."</b>\n";

          $mods['ou'] = $val;
        }

        /*Append description, it is required by gosaDepartment too */
        if (!isset($attrs['description'])) {
          $val = "GOsa";
          if (isset($attrs[$dep_infos['mainAttr']][0])) {
            $val = $attrs[$dep_infos['mainAttr']][0];
          }
          $str .= "<b>description: ".$val."</b>\n";

          $mods['description'] = $val;
        }
        $this->rootOC_details['target'] = $str;
        $this->rootOC_details['mods']   = $mods;

        /*  Add button that allows to open the migration details */
        $this->checks['rootOC']['STATUS']     = FALSE;
        $this->checks['rootOC']['STATUS_MSG'] = _("Failed");
        $this->checks['rootOC']['ERROR_MSG']  = "&nbsp;<input type='submit'
          name='root_add_objectclasses' value='"._("Migrate")."'>";

        return FALSE;
      } else {

        /* Add root object */
        $ldap->cd($cv['base']);
        if (isset($this->rootOC_details['mods'])) {
          $res  = $ldap->modify($this->rootOC_details['mods']);
          if (!$res) {
            msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $cv['base'], LDAP_MOD, get_class()), LDAP_ERROR);
          }
          $this->checkBaseOC();
          $this->check_administrativeAccount();
          return $res;
        } else {
          trigger_error("No modifications to make... ");
        }
      }
      return TRUE;
    }

    /* Create & remove of dummy object was successful */
    $this->checks['rootOC']['STATUS']     = TRUE;
    $this->checks['rootOC']['STATUS_MSG'] = _("Ok");
    $this->checks['rootOC']['ERROR_MSG']  = "";
  }


  /* Return ldif information for a
   * given attribute array
   */
  function array_to_ldif($atts)
  {
    $ret = "";
    unset($atts['count']);
    unset($atts['dn']);
    foreach ($atts as $name => $value) {
      if (is_numeric($name)) {
        continue;
      }
      if (is_array($value)) {
        unset($value['count']);
        foreach ($value as $a_val) {
          $ret .= $name.": ". $a_val."\n";
        }
      } else {
        $ret .= $name.": ". $value."\n";
      }
    }
    return preg_replace("/\n$/", "", $ret);
  }


  function get_user_list($class = 'gosaAccount')
  {
    /* Establish ldap connection */
    $ldap = $this->get_ldap_link();
    $ldap->search("(objectClass=$class)", array("dn"));

    $tmp = array();
    while ($attrs = $ldap->fetch()) {
      $tmp[base64_encode($attrs['dn'])] = LDAP::fix($attrs['dn']);
    }
    return $tmp;
  }


  function get_group_list()
  {
    return $this->get_user_list('posixGroup');
  }


  function get_all_people_ous()
  {
    return $this->get_all_ous('peopleou');
  }

  function get_all_group_ous()
  {
    return $this->get_all_ous('groupou');
  }

  function get_all_ous($cv_key)
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ou   = trim($cv[$cv_key]);

    /************
     * If ou is NOT empty
     * Get all valid ous, create one if necessary
     ************/
    if (!empty($ou)) {
      $ldap->search("($ou)", array('dn'));
      if ($ldap->count() == 0 ) {
        $ldap->create_missing_trees($ou.$cv['base']);
      }
      $ldap->search("($ou)", array('dn'));
      $tmp = array();
      while ($attrs = $ldap->fetch()) {
        if (!preg_match('/ou=snapshots,/', $attrs['dn'])) {
          $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);
        }
      }
    } else {
      /************
       * If ou is empty
       * Get all valid gosaDepartments
       ************/
      $ldap->cd($cv['base']);
      $tmp = array();
      $ldap->search('(&(objectClass=gosaDepartment)(ou=*))', array('dn'));
      $tmp[base64_encode($cv['base'])] = $ldap->fix($cv['base']);
      while ($attrs = $ldap->fetch()) {
        $tmp[base64_encode($attrs['dn'])] = $ldap->fix($attrs['dn']);;
      }
    }
    return $tmp;
  }


  function move($source, $destination)
  {
    /* Establish ldap connection */
    $ldap = $this->get_ldap_link();

     /* Update object references in gosaGroupOfNames */
    $ogs_to_fix = array();
    $ldap->search('(&(objectClass=gosaGroupOfNames)(member='.@LDAP::prepare4filter($source).'))', array('cn','member'));
    while ($attrs = $ldap->fetch()) {
      $dn = $attrs['dn'];
      $attrs = $this->cleanup_array($attrs);
      $member_new = array($destination);
      foreach ($attrs['member'] as $member) {
        if ($member != $source) {
          $member_new[] = $member;
        }
      }
      $attrs['member'] = $member_new;
      $ogs_to_fix[$dn] = $attrs;
    }

    /* Copy source to destination dn */
    $ldap->cat($source);
    $new_data = $this->cleanup_array($ldap->fetch());
    $ldap->cd($destination);
    $res = $ldap->add($new_data);

    /* Display warning if copy failed */
    if (!$res) {
      msg_dialog::display(_("LDAP error"), sprintf(_("Copy '%s' to '%s' failed:")."<br><br><i>%s</i>", LDAP::fix($source), LDAP::fix($destination), $ldap->get_error()), ERROR_DIALOG);
    } else {
      $res = $ldap->rmDir($source);
      if (!$ldap->success()) {
        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $source, LDAP_DEL, get_class()), LDAP_ERROR);
      }

      /* Object is copied, so update its references */
      foreach ($ogs_to_fix as $dn => $data) {
        $ldap->cd($dn);
        $ldap->modify($data);
      }
    }
  }


  /* Cleanup ldap result to be able to write it be to ldap */
  function cleanup_array($attrs)
  {
    foreach ($attrs as $key => $value) {
      if (is_numeric($key) || in_array($key, array("count","dn"))) {
        unset($attrs[$key]);
      }
      if (is_array($value) && isset($value['count'])) {
        unset($attrs[$key]['count']);
      }
    }
    return $attrs;
  }


  /*! \brief  Act in posts from the device migration dialog
   */
  function check_device_posts()
  {
    foreach (array_keys($this->device) as $key) {
      if (isset($_POST["migrate_".$key])) {
        $this->device[$key]['DETAILS'] = TRUE;
      } else {
        $this->device[$key]['DETAILS'] = FALSE;
      }
    }
  }


  /*! \brief  Check for old style (gosa-2.5) devices.
              Save readable informations and a list of migratable devices
               in $this->devices.
   */
  function check_usb_devices ()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);
    $res = $ldap->search("(&(|(objectClass=posixAccount)(objectClass=posixGroup))(gotoHotplugDevice=*))",
        array("cn","gotoHotplugDevice"));

    if (!$res) {
      $this->checks['old_style_devices']['STATUS']      = FALSE;
      $this->checks['old_style_devices']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['old_style_devices']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
      return;
    }

    /* If adding failed, tell the user */
    if ($ldap->count()) {

      $this->device = array();
      while ($attrs = $ldap->fetch()) {

        for ($j = 0; $j < $attrs['gotoHotplugDevice']['count']; $j++) {

          $after    = "";
          $current  = "";

          $entry = $attrs['gotoHotplugDevice'][$j];

          @list($name, $desc, $serial, $vendor, $product) = explode('|', $entry);

          $add      = 1;
          $new_name = $name;
          while (isset($dest[$new_name])) {
            $new_name = $name."_".$add;
            $add ++;
          }
          $name   = $new_name;
          $newdn  = "cn=$name,ou=devices,".preg_replace('/^[^,]+,/', '', $attrs['dn']);

          if (!isset($dest[$name])) {
            $dest[$name] = $newdn;

            $current .= "dn: ".$attrs['dn']."\n";

            for ($c = 0; $c < $attrs['gotoHotplugDevice']['count']; $c++) {
              if ($c == $j) {
                $current .= "<b>gotoHotplugDevice: ".$attrs['gotoHotplugDevice'][$c]."</b>\n";
              } else {
                $current .= "gotoHotplugDevice: ".$attrs['gotoHotplugDevice'][$c]."\n";
              }
            }

            $after .= "dn: $newdn\n";
            $after .= "changetype: add\n";
            $after .= "objectClass: top\n";
            $after .= "objectClass: gotoDevice\n";
            $after .= "cn: $name\n";
            $after .= "gotoHotplugDevice: $desc|$serial|$vendor|$product\n\n";

            $this->device[] = array(
                'CURRENT'     => $current,
                'AFTER'       => $after,
                'OLD_DEVICE'  => $entry,
                'DN'          => $attrs['dn'],
                'NEW_DN'      => $newdn,
                'DEVICE_NAME' => $name,
                'DETAILS'     => FALSE);
          }
        }
      }

      $this->checks['old_style_devices']['STATUS']      = FALSE;
      $this->checks['old_style_devices']['STATUS_MSG']  = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['old_style_devices']['ERROR_MSG']   =
        sprintf(_("There are %s devices that need to be migrated."), count($this->device)).
          "<input type='submit' name='device_dialog' value='"._("Migrate")."'>";
    } else {
      $this->checks['old_style_devices']['STATUS']      = TRUE;
      $this->checks['old_style_devices']['STATUS_MSG']  = _("Ok");
      $this->checks['old_style_devices']['ERROR_MSG']   = "";
    }
  }


  /*! \brief  Migrate all selected devices.
              Execute all required ldap actions to migrate the
               selected devices.
   */
  function migrate_usb_devices ()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Walk through migrateable devices and initiate migration for all
        devices that are checked (DETAILS==TRUE)
     */
    foreach ($this->device as $key => $device) {
      if ($device['DETAILS']) {

        /* Get source object and verify that the specified device is a
            member attribute of it.
         */
        $ldap->cd($cv['base']);
        $ldap->cat($device['DN']);
        $attrs = $ldap->fetch();
        if (in_array($device['OLD_DEVICE'], $attrs['gotoHotplugDevice'])) {

          /* Create new hotplug device object 'gotoDevice' */
          @list($name, $desc, $serial, $vendor, $product) = explode('|', $device['OLD_DEVICE']);
          $newdn = $device['NEW_DN'];
          $new_attr = array();
          $new_attr['cn'] = $device['DEVICE_NAME'];
          $new_attr['objectClass'] = array('top','gotoDevice');
          $new_attr['gotoHotplugDevice'] = "$desc|$serial|$vendor|$product";

          /* Add new object */
          $ldap->cd($cv['base']);
          $ldap->create_missing_trees(preg_replace("/^[^,]+,/", "", $newdn));
          $ldap->cd($newdn);
          $ldap->add($new_attr);

          /* Throw an error message if the action failed */
          if (!$ldap->success()) {
            msg_dialog::display(_("LDAP error"),
                sprintf(_("Adding '%s' to the LDAP failed: %s"),
                  "<b>".LDAP::fix($newdn)."</b>",
                  "<br><br><i>".$ldap->get_error()."</i>"), ERROR_DIALOG);
          } else {

            /* Remove old style device definition from source object */
            $update['gotoHotplugDevice'] = array();
            for ($i = 0; $i < $attrs['gotoHotplugDevice']['count']; $i++) {
              if ($attrs['gotoHotplugDevice'][$i] == $device['OLD_DEVICE']) {
                 continue;
              }
              $update['gotoHotplugDevice'][] = $attrs['gotoHotplugDevice'][$i];
            }

            $ldap->cd($device['DN']);
            $ldap->modify($update);
            $ldap->cat($device['DN'], array("gotoHotplugDevice"));
            if (!$ldap->success()) {
              msg_dialog::display(_("LDAP error"),
                  sprintf(_("Updating '%s' failed: %s"),
                    "<b>".LDAP::fix($device['DN'])."</b>",
                    "<br><br><i>".$ldap->get_error()."</b>"), ERROR_DIALOG);
            } else {
              unset($this->device[$key]);
            }
          }
        }
      }
    }
    $this->check_usb_devices();
  }


  /*! \brief  Check for old style (FD < 1.0.5) services that have to be migrated
               to be useable in FD >= 1.0.5.
              All required changes are stored in $this->service, also some
               readable informations describing the actions required
               to migrate the service
   */
  function check_services()
  {
    /* Establish ldap connection */
    $cv     = $this->parent->captured_values;
    $ldap_l = new LDAP(
      $cv['admin'], $cv['password'],
      $cv['connection'], FALSE,
      $cv['tls']
    );

    $ldap = new ldapMultiplexer($ldap_l);
    $this->service = array();

    /* Check for Ldap services that must be migrated */
    $ldap->cd($cv['base']);
    $res = $ldap->search('(objectClass=goLdapServer)');

    /* Check if we were able to query the ldap server */
    if (!$res) {
      $this->checks['old_style_services']['STATUS']     = FALSE;
      $this->checks['old_style_services']['STATUS_MSG'] = _("LDAP query failed");
      $this->checks['old_style_services']['ERROR_MSG']  = _("Possibly the 'root object' is missing.");
      return;
    }

    /* Walk through each configured ldap server
        and check if it is configured correctly.
     */
    while ($attrs = $ldap->fetch()) {
      if (!isset($attrs['goLdapURI'])) {
        $dn   = $attrs['dn'];
        $uri  = $attrs['goLdapBase'][0];
        if (preg_match("!^(ldaps?://[^/]+)/(.+)$!", $uri, $m)) {
          $this->service[] = array(
            'TYPE'    => 'modify' ,
            'DN'      => $dn,
            'DETAILS' => FALSE,
            'ATTRS'   => array('goLdapBase' => $m[2], 'goLdapURI' => $m[1]),
            'CURRENT' => 'goLdapBase: '.$uri,
            'AFTER'   => 'goLdapBase: '.$m[2].', goLdapURI: '.$m[1]
          );
        }
      }
    }

    /* Other sevices following here later ...maybe */

    /*  Update status message */
    if (count($this->service)) {
      $this->checks['old_style_services']['STATUS']     = FALSE;
      $this->checks['old_style_services']['STATUS_MSG'] = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['old_style_services']['ERROR_MSG']  =
        sprintf(_("There are %s services that need to be migrated."),
            count($this->service)).
        "<input type='submit' name='service_dialog' value='"._("Migrate")."'>";
    } else {
      $this->checks['old_style_services']['STATUS']     = TRUE;
      $this->checks['old_style_services']['STATUS_MSG'] = _("Ok");
      $this->checks['old_style_services']['ERROR_MSG']  = "";
    }
  }



  /*! \brief  Migrate selected services.
              This function executes the commands collected by the
               service_check() function.
   */
  function migrate_services()
  {
    /* Establish ldap connection */
    $ldap = $this->get_ldap_link();

    /* Handle each service */
    foreach ($this->service as $key => $service) {
      if ($service['DETAILS']) {
        /* Handle modify requests */
        if ($service['TYPE'] == "modify") {
          $ldap->cd($service['DN']);
          $ldap->modify($service['ATTRS']);

          /* Check if everything done was successful */
          if (!$ldap->success()) {
            msg_dialog::display(_("LDAP error"),
                sprintf(_("Updating '%s' failed: %s"),
                  "<b>".LDAP::fix($service['DN'])."</b>",
                  "<br><br><i>".$ldap->get_error()."</b>"), ERROR_DIALOG);
          } else {
            /* Remove action from list */
            unset($this->service[$key]);
          }
        }
      }
    }

    /* Update the service migration status */
    $this->check_services();
  }


  /*! \brief  Ensure that posts made on the service migration dialog
               are processed.
   */
  function check_service_posts()
  {
    foreach (array_keys($this->service) as $key) {
      if (isset($_POST["migrate_".$key])) {
        $this->service[$key]['DETAILS'] = TRUE;
      } else {
        $this->service[$key]['DETAILS'] = FALSE;
      }
    }
  }


  /*! \brief  This function checks the given ldap for old style (gosa-2.5)
               menu entries and will prepare a list of actions that are required
               to migrate them to gosa-2.6.
              All required actions and some readable informations are stored in
               $this->menu.
   */
  function check_menus()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* First detect all release names */
    $ldap->cd($cv['base']);
    $res = $ldap->search("(&(objectClass=organizational)(objectClass=FAIbranch))", array("ou","objectClass"));

    /* Check if we were able to query the ldap server */
    if (!$res) {
      $this->checks['old_style_menus']['STATUS']      = FALSE;
      $this->checks['old_style_menus']['STATUS_MSG']  = _("LDAP query failed");
      $this->checks['old_style_menus']['ERROR_MSG']   = _("Possibly the 'root object' is missing.");
      return;
    }

    /* Create application -> parameter mapping, used later to detect
        which configured parameter belongs to which application entry.
     */
    $amap = array();
    $todo = array();
    $ldap->cd($cv['base']);
    $ldap->search("(objectClass=gosaApplication)", array("cn", "gosaApplicationParameter"));
    while ($info = $ldap->fetch()) {
      if (isset($info['gosaApplicationParameter']['count'])) {
        for ($j = 0; $j < $info['gosaApplicationParameter']['count']; $j++) {
          $p = preg_replace("/^([^:]+):.*$/", "$1", $info['gosaApplicationParameter'][$j]);

          if (!isset($amap[$info['cn'][0]]) || !in_array($p, $amap[$info['cn'][0]])) {
            $amap[$info['cn'][0]][] = $p;
          }
        }
      } else {
        $amap[$info['cn'][0]] = array();
      }
    }

    /* Search for all groups that have an old style application menu configured */
    $ldap->cd($cv['base']);
    $ldap->search("(&(objectClass=gosaApplicationGroup)(objectClass=posixGroup)(FAIrelease=*))",
        array("gosaMemberApplication","gosaApplicationParameter","FAIrelease","objectClass"));

    /* Create readable prefix for "What will be done" infos */
    $s_add = "<i>"._("Add")."</i>\t";
    $s_del = "<i>"._("Remove")."</i>\t";

    /* Walk through all found old-style menu configurations.
        -Prepare ldap update list     $data
        -Prepare printable changes    $after/$current
     */
    while ($info = $ldap->fetch()) {

      $data     = array();
      $current  = "";
      $after    = "";

      /* Collect application parameter for this group */
      $params = array();
      if (isset($info['gosaApplicationParameter'])) {
        for ($i = 0; $i < $info['gosaApplicationParameter']['count']; $i++) {
          $name = preg_replace("/^([^:]+):.*$/", "$1", $info['gosaApplicationParameter'][$i]);
          $params[$name] = $info['gosaApplicationParameter'][$i];
        }
      }

      /* Create release container for each release/subrelease.
         eg.   "sisa/1.0.0":
         .       "ou=siga, ..."
         .       "ou=1.0.0,ou=siga, .."
       */
      $release  = "";
      $r        = $info['FAIrelease'][0];
      $z        = explode("/", $r);
      foreach ($z as $part) {

        if (!empty($part)) {
          $release = "ou=".$part.",".$release;

          /* Append release department information to "What will be done" info */
          $release_dn = $release.$info['dn'];
          $after      .= $s_add."dn: $release_dn\n";
          $after      .= $s_add."objectClass: top\n";
          $after      .= $s_add."objectClass: FAIbranch\n";
          $after      .= $s_add."objectClass: organizationalUnit\n";

          $after   .= $s_add."ou: $part\n";

          /* Append release data to ldap actions */
          $d = array();
          $d['objectClass'] = array("top","FAIbranch","organizationalUnit");
          $d['ou'] = $part;

          $data['ADD'][$release_dn] = $d;
        }
      }

      /* Add member applications to the array */
      $current .= "dn: ".$info['dn']."\n";
      $menu_structure = array();
      for ($i = 0; $i < $info['gosaMemberApplication']['count']; $i++) {
        list($name, $location, $priority) = explode("|", $info['gosaMemberApplication'][$i]);

        /* Create location dn */
        $location_dn = "";
        if (!empty($location)) {
          $location_dn = "cn=".$location.",";
        }

        /* Append old style element to current detail informations */
        $current .= $s_del."gosaMemberApplication: ".$info['gosaMemberApplication'][$i]."\n";

        /* Append ldap update action to remove the old menu entry attributes */
        unset($info['objectClass']['count']);
        $d = array();
        $d['gosaMemberApplication']      = array();
        $d['gosaApplicationParameter']  = array();
        if (isset($info['FAIrelease'])) {
          $d['FAIrelease'] = array();
        }
        $d['objectClass']               = array_remove_entries(array("gosaApplicationGroup","FAIreleaseTag"), $info['objectClass']);
        $data['MODIFY'][$info['dn']]    = $d;

        /* Create new application menu structure */
        if (isset($amap[$name])) {

          /* Append missing menu structure to "What is done info" */
          if (!isset($menu_structure[$location]) && !empty($location)) {
            $menu_structure[$location] = TRUE;

            $after .= "\n";
            $after .= $s_add."dn: $location_dn$release_dn\n";
            $after .= $s_add."objectClass: gotoSubmenuEntry\n";

            $after .= $s_add."cn: $location\n";

            /* Create ldap entry to append */
            $d = array();
            $d['cn'] = $location;
            $d['objectClass'] = array("gotoSubmenuEntry");
            $data['ADD'][$location_dn.$release_dn] = $d;
          }

          /* Append missing menu entry for "What is done info" */
          if (!empty($name)) {
            $after .= "\n";
            $after .= $s_add."dn: cn=$name,$location_dn$release_dn\n";
            $after .= $s_add."objectClass: gotoMenuEntry\n";
            $after .= $s_add."cn: $name\n";
            $after .= $s_add."gosaApplicationPriority: $priority\n";

            /* Create ldap entry */
            $d = array();
            $d['objectClass'] = array("gotoMenuEntry");
            $d['cn']                      = $name;
            $d['gosaApplicationPriority'] = $priority;

            foreach ($amap[$name] as $n) {
              if (isset($params[$n])) {
                $after .= $s_add."gosaApplicationParameter: ".$params[$n]."\n";

                $d['gosaApplicationParameter'][] = $params[$n];
              }
            }
            $data['ADD']["cn=$name,$location_dn$release_dn"] = $d;
          }
        }
      }

      /* Updated todo list */
      $todo[] = array(
        "DETAILS" => FALSE,
        "DN"      => $info['dn'],
        "AFTER"   => $after,
        "CURRENT" => $current,
        "TODO"    => $data
      );
    }

    /* Remember checks */
    $this->menu = $todo;

    /* Check if we were able to query the ldap server */
    if (count($this->menu)) {
      $this->checks['old_style_menus']['STATUS']      = FALSE;
      $this->checks['old_style_menus']['STATUS_MSG']  = "<div style='color:#F0A500'>"._("Warning")."</div>";
      $this->checks['old_style_menus']['ERROR_MSG']   = sprintf(_("There are %s application menus which have to be migrated."),
                                                      count($this->menu))."<input type='submit' name='menu_dialog' value='"._("Migrate")."'>";
    } else {
      $this->checks['old_style_menus']['STATUS']      = TRUE;
      $this->checks['old_style_menus']['STATUS_MSG']  = _("Ok");
      $this->checks['old_style_menus']['ERROR_MSG']   = "";
    }
  }


  /*! \brief  Handle posts for the menu_dialog
              Ensure that checked checkboxes stay checked.
   */
  function check_menu_posts()
  {
    foreach (array_keys($this->menu) as $key) {
      if (isset($_POST["migrate_".$key])) {
        $this->menu[$key]['DETAILS'] = TRUE;
      } else {
        $this->menu[$key]['DETAILS'] = FALSE;
      }
    }
  }


  /*! \brief  This function updates old-style application menus to
               valid 2.6 application menus.
              All selected menus will be converted (DETAILS = TRUE).
              The ldap actions collected by check_menus() will be executed.
   */
  function migrate_menus()
  {
    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);

    /* Walk through menus and detect selected menu */
    foreach ($this->menu as $key => $menu) {
      if ($menu['DETAILS']) {

        /* Excute all LDAP-ADD actions */
        $success = TRUE;
        foreach ($menu['TODO']['ADD'] as $dn => $data) {
          $ldap->cd($cv['base']);
          if (!$ldap->dn_exists($dn)) {
            $ldap->cd($dn);
            $ldap->add($data);
            if (!$ldap->success()) {
              msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_ADD, get_class()), LDAP_ERROR);
              $success = FALSE;
            }
          }
        }

        /* Execute all LDAP-MODIFY actions */
        foreach ($menu['TODO']['MODIFY'] as $dn => $data) {
          $ldap->cd($cv['base']);
          if ($ldap->dn_exists($dn)) {
            $ldap->cd($dn);
            $ldap->modify($data);
            if (!$ldap->success()) {
              msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()), LDAP_ERROR);
              $success = FALSE;
            }
          }
        }

        /* If every action was successful, remove this entry from the list */
        if ($success) {
          unset($this->menu[$key]);
        }
      }
    }

    /* Udpate migration status for application menus */
    $this->check_menus();
  }


  function migrate_selected_admin_users()
  {
    /* Updated ui selection */
    $this->migrate_users();

    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();

    /* Get current ACL configuration for the ldap base */
    $ldap->cat($cv['base']);
    $base_attrs   = $ldap->fetch();
    $acl_entries  = array();
    $acl_id       = -1;
    if (isset($base_attrs['gosaAclEntry'])) {
      for ($i = 0; $i < $base_attrs['gosaAclEntry']['count']; $i ++) {
        $acl_entries[] = $base_attrs['gosaAclEntry'][$i];
        $cur_id = preg_replace("/^([0-9]*):.*$/", "\\1", $base_attrs['gosaAclEntry'][$i]);
        if ($cur_id > $acl_id) {
          $acl_id = $cur_id;
        }
      }
    }

    /* Append ACLs selected in the migrate admin account dialog */
    foreach ($this->migrate_users as $entry) {
      if ($entry['checked']) {
        $acl_id ++;
        $acl_entries[] = $acl_id.$entry['change'];
      }
    }

    /* Check if the required objectClasses are available */
    $ocs = array();
    for ($i = 0;$i < $base_attrs['objectClass']['count']; $i++) {
      $ocs[] = $base_attrs['objectClass'][$i];
    }
    if (!in_array("gosaACL", $ocs)) {
      $ocs[] = "gosaACL";
    }

    /* Try to write changes */
    if (count($acl_entries)) {
      $new_entry['gosaAclEntry']  = $acl_entries;
      $new_entry['objectClass']   = $ocs;
      $ldap->cd($cv['base']);
      $ldap->modify($new_entry);
      if (!$ldap->success()) {
        $this->checks['acls']['TITLE']      = _("Checking for super administrator");
        $this->checks['acls']['STATUS']     = FALSE;
        $this->checks['acls']['STATUS_MSG'] = _("Failed");
        $this->checks['acls']['ERROR_MSG']  = "<br>".msgPool::ldaperror($cv['base'], $ldap->get_error(), LDAP_MOD);
      } else {
        $this->check_administrativeAccount();
      }
    }
  }


  function migrate_users()
  {
    /* Collect a list of available FusionDirectory users and groups */

    /* Establish ldap connection */
    $cv   = $this->parent->captured_values;
    $ldap = $this->get_ldap_link();
    $ldap->cd($cv['base']);

    $users = array();
    $ldap->search("(&(objectClass=gosaAccount)(objectClass=person)".
        "(objectClass=inetOrgPerson)(objectClass=organizationalPerson))", array("uid","dn"));
    while ($user_attrs = $ldap->fetch()) {
      $users[$user_attrs['dn']] = $user_attrs['uid'][0];
      $rusers[$user_attrs['uid'][0]] = $user_attrs['dn'];
    }
    $groups = array();
    $ldap->search("objectClass=posixGroup", array("cn","dn"));
    while ($group_attrs = $ldap->fetch()) {
      $groups[$group_attrs['dn']] = $group_attrs['cn'][0];
    }

    foreach (array_keys($this->migrate_users) as $id) {
      $this->migrate_users[$id]['checked'] = isset($_POST['migrate_admin_'.$id]);
    }

    /* Try to find an old GOsa 2.5 administrative account that may be migrated */
    if (!count($this->migrate_users)) {
      //FIXME
    }
  }
}
?>