c7bafb9c2beabb8c4980721ea4d3536660662774
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)
36 {
37 plugin::plugin ($config, $dn);
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->goLdapServerList);
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'])){
159 $this->is_account= !$this->is_account;
160 }
162 /* Do we represent a valid terminal? */
163 if (!$this->is_account && $this->parent == NULL){
164 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
165 _("This 'dn' has no terminal features.")."</b>";
166 return ($display);
167 }
169 /* Add module */
170 if (isset ($_POST['add_module'])){
171 if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
172 $this->add_list ($this->gotoModules, $_POST['module']);
173 }
174 }
176 /* Delete module */
177 if (isset ($_POST['delete_module'])){
178 if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
179 $this->del_list ($this->gotoModules, $_POST['modules_list']);
180 }
181 }
183 /* Show main page */
184 $smarty= get_smarty();
187 /* In this section server shares will be defined
188 * A user can select one of the given shares and a mount point
189 * and attach this combination to his setup.
190 */
191 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
192 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
193 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
195 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
196 * This entry will be, a combination of mountPoint and sharedefinitions
197 */
198 if(isset($_POST['gotoShareAdd'])){
199 /* We assign a share to this user, if we don't know where to mount the share */
200 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
201 print_red(_("You must specify a valid mount point."));
202 }else{
203 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
204 $s_mount = $_POST['gotoShareMountPoint'];
205 /* Preparing the new assignment */
206 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
207 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
208 }
209 }
211 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
212 * If there is no defined share selected, we will abort the deletion without any message
213 */
214 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
215 unset($this->gotoShares[$_POST['gotoShare']]);
216 }
218 $smarty->assign("gotoShares",$this->printOutAssignedShares());
219 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
221 /* Create divSelectBox for ldap server selection
222 */
223 $SelectBoxLdapServer = new divSelectBox("LdapServer");
224 $SelectBoxLdapServer->SetHeight(80);
226 /* Set first entry as selected, if $this->gotoLdapServer is empty
227 * or given entry is no longer available ...
228 */
229 $found = false;
230 foreach($this->goLdapServerList as $server => $name){
231 if($this->gotoLdapServer==$server){
232 $found = true;
233 }
234 }
236 /* Add Entries
237 */
238 foreach($this->goLdapServerList as $server => $visible){
239 $use ="";
240 if(($this->gotoLdapServer == $server) || ($found == false)) {
241 $found = true;
242 $use = " checked ";
243 };
245 $SelectBoxLdapServer->AddEntry(
246 array(
247 array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
248 "attach"=>"style='border-left:0px;'"),
249 array("string"=>$visible)
250 ));
251 }
253 $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
255 $smarty->assign("gotoShareACL", chkacl($this->acl, "gotoShareACL"));
256 foreach (array("gotoModules" ) as $val){
257 $smarty->assign("$val", $this->$val);
258 }
260 /* Values */
261 foreach(array("gotoBootKernel", "customParameters") as $val){
262 $smarty->assign($val, $this->$val);
263 $smarty->assign($val."ACL", chkacl($this->acl, $val));
264 }
266 /* Radio button group */
267 if (preg_match("/G/", $this->bootmode)) {
268 $smarty->assign("graphicalbootup", "checked");
269 } else {
270 $smarty->assign("graphicalbootup", "");
271 }
272 if (preg_match("/T/", $this->bootmode)) {
273 $smarty->assign("textbootup", "checked");
274 } else {
275 $smarty->assign("textbootup", "");
276 }
277 if (preg_match("/D/", $this->bootmode)) {
278 $smarty->assign("debugbootup", "checked");
279 } else {
280 $smarty->assign("debugbootup", "");
281 }
283 /* ACL's */
284 foreach (array("gotoKernelParameters", "gotoModules") as $value){
285 $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
286 }
288 /* Show main page */
289 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
290 }
292 function remove_from_parent()
293 {
294 $this->handle_post_events("remove");
295 }
298 /* Save data to object */
299 function save_object()
300 {
301 plugin::save_object();
303 /* Save group radio buttons */
304 if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
305 $this->bootmode= $_POST["bootmode"];
306 }
308 /* Save kernel parameters */
309 if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
310 $this->customParameters= $_POST["customParameters"];
311 }
312 }
315 /* Save to LDAP */
316 function save()
317 {
318 /* Find proper terminal path for tftp configuration
319 FIXME: This is suboptimal when the default has changed to
320 another location! */
321 if ($this->gotoTerminalPath == "default-inherit"){
322 $ldap= $this->config->get_ldap_link();
324 /* Strip relevant part from dn, keep trailing ',' */
325 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
326 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
328 /* Walk from top to base and try to load default values for
329 'gotoTerminalPath'. Abort when an entry is found. */
330 while (TRUE){
331 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
333 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
334 $this->config->current['BASE'], array('gotoTerminalPath'));
335 $attrs= $ldap->fetch();
336 if (isset($attrs['gotoTerminalPath'])){
337 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
338 break;
339 }
341 /* Nothing left? */
342 if ($tmp == ""){
343 break;
344 }
345 }
346 }
348 /* Add semi automatic values */
349 // FIXME: LDAP Server may not be set here...
350 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
351 $this->gotoTerminalPath.
352 ",ro,hard,nolock,fg,rsize=8192 ".
353 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
355 switch ($this->bootmode){
356 case "D":
357 $this->gotoKernelParameters.= " debug";
358 break;
359 case "G":
360 $this->gotoKernelParameters.= " splash=silent";
361 break;
362 }
363 if ($this->customParameters != ""){
364 $this->gotoKernelParameters.= " o ".$this->customParameters;
365 }
367 plugin::save();
369 /* Add missing arrays */
370 foreach (array("gotoModules") as $val){
371 if (isset ($this->$val) && count ($this->$val) != 0){
373 $this->attrs["$val"]= array_unique($this->$val);
374 }
375 if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
376 }
378 /* Strip out 'default' values */
379 foreach(array("gotoBootKernel","gotoLdapServer") as $value){
380 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
381 $this->attrs[$value] = array();
382 }
383 }
385 /* prepare share settings */
386 $tmp = array();
387 foreach($this->gotoShares as $name => $settings){
388 $tmp2 = split("\|",$name);
389 $name = $tmp2[0];
390 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
391 }
392 $this->attrs['gotoShare']=$tmp;
394 /* Write back to ldap */
395 $ldap= $this->config->get_ldap_link();
396 $ldap->cd($this->dn);
397 $this->cleanup();
398 $ldap->modify ($this->attrs);
400 show_ldap_error($ldap->get_error(), _("Saving terminal startup settings failed"));
401 $this->handle_post_events("modify");
402 }
404 /* Add value to array, check if unique */
405 function add_list (&$array, $value)
406 {
407 if ($value != ""){
408 $array[]= $value;
409 sort($array);
410 array_unique ($array);
411 }
412 }
415 /* Delete value to array, check if unique */
416 function del_list (&$array, $list)
417 {
418 $tmp= array();
419 foreach ($array as $mod){
420 if (!in_array($mod, $list)){
421 $tmp[]= $mod;
422 }
423 }
424 $array= $tmp;
425 }
427 /* Generate ListBox frindly output for the defined shares
428 * Possibly Add or remove an attribute here,
429 */
430 function printOutAssignedShares()
431 {
432 $a_return = array();
433 if(is_array($this->gotoShares)){
434 foreach($this->gotoShares as $share){
435 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
436 }
437 }
438 return($a_return);
439 }
442 }
444 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
445 ?>