Code

Fixed some sprintf
[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     unset($this->goExportEntryList[$id]);
217   }
219    function process_mounts() {
221     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
222     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
224     $mounts = array(
225       "objectClass" => "container",
226       "cn" => "mounts"
227     );
229     # load data from mounts container
230     $ldap = $this->config->get_ldap_link();
231     $ldap->cat($mountsdn, array('dn'));
232     $attrs = $ldap->fetch();
234     # mounts container not present yet, so we create it
235     if (count($attrs) == 0) {
236         $ldap->cd($mountsdn);
237         $ldap->add($mounts);
238         show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
239         gosa_log("Mount container '$mountsdn' has been created");
240     }
242     # remove deleted mounts from the container
243     foreach ($this->mounts_to_remove as $entry) {
244       $mount=$this->returnMountEntry($entry);
245       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
247       $ldap->cat($mountdn, array('dn'));
248       $attrs = $ldap->fetch();
250       if (count($attrs) != 0) {
251         $ldap->rmdir($mountdn);
252         show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
253         gosa_log("Mount object '".$mountdn."' has been removed");
254       }
255     }
257     # add new mounts to the container
258     foreach ($this->mounts_to_add as $entry) {
259       $mount=$this->returnMountEntry($entry);
260       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
261       $ldap->cd($mountdn);
262       $ldap->add($mount);
263       show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn)); 
264       gosa_log("Mount object '".$mountdn."' has been added");
265     }
266   }
268   function addToMountList($entry) 
269   {
270     if($this->acl_is_writeable("name")){
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     }
278   }
280   function deleteFromMountList($entry) 
281   {
282     if($this->acl_is_writeable("name")){
283       $type = $this->get_share_type($entry);
284       if (($type == "netatalk") || ($type == "NFS")) {
285         $this->mounts_to_remove[$entry] = $entry;
286         unset($this->mounts_to_add[$entry]);
287       }
288     }
289   }
291   function get_share_type($share) 
292   {
293     $tmp = split("\|", $share);
294     return $tmp[2];
295   }
297   function returnMountEntry($entry)
298   {
299     $item = split("\|", $entry);
300     $name = $item[0];
301     $description = $item[1];
302     $type = $item[2];
303     $charset = $item[3];
304     $path = $item[4];
305     $options = $item[5];
307     switch ($type) {
308       case "netatalk" : {
309         $mount = array(
310             "mountDirectory" => "/Network/Servers/",
311             "mountOption" => array(
312               "net",
313               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
314               ),
315             "mountType" => "url",
316             "objectClass" => "mount",
317             "cn" => $this->cn .":/".$name
318             );
319         break;
320       }
321       case "NFS" : {
322         $mount = array(
323             "mountDirectory" => "/Network/Servers/",
324             "mountOption" => "net",
325             "mountType" => "nfs",
326             "objectClass" => "mount",
327             "cn" => $this->cn .":".$path
328             );
329         break;
330       }
331       default : {
332                   continue;
333                 }
334     }
335     return $mount;
336   }
339   /* Return plugin informations for acl handling */
340   function plInfo()
341   {
342     return (array(
343           "plShortName"   => _("File service (Shares)"),
344           "plDescription" => _("File service - Shares")." ("._("Services").")",
345           "plSelfModify"  => FALSE,
346           "plDepends"     => array(),
347           "plPriority"    => 90,
348           "plSection"     => array("administration"),
349           "plCategory"    => array("server"),
351           "plProvidedAcls"=> array(
352               "name"        => _("Name"),
353               "netatalkmount" => _("Apple mounts"),
354               "description" => _("Description"),
355               "type"        => _("Type"),
356               "charset"     => _("Charset"),
357               "path"        => _("Path"),
358               "option"      => _("Option"),
359               "volume"      => _("Volume"))
361           ));
362   }
366 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
367 ?>