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 $this->gotoBootKernels = array("default"=>"["._("inherited")."]");
41 /* Get arrays */
42 foreach (array("gotoModules") as $val){
43 if (isset($this->attrs["$val"]["count"])){
44 for ($i= 0; $i<$this->attrs["count"]; $i++){
45 if (isset($this->attrs["$val"][$i])){
46 array_push($this->$val, $this->attrs["$val"][$i]);
47 }
48 }
49 }
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= fgets($fh, 256);
99 if(!empty($buffer)){
100 $this->gotoBootKernels[]= $buffer;
101 }
102 }
103 pclose($fh);
104 }
106 }
107 $tmp = $this->config->data['SERVERS']['LDAP'];
108 foreach($tmp as $server){
109 $visible = $server;
110 if($server == "default"){
111 $visible = "["._("inherited")."]";
112 }
113 $this->goLdapServerList[$server] = $visible;
114 }
116 /* Load hardware list */
117 $ldap= $this->config->get_ldap_link();
118 $ldap->cd($this->config->current['BASE']);
119 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".$this->dn."))");
120 if ($ldap->count() == 1){
121 $map= array("gotoLdapServer");
122 $attrs= $ldap->fetch();
124 foreach ($map as $name){
125 if (!isset($attrs[$name][0])){
126 continue;
127 }
129 switch ($name){
130 case 'gotoLdapServer':
131 $this->goLdapServerList= array_merge(array('default' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
132 break;
133 }
134 }
135 }
136 }
138 function execute()
139 {
140 /* Call parent execute */
141 plugin::execute();
143 /* Do we need to flip is_account state? */
144 if (isset($_POST['modify_state'])){
145 $this->is_account= !$this->is_account;
146 }
148 /* Do we represent a valid terminal? */
149 if (!$this->is_account && $this->parent == NULL){
150 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
151 _("This 'dn' has no terminal features.")."</b>";
152 return ($display);
153 }
155 /* Add module */
156 if (isset ($_POST['add_module'])){
157 if ($_POST['module'] != "" && chkacl ($this->acl, "gotoModule") == ""){
158 $this->add_list ($this->gotoModules, $_POST['module']);
159 }
160 }
162 /* Delete module */
163 if (isset ($_POST['delete_module'])){
164 if (count($_POST['modules_list']) && chkacl ($this->acl, "gotoModule") == ""){
165 $this->del_list ($this->gotoModules, $_POST['modules_list']);
166 }
167 }
169 /* Show main page */
170 $smarty= get_smarty();
173 /* In this section server shares will be defined
174 * A user can select one of the given shares and a mount point
175 * and attach this combination to his setup.
176 */
177 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
178 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
179 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
181 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
182 * This entry will be, a combination of mountPoint and sharedefinitions
183 */
184 if(isset($_POST['gotoShareAdd'])){
185 /* We assign a share to this user, if we don't know where to mount the share */
186 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
187 print_red(_("You must specify a valid mount point."));
188 }else{
189 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
190 $s_mount = $_POST['gotoShareMountPoint'];
191 /* Preparing the new assignment */
192 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
193 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
194 }
195 }
197 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
198 * If there is no defined share selected, we will abort the deletion without any message
199 */
200 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
201 unset($this->gotoShares[$_POST['gotoShare']]);
202 }
204 $smarty->assign("gotoShares",$this->printOutAssignedShares());
205 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
207 /* Create divSelectBox for ldap server selection
208 */
209 $SelectBoxLdapServer = new divSelectBox("LdapServer");
210 $SelectBoxLdapServer->SetHeight(80);
212 /* Set first entry as selected, if $this->gotoLdapServer is empty
213 * or given entry is no longer available ...
214 */
215 $found = false;
216 foreach($this->goLdapServerList as $server => $name){
217 if($this->gotoLdapServer==$server){
218 $found = true;
219 }
220 }
222 /* Add Entries
223 */
224 foreach($this->goLdapServerList as $server => $visible){
225 $use ="";
226 if(($this->gotoLdapServer == $server) || ($found == false)) {
227 $found = true;
228 $use = " checked ";
229 };
231 $SelectBoxLdapServer->AddEntry(
232 array(
233 array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
234 "attach"=>"style='border-left:0px;'"),
235 array("string"=>$visible)
236 ));
237 }
239 $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
241 $smarty->assign("gotoShareACL", chkacl($this->acl, "gotoShareACL"));
242 foreach (array("gotoModules" ) as $val){
243 $smarty->assign("$val", $this->$val);
244 }
246 /* Values */
247 foreach(array("gotoBootKernel", "customParameters") as $val){
248 $smarty->assign($val, $this->$val);
249 $smarty->assign($val."ACL", chkacl($this->acl, $val));
250 }
252 /* Radio button group */
253 if (preg_match("/G/", $this->bootmode)) {
254 $smarty->assign("graphicalbootup", "checked");
255 } else {
256 $smarty->assign("graphicalbootup", "");
257 }
258 if (preg_match("/T/", $this->bootmode)) {
259 $smarty->assign("textbootup", "checked");
260 } else {
261 $smarty->assign("textbootup", "");
262 }
263 if (preg_match("/D/", $this->bootmode)) {
264 $smarty->assign("debugbootup", "checked");
265 } else {
266 $smarty->assign("debugbootup", "");
267 }
269 /* ACL's */
270 foreach (array("gotoKernelParameters", "gotoModules") as $value){
271 $smarty->assign($value."ACL", chkacl($this->acl, "$value"));
272 }
274 /* Show main page */
275 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
276 }
278 function remove_from_parent()
279 {
280 $this->handle_post_events("remove");
281 }
284 /* Save data to object */
285 function save_object()
286 {
287 plugin::save_object();
289 /* Save group radio buttons */
290 if (chkacl ($this->acl, "bootmode") == "" && isset($_POST["bootmode"])){
291 $this->bootmode= $_POST["bootmode"];
292 }
294 /* Save kernel parameters */
295 if (chkacl ($this->acl, "gotoKernelParameters") == "" && isset($_POST["customParameters"])){
296 $this->customParameters= $_POST["customParameters"];
297 }
298 }
301 /* Save to LDAP */
302 function save()
303 {
304 /* Find proper terminal path for tftp configuration
305 FIXME: This is suboptimal when the default has changed to
306 another location! */
307 if ($this->gotoTerminalPath == "default"){
308 $ldap= $this->config->get_ldap_link();
310 /* Strip relevant part from dn, keep trailing ',' */
311 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
312 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
314 /* Walk from top to base and try to load default values for
315 'gotoTerminalPath'. Abort when an entry is found. */
316 while (TRUE){
317 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
319 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
320 $this->config->current['BASE'], array('gotoTerminalPath'));
321 $attrs= $ldap->fetch();
322 if (isset($attrs['gotoTerminalPath'])){
323 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
324 break;
325 }
327 /* Nothing left? */
328 if ($tmp == ""){
329 break;
330 }
331 }
332 }
334 /* Add semi automatic values */
335 // FIXME: LDAP Server may not be set here...
336 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
337 $this->gotoTerminalPath.
338 ",ro,hard,nolock,fg,rsize=8192 ".
339 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
341 switch ($this->bootmode){
342 case "D":
343 $this->gotoKernelParameters.= " debug";
344 break;
345 case "G":
346 $this->gotoKernelParameters.= " splash=silent";
347 break;
348 }
349 if ($this->customParameters != ""){
350 $this->gotoKernelParameters.= " o ".$this->customParameters;
351 }
353 plugin::save();
355 /* Add missing arrays */
356 foreach (array("gotoModules") as $val){
357 if (isset ($this->$val) && count ($this->$val) != 0){
359 $this->attrs["$val"]= array_unique($this->$val);
360 }
361 if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
362 }
364 /* Strip out 'default' values */
365 foreach(array("gotoBootKernel","gotoLdapServer") as $value){
366 if ($this->attrs[$value] == "default"){
367 $this->attrs[$value] = array();
368 }
369 }
371 /* prepare share settings */
372 $tmp = array();
373 foreach($this->gotoShares as $name => $settings){
374 $tmp2 = split("\|",$name);
375 $name = $tmp2[0];
376 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
377 }
378 $this->attrs['gotoShare']=$tmp;
380 /* Write back to ldap */
381 $ldap= $this->config->get_ldap_link();
382 $ldap->cd($this->dn);
383 $this->cleanup();
384 $ldap->modify ($this->attrs);
386 show_ldap_error($ldap->get_error(), _("Saving terminal startup settings failed"));
387 $this->handle_post_events("modify");
388 }
390 /* Add value to array, check if unique */
391 function add_list (&$array, $value)
392 {
393 if ($value != ""){
394 $array[]= $value;
395 sort($array);
396 array_unique ($array);
397 }
398 }
401 /* Delete value to array, check if unique */
402 function del_list (&$array, $list)
403 {
404 $tmp= array();
405 foreach ($array as $mod){
406 if (!in_array($mod, $list)){
407 $tmp[]= $mod;
408 }
409 }
410 $array= $tmp;
411 }
413 /* Generate ListBox frindly output for the defined shares
414 * Possibly Add or remove an attribute here,
415 */
416 function printOutAssignedShares()
417 {
418 $a_return = array();
419 if(is_array($this->gotoShares)){
420 foreach($this->gotoShares as $share){
421 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
422 }
423 }
424 return($a_return);
425 }
428 }
430 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
431 ?>