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