Code

Fixed uid/cn bug
[gosa.git] / plugins / admin / systems / class_workstationStartup.inc
index 0fa75c4b40585c28e3736cd37376fb8d0f56bbb5..3d4d1300dbe8a2c6d3fff8f8c13efbf8f4539f2d 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 class workstartup extends plugin
 {
   /* CLI vars */
@@ -8,10 +9,10 @@ class workstartup extends plugin
 
   /* Generic terminal attributes */
   var $bootmode             = "G";
-  var $goLdapServerList     = array("default");
-  var $gotoBootKernel       = "";
+  var $goLdapServerList     = array();
+  var $gotoBootKernel       = "default-inherited";
   var $gotoKernelParameters = "";
-  var $gotoLdapServer       = "";
+  var $gotoLdapServer       = "default-inherited";
   var $gotoModules          = array();
   var $gotoAutoFs           = array();
   var $gotoFilesystem       = array();
@@ -41,28 +42,45 @@ class workstartup extends plugin
   var $FAIrelease         = "";
   var $FAIdebianMirror    = "auto";
 
+  var $unresolved_classes = array();
+  
+  /* FAI class selection */
+  var $InheritedFAIclass           = array();
+  var $InheritedFAIrelease         = "";
+  var $InheritedFAIdebianMirror    = "auto";
+
   /* Contains all possible server/release/class settings */
   var $FAIServRepConfig   = array();
 
-  function workstartup ($config, $dn= NULL)
+  function workstartup ($config, $dn= NULL, $parent= NULL)
   {
-    plugin::plugin ($config, $dn);
+    plugin::plugin ($config, $dn, $parent);
 
     /* Creating a list of valid Mirrors 
      * none will not be saved to ldap.
      */
     $ldap   = $this->config->get_ldap_link();
     $ldap->cd($this->config->current['BASE']);
-    
+
+    foreach($this->config->data['SERVERS']['LDAP'] as $server) {
+      $this->goLdapServerList[$server]= $server; // $this->config->data['SERVERS']['LDAP'];
+    }
+   
     $_SESSION['getAvailableClassesForThisRelease_CACHE'] = array();
     $_SESSION['getAvailableClassesForThisRelease_CACHED_CLASSES'] = array();
 
-    /* Search all FAI objects */
-    $ldap->search("(|(objectClass=FAIpackageList)(objectClass=FAItemplate)(objectClass=FAIvariable)(objectClass=FAIscript)(objectClass=FAIhook)(objectClass=FAIprofile)(objectClass=FAIpartitionTable))",array("cn","objectClass","FAIdebianSection"));
-    /* Sort all entries, and attach elementtype.
-     * To be able to show the types in the listbox.
-     */
-    while($attr = $ldap->fetch()){
+    $ui = get_userinfo();
+    $res = get_list(          "(|(objectClass=FAIpackageList)".
+        "(objectClass=FAItemplate)".
+        "(objectClass=FAIvariable)".
+        "(objectClass=FAIscript)".
+        "(objectClass=FAIhook)".
+        "(objectClass=FAIprofile)".
+        "(objectClass=FAIpartitionTable))",
+        $ui->subtreeACL,$this->config->current['BASE'],
+        array("cn","objectClass","FAIdebianSection"));
+    
+    foreach($res as $attr){
       $cn = $attr['cn'][0];
     
       $_SESSION['getAvailableClassesForThisRelease_CACHED_CLASSES'][] = $attr;
@@ -108,8 +126,11 @@ class workstartup extends plugin
       natcasesort($this->FAIclasses);
     }
 
-    $this->FAIclassInfo = $tmp2;
-
+    if(isset($tmp2)){
+      $this->FAIclassInfo = $tmp2;
+    } else {
+      $this->FAIclassInfo = array();
+    }
 
     /* Build up an array like this one :
         [$url]['SERVER'] = 'srv1-002';
@@ -152,7 +173,35 @@ class workstartup extends plugin
         }
       }
     }
-    $this->FAIServRepConfig =$test;
+    
+    /* Add possible elements from hook */
+    $lines= $this->GetHookElements();
+    foreach ($lines as $hline){
+      $entries= split(";", $hline);
+      if (isset($entries[1]) && !isset($test[$entries[1]])){
+        $test[$entries[1]]['RELEASE']= array();
+
+        /* Split releases */
+        if (isset($entries[2])){
+          $releases= split(",", $entries[2]);
+
+          foreach ($releases as $release){
+            $rname= preg_replace('/:.*$/', '', $release);
+            $sections= split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release));
+            $classes= split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release));
+            $test[$entries[1]]['RELEASE'][$rname]= array();
+            $test[$entries[1]]['RELEASE'][$rname]['SECTION']= $sections;
+            foreach ($classes as $class){
+              if ($class != ""){
+                $test[$entries[1]]['RELEASE'][$rname]['PACKAGES'][$class]= $class;
+              }
+            }
+          }
+        }
+      }
+    }
+    
+    $this->FAIServRepConfig= $test;
 
     /* Get arrays */
     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
@@ -222,7 +271,43 @@ class workstartup extends plugin
     $this->orig_dn= $this->dn;
 
     /* Handle inheritance value "default" */
-    $this->gotoBootKernels= array("%default%" => '['._("inherited").']');
+    $this->gotoBootKernels= array();
+
+
+    /* Load hardware list */
+    $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))");
+    if ($ldap->count() == 1){
+      $map= array("gotoLdapServer","FAIclass","FAIdebianMirror","gotoBootKernel");
+      $attrs= $ldap->fetch();
+
+      foreach ($map as $name){
+        if (!isset($attrs[$name][0])){
+          continue;
+        }
+
+        switch ($name){
+          case 'gotoLdapServer':
+            $this->goLdapServerList= array_merge(array('default-inherited' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
+            break;
+
+          case 'gotoBootKernel':
+            $this->gotoBootKernels['default-inherited']=  _("inherited").' ['.$attrs[$name][0].']' ;
+            break;
+
+          case 'FAIclass':
+            $str = split(":",$attrs[$name][0]);
+            $this->InheritedFAIclass    = split("\ ",trim($str[0]));
+            $this->InheritedFAIrelease  = trim($str[1]);
+            break;
+
+          case 'FAIdebianMirror':
+            $this->InheritedFAIdebianMirror = $attrs[$name][0];
+            break;
+        }
+      }
+    }
 
     /* Get list of boot kernels */
     if (isset($this->config->data['TABS'])){
@@ -232,20 +317,40 @@ class workstartup extends plugin
         $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
             get_class($this));
       } else {
+        
+        $first = false; 
         $fh= popen($command, "r");
         while (!feof($fh)) {
           $buffer= trim(fgets($fh, 256));
-          
-          if ($buffer != ""){
-            $this->gotoBootKernels[preg_replace('/:.*$/', '', $buffer)]= $buffer;
+  
+          if(!empty($buffer)){
+
+            $name=$value = $buffer;
+            if(preg_match("/:/",$buffer)){
+              $name = preg_replace("/:.*$/","",$buffer);
+              $value= preg_replace("/^.*:/","",$buffer);
+              $this->gotoBootKernels[$name]= $name.":".$value;
+            }else{
+              $this->gotoBootKernels[$name]= $value;
+            }
+            if($this->gotoBootKernel =="" ){
+              $this->gotoBootKernel = $name;
+            }
+
           }
         }
         pclose($fh);
-        ksort($this->gotoBootKernels);
       }
-      
     }
 
+    /* Turn to default, if we've nothing to inherit */
+    if (!isset($this->gotoBootKernels['default-inherited']) && $this->gotoBootKernel == "default-inherited"){
+      $this->gotoBootKernel= "default";
+    }
+
+    if(count($this->FAIclass)==0 && $this->FAIrelease == ""){
+      $this->FAIdebianMirror = "inherited";
+    }
   }
 
   
@@ -263,28 +368,6 @@ class workstartup extends plugin
       return($_SESSION['getAvailableClassesForThisRelease_CACHE'][$release]);
     }
 
-    /* Create cache with all classes 
-     */
-    if((!isset($_SESSION['getAvailableClassesForThisRelease_CACHED_CLASSES'])) ||
-       (!is_array($_SESSION['getAvailableClassesForThisRelease_CACHED_CLASSES'])) ||
-       (count($_SESSION['getAvailableClassesForThisRelease_CACHED_CLASSES']) ==0 )){
-
-      /* Get ldap connection */
-      $ldap   = $this->config->get_ldap_link();
-      $ldap->cd($this->config->current['BASE']);
-
-      /* Get possible classes ... 
-         This would be faste with some kind of caching ... 
-       */
-      $ldap->search("(|(objectClass=FAIpackageList)(objectClass=FAItemplate)(objectClass=FAIvariable)(objectClass=FAIscript)(objectClass=FAIhook)(objectClass=FAIprofile)(objectClass=FAIpartitionTable))",array("cn"),true);
-      /* Sort all entries, and attach elementtype.
-       * To be able to show the types in the listbox.
-       */
-      while($attr = $ldap->fetch()){
-        $_SESSION['getAvailableClassesForThisRelease_CACHED_CLASSES'][] = $attr;
-      }  
-    }
-
     /* Walk through cache and get out what we need.
      *   
      *   Function od : "$this->generateDNSyn($release)"
@@ -321,6 +404,8 @@ class workstartup extends plugin
   function selectFriendlyClasses(){
     $tmp=array();
 
+    if($this->FAIdebianMirror == "inherited") return($tmp);
+
     /* check if the current release exists,
         else select the first one ..
      */
@@ -506,13 +591,6 @@ class workstartup extends plugin
       }
     }
 
-    /* Delete selected class from our list */
-    if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
-      if(isset($this->FAIclass[$_POST['FAIclassSel']])){
-        unset($this->FAIclass[$_POST['FAIclassSel']]);
-      }
-    }
-
     /* Show main page */
     $smarty= get_smarty();
     $smarty->assign("SelectBoxLdapServer","");
@@ -532,11 +610,13 @@ class workstartup extends plugin
       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
         print_red(_("You must specify a valid mount point."));
       }else{
-        $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
-        $s_mount = $_POST['gotoShareMountPoint'];
-        /* Preparing the new assignment */
-        $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
-        $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
+        if(count($this->gotoAvailableShares)){
+          $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
+          $s_mount = $_POST['gotoShareMountPoint'];
+          /* Preparing the new assignment */
+          $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
+          $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
+        }
       }
     }
 
@@ -548,11 +628,12 @@ class workstartup extends plugin
     }
 
     $smarty->assign("gotoShares",$this->printOutAssignedShares());
+    $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
 
     /* Arrays */
-    $tmp = $this->config->data['SERVERS']['LDAP'];
+    $tmp = $this->goLdapServerList;
 
     /* Create divSelectBox for ldap server selection
      */
@@ -571,18 +652,20 @@ class workstartup extends plugin
 
     /* Add Entries 
      */
-    foreach($tmp as $server){
+    foreach($tmp as $key => $server){
       $use ="";
       if(($this->gotoLdapServer == $server) || ($found == false)) {
         $found = true;
         $use = " checked ";
       };
 
+      $display = $server;
+
       $SelectBoxLdapServer->AddEntry(
           array(
-            array("string"=>$server),
-            array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
-                  "attach"=>"style='border-right:0px;'")
+            array("string"=>"<input type='radio' name='gotoLdapServer' value='".$key."' ".$use.">",
+                  "attach"=>"style='border-left:0px;'"),
+            array("string"=>$display)
             ));
     }    
 
@@ -600,11 +683,13 @@ class workstartup extends plugin
     }
 
     $smarty->assign("FAIdebianMirrors",$this->getFAIdebianMirrors());
+    $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
     $smarty->assign("FAIreleases",$this->getFAIreleases());
     $smarty->assign("FAIrelease",$this->FAIrelease);
     $smarty->assign("FAIclasses",$this->selectFriendlyClasses());
     $smarty->assign("FAIclassesKeys",array_flip($this->selectFriendlyClasses()));
     $smarty->assign("FAIclassKeys",$this->FAIclass);
+    $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
     
     $div = new divSelectBox("WSFAIscriptClasses");
     $div -> SetHeight("110");
@@ -614,18 +699,35 @@ class workstartup extends plugin
     $str_empty  = " &nbsp;<img src='images/empty.png' alt=\"\" width='7'>"; 
 
     $i = 1;
-    foreach($this->FAIclass as $class){
-      if($i==1){
-        $str = $str_empty.$str_down.$str_remove;
-      }elseif($i == count($this->FAIclass)){
-        $str = $str_up.$str_empty.$str_remove;
+
+    if($this->FAIdebianMirror == "inherited"){
+      $tmp = $this->InheritedFAIclass;
+    }else{
+      $tmp = $this->FAIclass;
+    }
+
+    foreach($tmp as $class){
+
+      $marker = "";
+      if(in_array_ics($class,$this->unresolved_classes)){
+        $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
+      }
+
+      if($this->FAIdebianMirror == "inherited"){
+        $str = "";
       }else{
-        $str = $str_up.$str_down.$str_remove;
+        if($i==1){
+          $str = $str_empty.$str_down.$str_remove;
+        }elseif($i == count($this->FAIclass)){
+          $str = $str_up.$str_empty.$str_remove;
+        }else{
+          $str = $str_up.$str_down.$str_remove;
+        }
       }
       $i ++ ; 
 
       $div->AddEntry(array(
-            array("string"=>$class),
+            array("string"=>$class.$marker),
             array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
             ));
     }  
@@ -687,6 +789,7 @@ class workstartup extends plugin
   function getFAIdebianMirrors()
   {
     $ret = array();
+    $ret['inherited']="["._("inherited")."]";
     $ret['auto']=_("automatic");
     $secs  = array();
 
@@ -723,29 +826,32 @@ class workstartup extends plugin
   {
     $ret = array();
 
+    if($this->FAIdebianMirror == "inherited") return(array());
+
     if(!isset($this->FAIServRepConfig[$this->FAIdebianMirror])){
       $this->FAIdebianMirror = "auto";
     }
 
-    $errorClasses = "";  
-  
-    foreach($this->FAIServRepConfig[$this->FAIdebianMirror]['RELEASE'] as $release => $sections){
-      $use = true;
-      
-      if(!count($this->FAIclass) == 0){
-        foreach($this->FAIclass as $class){
-          if(!in_array($class, $sections['PACKAGES'])){
-            $use = false;
-            $errorClasses[$class] = $class;
-          }else{
-            if(isset($errorClasses[$class])){
-              unset($errorClasses[$class]); 
+    $errorClasses = array();  
+    if(isset($this->FAIServRepConfig[$this->FAIdebianMirror]['RELEASE'])) {
+      foreach($this->FAIServRepConfig[$this->FAIdebianMirror]['RELEASE'] as $release => $sections){
+        $use = true;
+
+        if(!count($this->FAIclass) == 0){
+          foreach($this->FAIclass as $class){
+            if(!in_array($class, $sections['PACKAGES'])){
+              $use = false;
+              $errorClasses[$class] = $class;
+            }else{
+              if(isset($errorClasses[$class])){
+                unset($errorClasses[$class]); 
+              }
             }
           }
         }
-      }
-      if($use){
-        $ret[$release]=$release;
+        if($use){
+          $ret[$release]=$release;
+        }
       }
     } 
     if((count($ret) == 0 ) && ($this->FAIdebianMirror != "auto")){
@@ -765,8 +871,10 @@ class workstartup extends plugin
         $eClasses .= $class." ";
       }
       
-      $this->FAIclass= array();
-      print_red(sprintf(_("Can't resolve the given FAIclass(es) [%s] anyway, please check your FAI configurations, possibly some classes where deleted or renamed. !All classes have been removed from this account, press cancel if you don't want this to be saved."),$eClasses));
+      $this->unresolved_classes = $errorClasses;
+      $this->FAIdebianMirror = "auto";
+      //$this->FAIclass= array();
+      print_red(sprintf(_("Can't resolve the given FAIclass(es) [%s] anyway, please check your FAI configurations, possibly some classes where deleted or renamed. Server was reset to 'auto'."),$eClasses));
     }
     return($ret);
   }
@@ -785,6 +893,7 @@ class workstartup extends plugin
     if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
       $this->customParameters= $_POST["customParameters"];
     }
+
   }
 
 
@@ -811,7 +920,7 @@ class workstartup extends plugin
     /* Find proper terminal path for tftp configuration
        FIXME: This is suboptimal when the default has changed to
        another location! */
-    if ($this->gotoTerminalPath == "default"){
+    if (($this->gotoTerminalPath == "default")){
       $ldap= $this->config->get_ldap_link();
 
       /* Strip relevant part from dn, keep trailing ',' */
@@ -824,7 +933,7 @@ class workstartup extends plugin
         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
 
         $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
-            $this->config->current['BASE']);
+            $this->config->current['BASE'], array('gotoTerminalPath'));
         $attrs= $ldap->fetch();
         if (isset($attrs['gotoTerminalPath'])){
           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
@@ -859,15 +968,23 @@ class workstartup extends plugin
     unset( $this->attrs['FAIrelease'] );
     
     $str = "";
-    foreach($this->FAIclass as $class){
-      $str .= $class." ";
-    }
-    $str .= ":" . $this->FAIrelease;
-    $this->attrs['FAIclass']= "";
-    $this->attrs['FAIclass']= trim($str);
 
-    if(empty($this->attrs['FAIclass'])){
-      $this->attrs['FAIclass'] = array();
+    if($this->FAIdebianMirror == "inherited"){
+  
+      $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array(); 
+
+    }else{
+
+      foreach($this->FAIclass as $class){
+        $str .= $class." ";
+      }
+      $str .= ":" . $this->FAIrelease;
+      $this->attrs['FAIclass']= "";
+      $this->attrs['FAIclass']= trim($str);
+
+      if(empty($this->attrs['FAIclass'])){
+        $this->attrs['FAIclass'] = array();
+      }
     }
 
     /* Add missing arrays */
@@ -878,11 +995,13 @@ class workstartup extends plugin
       }
       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
     }
+
     /* Strip out 'default' values */
-    if ($this->attrs['gotoLdapServer'] == "default"){
+    if ($this->attrs['gotoLdapServer'] == "default-inherited"){
       $this->attrs['gotoLdapServer']= array();
     }
-    if ($this->attrs['gotoBootKernel'] == "%default%"){
+
+    if (($this->attrs['gotoBootKernel'] == "default-inherited") || ($this->attrs['gotoBootKernel'] == "%default%")){
       $this->attrs['gotoBootKernel']= array();
     }
 
@@ -890,15 +1009,15 @@ class workstartup extends plugin
     if($this->FAIdebianMirror == "none"){
       $this->FAIdebianMirror = "";
     }
-    
-    if((count($this->attrs['FAIclass'])==0)&&(empty($this->FAIdebianMirror))){
-      $tmp = array();
-      foreach($this->attrs['objectClass'] as $class){
-        if($class != "FAIobject"){
-          $tmp[] = $class;
-        }
-      }
-      $this->attrs['objectClass']     = $tmp;
+   
+    /* Get FAIstate from object, the generic tab could have changed it during execute */
+    $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->dn);
+    $ldap->cat($this->dn,array("FAIstate"));
+    $checkFAIstate = $ldap->fetch();
+
+    /* Remove FAI objects if no FAI class is selected */ 
+    if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
       $this->attrs['FAIclass']        = array();
       $this->attrs['FAIdebianMirror'] = array();
     }
@@ -912,12 +1031,10 @@ class workstartup extends plugin
     }
     $this->attrs['gotoShare']=$tmp;
 
-    $ldap= $this->config->get_ldap_link();
-    $ldap->cd($this->dn);
     $this->cleanup();
     $ldap->modify ($this->attrs); 
 
-    show_ldap_error($ldap->get_error());
+    show_ldap_error($ldap->get_error(), _("Saving workstation startup settings failed"));
     $this->handle_post_events("modify");
   }
 
@@ -958,6 +1075,29 @@ class workstartup extends plugin
     return($a_return);
   }
 
+
+  function GetHookElements()
+  {
+    $ret = array();
+    $cmd= search_config($this->config->data['TABS'], "servrepository", "REPOSITORY_HOOK");
+    if(!empty($cmd)){
+      $res = shell_exec($cmd);
+      $res2 = trim($res);
+      if((!$res)){
+        print_red(sprintf(_("Can't execute specified REPOSITORY_HOOK '%s' please check your gosa.conf."),$cmd));
+      }elseif(empty($res2)){
+        print_red(sprintf(_("The specified REPOSITORY_HOOK '%s', specified in your gosa.conf, returns an empty string."),$cmd));
+      }else{
+        $tmp = split("\n",$res);
+        foreach($tmp as $line){
+          if(empty($line)) continue;
+          $ret[]= $line;
+        }
+      }
+    }
+    return($ret);
+  }
+
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: