Code

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