Code

35b9cf51248086e3b0916ee2230d4258f0559942
[gosa.git] / plugins / admin / systems / class_goShareServer.inc
1 <?php
3 class goShareServer extends plugin{
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 $acl;
20   var $cn                   = "";
21   var $goShareServerStatus  = "";
22   var $goExportEntry        = array();
23   var $allow_mounts         = false;
24   var $mounts_to_remove     = array();
25   var $mounts_to_add        = array();
27   function goShareServer($config,$dn)
28   {
29     plugin::plugin($config,$dn);
31     $this->DisplayName = _("File service");
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(); 
57     if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList']))){
58       if($this->allow_mounts){
59         foreach($_POST['goExportEntryList'] as $entry){
60           $this->deleteFromMountList($this->goExportEntryList[$entry]);
61         }
62       }
63       foreach($_POST['goExportEntryList'] as $entry){
64         $this->deleteFromList($entry);
65       }
66     }
68     if(isset($_POST['NewNfsAdd'])){
69       $this->oldone = NULL;
70       $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
71       $this->dialog = true;
72     }
74     if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
75       $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
76       $add_mount=isset($this->mounts_to_add[$entry]);
77       $this->oldone=$entry;
78       $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
79       $this->dialog = true;
80     }
81     if(isset($this->o_subWindow)){
82       $this->o_subWindow->save_object(TRUE);
83     }
85     /* Save NFS setup */
86     if(isset($_POST['NFSsave'])){
87       if(count($this->o_subWindow->check())>0){
88         foreach($this->o_subWindow->check() as $msg) {
89           print_red($msg);
90         }
91       }else{
92         $this->o_subWindow->save_object();
93         $newone = $this->o_subWindow->save();
94         $this->addToList($newone);
95         if($this->allow_mounts){
96           if($this->oldone) {
97             $this->deleteFromMountList($this->oldone);
98           }
99           if ($this->o_subWindow->should_create_mount()) {
100             $this->addToMountList($newone);
101           }
102         }
103         unset($this->o_subWindow);
104         $this->dialog = false;
105       }
106     }
108     /* Cancel NFS setup */
109     if(isset($_POST['NFScancel'])){
110       $this->oldone = NULL;
111       unset($this->o_subWindow);
112       $this->dialog = false;
113     }
115     /* Execute NFS setup dialog*/
116     if(isset($this->o_subWindow)){
117       return $this->o_subWindow->execute();
118     }
124     foreach($this->attributes as $attr){
125       $smarty->assign($attr,$this->$attr);
126       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
127     }
128     $tellSmarty= array();
129     ksort($this->goExportEntryList);
130     foreach($this->goExportEntryList as $name=>$values){
131       $tmp = split("\|",$values);
132       $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
133     }
134     $smarty->assign("goExportEntry",array_keys($tellSmarty));
135     $smarty->assign("goExportEntryKeys",($tellSmarty));
136     $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
139     return($smarty->fetch(get_template_path("goShareServer.tpl",TRUE,dirname(__FILE__))));
140   }
143   function getListEntry()
144   {
145     $this->updateStatusState();
146     $flag = $this->StatusFlag;
147     $fields['Status']     = $this->$flag;
148     $fields['Message']    = _("Shares");
149     $fields['AllowStart'] = true;
150     $fields['AllowStop']  = true;
151     $fields['AllowRestart'] = true;
152     $fields['AllowRemove']= true;
153     $fields['AllowEdit']  = true;
154     return($fields);
155   }
158   function remove_from_parent()
159   {
160     plugin::remove_from_parent();
161     /* Check if this is a new entry ... add/modify */
162     $ldap = $this->config->get_ldap_link();
163     $ldap->cat($this->dn,array("objectClass"));
164     if($ldap->count()){
165       $ldap->cd($this->dn);
166       $ldap->modify($this->attrs);
167     }else{
168       $ldap->cd($this->dn);
169       $ldap->add($this->attrs);
170     }
171     show_ldap_error($ldap->get_error());
172     $this->handle_post_events("remove");
173   }
176   function save()
177   {
178     plugin::save();
180     /* Arrays */
181     foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
182       $this->attrs[$destination]= array();
183       foreach ($this->$source as $element){
184         $this->attrs[$destination][]= $element;
185       }
186     }
189     /* Process netatalk mounts */
190     if($this->allow_mounts) {
191       $this->process_mounts();
192     }
195     /* Check if this is a new entry ... add/modify */
196     $ldap = $this->config->get_ldap_link();
197     $ldap->cat($this->dn,array("objectClass"));
198     if($ldap->count()){
199       $ldap->cd($this->dn);
200       $ldap->modify($this->attrs);
201     }else{
202       $ldap->cd($this->dn);
203       $ldap->add($this->attrs);
204     }
205     show_ldap_error($ldap->get_error());
206     if($this->initially_was_account){
207       $this->handle_post_events("modify");
208     }else{
209       $this->handle_post_events("add");
210     }
211   }
214   /* Directly save new status flag */
215   function setStatus($value)
216   {
217     if($value == "none") return;
218     if(!$this->initially_was_account) return;
219     $ldap = $this->config->get_ldap_link();
220     $ldap->cd($this->dn);
221     $ldap->cat($this->dn,array("objectClass"));
222     if($ldap->count()){
224       $tmp = $ldap->fetch();
225       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
226         $attrs['objectClass'][] = $tmp['objectClass'][$i];
227       }
228       $flag = $this->StatusFlag;
229       $attrs[$flag] = $value;
230       $this->$flag = $value;
231       $ldap->modify($attrs);
232       show_ldap_error($ldap->get_error());
233       $this->action_hook();
234     }
235   }
238   function check()
239   { 
240     $message = plugin::check();
241     return($message);
242   }
245   function save_object()
246   {
247     if(isset($_POST['goShareServerPosted'])){
248       plugin::save_object();
249     }
250   } 
252   function addToList($entry){
253     $key =  key($entry);
254     $this->goExportEntryList[$key]=$entry[$key];
255   }
257   function deleteFromList($id){
258     unset($this->goExportEntryList[$id]);
259   }
261    function process_mounts() {
263     $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
264     $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
266     $mounts = array(
267       "objectClass" => "container",
268       "cn" => "mounts"
269     );
271     # load data from mounts container
272     $ldap = $this->config->get_ldap_link();
273     $ldap->cat($mountsdn, array('dn'));
274     $attrs = $ldap->fetch();
276     # mounts container not present yet, so we create it
277     if (count($attrs) == 0) {
278         $ldap->cd($mountsdn);
279         $ldap->add($mounts);
280         show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
281         gosa_log("Mount container '$mountsdn' has been created");
282     }
284     # remove deleted mounts from the container
285     foreach ($this->mounts_to_remove as $entry) {
286       $mount=$this->returnMountEntry($entry);
287       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
289       $ldap->cat($mountdn, array('dn'));
290       $attrs = $ldap->fetch();
292       if (count($attrs) != 0) {
293         $ldap->rmdir($mountdn);
294         show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
295         gosa_log("Mount object '".$mountdn."' has been removed");
296       }
297     }
299     # add new mounts to the container
300     foreach ($this->mounts_to_add as $entry) {
302       $mount=$this->returnMountEntry($entry);
303       $mountdn = "cn=".$mount["cn"].","."$mountsdn";
304       $ldap->cd($mountdn);
305       $ldap->add($mount);
306       show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
307       gosa_log("Mount object '".$mountdn."' has been added");
308     }
309   }
311    function action_hook($add_attrs= array())
312   {
313     /* Find postcreate entries for this class */
314     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
315     if ($command == "" && isset($this->config->data['TABS'])){
316       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
317     }
318     if ($command != ""){
319       /* Walk through attribute list */
320       foreach ($this->attributes as $attr){
321         if (!is_array($this->$attr)){
322           $command= preg_replace("/%$attr/", $this->$attr, $command);
323         }
324       }
325       $command= preg_replace("/%dn/", $this->dn, $command);
326       /* Additional attributes */
327       foreach ($add_attrs as $name => $value){
328         $command= preg_replace("/%$name/", $value, $command);
329       }
331       /* If there are still some %.. in our command, try to fill these with some other class vars */
332       if(preg_match("/%/",$command)){
333         $attrs = get_object_vars($this);
334         foreach($attrs as $name => $value){
335           if(!is_string($value)) continue;
336           $command= preg_replace("/%$name/", $value, $command);
337         }
338       }
340       if (check_command($command)){
341         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
342             $command, "Execute");
344         exec($command);
345       } else {
346         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
347         print_red ($message);
348       }
349     }
350   }
352   /* Get updates for status flag */
353   function updateStatusState()
354   {
355     if(empty($this->StatusFlag)) return;
357     $attrs = array();
358     $flag = $this->StatusFlag;
359     $ldap = $this->config->get_ldap_link();
360     $ldap->cd($this->cn);
361     $ldap->cat($this->dn,array($flag));
362     if($ldap->count()){
363       $attrs = $ldap->fetch();
364     }
365     if(isset($attrs[$flag][0])){
366       $this->$flag = $attrs[$flag][0];
367     }
368   }
371 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
372 ?>