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();
162 /* Remove status flag, it is not a memeber of
163 this->attributes, so ensure that it is deleted too */
164 if(!empty($this->StatusFlag)){
165 $this->attrs[$this->StatusFlag] = array();
166 }
168 /* Check if this is a new entry ... add/modify */
169 $ldap = $this->config->get_ldap_link();
170 $ldap->cat($this->dn,array("objectClass"));
171 if($ldap->count()){
172 $ldap->cd($this->dn);
173 $ldap->modify($this->attrs);
174 }else{
175 $ldap->cd($this->dn);
176 $ldap->add($this->attrs);
177 }
178 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system server/shares with dn '%s' failed."),$this->dn));
179 $this->handle_post_events("remove");
180 }
183 function save()
184 {
185 plugin::save();
187 /* Arrays */
188 foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
189 $this->attrs[$destination]= array();
190 foreach ($this->$source as $element){
191 $this->attrs[$destination][]= $element;
192 }
193 }
196 /* Process netatalk mounts */
197 if($this->allow_mounts) {
198 $this->process_mounts();
199 }
202 /* Check if this is a new entry ... add/modify */
203 $ldap = $this->config->get_ldap_link();
204 $ldap->cat($this->dn,array("objectClass"));
205 if($ldap->count()){
206 $ldap->cd($this->dn);
207 $ldap->modify($this->attrs);
208 }else{
209 $ldap->cd($this->dn);
210 $ldap->add($this->attrs);
211 }
212 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/shares with dn '%s' failed."),$this->dn));
213 if($this->initially_was_account){
214 $this->handle_post_events("modify");
215 }else{
216 $this->handle_post_events("add");
217 }
218 }
221 /* Directly save new status flag */
222 function setStatus($value)
223 {
224 if($value == "none") return;
225 if(!$this->initially_was_account) return;
226 $ldap = $this->config->get_ldap_link();
227 $ldap->cd($this->dn);
228 $ldap->cat($this->dn,array("objectClass"));
229 if($ldap->count()){
231 $tmp = $ldap->fetch();
232 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
233 $attrs['objectClass'][] = $tmp['objectClass'][$i];
234 }
235 $flag = $this->StatusFlag;
236 $attrs[$flag] = $value;
237 $this->$flag = $value;
238 $ldap->modify($attrs);
239 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for system server/shares with dn '%s' failed."),$this->dn));
240 $this->action_hook();
241 }
242 }
245 function check()
246 {
247 $message = plugin::check();
248 return($message);
249 }
252 function save_object()
253 {
254 if(isset($_POST['goShareServerPosted'])){
255 plugin::save_object();
256 }
257 }
259 function addToList($entry){
260 $key = key($entry);
261 $this->goExportEntryList[$key]=$entry[$key];
262 }
264 function deleteFromList($id){
265 unset($this->goExportEntryList[$id]);
266 }
268 function process_mounts() {
270 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
271 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
273 $mounts = array(
274 "objectClass" => "container",
275 "cn" => "mounts"
276 );
278 # load data from mounts container
279 $ldap = $this->config->get_ldap_link();
280 $ldap->cat($mountsdn, array('dn'));
281 $attrs = $ldap->fetch();
283 # mounts container not present yet, so we create it
284 if (count($attrs) == 0) {
285 $ldap->cd($mountsdn);
286 $ldap->add($mounts);
287 show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn));
288 gosa_log("Mount container '$mountsdn' has been created");
289 }
291 # remove deleted mounts from the container
292 foreach ($this->mounts_to_remove as $entry) {
293 $mount=$this->returnMountEntry($entry);
294 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
296 $ldap->cat($mountdn, array('dn'));
297 $attrs = $ldap->fetch();
299 if (count($attrs) != 0) {
300 $ldap->rmdir($mountdn);
301 show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn));
302 gosa_log("Mount object '".$mountdn."' has been removed");
303 }
304 }
306 # add new mounts to the container
307 foreach ($this->mounts_to_add as $entry) {
308 $mount=$this->returnMountEntry($entry);
309 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
310 $ldap->cd($mountdn);
311 $ldap->add($mount);
312 show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn));
313 gosa_log("Mount object '".$mountdn."' has been added");
314 }
315 }
317 function action_hook($add_attrs= array())
318 {
319 /* Find postcreate entries for this class */
320 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
321 if ($command == "" && isset($this->config->data['TABS'])){
322 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
323 }
324 if ($command != ""){
325 /* Walk through attribute list */
326 foreach ($this->attributes as $attr){
327 if (!is_array($this->$attr)){
328 $command= preg_replace("/%$attr/", $this->$attr, $command);
329 }
330 }
331 $command= preg_replace("/%dn/", $this->dn, $command);
332 /* Additional attributes */
333 foreach ($add_attrs as $name => $value){
334 $command= preg_replace("/%$name/", $value, $command);
335 }
337 /* If there are still some %.. in our command, try to fill these with some other class vars */
338 if(preg_match("/%/",$command)){
339 $attrs = get_object_vars($this);
340 foreach($attrs as $name => $value){
341 if(!is_string($value)) continue;
342 $command= preg_replace("/%$name/", $value, $command);
343 }
344 }
346 if (check_command($command)){
347 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
348 $command, "Execute");
350 exec($command);
351 } else {
352 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
353 print_red ($message);
354 }
355 }
356 }
358 /* Get updates for status flag */
359 function updateStatusState()
360 {
361 if(empty($this->StatusFlag)) return;
363 $attrs = array();
364 $flag = $this->StatusFlag;
365 $ldap = $this->config->get_ldap_link();
366 $ldap->cd($this->cn);
367 $ldap->cat($this->dn,array($flag));
368 if($ldap->count()){
369 $attrs = $ldap->fetch();
370 }
371 if(isset($attrs[$flag][0])){
372 $this->$flag = $attrs[$flag][0];
373 }
374 }
377 function addToMountList($entry)
378 {
379 $key = key($entry);
380 $type = $this->get_share_type($entry[$key]);
381 if (($type == "netatalk") || ($type == "NFS")) {
382 $this->mounts_to_add[$entry[$key]] = $entry[$key];
383 unset($this->mounts_to_remove[$entry[$key]]);
384 }
385 }
387 function deleteFromMountList($entry)
388 {
389 $type = $this->get_share_type($entry);
390 if (($type == "netatalk") || ($type == "NFS")) {
391 $this->mounts_to_remove[$entry] = $entry;
392 unset($this->mounts_to_add[$entry]);
393 }
394 }
396 function get_share_type($share)
397 {
398 $tmp = split("\|", $share);
399 return $tmp[2];
400 }
402 function returnMountEntry($entry)
403 {
404 $item = split("\|", $entry);
405 $name = $item[0];
406 $description = $item[1];
407 $type = $item[2];
408 $charset = $item[3];
409 $path = $item[4];
410 $options = $item[5];
412 switch ($type) {
413 case "netatalk" : {
414 $mount = array(
415 "mountDirectory" => "/Network/Servers/",
416 "mountOption" => array(
417 "net",
418 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
419 ),
420 "mountType" => "url",
421 "objectClass" => "mount",
422 "cn" => $this->cn .":/".$name
423 );
424 break;
425 }
426 case "NFS" : {
427 $mount = array(
428 "mountDirectory" => "/Network/Servers/",
429 "mountOption" => "net",
430 "mountType" => "nfs",
431 "objectClass" => "mount",
432 "cn" => $this->cn .":".$path
433 );
434 break;
435 }
436 default : {
437 continue;
438 }
439 }
440 return $mount;
441 }
443 }
444 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
445 ?>