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($_POST['goExportEntryList'] as $entry){
65 $this->deleteFromMountList($this->goExportEntryList[$entry]);
66 }
67 }
68 foreach($_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 print_red($msg);
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,$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 = split("\|",$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 $fields['AllowEdit'] = true;
159 return($fields);
160 }
163 function save()
164 {
165 plugin::save();
167 /* Arrays */
168 foreach (array("goExportEntryList"=>"goExportEntry") as $source => $destination){
169 $this->attrs[$destination]= array();
170 foreach ($this->$source as $element){
171 $this->attrs[$destination][]= $element;
172 }
173 }
176 /* Process netatalk mounts */
177 if($this->allow_mounts) {
178 $this->process_mounts();
179 }
181 /* Check if this is a new entry ... add/modify */
182 $ldap = $this->config->get_ldap_link();
183 $ldap->cat($this->dn,array("objectClass"));
184 if($ldap->count()){
185 $ldap->cd($this->dn);
186 $ldap->modify($this->attrs);
187 }else{
188 $ldap->cd($this->dn);
189 $ldap->add($this->attrs);
190 }
191 if (!$ldap->success()){
192 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
193 }
194 if($this->initially_was_account){
195 $this->handle_post_events("modify");
196 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
197 }else{
198 $this->handle_post_events("add");
199 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
200 }
201 }
204 function check()
205 {
206 $message = plugin::check();
207 return($message);
208 }
211 function save_object()
212 {
213 if(isset($_POST['goShareServerPosted'])){
214 plugin::save_object();
215 }
216 }
218 function addToList($entry){
219 $key = key($entry);
220 $this->goExportEntryList[$key]=$entry[$key];
221 }
223 function deleteFromList($id)
224 {
225 /* Check if the share is used by someone */
226 $ldap = $this->config->get_ldap_link();
227 $ldap->cd($this->config->current['BASE']);
228 $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*||*))", array("cn"));
229 $cnt= $ldap->count();
230 if ($cnt){
231 $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
232 $msg.= "<br><br><ul>";
233 while ($attrs= $ldap->fetch()){
234 $msg.= "<li>".$attrs["cn"][0]."</li>";
235 }
236 $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
237 print_red($msg);
239 } else {
240 /* Finally remove it */
241 unset($this->goExportEntryList[$id]);
242 }
243 }
245 function process_mounts() {
247 $clip = "cn=" . $this->cn . ",".get_ou('serverou');
248 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
250 $mounts = array(
251 "objectClass" => "container",
252 "cn" => "mounts"
253 );
255 # load data from mounts container
256 $ldap = $this->config->get_ldap_link();
257 $ldap->cat($mountsdn, array('dn'));
258 $attrs = $ldap->fetch();
260 # mounts container not present yet, so we create it
261 if (count($attrs) == 0) {
262 $ldap->cd($mountsdn);
263 $ldap->add($mounts);
264 if (!$ldap->success()){
265 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_ADD, get_class()));
266 }
267 new log("modify","server/".get_class($this),$mountsdn,array_keys($mounts),$ldap->get_error());
268 }
270 # remove deleted mounts from the container
271 foreach ($this->mounts_to_remove as $entry) {
272 $mount=$this->returnMountEntry($entry);
273 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
275 $ldap->cat($mountdn, array('dn'));
276 $attrs = $ldap->fetch();
278 if (count($attrs) != 0) {
279 $ldap->rmdir($mountdn);
280 if (!$ldap->success()){
281 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mountdn, LDAP_DEL, get_class()));
282 }
283 new log("remove","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
284 }
285 }
287 # add new mounts to the container
288 foreach ($this->mounts_to_add as $entry) {
289 $mount=$this->returnMountEntry($entry);
290 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
291 $ldap->cd($mountdn);
292 $ldap->add($mount);
293 if (!$ldap->success()){
294 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $mount, LDAP_ADD, get_class()));
295 }
296 new log("create","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
297 }
298 }
300 function addToMountList($entry)
301 {
302 if($this->acl_is_writeable("name")){
303 $key = key($entry);
304 $type = $this->get_share_type($entry[$key]);
305 if (($type == "netatalk") || ($type == "NFS")) {
306 $this->mounts_to_add[$entry[$key]] = $entry[$key];
307 unset($this->mounts_to_remove[$entry[$key]]);
308 }
309 }
310 }
312 function deleteFromMountList($entry)
313 {
314 if($this->acl_is_writeable("name")){
315 $type = $this->get_share_type($entry);
316 if (($type == "netatalk") || ($type == "NFS")) {
317 $this->mounts_to_remove[$entry] = $entry;
318 unset($this->mounts_to_add[$entry]);
319 }
320 }
321 }
323 function get_share_type($share)
324 {
325 $tmp = split("\|", $share);
326 return $tmp[2];
327 }
329 function returnMountEntry($entry)
330 {
331 $item = split("\|", $entry);
332 $name = $item[0];
333 $description = $item[1];
334 $type = $item[2];
335 $charset = $item[3];
336 $path = $item[4];
337 $options = $item[5];
339 switch ($type) {
340 case "netatalk" : {
341 $mount = array(
342 "mountDirectory" => "/Network/Servers/",
343 "mountOption" => array(
344 "net",
345 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
346 ),
347 "mountType" => "url",
348 "objectClass" => "mount",
349 "cn" => $this->cn .":/".$name
350 );
351 break;
352 }
353 case "NFS" : {
354 $mount = array(
355 "mountDirectory" => "/Network/Servers/",
356 "mountOption" => "net",
357 "mountType" => "nfs",
358 "objectClass" => "mount",
359 "cn" => $this->cn .":".$path
360 );
361 break;
362 }
363 default : {
364 continue;
365 }
366 }
367 return $mount;
368 }
371 function PrepareForCopyPaste($source)
372 {
373 plugin::PrepareForCopyPaste($source);
375 $tmp =array();
376 if(isset($source['goExportEntry'])){
377 if(isset($source['goExportEntry']['count'])){
378 for($i= 0; $i<$source['goExportEntry']['count']; $i++){
379 $entry= $source['goExportEntry'][$i];
380 $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
381 }
382 }
383 }
384 $this->goExportEntryList = $tmp;
385 $this->goExportEntry = $tmp;
386 }
389 /* Return plugin informations for acl handling */
390 static function plInfo()
391 {
392 return (array(
393 "plShortName" => _("File service (Shares)"),
394 "plDescription" => _("File service - Shares")." ("._("Services").")",
395 "plSelfModify" => FALSE,
396 "plDepends" => array(),
397 "plPriority" => 90,
398 "plSection" => array("administration"),
399 "plCategory" => array("server"),
401 "plProvidedAcls"=> array(
402 "name" => _("Name"),
403 "netatalkmount" => _("Apple mounts"),
404 "description" => _("Description"),
405 "type" => _("Type"),
406 "charset" => _("Charset"),
407 "path" => _("Path"),
408 "option" => _("Option"),
409 "volume" => _("Volume"))
411 ));
412 }
415 }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>