Code

5bfe63424829bfb9c29cbc9ed582b80543dbf893
[gosa.git] / plugins / admin / systems / class_goShareServer.inc
1 <?php
3 require_once("class_goService.inc");
5 class goShareServer extends goService{
7   var $cli_summary      = "This plugin is used within the ServerService Pluign \nand indicates that this server supports shares.";
8   var $cli_description  = "Some longer text\nfor help";
9   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
11   /* This plugin only writes its objectClass */
12   var $objectclasses    = array("goShareServer");
13   var $attributes       = array("goExportEntry");
14   var $StatusFlag       = "goShareServerStatus";
16   /* This class can't be assigned twice so it conflicts with itsself */
17   var $conflicts        = array("goShareServer");
19   var $DisplayName      = "";
20   var $dn               = NULL;
21   var $cn                   = "";
22   var $goShareServerStatus  = "";
23   var $goExportEntry        = array();
24   var $allow_mounts         = false;
25   var $mounts_to_remove     = array();
26   var $mounts_to_add        = array();
28   function goShareServer($config,$dn)
29   {
30     plugin::plugin($config,$dn);
32     $this->DisplayName = _("File service");
34     $tmp =array();
35     if(isset($this->attrs['goExportEntry'])){
36       if(isset($this->attrs['goExportEntry']['count'])){
37         for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
38           $entry= $this->attrs['goExportEntry'][$i];
39           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
40         }
41       }
42     } 
43     $this->goExportEntryList = $tmp;
45     $ldap = $this->config->get_ldap_link();
46     $avl_objectclasses = $ldap->get_objectclasses();
47     if (isset($avl_objectclasses["mount"])) {
48       $this->allow_mounts = true;
49     }
50   }
53   function execute()
54   { 
55     $smarty = get_smarty(); 
58     if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList']))){
59       if($this->allow_mounts){
60         foreach($_POST['goExportEntryList'] as $entry){
61           $this->deleteFromMountList($this->goExportEntryList[$entry]);
62         }
63       }
64       foreach($_POST['goExportEntryList'] as $entry){
65         $this->deleteFromList($entry);
66       }
67     }
69     if(isset($_POST['NewNfsAdd'])){
70       $this->oldone = NULL;
71       $this->o_subWindow = new servnfs($this->config, $this);
72       $this->o_subWindow->set_acl_category("server");
73       $this->o_subWindow->set_acl_base($this->dn);
74       $this->dialog = true;
75     }
77     if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
78       $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
79       $add_mount=isset($this->mounts_to_add[$entry]);
80       $this->oldone=$entry;
81       $this->o_subWindow = new servnfs($this->config,$this,$entry,$add_mount);
82       $this->o_subWindow->set_acl_base($this->dn);
83       $this->o_subWindow->set_acl_category("server");
84       $this->dialog = true;
85     }
86     if(isset($this->o_subWindow)){
87       $this->o_subWindow->save_object(TRUE);
88     }
90     /* Save NFS setup */
91     if(isset($_POST['NFSsave'])){
92       if(count($this->o_subWindow->check())>0){
93         foreach($this->o_subWindow->check() as $msg) {
94           print_red($msg);
95         }
96       }else{
97         $this->o_subWindow->save_object();
98         $newone = $this->o_subWindow->save();
99         $this->addToList($newone);
100         if($this->allow_mounts){
101           if($this->oldone) {
102             $this->deleteFromMountList($this->oldone);
103           }
104           if ($this->o_subWindow->should_create_mount()) {
105             $this->addToMountList($newone);
106           }
107         }
108         unset($this->o_subWindow);
109         $this->dialog = false;
110       }
111     }
113     /* Cancel NFS setup */
114     if(isset($_POST['NFScancel'])){
115       $this->oldone = NULL;
116       unset($this->o_subWindow);
117       $this->dialog = false;
118     }
120     /* Execute NFS setup dialog*/
121     if(isset($this->o_subWindow)){
122       return $this->o_subWindow->execute();
123     }
125     foreach($this->attributes as $attr){
126       $smarty->assign($attr,$this->$attr);
127     }
129     /* Set acls */
130     $tmp = $this->plInfo();
131     $acl = ""; 
132     foreach($tmp['plProvidedAcls'] as $name => $translated){
133       $acl .= $this->getacl($name);
134     }
135     $smarty->assign("goExportEntryACL",$acl);
136     $smarty->assign("createable",$this->acl_is_createable());
137     $smarty->assign("removeable",$this->acl_is_removeable());
139     $tellSmarty= array();
140     ksort($this->goExportEntryList);
141     foreach($this->goExportEntryList as $name=>$values){
142       $tmp = split("\|",$values);
143       $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
144     }
145     $smarty->assign("goExportEntry",array_keys($tellSmarty));
146     $smarty->assign("goExportEntryKeys",($tellSmarty));
147     return($smarty->fetch(get_template_path("goShareServer.tpl",TRUE,dirname(__FILE__))));
148   }
151   function getListEntry()
152   {
153     $fields = goService::getListEntry();
154     $fields['Message']    = _("File service (Shares)");
155     $fields['AllowEdit']  = true;
156     return($fields);
157   }
160   function save()
161   {
162     plugin::save();
164     /* Arrays */
165     foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
166       $this->attrs[$destination]= array();
167       foreach ($this->$source as $element){
168         $this->attrs[$destination][]= $element;
169       }
170     }
173     /* Process netatalk mounts */
174     if($this->allow_mounts) {
175       $this->process_mounts();
176     }
178     /* Check if this is a new entry ... add/modify */
179     $ldap = $this->config->get_ldap_link();
180     $ldap->cat($this->dn,array("objectClass"));
181     if($ldap->count()){
182       $ldap->cd($this->dn);
183       $ldap->modify($this->attrs);
184     }else{
185       $ldap->cd($this->dn);
186       $ldap->add($this->attrs);
187     }
188     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/shares with dn '%s' failed."),$this->dn));
189     if($this->initially_was_account){
190       $this->handle_post_events("modify");
191     }else{
192       $this->handle_post_events("add");
193     }
194   }
197   function check()
198   { 
199     $message = plugin::check();
200     return($message);
201   }
204   function save_object()
205   {
206     if(isset($_POST['goShareServerPosted'])){
207       plugin::save_object();
208     }
209   } 
211   function addToList($entry){
212     $key =  key($entry);
213     $this->goExportEntryList[$key]=$entry[$key];
214   }
216   function deleteFromList($id){
217     unset($this->goExportEntryList[$id]);
218   }
220    function process_mounts() {
222     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
223     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
225     $mounts = array(
226       "objectClass" => "container",
227       "cn" => "mounts"
228     );
230     # load data from mounts container
231     $ldap = $this->config->get_ldap_link();
232     $ldap->cat($mountsdn, array('dn'));
233     $attrs = $ldap->fetch();
235     # mounts container not present yet, so we create it
236     if (count($attrs) == 0) {
237         $ldap->cd($mountsdn);
238         $ldap->add($mounts);
239         show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
240         gosa_log("Mount container '$mountsdn' has been created");
241     }
243     # remove deleted mounts from the container
244     foreach ($this->mounts_to_remove as $entry) {
245       $mount=$this->returnMountEntry($entry);
246       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
248       $ldap->cat($mountdn, array('dn'));
249       $attrs = $ldap->fetch();
251       if (count($attrs) != 0) {
252         $ldap->rmdir($mountdn);
253         show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
254         gosa_log("Mount object '".$mountdn."' has been removed");
255       }
256     }
258     # add new mounts to the container
259     foreach ($this->mounts_to_add as $entry) {
260       $mount=$this->returnMountEntry($entry);
261       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
262       $ldap->cd($mountdn);
263       $ldap->add($mount);
264       show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
265       gosa_log("Mount object '".$mountdn."' has been added");
266     }
267   }
269   function addToMountList($entry) 
270   {
271     $key =  key($entry);
272     $type = $this->get_share_type($entry[$key]);
273     if (($type == "netatalk") || ($type == "NFS")) {
274       $this->mounts_to_add[$entry[$key]] = $entry[$key];
275       unset($this->mounts_to_remove[$entry[$key]]);
276     }
277   }
279   function deleteFromMountList($entry) 
280   {
281     $type = $this->get_share_type($entry);
282     if (($type == "netatalk") || ($type == "NFS")) {
283       $this->mounts_to_remove[$entry] = $entry;
284       unset($this->mounts_to_add[$entry]);
285     }
286   }
288   function get_share_type($share) 
289   {
290     $tmp = split("\|", $share);
291     return $tmp[2];
292   }
294   function returnMountEntry($entry)
295   {
296     $item = split("\|", $entry);
297     $name = $item[0];
298     $description = $item[1];
299     $type = $item[2];
300     $charset = $item[3];
301     $path = $item[4];
302     $options = $item[5];
304     switch ($type) {
305       case "netatalk" : {
306         $mount = array(
307             "mountDirectory" => "/Network/Servers/",
308             "mountOption" => array(
309               "net",
310               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
311               ),
312             "mountType" => "url",
313             "objectClass" => "mount",
314             "cn" => $this->cn .":/".$name
315             );
316         break;
317       }
318       case "NFS" : {
319         $mount = array(
320             "mountDirectory" => "/Network/Servers/",
321             "mountOption" => "net",
322             "mountType" => "nfs",
323             "objectClass" => "mount",
324             "cn" => $this->cn .":".$path
325             );
326         break;
327       }
328       default : {
329                   continue;
330                 }
331     }
332     return $mount;
333   }
336   /* Return plugin informations for acl handling */
337   function plInfo()
338   {
339     return (array(
340           "plShortName"   => _("Shares"),
341           "plDescription" => _("Share service"),
342           "plSelfModify"  => FALSE,
343           "plDepends"     => array(),
344           "plPriority"    => 0,
345           "plSection"     => array("administration"),
346           "plCategory"    => array("server"),
348           "plProvidedAcls"=> array(
349               "name"        => _("Name"),
350               "netatalkmount" => _("Apple mounts"),
351               "description" => _("Description"),
352               "type"        => _("Type"),
353               "charset"     => _("Charset"),
354               "path"        => _("Path"),
355               "option"      => _("Option"),
356               "volume"      => _("Volume"))
358           ));
359   }
363 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
364 ?>