dfae3014777783a594ef3e81bd8ba5dee3f04ce4
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= "default-inherit";
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","gotoModules");
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 $this->gotoBootKernels = array("default-inherit"=>"["._("inherited")."]");
41 /* Get arrays */
42 foreach (array("gotoModules") as $val){
43 $this->$val = array();
44 if (isset($this->attrs["$val"]["count"])){
45 for ($i= 0; $i<$this->attrs["count"]; $i++){
46 if (isset($this->attrs["$val"][$i])){
47 array_push($this->$val, $this->attrs["$val"][$i]);
48 }
49 }
50 }
51 sort ($this->$val);
52 $this->$val= array_unique($this->$val);
53 }
55 /* Parse Kernel Parameters to decide what boot mode is enabled */
56 if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
57 $this->bootmode= "G";
58 } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
59 $this->bootmode= "D";
60 } elseif ($this->gotoKernelParameters == "") {
61 $this->bootmode= "G";
62 } else {
63 $this->bootmode= "T";
64 }
65 if (preg_match("/ o /", $this->gotoKernelParameters)){
66 $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
67 } else {
68 $this->customParameters= "";
69 }
71 /* Prepare Shares */
72 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
73 unset($this->attrs['gotoShare']['count']);
74 foreach($this->attrs['gotoShare'] as $share){
75 $tmp = $tmp2 = array();
76 $tmp = split("\|",$share);
77 $tmp2['server'] =$tmp[0];
78 $tmp2['name'] =$tmp[1];
79 $tmp2['mountPoint'] =$tmp[2];
80 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
81 }
82 }
84 $this->gotoShareSelections= $config->getShareList(true);
85 $this->gotoAvailableShares= $config->getShareList(false);
87 $this->orig_dn= $this->dn;
89 /* Get list of boot kernels */
90 if (isset($this->config->data['TABS'])){
91 $command= search_config($this->config->data['TABS'], get_class($this), "KERNELS");
93 if (!check_command($command)){
94 $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
95 get_class($this));
96 } else {
97 $fh= popen($command, "r");
98 while (!feof($fh)) {
99 $buffer= trim(fgets($fh, 256));
101 if(!empty($buffer)){
103 $name=$value = $buffer;
105 if(preg_match("/:/",$buffer)){
106 $name = preg_replace("/:.*$/","",$buffer);
107 $value= preg_replace("/^.*:/","",$buffer);
108 $this->gotoBootKernels[$name]= $name.":".$value;
109 }else{
110 $this->gotoBootKernels[$name]= $value;
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");
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 }
145 }
146 }
148 if($this->is_account){
149 @log::log("view","terminal/".get_class($this),$this->dn);
150 }
151 }
153 function execute()
154 {
155 /* Call parent execute */
156 plugin::execute();
158 /* Do we need to flip is_account state? */
159 if (isset($_POST['modify_state'])){
160 $this->is_account= !$this->is_account;
161 }
163 /* Do we represent a valid terminal? */
164 if (!$this->is_account && $this->parent == NULL){
165 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
166 _("This 'dn' has no terminal features.")."</b>";
167 return ($display);
168 }
170 /* Add module */
171 if (isset ($_POST['add_module'])){
172 if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
173 $this->add_list ($this->gotoModules, $_POST['module']);
174 }
175 }
177 /* Delete module */
178 if (isset ($_POST['delete_module'])){
179 if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
180 $this->del_list ($this->gotoModules, $_POST['modules_list']);
181 }
182 }
184 /* Show main page */
185 $smarty= get_smarty();
187 /* Assign acls */
188 $tmp = $this->plInfo();
189 foreach($tmp['plProvidedAcls'] as $name => $translation){
190 $smarty->assign($name."ACL",$this->getacl($name));
191 }
194 /* In this section server shares will be defined
195 * A user can select one of the given shares and a mount point
196 * and attach this combination to his setup.
197 */
198 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
199 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
200 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
202 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
203 * This entry will be, a combination of mountPoint and sharedefinitions
204 */
205 if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
206 /* We assign a share to this user, if we don't know where to mount the share */
207 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
208 print_red(_("You must specify a valid mount point."));
209 }else{
210 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
211 $s_mount = $_POST['gotoShareMountPoint'];
212 /* Preparing the new assignment */
213 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
214 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
215 }
216 }
218 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
219 * If there is no defined share selected, we will abort the deletion without any message
220 */
221 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
222 unset($this->gotoShares[$_POST['gotoShare']]);
223 }
225 $smarty->assign("gotoShares",$this->printOutAssignedShares());
226 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
228 /* Create divSelectBox for ldap server selection
229 */
230 $SelectBoxLdapServer = new divSelectBox("LdapServer");
231 $SelectBoxLdapServer->SetHeight(80);
233 /* Set first entry as selected, if $this->gotoLdapServer is empty
234 * or given entry is no longer available ...
235 */
236 $found = false;
237 foreach($this->goLdapServerList as $server => $name){
238 if($this->gotoLdapServer==$server){
239 $found = true;
240 }
241 }
243 /* Add Entries
244 */
245 foreach($this->goLdapServerList as $server => $visible){
246 $use ="";
247 if(($this->gotoLdapServer == $server) || ($found == false)) {
248 $found = true;
249 $use = " checked ";
250 };
252 $SelectBoxLdapServer->AddEntry(
253 array(
254 array("string"=>"<input type='radio' name='gotoLdapServer' value='".$server."' ".$use.">",
255 "attach"=>"style='border-left:0px;'"),
256 array("string"=>$visible)
257 ));
258 }
260 $smarty->assign("SelectBoxLdapServer",$SelectBoxLdapServer->DrawList());
262 foreach (array("gotoModules" ) as $val){
263 $smarty->assign("$val", $this->$val);
264 }
266 /* Values */
267 foreach(array("gotoBootKernel", "customParameters") as $val){
268 $smarty->assign($val, $this->$val);
269 }
271 /* Radio button group */
272 if (preg_match("/G/", $this->bootmode)) {
273 $smarty->assign("graphicalbootup", "checked");
274 } else {
275 $smarty->assign("graphicalbootup", "");
276 }
277 if (preg_match("/T/", $this->bootmode)) {
278 $smarty->assign("textbootup", "checked");
279 } else {
280 $smarty->assign("textbootup", "");
281 }
282 if (preg_match("/D/", $this->bootmode)) {
283 $smarty->assign("debugbootup", "checked");
284 } else {
285 $smarty->assign("debugbootup", "");
286 }
288 /* Show main page */
289 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE)));
290 }
292 function remove_from_parent()
293 {
294 if($this->acl_is_removeable()){
295 $this->handle_post_events("remove");
296 @log::log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
297 }
298 }
301 /* Save data to object */
302 function save_object()
303 {
304 plugin::save_object();
306 /* Save group radio buttons */
307 if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
308 $this->bootmode= $_POST["bootmode"];
309 }
311 /* Save kernel parameters */
312 if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
313 $this->customParameters= $_POST["customParameters"];
314 }
315 }
318 /* Save to LDAP */
319 function save()
320 {
321 /* Find proper terminal path for tftp configuration
322 FIXME: This is suboptimal when the default has changed to
323 another location! */
324 if ($this->gotoTerminalPath == "default-inherit"){
325 $ldap= $this->config->get_ldap_link();
327 /* Strip relevant part from dn, keep trailing ',' */
328 $tmp= preg_replace("/^cn=[^,]+,ou=terminals,ou=systems,/i", "", $this->dn);
329 $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
331 /* Walk from top to base and try to load default values for
332 'gotoTerminalPath'. Abort when an entry is found. */
333 while (TRUE){
334 $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
336 $ldap->cat("cn=default,ou=terminals,ou=systems,$tmp".
337 $this->config->current['BASE'], array('gotoTerminalPath'));
338 $attrs= $ldap->fetch();
339 if (isset($attrs['gotoTerminalPath'])){
340 $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
341 break;
342 }
344 /* Nothing left? */
345 if ($tmp == ""){
346 break;
347 }
348 }
349 }
351 /* Add semi automatic values */
352 // FIXME: LDAP Server may not be set here...
353 $this->gotoKernelParameters= "root=/dev/nfs nfsroot=".
354 $this->gotoTerminalPath.
355 ",ro,hard,nolock,fg,rsize=8192 ".
356 "ip=::::::dhcp ldap=".base64_encode($this->gotoLdapServer);
358 switch ($this->bootmode){
359 case "D":
360 $this->gotoKernelParameters.= " debug";
361 break;
362 case "G":
363 $this->gotoKernelParameters.= " splash=silent";
364 break;
365 }
366 if ($this->customParameters != ""){
367 $this->gotoKernelParameters.= " o ".$this->customParameters;
368 }
370 plugin::save();
372 /* Add missing arrays */
373 foreach (array("gotoModules") as $val){
374 if (isset ($this->$val) && count ($this->$val) != 0){
376 $this->attrs["$val"]= array_unique($this->$val);
377 }
378 if(!isset($this->attrs["$val"])){
379 $this->attrs["$val"]=array();
380 }
381 }
383 /* Strip out 'default' values */
384 foreach(array("gotoBootKernel","gotoLdapServer") as $value){
385 if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default-inherit"){
386 $this->attrs[$value] = array();
387 }
388 }
390 /* prepare share settings */
391 $tmp = array();
392 foreach($this->gotoShares as $name => $settings){
393 $tmp2 = split("\|",$name);
394 $name = $tmp2[0];
395 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
396 }
397 $this->attrs['gotoShare']=$tmp;
399 /* Write back to ldap */
400 $ldap= $this->config->get_ldap_link();
401 $ldap->cd($this->dn);
403 $this->cleanup();
404 $ldap->modify ($this->attrs);
406 @log::log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
408 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system terminal/startup with dn '%s' failed."),$this->dn));
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 }
449 /* Return plugin informations for acl handling */
450 function plInfo()
451 {
452 return (array(
453 "plShortName" => _("Startup"),
454 "plDescription" => _("Terminal startup"),
455 "plSelfModify" => FALSE,
456 "plDepends" => array(),
457 "plPriority" => 5,
458 "plSection" => array("administration"),
459 "plCategory" => array("terminal"),
461 "plProvidedAcls"=> array(
462 "gotoLdapServer" => _("Ldap server"),
463 "gotoShare" => _("Shares"),
464 "gotoModules" => _("Kernel modules"),
465 "gotoBootKernel" => _("Boot kernel"),
466 "gotoKernelParameters"=> _("Kernel parameter"))
467 ));
468 }
471 }
473 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
474 ?>