1 <?php
3 class workgeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $gotoMode= "locked";
7 var $gotoSyslogServer= "";
8 var $gotoSyslogServers= array();
9 var $gotoNtpServer= array();
10 var $gotoNtpServers= array();
11 var $gotoSndModule= "";
12 var $gotoFloppyEnable= "";
13 var $gotoCdromEnable= "";
14 var $description= "";
15 var $ghCpuType= "-";
16 var $ghMemSize= "-";
17 var $ghUsbSupport= "-";
18 var $ghNetNic= array();
19 var $ghIdeDev= array();
20 var $ghScsiDev= array();
21 var $ghGfxAdapter= "-";
22 var $ghSoundAdapter= "-";
23 var $gotoLastUser= "-";
24 var $FAIscript= "";
25 var $FAIstate= "";
26 var $view_logged = FALSE;
28 /* Needed values and lists */
29 var $base= "";
30 var $cn= "";
31 var $l= "";
32 var $orig_dn= "";
34 /* Plugin side filled */
35 var $modes= array();
37 var $netConfigDNS;
39 var $inheritTimeServer = true;
41 /* attribute list for save action */
42 var $ignore_account= TRUE;
43 var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
44 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
45 "ghCpuType", "ghMemSize", "ghUsbSupport", "description",
46 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
47 var $objectclasses= array("top", "gotoWorkstation", "GOhard");
49 var $mapActions = array("reboot" => "",
50 "localboot" => "localboot",
51 "halt" => "",
52 "update" => "softupdate",
53 "reinstall" => "install",
54 "rescan" => "",
55 "wake" => "",
56 "memcheck" => "memcheck",
57 "sysinfo" => "sysinfo");
60 var $fai_activated = FALSE;
62 var $member_of_ogroup = FALSE;
64 function workgeneric (&$config, $dn= NULL, $parent= NULL)
65 {
66 $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
67 if(!empty($tmp)){
68 $this->fai_activated = TRUE;
69 }
71 plugin::plugin ($config, $dn, $parent);
73 if(!isset($this->parent->by_object['ogroup'])){
74 $ldap = $this->config->get_ldap_link();
75 $ldap->cd ($this->config->current['BASE']);
76 $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn"));
77 $this->member_of_ogroup = $ldap->count() >= 1;
78 }
80 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses);
82 /* Read arrays */
83 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
84 if (!isset($this->attrs[$val])){
85 continue;
86 }
87 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
88 array_push($this->$val, $this->attrs[$val][$i]);
89 }
90 }
92 /* Create used ntp server array */
93 $this->gotoNtpServer= array();
94 if(isset($this->attrs['gotoNtpServer'])){
95 $this->inheritTimeServer = false;
96 unset($this->attrs['gotoNtpServer']['count']);
97 foreach($this->attrs['gotoNtpServer'] as $server){
98 $this->gotoNtpServer[$server] = $server;
99 }
100 }
102 /* Set inherit checkbox state */
103 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
104 $this->inheritTimeServer = true;
105 $this->gotoNtpServer=array();
106 }
108 /* You can't inherit the NTP service, if we are not member in an object group */
109 if(!$this->member_of_ogroup){
110 $this->inheritTimeServer = FALSE;
111 }
113 /* Create available ntp options */
114 $tmp = $this->config->data['SERVERS']['NTP'];
115 $this->gotoNtpServers = array();
116 foreach($tmp as $key => $server){
117 if($server == "default") continue;
118 $this->gotoNtpServers[$server] = $server;
119 }
121 $this->modes["active"]= _("Activated");
122 $this->modes["locked"]= _("Locked");
124 /* Set base */
125 if ($this->dn == "new"){
126 $ui= get_userinfo();
127 $this->base= dn2base($ui->dn);
128 } else {
129 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
130 }
132 /* Create an array of all Syslog servers */
133 $tmp = $this->config->data['SERVERS']['SYSLOG'];
134 foreach($tmp as $server){
135 $visible = $server;
136 if($server == "default" && $this->member_of_ogroup) {
137 $visible = "["._("inherited")."]";
138 }
139 $this->gotoSyslogServers[$server] = $visible;
140 }
142 /* Save 'dn' for later referal */
143 $this->orig_dn= $this->dn;
144 }
147 function set_acl_base($base)
148 {
149 plugin::set_acl_base($base);
150 $this->netConfigDNS->set_acl_base($base);
151 }
153 function set_acl_category($cat)
154 {
155 plugin::set_acl_category($cat);
156 $this->netConfigDNS->set_acl_category($cat);
157 }
159 function execute()
160 {
161 /* Call parent execute */
162 plugin::execute();
164 if($this->is_account && !$this->view_logged){
165 $this->view_logged = TRUE;
166 new log("view","workstation/".get_class($this),$this->dn);
167 }
169 /* Do we need to flip is_account state? */
170 if(isset($_POST['modify_state'])){
171 if($this->is_account && $this->acl_is_removeable()){
172 $this->is_account= FALSE;
173 }elseif(!$this->is_account && $this->acl_is_createable()){
174 $this->is_account= TRUE;
175 }
176 }
178 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
179 $action= $_POST['saction'];
181 /* Check if we have an DaemonEvent for this action */
182 if(class_available("DaemonEvent")){
183 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
184 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
185 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
186 $tmp = new $evt['CLASS_NAME']($this->config);
187 $tmp->add_targets(array($this->netConfigDNS->macAddress));
188 $tmp->set_type(TRIGGERED_EVENT);
189 $o_queue = new gosaSupportDaemon();
190 if(!$o_queue->append($tmp)){
191 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
192 $o_queue->get_error()),ERROR_DIALOG);
193 }
194 }
195 }
197 }
199 /* Do we represent a valid terminal? */
200 if (!$this->is_account && $this->parent === NULL){
201 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
202 _("This 'dn' has no workstation features.")."</b>";
203 return($display);
204 }
206 /* Base select dialog */
207 $once = true;
208 foreach($_POST as $name => $value){
209 if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
210 $once = false;
211 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
212 $this->dialog->setCurrentBase($this->base);
213 }
214 }
216 /* Dialog handling */
217 if(is_object($this->dialog)){
218 /* Must be called before save_object */
219 $this->dialog->save_object();
221 if($this->dialog->isClosed()){
222 $this->dialog = false;
223 }elseif($this->dialog->isSelected()){
225 /* A new base was selected, check if it is a valid one */
226 $tmp = $this->get_allowed_bases();
227 if(isset($tmp[$this->dialog->isSelected()])){
228 $this->base = $this->dialog->isSelected();
229 }
231 $this->dialog= false;
232 }else{
233 return($this->dialog->execute());
234 }
235 }
237 /* Add new ntp Server to our list */
238 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
239 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
240 }
242 /* Delete selected NtpServer for list of used servers */
243 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
244 foreach($_POST['gotoNtpServerSelected'] as $name){
245 unset($this->gotoNtpServer[$name]);
246 }
247 }
249 /* Fill templating stuff */
250 $smarty= get_smarty();
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("description", $this->description);
260 $smarty->assign("l", $this->l);
261 $smarty->assign("bases", $this->get_allowed_bases());
262 $smarty->assign("staticAddress", "");
264 $tmp = array();
265 foreach($this->gotoNtpServers as $server){
266 if(!in_array($server,$this->gotoNtpServer)){
267 $tmp[$server] = $server;
268 }
269 }
270 $smarty->assign("gotoNtpServers",$tmp);
272 /* Check if workstation is online */
273 if (gosaSupportDaemon::ping($this->netConfigDNS->macAddress)){
274 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
275 "instant_update" => _("Instant update"),
276 "update" => _("Scheduled update"),
277 "reinstall" => _("Reinstall"),
278 "rescan" => _("Rescan hardware"),
279 "memcheck" => _("Memory test"),
280 "localboot" => _("Force localboot"),
281 "sysinfo" => _("System analysis")));
282 } else {
283 $smarty->assign("actions", array("wake" => _("Wake up"),
284 "reinstall" => _("Reinstall"),
285 "update" => _("Scheduled update"),
286 "memcheck" => _("Memory test"),
287 "localboot" => _("Force localboot"),
288 "sysinfo" => _("System analysis")));
289 }
290 /* Arrays */
291 $smarty->assign("modes", $this->modes);
292 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
293 $smarty->assign("syslogservers", $this->gotoSyslogServers);
294 $smarty->assign("fai_activated",$this->fai_activated);
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);
311 $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
313 $str = $this->netConfigDNS->execute();
314 if(is_object($this->netConfigDNS->dialog)){
315 return($str);
316 }
317 $smarty->assign("netconfig", $str);
319 /* Show main page */
320 return($smarty->fetch (get_template_path('workstation.tpl', TRUE, dirname(__FILE__))));
321 }
323 function remove_from_parent()
324 {
325 if($this->acl_is_removeable()){
327 $this->netConfigDNS->remove_from_parent();
328 $ldap= $this->config->get_ldap_link();
329 $ldap->rmdir($this->dn);
330 new log("remove","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
331 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
333 /* Optionally execute a command after we're done */
334 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
336 /* Delete references to object groups */
337 $ldap->cd ($this->config->current['BASE']);
338 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
339 while ($ldap->fetch()){
340 $og= new ogroup($this->config, $ldap->getDN());
341 unset($og->member[$this->dn]);
342 $og->save ();
343 }
344 }
346 if(isset($_POST["inheritAll"])){
347 $this->set_everything_to_inherited();
348 }
349 }
352 /* Save data to object */
353 function save_object()
354 {
356 /* Create a base backup and reset the
357 base directly after calling plugin::save_object();
358 Base will be set seperatly a few lines below */
359 $base_tmp = $this->base;
360 plugin::save_object();
361 $this->base = $base_tmp;
363 /* Save base, since this is no LDAP attribute */
364 $tmp = $this->get_allowed_bases();
365 if(isset($_POST['base'])){
366 if(isset($tmp[$_POST['base']])){
367 $this->base= $_POST['base'];
368 }
369 }
371 $this->netConfigDNS->save_object();
373 /* Set inherit mode */
374 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
375 if(isset($_POST["inheritTimeServer"]) && $this->member_of_ogroup){
376 $this->inheritTimeServer = true;
377 }else{
378 $this->inheritTimeServer = false;
379 }
380 }
382 if(isset($_POST["inheritAll"])){
383 $this->set_everything_to_inherited();
384 }
385 }
388 /* Check supplied data */
389 function check()
390 {
391 /* Call common method to give check the hook */
392 $message= plugin::check();
394 /* Skip IP & Mac checks if this is a template */
395 if($this->cn != "wdefault"){
396 $message= array_merge($message, $this->netConfigDNS->check());
397 }
399 $this->dn= "cn=".$this->cn.",".get_ou('workstationou').$this->base;
401 if ($this->cn == ""){
402 $message[]= _("The required field 'Workstation name' is not set.");
403 }
405 /* Check if given name is a valid host/dns name */
406 if(!tests::is_dns_name($this->cn)){
407 $message[] = _("Please specify a valid name for this object.");
408 }
410 if ($this->orig_dn != $this->dn){
411 $ldap= $this->config->get_ldap_link();
412 $ldap->cd ($this->base);
414 if($this->cn == "wdefault"){
415 $ldap->cat($this->dn);
416 }else{
417 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
418 }
419 if ($ldap->count() != 0){
420 while ($attrs= $ldap->fetch()){
421 if (preg_match("/cn=dhcp,/",$attrs['dn']) || preg_match ("/,".get_ou('incomingou')."/", $ldap->getDN())){
422 continue;
423 } else {
424 if ($attrs['dn'] != $this->orig_dn){
425 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
426 break;
427 }
428 }
429 }
430 }
431 }
433 /* Check for valid ntpServer selection */
434 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
435 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
436 }
438 return ($message);
439 }
442 /* Save to LDAP */
443 function save()
444 {
445 /* Detect mode changes */
446 $activate= (isset($this->saved_attributes['gotoMode']) &&
447 $this->gotoMode != $this->saved_attributes['gotoMode'] &&
448 $this->gotoMode == "active" &&
449 tests::is_ip($this->netConfigDNS->ipHostNumber));
451 plugin::save();
453 /* Strip out 'default' values */
454 foreach (array("gotoSyslogServer") as $val){
456 if (isset($this->attrs[$val]) && $this->attrs[$val] == "default"){
457 $this->attrs[$val]= array();
458 }
459 }
461 /* Add missing arrays */
462 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
463 if (isset ($this->$val) && count ($this->$val) != 0){
464 $this->attrs["$val"]= $this->$val;
465 }
466 }
468 /* Remove all empty values */
469 if ($this->orig_dn == 'new'){
470 $attrs= array();
471 foreach ($this->attrs as $key => $val){
472 if (is_array($val) && count($val) == 0){
473 continue;
474 }
475 $attrs[$key]= $val;
476 }
477 $this->attrs= $attrs;
478 }
480 /* Update ntp server settings */
481 if($this->inheritTimeServer){
482 if($this->is_new){
483 if(isset($this->attrs['gotoNtpServer'])){
484 unset($this->attrs['gotoNtpServer']);
485 }
486 }else{
487 $this->attrs['gotoNtpServer'] = array();
488 }
489 }else{
490 /* Set ntpServers */
491 $this->attrs['gotoNtpServer'] = array();
492 foreach($this->gotoNtpServer as $server){
493 $this->attrs['gotoNtpServer'][] = $server;
494 }
495 }
497 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
498 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
499 }
501 /* Write back to ldap */
502 $ldap= $this->config->get_ldap_link();
503 if ($this->orig_dn == 'new'){
504 $ldap->cd($this->config->current['BASE']);
505 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
506 $ldap->cd($this->dn);
507 $ldap->add($this->attrs);
508 new log("create","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
509 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
510 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
511 } else {
512 if ($this->orig_dn != $this->dn){
513 $this->move($this->orig_dn, $this->dn);
514 }
515 $ldap->cd($this->dn);
516 $this->cleanup();
517 $ldap->modify ($this->attrs);
518 new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
520 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
521 }
523 /* cn=default and macAddress=- indicates that this is a template */
524 if($this->cn == "wdefault"){
525 $this->netConfigDNS->macAddress = "-";
526 }
528 $this->netConfigDNS->cn = $this->cn;
529 $this->netConfigDNS->save();
530 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
532 if ($activate && class_available("DaemonEvent")){
534 /* Send installation activation
535 */
536 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
537 $o_queue = new gosaSupportDaemon();
538 if(isset($events['TRIGGERED']['DaemonEvent_installation_activation'])){
539 $evt = $events['TRIGGERED']['DaemonEvent_installation_activation'];
540 $tmp = new $evt['CLASS_NAME']($this->config);
541 $tmp->set_type(TRIGGERED_EVENT);
542 $tmp->add_targets(array($this->netConfigDNS->macAddress));
543 if(!$o_queue->append($tmp)){
544 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
545 $o_queue->get_error()),ERROR_DIALOG);
546 }
547 }
548 }
549 }
552 /* Display generic part for server copy & paste */
553 function getCopyDialog()
554 {
555 $vars = array("cn");
556 $smarty = get_smarty();
557 $smarty->assign("cn" ,$this->cn);
558 $smarty->assign("object","workstation");
559 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
560 $ret = array();
561 $ret['string'] = $str;
562 $ret['status'] = "";
563 return($ret);
564 }
567 function saveCopyDialog()
568 {
569 if(isset($_POST['cn'])){
570 $this->cn = $_POST['cn'];
571 }
572 }
575 function PrepareForCopyPaste($source)
576 {
577 plugin::PrepareForCopyPaste($source);
578 if(isset($source['macAddress'][0])){
579 $this->netConfigDNS->macAddress = $source['macAddress'][0];
580 }
581 if(isset($source['ipHostNumber'][0])){
582 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
583 }
585 /* Create used ntp server array */
586 $this->gotoNtpServer= array();
587 if(isset($source['gotoNtpServer'])){
588 $this->inheritTimeServer = false;
589 unset($source['gotoNtpServer']['count']);
590 foreach($source['gotoNtpServer'] as $server){
591 $this->gotoNtpServer[$server] = $server;
592 }
593 }
595 /* Set inherit checkbox state */
596 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer)==0)){
597 $this->inheritTimeServer = true;
598 $this->gotoNtpServer=array();
599 }
600 }
603 /* Return plugin informations for acl handling
604 #FIXME FAIscript seams to ununsed within this class... */
605 static function plInfo()
606 {
607 return (array(
608 "plShortName" => _("Generic"),
609 "plDescription" => _("Workstation generic"),
610 "plSelfModify" => FALSE,
611 "plDepends" => array(),
612 "plPriority" => 0,
613 "plSection" => array("administration"),
614 "plCategory" => array("workstation" => array("description" => _("Workstation"),
615 "objectClass" => "gotoWorkstation")),
616 "plProvidedAcls"=> array(
617 "cn" => _("Workstation name"),
618 "description" => _("Description") ,
619 "l" => _("Location") ,
620 "base" => _("Base") ,
621 "gotoMode" => _("Goto mode"),
622 "gotoSyslogServer" => _("Syslog server"),
623 "gotoNtpServer" => _("Ntp server"),
624 "gotoRootPasswd" => _("Root password"),
625 "FAIstate" => _("Action flag"))
626 ));
627 }
629 function set_everything_to_inherited()
630 {
631 $this->gotoSyslogServer = "default";
632 $this->inheritTimeServer = TRUE;
634 /* Set workstation service attributes to inherited */
635 if($this->member_of_ogroup && isset($this->parent->by_object['workservice'])){
636 foreach(array("gotoXKbLayout","gotoXKbModel","gotoXKbVariant",
637 "gotoXResolution","gotoXColordepth","gotoXMouseType","gotoXMouseport") as $name){
638 $this->parent->by_object['workservice']->$name = "default";
639 }
640 }
642 /* Set workstation startup attributes to inherited */
643 if($this->member_of_ogroup && isset($this->parent->by_object['workstartup'])){
644 $this->parent->by_object['workstartup']->gotoBootKernel = "default-inherited";
645 $this->parent->by_object['workstartup']->gotoLdapServer = "default-inherited";
646 $this->parent->by_object['workstartup']->FAIdebianMirror= "inherited";
647 }
648 }
649 }
650 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
651 ?>