1 <?php
3 class termgroup extends plugin
4 {
5 /* attribute list for save action */
6 var $attributes= array("gotoMode","gotoSyslogServer", "gotoNtpServer");
7 var $objectclasses= array("gotoWorkstationTemplate");
9 var $members= array();
11 var $gotoMode = "locked";
12 var $gotoSyslogServer = "";
13 var $gotoNtpServer = array();
14 var $gotoNtpServers = array();
15 var $modes = array();
16 var $inheritTimeServer = true;
17 var $is_account = true;
18 var $orig_dn = "";
19 var $didAction = FALSE;
20 var $mapActions = array("reboot" => "localboot",
21 "instant_update" => "softupdate",
22 "update" => "sceduledupdate",
23 "reinstall" => "install",
24 "rescan" => "",
25 "memcheck" => "memcheck",
26 "sysinfo" => "sysinfo");
28 var $CopyPasteVars = array("gotoNtpServers","modes","inheritTimeServer");
30 function termgroup ($config, $dn= NULL, $parent= NULL)
31 {
32 /***************
33 Some initialisations
34 ***************/
36 plugin::plugin($config, $dn, $parent);
38 $ldap= $config->get_ldap_link();
40 $this->is_account = true;
41 $this->modes["active"]= _("Activated");
42 $this->modes["locked"]= _("Locked");
43 // $this->modes["memcheck"]= _("Memory test");
44 // $this->modes["sysinfo"]= _("System analysis");
46 $this->orig_dn = $this->dn;
48 /***************
49 Get mac addresses from member objects
50 ***************/
52 /* We're only interested in the terminal members here, evaluate
53 these... */
54 if(isset($this->attrs['member'])){
55 for ($i= 0; $i<$this->attrs['member']['count']; $i++){
56 $member= $this->attrs['member'][$i];
57 $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
58 if (preg_match("/success/i", $ldap->error)){
59 $attrs = $ldap->fetch();
60 if (in_array("gotoTerminal", $attrs['objectClass']) ||
61 in_array("gotoWorkstation", $attrs['objectClass'])){
62 if (isset($attrs['macAddress'])){
63 $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
64 } else {
65 $this->members[$attrs['cn'][0]]= "";
66 }
67 }
68 }
69 }
70 }
72 /***************
73 Perpare NTP settings
74 ***************/
76 /* Create used ntp server array */
77 $this->gotoNtpServer= array();
78 if(isset($this->attrs['gotoNtpServer'])){
79 $this->inheritTimeServer = false;
80 unset($this->attrs['gotoNtpServer']['count']);
81 foreach($this->attrs['gotoNtpServer'] as $server){
82 $this->gotoNtpServer[$server] = $server;
83 }
84 }
86 /* Set inherit checkbox state */
87 if(in_array("default",$this->gotoNtpServer)){
88 $this->inheritTimeServer = true;
89 $this->gotoNtpServer=array();
90 }
92 /* Create available ntp options */
93 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
94 foreach($this->gotoNtpServers as $key => $server){
95 if($server == "default"){
96 unset($this->gotoNtpServers[$key]);
97 }
98 }
101 }
103 function check()
104 {
105 /* Call common method to give check the hook */
106 $message= plugin::check();
108 if ($this->acl_is_createable() && $this->dn == "new"){
109 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
110 }
112 /* Check for valid ntpServer selection */
113 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
114 $message[]= _("There must be at least one NTP server selected.");
115 }
116 return($message);
117 }
119 function remove_from_parent()
120 {
121 /* Workstation startup is using gotoWorkstationTemplate too,
122 if we remove this oc all other not manged attributes will cause errors */
123 if(isset($this->attrs['gotoKernelParameters'])){
124 $this->objectclasses = array();
125 }
127 /* Remove acc */
128 plugin::remove_from_parent();
129 $ldap = $this->config->get_ldap_link();
130 $ldap->cd($this->orig_dn);
131 $ldap->modify($this->attrs);
132 $this->handle_post_events("remove");
133 }
136 function update_term_member_FAIstate($act)
137 {
138 /* Get required informations */
139 $og = $this->parent->by_object['ogroup'];
140 $allobs = $og->allobjects;
142 /* Get correct value for FAIstate */
143 $action = $this->mapActions[$act];
145 /* Get ldap connection */
146 $ldap = $this->config->get_ldap_link();
147 $ldap->cd ($this->config->current['BASE']);
149 /* Foreach member of mthis ogroup ... */
150 foreach($og->mem as $key ){
152 /* check objectClasses and create attributes array */
153 $attrs = array("FAIstate"=>$action);
154 for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
155 $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
156 }
157 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
158 $attrs['objectClass'][] = "FAIobject";
159 }
160 if($attrs['FAIstate'] == ""){
161 $attrs['FAIstate'] = array();
162 }
164 /* If this objects is workstation,terminal or server upodate FAIstate */
165 if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
166 $ldap->cd ($key);
167 $ldap->modify($attrs);
168 show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
170 if(!preg_match("/success/i",$ldap->get_error())) {
171 gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
172 }else{
173 gosa_log("OK. Updating FAIstate to '".$action."' : ".$key);
174 }
175 }
176 }
177 }
179 function execute()
180 {
181 /* Call parent execute */
182 plugin::execute();
185 /***************
186 Handle requested action
187 ***************/
189 /* Watch for events */
190 if (isset($_POST['action'])){
191 $macaddresses="";
192 $names="";
193 foreach ($this->members as $cn => $macAddress){
194 $macaddresses.= "$macAddress ";
195 $names.= "$cn ";
196 }
198 if (isset($_POST['action'])){
200 /* Update members fai state */
201 $this->update_term_member_FAIstate(trim($_POST['saction']));
203 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
205 if ($cmd == ""){
206 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
207 } else {
208 exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
209 if ($retval != 0){
210 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
211 } else {
212 $this->didAction= TRUE;
214 /* Get dns from member objects. Create ldap object */
215 $member = $this->parent->by_object['ogroup']->member;
216 $ldap = $this->config->get_ldap_link();
217 $ldap->cd($this->config->current['BASE']);
219 /* walk trough members and add FAIstate */
220 foreach($member as $dn => $object){
222 /* Get object */
223 $ldap->cat($dn,array("objectClass"));
224 $res = $ldap->fetch();
225 $attrs = array();
227 /* Add FAI state */
228 $attrs['FAIstate'] = "";
229 if(isset($this->mapActions[$_POST['saction']])){
230 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
231 }
233 /* Fix objectClass index */
234 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
235 $attrs['objectClass'][] = $res['objectClass'][$i];
236 }
238 /* Check if we must add the objectClass */
239 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
240 $attrs['objectClass'][] = "FAIobject";
241 }
243 if($attrs['FAIstate'] == ""){
244 $attrs['FAIstate'] = array();
245 }
247 $ldap->cd($dn);
248 $ldap->modify($attrs);
249 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
250 }
251 $this->didAction= TRUE;
252 }
253 }
254 }
255 }
258 /***************
259 Add remove NTP server
260 ***************/
262 /* Add new ntp Server to our list */
263 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
264 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
265 }
267 /* Delete selected NtpServer for list of used servers */
268 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
269 foreach($_POST['gotoNtpServerSelected'] as $name){
270 unset($this->gotoNtpServer[$name]);
271 }
272 }
275 /***************
276 Prepare smarty
277 ***************/
279 /* Set government mode */
280 $smarty= get_smarty();
282 $tmp = $this->plInfo();
283 foreach($tmp['plProvidedAcls'] as $name => $translated) {
284 $smarty->assign($name."ACL",$this->getacl($name));
285 }
287 foreach($this->attributes as $attr){
288 $smarty->assign($attr, $this->$attr);
289 }
291 /* Variables */
292 foreach(array("gotoMode","gotoNtpServer") as $val){
293 $smarty->assign($val."_select", $this->$val);
294 }
296 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
297 "instant_update" => _("Instant update"),
298 "update" => _("Scheduled update"),
299 "reinstall" => _("Reinstall"),
300 "rescan" => _("Rescan hardware"),
301 "memcheck" => _("Memory test"),
302 "sysinfo" => _("System analysis")));
304 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
305 $smarty->assign("modes", $this->modes);
307 $tmp = array();
308 foreach($this->gotoNtpServers as $server){
309 if(!in_array($server,$this->gotoNtpServer)){
310 $tmp[$server] = $server;
311 }
312 }
314 $smarty->assign("gotoNtpServers",$tmp);
315 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
316 $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer);
318 /* Show main page */
319 return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
320 }
322 function save_object()
323 {
324 plugin::save_object();
325 /* Set inherit mode */
326 if(isset($_POST['workgeneric_posted'])){
327 if(isset($_POST["inheritTimeServer"])){
328 $this->inheritTimeServer = true;
329 }else{
330 $this->inheritTimeServer = false;
331 }
332 }
333 }
335 /* Save to LDAP */
336 function save()
337 {
338 plugin::save();
340 /***************
341 Prepare special vars
342 ***************/
344 /* Unset some special vars ... */
345 foreach (array("gotoSyslogServer") as $val){
346 if ($this->attrs[$val] == "default"){
347 $this->attrs[$val]= array();
348 }
349 }
351 /* Update ntp server settings */
352 if($this->inheritTimeServer){
353 $this->attrs['gotoNtpServer'] = "default";
354 }else{
355 /* Set ntpServers */
356 $this->attrs['gotoNtpServer'] = array();
357 foreach($this->gotoNtpServer as $server){
358 $this->attrs['gotoNtpServer'][] = $server;
359 }
360 }
363 /***************
364 Write to ldap
365 ***************/
367 /* Write back to ldap */
368 $ldap= $this->config->get_ldap_link();
369 $ldap->cd($this->dn);
370 $this->cleanup();
371 $ldap->modify ($this->attrs);
373 if(!$this->didAction){
374 $this->handle_post_events("modify");
375 }
376 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
378 }
380 function plInfo()
381 {
382 return (array(
383 "plShortName" => _("System"),
384 "plDescription" => _("System group"),
385 "plSelfModify" => FALSE,
386 "plDepends" => array(),
387 "plPriority" => 0,
388 "plSection" => array("administration"),
389 "plCategory" => array("ogroups"),
390 "plProvidedAcls"=> array(
391 "gotoMode" => _("Mode"),
392 "gotoSyslogServer" => _("Syslog server"),
393 "FAIstate" => _("Action flag"),
394 "gotoNtpServer" => _("Ntp server"))
395 ));
396 }
400 }
402 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
403 ?>