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("goShareServerStatus","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;
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 $flag = $this->StatusFlag;
146 $fields['Status'] = $this->$flag;
147 $fields['Message'] = _("Shares");
148 $fields['AllowStart'] = true;
149 $fields['AllowStop'] = true;
150 $fields['AllowRestart'] = true;
151 $fields['AllowRemove']= true;
152 $fields['AllowEdit'] = true;
153 return($fields);
154 }
157 function remove_from_parent()
158 {
159 plugin::remove_from_parent();
160 /* Check if this is a new entry ... add/modify */
161 $ldap = $this->config->get_ldap_link();
162 $ldap->cat($this->dn,array("objectClass"));
163 if($ldap->count()){
164 $ldap->cd($this->dn);
165 $ldap->modify($this->attrs);
166 }else{
167 $ldap->cd($this->dn);
168 $ldap->add($this->attrs);
169 }
170 show_ldap_error($ldap->get_error());
171 }
174 function save()
175 {
176 plugin::save();
178 /* Arrays */
179 foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
180 $this->attrs[$destination]= array();
181 foreach ($this->$source as $element){
182 $this->attrs[$destination][]= $element;
183 }
184 }
187 /* Process netatalk mounts */
188 if($this->allow_mounts) {
189 $this->process_mounts();
190 }
193 /* Check if this is a new entry ... add/modify */
194 $ldap = $this->config->get_ldap_link();
195 $ldap->cat($this->dn,array("objectClass"));
196 if($ldap->count()){
197 $ldap->cd($this->dn);
198 $ldap->modify($this->attrs);
199 }else{
200 $ldap->cd($this->dn);
201 $ldap->add($this->attrs);
202 }
203 show_ldap_error($ldap->get_error());
204 }
207 /* Directly save new status flag */
208 function setStatus($value)
209 {
210 if($value == "none") return;
211 $ldap = $this->config->get_ldap_link();
212 $ldap->cd($this->dn);
213 $ldap->cat($this->dn,array("objectClass"));
215 if($ldap->count()){
216 $attrs =array();
217 foreach(array() as $req) {
218 if(!isset($attrs[$req])){
219 if(empty($this->$req)){
220 print_red(sprintf(_("The required attribute '%s' is not set."),$req));
221 }else{
222 $attrs[$req] = $this->$req;
223 }
224 }else{
225 $attrs[$req] = $attrs[$req][0];
226 }
227 }
229 $tmp = $ldap->fetch();
230 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
231 $attrs['objectClass'][] = $tmp['objectClass'][$i];
232 }
233 if(!in_array("goShareServer",$attrs['objectClass'])){
234 $attrs['objectClass'][] = "goShareServer";
235 }
237 $flag = $this->StatusFlag;
238 $attrs[$flag] = $value;
239 $this->$flag = $value;
240 $ldap->modify($attrs);
241 show_ldap_error($ldap->get_error());
242 }
243 }
246 function check()
247 {
248 $message = plugin::check();
249 return($message);
250 }
253 function save_object()
254 {
255 if(isset($_POST['goShareServerPosted'])){
256 plugin::save_object();
257 }
258 }
260 function addToList($entry){
261 $key = key($entry);
262 $this->goExportEntryList[$key]=$entry[$key];
263 }
265 function deleteFromList($id){
266 unset($this->goExportEntryList[$id]);
267 }
269 function process_mounts() {
271 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
272 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
274 $mounts = array(
275 "objectClass" => "container",
276 "cn" => "mounts"
277 );
279 # load data from mounts container
280 $ldap = $this->config->get_ldap_link();
281 $ldap->cat($mountsdn, array('dn'));
282 $attrs = $ldap->fetch();
284 # mounts container not present yet, so we create it
285 if (count($attrs) == 0) {
286 $ldap->cd($mountsdn);
287 $ldap->add($mounts);
288 show_ldap_error($ldap->get_error(), _("Creating mount container failed"));
289 gosa_log("Mount container '$mountsdn' has been created");
290 }
292 # remove deleted mounts from the container
293 foreach ($this->mounts_to_remove as $entry) {
294 $mount=$this->returnMountEntry($entry);
295 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
297 $ldap->cat($mountdn, array('dn'));
298 $attrs = $ldap->fetch();
300 if (count($attrs) != 0) {
301 $ldap->rmdir($mountdn);
302 show_ldap_error($ldap->get_error(), _("Removing mount container failed"));
303 gosa_log("Mount object '".$mountdn."' has been removed");
304 }
305 }
307 # add new mounts to the container
308 foreach ($this->mounts_to_add as $entry) {
310 $mount=$this->returnMountEntry($entry);
311 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
312 $ldap->cd($mountdn);
313 $ldap->add($mount);
314 show_ldap_error($ldap->get_error(), _("Saving mount container failed"));
315 gosa_log("Mount object '".$mountdn."' has been added");
316 }
317 }
320 }
321 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
322 ?>