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->get_cfg_value(get_class($this), "systemKernelsHook");
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 // Prepare lists
161 $this->ldapList = new sortableListing(array(),array(), TRUE);
162 $this->ldapList->setDeleteable(true);
163 $this->ldapList->setEditable(false);
164 $this->ldapList->setWidth("100%");
165 $this->ldapList->setHeight("100px");
166 $this->ldapList->setHeader(array(_("LDAP server")));
167 $this->ldapList->setDefaultSortColumn(0);
169 }
171 function execute()
172 {
173 /* Call parent execute */
174 plugin::execute();
176 if($this->is_account && !$this->view_logged){
177 $this->view_logged = TRUE;
178 new log("view","terminal/".get_class($this),$this->dn);
179 }
181 /* Do we need to flip is_account state? */
182 if (isset($_POST['modify_state'])){
183 $this->is_account= !$this->is_account;
184 }
186 /* Do we represent a valid terminal? */
187 if (!$this->is_account && $this->parent === NULL){
188 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
189 msgPool::noValidExtension(_("terminal"))."</b>";
190 return ($display);
191 }
193 /* Add module */
194 if (isset ($_POST['add_module'])){
195 if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
196 $this->add_list ($this->gotoModules, $_POST['module']);
197 }
198 }
200 /* Delete module */
201 if (isset ($_POST['delete_module'])){
202 if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
203 $this->del_list ($this->gotoModules, $_POST['modules_list']);
204 }
205 }
207 /* Show main page */
208 $smarty= get_smarty();
210 /* Assign acls */
211 $tmp = $this->plInfo();
212 foreach($tmp['plProvidedAcls'] as $name => $translation){
213 $smarty->assign($name."ACL",$this->getacl($name));
214 }
216 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
218 /* In this section server shares will be defined
219 * A user can select one of the given shares and a mount point
220 * and attach this combination to his setup.
221 */
222 $smarty->assign("gotoShareSelections", $this->gotoShareSelections);
223 $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
224 $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
226 /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
227 * This entry will be, a combination of mountPoint and sharedefinitions
228 */
229 if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
230 /* We assign a share to this user, if we don't know where to mount the share */
231 if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
232 msg_dialog::display(_("Error"), msgPool::invalid(_("Mount point")), WARNING_DIALOG);
233 }elseif(isset($_POST['gotoShareSelection']) && isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
234 $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
235 $s_mount = $_POST['gotoShareMountPoint'];
236 /* Preparing the new assignment */
237 $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
238 $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
239 }
240 }
242 /* if the Post gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
243 * If there is no defined share selected, we will abort the deletion without any message
244 */
245 if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
246 unset($this->gotoShares[$_POST['gotoShare']]);
247 }
249 $smarty->assign("gotoShares",$this->printOutAssignedShares());
250 $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
251 $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
254 /* Add new ldap server to the list */
255 if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
256 if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
257 $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
258 if(!in_array($to_add,$this->gotoLdapServers)){
259 $this->gotoLdapServers[] = $to_add;
260 }
261 }
262 }
265 /* Move ldap servers up and down */
266 $this->ldapList->save_object();
267 $action = $this->ldapList->getAction();
268 if($action['action'] == 'reorder'){
269 $this->gotoLdapServers = array_values($this->ldapList->getMaintainedData());
270 }
271 if($action['action'] == 'delete'){
272 $id = $this->ldapList->getKey($action['targets'][0]);
273 $value = $this->gotoLdapServers[$id];
274 $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
275 }
277 $this->ldapList->setAcl($this->getacl('gotoLdapServer'));
279 /* Add Entries */
280 $data = $lData = array();
281 foreach($this->gotoLdapServers as $key => $server){
282 $data[$key]=$server;
284 /* Announce missing entries */
285 if(!in_array($server,$this->gotoLdapServerList)){
286 $server = $server." <font style='color:red'>(missing)</font>";
287 }
289 /* Convert old style entry */
290 if (!preg_match('%:ldaps?://%', $server)){
291 $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
292 } else {
293 $server= preg_replace("/^[^:]+:/", "", $server);
294 $lData[$key] = array('data'=>array($server));
295 }
296 }
297 $this->ldapList->setListData($data,$lData);
298 $this->ldapList->update();
303 if($this->gotoLdap_inherit){
304 $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
305 }else{
306 $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
307 }
309 $list = array();
310 foreach($this->gotoLdapServerList as $key => $entry){
311 if(!in_array($entry,$this->gotoLdapServers)){
312 $list[$key] = $entry;
313 }
314 }
315 $smarty->assign("gotoLdapServers", $this->ldapList->render());
316 $smarty->assign("gotoLdapServerList", $list);
317 $smarty->assign("gotoLdap_inherit", $this->gotoLdap_inherit);
318 $smarty->assign("JS", session::get('js'));
320 foreach (array("gotoModules" ) as $val){
321 $smarty->assign("$val", $this->$val);
322 }
324 /* Values */
325 foreach(array("gotoBootKernel", "gotoKernelParameters") as $val){
326 $smarty->assign($val, $this->$val);
327 }
329 /* Show main page */
330 return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
331 }
333 function remove_from_parent()
334 {
335 if($this->acl_is_removeable()){
336 $this->handle_post_events("remove");
337 new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
338 }
339 }
342 /* Save data to object */
343 function save_object()
344 {
345 plugin::save_object();
347 if(isset($_POST['TerminalStarttabPosted'])){
348 if(isset($_POST['gotoLdap_inherit'])){
349 $this->gotoLdap_inherit = TRUE;
350 }else{
351 $this->gotoLdap_inherit = FALSE;
352 }
353 }
354 }
357 /* Save to LDAP */
358 function save()
359 {
360 /* Depending on the baseobject (Ogroup / WS) we
361 * use another set of objectClasses
362 * In case of TS itself, we use "array("GOhard");"
363 * if we are currently editing from ogroup menu we use (array("goTerminalTemplate"))
364 */
365 if(isset($this->parent->by_object['ogroup'])){
366 $this->objectclasses = array("gotoTerminalTemplate");
367 }elseif(isset($this->parent->by_object['termgeneric'])){
368 $this->objectclasses = array("GOhard");
369 }else{
370 msg_dialog::display(_("Fatal error"),
371 "Object Type Configuration is unknown. Please contact the GOsa developers.",
372 FATAL_ERROR_DIALOG);
373 exit();
374 }
377 plugin::save();
379 /* Add missing arrays */
380 foreach (array("gotoModules") as $val){
381 if (isset ($this->$val) && count ($this->$val) != 0){
383 $this->attrs["$val"]= array_unique($this->$val);
384 }
385 if(!isset($this->attrs["$val"])){
386 $this->attrs["$val"]=array();
387 }
388 }
390 /* Prepare list of ldap servers */
391 $this->attrs['gotoLdapServer'] = array();
392 if(!$this->gotoLdap_inherit){
393 $i = 0;
394 foreach($this->gotoLdapServers as $server){
395 $i ++;
396 $this->attrs['gotoLdapServer'][] = $i.":".$server;
397 }
398 }
400 /* Strip out 'default' values */
401 if ($this->attrs['gotoBootKernel'] == "default-inherited"){
402 $this->attrs['gotoBootKernel']= array();
403 }
405 /* prepare share settings */
406 $tmp = array();
407 foreach($this->gotoShares as $name => $settings){
408 $tmp2 = explode("|",$name);
409 $name = $tmp2[0];
410 $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
411 }
412 $this->attrs['gotoShare']=$tmp;
414 /* Write back to ldap */
415 $ldap= $this->config->get_ldap_link();
416 $ldap->cd($this->dn);
418 $this->cleanup();
419 $ldap->modify ($this->attrs);
421 new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
423 if (!$ldap->success()){
424 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
425 }
426 $this->handle_post_events("modify");
427 }
429 /* Add value to array, check if unique */
430 function add_list (&$array, $value)
431 {
432 if ($value != ""){
433 $array[]= $value;
434 sort($array);
435 array_unique ($array);
436 }
437 }
440 /* Delete value to array, check if unique */
441 function del_list (&$array, $list)
442 {
443 $tmp= array();
444 foreach ($array as $mod){
445 if (!in_array($mod, $list)){
446 $tmp[]= $mod;
447 }
448 }
449 $array= $tmp;
450 }
452 /* Generate ListBox frindly output for the defined shares
453 * Possibly Add or remove an attribute here,
454 */
455 function printOutAssignedShares()
456 {
457 $a_return = array();
458 if(is_array($this->gotoShares)){
459 foreach($this->gotoShares as $share){
460 $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
461 }
462 }
463 return($a_return);
464 }
467 function PrepareForCopyPaste($source)
468 {
469 plugin::PrepareForCopyPaste($source);
471 $source_o = new termstartup ($this->config, $source['dn']);
473 foreach(array("gotoModules", "gotoKernelParameters","gotoShare",
474 "gotoKernelParameters","gotoShares",
475 "goLdapServerList","gotoBootKernel","gotoLdapServer",
476 "gotoBootKernels","gotoLdapServers","gotoLdapServers",
477 "gotoLdapServerList","gotoLdap_inherit","gotoShareSelections",
478 "gotoAvailableShares") as $attr){
479 $this->$attr = $source_o->$attr;
480 }
481 }
484 function array_switch_item($ar,$from,$to)
485 {
486 if(!is_array($ar)){
487 return(false);
488 }
489 if(!isset($ar[$from])){
490 return(false);
491 }
492 if(!isset($ar[$to])){
493 return(false);
494 }
496 $tmp = $ar[$from];
497 $ar[$from] = $ar[$to];
498 $ar[$to] = $tmp;
499 return($ar);
500 }
503 /* Return plugin informations for acl handling */
504 static function plInfo()
505 {
506 return (array(
507 "plShortName" => _("Startup"),
508 "plDescription" => _("Terminal startup"),
509 "plSelfModify" => FALSE,
510 "plDepends" => array(),
511 "plPriority" => 5,
512 "plSection" => array("administration"),
513 "plCategory" => array("terminal"),
515 "plProvidedAcls"=> array(
516 "gotoLdapServer" => _("Ldap server"),
517 "gotoShare" => _("Shares"),
518 "gotoModules" => _("Kernel modules"),
519 "gotoBootKernel" => _("Boot kernel"),
520 "gotoKernelParameters"=> _("Kernel parameter"))
521 ));
522 }
525 }
527 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
528 ?>