6a9f8424c60cd9cce51d3c57b9ec6661616e4776
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");
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 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system server/shares with dn '%s' failed."),$this->dn));
192 if($this->initially_was_account){
193 $this->handle_post_events("modify");
194 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
195 }else{
196 $this->handle_post_events("add");
197 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
198 }
199 }
202 function check()
203 {
204 $message = plugin::check();
205 return($message);
206 }
209 function save_object()
210 {
211 if(isset($_POST['goShareServerPosted'])){
212 plugin::save_object();
213 }
214 }
216 function addToList($entry){
217 $key = key($entry);
218 $this->goExportEntryList[$key]=$entry[$key];
219 }
221 function deleteFromList($id)
222 {
223 /* Check if the share is used by someone */
224 $ldap = $this->config->get_ldap_link();
225 $ldap->cd($this->config->current['BASE']);
226 $ldap->search("(|(gotoProfileServer=*|$id)(gotoShare=*|$id|*||*))", array("cn"));
227 $cnt= $ldap->count();
228 if ($cnt){
229 $msg= sprintf(_("The share can't be removed since it is still used by %d users:"), $cnt);
230 $msg.= "<br><br><ul>";
231 while ($attrs= $ldap->fetch()){
232 $msg.= "<li>".$attrs["cn"][0]."</li>";
233 }
234 $msg.= "</ul>"._("Please correct the share-/profile settings of these users");
235 print_red($msg);
237 } else {
238 /* Finally remove it */
239 unset($this->goExportEntryList[$id]);
240 }
241 }
243 function process_mounts() {
245 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
246 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
248 $mounts = array(
249 "objectClass" => "container",
250 "cn" => "mounts"
251 );
253 # load data from mounts container
254 $ldap = $this->config->get_ldap_link();
255 $ldap->cat($mountsdn, array('dn'));
256 $attrs = $ldap->fetch();
258 # mounts container not present yet, so we create it
259 if (count($attrs) == 0) {
260 $ldap->cd($mountsdn);
261 $ldap->add($mounts);
262 show_ldap_error($ldap->get_error(), sprintf(_("Creating system server/shares (mount container) with dn '%s' failed."),$this->dn));
263 new log("modify","server/".get_class($this),$mountsdn,array_keys($mounts),$ldap->get_error());
264 }
266 # remove deleted mounts from the container
267 foreach ($this->mounts_to_remove as $entry) {
268 $mount=$this->returnMountEntry($entry);
269 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
271 $ldap->cat($mountdn, array('dn'));
272 $attrs = $ldap->fetch();
274 if (count($attrs) != 0) {
275 $ldap->rmdir($mountdn);
276 show_ldap_error($ldap->get_error(), sprintf(_("Removing system server/shares (mount container) with dn '%s' failed."),$this->dn));
277 new log("remove","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
278 }
279 }
281 # add new mounts to the container
282 foreach ($this->mounts_to_add as $entry) {
283 $mount=$this->returnMountEntry($entry);
284 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
285 $ldap->cd($mountdn);
286 $ldap->add($mount);
287 show_ldap_error($ldap->get_error(), sprintf(_("Saving system server/shares (mount container) with dn '%s' failed."),$this->dn));
288 new log("create","server/".get_class($this),$mountdn,array_keys($mount),$ldap->get_error());
289 }
290 }
292 function addToMountList($entry)
293 {
294 if($this->acl_is_writeable("name")){
295 $key = key($entry);
296 $type = $this->get_share_type($entry[$key]);
297 if (($type == "netatalk") || ($type == "NFS")) {
298 $this->mounts_to_add[$entry[$key]] = $entry[$key];
299 unset($this->mounts_to_remove[$entry[$key]]);
300 }
301 }
302 }
304 function deleteFromMountList($entry)
305 {
306 if($this->acl_is_writeable("name")){
307 $type = $this->get_share_type($entry);
308 if (($type == "netatalk") || ($type == "NFS")) {
309 $this->mounts_to_remove[$entry] = $entry;
310 unset($this->mounts_to_add[$entry]);
311 }
312 }
313 }
315 function get_share_type($share)
316 {
317 $tmp = split("\|", $share);
318 return $tmp[2];
319 }
321 function returnMountEntry($entry)
322 {
323 $item = split("\|", $entry);
324 $name = $item[0];
325 $description = $item[1];
326 $type = $item[2];
327 $charset = $item[3];
328 $path = $item[4];
329 $options = $item[5];
331 switch ($type) {
332 case "netatalk" : {
333 $mount = array(
334 "mountDirectory" => "/Network/Servers/",
335 "mountOption" => array(
336 "net",
337 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
338 ),
339 "mountType" => "url",
340 "objectClass" => "mount",
341 "cn" => $this->cn .":/".$name
342 );
343 break;
344 }
345 case "NFS" : {
346 $mount = array(
347 "mountDirectory" => "/Network/Servers/",
348 "mountOption" => "net",
349 "mountType" => "nfs",
350 "objectClass" => "mount",
351 "cn" => $this->cn .":".$path
352 );
353 break;
354 }
355 default : {
356 continue;
357 }
358 }
359 return $mount;
360 }
363 function PrepareForCopyPaste($source)
364 {
365 plugin::PrepareForCopyPaste($source);
367 $tmp =array();
368 if(isset($source['goExportEntry'])){
369 if(isset($source['goExportEntry']['count'])){
370 for($i= 0; $i<$source['goExportEntry']['count']; $i++){
371 $entry= $source['goExportEntry'][$i];
372 $tmp[preg_replace('/\|.*$/', '', $entry)]= $entry;
373 }
374 }
375 }
376 $this->goExportEntryList = $tmp;
377 $this->goExportEntry = $tmp;
378 }
381 /* Return plugin informations for acl handling */
382 function plInfo()
383 {
384 return (array(
385 "plShortName" => _("File service (Shares)"),
386 "plDescription" => _("File service - Shares")." ("._("Services").")",
387 "plSelfModify" => FALSE,
388 "plDepends" => array(),
389 "plPriority" => 90,
390 "plSection" => array("administration"),
391 "plCategory" => array("server"),
393 "plProvidedAcls"=> array(
394 "name" => _("Name"),
395 "netatalkmount" => _("Apple mounts"),
396 "description" => _("Description"),
397 "type" => _("Type"),
398 "charset" => _("Charset"),
399 "path" => _("Path"),
400 "option" => _("Option"),
401 "volume" => _("Volume"))
403 ));
404 }
407 }
408 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
409 ?>