Code

Updated Workstation service
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_terminalStartup.inc
1 <?php
2 class termstartup extends plugin
3 {
4   /* Generic terminal attributes */
5   var $goLdapServerList= array();
6   var $gotoBootKernel= "default";
7   var $gotoKernelParameters= "";
8   var $gotoLdapServer= "";
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;
30   /* Helper */
31   var $orig_dn= "";
32   var $ignore_account= TRUE;
34   function termstartup (&$config, $dn= NULL, $parent= NULL)
35   {
36     plugin::plugin ($config, $dn, $parent);
38     $this->gotoBootKernels = array("default"=> ("default"));
40     /* Get arrays */
41     foreach (array("gotoModules") as $val){
42       $this->$val = array();
43       if (isset($this->attrs["$val"]["count"])){
44         for ($i= 0; $i<$this->attrs["count"]; $i++){
45           if (isset($this->attrs["$val"][$i])){
46             array_push($this->$val, $this->attrs["$val"][$i]);
47           }
48         }
49       }
50       sort ($this->$val);
51       $this->$val= array_unique($this->$val);
52     }
54     /* Prepare Shares */
55     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
56       unset($this->attrs['gotoShare']['count']);
57       foreach($this->attrs['gotoShare'] as $share){
58         $tmp = $tmp2 = array();
59         $tmp = split("\|",$share);
60         $tmp2['server']      =$tmp[0];
61         $tmp2['name']        =$tmp[1];
62         $tmp2['mountPoint']  =$tmp[2];
63         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
64       }
65     }
67     $this->gotoShareSelections= $config->getShareList(true);
68     $this->gotoAvailableShares= $config->getShareList(false);
70     $this->orig_dn= $this->dn;
72     /* Get list of boot kernels */
73     if (isset($this->config->data['TABS'])){
74       $command= $this->config->search(get_class($this), "systemKernelsHook",array('tabs'));
76       if (!check_command($command)){
77         $message[]= sprintf(_("Command '%s', specified as systemKernelsHook for plugin '%s' doesn't seem to exist."), $command,
78             get_class($this));
79       } else {
80         $fh= popen($command, "r");
81         while (!feof($fh)) {
82           $buffer= trim(fgets($fh, 256));
83           
84           if(!empty($buffer)){
85           
86             $name=$value = $buffer;
88             if(preg_match("/:/",$buffer)){
89               $name = preg_replace("/:.*$/","",$buffer);
90               $value= preg_replace("/^.*:/","",$buffer);
91               $this->gotoBootKernels[$name]= $name.":".$value;
92             }else{
93               $this->gotoBootKernels[$name]= $value;
94             }
95           }
96         }
97         pclose($fh);
98       }
100     }
101     
102     foreach($this->config->data['SERVERS']['LDAP'] as $server) {
103       $this->gotoLdapServerList[]= $server;
104     }
105     if(isset($this->attrs['gotoLdapServer'])){
106       unset($this->attrs['gotoLdapServer']['count']);
107       sort($this->attrs['gotoLdapServer']);
108       foreach($this->attrs['gotoLdapServer'] as $value){
109         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
110       }
111     }
112     if(!count($this->gotoLdapServers)){
113       $this->gotoLdap_inherit = TRUE;
114     }
116     /* Load hardware list */
117     $ldap= $this->config->get_ldap_link();
118     $ldap->cd($this->config->current['BASE']);
119     $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))");
120     if ($ldap->count() == 1){
121       $map= array("gotoLdapServer","gotoBootKernel");
122       $attrs= $ldap->fetch();
124       foreach ($map as $name){
125         if (!isset($attrs[$name][0])){
126           continue;
127         }
128         switch ($name){
129           case 'gotoBootKernel':
130             $this->gotoBootKernels["default"] = "["._("inherited")."]"; 
131           break;
132           case 'gotoLdapServer':
133             $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
134             break;
135         }
136       }
137     }
138   }
140   function execute()
141   {
142     /* Call parent execute */
143     plugin::execute();
145     if($this->is_account && !$this->view_logged){
146       $this->view_logged = TRUE;
147       new log("view","terminal/".get_class($this),$this->dn);
148     }
150     /* Do we need to flip is_account state? */
151     if (isset($_POST['modify_state'])){
152       $this->is_account= !$this->is_account;
153     }
155     /* Do we represent a valid terminal? */
156     if (!$this->is_account && $this->parent === NULL){
157       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
158         msgPool::noValidExtension(_("terminal"))."</b>";
159       return ($display);
160     }
162     /* Add module */
163     if (isset ($_POST['add_module'])){
164       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
165         $this->add_list ($this->gotoModules, $_POST['module']);
166       }
167     }
169     /* Delete module */
170     if (isset ($_POST['delete_module'])){
171       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
172         $this->del_list ($this->gotoModules, $_POST['modules_list']);
173       }
174     }
176     /* Show main page */
177     $smarty= get_smarty();
179     /* Assign acls */
180     $tmp = $this->plInfo();
181     foreach($tmp['plProvidedAcls'] as $name => $translation){
182       $smarty->assign($name."ACL",$this->getacl($name));
183     }
186        /* In this section server shares will be defined
187      * A user can select one of the given shares and a mount point
188      *  and attach this combination to his setup.
189      */
190     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
191     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
192     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
194     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
195      * This entry will be, a combination of mountPoint and sharedefinitions
196      */
197     if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
198       /* We assign a share to this user, if we don't know where to mount the share */
199       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
200         msg_dialog::display(_("Error"), msgPool::invalid(_("Mount point")), WARNING_DIALOG);
201       }else{
202         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
203         $s_mount = $_POST['gotoShareMountPoint'];
204         /* Preparing the new assignment */
205         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
206         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
207       }
208     }
210     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
211      * If there is no defined share selected, we will abort the deletion without any message
212      */
213     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
214       unset($this->gotoShares[$_POST['gotoShare']]);
215     }
217     $smarty->assign("gotoShares",$this->printOutAssignedShares());
218     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
220     /* Create divSelectBox for ldap server selection
221      */
222     $SelectBoxLdapServer = new divSelectBox("LdapServer");
223     $SelectBoxLdapServer->SetHeight(80);
225  /* Add new ldap server to the list */
226     if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
227       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
228         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
229         if(!in_array($to_add,$this->gotoLdapServers)){
230           $this->gotoLdapServers[] = $to_add;
231         }
232       }
233     }
235     /* Move ldap servers up and down */
236     if(!$this->gotoLdap_inherit){
237       foreach($_POST as $name => $value){
238         if(preg_match("/sort_ldap_up_/",$name)){
239           $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
240           $from =  $id;
241           $to   =  $id -1;
242           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
243           if($tmp){
244             $this->gotoLdapServers = $tmp;
245           }
246           break;
247         }
248         if(preg_match("/sort_ldap_down_/",$name)){
249           $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
250           $from =  $id;
251           $to   =  $id +1;
252           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
253           if($tmp){
254             $this->gotoLdapServers = $tmp;
255           }
256           break;
257         }
258         if(preg_match("/gotoLdapRemove_/",$name)){
259           $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
260           $value = $this->gotoLdapServers[$id];
261           $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
262           break;
263         }
264       }
265     }
266   /* Add Entries
267      */
268     foreach($this->gotoLdapServers as $key => $server){
269       if(!in_array($server,$this->gotoLdapServerList)){
270         $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
271       }
273       $SelectBoxLdapServer->AddEntry(
274           array(array("string" => $server),
275             array("string" =>
276               "<input class='center' type='image' src='images/lists/sort-up.png' name='sort_ldap_up_".$key."'>&nbsp;".
277               "<input class='center' type='image' src='images/lists/sort-down.png' name='sort_ldap_down_".$key."'>&nbsp;".
278               "<input class='center' type='image' src='images/lists/trash.png' name='gotoLdapRemove_".$key."'>",
279               "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
280     }
282     if($this->gotoLdap_inherit){
283       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
284     }else{
285       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
286     }
288     $list = array();
289     foreach($this->gotoLdapServerList as $key => $entry){
290       if(!in_array($entry,$this->gotoLdapServers)){
291         $list[$key] = $entry;
292       }
293     }
294     $smarty->assign("gotoLdapServers",    $SelectBoxLdapServer->DrawList());
295     $smarty->assign("gotoLdapServerList", $list);
296     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
297     $smarty->assign("JS",  session::get('js'));
299     foreach (array("gotoModules" ) as $val){
300       $smarty->assign("$val", $this->$val);
301     }
303     /* Values */
304     foreach(array("gotoBootKernel", "gotoKernelParameters") as $val){
305       $smarty->assign($val, $this->$val);
306     }
308     /* Show main page */
309     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
310   }
312   function remove_from_parent()
313   {
314     if($this->acl_is_removeable()){
315       $this->handle_post_events("remove");
316       new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
317     }
318   }
321   /* Save data to object */
322   function save_object()
323   {
324     plugin::save_object();
326     if(isset($_POST['TerminalStarttabPosted'])){
327       if(isset($_POST['gotoLdap_inherit'])){
328         $this->gotoLdap_inherit = TRUE;
329       }else{
330         $this->gotoLdap_inherit = FALSE;
331       }
333       /* Save kernel parameters */
334       if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["gotoKernelParameters"])){
335         $this->gotoKernelParameters= $_POST["gotoKernelParameters"];
336       }
337     }
338   }
341   /* Save to LDAP */
342   function save()
343   {
344     /* Find proper terminal path for tftp configuration
345        FIXME: This is suboptimal when the default has changed to
346        another location! */
347     if ($this->gotoTerminalPath == "default-inherit"){
348       $ldap= $this->config->get_ldap_link();
350       /* Strip relevant part from dn, keep trailing ',' */
351       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalRDN')."/i", "", $this->dn);
352       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
354       /* Walk from top to base and try to load default values for
355          'gotoTerminalPath'. Abort when an entry is found. */
356       while (TRUE){
357         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
359         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
360             $this->config->current['BASE'], array('gotoTerminalPath'));
361         $attrs= $ldap->fetch();
362         if (isset($attrs['gotoTerminalPath'])){
363           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
364           break;
365         }
367         /* Nothing left? */
368         if ($tmp == ""){
369           break;
370         }
371       }
372     }
373     
374     plugin::save();
376     /* Add missing arrays */
377     foreach (array("gotoModules") as $val){
378       if (isset ($this->$val) && count ($this->$val) != 0){
379     
380         $this->attrs["$val"]= array_unique($this->$val);
381       }
382       if(!isset($this->attrs["$val"])){
383        $this->attrs["$val"]=array();
384       }
385     }
387     /* Prepare list of ldap servers */
388     $this->attrs['gotoLdapServer'] = array();
389     if(!$this->gotoLdap_inherit){
390       $i = 0;
391       foreach($this->gotoLdapServers as $server){
392         $i ++;
393         $this->attrs['gotoLdapServer'][] = $i.":".$server;
394       }
395     }
397     /* Strip out 'default' values */
398     foreach(array("gotoBootKernel") as $value){
399       if (!isset($this->attrs[$value]) || $this->attrs[$value] == "default"){
400         $this->attrs[$value] = array();
401       } 
402     }
404      /* prepare share settings */
405     $tmp = array();
406     foreach($this->gotoShares as $name => $settings){
407       $tmp2 = split("\|",$name);
408       $name = $tmp2[0];
409       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
410     }
411     $this->attrs['gotoShare']=$tmp;
413     /* Write back to ldap */
414     $ldap= $this->config->get_ldap_link();
415     $ldap->cd($this->dn);
416     
417     $this->cleanup();
418     $ldap->modify ($this->attrs); 
420     new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
422     if (!$ldap->success()){
423       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
424     }
425     $this->handle_post_events("modify");
427     /* Check if LDAP server has changed */
428     if (isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent")){
429       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
430       $o_queue = new gosaSupportDaemon();
431       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
432         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
433         $macs = array($this->parent->by_object['termgeneric']->netConfigDNS->macAddress);
434   
435         /* Trigger event for all member objects 
436          */
437         foreach($macs as $mac){
438           $tmp = new $evt['CLASS_NAME']($this->config);
439           $tmp->set_type(TRIGGERED_EVENT);
440           $tmp->add_targets(array($mac));
441           if(!$o_queue->append($tmp)){
442             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
443           }
444         }
445       }
446     }
447   }
449   /* Add value to array, check if unique */
450   function add_list (&$array, $value)
451   {
452     if ($value != ""){
453       $array[]= $value;
454       sort($array);
455       array_unique ($array);
456     }
457   }
460   /* Delete value to array, check if unique */
461   function del_list (&$array, $list)
462   {
463     $tmp= array();
464     foreach ($array as $mod){
465       if (!in_array($mod, $list)){
466         $tmp[]= $mod;
467       }
468     }
469     $array= $tmp;
470   }
472    /* Generate ListBox frindly output for the defined shares
473    * Possibly Add or remove an attribute here,
474    */
475   function printOutAssignedShares()
476   {
477     $a_return = array();
478     if(is_array($this->gotoShares)){
479       foreach($this->gotoShares as $share){
480         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
481       }
482     }
483     return($a_return);
484   }
487   function PrepareForCopyPaste($source)
488   {
489     plugin::PrepareForCopyPaste($source);
491     $source_o = new termstartup ($this->config, $source['dn']);
493     foreach(array("gotoModules", "gotoKernelParameters","gotoShares","gotoKernelParameters","gotoTerminalPath","gotoShares","goLdapServerList","gotoBootKernel","gotoLdapServer","gotoBootKernels") as $attr){
494       $this->$attr = $source_o->$attr;
495     }
496   }
499   function array_switch_item($ar,$from,$to)
500   {
501     if(!is_array($ar)){
502       return(false);
503     }
504     if(!isset($ar[$from])){
505       return(false);
506     }
507     if(!isset($ar[$to])){
508       return(false);
509     }
511     $tmp = $ar[$from];
512     $ar[$from] = $ar[$to];
513     $ar[$to] = $tmp;
514     return($ar);
515   }
518   /* Return plugin informations for acl handling */
519   static function plInfo()
520   {
521     return (array(
522           "plShortName"   => _("Startup"),
523           "plDescription" => _("Terminal startup"),
524           "plSelfModify"  => FALSE,
525           "plDepends"     => array(),
526           "plPriority"    => 5,
527           "plSection"     => array("administration"),
528           "plCategory"    => array("terminal"),
530           "plProvidedAcls"=> array(
531             "gotoLdapServer"      => _("Ldap server"),
532             "gotoShare"           => _("Shares"),
533             "gotoModules"         => _("Kernel modules"),
534             "gotoBootKernel"      => _("Boot kernel"), 
535             "gotoKernelParameters"=> _("Kernel parameter"))
536           ));
537   }
542 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
543 ?>