Code

6b7f39aa382c514633f11ddc96f9bfc75fe96d00
[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();
27   var $view_logged  =FALSE;
29   function goShareServer($config,$dn)
30   {
31     goService::goService($config,$dn);
33     $this->DisplayName = _("File service");
35     $tmp =array();
36     if(isset($this->attrs['goExportEntry'])){
37       if(isset($this->attrs['goExportEntry']['count'])){
38         for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
39           $entry= $this->attrs['goExportEntry'][$i];
40           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
41         }
42       }
43     } 
44     $this->goExportEntryList = $tmp;
46     $ldap = $this->config->get_ldap_link();
47     $avl_objectclasses = $ldap->get_objectclasses();
48     if (isset($avl_objectclasses["mount"])) {
49       $this->allow_mounts = true;
50     }
51   }
54   function execute()
55   { 
56     $smarty = get_smarty(); 
58     if($this->is_account && !$this->view_logged){
59       $this->view_logged = TRUE;
60       new log("view","server/".get_class($this),$this->dn);
61     }
64     if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList'])) && ($this->acl_is_writeable("name"))){
65       if($this->allow_mounts){
66         foreach($_POST['goExportEntryList'] as $entry){
67           $this->deleteFromMountList($this->goExportEntryList[$entry]);
68         }
69       }
70       foreach($_POST['goExportEntryList'] as $entry){
71         $this->deleteFromList($entry);
72       }
73     }
75     if(isset($_POST['NewNfsAdd']) && ($this->acl_is_writeable("name"))){
76       $this->oldone = NULL;
77       $this->o_subWindow = new servnfs($this->config, $this);
78       $this->o_subWindow->set_acl_category("server");
79       $this->o_subWindow->set_acl_base($this->dn);
80       $this->dialog = true;
81     }
83     if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
84       $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
85       $add_mount=isset($this->mounts_to_add[$entry]);
86       $this->oldone=$entry;
87       $this->o_subWindow = new servnfs($this->config,$this,$entry,$add_mount);
88       $this->o_subWindow->set_acl_base($this->dn);
89       $this->o_subWindow->set_acl_category("server");
90       $this->dialog = true;
91     }
92     if(isset($this->o_subWindow)){
93       $this->o_subWindow->save_object(TRUE);
94     }
96     /* Save NFS setup */
97     if(isset($_POST['NFSsave'])){
98       if(count($this->o_subWindow->check())>0){
99         foreach($this->o_subWindow->check() as $msg) {
100           print_red($msg);
101         }
102       }else{
103         $this->o_subWindow->save_object();
104         $newone = $this->o_subWindow->save();
106         $this->addToList($newone);
107         if($this->allow_mounts){
108           if($this->oldone) {
109             $this->deleteFromMountList($this->oldone);
110           }
111           if ($this->o_subWindow->should_create_mount()) {
112             $this->addToMountList($newone);
113           }
114         }
115         unset($this->o_subWindow);
116         $this->dialog = false;
117       }
118     }
120     /* Cancel NFS setup */
121     if(isset($_POST['NFScancel'])){
122       $this->oldone = NULL;
123       unset($this->o_subWindow);
124       $this->dialog = false;
125     }
127     /* Execute NFS setup dialog*/
128     if(isset($this->o_subWindow)){
129       return $this->o_subWindow->execute();
130     }
132     foreach($this->attributes as $attr){
133       $smarty->assign($attr,$this->$attr);
134     }
136     /* Set acls */
137     $tmp = $this->plInfo();
138     foreach($tmp['plProvidedAcls'] as $name => $translated){
139       $smarty->assign($name."ACL",$this->getacl($name));
140     }
141     $smarty->assign("createable",$this->acl_is_createable());
142     $smarty->assign("removeable",$this->acl_is_removeable());
144     $tellSmarty= array();
145     ksort($this->goExportEntryList);
146     foreach($this->goExportEntryList as $name=>$values){
147       $tmp = split("\|",$values);
148       $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
149     }
150     $smarty->assign("goExportEntry",array_keys($tellSmarty));
151     $smarty->assign("goExportEntryKeys",($tellSmarty));
152     return($smarty->fetch(get_template_path("goShareServer.tpl",TRUE,dirname(__FILE__))));
153   }
156   function getListEntry()
157   {
158     $fields = goService::getListEntry();
159     $fields['Message']    = _("File service (Shares)");
160     $fields['AllowEdit']  = true;
161     return($fields);
162   }
165   function save()
166   {
167     plugin::save();
169     /* Arrays */
170     foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
171       $this->attrs[$destination]= array();
172       foreach ($this->$source as $element){
173         $this->attrs[$destination][]= $element;
174       }
175     }
178     /* Process netatalk mounts */
179     if($this->allow_mounts) {
180       $this->process_mounts();
181     }
183     /* Check if this is a new entry ... add/modify */
184     $ldap = $this->config->get_ldap_link();
185     $ldap->cat($this->dn,array("objectClass"));
186     if($ldap->count()){
187       $ldap->cd($this->dn);
188       $ldap->modify($this->attrs);
189     }else{
190       $ldap->cd($this->dn);
191       $ldap->add($this->attrs);
192     }
193     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/shares with dn '%s' failed."),$this->dn));
194     if($this->initially_was_account){
195       $this->handle_post_events("modify");
196       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
197     }else{
198       $this->handle_post_events("add");
199       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
200     }
201   }
204   function check()
205   { 
206     $message = plugin::check();
207     return($message);
208   }
211   function save_object()
212   {
213     if(isset($_POST['goShareServerPosted'])){
214       plugin::save_object();
215     }
216   } 
218   function addToList($entry){
219     $key =  key($entry);
220     $this->goExportEntryList[$key]=$entry[$key];
221   }
223   function deleteFromList($id)
224   {
225     /* Check if the share is used by someone */
226     $ldap = $this->config->get_ldap_link();
227     $ldap->cd($this->config->current['BASE']);
228     $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*))", array("cn"));
229     $cnt= $ldap->count();
230     if ($cnt){
231       $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
232       $msg.= "<br><br><ul>";
233       while ($attrs= $ldap->fetch()){
234         $msg.= "<li>".$attrs["cn"][0]."</li>";
235       }
236       $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
237       print_red($msg);
239     } else {
240       /* Finally remove it */
241       unset($this->goExportEntryList[$id]);
242     }
243   }
245    function process_mounts() {
247     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
248     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
250     $mounts = array(
251       "objectClass" => "container",
252       "cn" => "mounts"
253     );
255     # load data from mounts container
256     $ldap = $this->config->get_ldap_link();
257     $ldap->cat($mountsdn, array('dn'));
258     $attrs = $ldap->fetch();
260     # mounts container not present yet, so we create it
261     if (count($attrs) == 0) {
262         $ldap->cd($mountsdn);
263         $ldap->add($mounts);
264         show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
265         new log("modify","server/".get_class($this),$mountsdn,array_keys($mounts),$ldap->get_error());
266     }
268     # remove deleted mounts from the container
269     foreach ($this->mounts_to_remove as $entry) {
270       $mount=$this->returnMountEntry($entry);
271       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
273       $ldap->cat($mountdn, array('dn'));
274       $attrs = $ldap->fetch();
276       if (count($attrs) != 0) {
277         $ldap->rmdir($mountdn);
278         show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
279         new log("remove","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
280       }
281     }
283     # add new mounts to the container
284     foreach ($this->mounts_to_add as $entry) {
285       $mount=$this->returnMountEntry($entry);
286       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
287       $ldap->cd($mountdn);
288       $ldap->add($mount);
289       show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
290       new log("create","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
291     }
292   }
294   function addToMountList($entry) 
295   {
296     if($this->acl_is_writeable("name")){
297       $key =  key($entry);
298       $type = $this->get_share_type($entry[$key]);
299       if (($type == "netatalk") || ($type == "NFS")) {
300         $this->mounts_to_add[$entry[$key]] = $entry[$key];
301         unset($this->mounts_to_remove[$entry[$key]]);
302       }
303     }
304   }
306   function deleteFromMountList($entry) 
307   {
308     if($this->acl_is_writeable("name")){
309       $type = $this->get_share_type($entry);
310       if (($type == "netatalk") || ($type == "NFS")) {
311         $this->mounts_to_remove[$entry] = $entry;
312         unset($this->mounts_to_add[$entry]);
313       }
314     }
315   }
317   function get_share_type($share) 
318   {
319     $tmp = split("\|", $share);
320     return $tmp[2];
321   }
323   function returnMountEntry($entry)
324   {
325     $item = split("\|", $entry);
326     $name = $item[0];
327     $description = $item[1];
328     $type = $item[2];
329     $charset = $item[3];
330     $path = $item[4];
331     $options = $item[5];
333     switch ($type) {
334       case "netatalk" : {
335         $mount = array(
336             "mountDirectory" => "/Network/Servers/",
337             "mountOption" => array(
338               "net",
339               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
340               ),
341             "mountType" => "url",
342             "objectClass" => "mount",
343             "cn" => $this->cn .":/".$name
344             );
345         break;
346       }
347       case "NFS" : {
348         $mount = array(
349             "mountDirectory" => "/Network/Servers/",
350             "mountOption" => "net",
351             "mountType" => "nfs",
352             "objectClass" => "mount",
353             "cn" => $this->cn .":".$path
354             );
355         break;
356       }
357       default : {
358                   continue;
359                 }
360     }
361     return $mount;
362   }
365   function PrepareForCopyPaste($source)
366   {
367     plugin::PrepareForCopyPaste($source);
369     $tmp =array();
370     if(isset($source['goExportEntry'])){
371       if(isset($source['goExportEntry']['count'])){
372         for($i= 0; $i<$source['goExportEntry']['count']; $i++){
373           $entry= $source['goExportEntry'][$i];
374           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
375         }
376       }
377     }
378     $this->goExportEntryList = $tmp;
379     $this->goExportEntry = $tmp;
380   }
383   /* Return plugin informations for acl handling */
384   function plInfo()
385   {
386     return (array(
387           "plShortName"   => _("File service (Shares)"),
388           "plDescription" => _("File service - Shares")." ("._("Services").")",
389           "plSelfModify"  => FALSE,
390           "plDepends"     => array(),
391           "plPriority"    => 90,
392           "plSection"     => array("administration"),
393           "plCategory"    => array("server"),
395           "plProvidedAcls"=> array(
396               "name"        => _("Name"),
397               "netatalkmount" => _("Apple mounts"),
398               "description" => _("Description"),
399               "type"        => _("Type"),
400               "charset"     => _("Charset"),
401               "path"        => _("Path"),
402               "option"      => _("Option"),
403               "volume"      => _("Volume"))
405           ));
406   }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>