Code

5f580dde93e6d69bc6555e0bbb4f843506252a28
[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'])) && ($this->acl_is_writeable("name"))){
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']) && ($this->acl_is_writeable("name"))){
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();
100         $this->addToList($newone);
101         if($this->allow_mounts){
102           if($this->oldone) {
103             $this->deleteFromMountList($this->oldone);
104           }
105           if ($this->o_subWindow->should_create_mount()) {
106             $this->addToMountList($newone);
107           }
108         }
109         unset($this->o_subWindow);
110         $this->dialog = false;
111       }
112     }
114     /* Cancel NFS setup */
115     if(isset($_POST['NFScancel'])){
116       $this->oldone = NULL;
117       unset($this->o_subWindow);
118       $this->dialog = false;
119     }
121     /* Execute NFS setup dialog*/
122     if(isset($this->o_subWindow)){
123       return $this->o_subWindow->execute();
124     }
126     foreach($this->attributes as $attr){
127       $smarty->assign($attr,$this->$attr);
128     }
130     /* Set acls */
131     $tmp = $this->plInfo();
132     foreach($tmp['plProvidedAcls'] as $name => $translated){
133       $smarty->assign($name."ACL",$this->getacl($name));
134     }
135     $smarty->assign("createable",$this->acl_is_createable());
136     $smarty->assign("removeable",$this->acl_is_removeable());
138     $tellSmarty= array();
139     ksort($this->goExportEntryList);
140     foreach($this->goExportEntryList as $name=>$values){
141       $tmp = split("\|",$values);
142       $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
143     }
144     $smarty->assign("goExportEntry",array_keys($tellSmarty));
145     $smarty->assign("goExportEntryKeys",($tellSmarty));
146     return($smarty->fetch(get_template_path("goShareServer.tpl",TRUE,dirname(__FILE__))));
147   }
150   function getListEntry()
151   {
152     $fields = goService::getListEntry();
153     $fields['Message']    = _("File service (Shares)");
154     $fields['AllowEdit']  = true;
155     return($fields);
156   }
159   function save()
160   {
161     plugin::save();
163     /* Arrays */
164     foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
165       $this->attrs[$destination]= array();
166       foreach ($this->$source as $element){
167         $this->attrs[$destination][]= $element;
168       }
169     }
172     /* Process netatalk mounts */
173     if($this->allow_mounts) {
174       $this->process_mounts();
175     }
177     /* Check if this is a new entry ... add/modify */
178     $ldap = $this->config->get_ldap_link();
179     $ldap->cat($this->dn,array("objectClass"));
180     if($ldap->count()){
181       $ldap->cd($this->dn);
182       $ldap->modify($this->attrs);
183     }else{
184       $ldap->cd($this->dn);
185       $ldap->add($this->attrs);
186     }
187     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/shares with dn '%s' failed."),$this->dn));
188     if($this->initially_was_account){
189       $this->handle_post_events("modify");
190     }else{
191       $this->handle_post_events("add");
192     }
193   }
196   function check()
197   { 
198     $message = plugin::check();
199     return($message);
200   }
203   function save_object()
204   {
205     if(isset($_POST['goShareServerPosted'])){
206       plugin::save_object();
207     }
208   } 
210   function addToList($entry){
211     $key =  key($entry);
212     $this->goExportEntryList[$key]=$entry[$key];
213   }
215   function deleteFromList($id)
216   {
217     /* Check if the share is used by someone */
218     $ldap = $this->config->get_ldap_link();
219     $ldap->cd($this->config->current['BASE']);
220     $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*))", array("cn"));
221     $cnt= $ldap->count();
222     if ($cnt){
223       $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
224       $msg.= "<br><br><ul>";
225       while ($attrs= $ldap->fetch()){
226         $msg.= "<li>".$attrs["cn"][0]."</li>";
227       }
228       $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
229       print_red($msg);
231     } else {
232       /* Finally remove it */
233       unset($this->goExportEntryList[$id]);
234     }
235   }
237    function process_mounts() {
239     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
240     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
242     $mounts = array(
243       "objectClass" => "container",
244       "cn" => "mounts"
245     );
247     # load data from mounts container
248     $ldap = $this->config->get_ldap_link();
249     $ldap->cat($mountsdn, array('dn'));
250     $attrs = $ldap->fetch();
252     # mounts container not present yet, so we create it
253     if (count($attrs) == 0) {
254         $ldap->cd($mountsdn);
255         $ldap->add($mounts);
256         show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
257         gosa_log("Mount container '$mountsdn' has been created");
258     }
260     # remove deleted mounts from the container
261     foreach ($this->mounts_to_remove as $entry) {
262       $mount=$this->returnMountEntry($entry);
263       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
265       $ldap->cat($mountdn, array('dn'));
266       $attrs = $ldap->fetch();
268       if (count($attrs) != 0) {
269         $ldap->rmdir($mountdn);
270         show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
271         gosa_log("Mount object '".$mountdn."' has been removed");
272       }
273     }
275     # add new mounts to the container
276     foreach ($this->mounts_to_add as $entry) {
277       $mount=$this->returnMountEntry($entry);
278       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
279       $ldap->cd($mountdn);
280       $ldap->add($mount);
281       show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
282       gosa_log("Mount object '".$mountdn."' has been added");
283     }
284   }
286   function addToMountList($entry) 
287   {
288     if($this->acl_is_writeable("name")){
289       $key =  key($entry);
290       $type = $this->get_share_type($entry[$key]);
291       if (($type == "netatalk") || ($type == "NFS")) {
292         $this->mounts_to_add[$entry[$key]] = $entry[$key];
293         unset($this->mounts_to_remove[$entry[$key]]);
294       }
295     }
296   }
298   function deleteFromMountList($entry) 
299   {
300     if($this->acl_is_writeable("name")){
301       $type = $this->get_share_type($entry);
302       if (($type == "netatalk") || ($type == "NFS")) {
303         $this->mounts_to_remove[$entry] = $entry;
304         unset($this->mounts_to_add[$entry]);
305       }
306     }
307   }
309   function get_share_type($share) 
310   {
311     $tmp = split("\|", $share);
312     return $tmp[2];
313   }
315   function returnMountEntry($entry)
316   {
317     $item = split("\|", $entry);
318     $name = $item[0];
319     $description = $item[1];
320     $type = $item[2];
321     $charset = $item[3];
322     $path = $item[4];
323     $options = $item[5];
325     switch ($type) {
326       case "netatalk" : {
327         $mount = array(
328             "mountDirectory" => "/Network/Servers/",
329             "mountOption" => array(
330               "net",
331               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
332               ),
333             "mountType" => "url",
334             "objectClass" => "mount",
335             "cn" => $this->cn .":/".$name
336             );
337         break;
338       }
339       case "NFS" : {
340         $mount = array(
341             "mountDirectory" => "/Network/Servers/",
342             "mountOption" => "net",
343             "mountType" => "nfs",
344             "objectClass" => "mount",
345             "cn" => $this->cn .":".$path
346             );
347         break;
348       }
349       default : {
350                   continue;
351                 }
352     }
353     return $mount;
354   }
357   /* Return plugin informations for acl handling */
358   function plInfo()
359   {
360     return (array(
361           "plShortName"   => _("File service (Shares)"),
362           "plDescription" => _("File service - Shares")." ("._("Services").")",
363           "plSelfModify"  => FALSE,
364           "plDepends"     => array(),
365           "plPriority"    => 90,
366           "plSection"     => array("administration"),
367           "plCategory"    => array("server"),
369           "plProvidedAcls"=> array(
370               "name"        => _("Name"),
371               "netatalkmount" => _("Apple mounts"),
372               "description" => _("Description"),
373               "type"        => _("Type"),
374               "charset"     => _("Charset"),
375               "path"        => _("Path"),
376               "option"      => _("Option"),
377               "volume"      => _("Volume"))
379           ));
380   }
384 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
385 ?>