d5894c839b4ad70c76dade0bf138e2b71928c4e2
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");
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 if (isset($this->attrs["$val"]["count"])){
42 for ($i= 0; $i<$this->attrs["count"]; $i++){
43 if (isset($this->attrs["$val"][$i])){
44 array_push($this->$val, $this->attrs["$val"][$i]);
45 }
46 }
47 }
48 sort ($this->$val);
49 $this->$val= array_unique($this->$val);
50 }
52 /* Parse Kernel Parameters to decide what boot mode is enabled */
53 if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
54 $this->bootmode= "G";
55 } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
56 $this->bootmode= "D";
57 } elseif ($this->gotoKernelParameters == "") {
58 $this->bootmode= "G";
59 } else {
60 $this->bootmode= "T";
61 }
62 if (preg_match("/ o /", $this->gotoKernelParameters)){
63 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
64 } else {
65 $this->customParameters= "";
66 }
68 /* Prepare Shares */
69 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
70 unset($this->attrs['gotoShare']['count']);
71 foreach($this->attrs['gotoShare'] as $share){
72 $tmp = $tmp2 = array();
73 $tmp = split("\|",$share);
74 $tmp2['server'] =$tmp[0];
75 $tmp2['name'] =$tmp[1];
76 $tmp2['mountPoint'] =$tmp[2];
77 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
78 }
79 }
81 $this->gotoShareSelections= $config->getShareList(true);
82 $this->gotoAvailableShares= $config->getShareList(false);
84 $this->orig_dn= $this->dn;
86 /* Get list of boot kernels */
87 if (isset($this->config->data['TABS'])){
88 $command= search_config($this->config->data['TABS'], get_class($this), "KERNELS");
90 if (!check_command($command)){
91 $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
92 get_class($this));
93 } else {
94 $fh= popen($command, "r");
95 while (!feof($fh)) {
96 $buffer= trim(fgets($fh, 256));
98 if(!empty($buffer)){
100 $name=$value = $buffer;
102 if(preg_match("/:/",$buffer)){
103 $name = preg_replace("/:.*$/","",$buffer);
104 $value= preg_replace("/^.*:/","",$buffer);
105 $this->gotoBootKernels[$name]= $name.":".$value;
106 }else{
107 $this->gotoBootKernels[$name]= $value;
108 }
109 if(empty($this->gotoBootKernel)){
110 $this->gotoBootKernel = $name;
111 }
112 }
113 }
114 pclose($fh);
115 }
117 }
118 $tmp = $this->config->data['SERVERS']['LDAP'];
119 foreach($tmp as $server){
120 $visible = $server;
121 if($server == "default-inherit"){
122 $visible = "["._("inherited")."]";
123 }
124 $this->goLdapServerList[$server] = $visible;
125 }
127 /* Load hardware list */
128 $ldap= $this->config->get_ldap_link();
129 $ldap->cd($this->config->current['BASE']);
130 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))");
131 if ($ldap->count() == 1){
132 $map= array("gotoLdapServer","gotoBootKernel");
133 $attrs= $ldap->fetch();
135 foreach ($map as $name){
136 if (!isset($attrs[$name][0])){
137 continue;
138 }
140 switch ($name){
141 case 'gotoLdapServer':
142 $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
143 break;
144 case 'gotoBootKernel':
145 $this->gotoBootKernels= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->gotoBootKernels);
146 break;
147 }
148 }
149 }
150 }
152 function execute()
153 {
154 /* Call parent execute */
155 plugin::execute();
157 /* Do we need to flip is_account state? */
158 if (isset($_POST['modify_state'])){
160 /* Only change account state if allowed */
161 if($this->is_account && $this->acl == "#all#"){
162 $this->is_account= !$this->is_account;
163 }elseif(!$this->is_account && chkacl($this->acl,"create") == ""){
164 $this->is_account= !$this->is_account;
165 }
166 }
168 /* Do we represent a valid terminal? */
169 if (!$this->is_account && $this->parent == NULL){
170 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
171 _("This 'dn' has no terminal features.")."</b>";
172 return ($display);
173 }
175 /* Add module */
176 if (isset ($_POST['add_module'])){
177 if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
178 $this->add_list ($this->gotoModules, $_POST['module']);
179 }
180 }
182 /* Delete module */
183 if (isset ($_POST['delete_module'])){
184 if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
185 $this->del_list ($this->gotoModules, $_POST['modules_list']);
186 }
187 }
189 /* Show main page */
190 $smarty= get_smarty();
193 /* In this section server shares will be defined
194 * A user can select one of the given shares and a mount point
195 * and attach this combination to his setup.
196 */
197 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
198 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
199 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
201 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
202 * This entry will be, a combination of mountPoint and sharedefinitions
203 */
204 if(isset($_POST['gotoShareAdd']) && chkacl($this->acl,"gotoShare") == ""){
205 /* We assign a share to this user, if we don't know where to mount the share */
206 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
207 print_red(_("You must specify a valid mount point."));
208 }else{
209 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
210 $s_mount = $_POST['gotoShareMountPoint'];
211 /* Preparing the new assignment */
212 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
213 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
214 }
215 }
217 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
218 * If there is no defined share selected, we will abort the deletion without any message
219 */
220 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && chkacl($this->acl,"gotoShare") == ""){
221 unset($this->gotoShares[$_POST['gotoShare']]);
222 }
224 $smarty->assign("gotoShares",$this->printOutAssignedShares());
225 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
227 /* Create divSelectBox for ldap server selection
228 */
229 $SelectBoxLdapServer = new divSelectBox("LdapServer");
230 $SelectBoxLdapServer->SetHeight(80);
232 /* Set first entry as selected, if $this->gotoLdapServer is empty
233 * or given entry is no longer available ...
234 */
235 $found = false;
236 foreach($this->goLdapServerList as $server => $name){
237 if($this->gotoLdapServer==$server){
238 $found = true;
239 }
240 }
242 /* Add Entries
243 */
244 foreach($this->goLdapServerList as $server => $visible){
245 $use ="";
246 if(($this->gotoLdapServer == $server) || ($found == false)) {
247 $found = true;
248 $use = " checked ";
249 };
251 if(chkacl($this->acl,"gotoLdapServer") == ""){
252 $use.= " disabled ";
253 }
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 }
385 if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
386 }
388 /* Strip out 'default' values */
389 foreach(array("gotoBootKernel","gotoLdapServer") as $value){
390 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
391 $this->attrs[$value] = array();
392 }
393 }
395 /* prepare share settings */
396 $tmp = array();
397 foreach($this->gotoShares as $name => $settings){
398 $tmp2 = split("\|",$name);
399 $name = $tmp2[0];
400 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
401 }
402 $this->attrs['gotoShare']=$tmp;
404 /* Write back to ldap */
405 $ldap= $this->config->get_ldap_link();
406 $ldap->cd($this->dn);
407 $this->cleanup();
408 $ldap->modify ($this->attrs);
410 show_ldap_error($ldap->get_error(), _("Saving terminal startup settings failed"));
411 $this->handle_post_events("modify");
412 }
414 /* Add value to array, check if unique */
415 function add_list (&$array, $value)
416 {
417 if ($value != ""){
418 $array[]= $value;
419 sort($array);
420 array_unique ($array);
421 }
422 }
425 /* Delete value to array, check if unique */
426 function del_list (&$array, $list)
427 {
428 $tmp= array();
429 foreach ($array as $mod){
430 if (!in_array($mod, $list)){
431 $tmp[]= $mod;
432 }
433 }
434 $array= $tmp;
435 }
437 /* Generate ListBox frindly output for the defined shares
438 * Possibly Add or remove an attribute here,
439 */
440 function printOutAssignedShares()
441 {
442 $a_return = array();
443 if(is_array($this->gotoShares)){
444 foreach($this->gotoShares as $share){
445 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
446 }
447 }
448 return($a_return);
449 }
452 }
454 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
455 ?>