8f9df64942c87a179a9ac0d49a2e227187c312f6
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();
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 $this->FAIdebianMirrors['auto']=_("automatic");
56 while($attr = $ldap->fetch()){
57 if(isset($attr['FAIdebianMirror'])){
58 $this->FAIdebianMirrors[$attr['FAIdebianMirror'][0]]=$attr['FAIdebianMirror'][0];
59 }
60 }
62 /* Get arrays */
63 foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
64 if (isset($this->attrs["$val"]["count"])){
65 for ($i= 0; $i<$this->attrs["count"]; $i++){
66 if (isset($this->attrs["$val"][$i])){
67 array_push($this->$val, $this->attrs["$val"][$i]);
68 }
69 }
70 }
71 sort ($this->$val);
72 $this->$val= array_unique($this->$val);
73 }
75 /* Parse Kernel Parameters to decide what boot mode is enabled */
76 if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
77 $this->bootmode= "G";
78 } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
79 $this->bootmode= "D";
80 } elseif ($this->gotoKernelParameters == "") {
81 $this->bootmode= "G";
82 } else {
83 $this->bootmode= "T";
84 }
85 if (preg_match("/ o /", $this->gotoKernelParameters)){
86 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
87 } else {
88 $this->customParameters= "";
89 }
91 /* Prepare Shares */
92 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
93 unset($this->attrs['gotoShare']['count']);
94 foreach($this->attrs['gotoShare'] as $share){
95 $tmp = $tmp2 = array();
96 $tmp = split("\|",$share);
97 $tmp2['server'] =$tmp[0];
98 $tmp2['name'] =$tmp[1];
99 $tmp2['mountPoint'] =$tmp[2];
100 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
101 }
102 }
104 $this->gotoShareSelections= $config->getShareList(true);
105 $this->gotoAvailableShares= $config->getShareList(false);
106 $tmp2 = array();
108 $ldap = $this->config->get_ldap_link();
109 $ldap->cd($this->config->current['BASE']);
111 /* Search all FAI objects */
112 $ldap->search("(| (objectClass=FAIpackageList)(objectClass=FAItemplate)
113 (objectClass=FAIvariable)(objectClass=FAIscript)(objectClass=FAIhook)(objectClass=FAIprofile)
114 (objectClass=FAIpartitionTable))",array("objectClass","cn"),true);
115 /* Sort all entries, and attach elementtype.
116 * To be able to show the types in the listbox.
117 */
118 while($attr = $ldap->fetch()){
119 $cn = $attr['cn'][0];
120 if(in_array('FAIpackageList',$attr['objectClass'])){
121 $tmp2[$cn]['FAIpackageList']['obj'] = 'FAIpackageList';
122 $tmp2[$cn]['FAIpackageList']['kzl'] = 'Pl';
123 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
124 }
125 if(in_array('FAItemplate',$attr['objectClass'])){
126 $tmp2[$cn]['FAItemplate']['obj'] = 'FAItemplate';
127 $tmp2[$cn]['FAItemplate']['kzl'] = 'T';
128 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
129 }
130 if(in_array('FAIvariable',$attr['objectClass'])){
131 $tmp2[$cn]['FAIvariable']['obj'] = 'FAIvariable';
132 $tmp2[$cn]['FAIvariable']['kzl'] = 'V';
133 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
134 }
135 if(in_array('FAIscript',$attr['objectClass'])){
136 $tmp2[$cn]['FAIscript']['obj'] = 'FAIscript';
137 $tmp2[$cn]['FAIscript']['kzl'] = 'S';
138 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
139 }
140 if(in_array('FAIhook',$attr['objectClass'])){
141 $tmp2[$cn]['FAIhook']['obj'] = 'FAIhook';
142 $tmp2[$cn]['FAIhook']['kzl'] = 'H';
143 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
144 }
145 if(in_array('FAIpartitionTable',$attr['objectClass'])){
146 $tmp2[$cn]['FAIpartitionTable']['obj']= 'FAIpartitionTable';
147 $tmp2[$cn]['FAIpartitionTable']['kzl']= 'Pt';
148 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
149 }
150 if(in_array('FAIprofile',$attr['objectClass'])){
151 $tmp2[$cn]['FAIprofile']['obj']= 'FAIprofile';
152 $tmp2[$cn]['FAIprofile']['kzl']= 'P';
153 $this->FAIclasses[$attr['cn'][0]]=$attr['cn'][0];
154 }
155 }
156 if(is_array($this->FAIclasses)){
157 natcasesort($this->FAIclasses);
158 }
160 $this->FAIclassInfo = $tmp2;
162 if((isset($this->FAIclass))&&(!is_array($this->FAIclass))){
163 $tmp = array();
164 $tmp = split(" ",$this->FAIclass);
165 $tmp2 =array();
167 foreach($tmp as $class){
168 $tmp2[$class] = $class;
169 }
170 $this->FAIclass = $tmp2;
171 }
173 if(!is_array($this->FAIclass)){
174 $this->FAIclass =array();
175 }
177 $this->orig_dn= $this->dn;
178 }
180 /* Create array to display available classes/profiles in a selectbox */
181 function selectFriendlyClasses(){
182 $tmp=array();
183 foreach($this->FAIclasses as $class){
184 $str = "";
185 $skip = false;
186 if(isset($this->FAIclassInfo[$class])){
187 foreach($this->FAIclassInfo[$class] as $objs){
188 $str .= $objs['kzl']." ";
189 }
190 }
191 $tmp[$class] = $class." [".$str."]";
192 }
193 return($tmp);
194 }
196 function check()
197 {
198 $messages = array();
199 /* If there are packages selected, but no mirror show error */
200 if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
201 $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
202 }
203 return($messages);
204 }
206 function execute()
207 {
208 /* Call parent execute */
209 plugin::execute();
211 /* Do we need to flip is_account state? */
212 if (isset($_POST['modify_state'])){
213 $this->is_account= !$this->is_account;
214 }
216 /* Do we represent a valid terminal? */
217 if (!$this->is_account && $this->parent == NULL){
218 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
219 _("This 'dn' has no terminal features.")."</b>";
220 return ($display);
221 }
223 /* Add module */
224 if (isset ($_POST['add_module'])){
225 if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
226 $this->add_list ($this->gotoModules, $_POST['module']);
227 }
228 }
230 /* Delete module */
231 if (isset ($_POST['delete_module'])){
232 if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
233 $this->del_list ($this->gotoModules, $_POST['modules_list']);
234 }
235 }
237 /* FAI class management */
238 if((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))){
239 $found = 0 ;
241 /* If this new class/profile will attach a second partition table
242 * to our list of classes, abort and show a message.
243 */
244 foreach($this->FAIclass as $name){
245 if(isset($this->FAIclassInfo[$name])){
246 foreach($this->FAIclassInfo[$name] as $atr){
247 if($atr['obj'] == "FAIpartitionTable"){
248 $found ++ ;
249 }
250 }
251 }
252 }
254 if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
255 print_red(_("There is already a profile in your selection that contain partition table configurations."));
256 }else{
257 $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
258 }
259 }
261 $sort = false;
262 foreach($_POST as $name => $val){
264 $sort_type = false;
265 if((preg_match("/sort_up/",$name))&&(!$sort)){
266 $sort_type = "sort_up_";
267 }
268 if((preg_match("/sort_down/",$name))&&(!$sort)){
269 $sort_type = "sort_down_";
270 }
272 if(($sort_type)&&(!$sort)){
273 $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name)));
274 $sort = true;
276 $last = -1;
277 $change_down = -1;
279 /* Create array with numeric index */
280 $tmp = array();
281 foreach($this->FAIclass as $class){
282 $tmp [] = $class;
283 }
285 /* Walk trough array */
286 foreach($tmp as $key => $faiName){
287 if($faiName == $value){
288 if($sort_type == "sort_up_"){
289 if($last != -1){
290 $change_down= $last;
291 }
292 }else{
293 if(isset($tmp[$key+1])){
294 $change_down = $key;
295 }
296 }
297 }
298 $last = $key;
299 }
301 $tmp2 = array();
302 $skip = false;
304 foreach($tmp as $ky => $vl){
306 if($ky == $change_down){
307 $skip = $vl;
308 }else{
309 $tmp2[$vl] = $vl;
310 }
311 if(($skip != false)&&($ky != $change_down)){
312 $tmp2[$skip] = $skip;
313 $skip =false;
314 }
315 }
316 $this->FAIclass = $tmp2;
317 }
320 if(preg_match("/fai_remove/i",$name)){
321 $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/fai_remove_/i","",$name)));
322 unset($this->FAIclass[$value]);
323 }
324 }
326 /* Delete selected class from our list */
327 if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
328 if(isset($this->FAIclass[$_POST['FAIclassSel']])){
329 unset($this->FAIclass[$_POST['FAIclassSel']]);
330 }
331 }
333 /* Show main page */
334 $smarty= get_smarty();
336 /* In this section server shares will be defined
337 * A user can select one of the given shares and a mount point
338 * and attach this combination to his setup.
339 */
340 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
341 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
343 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
344 * This entry will be, a combination of mountPoint and sharedefinitions
345 */
346 if(isset($_POST['gotoShareAdd'])){
347 /* We assign a share to this user, if we don't know where to mount the share */
348 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
349 print_red(_("You must specify a valid mount point."));
350 }else{
351 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
352 $s_mount = $_POST['gotoShareMountPoint'];
353 /* Preparing the new assignment */
354 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
355 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
356 }
357 }
359 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
360 * If there is no defined share selected, we will abort the deletion without any message
361 */
362 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
363 unset($this->gotoShares[$_POST['gotoShare']]);
364 }
366 $smarty->assign("gotoShares",$this->printOutAssignedShares());
367 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
369 /* Arrays */
370 $smarty->assign("ldapservers", $this->config->data['SERVERS']['LDAP']);
371 $smarty->assign("gotoLdapServer_select", $this->gotoLdapServer);
372 $smarty->assign("gotoLdapServerACL", chkacl($this->acl, "gotoLdapServer"));
373 foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
374 $smarty->assign("$val", $this->$val);
375 }
377 /* Values */
378 foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror") as $val){
379 $smarty->assign($val, $this->$val);
380 $smarty->assign($val."ACL", chkacl($this->acl, $val));
381 }
383 $div = new divSelectBox("WSFAIscriptClasses");
385 $div -> SetHeight("110");
387 $str_up = " <input type='image' src='images/sort_up.png' name='sort_up_%s' value='%s'>";
388 $str_down = " <input type='image' src='images/sort_down.png' name='sort_down_%s' value='%s'>";
389 $str_remove = " <input type='image' src='images/edittrash.png' name='fai_remove_%s' value='%s'>";
390 $str_empty = " <img src='images/empty.png' alt=\"\" width='7'>";
392 $i = 1;
393 if(is_array($this->FAIclass)){
394 natcasesort($this->FAIclass);
395 }
397 foreach($this->FAIclass as $class){
398 if($i==1){
399 $str = $str_empty.$str_down.$str_remove;
400 }elseif($i == count($this->FAIclass)){
401 $str = $str_up.$str_empty.$str_remove;
402 }else{
403 $str = $str_up.$str_down.$str_remove;
404 }
405 $i ++ ;
407 $div->AddEntry(array(
408 array("string"=>$class),
409 array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
410 ));
411 }
413 $smarty->assign("FAIScriptlist",$div->DrawList());
414 $smarty->assign("FAIdebianMirrors",$this->FAIdebianMirrors);
415 $smarty->assign("FAIclasses",$this->selectFriendlyClasses());
416 $smarty->assign("FAIclassesKeys",array_flip($this->selectFriendlyClasses()));
417 $smarty->assign("FAIclassKeys",$this->FAIclass);
419 /* Radio button group */
420 if (preg_match("/G/", $this->bootmode)) {
421 $smarty->assign("graphicalbootup", "checked");
422 } else {
423 $smarty->assign("graphicalbootup", "");
424 }
425 if (preg_match("/T/", $this->bootmode)) {
426 $smarty->assign("textbootup", "checked");
427 } else {
428 $smarty->assign("textbootup", "");
429 }
430 if (preg_match("/D/", $this->bootmode)) {
431 $smarty->assign("debugbootup", "checked");
432 } else {
433 $smarty->assign("debugbootup", "");
434 }
436 /* ACL's */
437 foreach (array("gotoKernelParameters", "gotoModules", "gotoFilesystem","FAIclass") as $value){
438 $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
439 }
441 /* Show main page */
442 return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
443 }
445 function remove_from_parent()
446 {
447 $this->handle_post_events("remove");
448 }
451 /* Save data to object */
452 function save_object()
453 {
454 plugin::save_object();
456 /* Save group radio buttons */
457 if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
458 $this->bootmode= $_POST["bootmode"];
459 }
461 /* Save kernel parameters */
462 if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
463 $this->customParameters= $_POST["customParameters"];
464 }
465 }
468 /* Save to LDAP */
469 function save()
470 {
472 /* Depending on the baseobject (Ogroup / WS) we
473 * use another set of objectClasses
474 * In case of WS itself, we use "array("GOhard", "FAIobject");"
475 * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
476 */
477 if(isset($this->parent->by_object['ogroup'])){
478 $this->objectclasses = array("gotoWorkstationTemplate","GOhard", "FAIobject");
479 }elseif(isset($this->parent->by_object['workgeneric'])){
480 $this->objectclasses = array("GOhard", "FAIobject");
481 }else{
482 print "unknown";
483 exit();
484 }
486 /* Find proper terminal path for tftp configuration
487 FIXME: This is suboptimal when the default has changed to
488 another location! */
489 if ($this->gotoTerminalPath == "default"){
490 $ldap= $this->config->get_ldap_link();
492 /* Strip relevant part from dn, keep trailing ',' */
493 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
494 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
496 /* Walk from top to base and try to load default values for
497 'gotoTerminalPath'. Abort when an entry is found. */
498 while (TRUE){
499 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
501 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
502 $this->config->current['BASE']);
503 $attrs= $ldap->fetch();
504 if (isset($attrs['gotoTerminalPath'])){
505 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
506 break;
507 }
509 /* Nothing left? */
510 if ($tmp == ""){
511 break;
512 }
513 }
514 }
516 /* Add semi automatic values */
517 // FIXME: LDAP Server may not be set here...
518 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
519 $this->gotoTerminalPath.
520 ",ro,hard,nolock,fg,rsize=8192 ".
521 "ip=::::::dhcp LDAP=".$this->gotoLdapServer;
523 switch ($this->bootmode){
524 case "D":
525 $this->gotoKernelParameters.= " debug";
526 break;
527 case "G":
528 $this->gotoKernelParameters.= " splash=silent";
529 break;
530 }
531 if ($this->customParameters != ""){
532 $this->gotoKernelParameters.= " o ".$this->customParameters;
533 }
535 plugin::save();
537 $str = "";
538 foreach($this->FAIclass as $class){
539 $str .= $class." ";
540 }
541 $this->attrs['FAIclass']= "";
542 $this->attrs['FAIclass']= trim($str);
544 if(empty($this->attrs['FAIclass'])){
545 $this->attrs['FAIclass'] = array();
546 }
548 /* Add missing arrays */
549 foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
550 if (isset ($this->$val) && count ($this->$val) != 0){
552 $this->attrs["$val"]= array_unique($this->$val);
553 }
554 if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
555 }
556 /* Strip out 'default' values */
557 if ($this->attrs['gotoLdapServer'] == "default"){
558 unset ($this->attrs['gotoLdapServer']);
559 }
561 /* if mirror == none stop saving this attribute */
562 if($this->FAIdebianMirror == "none"){
563 $this->FAIdebianMirror = "";
564 }
566 if((count($this->attrs['FAIclass'])==0)&&(empty($this->FAIdebianMirror))){
567 $tmp = array();
568 foreach($this->attrs['objectClass'] as $class){
569 if($class != "FAIobject"){
570 $tmp[] = $class;
571 }
572 }
573 $this->attrs['objectClass'] = $tmp;
574 $this->attrs['FAIclass'] = array();
575 $this->attrs['FAIdebianMirror'] = array();
576 }
578 /* prepare share settings */
579 $tmp = array();
580 foreach($this->gotoShares as $name => $settings){
581 $tmp2= split("\|",$name);
582 $name = $tmp2[0];
583 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
584 }
585 $this->attrs['gotoShare']=$tmp;
587 $ldap= $this->config->get_ldap_link();
588 $ldap->cd($this->dn);
589 $ldap->modify($this->attrs);
590 show_ldap_error($ldap->get_error());
591 $this->handle_post_events("modify");
592 }
594 /* Add value to array, check if unique */
595 function add_list (&$array, $value)
596 {
597 if ($value != ""){
598 $array[]= $value;
599 sort($array);
600 array_unique ($array);
601 }
602 }
605 /* Delete value to array, check if unique */
606 function del_list (&$array, $list)
607 {
608 $tmp= array();
609 foreach ($array as $mod){
610 if (!in_array($mod, $list)){
611 $tmp[]= $mod;
612 }
613 }
614 $array= $tmp;
615 }
617 /* Generate ListBox frindly output for the defined shares
618 * Possibly Add or remove an attribute here,
619 */
620 function printOutAssignedShares()
621 {
622 $a_return = array();
623 if(is_array($this->gotoShares)){
624 foreach($this->gotoShares as $share){
625 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
626 }
627 }
628 return($a_return);
629 }
631 }
633 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
634 ?>