Code

c55ce9824b8192c538616c920504c26b44fc6bdb
[gosa.git] / trunk / 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     /* 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));
141           
142           if(!empty($buffer)){
143           
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     }
159     
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>&nbsp;<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("gotoShareKeys",array_flip($this->printOutAssignedShares()));
243     /* Create divSelectBox for ldap server selection
244      */
245     $SelectBoxLdapServer = new divSelectBox("LdapServer");
246     $SelectBoxLdapServer->SetHeight(80);
248  /* Add new ldap server to the list */
249     if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
250       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
251         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
252         if(!in_array($to_add,$this->gotoLdapServers)){
253           $this->gotoLdapServers[] = $to_add;
254         }
255       }
256     }
258     /* Move ldap servers up and down */
259     if(!$this->gotoLdap_inherit){
260       foreach($_POST as $name => $value){
261         if(preg_match("/sort_ldap_up_/",$name)){
262           $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
263           $from =  $id;
264           $to   =  $id -1;
265           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
266           if($tmp){
267             $this->gotoLdapServers = $tmp;
268           }
269           break;
270         }
271         if(preg_match("/sort_ldap_down_/",$name)){
272           $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
273           $from =  $id;
274           $to   =  $id +1;
275           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
276           if($tmp){
277             $this->gotoLdapServers = $tmp;
278           }
279           break;
280         }
281         if(preg_match("/gotoLdapRemove_/",$name)){
282           $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
283           $value = $this->gotoLdapServers[$id];
284           $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
285           break;
286         }
287       }
288     }
289   /* Add Entries
290      */
291     foreach($this->gotoLdapServers as $key => $server){
292       if(!in_array($server,$this->gotoLdapServerList)){
293         $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
294       }
296       $SelectBoxLdapServer->AddEntry(
297           array(array("string" => $server),
298             array("string" =>
299               "<input class='center' type='image' src='images/lists/sort-up.png' name='sort_ldap_up_".$key."'>&nbsp;".
300               "<input class='center' type='image' src='images/lists/sort-down.png' name='sort_ldap_down_".$key."'>&nbsp;".
301               "<input class='center' type='image' src='images/lists/trash.png' name='gotoLdapRemove_".$key."'>",
302               "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
303     }
305     if($this->gotoLdap_inherit){
306       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
307     }else{
308       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
309     }
311     $list = array();
312     foreach($this->gotoLdapServerList as $key => $entry){
313       if(!in_array($entry,$this->gotoLdapServers)){
314         $list[$key] = $entry;
315       }
316     }
317     $smarty->assign("gotoLdapServers",    $SelectBoxLdapServer->DrawList());
318     $smarty->assign("gotoLdapServerList", $list);
319     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
320     $smarty->assign("JS",  session::get('js'));
322     foreach (array("gotoModules" ) as $val){
323       $smarty->assign("$val", $this->$val);
324     }
326     /* Values */
327     foreach(array("gotoBootKernel", "gotoKernelParameters") as $val){
328       $smarty->assign($val, $this->$val);
329     }
331     /* Show main page */
332     return($smarty->fetch (get_template_path('terminalStartup.tpl', TRUE,dirname(__FILE__))));
333   }
335   function remove_from_parent()
336   {
337     if($this->acl_is_removeable()){
338       $this->handle_post_events("remove");
339       new log("remove","terminal/".get_class($this),$this->dn,array_keys($this->attrs));
340     }
341   }
344   /* Save data to object */
345   function save_object()
346   {
347     plugin::save_object();
349     if(isset($_POST['TerminalStarttabPosted'])){
350       if(isset($_POST['gotoLdap_inherit'])){
351         $this->gotoLdap_inherit = TRUE;
352       }else{
353         $this->gotoLdap_inherit = FALSE;
354       }
355     }
356   }
359   /* Save to LDAP */
360   function save()
361   {
362    /* Depending on the baseobject (Ogroup / WS) we
363      *  use another set of objectClasses
364      * In case of TS itself, we use  "array("GOhard");"
365      * if we are currently editing from ogroup menu we use (array("goTerminalTemplate"))
366      */
367     if(isset($this->parent->by_object['ogroup'])){
368       $this->objectclasses = array("gotoTerminalTemplate");
369     }elseif(isset($this->parent->by_object['termgeneric'])){
370       $this->objectclasses = array("GOhard");
371     }else{
372       msg_dialog::display(_("Fatal error"),
373           "Object Type Configuration is unknown. Please contact the GOsa developers.",
374           FATAL_ERROR_DIALOG);
375       exit();
376     }
378     /* Find proper terminal path for tftp configuration
379        FIXME: This is suboptimal when the default has changed to
380        another location! */
381     if ($this->gotoTerminalPath == "default-inherit"){
382       $ldap= $this->config->get_ldap_link();
384       /* Strip relevant part from dn, keep trailing ',' */
385       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalRDN')."/i", "", $this->dn);
386       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
388       /* Walk from top to base and try to load default values for
389          'gotoTerminalPath'. Abort when an entry is found. */
390       while (TRUE){
391         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
393         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
394             $this->config->current['BASE'], array('gotoTerminalPath'));
395         $attrs= $ldap->fetch();
396         if (isset($attrs['gotoTerminalPath'])){
397           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
398           break;
399         }
401         /* Nothing left? */
402         if ($tmp == ""){
403           break;
404         }
405       }
406     }
407     
408     plugin::save();
410     /* Add missing arrays */
411     foreach (array("gotoModules") as $val){
412       if (isset ($this->$val) && count ($this->$val) != 0){
413     
414         $this->attrs["$val"]= array_unique($this->$val);
415       }
416       if(!isset($this->attrs["$val"])){
417        $this->attrs["$val"]=array();
418       }
419     }
421     /* Prepare list of ldap servers */
422     $this->attrs['gotoLdapServer'] = array();
423     if(!$this->gotoLdap_inherit){
424       $i = 0;
425       foreach($this->gotoLdapServers as $server){
426         $i ++;
427         $this->attrs['gotoLdapServer'][] = $i.":".$server;
428       }
429     }
431     /* Strip out 'default' values */
432     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
433             $this->attrs['gotoBootKernel']= array();
434     }
436      /* prepare share settings */
437     $tmp = array();
438     foreach($this->gotoShares as $name => $settings){
439       $tmp2 = split("\|",$name);
440       $name = $tmp2[0];
441       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
442     }
443     $this->attrs['gotoShare']=$tmp;
445     /* Write back to ldap */
446     $ldap= $this->config->get_ldap_link();
447     $ldap->cd($this->dn);
448     
449     $this->cleanup();
450     $ldap->modify ($this->attrs); 
452     new log("modify","terminal/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
454     if (!$ldap->success()){
455       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
456     }
457     $this->handle_post_events("modify");
458   }
460   /* Add value to array, check if unique */
461   function add_list (&$array, $value)
462   {
463     if ($value != ""){
464       $array[]= $value;
465       sort($array);
466       array_unique ($array);
467     }
468   }
471   /* Delete value to array, check if unique */
472   function del_list (&$array, $list)
473   {
474     $tmp= array();
475     foreach ($array as $mod){
476       if (!in_array($mod, $list)){
477         $tmp[]= $mod;
478       }
479     }
480     $array= $tmp;
481   }
483    /* Generate ListBox frindly output for the defined shares
484    * Possibly Add or remove an attribute here,
485    */
486   function printOutAssignedShares()
487   {
488     $a_return = array();
489     if(is_array($this->gotoShares)){
490       foreach($this->gotoShares as $share){
491         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
492       }
493     }
494     return($a_return);
495   }
498   function PrepareForCopyPaste($source)
499   {
500     plugin::PrepareForCopyPaste($source);
502     $source_o = new termstartup ($this->config, $source['dn']);
504     foreach(array("gotoModules", "gotoKernelParameters","gotoShares","gotoKernelParameters","gotoTerminalPath","gotoShares","goLdapServerList","gotoBootKernel","gotoLdapServer","gotoBootKernels") as $attr){
505       $this->$attr = $source_o->$attr;
506     }
507   }
510   function array_switch_item($ar,$from,$to)
511   {
512     if(!is_array($ar)){
513       return(false);
514     }
515     if(!isset($ar[$from])){
516       return(false);
517     }
518     if(!isset($ar[$to])){
519       return(false);
520     }
522     $tmp = $ar[$from];
523     $ar[$from] = $ar[$to];
524     $ar[$to] = $tmp;
525     return($ar);
526   }
529   /* Return plugin informations for acl handling */
530   static function plInfo()
531   {
532     return (array(
533           "plShortName"   => _("Startup"),
534           "plDescription" => _("Terminal startup"),
535           "plSelfModify"  => FALSE,
536           "plDepends"     => array(),
537           "plPriority"    => 5,
538           "plSection"     => array("administration"),
539           "plCategory"    => array("terminal"),
541           "plProvidedAcls"=> array(
542             "gotoLdapServer"      => _("Ldap server"),
543             "gotoShare"           => _("Shares"),
544             "gotoModules"         => _("Kernel modules"),
545             "gotoBootKernel"      => _("Boot kernel"), 
546             "gotoKernelParameters"=> _("Kernel parameter"))
547           ));
548   }
553 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
554 ?>