1 <?php
2 class termstartup 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();
12 var $gotoBootKernel= "";
13 var $gotoKernelParameters= "";
14 var $gotoLdapServer= "";
15 var $gotoModules= array();
16 var $gotoTerminalPath= "";
17 var $gotoBootKernels= array();
19 /* Share */
20 var $gotoShares = array();// Currently Share Option
21 var $gotoShare = ""; // currently selected Share Option
22 var $gotoShareSelections= array();// Available Shares for this account in Listbox format
23 var $gotoAvailableShares= array();// Available Shares for this account
26 /* attribute list for save action */
27 var $attributes= array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters","gotoModules");
28 var $objectclasses= array("GOhard");
30 /* Helper */
31 var $customParameters= "";
32 var $orig_dn= "";
33 var $ignore_account= TRUE;
35 function termstartup ($config, $dn= NULL, $parent= NULL)
36 {
37 plugin::plugin ($config, $dn, $parent);
39 /* Get arrays */
40 foreach (array("gotoModules") as $val){
41 $tmp =array();
42 if (isset($this->attrs[$val]["count"])){
43 for ($i= 0; $i<$this->attrs[$val]["count"]; $i++){
44 if (isset($this->attrs[$val][$i])){
45 $tmp[] = $this->attrs["$val"][$i];
46 }
47 }
48 }
49 $this->$val = $tmp;
50 sort ($this->$val);
51 $this->$val= array_unique($this->$val);
52 }
54 /* Parse Kernel Parameters to decide what boot mode is enabled */
55 if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
56 $this->bootmode= "G";
57 } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
58 $this->bootmode= "D";
59 } elseif ($this->gotoKernelParameters == "") {
60 $this->bootmode= "G";
61 } else {
62 $this->bootmode= "T";
63 }
64 if (preg_match("/ o /", $this->gotoKernelParameters)){
65 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
66 } else {
67 $this->customParameters= "";
68 }
70 /* Prepare Shares */
71 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
72 unset($this->attrs['gotoShare']['count']);
73 foreach($this->attrs['gotoShare'] as $share){
74 $tmp = $tmp2 = array();
75 $tmp = split("\|",$share);
76 $tmp2['server'] =$tmp[0];
77 $tmp2['name'] =$tmp[1];
78 $tmp2['mountPoint'] =$tmp[2];
79 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
80 }
81 }
83 $this->gotoShareSelections= $config->getShareList(true);
84 $this->gotoAvailableShares= $config->getShareList(false);
86 $this->orig_dn= $this->dn;
88 /* Get list of boot kernels */
89 if (isset($this->config->data['TABS'])){
90 $command= search_config($this->config->data['TABS'], get_class($this), "KERNELS");
92 if (!check_command($command)){
93 $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
94 get_class($this));
95 } else {
96 $fh= popen($command, "r");
97 while (!feof($fh)) {
98 $buffer= trim(fgets($fh, 256));
100 if(!empty($buffer)){
102 $name=$value = $buffer;
104 if(preg_match("/:/",$buffer)){
105 $name = preg_replace("/:.*$/","",$buffer);
106 $value= preg_replace("/^.*:/","",$buffer);
107 $this->gotoBootKernels[$name]= $name.":".$value;
108 }else{
109 $this->gotoBootKernels[$name]= $value;
110 }
111 if(empty($this->gotoBootKernel)){
112 $this->gotoBootKernel = $name;
113 }
114 }
115 }
116 pclose($fh);
117 }
119 }
120 $tmp = $this->config->data['SERVERS']['LDAP'];
121 foreach($tmp as $server){
122 $visible = $server;
123 if($server == "default-inherit"){
124 $visible = "["._("inherited")."]";
125 }
126 $this->goLdapServerList[$server] = $visible;
127 }
129 /* Load hardware list */
130 $ldap= $this->config->get_ldap_link();
131 $ldap->cd($this->config->current['BASE']);
132 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))");
133 if ($ldap->count() == 1){
134 $map= array("gotoLdapServer","gotoBootKernel");
135 $attrs= $ldap->fetch();
137 foreach ($map as $name){
138 if (!isset($attrs[$name][0])){
139 continue;
140 }
142 switch ($name){
143 case 'gotoLdapServer':
144 $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
145 break;
146 case 'gotoBootKernel':
147 $this->gotoBootKernels= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->gotoBootKernels);
148 break;
149 }
150 }
151 }
152 }
154 function execute()
155 {
156 /* Call parent execute */
157 plugin::execute();
159 /* Do we need to flip is_account state? */
160 if (isset($_POST['modify_state'])){
162 /* Only change account state if allowed */
163 if($this->is_account && $this->acl == "#all#"){
164 $this->is_account= !$this->is_account;
165 }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
166 $this->is_account= !$this->is_account;
167 }
168 }
170 /* Do we represent a valid terminal? */
171 if (!$this->is_account && $this->parent == NULL){
172 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
173 _("This 'dn' has no terminal features.")."</b>";
174 return ($display);
175 }
177 /* Add module */
178 if (isset ($_POST['add_module'])){
179 if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
180 $this->add_list ($this->gotoModules, $_POST['module']);
181 }
182 }
184 /* Delete module */
185 if (isset ($_POST['delete_module'])){
186 if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
187 $this->gotoModules = $this->del_list($this->gotoModules, $_POST['modules_list']);
188 }
189 }
191 /* Show main page */
192 $smarty= get_smarty();
195 /* In this section server shares will be defined
196 * A user can select one of the given shares and a mount point
197 * and attach this combination to his setup.
198 */
199 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
200 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
201 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
203 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
204 * This entry will be, a combination of mountPoint and sharedefinitions
205 */
206 if(isset($_POST['gotoShareAdd']) && chkacl($this->acl,"gotoShare") == ""){
207 /* We assign a share to this user, if we don't know where to mount the share */
208 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
209 print_red(_("You must specify a valid mount point."));
210 }else{
211 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
212 $s_mount = $_POST['gotoShareMountPoint'];
213 /* Preparing the new assignment */
214 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
215 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
216 }
217 }
219 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
220 * If there is no defined share selected, we will abort the deletion without any message
221 */
222 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && chkacl($this->acl,"gotoShare") == ""){
223 unset($this->gotoShares[$_POST['gotoShare']]);
224 }
226 $smarty->assign("gotoShares",$this->printOutAssignedShares());
227 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
229 /* Create divSelectBox for ldap server selection
230 */
231 $SelectBoxLdapServer = new divSelectBox("LdapServer");
232 $SelectBoxLdapServer->SetHeight(80);
234 /* Set first entry as selected, if $this->gotoLdapServer is empty
235 * or given entry is no longer available ...
236 */
237 $found = false;
238 foreach($this->goLdapServerList as $server => $name){
239 if($this->gotoLdapServer==$server){
240 $found = true;
241 }
242 }
244 /* Add Entries
245 */
246 foreach($this->goLdapServerList as $server => $visible){
247 $use ="";
248 if(($this->gotoLdapServer == $server) || ($found == false)) {
249 $found = true;
250 $use = " checked ";
251 };
253 $use .= chkacl($this->acl,"gotoLdapServer");
255 $SelectBoxLdapServer->AddEntry(
256 array(
257 array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
258 "attach"=>"style='border-left:0px;'"),
259 array("string"=>$visible)
260 ));
261 }
263 $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
265 $smarty->assign("gotoShareACL", chkacl($this->acl, "gotoShareACL"));
266 foreach (array("gotoModules" ) as $val){
267 $smarty->assign("$val", $this->$val);
268 }
270 /* Values */
271 foreach(array("gotoBootKernel", "customParameters") as $val){
272 $smarty->assign($val, $this->$val);
273 $smarty->assign($val."ACL", chkacl($this->acl, $val));
274 }
276 /* Radio button group */
277 if (preg_match("/G/", $this->bootmode)) {
278 $smarty->assign("graphicalbootup", "checked");
279 } else {
280 $smarty->assign("graphicalbootup", "");
281 }
282 if (preg_match("/T/", $this->bootmode)) {
283 $smarty->assign("textbootup", "checked");
284 } else {
285 $smarty->assign("textbootup", "");
286 }
287 if (preg_match("/D/", $this->bootmode)) {
288 $smarty->assign("debugbootup", "checked");
289 } else {
290 $smarty->assign("debugbootup", "");
291 }
293 /* ACL's */
294 foreach (array("gotoKernelParameters", "gotoModules") as $value){
295 $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
296 }
298 /* Show main page */
299 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
300 }
302 function remove_from_parent()
303 {
304 $this->handle_post_events("remove");
305 }
308 /* Save data to object */
309 function save_object()
310 {
311 plugin::save_object();
313 /* Save group radio buttons */
314 if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
315 $this->bootmode= $_POST["bootmode"];
316 }
318 /* Save kernel parameters */
319 if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
320 $this->customParameters= $_POST["customParameters"];
321 }
322 }
325 /* Save to LDAP */
326 function save()
327 {
328 /* Find proper terminal path for tftp configuration
329 FIXME: This is suboptimal when the default has changed to
330 another location! */
331 if ($this->gotoTerminalPath == "default-inherit"){
332 $ldap= $this->config->get_ldap_link();
334 /* Strip relevant part from dn, keep trailing ',' */
335 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
336 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
338 /* Walk from top to base and try to load default values for
339 'gotoTerminalPath'. Abort when an entry is found. */
340 while (TRUE){
341 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
343 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
344 $this->config->current['BASE'], array('gotoTerminalPath'));
345 $attrs= $ldap->fetch();
346 if (isset($attrs['gotoTerminalPath'])){
347 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
348 break;
349 }
351 /* Nothing left? */
352 if ($tmp == ""){
353 break;
354 }
355 }
356 }
358 /* Add semi automatic values */
359 // FIXME: LDAP Server may not be set here...
360 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
361 $this->gotoTerminalPath.
362 ",ro,hard,nolock,fg,rsize=8192 ".
363 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
365 switch ($this->bootmode){
366 case "D":
367 $this->gotoKernelParameters.= " debug";
368 break;
369 case "G":
370 $this->gotoKernelParameters.= " splash=silent";
371 break;
372 }
373 if ($this->customParameters != ""){
374 $this->gotoKernelParameters.= " o ".$this->customParameters;
375 }
377 plugin::save();
379 /* Add missing arrays */
380 foreach (array("gotoModules") as $val){
381 if (isset ($this->$val) && count ($this->$val) != 0){
383 $this->attrs["$val"]= array_unique($this->$val);
384 }else{
385 $this->attrs["$val"]=array();
386 }
387 }
389 /* Strip out 'default' values */
390 foreach(array("gotoBootKernel","gotoLdapServer") as $value){
391 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
392 $this->attrs[$value] = array();
393 }
394 }
396 /* prepare share settings */
397 $tmp = array();
398 foreach($this->gotoShares as $name => $settings){
399 $tmp2 = split("\|",$name);
400 $name = $tmp2[0];
401 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
402 }
403 $this->attrs['gotoShare']=$tmp;
405 /* Write back to ldap */
406 $ldap= $this->config->get_ldap_link();
407 $ldap->cd($this->dn);
408 $this->cleanup();
409 $ldap->modify ($this->attrs);
411 show_ldap_error($ldap->get_error(), _("Saving terminal startup settings failed"));
412 $this->handle_post_events("modify");
413 }
415 /* Add value to array, check if unique */
416 function add_list (&$array, $value)
417 {
418 if ($value != ""){
419 $array[]= $value;
420 sort($array);
421 array_unique ($array);
422 }
423 }
426 /* Delete value to array, check if unique */
427 function del_list ($array, $list)
428 {
429 $tmp= array();
430 foreach ($array as $mod){
431 if (!in_array($mod, $list)){
432 $tmp[]= $mod;
433 }
434 }
435 return($tmp);
436 }
438 /* Generate ListBox frindly output for the defined shares
439 * Possibly Add or remove an attribute here,
440 */
441 function printOutAssignedShares()
442 {
443 $a_return = array();
444 if(is_array($this->gotoShares)){
445 foreach($this->gotoShares as $share){
446 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
447 }
448 }
449 return($a_return);
450 }
453 }
455 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
456 ?>