2d551801a8602efb9925fdc5ebd8c25ed59869c9
1 <?php
2 class workstartup extends plugin
3 {
4 /* CLI vars */
5 var $cli_summary= "Manage terminal startup options";
6 var $cli_description= "Some longer text\nfor help";
7 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* Generic terminal attributes */
10 var $bootmode = "G";
11 var $goLdapServerList = array("default");
12 var $gotoBootKernel = "default";
13 var $gotoKernelParameters = "";
14 var $gotoLdapServer = "";
15 var $gotoModules = array();
16 var $gotoAutoFs = array();
17 var $gotoFilesystem = array();
18 var $gotoTerminalPath = "";
19 var $FAIstatus = "";
20 var $FAIclass = array();
21 var $FAIclasses = array();
22 var $FAIclassInfo = array();
23 var $FAIdebianMirror = array();
26 /* attribute list for save action */
27 var $attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror");
28 var $objectclasses = array("GOhard", "FAIobject");
30 /* Share */
31 var $gotoShares = array();// Currently Share Option
32 var $gotoShare = ""; // currently selected Share Option
33 var $gotoShareSelections= array();// Available Shares for this account in Listbox format
34 var $gotoAvailableShares= array();// Available Shares for this account
36 /* Helper */
37 var $customParameters = "";
38 var $orig_dn = "";
39 var $ignore_account = TRUE;
40 var $FAIdebianMirrors = array();
42 function workstartup ($config, $dn= NULL)
43 {
44 plugin::plugin ($config, $dn);
46 /* Creating a list of valid Mirrors
47 * none will not be saved to ldap.
48 */
49 $this->FAIdebianMirrors = array("none"=>_("none"));
50 $ldap = $this->config->get_ldap_link();
51 $ldap->cd($this->config->current['BASE']);
52 $ldap->search("(objectClass=FAIrepository)",array("FAIdebianMirror"));
54 /* attach all attributes with "index => cn" to avoid multiple entries */
55 while($attr = $ldap->fetch()){
56 if(isset($attr['FAIdebianMirror'])){
57 $this->FAIdebianMirrors[$attr['FAIdebianMirror'][0]]=$attr['FAIdebianMirror'][0];
58 }
59 }
61 /* Get arrays */
62 foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
63 if (isset($this->attrs["$val"]["count"])){
64 for ($i= 0; $i<$this->attrs["count"]; $i++){
65 if (isset($this->attrs["$val"][$i])){
66 array_push($this->$val, $this->attrs["$val"][$i]);
67 }
68 }
69 }
70 sort ($this->$val);
71 $this->$val= array_unique($this->$val);
72 }
74 /* Parse Kernel Parameters to decide what boot mode is enabled */
75 if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
76 $this->bootmode= "G";
77 } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
78 $this->bootmode= "D";
79 } elseif ($this->gotoKernelParameters == "") {
80 $this->bootmode= "G";
81 } else {
82 $this->bootmode= "T";
83 }
84 if (preg_match("/ o /", $this->gotoKernelParameters)){
85 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
86 } else {
87 $this->customParameters= "";
88 }
90 /* Prepare Shares */
91 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
92 unset($this->attrs['gotoShare']['count']);
93 foreach($this->attrs['gotoShare'] as $share){
94 $tmp = $tmp2 = array();
95 $tmp = split("\|",$share);
96 $tmp2['server'] =$tmp[0];
97 $tmp2['name'] =$tmp[1];
98 $tmp2['mountPoint'] =$tmp[2];
99 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
100 }
101 }
103 $this->gotoShareSelections= $config->getShareList(true);
104 $this->gotoAvailableShares= $config->getShareList(false);
105 $tmp2 = array();
107 $ldap = $this->config->get_ldap_link();
108 $ldap->cd($this->config->current['BASE']);
110 /* Search all FAI objects */
111 $ldap->search("(| (objectClass=FAIpackageList)(objectClass=FAItemplate)
112 (objectClass=FAIvariable)(objectClass=FAIscript)(objectClass=FAIhook)(objectClass=FAIprofile)
113 (objectClass=FAIpartitionTable))",array("objectClass","cn"),true);
114 /* Sort all entries, and attach elementtype.
115 * To be able to show the types in the listbox.
116 */
117 while($attr = $ldap->fetch()){
118 $cn = $attr['cn'][0];
119 if(in_array('FAIpackageList',$attr['objectClass'])){
120 $tmp2[$cn]['FAIpackageList']['obj'] = 'FAIpackageList';
121 $tmp2[$cn]['FAIpackageList']['kzl'] = 'Pl';
122 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
123 }
124 if(in_array('FAItemplate',$attr['objectClass'])){
125 $tmp2[$cn]['FAItemplate']['obj'] = 'FAItemplate';
126 $tmp2[$cn]['FAItemplate']['kzl'] = 'T';
127 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
128 }
129 if(in_array('FAIvariable',$attr['objectClass'])){
130 $tmp2[$cn]['FAIvariable']['obj'] = 'FAIvariable';
131 $tmp2[$cn]['FAIvariable']['kzl'] = 'V';
132 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
133 }
134 if(in_array('FAIscript',$attr['objectClass'])){
135 $tmp2[$cn]['FAIscript']['obj'] = 'FAIscript';
136 $tmp2[$cn]['FAIscript']['kzl'] = 'S';
137 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
138 }
139 if(in_array('FAIhook',$attr['objectClass'])){
140 $tmp2[$cn]['FAIhook']['obj'] = 'FAIhook';
141 $tmp2[$cn]['FAIhook']['kzl'] = 'H';
142 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
143 }
144 if(in_array('FAIpartitionTable',$attr['objectClass'])){
145 $tmp2[$cn]['FAIpartitionTable']['obj']= 'FAIpartitionTable';
146 $tmp2[$cn]['FAIpartitionTable']['kzl']= 'Pt';
147 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
148 }
149 if(in_array('FAIprofile',$attr['objectClass'])){
150 $tmp2[$cn]['FAIprofile']['obj']= 'FAIprofile';
151 $tmp2[$cn]['FAIprofile']['kzl']= 'P';
152 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
153 }
154 }
156 $this->FAIclassInfo = $tmp2;
158 if((isset($this->FAIclass))&&(!is_array($this->FAIclass))){
159 $tmp = array();
160 $tmp = split(" ",$this->FAIclass);
161 $tmp2 =array();
163 foreach($tmp as $class){
164 $tmp2[$class] = $class;
165 }
166 $this->FAIclass = $tmp2;
167 }
169 if(!is_array($this->FAIclass)){
170 $this->FAIclass =array();
171 }
173 $this->orig_dn= $this->dn;
174 }
176 /* Create array to display available classes/profiles in a selectbox */
177 function selectFriendlyClasses(){
178 $tmp=array();
179 foreach($this->FAIclasses as $class){
180 $str = "";
181 $skip = false;
182 if(isset($this->FAIclassInfo[$class])){
183 foreach($this->FAIclassInfo[$class] as $objs){
184 $str .= $objs['kzl']." ";
185 }
186 }
187 $tmp[$class] = $class." [".$str."]";
188 }
189 return($tmp);
190 }
192 function check()
193 {
194 $messages = array();
195 /* If there are packages selected, but no mirror show error */
196 if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
197 $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
198 }
199 return($messages);
200 }
202 function execute()
203 {
204 /* Call parent execute */
205 plugin::execute();
207 /* Do we need to flip is_account state? */
208 if (isset($_POST['modify_state'])){
209 $this->is_account= !$this->is_account;
210 }
212 /* Do we represent a valid terminal? */
213 if (!$this->is_account && $this->parent == NULL){
214 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
215 _("This 'dn' has no terminal features.")."</b>";
216 return ($display);
217 }
219 /* Add module */
220 if (isset ($_POST['add_module'])){
221 if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
222 $this->add_list ($this->gotoModules, $_POST['module']);
223 }
224 }
226 /* Delete module */
227 if (isset ($_POST['delete_module'])){
228 if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
229 $this->del_list ($this->gotoModules, $_POST['modules_list']);
230 }
231 }
233 /* FAI class management */
234 if((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))){
235 $found = 0 ;
237 /* If this new class/profile will attach a second partition table
238 * to our list of classes, abort and show a message.
239 */
240 foreach($this->FAIclass as $name){
241 if(isset($this->FAIclassInfo[$name])){
242 foreach($this->FAIclassInfo[$name] as $atr){
243 if($atr['obj'] == "FAIpartitionTable"){
244 $found ++ ;
245 }
246 }
247 }
248 }
250 if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
251 print_red(_("There is already a profile in your selection that contain partition table configurations."));
252 }else{
253 $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
254 }
255 }
257 $sort = false;
258 foreach($_POST as $name => $val){
260 $sort_type = false;
261 if((preg_match("/sort_up/",$name))&&(!$sort)){
262 $sort_type = "sort_up_";
263 }
264 if((preg_match("/sort_down/",$name))&&(!$sort)){
265 $sort_type = "sort_down_";
266 }
268 if(($sort_type)&&(!$sort)){
269 $value = preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name));
270 $sort = true;
272 $last = -1;
273 $change_down = -1;
275 /* Create array with numeric index */
276 $tmp = array();
277 foreach($this->FAIclass as $class){
278 $tmp [] = $class;
279 }
281 /* Walk trough array */
282 foreach($tmp as $key => $faiName){
283 if($faiName == $value){
284 if($sort_type == "sort_up_"){
285 if($last != -1){
286 $change_down= $last;
287 }
288 }else{
289 if(isset($tmp[$key+1])){
290 $change_down = $key;
291 }
292 }
293 }
294 $last = $key;
295 }
297 $tmp2 = array();
298 $skip = false;
300 foreach($tmp as $ky => $vl){
302 if($ky == $change_down){
303 $skip = $vl;
304 }else{
305 $tmp2[$vl] = $vl;
306 }
307 if(($skip != false)&&($ky != $change_down)){
308 $tmp2[$skip] = $skip;
309 $skip =false;
310 }
311 }
312 $this->FAIclass = $tmp2;
313 }
316 if(preg_match("/fai_remove/i",$name)){
317 $value = preg_replace("/_.*$/i","",preg_replace("/fai_remove_/i","",$name));
318 unset($this->FAIclass[$value]);
319 }
320 }
322 /* Delete selected class from our list */
323 if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
324 if(isset($this->FAIclass[$_POST['FAIclassSel']])){
325 unset($this->FAIclass[$_POST['FAIclassSel']]);
326 }
327 }
329 /* Show main page */
330 $smarty= get_smarty();
332 /* In this section server shares will be defined
333 * A user can select one of the given shares and a mount point
334 * and attach this combination to his setup.
335 */
336 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
337 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
339 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
340 * This entry will be, a combination of mountPoint and sharedefinitions
341 */
342 if(isset($_POST['gotoShareAdd'])){
343 /* We assign a share to this user, if we don't know where to mount the share */
344 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
345 print_red(_("You must specify a valid mount point."));
346 }else{
347 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
348 $s_mount = $_POST['gotoShareMountPoint'];
349 /* Preparing the new assignment */
350 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
351 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
352 }
353 }
355 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
356 * If there is no defined share selected, we will abort the deletion without any message
357 */
358 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
359 unset($this->gotoShares[$_POST['gotoShare']]);
360 }
362 $smarty->assign("gotoShares",$this->printOutAssignedShares());
363 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
365 /* Arrays */
366 $smarty->assign("ldapservers", $this->config->data['SERVERS']['LDAP']);
367 $smarty->assign("gotoLdapServer_select", $this->gotoLdapServer);
368 $smarty->assign("gotoLdapServerACL", chkacl($this->acl, "gotoLdapServer"));
369 foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
370 $smarty->assign("$val", $this->$val);
371 }
373 /* Values */
374 foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror") as $val){
375 $smarty->assign($val, $this->$val);
376 $smarty->assign($val."ACL", chkacl($this->acl, $val));
377 }
379 $div = new divSelectBox("WSFAIscriptClasses");
381 $div -> SetHeight("110");
383 $str_up = " <input type='image' src='images/sort_up.png' name='sort_up_%s' value='%s'>";
384 $str_down = " <input type='image' src='images/sort_down.png' name='sort_down_%s' value='%s'>";
385 $str_remove = " <input type='image' src='images/edittrash.png' name='fai_remove_%s' value='%s'>";
386 $str_empty = " <img src='images/empty.png'i width='7'>";
388 $i = 1;
389 foreach($this->FAIclass as $class){
390 if($i==1){
391 $str = $str_empty.$str_down.$str_remove;
392 }elseif($i == count($this->FAIclass)){
393 $str = $str_up.$str_empty.$str_remove;
394 }else{
395 $str = $str_up.$str_down.$str_remove;
396 }
397 $i ++ ;
399 $div->AddEntry(array(
400 array("string"=>$class),
401 array("string"=>preg_replace("/\%s/",$class,$str),"attach"=>"style='width:50px;border-right:none;'")
402 ));
403 }
405 $smarty->assign("FAIScriptlist",$div->DrawList());
406 $smarty->assign("FAIdebianMirrors",$this->FAIdebianMirrors);
407 $smarty->assign("FAIclasses",$this->selectFriendlyClasses());
408 $smarty->assign("FAIclassesKeys",array_flip($this->selectFriendlyClasses()));
409 $smarty->assign("FAIclassKeys",$this->FAIclass);
411 /* Radio button group */
412 if (preg_match("/G/", $this->bootmode)) {
413 $smarty->assign("graphicalbootup", "checked");
414 } else {
415 $smarty->assign("graphicalbootup", "");
416 }
417 if (preg_match("/T/", $this->bootmode)) {
418 $smarty->assign("textbootup", "checked");
419 } else {
420 $smarty->assign("textbootup", "");
421 }
422 if (preg_match("/D/", $this->bootmode)) {
423 $smarty->assign("debugbootup", "checked");
424 } else {
425 $smarty->assign("debugbootup", "");
426 }
428 /* ACL's */
429 foreach (array("gotoKernelParameters", "gotoModules", "gotoFilesystem","FAIclass") as $value){
430 $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
431 }
433 /* Show main page */
434 return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
435 }
437 function remove_from_parent()
438 {
439 /*
440 $ldap= $this->config->get_ldap_link();
441 $ldap->rmdir($this->dn);
442 show_ldap_error($ldap->get_error());
443 $this->handle_post_events("remove");
444 */
447 /* Depending on the baseobject (Ogroup / WS) we
448 * use another set of objectClasses
449 * In case of WS itself, we use "array("GOhard", "FAIobject");"
450 * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
451 */
452 if(isset($this->parent->by_object['ogroup'])){
453 $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
454 }elseif(isset($this->parent->by_object['workgeneric'])){
455 $this->objectclasses = array("GOhard", "FAIobject");
456 }else{
457 print "unknown";
458 exit();
459 }
461 $ldap= $this->config->get_ldap_link();
462 plugin::remove_from_parent();
464 /* Remove additional attributes.*/
465 foreach(array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $attr){
466 $this->attrs [$attr] = array();
467 }
469 $ldap->cd($this->dn);
470 $ldap->modify($this->attrs);
471 show_ldap_error($ldap->get_error());
473 }
476 /* Save data to object */
477 function save_object()
478 {
479 plugin::save_object();
481 /* Save group radio buttons */
482 if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
483 $this->bootmode= $_POST["bootmode"];
484 }
486 /* Save kernel parameters */
487 if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
488 $this->customParameters= $_POST["customParameters"];
489 }
490 }
493 /* Save to LDAP */
494 function save()
495 {
497 /* Depending on the baseobject (Ogroup / WS) we
498 * use another set of objectClasses
499 * In case of WS itself, we use "array("GOhard", "FAIobject");"
500 * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
501 */
502 if(isset($this->parent->by_object['ogroup'])){
503 $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
504 }elseif(isset($this->parent->by_object['workgeneric'])){
505 $this->objectclasses = array("GOhard", "FAIobject");
506 }else{
507 print "unknown";
508 exit();
509 }
511 /* Find proper terminal path for tftp configuration
512 FIXME: This is suboptimal when the default has changed to
513 another location! */
514 if ($this->gotoTerminalPath == "default"){
515 $ldap= $this->config->get_ldap_link();
517 /* Strip relevant part from dn, keep trailing ',' */
518 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
519 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
521 /* Walk from top to base and try to load default values for
522 'gotoTerminalPath'. Abort when an entry is found. */
523 while (TRUE){
524 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
526 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
527 $this->config->current['BASE']);
528 $attrs= $ldap->fetch();
529 if (isset($attrs['gotoTerminalPath'])){
530 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
531 break;
532 }
534 /* Nothing left? */
535 if ($tmp == ""){
536 break;
537 }
538 }
539 }
541 /* Add semi automatic values */
542 // FIXME: LDAP Server may not be set here...
543 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
544 $this->gotoTerminalPath.
545 ",ro,hard,nolock,fg,rsize=8192 ".
546 "ip=::::::dhcp LDAP=".$this->gotoLdapServer;
548 switch ($this->bootmode){
549 case "D":
550 $this->gotoKernelParameters.= " debug";
551 break;
552 case "G":
553 $this->gotoKernelParameters.= " splash=silent";
554 break;
555 }
556 if ($this->customParameters != ""){
557 $this->gotoKernelParameters.= " o ".$this->customParameters;
558 }
560 plugin::save();
562 $str = "";
563 foreach($this->FAIclass as $class){
564 $str .= $class." ";
565 }
566 $this->attrs['FAIclass']= "";
567 $this->attrs['FAIclass']= trim($str);
569 if(empty($this->attrs['FAIclass'])){
570 $this->attrs['FAIclass'] = array();
571 }
573 /* Add missing arrays */
574 foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
575 if (isset ($this->$val) && count ($this->$val) != 0){
577 $this->attrs["$val"]= array_unique($this->$val);
578 }
579 if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
580 }
581 /* Strip out 'default' values */
582 if ($this->attrs['gotoLdapServer'] == "default"){
583 unset ($this->attrs['gotoLdapServer']);
584 }
586 /* if mirror == none stop saving this attribute */
587 if($this->FAIdebianMirror == "none"){
588 $this->FAIdebianMirror = "";
589 }
591 if((count($this->attrs['FAIclass'])==0)&&(empty($this->FAIdebianMirror))){
592 $tmp = array();
593 foreach($this->attrs['objectClass'] as $class){
594 if($class != "FAIobject"){
595 $tmp[] = $class;
596 }
597 }
598 $this->attrs['objectClass'] = $tmp;
599 $this->attrs['FAIclass'] = array();
600 $this->attrs['FAIdebianMirror'] = array();
601 }
603 /* prepare share settings */
604 $tmp = array();
605 foreach($this->gotoShares as $name => $settings){
606 $tmp2= split("\|",$name);
607 $name = $tmp2[0];
608 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
609 }
610 $this->attrs['gotoShare']=$tmp;
612 $ldap= $this->config->get_ldap_link();
613 $ldap->cd($this->dn);
614 $ldap->modify($this->attrs);
615 show_ldap_error($ldap->get_error());
616 $this->handle_post_events("modify");
617 }
619 /* Add value to array, check if unique */
620 function add_list (&$array, $value)
621 {
622 if ($value != ""){
623 $array[]= $value;
624 sort($array);
625 array_unique ($array);
626 }
627 }
630 /* Delete value to array, check if unique */
631 function del_list (&$array, $list)
632 {
633 $tmp= array();
634 foreach ($array as $mod){
635 if (!in_array($mod, $list)){
636 $tmp[]= $mod;
637 }
638 }
639 $array= $tmp;
640 }
642 /* Generate ListBox frindly output for the defined shares
643 * Possibly Add or remove an attribute here,
644 */
645 function printOutAssignedShares()
646 {
647 $a_return = array();
648 if(is_array($this->gotoShares)){
649 foreach($this->gotoShares as $share){
650 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
651 }
652 }
653 return($a_return);
654 }
656 }
658 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
659 ?>