1 <?php
3 class workgeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage workstation base 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 /* Generic terminal attributes */
11 var $gotoMode= "locked";
12 var $gotoSyslogServer= "";
13 var $gotoSyslogServers= array();
14 var $gotoNtpServer= array();
15 var $gotoNtpServers= array();
16 var $gotoSndModule= "";
17 var $gotoFloppyEnable= "";
18 var $gotoCdromEnable= "";
19 var $ghCpuType= "-";
20 var $ghMemSize= "-";
21 var $ghUsbSupport= "-";
22 var $ghNetNic= array();
23 var $ghIdeDev= array();
24 var $ghScsiDev= array();
25 var $ghGfxAdapter= "-";
26 var $ghSoundAdapter= "-";
27 var $gotoLastUser= "-";
28 var $FAIscript= "";
29 var $didAction= FALSE;
30 var $FAIstate= "";
32 /* Needed values and lists */
33 var $base= "";
34 var $cn= "";
35 var $l= "";
36 var $orig_dn= "";
38 /* Plugin side filled */
39 var $modes= array();
41 var $netConfigDNS;
43 var $inheritTimeServer = true;
45 /* attribute list for save action */
46 var $ignore_account= TRUE;
47 var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
48 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
49 "ghCpuType", "ghMemSize", "ghUsbSupport",
50 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
51 var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
53 var $mapActions = array("reboot" => "localboot",
54 "instant_update" => "softupdate",
55 "update" => "sceduledupdate",
56 "reinstall" => "install",
57 "rescan" => "",
58 "memcheck" => "memcheck",
59 "sysinfo" => "sysinfo");
63 function workgeneric ($config, $dn= NULL, $parent= NULL)
64 {
65 plugin::plugin ($config, $dn, $parent);
66 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
68 /* Read arrays */
69 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
70 if (!isset($this->attrs[$val])){
71 continue;
72 }
73 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
74 array_push($this->$val, $this->attrs[$val][$i]);
75 }
76 }
78 /* Create used ntp server array */
79 $this->gotoNtpServer= array();
80 if(isset($this->attrs['gotoNtpServer'])){
81 $this->inheritTimeServer = false;
82 unset($this->attrs['gotoNtpServer']['count']);
83 foreach($this->attrs['gotoNtpServer'] as $server){
84 $this->gotoNtpServer[$server] = $server;
85 }
86 }
88 /* Set inherit checkbox state */
89 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
90 $this->inheritTimeServer = true;
91 $this->gotoNtpServer=array();
92 }
94 /* Create available ntp options */
95 $tmp = $this->config->data['SERVERS']['NTP'];
96 $this->gotoNtpServers = array();
97 foreach($tmp as $key => $server){
98 if($server == "default") continue;
99 $this->gotoNtpServers[$server] = $server;
100 }
102 $this->modes["active"]= _("Activated");
103 $this->modes["locked"]= _("Locked");
105 /* Set base */
106 if ($this->dn == "new"){
107 $ui= get_userinfo();
108 $this->base= dn2base($ui->dn);
109 } else {
110 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
111 }
113 /* Create an array of all Syslog servers */
114 $tmp = $this->config->data['SERVERS']['SYSLOG'];
115 foreach($tmp as $server){
116 $visible = $server;
117 if($server == "default") {
118 $visible = "["._("inherited")."]";
119 }
120 $this->gotoSyslogServers[$server] = $visible;
121 }
123 /* Save 'dn' for later referal */
124 $this->orig_dn= $this->dn;
125 }
128 function set_acl_base($base)
129 {
130 plugin::set_acl_base($base);
131 $this->netConfigDNS->set_acl_base($base);
132 }
134 function set_acl_category($cat)
135 {
136 plugin::set_acl_category($cat);
137 $this->netConfigDNS->set_acl_category($cat);
138 }
140 function execute()
141 {
142 /* Call parent execute */
143 plugin::execute();
145 /* Do we need to flip is_account state? */
146 if(isset($_POST['modify_state'])){
147 if($this->is_account && $this->acl_is_removeable()){
148 $this->is_account= FALSE;
149 }elseif(!$this->is_account && $this->acl_is_createable()){
150 $this->is_account= TRUE;
151 }
152 }
154 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
155 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
156 if ($cmd == ""){
157 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
158 } else {
159 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
160 if ($retval != 0){
161 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
162 } else {
164 /* Set FAIstate */
165 $ldap = $this->config->get_ldap_link();
166 $ldap->cd($this->config->current['BASE']);
167 $ldap->cat($this->dn,array("objectClass"));
168 $res = $ldap->fetch();
170 $attrs = array();
171 $attrs['FAIstate'] = "";
172 if(isset($this->mapActions[$_POST['saction']])){
173 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
174 }
176 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
177 $attrs['objectClass'][] = $res['objectClass'][$i];
178 }
180 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
181 $attrs['objectClass'][] = "FAIobject";
182 }
184 if($attrs['FAIstate'] == ""){
185 #FIXME we should check if FAIobject is used anymore
186 $attrs['FAIstate'] = array();
187 }
189 $ldap->cd($this->dn);
190 $ldap->modify($attrs);
191 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
193 $this->didAction= TRUE;
194 }
195 }
196 }
198 /* Do we represent a valid terminal? */
199 if (!$this->is_account && $this->parent == NULL){
200 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
201 _("This 'dn' has no workstation features.")."</b>";
202 return($display);
203 }
205 /* Base select dialog */
206 $once = true;
207 foreach($_POST as $name => $value){
208 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
209 $once = false;
210 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
211 $this->dialog->setCurrentBase($this->base);
212 }
213 }
215 /* Dialog handling */
216 if(is_object($this->dialog)){
217 /* Must be called before save_object */
218 $this->dialog->save_object();
220 if($this->dialog->isClosed()){
221 $this->dialog = false;
222 }elseif($this->dialog->isSelected()){
223 $this->base = $this->dialog->isSelected();
224 $this->dialog= false;
225 }else{
226 return($this->dialog->execute());
227 }
228 }
230 /* Add new ntp Server to our list */
231 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
232 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
233 }
235 /* Delete selected NtpServer for list of used servers */
236 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
237 foreach($_POST['gotoNtpServerSelected'] as $name){
238 unset($this->gotoNtpServer[$name]);
239 }
240 }
242 /* Fill templating stuff */
243 $smarty= get_smarty();
245 /* Create base acls */
246 $baseACL = $this->getacl("base");
247 if(!$this->acl_is_moveable()) {
248 $baseACL = preg_replace("/w/","",$baseACL);
249 }
250 $smarty->assign("baseACL", $baseACL);
252 /* Set acls */
253 $tmp = $this->plInfo();
254 foreach($tmp['plProvidedAcls'] as $name => $translation){
255 $smarty->assign($name."ACL",$this->getacl($name));
256 }
258 $smarty->assign("cn", $this->cn);
259 $smarty->assign("l", $this->l);
260 $smarty->assign("bases", $this->get_allowed_bases());
261 $smarty->assign("staticAddress", "");
263 $tmp = array();
264 foreach($this->gotoNtpServers as $server){
265 if(!in_array($server,$this->gotoNtpServer)){
266 $tmp[$server] = $server;
267 }
268 }
269 $smarty->assign("gotoNtpServers",$tmp);
271 /* Check if workstation is online */
272 $query= "fping -q -r 1 -t 500 ".$this->cn;
273 exec ($query, $dummy, $retval);
275 /* Offline */
276 if ($retval == 0){
277 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
278 "instant_update" => _("Instant update"),
279 "update" => _("Scheduled update"),
280 "reinstall" => _("Reinstall"),
281 "rescan" => _("Rescan hardware"),
282 "memcheck" => _("Memory test"),
283 "sysinfo" => _("System analysis")));
284 } else {
285 $smarty->assign("actions", array("wake" => _("Wake up"),
286 "reinstall" => _("Reinstall"),
287 "update" => _("Scheduled update"),
288 "memcheck" => _("Memory test"),
289 "sysinfo" => _("System analysis")));
290 }
291 /* Arrays */
292 $smarty->assign("modes", $this->modes);
293 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
294 $smarty->assign("syslogservers", $this->gotoSyslogServers);
296 $ntpser = array();
297 foreach($this->gotoNtpServers as $server){
298 if(!in_array($server,$this->gotoNtpServer)){
299 $ntpser[$server] = $server;
300 }
301 }
302 $smarty->assign("gotoNtpServers", $ntpser);
304 /* Variables */
305 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
306 $smarty->assign($val."_select", $this->$val);
307 }
309 /* tell smarty the inherit checkbox state */
310 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
312 /* Show main page */
313 $smarty->assign("netconfig", $this->netConfigDNS->execute());
314 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
315 }
317 function remove_from_parent()
318 {
319 if($this->acl_is_removeable()){
321 $this->netConfigDNS->remove_from_parent();
322 $ldap= $this->config->get_ldap_link();
323 $ldap->rmdir($this->dn);
324 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
326 /* Optionally execute a command after we're done */
327 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
329 /* Delete references to object groups */
330 $ldap->cd ($this->config->current['BASE']);
331 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
332 while ($ldap->fetch()){
333 $og= new ogroup($this->config, $ldap->getDN());
334 unset($og->member[$this->dn]);
335 $og->save ();
336 }
337 }
338 }
341 /* Save data to object */
342 function save_object()
343 {
344 plugin::save_object();
346 /* Save base, since this is no LDAP attribute */
347 if((isset($_POST['base'])) && ($this->acl_is_moveable())){
348 $this->set_acl_base('dummy,'.$_POST['base']);
349 if($this->acl_is_moveable()){
351 if(isset($this->config->idepartments[$_POST['base']])){
352 $this->base = $_POST['base'];
353 if ($_POST['base'] != $this->base){
354 $this->is_modified= TRUE;
355 }
356 }
357 }
358 }
359 $this->netConfigDNS->save_object();
361 /* Set inherit mode */
362 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
363 if(isset($_POST["inheritTimeServer"])){
364 $this->inheritTimeServer = true;
365 }else{
366 $this->inheritTimeServer = false;
367 }
368 }
370 }
373 /* Check supplied data */
374 function check()
375 {
376 /* Call common method to give check the hook */
377 $message= plugin::check();
378 $message= array_merge($message, $this->netConfigDNS->check());
380 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
381 if ($this->orig_dn == "new" && !$this->acl_is_createable()){
382 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
383 }
386 if ($this->cn == ""){
387 $message[]= _("The required field 'Workstation name' is not set.");
388 }
390 if ($this->orig_dn != $this->dn){
391 $ldap= $this->config->get_ldap_link();
392 $ldap->cd ($this->base);
393 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
394 if ($ldap->count() != 0){
395 while ($attrs= $ldap->fetch()){
396 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
397 continue;
398 } else {
399 if ($attrs['dn'] != $this->orig_dn){
400 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
401 break;
402 }
403 }
404 }
405 }
406 }
408 /* Check for valid ntpServer selection */
409 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
410 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
411 }
413 return ($message);
414 }
417 /* Save to LDAP */
418 function save()
419 {
420 plugin::save();
422 /* Strip out 'default' values */
423 foreach (array("gotoSyslogServer") as $val){
425 if ($this->attrs[$val] == "default"){
426 $this->attrs[$val]= array();
427 }
428 }
430 /* Add missing arrays */
431 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
432 if (isset ($this->$val) && count ($this->$val) != 0){
433 $this->attrs["$val"]= $this->$val;
434 }
435 }
437 /* Remove all empty values */
438 if ($this->orig_dn == 'new'){
439 $attrs= array();
440 foreach ($this->attrs as $key => $val){
441 if (is_array($val) && count($val) == 0){
442 continue;
443 }
444 $attrs[$key]= $val;
445 }
446 $this->attrs= $attrs;
447 }
449 /* Update ntp server settings */
450 if($this->inheritTimeServer){
451 if($this->is_new){
452 if(isset($this->attrs['gotoNtpServer'])){
453 unset($this->attrs['gotoNtpServer']);
454 }
455 }else{
456 $this->attrs['gotoNtpServer'] = array();
457 }
458 }else{
459 /* Set ntpServers */
460 $this->attrs['gotoNtpServer'] = array();
461 foreach($this->gotoNtpServer as $server){
462 $this->attrs['gotoNtpServer'][] = $server;
463 }
464 }
466 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
467 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
468 }
470 /* Write back to ldap */
471 $ldap= $this->config->get_ldap_link();
472 if ($this->orig_dn == 'new'){
473 $ldap->cd($this->config->current['BASE']);
474 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
475 $ldap->cd($this->dn);
476 $ldap->add($this->attrs);
477 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
478 if(!$this->didAction){
479 $this->handle_post_events("add");
480 }
481 } else {
482 if ($this->orig_dn != $this->dn){
483 $this->move($this->orig_dn, $this->dn);
484 }
485 $ldap->cd($this->dn);
486 $this->cleanup();
487 $ldap->modify ($this->attrs);
489 if(!$this->didAction){
490 $this->handle_post_events("modify");
491 }
492 }
493 $this->netConfigDNS->cn = $this->cn;
494 $this->netConfigDNS->save($this->dn);
495 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
496 }
499 /* Return plugin informations for acl handling
500 #FIXME FAIscript seams to ununsed within this class... */
501 function plInfo()
502 {
503 return (array(
504 "plShortName" => _("Generic"),
505 "plDescription" => _("Workstation generic"),
506 "plSelfModify" => FALSE,
507 "plDepends" => array(),
508 "plPriority" => 0,
509 "plSection" => array("administration"),
510 "plCategory" => array("workstation" => array("description" => _("Workstation"),
511 "objectClass" => "gotoWorkstation")),
512 "plProvidedAcls"=> array(
513 "cn" => _("Workstation name"),
514 "l" => _("Location") ,
515 "base" => _("Base") ,
516 "gotoMode" => _("Goto mode"),
517 "gotoSyslogServer" => _("Syslog server"),
518 "gotoNtpServer" => _("Ntp server"),
519 "FAIstate" => _("Action flag"))
520 ));
521 }
523 }
525 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
526 ?>