Code

Updated ldap action handling for installable devices
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_workstationStartup.inc
1 <?php
2 class workstartup extends plugin
3 {
4     /* Ldap server list */
5     var $gotoLdapServers    = array();
6     var $gotoLdapServerList = array();
7     var $gotoLdap_inherit   = FALSE;
9     /* Generic terminal attributes */
10 # This is disabled as long there is no possiblilty to set these parameters
11     var $gotoBootKernel       = "default-inherited";
12     var $gotoKernelParameters = "";
13     var $gotoLdapServer       = "default-inherited";
14     var $gotoModules          = array();
15     var $gotoAutoFs           = array();
16     var $gotoFilesystem       = array();
17     var $gotoTerminalPath     = "";
18     var $gotoBootKernels      = array();
20     /* attribute list for save action */
21     var $attributes           = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", 
22             "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror", "FAIrelease");
23     var $objectclasses        = array("GOhard", "FAIobject");
25     /* Share */
26     var $gotoShares         = array();// Currently Share Option
27     var $gotoShare          = "";     // currently selected Share Option
28     var $gotoShareSelections= array();// Available Shares for this account in Listbox format
29     var $gotoAvailableShares= array();// Available Shares for this account
31     /* Helper */
32     var $orig_dn            = "";
33     var $ignore_account     = TRUE;
35     /* FAI class selection */ 
36     var $FAIclass           = array();  // The currently selected classes 
37     var $FAIrelease           = "";
38     var $FAIdebianMirror      = "auto";
39     var $si_active            = FALSE;
40     var $si_fai_action_failed = FALSE;
42     var $cache              = array(); // Used as cache in fai mehtods
44     var $FAIstatus          = "";
45     var $FAIclasses         = array();
47     var $view_logged        = FALSE;
49     /* FAI class selection */
50     var $InheritedFAIclass       = array();
51     var $InheritedFAIrelease     = "";
52     var $InheritedFAIdebianMirror= "auto";
54     var $CopyPasteVars    = array("gotoModules","gotoShares");
55     var $fai_activated    = FALSE;
56     var $o_group_dn       = "";
57     var $member_of_ogroup = FALSE;
59     function workstartup (&$config, $dn= NULL, $parent= NULL)
60     {
61         /* Check if FAI is active */
62         if($config->pluginEnabled("faiManagement")){
63             $this->fai_activated = TRUE;
64         }else{
65             $this->attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "gotoShare");
66             $this->objectclasses  = array("GOhard");
67         }
69         plugin::plugin ($config, $dn, $parent);
71         /* Check for si daemon */
72         $this->si_active = $this->config->get_cfg_value("core","gosaSupportURI") != "";
74         /* Check object group membership */
75         if(!isset($this->parent->by_object['ogroup'])){
76             $ldap = $this->config->get_ldap_link();
77             $ldap->cd ($this->config->current['BASE']);
78             $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn","dn"));
79             if($ldap->count()){
80                 $this->member_of_ogroup = TRUE;
81                 $attrs = $ldap->fetch();
82                 $this->o_group_dn = $attrs['dn'];
83             }
84         }
86         /* Creating a list of valid Mirrors 
87          * none will not be saved to ldap.
88          */
89         $ldap   = $this->config->get_ldap_link();
90         $ldap->cd($this->config->current['BASE']);
91         $ui = get_userinfo();
92         foreach($this->config->data['SERVERS']['LDAP'] as $dn => $data){
93             if($ui->get_category_permissions($data['dn'],"server",TRUE)){
94                 for($i = 0; $i < $data['goLdapBase']['count']; $i ++){
95                     $name = $data["cn"][0].":".$data["goLdapBase"][$i];
96                     $this->gotoLdapServerList[]= $name; 
97                 }
98             }
99         }
101         /* Get list of assigned ldap servers 
102          */ 
103         if(isset($this->attrs['gotoLdapServer'])){
104             unset($this->attrs['gotoLdapServer']['count']);
105             sort($this->attrs['gotoLdapServer']);
106             foreach($this->attrs['gotoLdapServer'] as $value){
107                 $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
108             }
109         } 
110         natcasesort($this->gotoLdapServerList);
112         if(!count($this->gotoLdapServers) && $this->member_of_ogroup){ 
113             $this->gotoLdap_inherit = TRUE;
114         }
116         /* FAI Initialization
117            Skip this if FAI is not activated 
118          */
119         if($this->fai_activated) {
121             /* Parse used FAIclasses (stored as string).
122              * The single classes are seperated by ' '.
123              * There is also the release type given, after first
124              *  occurrence of ':'.
125              */
126             $this->FAIclass =array();
127             if(isset($this->attrs['FAIclass'][0])){
128                 $tmp = explode(" ",$this->attrs['FAIclass'][0]);
129                 $tmp2 =array();  
131                 foreach($tmp as $class){
132                     if( ":" == $class[0] ) {
133                         $this->FAIrelease = trim(substr($class, 1));
134                     }else{
135                         $tmp2[$class] = $class;
136                     }
137                 }
138                 $this->FAIclass = $tmp2;
139             }
140         }
142         /* Get arrays */
143         foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
144             if (isset($this->attrs["$val"]["count"])){
145                 for ($i= 0; $i<$this->attrs["count"]; $i++){
146                     if (isset($this->attrs["$val"][$i])){
147                         array_push($this->$val, $this->attrs["$val"][$i]);
148                     }
149                 }
150             }
151             sort ($this->$val);
152             $this->$val= array_unique($this->$val);
153         }
155         /* Prepare Shares */
156         if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
157             unset($this->attrs['gotoShare']['count']);
158             foreach($this->attrs['gotoShare'] as $share){
159                 $tmp = $tmp2 = array();
160                 $tmp = explode("|",$share);
161                 $tmp2['server']      =$tmp[0];
162                 $tmp2['name']        =$tmp[1];
163                 $tmp2['mountPoint']  =$tmp[2];
164                 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
165             }
166         }
168         $this->gotoShareSelections= $config->getShareList(true);
169         $this->gotoAvailableShares= $config->getShareList(false);
170         $tmp2 = array();
173         $this->orig_dn= $this->dn;
175         /* Handle inheritance value "default" */
176         if ($this->member_of_ogroup){
177             $this->gotoBootKernels= array("default-inherited" => '['._("inherited").']'); 
178         }
180         /* If we are member in an object group,
181          *  we have to handle inherited values.
182          * So you can see what is inherited.
183          */
184         if ($this->member_of_ogroup){
186             if(count($this->FAIclass)==0 && $this->FAIrelease == ""){
187                 $this->FAIdebianMirror = "inherited";
188             }
190             if($this->fai_activated){
191                 $map= array("gotoBootKernel","FAIclass","FAIdebianMirror");
192             }else{
193                 $map= array("gotoBootKernel");
194             }
196             $ldap = $this->config->get_ldap_link();
197             $ldap->cat($this->o_group_dn);
198             $attrs= $ldap->fetch();
200             foreach ($map as $name){
201                 if (!isset($attrs[$name][0])){
202                     continue;
203                 }
205                 switch ($name){
206                     case 'gotoBootKernel':
207                         $this->gotoBootKernels['default-inherited']=  _("inherited").' ['.$attrs[$name][0].']' ;
208                         break;
210                     case 'FAIclass':
211                         $str = explode(":",$attrs[$name][0]);
212                         $this->InheritedFAIclass    = explode(" ",trim($str[0]));
213                         $this->InheritedFAIrelease  = trim($str[1]);
214                         break;
216                     case 'FAIdebianMirror':
217                         $this->InheritedFAIdebianMirror = $attrs[$name][0];
218                         break;
219                 }
220             }
221         }
223         $this->update_fai_cache();
225         if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
227             /* Check if the current mirror is available 
228              */
229             if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
230                 if(count($this->FAIclass)){
231                     msg_dialog::display(_("Error"), sprintf(_("FAI mirror '%s' is not available - setting to mirror 'auto'!"), $this->FAIdebianMirror), ERROR_DIALOG);
232                 }
233                 $this->FAIdebianMirror = "auto";
234                 $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
235                 $this->cache['CLASSES'] = array();
236                 $this->update_fai_cache();
237             }
239             /* Check if the current mirror is available 
240              */
241             if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
242                 $new_release = key($this->cache['SERVERS'][$this->FAIdebianMirror]); 
243                 if(count($this->FAIclass)){
244                     msg_dialog::display(_("Error"), sprintf(_("FAI release '%s' is not available on mirror '%s' - setting to release '%s'!"), $this->FAIrelease, $this->FAIdebianMirror,$new_release), ERROR_DIALOG);
245                 }
246                 $this->FAIrelease = $new_release;
247                 $this->cache['CLASSES'] = array();
248                 $this->update_fai_cache();
249             }
250         }
253         // Prepare lists
254         $this->ldapList = new sortableListing(array(),array(), TRUE);
255         $this->ldapList->setDeleteable(true);
256         $this->ldapList->setEditable(false);
257         $this->ldapList->setWidth("100%");
258         $this->ldapList->setHeight("100px");
259         $this->ldapList->setHeader(array(_("LDAP server")));
260         $this->ldapList->setDefaultSortColumn(0);
262         $this->faiList = new sortableListing(array(),array(), TRUE);
263         $this->faiList->setDeleteable(true);
264         $this->faiList->setEditable(false);
265         $this->faiList->setWidth("100%");
266         $this->faiList->setHeight("160px");
267         $this->faiList->setColspecs(array('50%','50%'));
268         $this->faiList->setHeader(array(_("Name"),_("Classes")));
269         $this->faiList->setDefaultSortColumn(0);
271     }
274     function check()
275     {
276         $messages = array();
278         /* Call common method to give check the hook */
279         $messages= plugin::check();
281         /* If there are packages selected, but no mirror show error */   
282         if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
283             $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
284         }
286         return($messages);
287     }
289     function execute()
290     {
291         /* Call parent execute */
292         plugin::execute();
294         if($this->is_account && !$this->view_logged){
295             $this->view_logged = TRUE;
296             new log("view","workstation/".get_class($this),$this->dn);
297         }
299         /* Do we represent a valid terminal? */
300         if (!$this->is_account && $this->parent === NULL){
301             $display= image("images/small-error.png")."<b>".msgPool::noValidExtension(_("workstation"))."</b>";
302             return ($display);
303         }
305         /* Add module */
306         if (isset ($_POST['add_module'])){
307             if ($_POST['module'] != "" && $this->acl_is_writeable("gotoModules")){
308                 $this->add_list ($this->gotoModules, get_post('module'));
309             }
310         }
312         /* Delete module */
313         if (isset ($_POST['delete_module'])){
314             if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoModules")){
315                 $this->del_list ($this->gotoModules, get_post('modules_list'));
316             }
317         }
319         /* FAI class management */
320         if($this->fai_activated){
321             if(((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))) && ($this->acl_is_writeable("FAIclass"))){
322                 $found = 0 ; 
324                 /* If this new class/profile will attach a second partition table
325                  * to our list of classes, abort and show a message.
326                  */
327                 foreach($this->FAIclass as $name){
328                     if(isset($this->FAIclassInfo[$name])){
329                         foreach($this->FAIclassInfo[$name] as $atr){
330                             if(isset($atr['obj'])){
331                                 if($atr['obj'] == "FAIpartitionTable"){
332                                     $found ++ ; 
333                                 }
334                             }
335                         }
336                     }
337                 }
339                 if((isset($this->FAIclassInfo[get_post('FAIclassesSel')]['FAIpartitionTable']))&&($found>0)){
340                     msg_dialog::display(_("Error"), _("There is already a profile containing a partition table in your configuration!") , ERROR_DIALOG);
341                 }else{
342                     $this->FAIclass[get_post('FAIclassesSel')]=get_post('FAIclassesSel');
343                 }
344             }
347             $this->faiList->save_object();
348             $action = $this->faiList->getAction();
349             if($action['action'] == 'delete'){
350                 $id = $this->faiList->getKey($action['targets'][0]);
351                 unset($this->FAIclass[$id]);
352             }
353             if($action['action'] == 'reorder'){
354                 $this->FAIclass= $this->faiList->getMaintainedData();
355             }
357             /* Move one used class class one position up or down */
358             if($this->acl_is_writeable("FAIclass")){
359                 foreach($_POST as $name => $val){
362                     if(preg_match("/fai_remove/i",$name)){
363                         $value = postDecode(preg_replace("/fai_remove_/i","",$name));
364                         unset($this->FAIclass[$value]);
365                     }
366                 }
367             }
369             /* Delete selected class from our list */
370             if($this->acl_is_writeable("FAIclass")){
371                 if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
372                     if(isset($this->FAIclass[get_post('FAIclassSel')])){
373                         unset($this->FAIclass[get_post('FAIclassSel')]);
374                     }
375                 }
376             }
377         }// END fai handling
379         /* Show main page */
380         $smarty= get_smarty();
382         /* Assign ACLs to smarty */
383         $tmp = $this->plInfo();
384         foreach($tmp['plProvidedAcls'] as $name => $translation){
385             $smarty->assign($name."ACL",$this->getacl($name));
386         } 
388         $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
390         /* In this section server shares will be defined
391          * A user can select one of the given shares and a mount point
392          *  and attach this combination to his setup.
393          */
394         $smarty->assign("gotoShareSelections",    set_post($this->gotoShareSelections));
395         $smarty->assign("gotoShareSelectionKeys", set_post(array_flip($this->gotoShareSelections)));
397         /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
398          * This entry will be, a combination of mountPoint and sharedefinitions
399          */
400         if((isset($_POST['gotoShareAdd'])) && isset($_POST['gotoShareSelection']) && ($this->acl_is_writeable("gotoShare"))) {
401             /* We assign a share to this user, if we don't know where to mount the share */
402             if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
403                 msg_dialog::display(_("Error"), msgPool::required(_("Mount point")), ERROR_DIALOG);
404             }else{
406                 if(isset($this->gotoAvailableShares[get_post('gotoShareSelection')])){
407                     $a_share = $this->gotoAvailableShares[get_post('gotoShareSelection')];
408                     $s_mount = get_post('gotoShareMountPoint');
409                     /* Preparing the new assignment */
410                     $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
411                     $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
412                 }
413             }
414         }
416         /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
417          * If there is no defined share selected, we will abort the deletion without any message
418          */
419         if(($this->acl_is_writeable("gotoShare"))&& (isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
420             unset($this->gotoShares[get_post('gotoShare')]);
421         }
423         $smarty->assign("gotoShares",       set_post($this->printOutAssignedShares()));
424         $smarty->assign("gotoSharesCount",  count($this->printOutAssignedShares()));
425         $smarty->assign("gotoShareKeys",    set_post(array_flip($this->printOutAssignedShares())));
426         $smarty->assign("gotoBootKernels",  set_post($this->gotoBootKernels));
428         $this->ldapList->setAcl($this->getacl('gotoLdapServer'));
430         /* Add new ldap server to the list */
431         if($this->acl_is_writeable("gotoLdapServer") && 
432                 !$this->gotoLdap_inherit && 
433                 isset($_POST['add_ldap_server']) && 
434                 isset($_POST['ldap_server_to_add'])){
435             if(isset($this->gotoLdapServerList[get_post('ldap_server_to_add')])){
436                 $to_add = $this->gotoLdapServerList[get_post('ldap_server_to_add')];
437                 if(!in_array($to_add,$this->gotoLdapServers)){
438                     $this->gotoLdapServers[] = $to_add;
439                 }
440             }
441         }
443         /* Move ldap servers up and down */
444         $this->ldapList->save_object();
445         $action = $this->ldapList->getAction();
446         if($action['action'] == 'reorder'){
447             $this->gotoLdapServers = array_values($this->ldapList->getMaintainedData());
448         }
449         if($action['action'] == 'delete'){
450             $id = $this->ldapList->getKey($action['targets'][0]);
451             $value = $this->gotoLdapServers[$id];
452             $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
453         }
455         /* Add Entries */
456         $data = $lData = array();
457         foreach($this->gotoLdapServers as $key => $server){
458             $data[$key]=$server;
460             /* Announce missing entries */
461             if(!in_array($server,$this->gotoLdapServerList)){
462                 $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
463             }
465             /* Convert old style entry */
466             if (!preg_match('%:ldaps?://%', $server)){
467                 $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
468         } else {
469             $server= preg_replace("/^[^:]+:/", "", $server);
470             $lData[$key] = array('data'=>array($server));
471         }    
472         }
473         $this->ldapList->setListData($data,$lData);
474         $this->ldapList->update();
477         if($this->gotoLdap_inherit){
478             $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
479         }else{
480             $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
481         }
483         $list = array();
484         foreach($this->gotoLdapServerList as $key => $entry){
485             if(!in_array($entry,$this->gotoLdapServers)){
487                 /* Convert old style entry */
488                 if (!preg_match('%:ldap[s]*://%', $entry)){
489                     $entry= "ldap://".preg_replace('/^([^:]+):/', '\1/', $entry);
491                 /* Beautify new style entries */
492             } else {
493                 $entry= preg_replace("/^[^:]+:/", "", $entry);
494             }
496             $list[$key] = $entry;
497             }
498         }
499         $smarty->assign("gotoLdapServers",    $this->ldapList->render());
500         $smarty->assign("gotoLdapServerList", $list);
501         $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
503         foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
504             $smarty->assign("$val", set_post($this->$val));
505         }
507         /* Values */
508         foreach(array("gotoBootKernel","gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
509             $smarty->assign($val, set_post($this->$val));
510         }
512         $smarty->assign("fai_activated",$this->fai_activated);
514         /* Create FAI output */
515         $this->update_fai_cache();
516         $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
517         $smarty->assign("si_active",$this->si_active);
519         if(!$this->si_fai_action_failed && $this->si_active && $this->fai_activated){
521             $smarty->assign("FAIservers"  ,    set_post($this->cache['SERVERS']));
522             $smarty->assign("FAIdebianMirror", set_post($this->FAIdebianMirror));
523             $smarty->assign("FAIrelease"  ,    set_post($this->FAIrelease));
524             $smarty->assign("FAIclasses"  ,    set_post($this->selectable_classes()));
526             /* Get classes for release from cache.
527              * Or build cache
528              */
529             if($this->FAIdebianMirror == "inherited"){
530                 $release = $this->InheritedFAIrelease;
531             }else{
532                 $release = $this->FAIrelease;
533             }
535             $smarty->assign("gotoBootKernels", set_post($this->cache['KERNELS'][$release]));
536             $smarty->assign("InheritedFAIrelease", set_post($this->InheritedFAIrelease));
538             /* Get classes */
539             if($this->FAIdebianMirror == "inherited"){
540                 $tmp = $this->InheritedFAIclass;
541             }else{
542                 $tmp = $this->FAIclass;
543             }
545             /* Get invalid classes */
546             $invalid = $this->get_invalid_classes($tmp);
547             $data = $lData = array();
549             /* Draw every single entry */
550             $i = 1;
551             if($this->acl_is_readable("FAIclass")){
552                 foreach($tmp as $class){
554                     /* Mark invalid classes. (Not in selected release)
555                      */
556                     $marker = "";
557                     if(in_array_ics($class,$invalid)){
558                         $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
559                     }
561                     $i ++ ; 
563                     /* Get Description tag 
564                      *  There may be several FAI objects with the same class name, 
565                      *   use the description from FAIprofile, if possible.
566                      */  
567                     $desc = ""; 
569                     $objects = array(
570                             "Pl"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png"),
571                             "Pt"     => array("IMG"=> "plugins/fai/images/fai_packages.png"),
572                             "S"          => array("IMG"=> "plugins/fai/images/fai_script.png"),
573                             "V"        => array("IMG"=> "plugins/fai/images/fai_variable.png"),
574                             "H"            => array("IMG"=> "plugins/fai/images/fai_hook.png"),
575                             "P"         => array("IMG"=> "plugins/fai/images/fai_profile.png"),
576                             "T"        => array("IMG"=> "plugins/fai/images/fai_template.png"));
578                     if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
579                         $desc ="";
580                         foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
581                             if(isset($types['Abbr'])){
582                                 $desc.= image($objects[$types['Abbr']]['IMG']);
583                                 if($types['Type'] == "FAIprofile"){
584                                     break;
585                                 }
586                             }
587                         }
588                     }
589                     if(!empty($desc)){
590                         $desc = "&nbsp;".trim($desc)."";
591                     }        
593                     $data[$class] = $class;
594                     $lData[$class] = array('data' => array($class,$desc));
595                 }  
596             }// END FAI output generation 
597         }// END FAI output generation 
599         $this->faiList->setAcl('rwcdm');
600         $this->faiList->setReorderable($this->FAIdebianMirror != "inherited");
601         $this->faiList->setListData($data,$lData);
602         $this->faiList->update();
604         $smarty->assign("FAIScriptlist", $this->faiList->render());
606         /* Show main page */
607         $smarty->assign("gotoKernelParameters",set_post($this->gotoKernelParameters));
608         return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
609     }
612     function remove_from_parent()
613     {
614         $this->handle_post_events("remove");
615         new log("remove","workstation/".get_class($this),$this->dn);
616     }
619     /* Save data to object */
620     function save_object()
621     {
622         $old_mirror  = $this->FAIdebianMirror;
623         plugin::save_object();
625         /* Update release */
626         if($old_mirror != $this->FAIdebianMirror){
627             if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
628                 $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
629             }
630         }
632         if(isset($_POST['WorkstationStarttabPosted'])){
633             if(isset($_POST['gotoLdap_inherit'])){
634                 $this->gotoLdap_inherit = TRUE;
635             }else{
636                 $this->gotoLdap_inherit = FALSE;
637             }
638         }
639     }
642     /* Save to LDAP */
643     function save()
644     {
646         /* Depending on the baseobject (Ogroup / WS) we
647          *  use another set of objectClasses
648          * In case of WS itself, we use  "array("GOhard", "FAIobject");"
649          * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
650          */
651         if(isset($this->parent->by_object['ogroup'])){
652             $this->objectclasses = array("gotoWorkstationTemplate");
653         }elseif(isset($this->parent->by_object['workgeneric'])){
654             $this->objectclasses = array("GOhard");
655         }elseif(isset($this->parent->by_object['servgeneric'])){
656             $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
657         }else{
658             msg_dialog::display(_("Fatal error"),
659                     "Object Type Configuration is unknown. Please contact the GOsa developers.",
660                     FATAL_ERROR_DIALOG);
661             exit();
662         }
664         /* Append FAI class */
665         if($this->fai_activated){
666             $this->objectclasses[]  = "FAIobject";
667         }
669         /* Find proper terminal path for tftp configuration
670 FIXME: This is suboptimal when the default has changed to
671 another location! */
672         if (($this->gotoTerminalPath == "default")){
673             $ldap= $this->config->get_ldap_link();
675             /* Strip relevant part from dn, keep trailing ',' */
676             $tmp= preg_replace("/^cn=[^,]+,".preg_quote(get_ou("termgeneric", "terminalRDN"), '/')."/i", "", $this->dn);
677             $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
679             /* Walk from top to base and try to load default values for
680                'gotoTerminalPath'. Abort when an entry is found. */
681             while (TRUE){
682                 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
684                 $ldap->cat("cn=default,".get_ou("termgeneric", "terminalRDN").$tmp.
685                         $this->config->current['BASE'], array('gotoTerminalPath'));
686                 $attrs= $ldap->fetch();
687                 if (isset($attrs['gotoTerminalPath'])){
688                     $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
689                     break;
690                 }
692                 /* Nothing left? */
693                 if ($tmp == ""){
694                     break;
695                 }
696             }
697         }
699         plugin::save();
701         unset( $this->attrs['FAIrelease'] );
702         $str = "";
704         /* Skip FAI attribute handling if not necessary */
705         if($this->fai_activated && !$this->si_fai_action_failed){
706             if($this->FAIdebianMirror == "inherited"){
707                 $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
708             }else{
709                 foreach($this->FAIclass as $class){
710                     $str .= $class." ";
711                 }
712                 $str = trim($str);
713                 if(empty($this->attrs['FAIclass'])){
714                     $this->attrs['FAIclass'] = array();
715                 }else{
716                     $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
717                 }
718             }
719         }
721         /* Add missing arrays */
722         foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
723             if (isset ($this->$val) && count ($this->$val) != 0){
725                 $this->attrs["$val"]= array_unique($this->$val);
726             }
727             if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
728         }
730         /* Prepare list of ldap servers */
731         $this->attrs['gotoLdapServer'] = array();
732         if(!$this->gotoLdap_inherit){
733             $i = 0;
734             foreach($this->gotoLdapServers as $server){
735                 $i ++;
736                 $this->attrs['gotoLdapServer'][] = $i.":".$server;
737             }
738         }
740         if ($this->attrs['gotoBootKernel'] == "default-inherited"){
741             $this->attrs['gotoBootKernel']= array();
742         }
744         /* if mirror == none stop saving this attribute */
745         if($this->FAIdebianMirror == "none"){
746             $this->FAIdebianMirror = "";
747         }
749         /* Get FAIstate from object, the generic tab could have changed it during execute */
750         $ldap= $this->config->get_ldap_link();
751         $ldap->cd($this->dn);
754         /* Skip FAI attribute handling if not necessary */
755         if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
756             $ldap->cat($this->dn,array("FAIstate"));
757             $checkFAIstate = $ldap->fetch();
759             /* Remove FAI objects if no FAI class is selected */ 
760             if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
761                 $this->attrs['FAIclass']        = array();
762                 $this->attrs['FAIdebianMirror'] = array();
763             }
764         }else{
766             /* Don't touch FAI objects if something went wrong with the si daemon.
767              */
768             if(isset($this->attrs['FAIclass'])) unset($this->attrs['FAIclass']);
769             if(isset($this->attrs['FAIdebianMirror'])) unset($this->attrs['FAIdebianMirror']);
770         }
772         /* prepare share settings */
773         $tmp = array();
774         foreach($this->gotoShares as $name => $settings){
775             $tmp2= explode("|",$name);
776             $name = $tmp2[0];
777             $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
778         }
779         $this->attrs['gotoShare']=$tmp;
780         $this->cleanup();
781         $ldap->modify ($this->attrs); 
782         new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
784         if (!$ldap->success()){
785             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
786         }
787         $this->handle_post_events("modify");
789         /* Check if LDAP server has changed */
790         if ($this->si_active && (isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent") || $this->gotoLdap_inherit)){
791             $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
792             $o_queue = new gosaSupportDaemon();
793             if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
794                 $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
795                 $macs = array();
797                 /* Get list of macAddresses 
798                  */
799                 if(isset($this->parent->by_object['ogroup'])){
801                     /* If we are an object group, add all member macs 
802                      */
803                     $p = $this->parent->by_object['ogroup'];
804                     foreach($p->memberList as $dn => $obj){
805                         if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
806                             $macs[] = $p->objcache[$dn]['macAddress'];
807                         }
808                     }
809                 }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
811                     /* We are a workstation. Add current mac.
812                      */
813                     $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
814                     if(!empty($mac)){
815                         $macs[] = $mac;
816                     }          
817                 }elseif(isset($this->parent->by_object['servgeneric']->netConfigDNS->macAddress)){
819                     /* We are a server. Add current mac.
820                      */
821                     $mac = $this->parent->by_object['servgeneric']->netConfigDNS->macAddress;
822                     if(!empty($mac)){
823                         $macs[] = $mac;
824                     }          
825                 }
827                 /* Trigger event for all member objects 
828                  */
829                 foreach($macs as $mac){
830                     $tmp = new $evt['CLASS_NAME']($this->config);
831                     $tmp->set_type(TRIGGERED_EVENT);
832                     $tmp->add_targets(array($mac));
833                     if(!$o_queue->append($tmp)){
834                         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
835                     }
836                 }
837             }
838         }
839     }
842     /* Add value to array, check if unique */
843     function add_list (&$array, $value)
844     {
845         if ($value != ""){
846             $array[]= $value;
847             sort($array);
848             array_unique ($array);
849         }
850     }
853     /* Delete value to array, check if unique */
854     function del_list (&$array, $list)
855     {
856         $tmp= array();
857         foreach ($array as $mod){
858             if (!in_array($mod, $list)){
859                 $tmp[]= $mod;
860             }
861         }
862         $array= $tmp;
863     }
865     /* Generate ListBox frindly output for the defined shares
866      * Possibly Add or remove an attribute here,
867      */
868     function printOutAssignedShares()
869     {
870         $a_return = array();
871         if(is_array($this->gotoShares)){
872             foreach($this->gotoShares as $share){
873                 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
874             }
875         }
876         return($a_return);
877     }
881     function PrepareForCopyPaste($source)
882     {
883         plugin::PrepareForCopyPaste($source);    
884         $source_o = new workstartup ($this->config, $source['dn']);
885         foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
886                     "gotoKernelParameters","gotoShares","gotoLdapServers","gotoLdapServerList",
887                     "gotoLdap_inherit") as $attr){
888             $this->$attr = $source_o->$attr;
889         }
890     }
893     function array_switch_item($ar,$from,$to)
894     {
895         if(!is_array($ar)){
896             return(false);
897         }
898         if(!isset($ar[$from])){
899             return(false);
900         }
901         if(!isset($ar[$to])){
902             return(false);
903         }
905         $tmp = $ar[$from];
906         $ar[$from] = $ar[$to];    
907         $ar[$to] = $tmp;    
908         return($ar);
909     }
912     /* Return plugin informations for acl handling */ 
913     static function plInfo()
914     {
915         return (array( 
916                     "plShortName"   => _("Startup"),
917                     "plDescription" => _("System startup"),
918                     "plSelfModify"  => FALSE,
919                     "plDepends"     => array(),
920                     "plPriority"    => 9,
921                     "plSection"     => array("administration"),           
922                     "plCategory"    => array("workstation","server","ogroups"),
924                     "plProvidedAcls"=> array(
925                         "gotoLdapServer"        => _("LDAP server"),
926                         "gotoBootKernel"        => _("Boot kernel"),
927                         "gotoKernelParameters"  => _("Kernel parameter"),
929                         "gotoModules"           => _("Kernel modules"),
930                         "gotoShare"             => _("Shares"),
932                         "FAIclass"              => _("FAI classes"),
933                         "FAIdebianMirror"       => _("Debian mirror"),
934                         "FAIrelease"            => _("Debian release"),
936                         "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
937                         ));
938     }
941     /* Updates release dns 
942      *  and reads all classes for the current release, 
943      *  if not already done ($this->cache).
944      */
945     function update_fai_cache($first_call = FALSE)
946     {
947         $force = FALSE;
948         if(!$this->si_active) return; 
949         $start = microtime(TRUE);  
951         if($this->si_fai_action_failed && !isset($_POST['fai_si_retry'])) return;
953         $this->si_fai_action_failed = FALSE;
955         /* Get the list of available servers and their releases. 
956          */
957         if($force || !isset($this->cache['SERVERS'])){
959             $o_queue = new gosaSupportDaemon();
960             $tmp = $o_queue->FAI_get_server();
961             if($o_queue->is_error()){
962                 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
963                 $this->si_fai_action_failed = TRUE;
964                 $this->cache = array();
965                 return;
966             }else{
968                 foreach($tmp as $entry){
969                     $rel = $entry['FAI_RELEASE'];
970                     $this->cache['SERVERS']['auto'][$rel] = $rel;
971                     $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
972                     uksort($this->cache['SERVERS']['auto'], 'strnatcasecmp');
973                     uksort($this->cache['SERVERS'][$entry['SERVER']], 'strnatcasecmp');
974                 }
975             }
976         }
978         /* Ensure that our selection is valid, else we get several PHP warnings 
979            if there is no FAI configuration at all.
980          */
981         if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
982             $this->cache['SERVERS'][$this->FAIdebianMirror][''] ='';
983         }
985         /* Build up arrays, without checks */
986         if(!$first_call){
988             /* Check if the selected mirror is available */
989             if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
990                 $this->FAIdebianMirror = "auto";
991                 $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
992                 trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
993             }
995             /* Check if the selected release is available */
996             if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
998                 if($this->FAIrelease != ""){
999                     trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1000                 }
1001                 $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1002             }
1003         }
1005         /* Get classes for release from cache. 
1006          * Or build cache
1007          */
1008         if($this->FAIdebianMirror == "inherited"){
1009             $release = $this->InheritedFAIrelease;
1010         }else{
1011             $release = $this->FAIrelease;
1012         }
1014         if($force || !isset($this->cache['CLASSES'][$release]) && $release != ""){
1016             /* Get the list of available servers and their releases.
1017              */
1018             $o_queue = new gosaSupportDaemon();
1019             $tmp = $o_queue->FAI_get_classes($release);
1021             $this->cache['CLASSES'][$release] = array();
1022             if($o_queue->is_error()){
1023                 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1024                 $this->si_fai_action_failed = TRUE;
1025                 $this->cache=array();
1026                 return;
1027             }else{
1028                 foreach($tmp as $entry){
1029                     $class = $entry['CLASS'];
1030                     $this->cache['CLASSES'][$release][$class][] = $this->analyse_fai_object($entry); 
1031                 }
1032             }
1034             /* Add object caught from external hook
1035              */
1036             $lines= $this->GetHookElements();
1037             foreach ($lines as $hline){
1038                 $entries= explode(";", $hline);
1039                 $server = $entries['0'];
1040                 $url    = $entries['1'];
1041                 if (!empty($url)){
1043                     /* Split releases */
1044                     if (isset($entries[2])){
1045                         $releases= explode(",", $entries[2]);
1047                         foreach ($releases as $release_data){
1048                             $release_c  = preg_replace('/:.*$/', '', $release_data);
1049                             $sections_c = explode(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1050                             $classes_c  = explode('|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1052                             if($release_c == $release){
1053                                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1054                                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1055                                 foreach ($classes_c as $class){
1056                                     if ($class != ""){
1057                                         $this->cache['CLASSES'][$release_c][$class]= array();
1058                                     }
1059                                 }
1060                             }
1061                         }
1062                     }
1063                 }
1064             }
1065             uksort($this->cache['SERVERS'], 'strnatcasecmp');
1067             /* Only add inherit option, if we are part in an object group
1068              */
1069             if($this->member_of_ogroup){
1070                 $this->cache['SERVERS'] = array_merge(array('inherited' => array()),$this->cache['SERVERS']);
1071             }
1072         }
1074         /* Get list of available kernel for this release 
1075          */
1076         if(!isset($this->cache['KERNELS'])) $this->cache['KERNELS'] = array();
1078         if($force || !isset($this->cache['KERNELS'][$release])){
1079             $o_queue = new gosaSupportDaemon();
1080             $tmp = $o_queue->FAI_get_kernels($release);
1081             $this->cache['KERNELS'][$release] = array();
1082             foreach($this->gotoBootKernels as $name => $default){
1083                 $this->cache['KERNELS'][$release][$name] = $default;
1084             }
1085             foreach($tmp as $kernel){
1086                 if(empty($kernel)) continue;
1087                 $this->cache['KERNELS'][$release][$kernel]=$kernel;
1088             }
1089             ksort($this->cache['KERNELS'][$release]);
1090         }
1091     }
1094     /* This function return an array containing all 
1095      *  invalid classes for the selected server/release
1096      */
1097     function get_invalid_classes($classes)
1098     {
1099         $this->update_fai_cache();
1100         if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1101             $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1102         }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1103             $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1104         }else{
1105             $release_classes = array();
1106         }
1109         /* Detect all classes that are not valid 
1110          *  for the selected release 
1111          */
1112         $NA = array();
1113         foreach($classes as $class){
1114             if(!isset($release_classes[$class])){
1115                 $NA[] = $class;
1116             }
1117         }
1118         return($NA);
1119     }  
1122     /* Get all selectable classes for the ui select box
1123      */
1124     function selectable_classes()
1125     {
1126         $this->update_fai_cache();
1128         if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1129             $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1130         }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1131             $classes = $this->cache['CLASSES'][$this->FAIrelease];
1132         }else{
1133             $classes = array();
1134         }
1136         $Abbr ="";
1137         $ret= array();
1138         foreach($classes as $class_name => $class_types){
1139             if(!in_array($class_name,$this->FAIclass)){
1140                 $Abbr = "";
1141                 foreach($class_types as $type){
1142                     if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1143                         $Abbr .= $type['Abbr']." ";
1144                     }
1145                 }
1146                 $ret[$class_name] = trim($Abbr);
1147             }
1148         }
1149         uksort($ret, 'strnatcasecmp');
1150         return($ret);
1151     }
1154     /* Analyse FAI object and return an array with usefull informations like 
1155      *  FAIobject type.
1156      */
1157     function analyse_fai_object($attr)
1158     {
1159         $tmp = array();
1160         switch($attr['TYPE']){
1162             case 'FAIpackageList':
1163                 $tmp["Type"]= 'FAIpackageList';
1164                 $tmp["Abbr"]= 'Pl';
1165                 break;
1166             case 'FAItemplate': 
1167                 $tmp["Type"]= 'FAItemplate'; 
1168                 $tmp["Abbr"]= 'T'; 
1169                 break;
1170             case 'FAIvariable':
1171                 $tmp["Type"]= 'FAIvariable'; 
1172                 $tmp["Abbr"]= 'V'; 
1173                 break;
1174             case 'FAIscript':
1175                 $tmp["Type"]= 'FAIscript'; 
1176                 $tmp["Abbr"]= 'S'; 
1177                 break;
1178             case 'FAIhook':
1179                 $tmp["Type"]= 'FAIhook'; 
1180                 $tmp["Abbr"]= 'H'; 
1181                 break;
1182             case 'FAIpartitionTable':
1183                 $tmp["Type"]= 'FAIpartitionTable'; 
1184                 $tmp["Abbr"]= 'Pt'; 
1185                 break;
1186             case 'FAIprofile':
1187                 $tmp["Type"]= 'FAIprofile'; 
1188                 $tmp["Abbr"]= 'P'; 
1189                 break;
1190             default: trigger_error("Unknown FAI object type!");;
1191         }
1192         return($tmp);
1193     }
1196     /* Return repository hook output, if possible.
1197      */
1198     function GetHookElements()
1199     {
1200         $ret = array();
1201         $cmd= $this->config->get_cfg_value("servrepository", "repositoryBranchHook");
1202         if(!empty($cmd)){
1203             $res = shell_exec($cmd);
1204             $res2 = trim($res);
1205             if((!$res)){
1206                 msg_dialog::display(_("Configuration error"), msgPool::cmdexecfailed("repositoryBranchHook", $cmd), ERROR_DIALOG);
1207             }elseif(empty($res2)){
1208                 msg_dialog::display(_("Configuration error"), _("'repositoryBranchHook' returned no result!"), ERROR_DIALOG);
1209             }else{
1210                 $tmp = preg_split("/\n/",$res);
1211                 foreach($tmp as $line){
1212                     if(empty($line)) continue;
1213                     $ret[]= $line;
1214                 }
1215             }
1216         }
1217         return($ret);
1218     }
1221     /* This function creates the release name out of a dn 
1222      *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1223      */
1224     function dn_to_release_name($dn)
1225     {
1226         $relevant = preg_replace("/,".preg_quote(get_ou("faiManagement", "faiBaseRDN"), '/').".*$/i","",$dn);
1227         $parts    = array_reverse(explode(",",$relevant));
1228         $str ="";
1229         foreach($parts as $part){
1230             $str .= preg_replace("/^ou=/","",$part)."/";
1231         }
1232         return(preg_replace("/\/$/","",$str)); 
1233     }
1236 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1237 ?>