Code

Updated Workstation
[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-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 = split("\|",$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     foreach($this->config->data['SERVERS']['LDAP'] as $server) {
75       $this->gotoLdapServerList[]= $server;
76     }
77     if(isset($this->attrs['gotoLdapServer'])){
78       unset($this->attrs['gotoLdapServer']['count']);
79       sort($this->attrs['gotoLdapServer']);
80       foreach($this->attrs['gotoLdapServer'] as $value){
81         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
82       }
83     }
84     natcasesort($this->gotoLdapServerList);
86     /* Load hardware list */
87     $ldap= $this->config->get_ldap_link();
88     $ldap->cd($this->config->current['BASE']);
89     $ldap->search("(&(|(objectClass=gotoTerminalTemplate)(objectClass=gotoWorkstationTemplate))(member=".LDAP::prepare4filter($this->dn)."))");
90     if ($ldap->count() == 1){
91       $map= array("gotoLdapServer","gotoBootKernel");
92       $attrs= $ldap->fetch();
93       $this->member_of_ogroup = TRUE;
94       $this->o_group_dn = $attrs['dn'];
96       foreach ($map as $name){
97         if (!isset($attrs[$name][0])){
98           continue;
99         }
100         switch ($name){
101           case 'gotoBootKernel':
102                   /* Handle inheritance value "default" */
103                   if ($this->member_of_ogroup){
104                           $this->gotoBootKernels["default-inherited"]= _("inherited")." [".$attrs['gotoBootKernel'][0]."]";
105                   }
106           break;
107           case 'gotoLdapServer':
108             $this->goLdapServerList= array_merge(array('default-inherit' => _("inherited").' ['.$attrs[$name][0].']' ), $this->goLdapServerList);
109             break;
110         }
111       }
112     }
114     if(!count($this->gotoLdapServers) && $this->member_of_ogroup){
115       $this->gotoLdap_inherit = TRUE;
116     }
118     /* Get list of boot kernels */
119     if (isset($this->config->data['TABS'])){
120       $command= $this->config->search(get_class($this), "systemKernelsHook",array('tabs'));
122       if (!check_command($command)){
123         $message[]= sprintf(_("Command '%s', specified as systemKernelsHook for plugin '%s' doesn't seem to exist."), $command,
124             get_class($this));
125       } else {
126         $fh= popen($command, "r");
127         while (!feof($fh)) {
128           $buffer= trim(fgets($fh, 256));
129           
130           if(!empty($buffer)){
131           
132             $name=$value = $buffer;
134             if(preg_match("/:/",$buffer)){
135               $name = preg_replace("/:.*$/","",$buffer);
136               $value= preg_replace("/^.*:/","",$buffer);
137               $this->gotoBootKernels[$name]= $name.":".$value;
138             }else{
139               $this->gotoBootKernels[$name]= $value;
140             }
141           }
142         }
143         pclose($fh);
144       }
146     }
147     
148   }
150   function execute()
151   {
152     /* Call parent execute */
153     plugin::execute();
155     if($this->is_account && !$this->view_logged){
156       $this->view_logged = TRUE;
157       new log("view","terminal/".get_class($this),$this->dn);
158     }
160     /* Do we need to flip is_account state? */
161     if (isset($_POST['modify_state'])){
162       $this->is_account= !$this->is_account;
163     }
165     /* Do we represent a valid terminal? */
166     if (!$this->is_account && $this->parent === NULL){
167       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
168         msgPool::noValidExtension(_("terminal"))."</b>";
169       return ($display);
170     }
172     /* Add module */
173     if (isset ($_POST['add_module'])){
174       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoMode")){
175         $this->add_list ($this->gotoModules, $_POST['module']);
176       }
177     }
179     /* Delete module */
180     if (isset ($_POST['delete_module'])){
181       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoMode")){
182         $this->del_list ($this->gotoModules, $_POST['modules_list']);
183       }
184     }
186     /* Show main page */
187     $smarty= get_smarty();
189     /* Assign acls */
190     $tmp = $this->plInfo();
191     foreach($tmp['plProvidedAcls'] as $name => $translation){
192       $smarty->assign($name."ACL",$this->getacl($name));
193     }
195     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
197     /* In this section server shares will be defined
198      * A user can select one of the given shares and a mount point
199      *  and attach this combination to his setup.
200      */
201     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
202     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
203     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
205     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
206      * This entry will be, a combination of mountPoint and sharedefinitions
207      */
208     if(isset($_POST['gotoShareAdd']) && $this->acl_is_writeable("gotoShare")){
209       /* We assign a share to this user, if we don't know where to mount the share */
210       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
211         msg_dialog::display(_("Error"), msgPool::invalid(_("Mount point")), WARNING_DIALOG);
212       }elseif(isset($_POST['gotoShareSelection']) && isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
213         $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
214         $s_mount = $_POST['gotoShareMountPoint'];
215         /* Preparing the new assignment */
216         $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
217         $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
218       }
219     }
221     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
222      * If there is no defined share selected, we will abort the deletion without any message
223      */
224     if((isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare'])) && $this->acl_is_writeable("gotoShare")){
225       unset($this->gotoShares[$_POST['gotoShare']]);
226     }
228     $smarty->assign("gotoShares",$this->printOutAssignedShares());
229     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
231     /* Create divSelectBox for ldap server selection
232      */
233     $SelectBoxLdapServer = new divSelectBox("LdapServer");
234     $SelectBoxLdapServer->SetHeight(80);
236  /* Add new ldap server to the list */
237     if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
238       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
239         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
240         if(!in_array($to_add,$this->gotoLdapServers)){
241           $this->gotoLdapServers[] = $to_add;
242         }
243       }
244     }
246     /* Move ldap servers up and down */
247     if(!$this->gotoLdap_inherit){
248       foreach($_POST as $name => $value){
249         if(preg_match("/sort_ldap_up_/",$name)){
250           $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
251           $from =  $id;
252           $to   =  $id -1;
253           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
254           if($tmp){
255             $this->gotoLdapServers = $tmp;
256           }
257           break;
258         }
259         if(preg_match("/sort_ldap_down_/",$name)){
260           $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
261           $from =  $id;
262           $to   =  $id +1;
263           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
264           if($tmp){
265             $this->gotoLdapServers = $tmp;
266           }
267           break;
268         }
269         if(preg_match("/gotoLdapRemove_/",$name)){
270           $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
271           $value = $this->gotoLdapServers[$id];
272           $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
273           break;
274         }
275       }
276     }
277   /* Add Entries
278      */
279     foreach($this->gotoLdapServers as $key => $server){
280       if(!in_array($server,$this->gotoLdapServerList)){
281         $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
282       }
284       $SelectBoxLdapServer->AddEntry(
285           array(array("string" => $server),
286             array("string" =>
287               "<input class='center' type='image' src='images/lists/sort-up.png' name='sort_ldap_up_".$key."'>&nbsp;".
288               "<input class='center' type='image' src='images/lists/sort-down.png' name='sort_ldap_down_".$key."'>&nbsp;".
289               "<input class='center' type='image' src='images/lists/trash.png' name='gotoLdapRemove_".$key."'>",
290               "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
291     }
293     if($this->gotoLdap_inherit){
294       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
295     }else{
296       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
297     }
299     $list = array();
300     foreach($this->gotoLdapServerList as $key => $entry){
301       if(!in_array($entry,$this->gotoLdapServers)){
302         $list[$key] = $entry;
303       }
304     }
305     $smarty->assign("gotoLdapServers",    $SelectBoxLdapServer->DrawList());
306     $smarty->assign("gotoLdapServerList", $list);
307     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
308     $smarty->assign("JS",  session::get('js'));
310     foreach (array("gotoModules" ) as $val){
311       $smarty->assign("$val", $this->$val);
312     }
314     /* Values */
315     foreach(array("gotoBootKernel", "gotoKernelParameters") as $val){
316       $smarty->assign($val, $this->$val);
317     }
319     /* Show main page */
320     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
321   }
323   function remove_from_parent()
324   {
325     if($this->acl_is_removeable()){
326       $this->handle_post_events("remove");
327       new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
328     }
329   }
332   /* Save data to object */
333   function save_object()
334   {
335     plugin::save_object();
337     if(isset($_POST['TerminalStarttabPosted'])){
338       if(isset($_POST['gotoLdap_inherit'])){
339         $this->gotoLdap_inherit = TRUE;
340       }else{
341         $this->gotoLdap_inherit = FALSE;
342       }
344       /* Save kernel parameters */
345       if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["gotoKernelParameters"])){
346         $this->gotoKernelParameters= $_POST["gotoKernelParameters"];
347       }
348     }
349   }
352   /* Save to LDAP */
353   function save()
354   {
355    /* Depending on the baseobject (Ogroup / WS) we
356      *  use another set of objectClasses
357      * In case of TS itself, we use  "array("GOhard");"
358      * if we are currently editing from ogroup menu we use (array("goTerminalTemplate"))
359      */
360     if(isset($this->parent->by_object['ogroup'])){
361       $this->objectclasses = array("gotoTerminalTemplate");
362     }elseif(isset($this->parent->by_object['termgeneric'])){
363       $this->objectclasses = array("GOhard");
364     }else{
365       msg_dialog::display(_("Fatal error"),
366           "Object Type Configuration is unknown. Please contact the GOsa developers.",
367           FATAL_ERROR_DIALOG);
368       exit();
369     }
371     /* Find proper terminal path for tftp configuration
372        FIXME: This is suboptimal when the default has changed to
373        another location! */
374     if ($this->gotoTerminalPath == "default-inherit"){
375       $ldap= $this->config->get_ldap_link();
377       /* Strip relevant part from dn, keep trailing ',' */
378       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalRDN')."/i", "", $this->dn);
379       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
381       /* Walk from top to base and try to load default values for
382          'gotoTerminalPath'. Abort when an entry is found. */
383       while (TRUE){
384         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
386         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
387             $this->config->current['BASE'], array('gotoTerminalPath'));
388         $attrs= $ldap->fetch();
389         if (isset($attrs['gotoTerminalPath'])){
390           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
391           break;
392         }
394         /* Nothing left? */
395         if ($tmp == ""){
396           break;
397         }
398       }
399     }
400     
401     plugin::save();
403     /* Add missing arrays */
404     foreach (array("gotoModules") as $val){
405       if (isset ($this->$val) && count ($this->$val) != 0){
406     
407         $this->attrs["$val"]= array_unique($this->$val);
408       }
409       if(!isset($this->attrs["$val"])){
410        $this->attrs["$val"]=array();
411       }
412     }
414     /* Prepare list of ldap servers */
415     $this->attrs['gotoLdapServer'] = array();
416     if(!$this->gotoLdap_inherit){
417       $i = 0;
418       foreach($this->gotoLdapServers as $server){
419         $i ++;
420         $this->attrs['gotoLdapServer'][] = $i.":".$server;
421       }
422     }
424     /* Strip out 'default' values */
425     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
426             $this->attrs['gotoBootKernel']= array();
427     }
429      /* prepare share settings */
430     $tmp = array();
431     foreach($this->gotoShares as $name => $settings){
432       $tmp2 = split("\|",$name);
433       $name = $tmp2[0];
434       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
435     }
436     $this->attrs['gotoShare']=$tmp;
438     /* Write back to ldap */
439     $ldap= $this->config->get_ldap_link();
440     $ldap->cd($this->dn);
441     
442     $this->cleanup();
443     $ldap->modify ($this->attrs); 
445     new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
447     if (!$ldap->success()){
448       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
449     }
450     $this->handle_post_events("modify");
451   }
453   /* Add value to array, check if unique */
454   function add_list (&$array, $value)
455   {
456     if ($value != ""){
457       $array[]= $value;
458       sort($array);
459       array_unique ($array);
460     }
461   }
464   /* Delete value to array, check if unique */
465   function del_list (&$array, $list)
466   {
467     $tmp= array();
468     foreach ($array as $mod){
469       if (!in_array($mod, $list)){
470         $tmp[]= $mod;
471       }
472     }
473     $array= $tmp;
474   }
476    /* Generate ListBox frindly output for the defined shares
477    * Possibly Add or remove an attribute here,
478    */
479   function printOutAssignedShares()
480   {
481     $a_return = array();
482     if(is_array($this->gotoShares)){
483       foreach($this->gotoShares as $share){
484         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
485       }
486     }
487     return($a_return);
488   }
491   function PrepareForCopyPaste($source)
492   {
493     plugin::PrepareForCopyPaste($source);
495     $source_o = new termstartup ($this->config, $source['dn']);
497     foreach(array("gotoModules", "gotoKernelParameters","gotoShares","gotoKernelParameters","gotoTerminalPath","gotoShares","goLdapServerList","gotoBootKernel","gotoLdapServer","gotoBootKernels") as $attr){
498       $this->$attr = $source_o->$attr;
499     }
500   }
503   function array_switch_item($ar,$from,$to)
504   {
505     if(!is_array($ar)){
506       return(false);
507     }
508     if(!isset($ar[$from])){
509       return(false);
510     }
511     if(!isset($ar[$to])){
512       return(false);
513     }
515     $tmp = $ar[$from];
516     $ar[$from] = $ar[$to];
517     $ar[$to] = $tmp;
518     return($ar);
519   }
522   /* Return plugin informations for acl handling */
523   static function plInfo()
524   {
525     return (array(
526           "plShortName"   => _("Startup"),
527           "plDescription" => _("Terminal startup"),
528           "plSelfModify"  => FALSE,
529           "plDepends"     => array(),
530           "plPriority"    => 5,
531           "plSection"     => array("administration"),
532           "plCategory"    => array("terminal"),
534           "plProvidedAcls"=> array(
535             "gotoLdapServer"      => _("Ldap server"),
536             "gotoShare"           => _("Shares"),
537             "gotoModules"         => _("Kernel modules"),
538             "gotoBootKernel"      => _("Boot kernel"), 
539             "gotoKernelParameters"=> _("Kernel parameter"))
540           ));
541   }
546 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
547 ?>