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("gotoSharesCount",count($this->printOutAssignedShares()));
242 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
244 /* Create divSelectBox for ldap server selection
245 */
246 $SelectBoxLdapServer = new divSelectBox("LdapServer");
247 $SelectBoxLdapServer->SetHeight(80);
249 /* Add new ldap server to the list */
250 if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
251 if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
252 $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
253 if(!in_array($to_add,$this->gotoLdapServers)){
254 $this->gotoLdapServers[] = $to_add;
255 }
256 }
257 }
259 /* Move ldap servers up and down */
260 if(!$this->gotoLdap_inherit && $this->acl_is_writeable("gotoLdapServer")){
261 foreach($_POST as $name => $value){
262 if(preg_match("/sort_ldap_up_/",$name)){
263 $id = preg_replace("/^sort_ldap_up_([0-9]*)$/","\\1",$name);
264 $from = $id;
265 $to = $id -1;
266 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
267 if($tmp){
268 $this->gotoLdapServers = $tmp;
269 }
270 break;
271 }
272 if(preg_match("/sort_ldap_down_/",$name)){
273 $id = preg_replace("/^sort_ldap_down_([0-9]*)$/","\\1",$name);
274 $from = $id;
275 $to = $id +1;
276 $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
277 if($tmp){
278 $this->gotoLdapServers = $tmp;
279 }
280 break;
281 }
282 if(preg_match("/gotoLdapRemove_/",$name)){
283 $id = preg_replace("/^gotoLdapRemove_([0-9]*)$/","\\1",$name);
284 $value = $this->gotoLdapServers[$id];
285 $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
286 break;
287 }
288 }
289 }
291 /* Add Entries
292 */
293 if($this->acl_is_readable("gotoLdapServer")){
295 foreach($this->gotoLdapServers as $key => $server){
297 /* Announce missing entries */
298 if(!in_array($server,$this->gotoLdapServerList)){
299 $server = $server." <font style='color:red'>(missing)</font>";
300 }
302 /* Convert old style entry */
303 if (!preg_match('%:ldaps?://%', $server)){
304 $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
306 /* Beautify new style entries */
307 } else {
308 $server= preg_replace("/^[^:]+:/", "", $server);
309 }
311 $SelectBoxLdapServer->AddEntry(
312 array(
313 array(
314 "string" => $server),
315 array("string" =>
316 image('images/lists/sort-up.png',"sort_ldap_up_".$key,'',"top").
317 image('images/lists/sort-down.png',"sort_ldap_down_".$key,'',"bottom").
318 image('images/lists/trash.png',"gotoLdapRemove_".$key),
319 "attach" => "style='text-align:right;width:60px;border-right:0px;'")));
320 }
321 }
324 if($this->gotoLdap_inherit){
325 $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
326 }else{
327 $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
328 }
330 $list = array();
331 foreach($this->gotoLdapServerList as $key => $entry){
332 if(!in_array($entry,$this->gotoLdapServers)){
333 $list[$key] = $entry;
334 }
335 }
336 $smarty->assign("gotoLdapServers", $SelectBoxLdapServer->DrawList());
337 $smarty->assign("gotoLdapServerList", $list);
338 $smarty->assign("gotoLdap_inherit", $this->gotoLdap_inherit);
339 $smarty->assign("JS", session::get('js'));
341 foreach (array("gotoModules" ) as $val){
342 $smarty->assign("$val", $this->$val);
343 }
345 /* Values */
346 foreach(array("gotoBootKernel", "gotoKernelParameters") as $val){
347 $smarty->assign($val, $this->$val);
348 }
350 /* Show main page */
351 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
352 }
354 function remove_from_parent()
355 {
356 if($this->acl_is_removeable()){
357 $this->handle_post_events("remove");
358 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
359 }
360 }
363 /* Save data to object */
364 function save_object()
365 {
366 plugin::save_object();
368 if(isset($_POST['TerminalStarttabPosted'])){
369 if(isset($_POST['gotoLdap_inherit'])){
370 $this->gotoLdap_inherit = TRUE;
371 }else{
372 $this->gotoLdap_inherit = FALSE;
373 }
374 }
375 }
378 /* Save to LDAP */
379 function save()
380 {
381 /* Depending on the baseobject (Ogroup / WS) we
382 * use another set of objectClasses
383 * In case of TS itself, we use "array("GOhard");"
384 * if we are currently editing from ogroup menu we use (array("goTerminalTemplate"))
385 */
386 if(isset($this->parent->by_object['ogroup'])){
387 $this->objectclasses = array("gotoTerminalTemplate");
388 }elseif(isset($this->parent->by_object['termgeneric'])){
389 $this->objectclasses = array("GOhard");
390 }else{
391 msg_dialog::display(_("Fatal error"),
392 "Object Type Configuration is unknown. Please contact the GOsa developers.",
393 FATAL_ERROR_DIALOG);
394 exit();
395 }
398 plugin::save();
400 /* Add missing arrays */
401 foreach (array("gotoModules") as $val){
402 if (isset ($this->$val) && count ($this->$val) != 0){
404 $this->attrs["$val"]= array_unique($this->$val);
405 }
406 if(!isset($this->attrs["$val"])){
407 $this->attrs["$val"]=array();
408 }
409 }
411 /* Prepare list of ldap servers */
412 $this->attrs['gotoLdapServer'] = array();
413 if(!$this->gotoLdap_inherit){
414 $i = 0;
415 foreach($this->gotoLdapServers as $server){
416 $i ++;
417 $this->attrs['gotoLdapServer'][] = $i.":".$server;
418 }
419 }
421 /* Strip out 'default' values */
422 if ($this->attrs['gotoBootKernel'] == "default-inherited"){
423 $this->attrs['gotoBootKernel']= array();
424 }
426 /* prepare share settings */
427 $tmp = array();
428 foreach($this->gotoShares as $name => $settings){
429 $tmp2 = explode("|",$name);
430 $name = $tmp2[0];
431 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
432 }
433 $this->attrs['gotoShare']=$tmp;
435 /* Write back to ldap */
436 $ldap= $this->config->get_ldap_link();
437 $ldap->cd($this->dn);
439 $this->cleanup();
440 $ldap->modify ($this->attrs);
442 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
444 if (!$ldap->success()){
445 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
446 }
447 $this->handle_post_events("modify");
448 }
450 /* Add value to array, check if unique */
451 function add_list (&$array, $value)
452 {
453 if ($value != ""){
454 $array[]= $value;
455 sort($array);
456 array_unique ($array);
457 }
458 }
461 /* Delete value to array, check if unique */
462 function del_list (&$array, $list)
463 {
464 $tmp= array();
465 foreach ($array as $mod){
466 if (!in_array($mod, $list)){
467 $tmp[]= $mod;
468 }
469 }
470 $array= $tmp;
471 }
473 /* Generate ListBox frindly output for the defined shares
474 * Possibly Add or remove an attribute here,
475 */
476 function printOutAssignedShares()
477 {
478 $a_return = array();
479 if(is_array($this->gotoShares)){
480 foreach($this->gotoShares as $share){
481 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
482 }
483 }
484 return($a_return);
485 }
488 function PrepareForCopyPaste($source)
489 {
490 plugin::PrepareForCopyPaste($source);
492 $source_o = new termstartup ($this->config, $source['dn']);
494 foreach(array("gotoModules", "gotoKernelParameters","gotoShare",
495 "gotoKernelParameters","gotoShares",
496 "goLdapServerList","gotoBootKernel","gotoLdapServer",
497 "gotoBootKernels","gotoLdapServers","gotoLdapServers",
498 "gotoLdapServerList","gotoLdap_inherit","gotoShareSelections",
499 "gotoAvailableShares") as $attr){
500 $this->$attr = $source_o->$attr;
501 }
502 }
505 function array_switch_item($ar,$from,$to)
506 {
507 if(!is_array($ar)){
508 return(false);
509 }
510 if(!isset($ar[$from])){
511 return(false);
512 }
513 if(!isset($ar[$to])){
514 return(false);
515 }
517 $tmp = $ar[$from];
518 $ar[$from] = $ar[$to];
519 $ar[$to] = $tmp;
520 return($ar);
521 }
524 /* Return plugin informations for acl handling */
525 static function plInfo()
526 {
527 return (array(
528 "plShortName" => _("Startup"),
529 "plDescription" => _("Terminal startup"),
530 "plSelfModify" => FALSE,
531 "plDepends" => array(),
532 "plPriority" => 5,
533 "plSection" => array("administration"),
534 "plCategory" => array("terminal"),
536 "plProvidedAcls"=> array(
537 "gotoLdapServer" => _("Ldap server"),
538 "gotoShare" => _("Shares"),
539 "gotoModules" => _("Kernel modules"),
540 "gotoBootKernel" => _("Boot kernel"),
541 "gotoKernelParameters"=> _("Kernel parameter"))
542 ));
543 }
546 }
548 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
549 ?>