1 <?php
5 class migrateRDN implements propertyMigration
6 {
7 protected $property = NULL;
8 protected $config = NULL;
9 protected $found = array();
10 protected $filter ="";
12 function __construct($config,$property)
13 {
14 $this->property = &$property;
15 $this->config = &$config;
16 }
18 function checkForIssues()
19 {
20 $this->found = array();
21 $ldap= $this->config->get_ldap_link();
22 $ldap->cd($this->config->current['BASE']);
23 $ldap2= $this->config->get_ldap_link();
24 $ldap2->cd($this->config->current['BASE']);
26 // If the userRDN wasn't empty, then only search for users inside of the old userRDN.
27 $initialValue = $this->property->getValue();
28 $targetValue = $this->property->getValue(TRUE);
29 $dnMatch = "";
30 if(!empty($initialValue)){
31 foreach(preg_split("/,/", $initialValue) as $rdnPart){
32 if(empty($rdnPart)) continue;
33 list($namingAttrs, $container) = preg_split("/=/",$rdnPart,2);
34 $container = trim($container,', ');
35 $dnMatch.= "({$namingAttrs}:dn:={$container})";
36 }
37 }
39 // Search for users
40 $filter = sprintf($this->filter,$dnMatch);
41 $ldap->search($filter,array('dn'));
42 $found = FALSE;
43 while($attrs = $ldap->fetch()){
44 $dn = $attrs['dn'];
45 $dnTo = $dn;
47 // If there intially was no userDN given then just add the new userRDN to the user dns
48 // and create the new container objects.
49 if(empty($initialValue)){
50 list($namingAttrs, $container) = preg_split("/=/",$targetValue,2);
51 list($name, $container) = preg_split("/,/",$dn,2);
53 // Ensure that we handle a valid gosaDepartment container.
54 while(!isset($this->config->idepartments[$container])){
55 $container = preg_replace("/^[^,]*+,/","",$container);
56 }
58 // Queue new containuer to be created.
59 if(!preg_match("/^".preg_quote($targetValue,'/i')."/", $container)){
60 $dnTo = $name.",".$targetValue.$container;
61 if(!$ldap->dn_exists($targetValue.$container)){
62 $this->found['add'][$targetValue.$container] = array();
63 }
64 if($dn != $dnTo){
65 $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
66 $found = TRUE;
67 }
68 }
69 }
71 // If there intially was a userDN given then replace it with the new one.
72 if(!empty($initialValue)){
74 list($name, $container) = preg_split("/,/",$dn,2);
75 if(preg_match("/^".preg_quote($initialValue,'/i')."/", $container)){
76 $container = preg_replace("/^".preg_quote($initialValue,'/')."/",$targetValue,$container);
78 // Ensure that we handle a valid gosaDepartment container.
79 while(!isset($this->config->idepartments[$container])){
80 $container = preg_replace("/^[^,]*+,/","",$container);
81 }
83 $dnTo = $name.",".$targetValue.$container;
84 if(!empty($targetValue) && !$ldap->dn_exists($targetValue.$container)){
85 $this->found['add'][$targetValue.$container] = array();
86 }
87 if($dn != $dnTo){
88 $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
89 $found = TRUE;
90 }
91 }
92 }
93 }
94 return($found);
95 }
97 function execute()
98 {
99 $str = "";
100 if(count($this->found['add'])) {
101 $str.= "<br><h3>"._("Objects that will be added")."</h3>";
102 foreach($this->found['add'] as $dn => $attrs){
103 $str.= $dn."<br>";
104 }
105 }
106 if(count($this->found['move'])) {
107 $str.= "<br><h3>"._("Objects that will be moved")."</h3>";
108 $str.="<pre>";
109 foreach($this->found['move'] as $id => $data){
110 $checked = (!isset($_POST['migrateNow'])) ? 'checked':'';
111 $str.= "<input $checked type='checkbox' value='1' name='migrateEntry_{$id}'>";
112 $str.= sprintf(_("Moving object '%s' to '%s'"), $data['from'], $data['to'])."<br>";
113 }
114 $str.="</pre>";
115 }
116 $str.= "<button name='migrateNow'>"._("Migrate")."</button>";
117 return($str);
118 }
120 function save_object()
121 {
122 if(isset($_POST['migrateNow'])){
123 $ldap = $this->config->get_ldap_link();
124 $ldap->cd($this->config->current['BASE']);
126 // Try to add the new container objects
127 foreach($this->found['add'] as $dn => $data){
128 $ldap->cd($this->config->current['BASE']);
129 $ldap->create_missing_trees(ldap::convert($dn));
130 }
132 // Now move the objects to the new traget
133 $tmp = new plugin($this->config,NULL);
134 foreach($this->found['move'] as $id => $data){
135 if(isset($_POST["migrateEntry_{$id}"])){
136 $tmp->move($data['from'], $data['to']);
137 }
138 }
139 $this->checkForIssues();
140 }
141 }
143 function check()
144 {
145 return(array());
146 }
147 }
150 ?>