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(get_post('goExportEntryList') as $entry){
65 $this->deleteFromMountList($this->goExportEntryList[$entry]);
66 }
67 }
68 foreach(get_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,set_post($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 = explode("|",$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 return($fields);
159 }
162 function save()
163 {
164 plugin::save();
166 /* Arrays */
167 foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
168 $this->attrs[$destination]= array();
169 foreach ($this->$source as $element){
170 $this->attrs[$destination][]= $element;
171 }
172 }
175 /* Process netatalk mounts */
176 if($this->allow_mounts) {
177 $this->process_mounts();
178 }
180 /* Check if this is a new entry ... add/modify */
181 $ldap = $this->config->get_ldap_link();
182 $ldap->cat($this->dn,array("objectClass"));
183 if($ldap->count()){
184 $ldap->cd($this->dn);
185 $ldap->modify($this->attrs);
186 }else{
187 $ldap->cd($this->dn);
188 $ldap->add($this->attrs);
189 }
190 if (!$ldap->success()){
191 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
192 }
193 if($this->initially_was_account){
194 $this->handle_post_events("modify");
195 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
196 }else{
197 $this->handle_post_events("add");
198 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
199 }
200 }
203 function check()
204 {
205 $message = plugin::check();
206 return($message);
207 }
210 function save_object()
211 {
212 if(isset($_POST['goShareServerPosted'])){
213 plugin::save_object();
214 }
215 }
217 function allow_remove()
218 {
219 /* Check if the service is still in use */
220 $ldap = $this->config->get_ldap_link();
221 $ldap->cd($this->config->current['BASE']);
222 $cn = $ldap->getCn($this->dn);
223 $ldap->search("(&(|(objectClass=posixGroup)(objectClass=posixAccount)(objectClass=gosaGroupOfNames))(gotoShare=$cn|*))", array("dn"));
225 if($ldap->count()){
226 /* Number of entries shown in warning */
227 $i = 3;
228 $str = '<p>';
229 while(($attrs = $ldap->fetch()) && $i >= 0){
230 $i --;
231 if(isset($attrs['dn'])){
232 $str .= '<i>' . $attrs['dn']."</i><br/>";
233 }
234 }
235 $str .= '</p>';
236 return(sprintf(_("Cannot remove share - it is still in use by these objects: %s"), $str));
237 }
238 }
240 function addToList($entry){
241 $key = key($entry);
242 $this->goExportEntryList[$key]=$entry[$key];
243 }
245 function deleteFromList($id)
246 {
247 /* Check if the share is used by someone */
248 $ldap = $this->config->get_ldap_link();
249 $ldap->cd($this->config->current['BASE']);
250 $cn = $ldap->getCn($this->dn);
251 $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=$cn|$id|*))", array("cn"));
252 if ($ldap->count() != 0){
253 while ($attrs= $ldap->fetch()){
254 $obj[$ldap->getDN()]= $attrs['cn'][0];
255 }
256 msg_dialog::display(_("Error"), msgPool::stillInUse(_("share"), msgPool::buildList($obj)), ERROR_DIALOG);
258 } else {
259 /* Finally remove it */
260 unset($this->goExportEntryList[$id]);
261 }
262 }
264 function process_mounts() {
266 $clip = "cn=" . $this->cn . ",".get_ou("servgeneric", "serverRDN");
267 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
269 $mounts = array(
270 "objectClass" => "container",
271 "cn" => "mounts"
272 );
274 # load data from mounts container
275 $ldap = $this->config->get_ldap_link();
276 $ldap->cat($mountsdn, array('dn'));
277 $attrs = $ldap->fetch();
279 # mounts container not present yet, so we create it
280 if (count($attrs) == 0) {
281 $ldap->cd($mountsdn);
282 $ldap->add($mounts);
283 if (!$ldap->success()){
284 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
285 }
286 new log("modify","server/".get_class($this),$mountsdn,array_keys($mounts),$ldap->get_error());
287 }
289 # remove deleted mounts from the container
290 foreach ($this->mounts_to_remove as $entry) {
291 $mount=$this->returnMountEntry($entry);
292 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
294 $ldap->cat($mountdn, array('dn'));
295 $attrs = $ldap->fetch();
297 if (count($attrs) != 0) {
298 $ldap->rmdir($mountdn);
299 if (!$ldap->success()){
300 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mountdn, LDAP_DEL, get_class()));
301 }
302 new log("remove","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
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 if (!$ldap->success()){
313 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mount, LDAP_ADD, get_class()));
314 }
315 new log("create","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
316 }
317 }
319 function addToMountList($entry)
320 {
321 if($this->acl_is_writeable("name")){
322 $key = key($entry);
323 $type = $this->get_share_type($entry[$key]);
324 if (($type == "netatalk") || ($type == "NFS")) {
325 $this->mounts_to_add[$entry[$key]] = $entry[$key];
326 unset($this->mounts_to_remove[$entry[$key]]);
327 }
328 }
329 }
331 function deleteFromMountList($entry)
332 {
333 if($this->acl_is_writeable("name")){
334 $type = $this->get_share_type($entry);
335 if (($type == "netatalk") || ($type == "NFS")) {
336 $this->mounts_to_remove[$entry] = $entry;
337 unset($this->mounts_to_add[$entry]);
338 }
339 }
340 }
342 function get_share_type($share)
343 {
344 $tmp = explode("|", $share);
345 return $tmp[2];
346 }
348 function returnMountEntry($entry)
349 {
350 $item = explode("|", $entry);
351 $name = $item[0];
352 $description = $item[1];
353 $type = $item[2];
354 $charset = $item[3];
355 $path = $item[4];
356 $options = $item[5];
358 switch ($type) {
359 case "netatalk" : {
360 $mount = array(
361 "mountDirectory" => "/Network/Servers/",
362 "mountOption" => array(
363 "net",
364 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
365 ),
366 "mountType" => "url",
367 "objectClass" => "mount",
368 "cn" => $this->cn .":/".$name
369 );
370 break;
371 }
372 case "NFS" : {
373 $mount = array(
374 "mountDirectory" => "/Network/Servers/",
375 "mountOption" => "net",
376 "mountType" => "nfs",
377 "objectClass" => "mount",
378 "cn" => $this->cn .":".$path
379 );
380 break;
381 }
382 default : {
383 continue;
384 }
385 }
386 return $mount;
387 }
390 function PrepareForCopyPaste($source)
391 {
392 plugin::PrepareForCopyPaste($source);
394 $tmp =array();
395 if(isset($source['goExportEntry'])){
396 if(isset($source['goExportEntry']['count'])){
397 for($i= 0; $i<$source['goExportEntry']['count']; $i++){
398 $entry= $source['goExportEntry'][$i];
399 $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
400 }
401 }
402 }
403 $this->goExportEntryList = $tmp;
404 $this->goExportEntry = $tmp;
405 }
408 /* Return plugin informations for acl handling */
409 static function plInfo()
410 {
411 return (array(
412 "plShortName" => _("File service (Shares)"),
413 "plDescription" => _("File service - Shares")." ("._("Services").")",
414 "plSelfModify" => FALSE,
415 "plDepends" => array(),
416 "plPriority" => 90,
417 "plSection" => array("administration"),
418 "plRequirements"=> array(
419 'ldapSchema' => array('goShareServer' => '>=2.7'),
420 'onFailureDisablePlugin' => array(get_class())
421 ),
422 "plCategory" => array("server"),
424 "plProvidedAcls"=> array(
425 "start" => _("Start"),
426 "stop" => _("Stop"),
427 "restart" => _("Restart"),
428 "name" => _("Name"),
429 "goExportEntry" => _("Share entries"),
430 "appleMounts" => _("Apple mounts"),
431 "netatalkmount" => _("Apple mounts"),
432 "description" => _("Description"),
433 "type" => _("Type"),
434 "charset" => _("Character set"),
435 "path" => _("Path"),
436 "option" => _("Option"),
437 "volume" => _("Volume"))
439 ));
440 }
443 }
444 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
445 ?>