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 $use .= chkacl($this->acl,"gotoLdapServer");
253 $SelectBoxLdapServer->AddEntry(
254 array(
255 array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
256 "attach"=>"style='border-left:0px;'"),
257 array("string"=>$visible)
258 ));
259 }
261 $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
263 $smarty->assign("gotoShareACL", chkacl($this->acl, "gotoShareACL"));
264 foreach (array("gotoModules" ) as $val){
265 $smarty->assign("$val", $this->$val);
266 }
268 /* Values */
269 foreach(array("gotoBootKernel", "customParameters") as $val){
270 $smarty->assign($val, $this->$val);
271 $smarty->assign($val."ACL", chkacl($this->acl, $val));
272 }
274 /* Radio button group */
275 if (preg_match("/G/", $this->bootmode)) {
276 $smarty->assign("graphicalbootup", "checked");
277 } else {
278 $smarty->assign("graphicalbootup", "");
279 }
280 if (preg_match("/T/", $this->bootmode)) {
281 $smarty->assign("textbootup", "checked");
282 } else {
283 $smarty->assign("textbootup", "");
284 }
285 if (preg_match("/D/", $this->bootmode)) {
286 $smarty->assign("debugbootup", "checked");
287 } else {
288 $smarty->assign("debugbootup", "");
289 }
291 /* ACL's */
292 foreach (array("gotoKernelParameters", "gotoModules") as $value){
293 $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
294 }
296 /* Show main page */
297 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
298 }
300 function remove_from_parent()
301 {
302 $this->handle_post_events("remove");
303 }
306 /* Save data to object */
307 function save_object()
308 {
309 plugin::save_object();
311 /* Save group radio buttons */
312 if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
313 $this->bootmode= $_POST["bootmode"];
314 }
316 /* Save kernel parameters */
317 if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
318 $this->customParameters= $_POST["customParameters"];
319 }
320 }
323 /* Save to LDAP */
324 function save()
325 {
326 /* Find proper terminal path for tftp configuration
327 FIXME: This is suboptimal when the default has changed to
328 another location! */
329 if ($this->gotoTerminalPath == "default-inherit"){
330 $ldap= $this->config->get_ldap_link();
332 /* Strip relevant part from dn, keep trailing ',' */
333 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
334 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
336 /* Walk from top to base and try to load default values for
337 'gotoTerminalPath'. Abort when an entry is found. */
338 while (TRUE){
339 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
341 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
342 $this->config->current['BASE'], array('gotoTerminalPath'));
343 $attrs= $ldap->fetch();
344 if (isset($attrs['gotoTerminalPath'])){
345 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
346 break;
347 }
349 /* Nothing left? */
350 if ($tmp == ""){
351 break;
352 }
353 }
354 }
356 /* Add semi automatic values */
357 // FIXME: LDAP Server may not be set here...
358 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
359 $this->gotoTerminalPath.
360 ",ro,hard,nolock,fg,rsize=8192 ".
361 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
363 switch ($this->bootmode){
364 case "D":
365 $this->gotoKernelParameters.= " debug";
366 break;
367 case "G":
368 $this->gotoKernelParameters.= " splash=silent";
369 break;
370 }
371 if ($this->customParameters != ""){
372 $this->gotoKernelParameters.= " o ".$this->customParameters;
373 }
375 plugin::save();
377 /* Add missing arrays */
378 foreach (array("gotoModules") as $val){
379 if (isset ($this->$val) && count ($this->$val) != 0){
381 $this->attrs["$val"]= array_unique($this->$val);
382 }
383 if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
384 }
386 /* Strip out 'default' values */
387 foreach(array("gotoBootKernel","gotoLdapServer") as $value){
388 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
389 $this->attrs[$value] = array();
390 }
391 }
393 /* prepare share settings */
394 $tmp = array();
395 foreach($this->gotoShares as $name => $settings){
396 $tmp2 = split("\|",$name);
397 $name = $tmp2[0];
398 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
399 }
400 $this->attrs['gotoShare']=$tmp;
402 /* Write back to ldap */
403 $ldap= $this->config->get_ldap_link();
404 $ldap->cd($this->dn);
405 $this->cleanup();
406 $ldap->modify ($this->attrs);
408 show_ldap_error($ldap->get_error(), _("Saving terminal startup settings failed"));
409 $this->handle_post_events("modify");
410 }
412 /* Add value to array, check if unique */
413 function add_list (&$array, $value)
414 {
415 if ($value != ""){
416 $array[]= $value;
417 sort($array);
418 array_unique ($array);
419 }
420 }
423 /* Delete value to array, check if unique */
424 function del_list (&$array, $list)
425 {
426 $tmp= array();
427 foreach ($array as $mod){
428 if (!in_array($mod, $list)){
429 $tmp[]= $mod;
430 }
431 }
432 $array= $tmp;
433 }
435 /* Generate ListBox frindly output for the defined shares
436 * Possibly Add or remove an attribute here,
437 */
438 function printOutAssignedShares()
439 {
440 $a_return = array();
441 if(is_array($this->gotoShares)){
442 foreach($this->gotoShares as $share){
443 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
444 }
445 }
446 return($a_return);
447 }
450 }
452 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
453 ?>