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