Code

* Created "old" branch and moved stuff
[gosa.git] / branches / old / gosa-plugins / goto / admin / systems / services / shares / class_goShareServer.inc
1 <?php
3 class goShareServer extends goService{
5   var $cli_summary      = "This plugin is used within the ServerService Pluign \nand indicates that this server supports shares.";
6   var $cli_description  = "Some longer text\nfor help";
7   var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* This plugin only writes its objectClass */
10   var $objectclasses    = array("goShareServer");
11   var $attributes       = array("goExportEntry");
12   var $StatusFlag       = "goShareServerStatus";
14   /* This class can't be assigned twice so it conflicts with itsself */
15   var $conflicts        = array("goShareServer");
17   var $DisplayName      = "";
18   var $dn               = NULL;
19   var $cn                   = "";
20   var $goShareServerStatus  = "";
21   var $goExportEntry        = array();
22   var $allow_mounts         = false;
23   var $mounts_to_remove     = array();
24   var $mounts_to_add        = array();
25   var $view_logged  =FALSE;
27   function goShareServer(&$config,$dn)
28   {
29     goService::goService($config,$dn);
31     $this->DisplayName = _("File service (Shares)");
33     $tmp =array();
34     if(isset($this->attrs['goExportEntry'])){
35       if(isset($this->attrs['goExportEntry']['count'])){
36         for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
37           $entry= $this->attrs['goExportEntry'][$i];
38           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
39         }
40       }
41     } 
42     $this->goExportEntryList = $tmp;
44     $ldap = $this->config->get_ldap_link();
45     $avl_objectclasses = $ldap->get_objectclasses();
46     if (isset($avl_objectclasses["mount"])) {
47       $this->allow_mounts = true;
48     }
49   }
52   function execute()
53   { 
54     $smarty = get_smarty(); 
56     if($this->is_account && !$this->view_logged){
57       $this->view_logged = TRUE;
58       new log("view","server/".get_class($this),$this->dn);
59     }
62     if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList'])) && ($this->acl_is_writeable("name"))){
63       if($this->allow_mounts){
64         foreach($_POST['goExportEntryList'] as $entry){
65           $this->deleteFromMountList($this->goExportEntryList[$entry]);
66         }
67       }
68       foreach($_POST['goExportEntryList'] as $entry){
69         $this->deleteFromList($entry);
70       }
71     }
73     if(isset($_POST['NewNfsAdd']) && ($this->acl_is_writeable("name"))){
74       $this->oldone = NULL;
75       $this->o_subWindow = new servnfs($this->config, $this);
76       $this->o_subWindow->set_acl_category("server");
77       $this->o_subWindow->set_acl_base($this->dn);
78       $this->dialog = true;
79     }
81     if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
82       $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
83       $add_mount=isset($this->mounts_to_add[$entry]);
84       $this->oldone=$entry;
85       $this->o_subWindow = new servnfs($this->config,$this,$entry,$add_mount);
86       $this->o_subWindow->set_acl_base($this->dn);
87       $this->o_subWindow->set_acl_category("server");
88       $this->dialog = true;
89     }
90     if(isset($this->o_subWindow)){
91       $this->o_subWindow->save_object(TRUE);
92     }
94     /* Save NFS setup */
95     if(isset($_POST['NFSsave']) && isset($this->o_subWindow) && is_object($this->o_subWindow)){
96       if(count($this->o_subWindow->check())>0){
97         foreach($this->o_subWindow->check() as $msg) {
98           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
99         }
100       }else{
101         $this->o_subWindow->save_object();
102         $newone = $this->o_subWindow->save();
104         $this->addToList($newone);
105         if($this->allow_mounts){
106           if($this->oldone != NULL) {
107             $this->deleteFromMountList($this->oldone);
108           }
109           if ($this->o_subWindow->should_create_mount()) {
110             $this->addToMountList($newone);
111           }
112         }
113         unset($this->o_subWindow);
114         $this->dialog = false;
115       }
116     }
118     /* Cancel NFS setup */
119     if(isset($_POST['NFScancel'])){
120       $this->oldone = NULL;
121       unset($this->o_subWindow);
122       $this->dialog = false;
123     }
125     /* Execute NFS setup dialog*/
126     if(isset($this->o_subWindow)){
127       return $this->o_subWindow->execute();
128     }
130     foreach($this->attributes as $attr){
131       $smarty->assign($attr,$this->$attr);
132     }
134     /* Set acls */
135     $tmp = $this->plInfo();
136     foreach($tmp['plProvidedAcls'] as $name => $translated){
137       $smarty->assign($name."ACL",$this->getacl($name));
138     }
139     $smarty->assign("createable",$this->acl_is_createable());
140     $smarty->assign("removeable",$this->acl_is_removeable());
142     $tellSmarty= array();
143     ksort($this->goExportEntryList);
144     foreach($this->goExportEntryList as $name=>$values){
145       $tmp = split("\|",$values);
146       $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
147     }
148     $smarty->assign("goExportEntry",array_keys($tellSmarty));
149     $smarty->assign("goExportEntryKeys",($tellSmarty));
150     return($smarty->fetch(get_template_path("goShareServer.tpl",TRUE,dirname(__FILE__))));
151   }
154   function getListEntry()
155   {
156     $fields = goService::getListEntry();
157     $fields['Message']    = _("File service (Shares)");
158     #$fields['AllowEdit']  = true;
159     return($fields);
160   }
163   function save()
164   {
165     plugin::save();
167     /* Arrays */
168     foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
169       $this->attrs[$destination]= array();
170       foreach ($this->$source as $element){
171         $this->attrs[$destination][]= $element;
172       }
173     }
176     /* Process netatalk mounts */
177     if($this->allow_mounts) {
178       $this->process_mounts();
179     }
181     /* Check if this is a new entry ... add/modify */
182     $ldap = $this->config->get_ldap_link();
183     $ldap->cat($this->dn,array("objectClass"));
184     if($ldap->count()){
185       $ldap->cd($this->dn);
186       $ldap->modify($this->attrs);
187     }else{
188       $ldap->cd($this->dn);
189       $ldap->add($this->attrs);
190     }
191     if (!$ldap->success()){
192       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
193     }
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     if ($ldap->count != 0){
230       while ($attrs= $ldap->fetch()){
231         $obj[$ldap->getDN()]= $attrs['cn'][0];
232       }
233       msg_dialog::display(_("Error"), msgPool::stillInUse(_("share"), msgPool::buildList($obj)), ERROR_DIALOG);
235     } else {
236       /* Finally remove it */
237       unset($this->goExportEntryList[$id]);
238     }
239   }
241    function process_mounts() {
243     $clip = "cn=" . $this->cn . ",".get_ou('serverou');
244     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
246     $mounts = array(
247       "objectClass" => "container",
248       "cn" => "mounts"
249     );
251     # load data from mounts container
252     $ldap = $this->config->get_ldap_link();
253     $ldap->cat($mountsdn, array('dn'));
254     $attrs = $ldap->fetch();
256     # mounts container not present yet, so we create it
257     if (count($attrs) == 0) {
258         $ldap->cd($mountsdn);
259         $ldap->add($mounts);
260         if (!$ldap->success()){
261           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
262         }
263         new log("modify","server/".get_class($this),$mountsdn,array_keys($mounts),$ldap->get_error());
264     }
266     # remove deleted mounts from the container
267     foreach ($this->mounts_to_remove as $entry) {
268       $mount=$this->returnMountEntry($entry);
269       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
271       $ldap->cat($mountdn, array('dn'));
272       $attrs = $ldap->fetch();
274       if (count($attrs) != 0) {
275         $ldap->rmdir($mountdn);
276         if (!$ldap->success()){
277           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mountdn, LDAP_DEL, get_class()));
278         }
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       if (!$ldap->success()){
290         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mount, LDAP_ADD, get_class()));
291       }
292       new log("create","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
293     }
294   }
296   function addToMountList($entry) 
297   {
298     if($this->acl_is_writeable("name")){
299       $key =  key($entry);
300       $type = $this->get_share_type($entry[$key]);
301       if (($type == "netatalk") || ($type == "NFS")) {
302         $this->mounts_to_add[$entry[$key]] = $entry[$key];
303         unset($this->mounts_to_remove[$entry[$key]]);
304       }
305     }
306   }
308   function deleteFromMountList($entry) 
309   {
310     if($this->acl_is_writeable("name")){
311       $type = $this->get_share_type($entry);
312       if (($type == "netatalk") || ($type == "NFS")) {
313         $this->mounts_to_remove[$entry] = $entry;
314         unset($this->mounts_to_add[$entry]);
315       }
316     }
317   }
319   function get_share_type($share) 
320   {
321     $tmp = split("\|", $share);
322     return $tmp[2];
323   }
325   function returnMountEntry($entry)
326   {
327     $item = split("\|", $entry);
328     $name = $item[0];
329     $description = $item[1];
330     $type = $item[2];
331     $charset = $item[3];
332     $path = $item[4];
333     $options = $item[5];
335     switch ($type) {
336       case "netatalk" : {
337         $mount = array(
338             "mountDirectory" => "/Network/Servers/",
339             "mountOption" => array(
340               "net",
341               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
342               ),
343             "mountType" => "url",
344             "objectClass" => "mount",
345             "cn" => $this->cn .":/".$name
346             );
347         break;
348       }
349       case "NFS" : {
350         $mount = array(
351             "mountDirectory" => "/Network/Servers/",
352             "mountOption" => "net",
353             "mountType" => "nfs",
354             "objectClass" => "mount",
355             "cn" => $this->cn .":".$path
356             );
357         break;
358       }
359       default : {
360                   continue;
361                 }
362     }
363     return $mount;
364   }
367   function PrepareForCopyPaste($source)
368   {
369     plugin::PrepareForCopyPaste($source);
371     $tmp =array();
372     if(isset($source['goExportEntry'])){
373       if(isset($source['goExportEntry']['count'])){
374         for($i= 0; $i<$source['goExportEntry']['count']; $i++){
375           $entry= $source['goExportEntry'][$i];
376           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
377         }
378       }
379     }
380     $this->goExportEntryList = $tmp;
381     $this->goExportEntry = $tmp;
382   }
385   /* Return plugin informations for acl handling */
386   static function plInfo()
387   {
388     return (array(
389           "plShortName"   => _("File service (Shares)"),
390           "plDescription" => _("File service - Shares")." ("._("Services").")",
391           "plSelfModify"  => FALSE,
392           "plDepends"     => array(),
393           "plPriority"    => 90,
394           "plSection"     => array("administration"),
395           "plCategory"    => array("server"),
397           "plProvidedAcls"=> array(
398               "name"        => _("Name"),
399               "netatalkmount" => _("Apple mounts"),
400               "description" => _("Description"),
401               "type"        => _("Type"),
402               "charset"     => _("Charset"),
403               "path"        => _("Path"),
404               "option"      => _("Option"),
405               "volume"      => _("Volume"))
407           ));
408   }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>