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");
62 var $fai_activated = FALSE;
64 function workgeneric ($config, $dn= NULL, $parent= NULL)
65 {
66 /* Check if FAI is activated */
67 $tmp = search_config($config->data,"faiManagement","CLASS");
68 if(!empty($tmp)){
69 $this->fai_activated = TRUE;
70 }
72 plugin::plugin ($config, $dn, $parent);
73 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
74 $this->netConfigDNS->acl = $this->acl;
76 /* Read arrays */
77 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
78 if (!isset($this->attrs[$val])){
79 continue;
80 }
81 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
82 array_push($this->$val, $this->attrs[$val][$i]);
83 }
84 }
86 /* Create used ntp server array */
87 $this->gotoNtpServer= array();
88 if(isset($this->attrs['gotoNtpServer'])){
89 $this->inheritTimeServer = false;
90 unset($this->attrs['gotoNtpServer']['count']);
91 foreach($this->attrs['gotoNtpServer'] as $server){
92 $this->gotoNtpServer[$server] = $server;
93 }
94 }
96 /* Set inherit checkbox state */
97 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
98 $this->inheritTimeServer = true;
99 $this->gotoNtpServer=array();
100 }
102 /* Create available ntp options */
103 $tmp = $this->config->data['SERVERS']['NTP'];
104 $this->gotoNtpServers = array();
105 foreach($tmp as $key => $server){
106 if($server == "default") continue;
107 $this->gotoNtpServers[$server] = $server;
108 }
110 $this->modes["active"]= _("Activated");
111 $this->modes["locked"]= _("Locked");
113 /* Set base */
114 if ($this->dn == "new"){
115 $ui= get_userinfo();
116 $this->base= dn2base($ui->dn);
117 } else {
118 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
119 }
121 /* Create an array of all Syslog servers */
122 $tmp = $this->config->data['SERVERS']['SYSLOG'];
123 foreach($tmp as $server){
124 $visible = $server;
125 if($server == "default") {
126 $visible = "["._("inherited")."]";
127 }
128 $this->gotoSyslogServers[$server] = $visible;
129 }
131 /* Save 'dn' for later referal */
132 $this->orig_dn= $this->dn;
133 }
135 function execute()
136 {
137 /* Call parent execute */
138 plugin::execute();
139 $this->netConfigDNS->acl = $this->acl;
141 /* Do we need to flip is_account state? */
142 if (isset($_POST['modify_state'])){
143 $this->is_account= !$this->is_account;
144 }
146 if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
147 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
148 if ($cmd == ""){
149 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
150 } else {
151 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
152 if ($retval != 0){
153 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
154 } elseif ($_POST['saction'] != "wake") {
156 if($this->fai_activated && $this->dn != "new"){
157 /* Set FAIstate */
158 $ldap = $this->config->get_ldap_link();
159 $ldap->cd($this->config->current['BASE']);
160 $ldap->cat($this->dn,array("objectClass"));
161 $res = $ldap->fetch();
163 $attrs = array();
164 $attrs['FAIstate'] = "";
165 if(isset($this->mapActions[$_POST['saction']])){
166 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
167 }
169 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
170 $attrs['objectClass'][] = $res['objectClass'][$i];
171 }
173 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
174 $attrs['objectClass'][] = "FAIobject";
175 }
177 if($attrs['FAIstate'] == ""){
178 #FIXME we should check if FAIobject is used anymore
179 $attrs['FAIstate'] = array();
180 }
182 $ldap->cd($this->dn);
183 $ldap->modify($attrs);
184 show_ldap_error($ldap->get_error());
186 $this->didAction= TRUE;
187 }
188 }
189 }
190 }
192 /* Do we represent a valid terminal? */
193 if (!$this->is_account && $this->parent == NULL){
194 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
195 _("This 'dn' has no workstation features.")."</b>";
196 return($display);
197 }
199 /* Base select dialog */
200 $once = true;
201 foreach($_POST as $name => $value){
202 if(preg_match("/^chooseBase/",$name) && $once){
203 $once = false;
204 $this->dialog = new baseSelectDialog($this->config);
205 $this->dialog->setCurrentBase($this->base);
206 }
207 }
209 /* Dialog handling */
210 if(is_object($this->dialog)){
211 /* Must be called before save_object */
212 $this->dialog->save_object();
214 if($this->dialog->isClosed()){
215 $this->dialog = false;
216 }elseif($this->dialog->isSelected()){
217 $this->base = $this->dialog->isSelected();
218 $this->dialog= false;
219 }else{
220 return($this->dialog->execute());
221 }
222 }
224 /* Add new ntp Server to our list */
225 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer") == ""){
226 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
227 }
229 /* Delete selected NtpServer for list of used servers */
230 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer") == ""){
231 foreach($_POST['gotoNtpServerSelected'] as $name){
232 unset($this->gotoNtpServer[$name]);
233 }
234 }
236 /* Fill templating stuff */
237 $smarty= get_smarty();
238 $smarty->assign("cn", $this->cn);
239 $smarty->assign("l", $this->l);
240 $smarty->assign("bases", $this->config->idepartments);
241 $smarty->assign("staticAddress", "");
242 $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
244 /* Check if workstation is online */
245 $query= "fping -q -r 1 -t 500 ".$this->cn;
246 exec ($query, $dummy, $retval);
248 foreach($this->attributes as $attrs ){
249 $smarty->assign($attrs."ACL",chkacl($this->acl,$attrs));
250 }
252 /* Offline */
253 if ($retval == 0){
254 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
255 "instant_update" => _("Instant update"),
256 "update" => _("Scheduled update"),
257 "reinstall" => _("Reinstall"),
258 "rescan" => _("Rescan hardware"),
259 "memcheck" => _("Memory test"),
260 "sysinfo" => _("System analysis")));
261 } else {
262 $smarty->assign("actions", array("wake" => _("Wake up"),
263 "reinstall" => _("Reinstall"),
264 "update" => _("Scheduled update"),
265 "memcheck" => _("Memory test"),
266 "sysinfo" => _("System analysis")));
267 }
268 /* Arrays */
269 $smarty->assign("modes", $this->modes);
270 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
271 $smarty->assign("syslogservers", $this->gotoSyslogServers);
272 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
274 /* Variables */
275 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
276 $smarty->assign($val."_select", $this->$val);
277 $smarty->assign($val."ACL", chkacl($this->acl, $val));
278 }
279 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
281 /* tell smarty the inherit checkbox state */
282 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
284 /* Show main page */
285 $this->netConfigDNS->cn= $this->cn;
286 $smarty->assign("netconfig", $this->netConfigDNS->execute());
287 $smarty->assign("fai_activated",$this->fai_activated);
288 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
289 }
291 function remove_from_parent()
292 {
293 $this->netConfigDNS->acl = $this->acl;
294 $this->netConfigDNS->remove_from_parent();
295 $ldap= $this->config->get_ldap_link();
296 $ldap->rmdir($this->dn);
297 show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
299 /* Optionally execute a command after we're done */
300 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
302 /* Delete references to object groups */
303 $ldap->cd ($this->config->current['BASE']);
304 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
305 while ($ldap->fetch()){
306 $og= new ogroup($this->config, $ldap->getDN());
307 unset($og->member[$this->dn]);
308 $og->save ();
309 }
310 }
313 /* Save data to object */
314 function save_object()
315 {
316 plugin::save_object();
318 /* Save base, since this is no LDAP attribute */
319 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
320 $this->base= $_POST['base'];
321 }
322 $this->netConfigDNS->save_object();
324 /* Set inherit mode */
325 if(isset($_POST['workgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
326 if(isset($_POST["inheritTimeServer"])){
327 $this->inheritTimeServer = true;
328 }else{
329 $this->inheritTimeServer = false;
330 }
331 }
332 }
335 /* Check supplied data */
336 function check()
337 {
338 /* Call common method to give check the hook */
339 $message= plugin::check();
341 if($this->cn != "wdefault"){
342 $message= array_merge($message, $this->netConfigDNS->check());
343 }
345 $ui= get_userinfo();
346 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
347 $acl= get_permissions ($this->dn, $ui->subtreeACL);
348 $acl= get_module_permission($acl, "group", $this->dn);
349 if (chkacl($acl, "create") != ""){
350 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
351 }
353 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
354 $message[]= _("The required field 'Workstation name' is not set.");
355 }
357 if ($this->orig_dn != $this->dn){
358 $ldap= $this->config->get_ldap_link();
359 $ldap->cd ($this->base);
361 if($this->cn == "wdefault"){
362 $ldap->cat($this->dn);
363 }else{
364 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
365 }
366 if ($ldap->count() != 0){
367 while ($attrs= $ldap->fetch()){
368 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
369 continue;
370 } else {
371 if ($attrs['dn'] != $this->orig_dn){
372 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
373 break;
374 }
375 }
376 }
377 }
378 }
381 /* Check for valid ntpServer selection */
382 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
383 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
384 }
386 return ($message);
387 }
390 /* Save to LDAP */
391 function save()
392 {
393 $this->netConfigDNS->acl = $this->acl;
394 plugin::save();
396 /* Strip out 'default' values */
397 foreach (array("gotoSyslogServer") as $val){
398 if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
399 $this->attrs[$val]= array();
400 }
401 }
403 /* Add missing arrays */
404 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
405 if (isset ($this->$val) && count ($this->$val) != 0){
406 $this->attrs["$val"]= $this->$val;
407 }
408 }
410 /* Remove all empty values */
411 if ($this->orig_dn == 'new'){
412 $attrs= array();
413 foreach ($this->attrs as $key => $val){
414 if (is_array($val) && count($val) == 0){
415 continue;
416 }
417 $attrs[$key]= $val;
418 }
419 $this->attrs= $attrs;
420 }
422 /* Update ntp server settings */
423 if($this->inheritTimeServer){
424 if($this->new){
425 if(isset($this->attrs['gotoNtpServer'])){
426 unset($this->attrs['gotoNtpServer']);
427 }
428 }else{
429 $this->attrs['gotoNtpServer'] = array();
430 }
431 }else{
432 /* Set ntpServers */
433 $this->attrs['gotoNtpServer'] = array();
434 foreach($this->gotoNtpServer as $server){
435 $this->attrs['gotoNtpServer'][] = $server;
436 }
437 }
439 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
440 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
441 }
443 /* Write back to ldap */
444 $ldap= $this->config->get_ldap_link();
445 if ($this->orig_dn == 'new'){
446 $ldap->cd($this->config->current['BASE']);
447 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
448 $ldap->cd($this->dn);
449 $ldap->add($this->attrs);
450 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
451 if(!$this->didAction){
452 $this->handle_post_events("add");
453 }
454 } else {
455 if ($this->orig_dn != $this->dn){
456 $this->move($this->orig_dn, $this->dn);
457 }
458 $ldap->cd($this->dn);
459 $this->cleanup();
460 $ldap->modify ($this->attrs);
462 if(!$this->didAction){
463 $this->handle_post_events("modify");
464 }
465 }
467 /* Set macAddress to '-' this indicates that this is a Workstation Template */
468 if($this->cn == "wdefault"){
469 $this->netConfigDNS->macAddress = "-";
470 }
472 $this->netConfigDNS->cn = $this->cn;
473 $this->netConfigDNS->save($this->dn);
474 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
475 }
477 }
479 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
480 ?>