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