Code

deefb0be5d9b64e7f429e01574b7ed44c3fd6453
[gosa.git] / gosa-core / plugins / addons / configViewer / migration / class_migrateRDN.inc
1 <?php
5 class migrateRDN implements propertyMigration
6 {
7     protected $property = NULL;
8     protected $config = NULL;
9     protected $found = array();
10     protected $filter ="";
12     // Additional suffixes or prefixes 
13     // e.g. for 'faiScriptRDN' (ou=scripts,) moving to new destination lets say 
14     //  to 'ou=FAIscripts,' would break stuff with having 'ou=fai,ou=systems,ou=config' 
15     // prepended.
16     // 
17     protected $suffix = ""; 
18     protected $prefix = ""; 
20     function __construct($config,$property)
21     {
22         $this->property = &$property;
23         $this->config = &$config;
24     }   
26     function getChanges()
27     {
28         return($this->found);
29     }
31     function checkForIssues()
32     {
33         $this->found = array();
34         $ldap= $this->config->get_ldap_link();
35         $ldap->cd($this->config->current['BASE']);
36         $ldap2= $this->config->get_ldap_link();
37         $ldap2->cd($this->config->current['BASE']);
39         // If the userRDN wasn't empty, then only search for users inside of the old userRDN.
40         $initialValue = $this->prefix.$this->property->getValue().$this->suffix;
41         $targetValue = $this->prefix.$this->property->getValue(TRUE).$this->suffix;
43         $dnMatch = "";
44         if(!empty($initialValue)){
45             foreach(preg_split("/,/", $initialValue) as $rdnPart){
46                 if(empty($rdnPart)) continue;
47                 list($namingAttrs, $container) = preg_split("/=/",$rdnPart,2);
48                 $container = trim($container,', ');
49                 $dnMatch.= "({$namingAttrs}:dn:={$container})";
50             }
51         }
53         // Search for users
54         $filter = sprintf($this->filter,$dnMatch);
55         $ldap->search($filter,array('dn'));
56         $found = FALSE;
57         while($attrs = $ldap->fetch()){
58             $dn = $attrs['dn'];
59             $dnTo = $dn;
61             // If there intially was no userDN given then just add the new userRDN to the user dns
62             //  and create the new container objects.
63             if(empty($initialValue)){
64                 list($namingAttrs, $container) = preg_split("/=/",$targetValue,2);
65                 list($name, $container) = preg_split("/,/",$dn,2);
67                 // Ensure that we handle a valid gosaDepartment container.
68                 while(!isset($this->config->idepartments[$container])){
69                     $container = preg_replace("/^[^,]*+,/","",$container);
70                 }
72                 // Queue new containuer to be created.
73                 if(!preg_match("/^".preg_quote($targetValue,'/i')."/", $container)){
74                     $dnTo = $name.",".$targetValue.$container;
75                     if(!$ldap->dn_exists($targetValue.$container)){
76                         $this->found['add'][$targetValue.$container] = array(); 
77                     }
78                     if($dn != $dnTo){
79                         $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
80                         $found = TRUE;
81                     }
82                 }
83             }
85             // If there intially was a userDN given then replace it with the new one.
86             if(!empty($initialValue)){
88                 list($name, $container) = preg_split("/,/",$dn,2);
89                 if(preg_match("/^".preg_quote($initialValue,'/i')."/", $container)){
90                     $container = preg_replace("/^".preg_quote($initialValue,'/')."/",$targetValue,$container);
92                     // Ensure that we handle a valid gosaDepartment container.
93                     while(!isset($this->config->idepartments[$container])){
94                         $container = preg_replace("/^[^,]*+,/","",$container);
95                     }
98                     $dnTo = $name.",".$targetValue.$container;
99                     if(!empty($targetValue) && !$ldap->dn_exists($targetValue.$container)){
100                         $this->found['add'][$targetValue.$container] = array(); 
101                     }
102                     if($dn != $dnTo){
103                         $this->found['move'][] = array('from' => $dn, 'to' => $dnTo);
104                         $found = TRUE;
105                     }
106                 }
107             }    
108         }
109         return($found);
110     }
112     function execute()
113     {
114         $str = "";
115         if(count($this->found['add'])) {
116             $str.= "<br><h3>"._("Objects that will be added")."</h3>";
117             foreach($this->found['add'] as $dn => $attrs){
118                 $str.= $dn."<br>";
119             }
120         }
121         if(count($this->found['move'])) {
122             $str.= "<br><h3>"._("Objects that will be moved")."</h3>";
123             $str.="<pre>";
124             foreach($this->found['move'] as $id => $data){
125                 $checked = (!isset($_POST["migrateNow".get_class($this)])) ? 'checked':'';
126                 $str.= "<input $checked type='checkbox' value='1' name='migrateEntry_{$id}'>";
127                 $str.= sprintf(_("Moving object '%s' to '%s'"), $data['from'], $data['to'])."<br>";
128             }
129             $str.="</pre>";
130         }
131         $str.= "<button name='migrateNow".get_class($this)."'>"._("Migrate")."</button>";
132         return($str);
133     }
134    
135     function save_object()
136     {
137         if(isset($_POST["migrateNow".get_class($this)])){
138             $ldap = $this->config->get_ldap_link();
139             $ldap->cd($this->config->current['BASE']);
141             // Try to add the new container objects
142             foreach($this->found['add'] as $dn => $data){
143                 $ldap->cd($this->config->current['BASE']);
144                 $ldap->create_missing_trees(ldap::convert($dn));
145             }
147             // Now move the objects to the new traget
148             $tmp = new plugin($this->config,NULL);
149             foreach($this->found['move'] as $id => $data){
150                 if(isset($_POST["migrateEntry_{$id}"])){
151                     $tmp->move($data['from'], $data['to']);
152                 }
153             }
154             $this->checkForIssues();
155         }
156     } 
158     function check()
159     {
160         return(array());
161     }
162
165 ?>