afc35e42b5e5d2e4c2db9be4209a05db3d7d15e6
1 <?php
3 class servservice extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage server basic objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 var $goExportEntry = array();
11 var $goTimeSource = array();
12 var $goLdapBase = "";
13 var $goXdmcpIsEnabled = "";
14 var $goFontPath = "";
15 var $goNTPServer = "";
16 var $goShareServer = false;
17 var $goLdapServer = "";
18 var $goTerminalServer = "";
19 var $goSyslogServer = "";
20 var $goCupsServer = "";
21 var $goMailServer = "";
22 var $o_subWindow = NULL;
24 /* attribute list for save action */
25 var $ignore_account= TRUE;
26 var $attributes = array("goLdapBase","goXdmcpIsEnabled","goFontPath","goExportEntry","goTimeSource");
27 var $possible_objectclasses= array( "goShareServer", "goNtpServer", "goServer", "GOhard", "goLdapServer",
28 "goTerminalServer", "goSyslogServer", "goCupsServer","goMailServer");
29 var $objectclasses = array( "top","goServer", "GOhard");
30 var $additionaloc = array( "goShareServer" => array("goExportEntry"),
31 "goNtpServer" => array("goTimeSource"),
32 "goLdapServer" => array("goLdapBase"),
33 "goTerminalServer"=> array("goXdmcpIsEnabled", "goFontPath"),
34 "goSyslogServer" => array(),
35 "goMailServer" => array(),
36 "goCupsServer" => array());
38 var $allow_mounts = false; //do we allow mount entries?
39 var $mounts_to_add = array();
40 var $mounts_to_remove = array();
41 var $oldone = NULL; //temp dave for old mount entry
43 function servservice ($config, $dn= NULL)
44 {
46 plugin::plugin ($config, $dn);
48 $ldap = $this->config->get_ldap_link();
49 $avl_objectclasses = $ldap->get_objectclasses();
50 if (isset($avl_objectclasses["mount"])) {
51 $this->allow_mounts = true;
52 }
54 /* Assemble final object class list */
55 foreach ($this->additionaloc as $oc => $dummy){
56 if (isset($this->attrs['objectClass']) && in_array($oc, $this->attrs['objectClass'])){
57 $this->objectclasses[$oc]= $oc;
58 }
59 }
61 /* Load arrays */
62 foreach (array("goTimeSource") as $name){
63 $tmp= array();
64 if (isset($this->attrs[$name])){
65 for ($i= 0; $i<$this->attrs[$name]['count']; $i++){
66 $tmp[$this->attrs[$name][$i]]= $this->attrs[$name][$i];
67 }
68 }
69 $this->$name= $tmp;
70 }
73 $tmp =array();
74 $tmp2=array();
75 if(isset($this->attrs['goExportEntry'])){
76 unset($this->attrs['goExportEntry']['count']);
77 if((isset($this->attrs['goExportEntry']))&&(isset($this->attrs['goExportEntry']))){
78 foreach($this->attrs['goExportEntry'] as $entry){
79 $tmp2= split("\|",$entry);
80 $tmp[$tmp2[0]]= $entry;
81 }
82 }
83 }
84 $this->goExportEntry = $tmp;
86 /* Always is account... */
87 $this->is_account= TRUE;
89 /* Check if goShareServer is defined */
90 if((isset($this->attrs['objectClass']))&&(is_array($this->attrs['objectClass']))){
91 if(in_array("goShareServer",$this->attrs['objectClass'])){
92 $this->goShareServer = true;
93 }
94 }
95 }
97 function addToList($entry){
98 $key = key($entry);
99 $this->goExportEntry[$key]=$entry[$key];
100 }
102 function deleteFromList($id){
103 unset($this->goExportEntry[$id]);
104 }
106 function addToMountList($entry) {
107 $key = key($entry);
108 $type = $this->get_share_type($entry[$key]);
109 if (($type == "netatalk") || ($type == "NFS")) {
110 $this->mounts_to_add[$entry[$key]] = $entry[$key];
111 unset($this->mounts_to_remove[$entry[$key]]);
112 }
113 }
115 function deleteFromMountList($entry) {
116 $type = $this->get_share_type($entry);
117 if (($type == "netatalk") || ($type == "NFS")) {
118 $this->mounts_to_remove[$entry] = $entry;
119 unset($this->mounts_to_add[$entry]);
120 }
121 }
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
128 /* Fill templating stuff */
129 $smarty= get_smarty();
131 $smarty->assign("staticAddress", "");
133 if((isset($_POST['DelNfsEnt']))&&(isset($_POST['goExportEntry']))){
134 if($this->allow_mounts){
135 $this->deleteFromMountList($this->goExportEntry[$_POST['goExportEntry']]);
136 }
137 $this->deleteFromList($_POST['goExportEntry']);
138 }
140 if(isset($_POST['NewNfsAdd'])){
141 $this->oldone = NULL;
142 $this->o_subWindow = new servnfs($this->config,$this->acl, $this->allow_mounts, $this->dn);
143 $this->dialog = true;
144 }
146 if((isset($_POST['NewNfsEdit']))&&(isset($_POST['goExportEntry']))){
147 $entry = $this->goExportEntry[$_POST['goExportEntry']];
148 $add_mount=isset($this->mounts_to_add[$entry]);
149 $this->oldone=$entry;
150 $this->o_subWindow = new servnfs($this->config,$this->acl,$this->allow_mounts,$this->dn,$entry,$add_mount);
151 $this->dialog = true;
152 }
154 if(isset($this->o_subWindow)){
155 $this->o_subWindow->save_object(TRUE);
156 }
158 /* Save NFS setup */
159 if(isset($_POST['NFSsave'])){
160 if(count($this->o_subWindow->check())>0){
161 foreach($this->o_subWindow->check() as $msg) {
162 print_red($msg);
163 }
164 }else{
165 $this->o_subWindow->save_object();
166 $newone = $this->o_subWindow->save();
167 $this->addToList($newone);
168 if($this->allow_mounts){
169 if($this->oldone) {
170 $this->deleteFromMountList($this->oldone);
171 }
172 if ($this->o_subWindow->should_create_mount()) {
173 $this->addToMountList($newone);
174 }
175 }
176 unset($this->o_subWindow);
177 $this->dialog = false;
178 }
179 }
181 /* Cancel NFS setup */
182 if(isset($_POST['NFScancel'])){
183 $this->oldone = NULL;
184 unset($this->o_subWindow);
185 $this->dialog = false;
186 }
188 /* Execute NFS setup dialog*/
189 if(isset($this->o_subWindow)){
190 return $this->o_subWindow->execute();
191 }
193 /* Here we add a new entry */
194 if(isset($_POST['NewNTPAdd']) && $_POST['NewNTPExport'] != "") {
195 $this->goTimeSource[$_POST['NewNTPExport']]= $_POST['NewNTPExport'];
196 asort($this->goTimeSource);
197 }
199 /* Deleting an Entry, is a bit more complicated than adding one*/
200 if(isset($_POST['DelNTPEnt'])) {
201 foreach ($_POST['goTimeSource'] as $entry){
202 if (isset($this->goTimeSource[$entry])){
203 unset($this->goTimeSource[$entry]);
204 }
205 }
206 }
208 /* Attributes */
209 foreach ($this->attributes as $attr){
210 $smarty->assign("$attr", $this->$attr);
211 $smarty->assign("$attr"."ACL", chkacl($this->acl, $attr));
212 $smarty->assign($attr."State","");
213 }
215 $tellSmarty=array();
216 ksort($this->goExportEntry);
217 foreach($this->goExportEntry as $name=>$values){
218 $tmp = split("\|",$values);
219 $tellSmarty[$name] = $tmp[0]." ".$tmp[4]." (".$tmp[2].")";
220 }
221 $smarty->assign("goExportEntry",array_keys($tellSmarty));
222 $smarty->assign("goExportEntryKeys",($tellSmarty));
223 $smarty->assign("goExportEntryACL", chkacl($this->acl, "goExportEntry"));
225 $smarty->assign("goTimeSource", $this->goTimeSource);
226 $smarty->assign("goTimeSourceACL", chkacl($this->acl, "goTimeSource"));
227 $smarty->assign("goTimeSourceState","");
230 /* Classes... */
231 foreach ($this->additionaloc as $oc => $dummy){
232 if (isset($this->objectclasses[$oc])){
233 $smarty->assign("$oc", "checked");
234 $smarty->assign("$oc"."State", "");
235 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
237 } else {
238 $smarty->assign("$oc", "");
239 $smarty->assign("$oc"."ACL", chkacl($this->acl, $oc));
240 $smarty->assign("$oc"."State", "disabled");
241 }
242 }
244 if(!$this->goShareServer){
245 $smarty->assign("goShareServerState", " disabled ");
246 $smarty->assign("goExportEntryACL", " disabled ");
247 }else{
248 $smarty->assign("goShareServerState", " ");
249 $smarty->assign("goExportEntryACL", " ");
250 }
252 /* Different handling for checkbox */
253 if($this->goXdmcpIsEnabled == "true"){
254 $smarty->assign("goXdmcpIsEnabled","checked");
255 } else {
256 $smarty->assign("goXdmcpIsEnabled","");
257 }
259 return($smarty->fetch (get_template_path('servservice.tpl', TRUE)));
260 }
263 function remove_from_parent()
264 {
265 /* This cannot be removed... */
266 }
269 /* Save data to object */
270 function save_object()
271 {
272 plugin::save_object();
273 if (isset($_POST['servicetab'])){
274 $tmp = $this->goTimeSource;
276 if(isset($_POST['goLdapBase'])){
277 $this->goLdapBase = $_POST['goLdapBase'];
278 }
280 /* Save checkbox state */
281 foreach ($this->additionaloc as $oc => $dummy){
282 // if($oc == "goNtpServer") continue;
283 if (chkacl($this->acl, $oc) == ""){
284 if (isset($_POST[$oc]) && $_POST[$oc] == '1'){
285 $this->objectclasses[$oc]= $oc;
286 } else {
287 unset($this->objectclasses[$oc]);
288 }
289 }
290 }
292 /* Save xdmcp is enabled flag */
293 if (isset($_POST['goXdmcpIsEnabled'])){
294 $this->goXdmcpIsEnabled= "true";
295 } else {
296 $this->goXdmcpIsEnabled= "false";
297 }
299 /* Save xdmcp is enabled flag */
300 if (isset($_POST['goShareServer'])){
301 $this->goShareServer = true;
302 } else {
303 $this->goShareServer = false;
304 }
305 $this->goTimeSource = array();
306 $this->goTimeSource = $tmp;
307 }
308 }
311 /* Check supplied data */
312 function check()
313 {
314 /* Call common method to give check the hook */
315 $message= plugin::check();
317 if((isset($_POST['goTerminalServer']))&&(empty($this->goFontPath))){
318 $message[]=_("Terminal server, must have fontpath specified.");
319 }
321 return ($message);
322 }
325 /* Save to LDAP */
326 function save()
327 {
328 plugin::save();
330 $tmp= array();
332 /* Remove all from this plugin */
333 foreach($this->attrs['objectClass'] as $oc){
334 if (!in_array_ics($oc, $this->possible_objectclasses)){
335 $tmp[]= $oc;
336 }
337 }
339 /* Merge our current objectclasses */
340 foreach($this->objectclasses as $oc){
341 if (!in_array_ics($oc, $tmp)){
342 $tmp[]= $oc;
343 }
344 }
346 /* Reassign cleaned value */
347 $this->attrs['objectClass']= $tmp;
349 /* Arrays */
350 foreach (array("goTimeSource", "goExportEntry") as $name){
351 $this->attrs[$name]= array();
352 foreach ($this->$name as $element){
353 $this->attrs[$name][]= $element;
354 }
355 }
357 /* Process netatalk mounts */
358 if($this->allow_mounts) {
359 $this->process_mounts();
360 }
362 /* Remove illegal attributes */
363 foreach ($this->additionaloc as $oc => $attrs){
364 if (!in_array($oc, $this->objectclasses)){
365 foreach ($attrs as $attr){
366 $this->attrs[$attr]= array();
367 }
368 }
369 }
371 /* Write to LDAP */
372 $ldap= $this->config->get_ldap_link();
373 $ldap->cd($this->dn);
374 $this->cleanup();
375 $ldap->modify ($this->attrs);
377 show_ldap_error($ldap->get_error());
379 /* Optionally execute a command after we're done */
380 if ($this->initially_was_account == $this->is_account){
381 if ($this->is_modified){
382 $this->handle_post_events("mofify");
383 }
384 } else {
385 $this->handle_post_events("add");
386 }
387 }
389 function process_mounts() {
391 $clip = "cn=" . $this->cn . ",ou=servers,ou=systems,";
392 $mountsdn = "cn=mounts," . substr($this->dn, strlen($clip));
394 $mounts = array(
395 "objectClass" => "container",
396 "cn" => "mounts"
397 );
399 # load data from mounts container
400 $ldap = $this->config->get_ldap_link();
401 $ldap->cat($mountsdn);
402 $attrs = $ldap->fetch();
404 # mounts container not present yet, so we create it
405 if (count($attrs) == 0) {
406 $ldap->cd($mountsdn);
407 $ldap->add($mounts);
408 show_ldap_error($ldap->get_error());
409 gosa_log("Mount container '$mountsdn' has been created");
410 }
412 # remove deleted mounts from the container
413 foreach ($this->mounts_to_remove as $entry) {
414 $mount=$this->returnMountEntry($entry);
415 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
417 $ldap->cat($mountdn);
418 $attrs = $ldap->fetch();
420 if (count($attrs) != 0) {
421 $ldap->rmdir($mountdn);
422 show_ldap_error($ldap->get_error());
423 gosa_log("Mount object '".$mountdn."' has been removed");
424 }
425 }
427 # add new mounts to the container
428 foreach ($this->mounts_to_add as $entry) {
430 $mount=$this->returnMountEntry($entry);
431 $mountdn = "cn=".$mount["cn"].","."$mountsdn";
432 $ldap->cd($mountdn);
433 $ldap->add($mount);
434 show_ldap_error($ldap->get_error());
435 gosa_log("Mount object '".$mountdn."' has been added");
436 }
437 }
439 function get_share_type($share) {
440 $tmp = split("\|", $share);
441 return $tmp[2];
442 }
445 function returnMountEntry($entry) {
446 $item = split("\|", $entry);
447 $name = $item[0];
448 $description = $item[1];
449 $type = $item[2];
450 $charset = $item[3];
451 $path = $item[4];
452 $options = $item[5];
454 switch ($type) {
455 case "netatalk" : {
456 $mount = array(
457 "mountDirectory" => "/Network/Servers/",
458 "mountOption" => array(
459 "net",
460 "url==afp://;AUTH=NO%20USER%20AUTHENT@".$this->cn."/$name/"
461 ),
462 "mountType" => "url",
463 "objectClass" => "mount",
464 "cn" => $this->cn .":/".$name
465 );
466 break;
467 }
468 case "NFS" : {
469 $mount = array(
470 "mountDirectory" => "/Network/Servers/",
471 "mountOption" => "net",
472 "mountType" => "nfs",
473 "objectClass" => "mount",
474 "cn" => $this->cn .":".$path
475 );
476 break;
477 }
478 default : {
479 continue;
480 }
481 }
482 return $mount;
483 }
485 }
486 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
487 ?>