Code

removed duplicated message
[gosa.git] / setup / class_setupStep_Migrate.inc
1 <?php
3 /*
4    This code is part of GOsa (https://gosa.gonicus.de)
5    Copyright (C) 2007 Fabian Hickert
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 */
23 class Step_Migrate extends setup_step
24 {
25   var $languages      = array();
26   var $attributes     = array();
27   var $header_image   = "images/monitoring.png";
28   var $checks         = array();
30   /* Department migration attributes */
31   var $migration_dialog   = FALSE;
32   var $deps_to_migrate    = array();
34   
36   function Step_Migrate()
37   {
38     $this->update_strings(); 
39     $this->initialize_checks();
40   }
42   function update_strings()
43   {
44     $this->s_title      = _("LDAP inspection");
45     $this->s_title_long = _("LDAP inspection");
46     $this->s_info       = _("Analyze your current LDAP for GOsa compatibility");
47   }
48  
49   function initialize_checks()
50   {
51     $this->checks = array();
52     $this->checks['permissions']['TITLE']     = _("Checking permissions on ldap database");
53     $this->checks['permissions']['STATUS']    = FALSE;
54     $this->checks['permissions']['STATUS_MSG']= "";
55     $this->checks['permissions']['ERROR_MSG'] = "";
56     $this->check_ldap_permissions();
58     $this->checks['deps_visible']['TITLE']     = _("Checking for invisible deparmtments");
59     $this->checks['deps_visible']['STATUS']    = FALSE;
60     $this->checks['deps_visible']['STATUS_MSG']= "";
61     $this->checks['deps_visible']['ERROR_MSG'] = "";
62     $this->check_visible_organizationalUnits();
63   }
66   /* Check ldap accessibility 
67    * Create and remove a dummy object, 
68    *  to ensure that we have the necessary permissions
69    */
70   function check_ldap_permissions()
71   {
72     $cv = $this->parent->captured_values;
73     $ldap = new LDAP($cv['admin'],
74                      $cv['password'],
75                      $cv['connection'],
76                      FALSE,
77                      $cv['tls']);
78     $name     = "GOsa_setup_text_entry_".session_id().rand(0,999999);
79     $dn       = "ou=".$name.",".$cv['base'];
80     $testEntry= array();
81     $testEntry['objectClass'][]= "top";
82     $testEntry['objectClass'][]= "organizationalUnit";
83     $testEntry['objectClass'][]= "gosaDepartment";
84     $testEntry['description']= "Created by GOsa setup, this object can be removed.";
85     $testEntry['ou']  = $name;
86  
87     $ldap->cd ($dn);
88     $res = $ldap->add($testEntry);
89     if(!$res){
90       $this->checks['permissions']['STATUS']    = FALSE;
91       $this->checks['permissions']['STATUS_MSG']= _("Failed");
92       $this->checks['permissions']['ERROR_MSG'] = 
93         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
94       $this->checks['permissions']['ERROR_MSG'].=
95         "<input type='submit' name='retry_permissions' value='"._("Retry")."'>";
96       return(false);
97     }
99     $res = $ldap->rmDir($dn);
100     if(!$res){
101       $this->checks['permissions']['STATUS']    = FALSE;
102       $this->checks['permissions']['STATUS_MSG']= _("Failed");
103       $this->checks['permissions']['ERROR_MSG'] = 
104         sprintf(_("The specified user '%s' does not have full access to your ldap database."),$cv['admin']);
105       $this->checks['permissions']['ERROR_MSG'].=
106         "<input type='submit' name='retry_permissions' value='"._("Retry")."'>";
107       return(false);
108     }
110     $this->checks['permissions']['STATUS']    = TRUE;
111     $this->checks['permissions']['STATUS_MSG']= _("Ok");
112     $this->checks['permissions']['ERROR_MSG'] = "<input type='submit' name='retry_permissions' value='"._("Retry")."'>";
113     return(true);
114   } 
117   function check_visible_organizationalUnits()
118   {
119     $this->deps_to_migrate = array();
120     $cnt_ok = 0;
122     /* Get collected configuration settings */
123     $cv = $this->parent->captured_values;
125     /* Establish ldap connection */
126     $ldap = new LDAP($cv['admin'],
127                      $cv['password'],
128                      $cv['connection'],
129                      FALSE,
130                      $cv['tls']);
132     /* Skip GOsa internal departments */
133     $skip_dns = array("/^ou=people,/","/^ou=groups,/","/(,|)ou=configs,/","/(,|)ou=systems,/",
134                       "/^ou=apps,/","/^ou=mime,/","/^ou=aclroles,/","/^ou=incoming,/",
135                       "/ou=snapshots,/","/(,|)dc=addressbook,/","/^(,|)ou=machineaccounts,/",
136                       "/(,|)ou=winstations,/");
139     /* Get all invisible departments */
140     $ldap->cd($cv['base']); 
141     $ldap->search("(&(objectClass=organizationalUnit)(!(objectClass=gosaDepartment)))",array("ou","description","dn"));
142     while($attrs = $ldap->fetch()){
143       $attrs['checked'] = FALSE;
144       $attrs['before']  = "";
145       $attrs['after']   = "";
147       $this->deps_to_migrate[] = $attrs;
148     }
149   
150     /* Filter returned list of departments */
151     foreach($this->deps_to_migrate as $key => $attrs){
152       $dn = $attrs['dn'];
153       $skip = false;
154       foreach($skip_dns as $skip_dn){
155         if(preg_match($skip_dn,$dn)){
156           $skip = true;
157         }
158       }
159       if($skip){
160         unset($this->deps_to_migrate[$key]);
161       }
162     }
163   
164     /* No invisible */
165     if(count($this->deps_to_migrate) == 0){
166       $this->checks['deps_visible']['STATUS']    = TRUE;
167       $this->checks['deps_visible']['STATUS_MSG']= _("Ok");
168       $this->checks['deps_visible']['ERROR_MSG'] = "";
169       $this->checks['deps_visible']['ERROR_MSG'] .= "<input type='submit' name='deps_visible_migrate_refresh' value='"._("Retry")."'>";
170     }else{
171       $this->checks['deps_visible']['STATUS']    = FALSE;
172       $this->checks['deps_visible']['STATUS_MSG']= "";//sprintf(_("%s entries found"),count($this->deps_to_migrate));
173       $this->checks['deps_visible']['ERROR_MSG'] = sprintf(_("Found %s departments that will not be visible in GOsa."),count($this->deps_to_migrate));
174       $this->checks['deps_visible']['ERROR_MSG'] .= "<input type='submit' name='deps_visible_migrate' value='"._("Migrate")."'>";
175       $this->checks['deps_visible']['ERROR_MSG'] .= "<input type='submit' name='deps_visible_migrate_refresh' value='"._("Reload list")."'>";
176     }
178   }
181     
182   /* Start deparmtment migration */  
183   function migrate_organizationalUnits($only_ldif = FALSE)
184   {
185     /* Get collected configuration settings */
186     $cv = $this->parent->captured_values;
188     /* Establish ldap connection */
189     $ldap = new LDAP($cv['admin'],
190                      $cv['password'],
191                      $cv['connection'],
192                      FALSE,
193                      $cv['tls']);
195     foreach($this->deps_to_migrate as $key => $dep){
196       if($dep['checked']){
198         $ldap->cat($dep['dn'],array("objectClass","description"));
199         $attrs      = $ldap->fetch();
200         $new_attrs  = array();
202         for($i = 0 ; $i < $attrs['objectClass']['count']; $i ++ ){
203           $new_attrs['objectClass'][]   = $attrs['objectClass'][$i];
204         }
205         $new_attrs['objectClass'][] = "gosaDepartment";
206     
207         if(!isset($attrs['description'])){
208           $new_attrs['description'][] = "GOsa department";
209         }
210       
211         
212    
213         if($only_ldif){
214           $this->deps_to_migrate[$key]['before'] = $this->array_to_ldif($attrs);
215           $this->deps_to_migrate[$key]['after']  = $this->array_to_ldif($new_attrs);
216         }else{
217           $ldap->cd($attrs['dn']);
218           if(!$ldap->modify($new_attrs)){
219             print_red(sprintf(_("Failed to migrate the department '%s' into GOsa, error message is as follows '%s'."),$attrs['dn'],$ldap->get_error()));
220             return(false);
221           }
222         }
223       }
224     }
225     return(TRUE);
226   }
230   function execute()
231   {
232     /* Permission check */
233     $this->check_ldap_permissions();
235     /* Migration options 
236      */
237     /* Refresh list of deparments */
238     if(isset($_POST['deps_visible_migrate_refresh'])){
239       $this->check_visible_organizationalUnits();
240     }
242     /* Open migration dialog */
243     if(isset($_POST['deps_visible_migrate'])){
244       $this->migration_dialog = TRUE;
245       $this->dialog =TRUE;
246     }
248     /* Close migration dialog */
249     if(isset($_POST['deps_visible_migrate_close'])){
250       $this->migration_dialog = FALSE;
251       $this->dialog =FALSE;
252     }
254     /* Start migration */
255     if(isset($_POST['deps_visible_migrate_migrate'])){
256       if($this->migrate_organizationalUnits()){
257         $this->check_visible_organizationalUnits();
258       }
259     }
261     /* Start migration */
262     if(isset($_POST['deps_visible_migrate_whatsdone'])){
263       $this->migrate_organizationalUnits(TRUE);
264     }
266     /* Display migration dialog */
267     if($this->migration_dialog){
268       $smarty = get_smarty();
269       $smarty->assign("deps_to_migrate",$this->deps_to_migrate);
270       $smarty->assign("method","migrate");
271       return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
272     }
274     $smarty = get_smarty();
275     $smarty->assign("checks",$this->checks);
276     $smarty->assign("method","default");
277     return($smarty->fetch(get_template_path("setup_migrate.tpl",TRUE,dirname(__FILE__))));
278   }
280   function save_object()
281   {
282     if($this->migration_dialog){
283       foreach($this->deps_to_migrate as $id => $data){
285         if(isset($_POST['migrate_'.$id])){
286           $this->deps_to_migrate[$id]['checked'] = TRUE;
287         }else{
288           $this->deps_to_migrate[$id]['checked'] = FALSE;
289         }
290       }
291     }
293   }
295   function array_to_ldif($atts)
296   {
297     $ret = "";
298     unset($atts['count']);
299     unset($atts['dn']);
300     foreach($atts as $name => $value){
302       if(is_numeric($name)) {
303         continue;
304       }
306       if(is_array($value)){
307         unset($value['count']);
308         foreach($value as $a_val){
309           if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $a_val)){
310             $ret .= $name.":: ". base64_encode($a_val)."\n";
311           }else{
312             $ret .= $name.": ". $a_val."\n";
313           }
314         }
315       }else{
316         if(!preg_match('/^[a-z0-9+@#.=, \/ -]+$/i', $value)){
317           $ret .= $name.": ". base64_encode($value)."\n";
318         }else{
319           $ret .= $name.": ". $value."\n";
320         }
321       }
322     }
323     return(preg_replace("/\n$/","",$ret));
324   }
328 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
329 ?>