1 <?php
3 require_once("class_goService.inc");
5 class goShareServer extends goService{
7 var $cli_summary = "This plugin is used within the ServerService Pluign \nand indicates that this server supports shares.";
8 var $cli_description = "Some longer text\nfor help";
9 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
11 /* This plugin only writes its objectClass */
12 var $objectclasses = array("goShareServer");
13 var $attributes = array("goExportEntry");
14 var $StatusFlag = "goShareServerStatus";
16 /* This class can't be assigned twice so it conflicts with itsself */
17 var $conflicts = array("goShareServer");
19 var $DisplayName = "";
20 var $dn = NULL;
21 var $cn = "";
22 var $goShareServerStatus = "";
23 var $goExportEntry = array();
24 var $allow_mounts = false;
25 var $mounts_to_remove = array();
26 var $mounts_to_add = array();
28 function goShareServer($config,$dn)
29 {
30 goService::goService($config,$dn);
32 $this->DisplayName = _("File service");
34 $tmp =array();
35 if(isset($this->attrs['goExportEntry'])){
36 if(isset($this->attrs['goExportEntry']['count'])){
37 for($i= 0; $i<$this->attrs['goExportEntry']['count']; $i++){
38 $entry= $this->attrs['goExportEntry'][$i];
39 $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
40 }
41 }
42 }
43 $this->goExportEntryList = $tmp;
45 $ldap = $this->config->get_ldap_link();
46 $avl_objectclasses = $ldap->get_objectclasses();
47 if (isset($avl_objectclasses["mount"])) {
48 $this->allow_mounts = true;
49 }
50 }
53 function execute()
54 {
55 $smarty = get_smarty();
58 if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntryList'])) && ($this->acl_is_writeable("name"))){
59 if($this->allow_mounts){
60 foreach($_POST['goExportEntryList'] as $entry){
61 $this->deleteFromMountList($this->goExportEntryList[$entry]);
62 }
63 }
64 foreach($_POST['goExportEntryList'] as $entry){
65 $this->deleteFromList($entry);
66 }
67 }
69 if(isset($_POST['NewNfsAdd']) && ($this->acl_is_writeable("name"))){
70 $this->oldone = NULL;
71 $this->o_subWindow = new servnfs($this->config, $this);
72 $this->o_subWindow->set_acl_category("server");
73 $this->o_subWindow->set_acl_base($this->dn);
74 $this->dialog = true;
75 }
77 if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntryList']))){
78 $entry = $this->goExportEntryList[$_POST['goExportEntryList'][0]];
79 $add_mount=isset($this->mounts_to_add[$entry]);
80 $this->oldone=$entry;
81 $this->o_subWindow = new servnfs($this->config,$this,$entry,$add_mount);
82 $this->o_subWindow->set_acl_base($this->dn);
83 $this->o_subWindow->set_acl_category("server");
84 $this->dialog = true;
85 }
86 if(isset($this->o_subWindow)){
87 $this->o_subWindow->save_object(TRUE);
88 }
90 /* Save NFS setup */
91 if(isset($_POST['NFSsave'])){
92 if(count($this->o_subWindow->check())>0){
93 foreach($this->o_subWindow->check() as $msg) {
94 print_red($msg);
95 }
96 }else{
97 $this->o_subWindow->save_object();
98 $newone = $this->o_subWindow->save();
100 $this->addToList($newone);
101 if($this->allow_mounts){
102 if($this->oldone) {
103 $this->deleteFromMountList($this->oldone);
104 }
105 if ($this->o_subWindow->should_create_mount()) {
106 $this->addToMountList($newone);
107 }
108 }
109 unset($this->o_subWindow);
110 $this->dialog = false;
111 }
112 }
114 /* Cancel NFS setup */
115 if(isset($_POST['NFScancel'])){
116 $this->oldone = NULL;
117 unset($this->o_subWindow);
118 $this->dialog = false;
119 }
121 /* Execute NFS setup dialog*/
122 if(isset($this->o_subWindow)){
123 return $this->o_subWindow->execute();
124 }
126 foreach($this->attributes as $attr){
127 $smarty->assign($attr,$this->$attr);
128 }
130 /* Set acls */
131 $tmp = $this->plInfo();
132 foreach($tmp['plProvidedAcls'] as $name => $translated){
133 $smarty->assign($name."ACL",$this->getacl($name));
134 }
135 $smarty->assign("createable",$this->acl_is_createable());
136 $smarty->assign("removeable",$this->acl_is_removeable());
138 $tellSmarty= array();
139 ksort($this->goExportEntryList);
140 foreach($this->goExportEntryList as $name=>$values){
141 $tmp = split("\|",$values);
142 $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
143 }
144 $smarty->assign("goExportEntry",array_keys($tellSmarty));
145 $smarty->assign("goExportEntryKeys",($tellSmarty));
146 return($smarty->fetch(get_template_path("goShareServer.tpl",TRUE,dirname(__FILE__))));
147 }
150 function getListEntry()
151 {
152 $fields = goService::getListEntry();
153 $fields['Message'] = _("File service (Shares)");
154 $fields['AllowEdit'] = true;
155 return($fields);
156 }
159 function save()
160 {
161 plugin::save();
163 /* Arrays */
164 foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
165 $this->attrs[$destination]= array();
166 foreach ($this->$source as $element){
167 $this->attrs[$destination][]= $element;
168 }
169 }
172 /* Process netatalk mounts */
173 if($this->allow_mounts) {
174 $this->process_mounts();
175 }
177 /* Check if this is a new entry ... add/modify */
178 $ldap = $this->config->get_ldap_link();
179 $ldap->cat($this->dn,array("objectClass"));
180 if($ldap->count()){
181 $ldap->cd($this->dn);
182 $ldap->modify($this->attrs);
183 }else{
184 $ldap->cd($this->dn);
185 $ldap->add($this->attrs);
186 }
187 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/shares with dn '%s' failed."),$this->dn));
188 if($this->initially_was_account){
189 $this->handle_post_events("modify");
190 @log::log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
191 }else{
192 $this->handle_post_events("add");
193 @log::log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
194 }
195 }
198 function check()
199 {
200 $message = plugin::check();
201 return($message);
202 }
205 function save_object()
206 {
207 if(isset($_POST['goShareServerPosted'])){
208 plugin::save_object();
209 }
210 }
212 function addToList($entry){
213 $key = key($entry);
214 $this->goExportEntryList[$key]=$entry[$key];
215 }
217 function deleteFromList($id)
218 {
219 /* Check if the share is used by someone */
220 $ldap = $this->config->get_ldap_link();
221 $ldap->cd($this->config->current['BASE']);
222 $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*))", array("cn"));
223 $cnt= $ldap->count();
224 if ($cnt){
225 $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
226 $msg.= "<br><br><ul>";
227 while ($attrs= $ldap->fetch()){
228 $msg.= "<li>".$attrs["cn"][0]."</li>";
229 }
230 $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
231 print_red($msg);
233 } else {
234 /* Finally remove it */
235 unset($this->goExportEntryList[$id]);
236 }
237 }
239 function process_mounts() {
241 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
242 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
244 $mounts = array(
245 "objectClass" => "container",
246 "cn" => "mounts"
247 );
249 # load data from mounts container
250 $ldap = $this->config->get_ldap_link();
251 $ldap->cat($mountsdn, array('dn'));
252 $attrs = $ldap->fetch();
254 # mounts container not present yet, so we create it
255 if (count($attrs) == 0) {
256 $ldap->cd($mountsdn);
257 $ldap->add($mounts);
258 show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn));
259 gosa_log("Mount container '$mountsdn' has been created");
260 }
262 # remove deleted mounts from the container
263 foreach ($this->mounts_to_remove as $entry) {
264 $mount=$this->returnMountEntry($entry);
265 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
267 $ldap->cat($mountdn, array('dn'));
268 $attrs = $ldap->fetch();
270 if (count($attrs) != 0) {
271 $ldap->rmdir($mountdn);
272 show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn));
273 gosa_log("Mount object '".$mountdn."' has been removed");
274 }
275 }
277 # add new mounts to the container
278 foreach ($this->mounts_to_add as $entry) {
279 $mount=$this->returnMountEntry($entry);
280 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
281 $ldap->cd($mountdn);
282 $ldap->add($mount);
283 show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn));
284 gosa_log("Mount object '".$mountdn."' has been added");
285 }
286 }
288 function addToMountList($entry)
289 {
290 if($this->acl_is_writeable("name")){
291 $key = key($entry);
292 $type = $this->get_share_type($entry[$key]);
293 if (($type == "netatalk") || ($type == "NFS")) {
294 $this->mounts_to_add[$entry[$key]] = $entry[$key];
295 unset($this->mounts_to_remove[$entry[$key]]);
296 }
297 }
298 }
300 function deleteFromMountList($entry)
301 {
302 if($this->acl_is_writeable("name")){
303 $type = $this->get_share_type($entry);
304 if (($type == "netatalk") || ($type == "NFS")) {
305 $this->mounts_to_remove[$entry] = $entry;
306 unset($this->mounts_to_add[$entry]);
307 }
308 }
309 }
311 function get_share_type($share)
312 {
313 $tmp = split("\|", $share);
314 return $tmp[2];
315 }
317 function returnMountEntry($entry)
318 {
319 $item = split("\|", $entry);
320 $name = $item[0];
321 $description = $item[1];
322 $type = $item[2];
323 $charset = $item[3];
324 $path = $item[4];
325 $options = $item[5];
327 switch ($type) {
328 case "netatalk" : {
329 $mount = array(
330 "mountDirectory" => "/Network/Servers/",
331 "mountOption" => array(
332 "net",
333 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
334 ),
335 "mountType" => "url",
336 "objectClass" => "mount",
337 "cn" => $this->cn .":/".$name
338 );
339 break;
340 }
341 case "NFS" : {
342 $mount = array(
343 "mountDirectory" => "/Network/Servers/",
344 "mountOption" => "net",
345 "mountType" => "nfs",
346 "objectClass" => "mount",
347 "cn" => $this->cn .":".$path
348 );
349 break;
350 }
351 default : {
352 continue;
353 }
354 }
355 return $mount;
356 }
359 /* Return plugin informations for acl handling */
360 function plInfo()
361 {
362 return (array(
363 "plShortName" => _("File service (Shares)"),
364 "plDescription" => _("File service - Shares")." ("._("Services").")",
365 "plSelfModify" => FALSE,
366 "plDepends" => array(),
367 "plPriority" => 90,
368 "plSection" => array("administration"),
369 "plCategory" => array("server"),
371 "plProvidedAcls"=> array(
372 "name" => _("Name"),
373 "netatalkmount" => _("Apple mounts"),
374 "description" => _("Description"),
375 "type" => _("Type"),
376 "charset" => _("Charset"),
377 "path" => _("Path"),
378 "option" => _("Option"),
379 "volume" => _("Volume"))
381 ));
382 }
385 }
386 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
387 ?>