Code

Updated get_ou it receives values from the config registry now.
[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;
34  
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;
48   
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();
171   
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       }
238   
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   }
273   
274   function check()
275   {
276     $messages = array();
277     
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, $_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, $_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[$_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[$_POST['FAIclassesSel']]=$_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[$_POST['FAIclassSel']])){
373             unset($this->FAIclass[$_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",    $this->gotoShareSelections);
395     $smarty->assign("gotoShareSelectionKeys", 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{
405     
406         if(isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
407           $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
408           $s_mount = $_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[$_POST['gotoShare']]);
421     }
423     $smarty->assign("gotoShares",$this->printOutAssignedShares());
424     $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
425     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
426     $smarty->assign("gotoBootKernels",$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[$_POST['ldap_server_to_add']])){
436         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
437         if(!in_array($to_add,$this->gotoLdapServers)){
438           $this->gotoLdapServers[] = $to_add;
439         }
440       }
441     }
442     
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     }
482     
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);
502     $smarty->assign("JS",  session::get('js'));
504     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
505       $smarty->assign("$val", $this->$val);
506     }
508     /* Values */
509     foreach(array("gotoBootKernel","gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
510       $smarty->assign($val, $this->$val);
511     }
513     $smarty->assign("fai_activated",$this->fai_activated);
515     /* Create FAI output */
516     $this->update_fai_cache();
517     $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
518     $smarty->assign("si_active",$this->si_active);
519   
520     if(!$this->si_fai_action_failed && $this->si_active && $this->fai_activated){
522       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
523       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
524       $smarty->assign("FAIrelease"  , $this->FAIrelease);
525       $smarty->assign("FAIclasses"  , $this->selectable_classes());
527       /* Get classes for release from cache.
528        * Or build cache
529        */
530       if($this->FAIdebianMirror == "inherited"){
531         $release = $this->InheritedFAIrelease;
532       }else{
533         $release = $this->FAIrelease;
534       }
536       $smarty->assign("gotoBootKernels",$this->cache['KERNELS'][$release]);
537       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
539       /* Get classes */
540       if($this->FAIdebianMirror == "inherited"){
541         $tmp = $this->InheritedFAIclass;
542       }else{
543         $tmp = $this->FAIclass;
544       }
546       /* Get invalid classes */
547       $invalid = $this->get_invalid_classes($tmp);
548       $data = $lData = array();
550       /* Draw every single entry */
551       $i = 1;
552       if($this->acl_is_readable("FAIclass")){
553         foreach($tmp as $class){
555           /* Mark invalid classes. (Not in selected release)
556            */
557           $marker = "";
558           if(in_array_ics($class,$invalid)){
559             $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
560           }
562           $i ++ ; 
564           /* Get Description tag 
565            *  There may be several FAI objects with the same class name, 
566            *   use the description from FAIprofile, if possible.
567            */  
568           $desc = ""; 
570           $objects = array(
571               "Pl"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png"),
572               "Pt"     => array("IMG"=> "plugins/fai/images/fai_packages.png"),
573               "S"          => array("IMG"=> "plugins/fai/images/fai_script.png"),
574               "V"        => array("IMG"=> "plugins/fai/images/fai_variable.png"),
575               "H"            => array("IMG"=> "plugins/fai/images/fai_hook.png"),
576               "P"         => array("IMG"=> "plugins/fai/images/fai_profile.png"),
577               "T"        => array("IMG"=> "plugins/fai/images/fai_template.png"));
579           if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
580             $desc ="";
581             foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
582               if(isset($types['Abbr'])){
583                 $desc.= image($objects[$types['Abbr']]['IMG']);
584                 if($types['Type'] == "FAIprofile"){
585                   break;
586                 }
587               }
588             }
589           }
590           if(!empty($desc)){
591             $desc = "&nbsp;".trim($desc)."";
592           }        
594           $data[$class] = $class;
595           $lData[$class] = array('data' => array($class,$desc));
596         }  
597       }// END FAI output generation 
598     }// END FAI output generation 
600     $this->faiList->setAcl('rwcdm');
601     $this->faiList->setReorderable($this->FAIdebianMirror != "inherited");
602     $this->faiList->setListData($data,$lData);
603     $this->faiList->update();
605     $smarty->assign("FAIScriptlist", $this->faiList->render());
607     /* Show main page */
608     $smarty->assign("gotoKernelParameters",$this->gotoKernelParameters);
609     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
610   }
613   function remove_from_parent()
614   {
615     $this->handle_post_events("remove");
616     new log("remove","workstation/".get_class($this),$this->dn);
617   }
620   /* Save data to object */
621   function save_object()
622   {
623     $old_mirror  = $this->FAIdebianMirror;
624     plugin::save_object();
626     /* Update release */
627     if($old_mirror != $this->FAIdebianMirror){
628       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
629         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
630       }
631     }
633     if(isset($_POST['WorkstationStarttabPosted'])){
634       if(isset($_POST['gotoLdap_inherit'])){
635         $this->gotoLdap_inherit = TRUE;
636       }else{
637         $this->gotoLdap_inherit = FALSE;
638       }
639     }
640   }
643   /* Save to LDAP */
644   function save()
645   {
647     /* Depending on the baseobject (Ogroup / WS) we
648      *  use another set of objectClasses
649      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
650      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
651      */
652     if(isset($this->parent->by_object['ogroup'])){
653       $this->objectclasses = array("gotoWorkstationTemplate");
654     }elseif(isset($this->parent->by_object['workgeneric'])){
655       $this->objectclasses = array("GOhard");
656     }elseif(isset($this->parent->by_object['servgeneric'])){
657       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
658     }else{
659       msg_dialog::display(_("Fatal error"),
660           "Object Type Configuration is unknown. Please contact the GOsa developers.",
661           FATAL_ERROR_DIALOG);
662       exit();
663     }
665     /* Append FAI class */
666     if($this->fai_activated){
667       $this->objectclasses[]  = "FAIobject";
668     }
670     /* Find proper terminal path for tftp configuration
671        FIXME: This is suboptimal when the default has changed to
672        another location! */
673     if (($this->gotoTerminalPath == "default")){
674       $ldap= $this->config->get_ldap_link();
676       /* Strip relevant part from dn, keep trailing ',' */
677       $tmp= preg_replace("/^cn=[^,]+,".preg_quote(get_ou("termgeneric", "terminalRDN") , '/')."/i", "", $this->dn);
678       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
680       /* Walk from top to base and try to load default values for
681          'gotoTerminalPath'. Abort when an entry is found. */
682       while (TRUE){
683         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
685         $ldap->cat("cn=default,".get_ou("termgeneric", "terminalRDN") .$tmp.
686             $this->config->current['BASE'], array('gotoTerminalPath'));
687         $attrs= $ldap->fetch();
688         if (isset($attrs['gotoTerminalPath'])){
689           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
690           break;
691         }
693         /* Nothing left? */
694         if ($tmp == ""){
695           break;
696         }
697       }
698     }
700     plugin::save();
702     unset( $this->attrs['FAIrelease'] );
703     $str = "";
705     /* Skip FAI attribute handling if not necessary */
706     if($this->fai_activated && !$this->si_fai_action_failed){
707       if($this->FAIdebianMirror == "inherited"){
708         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
709       }else{
710         foreach($this->FAIclass as $class){
711           $str .= $class." ";
712         }
713         $str = trim($str);
714         if(empty($this->attrs['FAIclass'])){
715           $this->attrs['FAIclass'] = array();
716         }else{
717           $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
718         }
719       }
720     }
722     /* Add missing arrays */
723     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
724       if (isset ($this->$val) && count ($this->$val) != 0){
725     
726         $this->attrs["$val"]= array_unique($this->$val);
727       }
728       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
729     }
731     /* Prepare list of ldap servers */
732     $this->attrs['gotoLdapServer'] = array();
733     if(!$this->gotoLdap_inherit){
734       $i = 0;
735       foreach($this->gotoLdapServers as $server){
736         $i ++;
737         $this->attrs['gotoLdapServer'][] = $i.":".$server;
738       }
739     }
741     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
742       $this->attrs['gotoBootKernel']= array();
743     }
745     /* if mirror == none stop saving this attribute */
746     if($this->FAIdebianMirror == "none"){
747       $this->FAIdebianMirror = "";
748     }
749    
750     /* Get FAIstate from object, the generic tab could have changed it during execute */
751     $ldap= $this->config->get_ldap_link();
752     $ldap->cd($this->dn);
755     /* Skip FAI attribute handling if not necessary */
756     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
757       $ldap->cat($this->dn,array("FAIstate"));
758       $checkFAIstate = $ldap->fetch();
760       /* Remove FAI objects if no FAI class is selected */ 
761       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
762         $this->attrs['FAIclass']        = array();
763         $this->attrs['FAIdebianMirror'] = array();
764       }
765     }else{
767       /* Don't touch FAI objects if something went wrong with the si daemon.
768        */
769       if(isset($this->attrs['FAIclass'])) unset($this->attrs['FAIclass']);
770       if(isset($this->attrs['FAIdebianMirror'])) unset($this->attrs['FAIdebianMirror']);
771     }
773     /* prepare share settings */
774     $tmp = array();
775     foreach($this->gotoShares as $name => $settings){
776       $tmp2= explode("|",$name);
777       $name = $tmp2[0];
778       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
779     }
780     $this->attrs['gotoShare']=$tmp;
781     $this->cleanup();
782     $ldap->modify ($this->attrs); 
783     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
785     if (!$ldap->success()){
786       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
787     }
788     $this->handle_post_events("modify");
790     /* Check if LDAP server has changed */
791     if ($this->si_active && (isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent") || $this->gotoLdap_inherit)){
792       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
793       $o_queue = new gosaSupportDaemon();
794       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
795         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
796         $macs = array();
797     
798         /* Get list of macAddresses 
799          */
800         if(isset($this->parent->by_object['ogroup'])){
801         
802           /* If we are an object group, add all member macs 
803            */
804           $p = $this->parent->by_object['ogroup'];
805           foreach($p->memberList as $dn => $obj){
806             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
807               $macs[] = $p->objcache[$dn]['macAddress'];
808             }
809           }
810         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
812           /* We are a workstation. Add current mac.
813            */
814           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
815           if(!empty($mac)){
816             $macs[] = $mac;
817           }          
818         }elseif(isset($this->parent->by_object['servgeneric']->netConfigDNS->macAddress)){
820           /* We are a server. Add current mac.
821            */
822           $mac = $this->parent->by_object['servgeneric']->netConfigDNS->macAddress;
823           if(!empty($mac)){
824             $macs[] = $mac;
825           }          
826         }
828         /* Trigger event for all member objects 
829          */
830         foreach($macs as $mac){
831           $tmp = new $evt['CLASS_NAME']($this->config);
832           $tmp->set_type(TRIGGERED_EVENT);
833           $tmp->add_targets(array($mac));
834           if(!$o_queue->append($tmp)){
835             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
836           }
837         }
838       }
839     }
840   }
843   /* Add value to array, check if unique */
844   function add_list (&$array, $value)
845   {
846     if ($value != ""){
847       $array[]= $value;
848       sort($array);
849       array_unique ($array);
850     }
851   }
854   /* Delete value to array, check if unique */
855   function del_list (&$array, $list)
856   {
857     $tmp= array();
858     foreach ($array as $mod){
859       if (!in_array($mod, $list)){
860         $tmp[]= $mod;
861       }
862     }
863     $array= $tmp;
864   }
866   /* Generate ListBox frindly output for the defined shares
867    * Possibly Add or remove an attribute here,
868    */
869   function printOutAssignedShares()
870   {
871     $a_return = array();
872     if(is_array($this->gotoShares)){
873       foreach($this->gotoShares as $share){
874         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
875       }
876     }
877     return($a_return);
878   }
882   function PrepareForCopyPaste($source)
883   {
884     plugin::PrepareForCopyPaste($source);    
885     $source_o = new workstartup ($this->config, $source['dn']);
886     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
887           "gotoKernelParameters","gotoShares","gotoLdapServers","gotoLdapServerList",
888           "gotoLdap_inherit") as $attr){
889       $this->$attr = $source_o->$attr;
890     }
891   }
893   
894   function array_switch_item($ar,$from,$to)
895   {
896     if(!is_array($ar)){
897       return(false);
898     }
899     if(!isset($ar[$from])){
900       return(false);
901     }
902     if(!isset($ar[$to])){
903       return(false);
904     }
906     $tmp = $ar[$from];
907     $ar[$from] = $ar[$to];    
908     $ar[$to] = $tmp;    
909     return($ar);
910   }
913   /* Return plugin informations for acl handling */ 
914   static function plInfo()
915   {
916     return (array( 
917           "plShortName"   => _("Startup"),
918           "plDescription" => _("System startup"),
919           "plSelfModify"  => FALSE,
920           "plDepends"     => array(),
921           "plPriority"    => 9,
922           "plSection"     => array("administration"),           
923           "plCategory"    => array("workstation","server","ogroups"),
925           "plProvidedAcls"=> array(
926             "gotoLdapServer"        => _("Ldap server"),
927             "gotoBootKernel"        => _("Boot kernel"),
928             "gotoKernelParameters"  => _("Kernel parameter"),
930             "gotoModules"           => _("Kernel modules"),
931             "gotoShare"             => _("Shares"),
933             "FAIclass"              => _("FAI classes"),
934             "FAIdebianMirror"       => _("Debian mirror"),
935             "FAIrelease"            => _("Debian release"),
937             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
938           ));
939   }
942   /* Updates release dns 
943    *  and reads all classes for the current release, 
944    *  if not already done ($this->cache).
945    */
946   function update_fai_cache($first_call = FALSE)
947   {
948     $force = FALSE;
949     if(!$this->si_active) return; 
950     $start = microtime(TRUE);  
952     if($this->si_fai_action_failed && !isset($_POST['fai_si_retry'])) return;
954     $this->si_fai_action_failed = FALSE;
956     /* Get the list of available servers and their releases. 
957      */
958     if($force || !isset($this->cache['SERVERS'])){
960       $o_queue = new gosaSupportDaemon();
961       $tmp = $o_queue->FAI_get_server();
962       if($o_queue->is_error()){
963         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
964         $this->si_fai_action_failed = TRUE;
965         $this->cache = array();
966         return;
967       }else{
969         foreach($tmp as $entry){
970           $rel = $entry['FAI_RELEASE'];
971           $this->cache['SERVERS']['auto'][$rel] = $rel;
972           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
973           uksort($this->cache['SERVERS']['auto'], 'strnatcasecmp');
974           uksort($this->cache['SERVERS'][$entry['SERVER']], 'strnatcasecmp');
975         }
976       }
977     }
979     /* Ensure that our selection is valid, else we get several PHP warnings 
980         if there is no FAI configuration at all.
981      */
982     if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
983       $this->cache['SERVERS'][$this->FAIdebianMirror][''] ='';
984     }
986     /* Build up arrays, without checks */
987     if(!$first_call){
989       /* Check if the selected mirror is available */
990       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
991         $this->FAIdebianMirror = "auto";
992         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
993         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
994       }
996       /* Check if the selected release is available */
997       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
999         if($this->FAIrelease != ""){
1000           trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1001         }
1002         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1003       }
1004     }
1006     /* Get classes for release from cache. 
1007      * Or build cache
1008      */
1009     if($this->FAIdebianMirror == "inherited"){
1010       $release = $this->InheritedFAIrelease;
1011     }else{
1012       $release = $this->FAIrelease;
1013     }
1015     if($force || !isset($this->cache['CLASSES'][$release]) && $release != ""){
1017       /* Get the list of available servers and their releases.
1018        */
1019       $o_queue = new gosaSupportDaemon();
1020       $tmp = $o_queue->FAI_get_classes($release);
1022       $this->cache['CLASSES'][$release] = array();
1023       if($o_queue->is_error()){
1024         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1025         $this->si_fai_action_failed = TRUE;
1026         $this->cache=array();
1027         return;
1028       }else{
1029         foreach($tmp as $entry){
1030           $class = $entry['CLASS'];
1031           $this->cache['CLASSES'][$release][$class][] = $this->analyse_fai_object($entry); 
1032         }
1033       }
1035       /* Add object caught from external hook
1036        */
1037       $lines= $this->GetHookElements();
1038       foreach ($lines as $hline){
1039         $entries= explode(";", $hline);
1040         $server = $entries['0'];
1041         $url    = $entries['1'];
1042         if (!empty($url)){
1044           /* Split releases */
1045           if (isset($entries[2])){
1046             $releases= explode(",", $entries[2]);
1048             foreach ($releases as $release_data){
1049               $release_c  = preg_replace('/:.*$/', '', $release_data);
1050               $sections_c = explode(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1051               $classes_c  = explode('|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1053               if($release_c == $release){
1054                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1055                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1056                 foreach ($classes_c as $class){
1057                   if ($class != ""){
1058                     $this->cache['CLASSES'][$release_c][$class]= array();
1059                   }
1060                 }
1061               }
1062             }
1063           }
1064         }
1065       }
1066       uksort($this->cache['SERVERS'], 'strnatcasecmp');
1068       /* Only add inherit option, if we are part in an object group
1069        */
1070       if($this->member_of_ogroup){
1071         $this->cache['SERVERS'] = array_merge(array('inherited' => array()),$this->cache['SERVERS']);
1072       }
1073     }
1075     /* Get list of available kernel for this release 
1076      */
1077     if(!isset($this->cache['KERNELS'])) $this->cache['KERNELS'] = array();
1079     if($force || !isset($this->cache['KERNELS'][$release])){
1080       $o_queue = new gosaSupportDaemon();
1081       $tmp = $o_queue->FAI_get_kernels($release);
1082       $this->cache['KERNELS'][$release] = array();
1083       foreach($this->gotoBootKernels as $name => $default){
1084         $this->cache['KERNELS'][$release][$name] = $default;
1085       }
1086       foreach($tmp as $kernel){
1087         if(empty($kernel)) continue;
1088         $this->cache['KERNELS'][$release][$kernel]=$kernel;
1089       }
1090       ksort($this->cache['KERNELS'][$release]);
1091     }
1092   }
1095   /* This function return an array containing all 
1096    *  invalid classes for the selected server/release
1097    */
1098   function get_invalid_classes($classes)
1099   {
1100     $this->update_fai_cache();
1101     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1102       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1103     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1104       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1105     }else{
1106       $release_classes = array();
1107     }
1110     /* Detect all classes that are not valid 
1111      *  for the selected release 
1112      */
1113     $NA = array();
1114     foreach($classes as $class){
1115       if(!isset($release_classes[$class])){
1116         $NA[] = $class;
1117       }
1118     }
1119     return($NA);
1120   }  
1122   
1123   /* Get all selectable classes for the ui select box
1124    */
1125   function selectable_classes()
1126   {
1127     $this->update_fai_cache();
1129     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1130       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1131     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1132       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1133     }else{
1134       $classes = array();
1135     }
1137     $Abbr ="";
1138     $ret= array();
1139     foreach($classes as $class_name => $class_types){
1140       if(!in_array($class_name,$this->FAIclass)){
1141         $Abbr = "";
1142         foreach($class_types as $type){
1143           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1144             $Abbr .= $type['Abbr']." ";
1145           }
1146         }
1147         $ret[$class_name] = trim($Abbr);
1148       }
1149     }
1150     uksort($ret, 'strnatcasecmp');
1151     return($ret);
1152   }
1155   /* Analyse FAI object and return an array with usefull informations like 
1156    *  FAIobject type.
1157    */
1158   function analyse_fai_object($attr)
1159   {
1160     $tmp = array();
1161     switch($attr['TYPE']){
1163       case 'FAIpackageList':
1164         $tmp["Type"]= 'FAIpackageList';
1165         $tmp["Abbr"]= 'Pl';
1166         break;
1167       case 'FAItemplate': 
1168         $tmp["Type"]= 'FAItemplate'; 
1169         $tmp["Abbr"]= 'T'; 
1170         break;
1171       case 'FAIvariable':
1172         $tmp["Type"]= 'FAIvariable'; 
1173         $tmp["Abbr"]= 'V'; 
1174         break;
1175       case 'FAIscript':
1176         $tmp["Type"]= 'FAIscript'; 
1177         $tmp["Abbr"]= 'S'; 
1178         break;
1179       case 'FAIhook':
1180         $tmp["Type"]= 'FAIhook'; 
1181         $tmp["Abbr"]= 'H'; 
1182         break;
1183       case 'FAIpartitionTable':
1184         $tmp["Type"]= 'FAIpartitionTable'; 
1185         $tmp["Abbr"]= 'Pt'; 
1186         break;
1187       case 'FAIprofile':
1188         $tmp["Type"]= 'FAIprofile'; 
1189         $tmp["Abbr"]= 'P'; 
1190         break;
1191       default: trigger_error("Unknown FAI object type!");;
1192     }
1193     return($tmp);
1194   }
1197   /* Return repository hook output, if possible.
1198    */
1199   function GetHookElements()
1200   {
1201     $ret = array();
1202     $cmd= $this->config->get_cfg_value("servrepository", "repositoryBranchHook");
1203     if(!empty($cmd)){
1204       $res = shell_exec($cmd);
1205       $res2 = trim($res);
1206       if((!$res)){
1207         msg_dialog::display(_("Configuration error"), msgPool::cmdexecfailed("repositoryBranchHook", $cmd), ERROR_DIALOG);
1208       }elseif(empty($res2)){
1209         msg_dialog::display(_("Configuration error"), _("'repositoryBranchHook' returned no result!"), ERROR_DIALOG);
1210       }else{
1211         $tmp = preg_split("/\n/",$res);
1212         foreach($tmp as $line){
1213           if(empty($line)) continue;
1214           $ret[]= $line;
1215         }
1216       }
1217     }
1218     return($ret);
1219   }
1222   /* This function creates the release name out of a dn 
1223    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1224    */
1225   function dn_to_release_name($dn)
1226   {
1227     $relevant = preg_replace("/,".preg_quote(get_ou("termgeneric", "terminalRDN") , '/').".*$/i","",$dn);
1228     $parts    = array_reverse(explode(",",$relevant));
1229     $str ="";
1230     foreach($parts as $part){
1231       $str .= preg_replace("/^ou=/","",$part)."/";
1232     }
1233     return(preg_replace("/\/$/","",$str)); 
1234   }
1237 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1238 ?>