4c25e5fb83cdf8aa8b02f8615208576279bbf30e
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)
64 {
65 plugin::plugin ($config, $dn);
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 }
127 function execute()
128 {
129 /* Call parent execute */
130 plugin::execute();
132 /* Do we need to flip is_account state? */
133 if (isset($_POST['modify_state'])){
134 $this->is_account= !$this->is_account;
135 }
137 if (isset($_POST['action'])){
138 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
139 if ($cmd == ""){
140 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
141 } else {
142 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
143 if ($retval != 0){
144 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
145 } else {
147 /* Set FAIstate */
148 $ldap = $this->config->get_ldap_link();
149 $ldap->cd($this->config->current['BASE']);
150 $ldap->cat($this->dn,array("objectClass"));
151 $res = $ldap->fetch();
153 $attrs = array();
154 $attrs['FAIstate'] = "";
155 if(isset($this->mapActions[$_POST['saction']])){
156 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
157 }
159 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
160 $attrs['objectClass'][] = $res['objectClass'][$i];
161 }
163 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
164 $attrs['objectClass'][] = "FAIobject";
165 }
167 if($attrs['FAIstate'] == ""){
168 #FIXME we should check if FAIobject is used anymore
169 $attrs['FAIstate'] = array();
170 }
172 $ldap->cd($this->dn);
173 $ldap->modify($attrs);
174 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
176 $this->didAction= TRUE;
177 }
178 }
179 }
181 /* Do we represent a valid terminal? */
182 if (!$this->is_account && $this->parent == NULL){
183 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
184 _("This 'dn' has no workstation features.")."</b>";
185 return($display);
186 }
188 /* Base select dialog */
189 $once = true;
190 foreach($_POST as $name => $value){
191 if(preg_match("/^chooseBase/",$name) && $once){
192 $once = false;
193 $this->dialog = new baseSelectDialog($this->config,$this);
194 $this->dialog->setCurrentBase($this->base);
195 }
196 }
198 /* Dialog handling */
199 if(is_object($this->dialog)){
200 /* Must be called before save_object */
201 $this->dialog->save_object();
203 if($this->dialog->isClosed()){
204 $this->dialog = false;
205 }elseif($this->dialog->isSelected()){
206 $this->base = $this->dialog->isSelected();
207 $this->dialog= false;
208 }else{
209 return($this->dialog->execute());
210 }
211 }
213 /* Add new ntp Server to our list */
214 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
215 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
216 }
218 /* Delete selected NtpServer for list of used servers */
219 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
220 foreach($_POST['gotoNtpServerSelected'] as $name){
221 unset($this->gotoNtpServer[$name]);
222 }
223 }
225 /* Fill templating stuff */
226 $smarty= get_smarty();
228 /* Create base acls */
229 $baseACL = $this->getacl("base");
230 if(!$this->acl_is_moveable()) {
231 $baseACL = preg_replace("/w/","",$baseACL);
232 }
233 $smarty->assign("baseACL", $baseACL);
235 /* Set acls */
236 $tmp = $this->plInfo();
237 foreach($tmp['plProvidedAcls'] as $name => $translation){
238 $smarty->assign($name."ACL",$this->getacl($name));
239 }
241 $smarty->assign("cn", $this->cn);
242 $smarty->assign("l", $this->l);
243 $smarty->assign("bases", $this->config->idepartments);
244 $smarty->assign("staticAddress", "");
246 $tmp = array();
247 foreach($this->gotoNtpServers as $server){
248 if(!in_array($server,$this->gotoNtpServer)){
249 $tmp[$server] = $server;
250 }
251 }
252 $smarty->assign("gotoNtpServers",$tmp);
254 /* Check if workstation is online */
255 $query= "fping -q -r 1 -t 500 ".$this->cn;
256 exec ($query, $dummy, $retval);
258 /* Offline */
259 if ($retval == 0){
260 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
261 "instant_update" => _("Instant update"),
262 "update" => _("Scheduled update"),
263 "reinstall" => _("Reinstall"),
264 "rescan" => _("Rescan hardware"),
265 "memcheck" => _("Memory test"),
266 "sysinfo" => _("System analysis")));
267 } else {
268 $smarty->assign("actions", array("wake" => _("Wake up"),
269 "reinstall" => _("Reinstall"),
270 "update" => _("Scheduled update"),
271 "memcheck" => _("Memory test"),
272 "sysinfo" => _("System analysis")));
273 }
274 /* Arrays */
275 $smarty->assign("modes", $this->modes);
276 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
277 $smarty->assign("syslogservers", $this->gotoSyslogServers);
279 $ntpser = array();
280 foreach($this->gotoNtpServers as $server){
281 if(!in_array($server,$this->gotoNtpServer)){
282 $ntpser[$server] = $server;
283 }
284 }
285 $smarty->assign("gotoNtpServers", $ntpser);
287 /* Variables */
288 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
289 $smarty->assign($val."_select", $this->$val);
290 }
292 /* tell smarty the inherit checkbox state */
293 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
295 /* Show main page */
296 $smarty->assign("netconfig", $this->netConfigDNS->execute());
297 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
298 }
300 function remove_from_parent()
301 {
302 $this->netConfigDNS->remove_from_parent();
303 $ldap= $this->config->get_ldap_link();
304 $ldap->rmdir($this->dn);
305 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
307 /* Optionally execute a command after we're done */
308 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
310 /* Delete references to object groups */
311 $ldap->cd ($this->config->current['BASE']);
312 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
313 while ($ldap->fetch()){
314 $og= new ogroup($this->config, $ldap->getDN());
315 unset($og->member[$this->dn]);
316 $og->save ();
317 }
318 }
321 /* Save data to object */
322 function save_object()
323 {
324 plugin::save_object();
326 /* Save base, since this is no LDAP attribute */
327 if((isset($_POST['base'])) && ($this->acl_is_moveable())){
328 $this->set_acl_base('dummy,'.$_POST['base']);
329 if($this->acl_is_moveable()){
331 if(isset($this->config->idepartments[$_POST['base']])){
332 $this->base = $_POST['base'];
333 if ($_POST['base'] != $this->base){
334 $this->is_modified= TRUE;
335 }
336 }
337 }else{
339 print_red(sprintf(_("You are not allowed to move this object to '%s'."),LDAP::fix($_POST['base'])));
340 $this->set_acl_base('dummy,'.$this->base);
341 }
342 }
343 $this->netConfigDNS->save_object();
345 /* Set inherit mode */
346 if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
347 if(isset($_POST["inheritTimeServer"])){
348 $this->inheritTimeServer = true;
349 }else{
350 $this->inheritTimeServer = false;
351 }
352 }
354 }
357 /* Check supplied data */
358 function check()
359 {
360 /* Call common method to give check the hook */
361 $message= plugin::check();
362 $message= array_merge($message, $this->netConfigDNS->check());
364 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
365 if ($this->orig_dn == "new" && !$this->acl_is_createable()){
366 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
367 }
370 if ($this->cn == ""){
371 $message[]= _("The required field 'Workstation name' is not set.");
372 }
374 if ($this->orig_dn != $this->dn){
375 $ldap= $this->config->get_ldap_link();
376 $ldap->cd ($this->base);
377 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
378 if ($ldap->count() != 0){
379 while ($attrs= $ldap->fetch()){
380 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
381 continue;
382 } else {
383 if ($attrs['dn'] != $this->orig_dn){
384 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
385 break;
386 }
387 }
388 }
389 }
390 }
392 /* Check for valid ntpServer selection */
393 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
394 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
395 }
397 return ($message);
398 }
401 /* Save to LDAP */
402 function save()
403 {
404 plugin::save();
406 /* Strip out 'default' values */
407 foreach (array("gotoSyslogServer") as $val){
409 if ($this->attrs[$val] == "default"){
410 $this->attrs[$val]= array();
411 }
412 }
414 /* Add missing arrays */
415 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
416 if (isset ($this->$val) && count ($this->$val) != 0){
417 $this->attrs["$val"]= $this->$val;
418 }
419 }
421 /* Remove all empty values */
422 if ($this->orig_dn == 'new'){
423 $attrs= array();
424 foreach ($this->attrs as $key => $val){
425 if (is_array($val) && count($val) == 0){
426 continue;
427 }
428 $attrs[$key]= $val;
429 }
430 $this->attrs= $attrs;
431 }
433 /* Update ntp server settings */
434 if($this->inheritTimeServer){
435 if($this->is_new){
436 if(isset($this->attrs['gotoNtpServer'])){
437 unset($this->attrs['gotoNtpServer']);
438 }
439 }else{
440 $this->attrs['gotoNtpServer'] = array();
441 }
442 }else{
443 /* Set ntpServers */
444 $this->attrs['gotoNtpServer'] = array();
445 foreach($this->gotoNtpServer as $server){
446 $this->attrs['gotoNtpServer'][] = $server;
447 }
448 }
450 if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
451 $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
452 }
454 /* Write back to ldap */
455 $ldap= $this->config->get_ldap_link();
456 if ($this->orig_dn == 'new'){
457 $ldap->cd($this->config->current['BASE']);
458 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
459 $ldap->cd($this->dn);
460 $ldap->add($this->attrs);
461 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
462 if(!$this->didAction){
463 $this->handle_post_events("add");
464 }
465 } else {
466 if ($this->orig_dn != $this->dn){
467 $this->move($this->orig_dn, $this->dn);
468 }
469 $ldap->cd($this->dn);
470 $this->cleanup();
471 $ldap->modify ($this->attrs);
473 if(!$this->didAction){
474 $this->handle_post_events("modify");
475 }
476 }
477 $this->netConfigDNS->cn = $this->cn;
478 $this->netConfigDNS->save($this->dn);
479 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
480 }
483 /* Return plugin informations for acl handling
484 #FIXME FAIscript seams to ununsed within this class... */
485 function plInfo()
486 {
487 return (array(
488 "plShortName" => _("Generic"),
489 "plDescription" => _("Workstation generic"),
490 "plSelfModify" => FALSE,
491 "plDepends" => array(),
492 "plPriority" => 0,
493 "plSection" => array("administration"),
494 "plCategory" => array("workstation" => array("description" => _("Workstation"),
495 "objectClass" => "gotoWorkstation")),
496 "plProvidedAcls"=> array(
497 "cn" => _("Workstation name"),
498 "l" => _("Location") ,
499 "base" => _("Base") ,
500 "gotoMode" => _("Goto mode"),
501 "gotoSyslogServer" => _("Syslog server"),
502 "gotoNtpServer" => _("Ntp server"),
503 "FAIstate" => _("Action flag"))
504 ));
505 }
507 }
509 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
510 ?>