Code

c85f79c9321f31f2d02b7feadc7f8fbf3964bce2
[gosa.git] / trunk / 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 allow_remove()
219   {
220   /* Check if the service is still in use */
221     $ldap = $this->config->get_ldap_link();
222     $ldap->cd($this->config->current['BASE']);
223     $cn = $ldap->getCn($this->dn);
224     $ldap->search("(&(|(objectClass=posixGroup)(objectClass=posixAccount)(objectClass=gosaGroupOfNames))(gotoShare=$cn|*))", array("dn"));
226     if($ldap->count()){
227       /* Number of entries shown in warning */
228       $i    = 3;
229       $str  = '<p>';
230       while(($attrs = $ldap->fetch()) && $i >= 0){
231         $i --;
232         if(isset($attrs['dn'])){
233           $str .= '<i>' . $attrs['dn']."</i><br/>";
234         }
235       }
236       $str .= '</p>';
237       return(sprintf(_("Cannot remove share - it is still in use by these objects: %s"), $str));
238       }
239   }
241   function addToList($entry){
242     $key =  key($entry);
243     $this->goExportEntryList[$key]=$entry[$key];
244   }
246   function deleteFromList($id)
247   {
248     /* Check if the share is used by someone */
249     $ldap = $this->config->get_ldap_link();
250     $ldap->cd($this->config->current['BASE']);
251     $cn = $ldap->getCn($this->dn);
252     $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=$cn|$id|*))", array("cn"));
253     if ($ldap->count() != 0){
254       while ($attrs= $ldap->fetch()){
255         $obj[$ldap->getDN()]= $attrs['cn'][0];
256       }
257       msg_dialog::display(_("Error"), msgPool::stillInUse(_("share"), msgPool::buildList($obj)), ERROR_DIALOG);
259     } else {
260       /* Finally remove it */
261       unset($this->goExportEntryList[$id]);
262     }
263   }
265    function process_mounts() {
267     $clip = "cn=" . $this->cn . ",".get_ou('serverRDN');
268     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
270     $mounts = array(
271       "objectClass" => "container",
272       "cn" => "mounts"
273     );
275     # load data from mounts container
276     $ldap = $this->config->get_ldap_link();
277     $ldap->cat($mountsdn, array('dn'));
278     $attrs = $ldap->fetch();
280     # mounts container not present yet, so we create it
281     if (count($attrs) == 0) {
282         $ldap->cd($mountsdn);
283         $ldap->add($mounts);
284         if (!$ldap->success()){
285           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
286         }
287         new log("modify","server/".get_class($this),$mountsdn,array_keys($mounts),$ldap->get_error());
288     }
290     # remove deleted mounts from the container
291     foreach ($this->mounts_to_remove as $entry) {
292       $mount=$this->returnMountEntry($entry);
293       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
295       $ldap->cat($mountdn, array('dn'));
296       $attrs = $ldap->fetch();
298       if (count($attrs) != 0) {
299         $ldap->rmdir($mountdn);
300         if (!$ldap->success()){
301           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mountdn, LDAP_DEL, get_class()));
302         }
303         new log("remove","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
304       }
305     }
307     # add new mounts to the container
308     foreach ($this->mounts_to_add as $entry) {
309       $mount=$this->returnMountEntry($entry);
310       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
311       $ldap->cd($mountdn);
312       $ldap->add($mount);
313       if (!$ldap->success()){
314         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mount, LDAP_ADD, get_class()));
315       }
316       new log("create","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
317     }
318   }
320   function addToMountList($entry) 
321   {
322     if($this->acl_is_writeable("name")){
323       $key =  key($entry);
324       $type = $this->get_share_type($entry[$key]);
325       if (($type == "netatalk") || ($type == "NFS")) {
326         $this->mounts_to_add[$entry[$key]] = $entry[$key];
327         unset($this->mounts_to_remove[$entry[$key]]);
328       }
329     }
330   }
332   function deleteFromMountList($entry) 
333   {
334     if($this->acl_is_writeable("name")){
335       $type = $this->get_share_type($entry);
336       if (($type == "netatalk") || ($type == "NFS")) {
337         $this->mounts_to_remove[$entry] = $entry;
338         unset($this->mounts_to_add[$entry]);
339       }
340     }
341   }
343   function get_share_type($share) 
344   {
345     $tmp = split("\|", $share);
346     return $tmp[2];
347   }
349   function returnMountEntry($entry)
350   {
351     $item = split("\|", $entry);
352     $name = $item[0];
353     $description = $item[1];
354     $type = $item[2];
355     $charset = $item[3];
356     $path = $item[4];
357     $options = $item[5];
359     switch ($type) {
360       case "netatalk" : {
361         $mount = array(
362             "mountDirectory" => "/Network/Servers/",
363             "mountOption" => array(
364               "net",
365               "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
366               ),
367             "mountType" => "url",
368             "objectClass" => "mount",
369             "cn" => $this->cn .":/".$name
370             );
371         break;
372       }
373       case "NFS" : {
374         $mount = array(
375             "mountDirectory" => "/Network/Servers/",
376             "mountOption" => "net",
377             "mountType" => "nfs",
378             "objectClass" => "mount",
379             "cn" => $this->cn .":".$path
380             );
381         break;
382       }
383       default : {
384                   continue;
385                 }
386     }
387     return $mount;
388   }
391   function PrepareForCopyPaste($source)
392   {
393     plugin::PrepareForCopyPaste($source);
395     $tmp =array();
396     if(isset($source['goExportEntry'])){
397       if(isset($source['goExportEntry']['count'])){
398         for($i= 0; $i<$source['goExportEntry']['count']; $i++){
399           $entry= $source['goExportEntry'][$i];
400           $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
401         }
402       }
403     }
404     $this->goExportEntryList = $tmp;
405     $this->goExportEntry = $tmp;
406   }
409   /* Return plugin informations for acl handling */
410   static function plInfo()
411   {
412     return (array(
413           "plShortName"   => _("File service (Shares)"),
414           "plDescription" => _("File service - Shares")." ("._("Services").")",
415           "plSelfModify"  => FALSE,
416           "plDepends"     => array(),
417           "plPriority"    => 90,
418           "plSection"     => array("administration"),
419           "plCategory"    => array("server"),
421           "plProvidedAcls"=> array(
422               "name"        => _("Name"),
423               "netatalkmount" => _("Apple mounts"),
424               "description" => _("Description"),
425               "type"        => _("Type"),
426               "charset"     => _("Charset"),
427               "path"        => _("Path"),
428               "option"      => _("Option"),
429               "volume"      => _("Volume"))
431           ));
432   }
436 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
437 ?>