Code

Some server/terminal fixes
[gosa.git] / plugins / admin / systems / class_servService.inc
1 <?php
3 class servservice extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage server basic objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   var $goExportEntry    = array();
11   var $goExportEntryList= array();
12   var $goTimeSource     = array();
13   var $goLdapBase       = "";
14   var $goXdmcpIsEnabled = "";
15   var $goFontPath       = "";
16   var $goNTPServer      = "";
17   var $goShareServer    = false;
18   var $goLdapServer     = "";
19   var $goTerminalServer = "";
20   var $goSyslogServer   = "";
21   var $goCupsServer     = "";
22   var $goMailServer     = "";
23   var $o_subWindow      = NULL;
25   /* attribute list for save action */
26   var $ignore_account= TRUE;
27   var $attributes       = array("goLdapBase","goXdmcpIsEnabled","goFontPath","goExportEntry","goTimeSource");
28   var $possible_objectclasses= array( "goShareServer", "goNtpServer", "goServer", "GOhard", "goLdapServer",
29                                       "goTerminalServer", "goSyslogServer", "goCupsServer","goMailServer");
30   var $objectclasses    = array( "top","goServer", "GOhard"); 
31   var $additionaloc     = array( "goShareServer"     => array("goExportEntry"),
32                                  "goNtpServer"     => array("goTimeSource"),
33                                  "goLdapServer"    => array("goLdapBase"),
34                                  "goTerminalServer"=> array("goXdmcpIsEnabled", "goFontPath"),
35                                  "goSyslogServer"  => array(),
36                                  "goMailServer"    => array(),
37                                  "goCupsServer"    => array());
39   var $allow_mounts = false; //do we allow mount entries?
40   var $mounts_to_add = array();
41   var $mounts_to_remove = array();
42   var $oldone = NULL; //temp dave for old mount entry
44   function servservice ($config, $dn= NULL, $parent= NULL)
45   {
46     
47     plugin::plugin ($config, $dn, $parent);
48     
49     $ldap = $this->config->get_ldap_link();
50     $avl_objectclasses = $ldap->get_objectclasses();
51     if (isset($avl_objectclasses["mount"])) {
52         $this->allow_mounts = true;
53     }
54     
55     /* Assemble final object class list */
56     foreach ($this->additionaloc as $oc => $dummy){
57       if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
58         $this->objectclasses[$oc]= $oc;
59       }
60     }
62     /* Load arrays */
63     foreach (array("goTimeSource") as $name){
64       $tmp= array();
65       if (isset($this->attrs[$name])){
66         for ($i= 0; $i<$this->attrs[$name]['count']; $i++){
67           $tmp[$this->attrs[$name][$i]]= $this->attrs[$name][$i];
68         }
69       }
70       $this->$name= $tmp;
71     }
73     $tmp =array();
74     $tmp2=array();
75     if(isset($this->attrs['goExportEntry'])){
76       if(isset($this->attrs['goExportEntry']['count'])){
77         for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
78           $entry= $this->attrs['goExportEntry'][$i];
79           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
80         }
81       }
82     }
83     $this->goExportEntryList = $tmp;
85     /* Always is account... */
86     $this->is_account= TRUE;
88     /* Check if goShareServer is defined */
89     if((isset($this->attrs['objectClass']))&&(is_array($this->attrs['objectClass']))){ 
90       if(in_array("goShareServer",$this->attrs['objectClass'])){
91         $this->goShareServer = true;    
92       }
93     }
94   }
96   function addToList($entry){
97     $key =  key($entry);
98     $this->goExportEntryList[$key]=$entry[$key];
99   }
101   function deleteFromList($id){
102     /* Check if the share is used by someone */
103     $ldap = $this->config->get_ldap_link();
104     $ldap->cd($this->config->current['BASE']);
105     $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*))", array("cn"));
106     $cnt= $ldap->count();
107     if ($cnt){
108       $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
109       $msg.= "<br><br><ul>";
110       while ($attrs= $ldap->fetch()){
111         $msg.= "<li>".$attrs["cn"][0]."</li>";
112       }
113       $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
114       print_red($msg);
115       
116     } else {
117       /* Finally remove it */
118       unset($this->goExportEntryList[$id]);
119     }
120   }
121   
122   function addToMountList($entry) {
123     $key =  key($entry);
124     $type = $this->get_share_type($entry[$key]);
125     if (($type == "netatalk") || ($type == "NFS")) {
126         $this->mounts_to_add[$entry[$key]] = $entry[$key];
127       unset($this->mounts_to_remove[$entry[$key]]);
128     }
129   }
130   
131   function deleteFromMountList($entry) {
132     $type = $this->get_share_type($entry);
133     if (($type == "netatalk") || ($type == "NFS")) {
134       $this->mounts_to_remove[$entry] = $entry;
135       unset($this->mounts_to_add[$entry]);
136     }
137   }
139   function execute()
140   {
141     /* Call parent execute */
142     plugin::execute();
144     /* Fill templating stuff */
145     $smarty= get_smarty();
146     $smarty->assign("staticAddress", "");
148     if((isset($_POST['DelNfsEnt'])) && (isset($_POST['goExportEntryList'])) && chkacl($this->acl,"goExportEntry") == ""){
149       if($this->allow_mounts){
150         foreach($_POST['goExportEntryList'] as $entry){
151           $this->deleteFromMountList($this->goExportEntryList[$entry]);
152         }
153       }
154       foreach($_POST['goExportEntryList'] as $entry){
155         $this->deleteFromList($entry);
156       }
157     }
159     if(isset($_POST['NewNfsAdd']) && chkacl($this->acl,"goExportEntry") == ""){
160       $this->oldone = NULL;
161       $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
162       $this->dialog = true;
163     }
165     if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList'])) && chkacl($this->acl,"goExportEntry") == ""){
166       $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
167       $add_mount=isset($this->mounts_to_add[$entry]);
168       $this->oldone=$entry;
169       $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
170       $this->dialog = true;
171     }
173     if(isset($this->o_subWindow)){
174     $this->o_subWindow->save_object(TRUE);
175     }
177     /* Save NFS setup */
178     if(isset($_POST['NFSsave'])){
179       if(count($this->o_subWindow->check())>0){
180         foreach($this->o_subWindow->check() as $msg) {
181           print_red($msg);
182         }
183       }else{
184         $this->o_subWindow->save_object();
185         $newone = $this->o_subWindow->save();
186         $this->addToList($newone);
187         if($this->allow_mounts){
188           if($this->oldone) {
189               $this->deleteFromMountList($this->oldone);
190           }
191           if ($this->o_subWindow->should_create_mount()) {
192               $this->addToMountList($newone);
193           }
194         }
195         unset($this->o_subWindow);
196         $this->dialog = false;
197       }
198     }
199     
200     /* Cancel NFS setup */
201     if(isset($_POST['NFScancel'])){
202       $this->oldone = NULL;
203       unset($this->o_subWindow);
204       $this->dialog = false;
205     }
206   
207     /* Execute NFS setup dialog*/
208     if(isset($this->o_subWindow)){
209       return $this->o_subWindow->execute(); 
210     }
212     /* Here we add a new entry  */
213     if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "" && chkacl($this->acl,"goNtpServer") == "") {
214       $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
215       asort($this->goTimeSource);
216     }
218     /* Deleting an Entry, is a bit more complicated than adding one*/
219     if(isset($_POST['DelNTPEnt']) && chkacl($this->acl,"goNtpServer") == "") {
220       foreach ($_POST['goTimeSource'] as $entry){
221         if (isset($this->goTimeSource[$entry])){
222           unset($this->goTimeSource[$entry]);
223         }
224       }
225     }
227     /* Attributes */
228     foreach ($this->attributes as $attr){
229       $smarty->assign("$attr", $this->$attr);
230       $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
231       $smarty->assign($attr."State","");
232     }
233     
234     $tellSmarty=array();
235     ksort($this->goExportEntryList);
236     foreach($this->goExportEntryList as $name=>$values){
237        $tmp = split("\|",$values);
238        $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
239     }
240     $smarty->assign("goExportEntry",array_keys($tellSmarty));
241     $smarty->assign("goExportEntryKeys",($tellSmarty));
242     $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
244     $smarty->assign("goTimeSource", $this->goTimeSource);
245     $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
246     $smarty->assign("goTimeSourceState","");
247     
249     /* Classes... */
250     foreach ($this->additionaloc as $oc => $dummy){
251       if (isset($this->objectclasses[$oc])){
252         $smarty->assign("$oc", "checked");
253         $smarty->assign("$oc"."State", "");
254         $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
256       } else {
257         $smarty->assign("$oc", "");
258         $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
259         $smarty->assign("$oc"."State", "disabled");
260       }
261     }
263     if(!$this->goShareServer){
264       $smarty->assign("goShareServerState", " disabled ");
265       $smarty->assign("goExportEntryACL", " disabled ");
266     }else{
267       $smarty->assign("goShareServerState", "  ");
268       $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
269     }
271     /* Different handling for checkbox */
272     if($this->goXdmcpIsEnabled == "true"){
273       $smarty->assign("goXdmcpIsEnabled","checked");
274     } else {
275       $smarty->assign("goXdmcpIsEnabled","");
276     }
278     return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
279   }
282   function remove_from_parent()
283   {
284     /* This cannot be removed... */
285   }
288   /* Save data to object */
289   function save_object()
290   {
291     plugin::save_object();
292     if (isset($_POST['servicetab'])){
293       $tmp = $this->goTimeSource;
294   
295       if(isset($_POST['goLdapBase']) && chkacl($this->acl,"goLdapBase") == ""){
296         $this->goLdapBase = $_POST['goLdapBase'];
297       }
298   
299       /* Save checkbox state */
300       foreach ($this->additionaloc as $oc => $dummy){
301 //        if($oc == "goNtpServer") continue;
302         if (chkacl($this->acl, $oc) == ""){
303           if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
304             $this->objectclasses[$oc]= $oc;
305           } else {
306             unset($this->objectclasses[$oc]);
307           }
308         }
309       }
311       /* Save xdmcp is enabled flag */
312       if(chkacl($this->acl,"goXdmcpIsEnabled") == ""){
313         if (isset($_POST['goXdmcpIsEnabled'])){
314           $this->goXdmcpIsEnabled= "true";
315         } else {
316           $this->goXdmcpIsEnabled= "false";
317         }
318       }
319         
320       /* Save xdmcp is enabled flag */
321       if(chkacl($this->acl,"goExportEntry") == ""){
322         if (isset($_POST['goShareServer'])){
323           $this->goShareServer = true;
324         } else {
325           $this->goShareServer = false;
326         }
327       }
328       $this->goTimeSource = array();
329       $this->goTimeSource = $tmp;
330     }
331   }
334   /* Check supplied data */
335   function check()
336   {
337     /* Call common method to give check the hook */
338     $message= plugin::check();
339   
340     if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
341       $message[]=_("Terminal server, must have fontpath specified.");
342     }
344     return ($message);
345   }
348   /* Save to LDAP */
349   function save()
350   {
351     plugin::save();
353     $tmp= array();
355     /* Remove all from this plugin */
356     foreach($this->attrs['objectClass'] as $oc){
357       if (!in_array_ics($oc, $this->possible_objectclasses)){
358         $tmp[]= $oc;
359       }
360     }
361     
362     /* Merge our current objectclasses */
363     foreach($this->objectclasses as $oc){
364       if (!in_array_ics($oc, $tmp)){
365         $tmp[]= $oc;
366       }
367     }
369     /* Reassign cleaned value */
370     $this->attrs['objectClass']= $tmp;
372     /* Arrays */
373     foreach (array("goTimeSource"=>"goTimeSource", "goExportEntryList"=>"goExportEntry") as $source => $destination){
374       $this->attrs[$destination]= array();
375       foreach ($this->$source as $element){
376         $this->attrs[$destination][]= $element;
377       }
378     }
379     
380     /* Process netatalk mounts */
381     if($this->allow_mounts) {
382       $this->process_mounts();
383     }
384  
385     /* Remove illegal attributes */
386     foreach ($this->additionaloc as $oc => $attrs){
387       if (!in_array($oc, $this->objectclasses)){
388         foreach ($attrs as $attr){
389           $this->attrs[$attr]= array();
390         }
391       }
392     }
394     /* Write to LDAP */
395     $ldap= $this->config->get_ldap_link();
396     $ldap->cd($this->dn);
397     $this->cleanup();
398     $ldap->modify ($this->attrs); 
400     show_ldap_error($ldap->get_error(), _("Saving server service object failed"));
401     
402     /* Optionally execute a command after we're done */
403     if ($this->initially_was_account == $this->is_account){
404       if ($this->is_modified){
405         $this->handle_post_events("modify");
406       }
407     } else {
408       $this->handle_post_events("add");
409     }
410   }
411   
412   function process_mounts() {
413     
414     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
415     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
416     
417     $mounts = array(
418       "objectClass" => "container",
419       "cn" => "mounts"
420     );
421     
422     # load data from mounts container
423     $ldap = $this->config->get_ldap_link();
424     $ldap->cat($mountsdn, array('dn'));
425     $attrs = $ldap->fetch();
426     
427     # mounts container not present yet, so we create it
428     if (count($attrs) == 0) {
429         $ldap->cd($mountsdn);
430         $ldap->add($mounts);
431         show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
432         gosa_log("Mount container '$mountsdn' has been created");
433     }
435     # remove deleted mounts from the container
436     foreach ($this->mounts_to_remove as $entry) {
437       $mount=$this->returnMountEntry($entry);
438       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
440       $ldap->cat($mountdn, array('dn'));
441       $attrs = $ldap->fetch();
443       if (count($attrs) != 0) {
444         $ldap->rmdir($mountdn);
445         show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
446         gosa_log("Mount object '".$mountdn."' has been removed");
447       }
448     }
450     # add new mounts to the container
451     foreach ($this->mounts_to_add as $entry) {
453       $mount=$this->returnMountEntry($entry);
454       $mountdn = "cn=".$mount["cn"].","."$mountsdn";  
455       $ldap->cd($mountdn);
456       $ldap->add($mount);
457       show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
458       gosa_log("Mount object '".$mountdn."' has been added");
459     }
460   }
461   
462   function get_share_type($share) {
463     $tmp = split("\|", $share);
464     return $tmp[2];
465   }
468   function returnMountEntry($entry) {
469   $item = split("\|", $entry);
470     $name = $item[0];
471     $description = $item[1];
472     $type = $item[2];
473     $charset = $item[3];
474     $path = $item[4];
475     $options = $item[5];
476     
477     switch ($type) {
478       case "netatalk" : {
479             $mount = array(
480             "mountDirectory" => "/Network/Servers/",
481             "mountOption" => array(
482               "net",
483               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
484             ),
485             "mountType" => "url",
486             "objectClass" => "mount",
487               "cn" => $this->cn .":/".$name
488           );
489           break;
490       }
491       case "NFS" : {
492         $mount = array(
493             "mountDirectory" => "/Network/Servers/",
494             "mountOption" => "net",
495             "mountType" => "nfs",
496             "objectClass" => "mount",
497               "cn" => $this->cn .":".$path
498           );
499           break;
500       }        
501       default : {
502         continue;
503       }
504     }
505   return $mount;
506   }
509 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
510 ?>