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 $ghCpuType= "-";
15 var $ghMemSize= "-";
16 var $ghUsbSupport= "-";
17 var $ghNetNic= array();
18 var $ghIdeDev= array();
19 var $ghScsiDev= array();
20 var $ghGfxAdapter= "-";
21 var $ghSoundAdapter= "-";
22 var $gotoLastUser= "-";
23 var $FAIscript= "";
24 var $didAction= FALSE;
25 var $FAIstate= "";
27 /* Needed values and lists */
28 var $base= "";
29 var $cn= "";
30 var $l= "";
31 var $orig_dn= "";
33 /* Plugin side filled */
34 var $modes= array();
36 var $netConfigDNS;
38 var $inheritTimeServer = true;
40 /* attribute list for save action */
41 var $ignore_account= TRUE;
42 var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
43 "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44 "ghCpuType", "ghMemSize", "ghUsbSupport", "FAIstate",
45 "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
46 var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
48 var $mapActions = array("reboot" => "",
49 "localboot" => "localboot",
50 "halt" => "",
51 "instant_update" => "softupdate",
52 "update" => "scheduledupdate",
53 "reinstall" => "install",
54 "rescan" => "",
55 "memcheck" => "memcheck",
56 "sysinfo" => "sysinfo");
59 var $fai_activated = FALSE;
61 function workgeneric ($config, $dn= NULL, $parent= NULL)
62 {
63 /* Check if FAI is activated */
64 $tmp = search_config($config->data,"faiManagement","CLASS");
65 if(!empty($tmp)){
66 $this->fai_activated = TRUE;
67 }
69 plugin::plugin ($config, $dn, $parent);
70 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
71 $this->netConfigDNS->acl = $this->acl;
73 /* Read arrays */
74 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
75 if (!isset($this->attrs[$val])){
76 continue;
77 }
78 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
79 array_push($this->$val, $this->attrs[$val][$i]);
80 }
81 }
83 /* Create used ntp server array */
84 $this->gotoNtpServer= array();
85 if(isset($this->attrs['gotoNtpServer'])){
86 $this->inheritTimeServer = false;
87 unset($this->attrs['gotoNtpServer']['count']);
88 foreach($this->attrs['gotoNtpServer'] as $server){
89 $this->gotoNtpServer[$server] = $server;
90 }
91 }
93 /* Set inherit checkbox state */
94 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
95 $this->inheritTimeServer = true;
96 $this->gotoNtpServer=array();
97 }
99 /* Create available ntp options */
100 $tmp = $this->config->data['SERVERS']['NTP'];
101 $this->gotoNtpServers = array();
102 foreach($tmp as $key => $server){
103 if($server == "default") continue;
104 $this->gotoNtpServers[$server] = $server;
105 }
107 $this->modes["active"]= _("Activated");
108 $this->modes["locked"]= _("Locked");
110 /* Set base */
111 if ($this->dn == "new"){
112 $ui= get_userinfo();
113 $this->base= dn2base($ui->dn);
114 } else {
115 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
116 }
118 /* Create an array of all Syslog servers */
119 $tmp = $this->config->data['SERVERS']['SYSLOG'];
120 foreach($tmp as $server){
121 $visible = $server;
122 if($server == "default") {
123 $visible = "["._("inherited")."]";
124 }
125 $this->gotoSyslogServers[$server] = $visible;
126 }
128 /* Save 'dn' for later referal */
129 $this->orig_dn= $this->dn;
130 }
132 function execute()
133 {
134 /* Call parent execute */
135 plugin::execute();
136 $this->netConfigDNS->acl = $this->acl;
138 /* Do we need to flip is_account state? */
139 if (isset($_POST['modify_state'])){
140 $this->is_account= !$this->is_account;
141 }
143 if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
144 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
145 if ($cmd == ""){
146 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
147 } else {
148 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
149 if ($retval != 0){
150 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
151 } elseif ($_POST['saction'] != "wake") {
153 if($this->fai_activated && $this->dn != "new"){
154 /* Set FAIstate */
155 $ldap = $this->config->get_ldap_link();
156 $ldap->cd($this->config->current['BASE']);
157 $ldap->cat($this->dn,array("objectClass"));
158 $res = $ldap->fetch();
160 $attrs = array();
161 $attrs['FAIstate'] = $this->FAIstate;
162 if(isset($this->mapActions[$_POST['saction']]) && $this->mapActions[$_POST['saction']] != ""){
163 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
164 }
166 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
167 $attrs['objectClass'][] = $res['objectClass'][$i];
168 }
170 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
171 $attrs['objectClass'][] = "FAIobject";
172 }
174 if($attrs['FAIstate'] == ""){
175 #FIXME we should check if FAIobject is used anymore
176 $attrs['FAIstate'] = array();
177 }
179 $ldap->cd($this->dn);
180 $ldap->modify($attrs);
181 show_ldap_error($ldap->get_error());
183 $this->didAction= TRUE;
184 }
185 }
186 }
187 }
189 /* Do we represent a valid terminal? */
190 if (!$this->is_account && $this->parent == NULL){
191 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
192 _("This 'dn' has no workstation features.")."</b>";
193 return($display);
194 }
196 /* Base select dialog */
197 $once = true;
198 foreach($_POST as $name => $value){
199 if(preg_match("/^chooseBase/",$name) && $once){
200 $once = false;
201 $this->dialog = new baseSelectDialog($this->config);
202 $this->dialog->setCurrentBase($this->base);
203 }
204 }
206 /* Dialog handling */
207 if(is_object($this->dialog)){
208 /* Must be called before save_object */
209 $this->dialog->save_object();
211 if($this->dialog->isClosed()){
212 $this->dialog = false;
213 }elseif($this->dialog->isSelected()){
214 $this->base = $this->dialog->isSelected();
215 $this->dialog= false;
216 }else{
217 return($this->dialog->execute());
218 }
219 }
221 /* Add new ntp Server to our list */
222 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer") == ""){
223 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
224 }
226 /* Delete selected NtpServer for list of used servers */
227 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer") == ""){
228 foreach($_POST['gotoNtpServerSelected'] as $name){
229 unset($this->gotoNtpServer[$name]);
230 }
231 }
233 /* Fill templating stuff */
234 $smarty= get_smarty();
235 $smarty->assign("cn", $this->cn);
236 $smarty->assign("l", $this->l);
237 $smarty->assign("bases", $this->config->idepartments);
238 $smarty->assign("staticAddress", "");
239 $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
241 /* Check if workstation is online */
242 $query= "fping -q -r 1 -t 500 ".$this->cn;
243 exec ($query, $dummy, $retval);
245 foreach($this->attributes as $attrs ){
246 $smarty->assign($attrs."ACL",chkacl($this->acl,$attrs));
247 }
249 /* Offline */
250 if ($retval == 0){
251 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
252 "instant_update" => _("Instant update"),
253 "update" => _("Scheduled update"),
254 "reinstall" => _("Reinstall"),
255 "rescan" => _("Rescan hardware"),
256 "memcheck" => _("Memory test"),
257 "localboot" => _("Force localboot"),
258 "sysinfo" => _("System analysis")));
259 } else {
260 $smarty->assign("actions", array("wake" => _("Wake up"),
261 "reinstall" => _("Reinstall"),
262 "update" => _("Scheduled update"),
263 "memcheck" => _("Memory test"),
264 "localboot" => _("Force localboot"),
265 "sysinfo" => _("System analysis")));
266 }
267 /* Arrays */
268 $smarty->assign("modes", $this->modes);
269 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
270 $smarty->assign("syslogservers", $this->gotoSyslogServers);
271 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
273 /* Variables */
274 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
275 $smarty->assign($val."_select", $this->$val);
276 $smarty->assign($val."ACL", chkacl($this->acl, $val));
277 }
278 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
280 /* tell smarty the inherit checkbox state */
281 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
283 /* Show main page */
284 $this->netConfigDNS->cn= $this->cn;
285 $smarty->assign("netconfig", $this->netConfigDNS->execute());
286 $smarty->assign("fai_activated",$this->fai_activated);
287 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
288 }
290 function remove_from_parent()
291 {
292 $this->netConfigDNS->acl = $this->acl;
293 $this->netConfigDNS->remove_from_parent();
294 $ldap= $this->config->get_ldap_link();
295 $ldap->rmdir($this->dn);
296 show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
298 /* Optionally execute a command after we're done */
299 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
301 /* Delete references to object groups */
302 $ldap->cd ($this->config->current['BASE']);
303 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
304 while ($ldap->fetch()){
305 $og= new ogroup($this->config, $ldap->getDN());
306 unset($og->member[$this->dn]);
307 $og->save ();
308 }
309 }
312 /* Save data to object */
313 function save_object()
314 {
315 plugin::save_object();
317 /* Save base, since this is no LDAP attribute */
318 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
319 $this->base= $_POST['base'];
320 }
321 $this->netConfigDNS->save_object();
323 /* Set inherit mode */
324 if(isset($_POST['workgeneric_posted']) && chkacl($this->acl,"gotoNtpServer") == ""){
325 if(isset($_POST["inheritTimeServer"])){
326 $this->inheritTimeServer = true;
327 }else{
328 $this->inheritTimeServer = false;
329 }
330 }
331 }
334 /* Check supplied data */
335 function check()
336 {
337 /* Call common method to give check the hook */
338 $message= plugin::check();
340 if($this->cn != "wdefault"){
341 $message= array_merge($message, $this->netConfigDNS->check());
342 }
344 $ui= get_userinfo();
345 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
346 $acl= get_permissions ($this->dn, $ui->subtreeACL);
347 $acl= get_module_permission($acl, "group", $this->dn);
348 if (chkacl($acl, "create") != ""){
349 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
350 }
352 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
353 $message[]= _("The required field 'Workstation name' is not set.");
354 }
356 if ($this->orig_dn != $this->dn){
357 $ldap= $this->config->get_ldap_link();
358 $ldap->cd ($this->base);
360 if($this->cn == "wdefault"){
361 $ldap->cat($this->dn);
362 }else{
363 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
364 }
365 if ($ldap->count() != 0){
366 while ($attrs= $ldap->fetch()){
367 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
368 continue;
369 } else {
370 if ($attrs['dn'] != $this->orig_dn){
371 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
372 break;
373 }
374 }
375 }
376 }
377 }
380 /* Check for valid ntpServer selection */
381 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
382 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
383 }
385 return ($message);
386 }
389 /* Save to LDAP */
390 function save()
391 {
392 $this->netConfigDNS->acl = $this->acl;
393 plugin::save();
395 /* Strip out 'default' values */
396 foreach (array("gotoSyslogServer") as $val){
397 if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
398 $this->attrs[$val]= array();
399 }
400 }
402 /* Remove FAIstate */
403 if (isset($this->attrs['FAIstate'])){
404 unset ($this->attrs['FAIstate']);
405 }
407 /* Add missing arrays */
408 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
409 if (isset ($this->$val) && count ($this->$val) != 0){
410 $this->attrs["$val"]= $this->$val;
411 }
412 }
414 /* Remove all empty values */
415 if ($this->orig_dn == 'new'){
416 $attrs= array();
417 foreach ($this->attrs as $key => $val){
418 if (is_array($val) && count($val) == 0){
419 continue;
420 }
421 $attrs[$key]= $val;
422 }
423 $this->attrs= $attrs;
424 }
426 /* Update ntp server settings */
427 if($this->inheritTimeServer){
428 if($this->new){
429 if(isset($this->attrs['gotoNtpServer'])){
430 unset($this->attrs['gotoNtpServer']);
431 }
432 }else{
433 $this->attrs['gotoNtpServer'] = array();
434 }
435 }else{
436 /* Set ntpServers */
437 $this->attrs['gotoNtpServer'] = array();
438 foreach($this->gotoNtpServer as $server){
439 $this->attrs['gotoNtpServer'][] = $server;
440 }
441 }
443 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
444 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
445 }
447 /* Write back to ldap */
448 $ldap= $this->config->get_ldap_link();
449 if ($this->orig_dn == 'new'){
450 $ldap->cd($this->config->current['BASE']);
451 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
452 $ldap->cd($this->dn);
453 $ldap->add($this->attrs);
454 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
455 if(!$this->didAction){
456 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
457 }
458 } else {
459 if ($this->orig_dn != $this->dn){
460 $this->move($this->orig_dn, $this->dn);
461 }
462 $ldap->cd($this->dn);
463 $this->cleanup();
464 $ldap->modify ($this->attrs);
466 if(!$this->didAction){
467 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
468 }
469 }
471 /* Set macAddress to '-' this indicates that this is a Workstation Template */
472 if($this->cn == "wdefault"){
473 $this->netConfigDNS->macAddress = "-";
474 }
476 $this->netConfigDNS->cn = $this->cn;
477 $this->netConfigDNS->save($this->dn);
478 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
479 }
481 }
483 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
484 ?>