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 function workgeneric ($config, $dn= NULL)
54 {
55 plugin::plugin ($config, $dn);
56 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
58 /* Read arrays */
59 foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
60 if (!isset($this->attrs[$val])){
61 continue;
62 }
63 for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
64 array_push($this->$val, $this->attrs[$val][$i]);
65 }
66 }
68 /* Create used ntp server array */
69 $this->gotoNtpServer= array();
70 if(isset($this->attrs['gotoNtpServer'])){
71 $this->inheritTimeServer = false;
72 unset($this->attrs['gotoNtpServer']['count']);
73 foreach($this->attrs['gotoNtpServer'] as $server){
74 $this->gotoNtpServer[$server] = $server;
75 }
76 }
78 /* Set inherit checkbox state */
79 if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
80 $this->inheritTimeServer = true;
81 $this->gotoNtpServer=array();
82 }
84 /* Create available ntp options */
85 $tmp = $this->config->data['SERVERS']['NTP'];
86 $this->gotoNtpServers = array();
87 foreach($tmp as $key => $server){
88 if($server == "default") continue;
89 $this->gotoNtpServers[$server] = $server;
90 }
92 $this->modes["active"]= _("Activated");
93 $this->modes["locked"]= _("Locked");
94 $this->modes["memcheck"]= _("Memory test");
95 $this->modes["sysinfo"]= _("System analysis");
97 /* Set base */
98 if ($this->dn == "new"){
99 $ui= get_userinfo();
100 $this->base= dn2base($ui->dn);
101 } else {
102 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
103 }
105 /* Create an array of all Syslog servers */
106 $tmp = $this->config->data['SERVERS']['SYSLOG'];
107 foreach($tmp as $server){
108 $visible = $server;
109 if($server == "default") {
110 $visible = "["._("inherited")."]";
111 }
112 $this->gotoSyslogServers[$server] = $visible;
113 }
115 /* Save 'dn' for later referal */
116 $this->orig_dn= $this->dn;
117 }
119 function execute()
120 {
121 /* Call parent execute */
122 plugin::execute();
124 /* Do we need to flip is_account state? */
125 if (isset($_POST['modify_state'])){
126 $this->is_account= !$this->is_account;
127 }
129 if (isset($_POST['action'])){
130 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
131 if ($cmd == ""){
132 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
133 } else {
134 exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
135 if ($retval != 0){
136 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
137 } else {
138 $this->didAction= TRUE;
139 }
140 }
141 }
143 /* Do we represent a valid terminal? */
144 if (!$this->is_account && $this->parent == NULL){
145 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
146 _("This 'dn' has no workstation features.")."</b>";
147 return($display);
148 }
150 /* Base select dialog */
151 $once = true;
152 foreach($_POST as $name => $value){
153 if(preg_match("/^chooseBase/",$name) && $once){
154 $once = false;
155 $this->dialog = new baseSelectDialog($this->config);
156 $this->dialog->setCurrentBase($this->base);
157 }
158 }
160 /* Dialog handling */
161 if(is_object($this->dialog)){
162 /* Must be called before save_object */
163 $this->dialog->save_object();
165 if($this->dialog->isClosed()){
166 $this->dialog = false;
167 }elseif($this->dialog->isSelected()){
168 $this->base = $this->dialog->isSelected();
169 $this->dialog= false;
170 }else{
171 return($this->dialog->execute());
172 }
173 }
175 /* Add new ntp Server to our list */
176 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
177 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
178 }
180 /* Delete selected NtpServer for list of used servers */
181 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
182 foreach($_POST['gotoNtpServerSelected'] as $name){
183 unset($this->gotoNtpServer[$name]);
184 }
185 }
187 /* Fill templating stuff */
188 $smarty= get_smarty();
189 $smarty->assign("cn", $this->cn);
190 $smarty->assign("l", $this->l);
191 $smarty->assign("bases", $this->config->idepartments);
192 $smarty->assign("staticAddress", "");
193 $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
195 /* Check if workstation is online */
196 $query= "fping -q -r 1 -t 500 ".$this->cn;
197 exec ($query, $dummy, $retval);
199 /* Offline */
200 if ($retval == 0){
201 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
202 "instant_update" => _("Instant update"),
203 "update" => _("Scheduled update"),
204 "reinstall" => _("Reinstall"),
205 "rescan" => _("Rescan hardware")));
206 } else {
207 $smarty->assign("actions", array("wake" => _("Wake up"),
208 "reinstall" => _("Reinstall"),
209 "update" => _("Scheduled update")));
210 }
211 /* Arrays */
212 $smarty->assign("modes", $this->modes);
213 $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
214 $smarty->assign("syslogservers", $this->gotoSyslogServers);
215 $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
217 /* Variables */
218 foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
219 $smarty->assign($val."_select", $this->$val);
220 $smarty->assign($val."ACL", chkacl($this->acl, $val));
221 }
222 $smarty->assign("actionACL", chkacl($this->acl, 'action'));
224 /* tell smarty the inherit checkbox state */
225 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
227 /* Show main page */
228 $smarty->assign("netconfig", $this->netConfigDNS->execute());
229 return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
230 }
232 function remove_from_parent()
233 {
234 $this->netConfigDNS->remove_from_parent();
235 $ldap= $this->config->get_ldap_link();
236 $ldap->rmdir($this->dn);
237 show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
239 /* Optionally execute a command after we're done */
240 $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
242 /* Delete references to object groups */
243 $ldap->cd ($this->config->current['BASE']);
244 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
245 while ($ldap->fetch()){
246 $og= new ogroup($this->config, $ldap->getDN());
247 unset($og->member[$this->dn]);
248 $og->save ();
249 }
250 }
253 /* Save data to object */
254 function save_object()
255 {
256 plugin::save_object();
258 /* Save base, since this is no LDAP attribute */
259 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
260 $this->base= $_POST['base'];
261 }
262 $this->netConfigDNS->save_object();
264 /* Set inherit mode */
265 if(isset($_POST['workgeneric_posted'])){
266 if(isset($_POST["inheritTimeServer"])){
267 $this->inheritTimeServer = true;
268 }else{
269 $this->inheritTimeServer = false;
270 }
271 }
273 }
276 /* Check supplied data */
277 function check()
278 {
279 /* Call common method to give check the hook */
280 $message= plugin::check();
281 $message= array_merge($message, $this->netConfigDNS->check());
283 $ui= get_userinfo();
284 $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
285 $acl= get_permissions ($this->dn, $ui->subtreeACL);
286 $acl= get_module_permission($acl, "group", $this->dn);
287 if (chkacl($acl, "create") != ""){
288 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
289 }
291 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
292 $message[]= _("The required field 'Workstation name' is not set.");
293 }
295 if ($this->orig_dn != $this->dn){
296 $ldap= $this->config->get_ldap_link();
297 $ldap->cd ($this->base);
298 $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
299 if ($ldap->count() != 0){
300 while ($attrs= $ldap->fetch()){
301 if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
302 continue;
303 } else {
304 if ($attrs['dn'] != $this->orig_dn){
305 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
306 break;
307 }
308 }
309 }
310 }
311 }
313 /* Check for valid ntpServer selection */
314 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
315 $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
316 }
318 return ($message);
319 }
322 /* Save to LDAP */
323 function save()
324 {
325 plugin::save();
327 /* Strip out 'default' values */
328 foreach (array("gotoSyslogServer") as $val){
330 if ($this->attrs[$val] == "default"){
331 $this->attrs[$val]= array();
332 }
333 }
335 /* Add missing arrays */
336 foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
337 if (isset ($this->$val) && count ($this->$val) != 0){
338 $this->attrs["$val"]= $this->$val;
339 }
340 }
342 /* Remove all empty values */
343 if ($this->orig_dn == 'new'){
344 $attrs= array();
345 foreach ($this->attrs as $key => $val){
346 if (is_array($val) && count($val) == 0){
347 continue;
348 }
349 $attrs[$key]= $val;
350 }
351 $this->attrs= $attrs;
352 }
354 /* Update ntp server settings */
355 if($this->inheritTimeServer){
356 if($this->new){
357 if(isset($this->attrs['gotoNtpServer'])){
358 unset($this->attrs['gotoNtpServer']);
359 }
360 }else{
361 $this->attrs['gotoNtpServer'] = array();
362 }
363 }else{
364 /* Set ntpServers */
365 $this->attrs['gotoNtpServer'] = array();
366 foreach($this->gotoNtpServer as $server){
367 $this->attrs['gotoNtpServer'][] = $server;
368 }
369 }
371 /* Write back to ldap */
372 $ldap= $this->config->get_ldap_link();
373 if ($this->orig_dn == 'new'){
374 $ldap->cd($this->config->current['BASE']);
375 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
376 $ldap->cd($this->dn);
377 $ldap->add($this->attrs);
378 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
379 if(!$this->didAction){
380 $this->handle_post_events("add");
381 }
382 } else {
383 if ($this->orig_dn != $this->dn){
384 $this->move($this->orig_dn, $this->dn);
385 }
386 $ldap->cd($this->dn);
387 $this->cleanup();
388 // $this->attrs['gosaUnitTag']= array();
389 $ldap->modify ($this->attrs);
391 if(!$this->didAction){
392 $this->handle_post_events("modify");
393 }
394 }
395 $this->netConfigDNS->cn = $this->cn;
396 $this->netConfigDNS->save($this->dn);
397 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
398 }
400 }
402 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
403 ?>