1 <?php
2 class termstartup extends plugin
3 {
4 /* Generic terminal attributes */
5 var $goLdapServerList= array();
6 var $gotoBootKernel= "default-inherited";
7 var $gotoKernelParameters= "";
8 var $gotoLdapServer= "default-inherited";
9 var $gotoModules= array();
10 var $gotoTerminalPath= "";
11 var $gotoBootKernels= array();
13 /* Ldap server list */
14 var $gotoLdapServers = array();
15 var $gotoLdapServerList = array();
16 var $gotoLdap_inherit = FALSE;
18 /* Share */
19 var $gotoShares = array();// Currently Share Option
20 var $gotoShare = ""; // currently selected Share Option
21 var $gotoShareSelections= array();// Available Shares for this account in Listbox format
22 var $gotoAvailableShares= array();// Available Shares for this account
25 /* attribute list for save action */
26 var $attributes= array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters","gotoModules");
27 var $objectclasses= array("GOhard");
28 var $view_logged = FALSE;
29 var $member_of_ogroup = FALSE;
30 var $o_group_dn = "";
32 /* Helper */
33 var $orig_dn= "";
34 var $ignore_account= TRUE;
36 function termstartup (&$config, $dn= NULL, $parent= NULL)
37 {
38 plugin::plugin ($config, $dn, $parent);
40 $this->gotoBootKernels = array("default"=> ("default"));
42 /* Get arrays */
43 foreach (array("gotoModules") as $val){
44 $this->$val = array();
45 if (isset($this->attrs["$val"]["count"])){
46 for ($i= 0; $i<$this->attrs["count"]; $i++){
47 if (isset($this->attrs["$val"][$i])){
48 array_push($this->$val, $this->attrs["$val"][$i]);
49 }
50 }
51 }
52 sort ($this->$val);
53 $this->$val= array_unique($this->$val);
54 }
56 /* Prepare Shares */
57 if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
58 unset($this->attrs['gotoShare']['count']);
59 foreach($this->attrs['gotoShare'] as $share){
60 $tmp = $tmp2 = array();
61 $tmp = explode("|",$share);
62 $tmp2['server'] =$tmp[0];
63 $tmp2['name'] =$tmp[1];
64 $tmp2['mountPoint'] =$tmp[2];
65 $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
66 }
67 }
69 $this->gotoShareSelections= $config->getShareList(true);
70 $this->gotoAvailableShares= $config->getShareList(false);
72 $this->orig_dn= $this->dn;
74 /* Creating a list of valid Mirrors
75 * none will not be saved to ldap.
76 */
77 $ldap = $this->config->get_ldap_link();
78 $ldap->cd($this->config->current['BASE']);
79 $ui = get_userinfo();
80 foreach($this->config->data['SERVERS']['LDAP'] as $dn => $data){
81 if($ui->get_category_permissions($data['dn'],"server",TRUE)){
82 for($i = 0; $i < $data['goLdapBase']['count']; $i ++){
83 $name = $data["cn"][0].":".$data["goLdapBase"][$i];
84 $this->gotoLdapServerList[]= $name;
85 }
86 }
87 }
89 if(isset($this->attrs['gotoLdapServer'])){
90 unset($this->attrs['gotoLdapServer']['count']);
91 sort($this->attrs['gotoLdapServer']);
92 foreach($this->attrs['gotoLdapServer'] as $value){
93 $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
94 }
95 }
96 natcasesort($this->gotoLdapServerList);
98 /* Load hardware list */
99 $ldap= $this->config->get_ldap_link();
100 $ldap->cd($this->config->current['BASE']);
101 $ldap->search("(&(|(objectClass=gotoTerminalTemplate)(objectClass=gotoWorkstationTemplate))(member=".LDAP::prepare4filter($this->dn)."))");
102 if ($ldap->count() == 1){
103 $map= array("gotoLdapServer","gotoBootKernel");
104 $attrs= $ldap->fetch();
105 $this->member_of_ogroup = TRUE;
106 $this->o_group_dn = $attrs['dn'];
108 foreach ($map as $name){
109 if (!isset($attrs[$name][0])){
110 continue;
111 }
112 switch ($name){
113 case 'gotoBootKernel':
114 /* Handle inheritance value "default" */
115 if ($this->member_of_ogroup){
116 $this->gotoBootKernels["default-inherited"]= _("inherited")." [".$attrs['gotoBootKernel'][0]."]";
117 }
118 break;
119 case 'gotoLdapServer':
120 $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
121 break;
122 }
123 }
124 }
126 if(!count($this->gotoLdapServers) && $this->member_of_ogroup){
127 $this->gotoLdap_inherit = TRUE;
128 }
130 /* Get list of boot kernels */
131 if (isset($this->config->data['TABS'])){
132 $command= $this->config->search(get_class($this), "systemKernelsHook",array('tabs'));
134 if (!check_command($command)){
135 $message[]= sprintf(_("Command '%s', specified as systemKernelsHook for plugin '%s' doesn't seem to exist."), $command,
136 get_class($this));
137 } else {
138 $fh= popen($command, "r");
139 while (!feof($fh)) {
140 $buffer= trim(fgets($fh, 256));
142 if(!empty($buffer)){
144 $name=$value = $buffer;
146 if(preg_match("/:/",$buffer)){
147 $name = preg_replace("/:.*$/","",$buffer);
148 $value= preg_replace("/^.*:/","",$buffer);
149 $this->gotoBootKernels[$name]= $name.":".$value;
150 }else{
151 $this->gotoBootKernels[$name]= $value;
152 }
153 }
154 }
155 pclose($fh);
156 }
158 }
160 }
162 function execute()
163 {
164 /* Call parent execute */
165 plugin::execute();
167 if($this->is_account && !$this->view_logged){
168 $this->view_logged = TRUE;
169 new log("view","terminal/".get_class($this),$this->dn);
170 }
172 /* Do we need to flip is_account state? */
173 if (isset($_POST['modify_state'])){
174 $this->is_account= !$this->is_account;
175 }
177 /* Do we represent a valid terminal? */
178 if (!$this->is_account && $this->parent === NULL){
179 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
180 msgPool::noValidExtension(_("terminal"))."</b>";
181 return ($display);
182 }
184 /* Add module */
185 if (isset ($_POST['add_module'])){
186 if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
187 $this->add_list ($this->gotoModules, $_POST['module']);
188 }
189 }
191 /* Delete module */
192 if (isset ($_POST['delete_module'])){
193 if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
194 $this->del_list ($this->gotoModules, $_POST['modules_list']);
195 }
196 }
198 /* Show main page */
199 $smarty= get_smarty();
201 /* Assign acls */
202 $tmp = $this->plInfo();
203 foreach($tmp['plProvidedAcls'] as $name => $translation){
204 $smarty->assign($name."ACL",$this->getacl($name));
205 }
207 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
209 /* In this section server shares will be defined
210 * A user can select one of the given shares and a mount point
211 * and attach this combination to his setup.
212 */
213 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
214 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
215 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
217 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
218 * This entry will be, a combination of mountPoint and sharedefinitions
219 */
220 if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
221 /* We assign a share to this user, if we don't know where to mount the share */
222 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
223 msg_dialog::display(_("Error"), msgPool::invalid(_("Mount point")), WARNING_DIALOG);
224 }elseif(isset($_POST['gotoShareSelection']) && isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
225 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
226 $s_mount = $_POST['gotoShareMountPoint'];
227 /* Preparing the new assignment */
228 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
229 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
230 }
231 }
233 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
234 * If there is no defined share selected, we will abort the deletion without any message
235 */
236 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
237 unset($this->gotoShares[$_POST['gotoShare']]);
238 }
240 $smarty->assign("gotoShares",$this->printOutAssignedShares());
241 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
243 /* Create divSelectBox for ldap server selection
244 */
245 $SelectBoxLdapServer = new divSelectBox("LdapServer");
246 $SelectBoxLdapServer->SetHeight(80);
248 /* Add new ldap server to the list */
249 if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
250 if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
251 $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
252 if(!in_array($to_add,$this->gotoLdapServers)){
253 $this->gotoLdapServers[] = $to_add;
254 }
255 }
256 }
258 /* Move ldap servers up and down */
259 if(!$this->gotoLdap_inherit){
260 foreach($_POST as $name => $value){
261 if(preg_match("/sort_ldap_up_/",$name)){
262 $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
263 $from = $id;
264 $to = $id -1;
265 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
266 if($tmp){
267 $this->gotoLdapServers = $tmp;
268 }
269 break;
270 }
271 if(preg_match("/sort_ldap_down_/",$name)){
272 $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
273 $from = $id;
274 $to = $id +1;
275 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
276 if($tmp){
277 $this->gotoLdapServers = $tmp;
278 }
279 break;
280 }
281 if(preg_match("/gotoLdapRemove_/",$name)){
282 $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
283 $value = $this->gotoLdapServers[$id];
284 $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
285 break;
286 }
287 }
288 }
289 /* Add Entries
290 */
291 foreach($this->gotoLdapServers as $key => $server){
292 if(!in_array($server,$this->gotoLdapServerList)){
293 $server = $server." <font style='color:red'>(missing)</font>";
294 }
296 $SelectBoxLdapServer->AddEntry(
297 array(array("string" => $server),
298 array("string" =>
299 "<input class='center' type='image' src='images/lists/sort-up.png' name='sort_ldap_up_".$key."'> ".
300 "<input class='center' type='image' src='images/lists/sort-down.png' name='sort_ldap_down_".$key."'> ".
301 "<input class='center' type='image' src='images/lists/trash.png' name='gotoLdapRemove_".$key."'>",
302 "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
303 }
305 if($this->gotoLdap_inherit){
306 $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
307 }else{
308 $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
309 }
311 $list = array();
312 foreach($this->gotoLdapServerList as $key => $entry){
313 if(!in_array($entry,$this->gotoLdapServers)){
314 $list[$key] = $entry;
315 }
316 }
317 $smarty->assign("gotoLdapServers", $SelectBoxLdapServer->DrawList());
318 $smarty->assign("gotoLdapServerList", $list);
319 $smarty->assign("gotoLdap_inherit", $this->gotoLdap_inherit);
320 $smarty->assign("JS", session::get('js'));
322 foreach (array("gotoModules" ) as $val){
323 $smarty->assign("$val", $this->$val);
324 }
326 /* Values */
327 foreach(array("gotoBootKernel", "gotoKernelParameters") as $val){
328 $smarty->assign($val, $this->$val);
329 }
331 /* Show main page */
332 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
333 }
335 function remove_from_parent()
336 {
337 if($this->acl_is_removeable()){
338 $this->handle_post_events("remove");
339 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
340 }
341 }
344 /* Save data to object */
345 function save_object()
346 {
347 plugin::save_object();
349 if(isset($_POST['TerminalStarttabPosted'])){
350 if(isset($_POST['gotoLdap_inherit'])){
351 $this->gotoLdap_inherit = TRUE;
352 }else{
353 $this->gotoLdap_inherit = FALSE;
354 }
355 }
356 }
359 /* Save to LDAP */
360 function save()
361 {
362 /* Depending on the baseobject (Ogroup / WS) we
363 * use another set of objectClasses
364 * In case of TS itself, we use "array("GOhard");"
365 * if we are currently editing from ogroup menu we use (array("goTerminalTemplate"))
366 */
367 if(isset($this->parent->by_object['ogroup'])){
368 $this->objectclasses = array("gotoTerminalTemplate");
369 }elseif(isset($this->parent->by_object['termgeneric'])){
370 $this->objectclasses = array("GOhard");
371 }else{
372 msg_dialog::display(_("Fatal error"),
373 "Object Type Configuration is unknown. Please contact the GOsa developers.",
374 FATAL_ERROR_DIALOG);
375 exit();
376 }
379 plugin::save();
381 /* Add missing arrays */
382 foreach (array("gotoModules") as $val){
383 if (isset ($this->$val) && count ($this->$val) != 0){
385 $this->attrs["$val"]= array_unique($this->$val);
386 }
387 if(!isset($this->attrs["$val"])){
388 $this->attrs["$val"]=array();
389 }
390 }
392 /* Prepare list of ldap servers */
393 $this->attrs['gotoLdapServer'] = array();
394 if(!$this->gotoLdap_inherit){
395 $i = 0;
396 foreach($this->gotoLdapServers as $server){
397 $i ++;
398 $this->attrs['gotoLdapServer'][] = $i.":".$server;
399 }
400 }
402 /* Strip out 'default' values */
403 if ($this->attrs['gotoBootKernel'] == "default-inherited"){
404 $this->attrs['gotoBootKernel']= array();
405 }
407 /* prepare share settings */
408 $tmp = array();
409 foreach($this->gotoShares as $name => $settings){
410 $tmp2 = explode("|",$name);
411 $name = $tmp2[0];
412 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
413 }
414 $this->attrs['gotoShare']=$tmp;
416 /* Write back to ldap */
417 $ldap= $this->config->get_ldap_link();
418 $ldap->cd($this->dn);
420 $this->cleanup();
421 $ldap->modify ($this->attrs);
423 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
425 if (!$ldap->success()){
426 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
427 }
428 $this->handle_post_events("modify");
429 }
431 /* Add value to array, check if unique */
432 function add_list (&$array, $value)
433 {
434 if ($value != ""){
435 $array[]= $value;
436 sort($array);
437 array_unique ($array);
438 }
439 }
442 /* Delete value to array, check if unique */
443 function del_list (&$array, $list)
444 {
445 $tmp= array();
446 foreach ($array as $mod){
447 if (!in_array($mod, $list)){
448 $tmp[]= $mod;
449 }
450 }
451 $array= $tmp;
452 }
454 /* Generate ListBox frindly output for the defined shares
455 * Possibly Add or remove an attribute here,
456 */
457 function printOutAssignedShares()
458 {
459 $a_return = array();
460 if(is_array($this->gotoShares)){
461 foreach($this->gotoShares as $share){
462 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
463 }
464 }
465 return($a_return);
466 }
469 function PrepareForCopyPaste($source)
470 {
471 plugin::PrepareForCopyPaste($source);
473 $source_o = new termstartup ($this->config, $source['dn']);
475 foreach(array("gotoModules", "gotoKernelParameters","gotoShare",
476 "gotoKernelParameters","gotoShares",
477 "goLdapServerList","gotoBootKernel","gotoLdapServer",
478 "gotoBootKernels","gotoLdapServers","gotoLdapServers",
479 "gotoLdapServerList","gotoLdap_inherit","gotoShareSelections",
480 "gotoAvailableShares") as $attr){
481 $this->$attr = $source_o->$attr;
482 }
483 }
486 function array_switch_item($ar,$from,$to)
487 {
488 if(!is_array($ar)){
489 return(false);
490 }
491 if(!isset($ar[$from])){
492 return(false);
493 }
494 if(!isset($ar[$to])){
495 return(false);
496 }
498 $tmp = $ar[$from];
499 $ar[$from] = $ar[$to];
500 $ar[$to] = $tmp;
501 return($ar);
502 }
505 /* Return plugin informations for acl handling */
506 static function plInfo()
507 {
508 return (array(
509 "plShortName" => _("Startup"),
510 "plDescription" => _("Terminal startup"),
511 "plSelfModify" => FALSE,
512 "plDepends" => array(),
513 "plPriority" => 5,
514 "plSection" => array("administration"),
515 "plCategory" => array("terminal"),
517 "plProvidedAcls"=> array(
518 "gotoLdapServer" => _("Ldap server"),
519 "gotoShare" => _("Shares"),
520 "gotoModules" => _("Kernel modules"),
521 "gotoBootKernel" => _("Boot kernel"),
522 "gotoKernelParameters"=> _("Kernel parameter"))
523 ));
524 }
527 }
529 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
530 ?>