Code

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