Code

9444ed8721373b3297be1efb2cb834440f96c899
[gosa.git] / plugins / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 require "tabsPartition.inc";
21 require "tabsVariable.inc";
22 require "tabsHook.inc";
23 require "tabsTemplate.inc";
24 require "tabsScript.inc";
25 require "tabsProfile.inc";
26 require "tabsPackage.inc";
28 class faiManagement extends plugin
29 {
30         /* Definitions */
31         var $plHeadline                 = "FAI";
32         var $plDescription              = "Fully Automatic Installation - management";
34         /* CLI vars */
35         var $cli_summary                = "Handling of FAI entries";
36         var $cli_description    = "This plugin represents a management tool\n
37                 which allows us to manage all needed attributes for fully automatic installations (FAI)";
38         var $cli_parameters             = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
40         /* Headpage attributes */
41         var $departments= array();
42         var $deptabs= NULL;
44         /* attribute list for save action */
45         var $attributes                 = array();      //      Attributes Managed by this plugin, none, 
46         //      because this is only an overview over all objects already defined in FAI.
47         var $objectclasses              = array();      //      ObjectClasses which the attributes are related to
48         var $dialog                             = array();      //      This object contains every dialog we have currently opened
50         var $objects                    = array();      //      This array contains all available objects shown in divlist
51         var $is_dialog                  = false;
53         /* construction/reconstruction 
54          * The Filter ($faifilter stored in $_SESSION['faifilter']) defines the last 
55          *  selected department and filter options
56          * If it is not defined already we create a new fresh faifilter.
57          */
58         function faiManagement ($config, $ui)
59         {
60                 $this->ui                       = $ui;  
62                 /* Set current dn to "", */
63                 $this->dn                       = "";
65                 /* Assign config */
66                 $this->config           = $config;
68                 /* Get global filter config */
69                 if (!is_global("faifilter")){
70                         $base   = get_base_from_people($ui->dn);
71                         $faifilter= array("base" => "ou=fai,ou=configs,ou=systems,".$base,
72                                         "regex" => "*");
73                         $faifilter['ShowProfiles']      = true;
74                         $faifilter['ShowTemplates'] = true;
75                         $faifilter['ShowScripts']       = true;
76                         $faifilter['ShowHooks']         = true;
77                         $faifilter['ShowVariables']     = true;
78                         $faifilter['ShowPackages']      = true;
79                         $faifilter['ShowPartitions']=true;
80                         register_global("faifilter", $faifilter);
81                 }
82         }
84         function execute()
85         {
86                 $display        = "";
87                 $smarty         = get_smarty();
88                 $s_action       = "";
89                 $s_entry        = "";
90                 /* filter management 
91                  * Filter will be changed if POST['regex'] or $_GET['search'] isset
92                  * New Filter will be stored in session and is used to generate list contents
93                  */     
94                 $faifilter = $_SESSION['faifilter'];
96                 /* Filter is posted by apply button 
97                  * Check every single chkbox 
98                  */
99                 if(isset($_POST['regex'])){
100                         $faifilter['regex'] = $_POST['regex']."*";
101                         foreach(array("ShowPartitions","ShowProfiles","ShowTemplates","ShowScripts","ShowHooks","ShowVariables","ShowPackages") as $attrs){
102                                 if(isset($_POST[$attrs])){
103                                         $faifilter[$attrs] = true;
104                                 }else{
105                                         $faifilter[$attrs] = false;
106                                 }
107                         }
108                 }
110                 /* Check ImageButton posts
111                  * Create new tab ich new_xx is posted
112                  */
113                 foreach($_POST as $key => $val){
114                         if(preg_match("/create_partition/i",$key)){
115                                 $s_action = "new_partition";
116                         }elseif(preg_match("/create_script/i",$key)){
117                                 $s_action = "new_script";
118                         }elseif(preg_match("/create_hook/i",$key)){
119                                 $s_action = "new_hook";
120                         }elseif(preg_match("/create_variable/i",$key)){
121                                 $s_action = "new_variable";
122                         }elseif(preg_match("/create_template/i",$key)){
123                                 $s_action = "new_template";
124                         }elseif(preg_match("/create_package/i",$key)){
125                                 $s_action = "new_package";
126                         }elseif(preg_match("/create_profile/i",$key)){
127                                 $s_action = "new_profile";
128                         }elseif(preg_match("/edit_continue/",$key)){
129                                 $s_action = "select_class_name_finished";
130                         }elseif(preg_match("/^entry_edit_.*/",$key)){
131                                 $s_entry = preg_replace("/^entry_edit_/","",$key);
132                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
133                                 $s_action = "edit";
134                         }elseif(preg_match("/^entry_delete_.*/",$key)){
135                                 $s_entry = preg_replace("/^entry_delete_/","",$key);
136                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
137                                 $s_action = "delete";
138                         }
140                 }
142                 if(isset($_GET['edit_entry'])){
143                         $s_entry = $_GET['edit_entry'];
144                         $s_action = "edit";
145                 }
147                 /* Confirm dialog 
148                  * Delte object 
149          */
150                 if ($s_action=="delete"){
152                         /* Get 'dn' from posted termlinst */
153                         $this->dn= $this->objects[$s_entry]['dn'];
155                         /* Load permissions for selected 'dn' and check if
156                            we're allowed to remove this 'dn' */
157                         $acl=           get_permissions ($this->dn, $this->ui->subtreeACL);
158                         $this->acl= get_module_permission($acl, "FAIclass", $this->dn);
159                         if (chkacl($this->acl, "delete") == ""){
161                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
162                                 if (($user= get_lock($this->dn)) != ""){
163                                         return(gen_locked_message ($user, $this->dn));
164                                 }
166                                 /* Lock the current entry, so nobody will edit it during deletion */
167                                 add_lock ($this->dn, $this->ui->dn);
168                                 $smarty->assign("warning", sprintf(_("You're about to delete all information about the FAI class at '%s'."), $this->dn));
169                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
170                         } else {
172                                 /* Obviously the user isn't allowed to delete. Show message and
173                                    clean session. */
174                                 print_red (_("You are not allowed to delete this component!"));
175                         }
176                 }
178                 /* Delete canceled? */
179                 if (isset($_POST['delete_cancel'])){
180                         del_lock ($this->dn);
181                 }
184                 /* Deltetion was confirmed, so delete this entry
185                  */
186                 if (isset($_POST['delete_terminal_confirm'])){
188                         /* Some nice guy may send this as POST, so we've to check
189                            for the permissions again. */
190                         if (chkacl($this->acl, "delete") == ""){
192                                 /* Find out more about the object type */
193                                 $ldap   = $this->config->get_ldap_link();
194                                 $ldap->cat($this->dn);
195                                 $attrs  = $ldap->fetch();
197                                 $type   = $this->get_type($attrs);                      
199                                 $this->dialog= new $type[0]($this->config,      $this->config->data['TABS'][$type[2]], $this->dn);
200                                 $this->dialog->set_acl(array($this->acl));
202                                 $this->dialog->by_object[$type[1]]->remove_from_parent ();
203                                 unset ($this->dialog);
204                                 gosa_log ("FAI class '".$this->dn."' has been removed");
205                                 $this->dialog= NULL;
207                                 /* Terminal list has changed, reload it. */
208                                 $this->reload ();
209                         } else {
211                                 /* Normally this shouldn't be reached, send some extra
212                                    logs to notify the administrator */
213                                 print_red (_("You are not allowed to delete this component!"));
214                                 gosa_log ("Warning: '".$this->ui->uid."' tried to trick system ".
215                                                 "deletion.");
216                         }
218                         /* Remove lock file after successfull deletion */
219                         del_lock ($this->dn);
220                 }
222                 /* Edit Entry */
223                 if($s_action == "edit"){
224                         $entry  = $this->objects[$s_entry];
226                         $a_setup = ($this->get_type($entry));
227                 
228                         $this->dn = $entry['dn'];
230                         $this->dialog= new $a_setup[0]($this->config,
231                                         $this->config->data['TABS'][$a_setup[2]],$this->dn);
232                         $this->is_dialog = true;
233                 }
236                 /* Dialog handling */
237                 if($s_action == "new_partition"){
238                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIpartitionTable");
239                 }
241                 /* Dialog handling */
242                 if($s_action == "new_script"){
243                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIscript");
244                 }
246                 /* Dialog handling */
247                 if($s_action == "new_hook"){
248                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIhook");
249                 }
251                 /* Dialog handling */
252                 if($s_action == "new_variable"){
253                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIvariable");
254                 }
256                 /* Dialog handling */
257                 if($s_action == "new_template"){
258                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAItemplate");
259                 }
261                 /* Dialog handling */
262                 if($s_action == "new_package"){
263                         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,"FAIpackageList");
264                 }
266                 /* New Profile */
267                 if($s_action == "new_profile"){
268                         $this->dn = "new" ;
269                         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
271                         $this->dialog = new $a_setup[0]($this->config,
272                                         $this->config->data['TABS'][$a_setup[2]],$this->dn);
273                         $this->is_dialog = true;
275                 }
277                 if($s_action == "select_class_name_finished"){
278                         $this->dialog->save_object();
279                         if(count($this->dialog->check())!=0){
280                                 foreach($this->dialog->check() as $msg){
281                                         print_red($msg);
282                                 }               
283                         }else{
284                                 $this->dn = "new" ;
285                                 $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
286                                 $name = $this->dialog->save();
288                                 $this->dialog = new $a_setup[0]($this->config,
289                                                 $this->config->data['TABS'][$a_setup[2]],$this->dn);
290                                 $this->dialog->by_object[$a_setup[1]]->cn = $name;
291                                 $this->is_dialog = true;
292                         }               
293                 }       
296                 /* Search is set */
297                 if(isset($_GET['search'])){
298                         $faifilter['regex']=$_GET['search']."*";
299                 }
301                 /* remove double asterisks and save new filter */
302                 $faifilter['regex'] = preg_replace("/\*\*/","*",$faifilter['regex']);
303                 $_SESSION['faifilter']= $faifilter;
305                 /* Edit dialog was canceled 
306                  * Remove dialog an show management dialog
307                  */
308                 if(isset($_POST['edit_cancel'])){
309                         unset($this->dialog);
310                         $this->dialog=NULL;
311                         $this->is_dialog = false;
312                 }
315                 /* This check if the given tab could be saved 
316                  * If it was possible to save it, remove dialog object. 
317                  * If it wasn't possible, show errors and keep dialog.
318                  */
319                 if(isset($_POST['edit_finish'])){
320                         $this->dialog->save_object();
321                         if(count($this->dialog->check())!=0){
322                                 foreach($this->dialog->check() as $msg){
323                                         print_red($msg);
324                                 }
325                         }else{
326                                 $this->dialog->save();
327                                 unset($this->dialog);
328                                 $this->dialog=NULL;
329                                 $this->is_dialog=false;
330                         }
331                 }
334                 /* If dialog is set, but $this->is_dialog==false, then 
335                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
336                  * If is_dialog == true, we are currently editing tab objects.
337                  *  Here we need both, save and cancel
338                  */ 
340                 if($this->dialog != NULL){
341                         $display .= $this->dialog->execute();
342                         /* Don't show buttons if tab dialog requests this */
343                         if (((isset($this->dialog->current))&&($this->dialog->by_object[$this->dialog->current]->is_dialog))){
344                                 
345                         }elseif(((isset($this->dialog->current))&&(!$this->dialog->by_object[$this->dialog->current]->is_dialog))){
346                                 $display.= "<p style=\"text-align:right\">\n";
347                                 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
348                                 $display.= "&nbsp;\n";
349                                 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
350                                 $display.= "</p>";
351                         }elseif(!isset($this->dialog->current)){
352                                 $display.= "<p style=\"text-align:right\">\n";
353                                 $display.= "<input type=submit name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
354                                 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
355                                 $display.= "</p>";
356                         }
357                         return($display);
358                 }
360                 /* Show Management template
361                  * The following code will only be reached if no dialog (tabs or dialogs)
362                  *  are currently opened.
363                  * Assign all reguired vars to template engine
364                  */
366                 /* Create listhead, it will be shown on top of the divlist. 
367                  * It provides general navigation and object creation
368                  */
369                 $faihead = 
370                         "<div style='background:#F0F0F9;padding:5px;'>&nbsp;".
372                         " <input type='image' src='images/fai_new_profile.png' align='middle' 
373                         title='"._("New profile")."' name='Create_profile' alt='"._("P")."'>&nbsp;".
374                         
375                         "<img src=\"images/list_seperator.png\" alt=\"\" align=\"middle\" height=\"16\" width=\"1\">&nbsp;".
376                                 
377                         " <input type='image' src='images/fai_new_partitionTable.png' align='middle' 
378                         title='"._("New partition table")."' name='Create_partition' alt='"._("PT")."'>&nbsp;".
380                         " <input type='image' src='images/fai_new_script.png' align='middle' 
381                         title='"._("New scripts")."' name='Create_script' alt='"._("S")."'>&nbsp;".
383                         " <input type='image' src='images/fai_new_hook.png' align='middle' 
384                         title='"._("New hooks")."' name='Create_hook' alt='"._("H")."'>&nbsp;".
386                         " <input type='image' src='images/fai_new_variable.png' align='middle' 
387                         title='"._("New variables")."' name='Create_variable' alt='"._("V")."'>&nbsp;".
388                         
389                         " <input type='image' src='images/fai_new_template.png' align='middle' 
390                         title='"._("New templates")."' name='Create_template' alt='"._("T")."'>&nbsp;".
392                         " <input type='image' src='images/fai_new_packages.png' align='middle' 
393                         title='"._("New package list")."' name='Create_package' alt='"._("PK")."'>&nbsp;".
394                         
395                         "</div>";
397                 $this->reload();
399                 /* Create list with objects */
400                 $divlist = new divlist("faiManagement"); 
401                 $divlist->SetEntriesPerPage(20);
402                 $divlist->SetSummary(_("This table displays all systems, in the selected tree."));
404                 $divlist->SetHeader(array(
405                                         array("string" => "&nbsp;"),
406                                         array("string" => _("Name of FAI class")),
407                                         array("string" => _("Class type")),
408                                         array("string" => _("Actions"), "attach"=>"style='border:none;width:40px;'")));
411                 $action  = "<input type='image' src='images/edit.png'   alt='"._("edit")."'   name='entry_edit_%KEY%' title='"._("Edit class")."'>";
412                 $action .= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='entry_delete_%KEY%' title='"._("Delete class")."'>";
414                 $editlink ="<a href='?plug=".$_GET['plug']."&edit_entry=%KEY%'>%NAME%</a>";             
416                 /* Attach objects */
417                 foreach($this->objects as $key => $value){
418                                 $info= "";
419                                 $img = "";
420                                 $type = $value['type'];
421                                 $abort=false;
423                                 switch($type) {
424                                         case "FAIpartitionTable"        : 
425                                                 if(!$faifilter['ShowPartitions']){ 
426                                                         $abort = true;
427                                                 }
428                                         $img="<img src='images/fai_partitionTable.png' title='"._("Partition table")."' alt='PT'>";
429                                         $info = _("Partition table");
430                                         break;
431                                         case "FAIpackageList"           : 
432                                                 if(!$faifilter['ShowPackages']){ 
433                                                         $abort = true;
434                                                 }
435                                         $img="<img src='images/fai_packages.png' title='"._("Package list")."' alt='PL'>";
436                                         $info = _("Package list");
437                                         break;
438                                         case "FAIscript"                        : 
439                                                 if(!$faifilter['ShowScripts']){ 
440                                                         $abort = true;
441                                                 }
442                                         $img="<img src='images/fai_script' title='"._("Scripts")."' alt='S'>";
443                                         $info = _("Scripts");
444                                         break;
445                                         case "FAIvariable"                      : 
446                                                 if(!$faifilter['ShowVariables']){ 
447                                                         $abort = true;
448                                                 }
449                                         $img="<img src='images/fai_variable.png' title='"._("Variables")."' alt='V'>";
450                                         $info = _("Variables");
451                                         break;
452                                         case "FAIhook"                          :
453                                                 if(!$faifilter['ShowHooks']){ 
454                                                         $abort = true;
455                                                 }
456                                         $img="<img src='images/fai_hook.png' title='"._("Hooks")."' alt='H'>";
457                                         $info = _("Hooks");
458                                         break;
459                                         case "FAIprofile"                       : 
460                                                 if(!$faifilter['ShowProfiles']){ 
461                                                         $abort = true;
462                                                 }
463                                         $img="<img src='images/fai_profile.png' title='"._("Profile")."' alt='P'>";
464                                         $info = _("Profile");
465                                         break;
466                                         case "FAItemplate"                      : 
467                                                 if(!$faifilter['ShowTemplates']){ 
468                                                         $abort = true;
469                                                 }
470                                         $img="<img src='images/fai_template.png' title='"._("Templates")."' alt='T'>";
471                                         $info = _("Templates");
472                                         break;
473                                         default                                         : 
474                                         $img="<img src='images/empty.png' >";$info = "";break;
476                                 }
477                                 
478                                 if(!$abort)     {
479                                         if((isset($value['description']))&&(!empty($value['description']))){
480                                                 $desc= " [".$value['description']."]";
481                                         }else{
482                                                 $desc= "";
483                                         }
485                                         $field1 = array("string" => $img , "attach" => "style='height:26px;width=20px;'");
486                                         $field2 = array("string" => preg_replace(array("/%KEY%/","/%NAME%/"),array($key,$value['cn'].$desc),$editlink) , "attach" => "");
487                                         $field3 = array("string" => $info, "attach" => "");
488                                         $field4 = array("string" => preg_replace("/%KEY%/",$key,$action) , "attach"=>"style='text-align:right;border:none'");
489                                         $divlist->AddEntry(array($field1,$field2,$field3,$field4));
491                         }
492                 }
494                 foreach($faifilter as $attrs => $value){
495                         if($value){
496                                 $smarty->assign($attrs."CHK", " checked ");
497                         }else{
498                                 $smarty->assign($attrs."CHK", " ");
499                         }
500                 }
502                 /* Assign all reguired vars to template engine */
503                 $smarty->assign("faihead"               , $faihead);    
504                 $smarty->assign("failist"               , $divlist->DrawList());
505                 $smarty->assign("regex"                 , $faifilter['regex']); 
506                 $smarty->assign("infoimage"             , get_template_path('images/info.png'));
507                 $smarty->assign("launchimage"   , get_template_path('images/launch.png'));
508                 $smarty->assign("alphabet"              , generate_alphabet());
509                 $smarty->assign("apply"                 , apply_filter());
510                 $smarty->assign("search_image"  , get_template_path('images/search.png'));
512                 $display        = $smarty->fetch(get_template_path('headpage.tpl', TRUE, dirname(__FILE__)));
513                 return ($display);
514         }
517         function reload()
518         {
519                 /* Create a new list of FAI object 
520                  * Object list depends on faifilter['regex']
521                  * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
522                  */
523                 $faifilter= get_global('faifilter');
524                 // Added for dirlist function...
526                 /* Set base for all searches */
527                 $base= $faifilter['base'];
528                 $regex = $faifilter['regex'];   
530                 /* Array to save objects */
531                 $objects = array();
532         
533                 $this->objects=array();
535                 $res= get_list($this->ui->subtreeACL, "(objectClass=organizationalUnit)", 
536                                 FALSE, $base, array("cn","description","objectClass"),TRUE);
538                 foreach($res as $objecttypes){
539                         $res2 = get_list($this->ui->subtreeACL, "(&(objectClass=*)(cn=".$regex."))",
540                                         FALSE, $objecttypes['dn'], array("cn","description","objectClass","FAIclass"),TRUE);
541                         foreach($res2 as $object){
542                                 $type= "";      
543                                 unset($object['objectClass']['count']);
544                                 if(!isset($object['description'][0])){
545                                         $object['description'][0]="";
546                                 }
547                                 
548                                 /* Clean up object informations */
549                                 $obj['cn']                      = $object['cn'][0];
550                                 $obj['dn']                      = $object['dn'];
551                                 $obj['description'] = $object['description'][0];
552                                 $obj['objectClass'] = $object['objectClass'];
554                                 if(in_array("FAIpartitionTable",$obj['objectClass'])){
555                                         $type = "FAIpartitionTable";
556                                 }elseif(in_array("FAIpackageList",$obj['objectClass'])){
557                                         $type = "FAIpackageList";
558                                 }elseif(in_array("FAIscript",$obj['objectClass'])){
559                                         $type = "FAIscript";
560                                 }elseif(in_array("FAIvariable",$obj['objectClass'])){
561                                         $type = "FAIvariable";
562                                 }elseif(in_array("FAIhook",$obj['objectClass'])){
563                                         $type = "FAIhook";
564                                 }elseif(in_array("FAIprofile",$obj['objectClass'])){
565                                         $obj['FAIclass']        = $object['FAIclass'][0];
566                                         $type = "FAIprofile";
567                                 }elseif(in_array("FAItemplate",$obj['objectClass'])){
568                                         $type = "FAItemplate";
569                                 }
570                                 $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
571                                 $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
572  
573                         }
574                 }
575                 ksort($this->objects);
576                 reset ($this->objects);
577         }
579         function remove_lock()
580         {
581                 if (isset($this->dn)){
582                         del_lock ($this->dn);
583                 }
584         }
586         function get_type($array){
587                 if(in_array("FAIpartitionTable",$array['objectClass'])){
588                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
589                 }
590                 if(in_array("FAIscript",$array['objectClass'])){
591                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
592                 }
593                 if(in_array("FAItemplate",$array['objectClass'])){
594                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
595                 }
596                 if(in_array("FAIhook",$array['objectClass'])){
597                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
598                 }
599                 if(in_array("FAIvariable",$array['objectClass'])){
600                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
601                 }
602                 if(in_array("FAIprofile",$array['objectClass'])){
603                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
604                 }
605                 
606                 if(in_array("FAIpackageList",$array['objectClass'])){
607                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
608                 }
609         }
613 ?>