1 <?php
3 class termgroup extends plugin
4 {
5 /* attribute list for save action */
6 var $members= array();
8 var $gotoMode = "locked";
9 var $gotoSyslogServer = "";
10 var $gotoNtpServer = array();
11 var $gotoNtpServers = array();
12 var $modes = array();
13 var $inheritTimeServer = true;
14 var $is_account = true;
15 var $orig_dn = "";
16 var $didAction = FALSE;
17 var $mapActions = array("reboot" => "localboot",
18 "instant_update" => "softupdate",
19 "update" => "sceduledupdate",
20 "reinstall" => "install",
21 "rescan" => "",
22 "memcheck" => "memcheck",
23 "sysinfo" => "sysinfo");
25 var $attributes = array("gotoMode","gotoSyslogServer", "gotoNtpServer");
26 var $objectclasses = array("gotoWorkstationTemplate");
27 var $CopyPasteVars = array("gotoNtpServers","modes","inheritTimeServer","members");
28 var $view_logged = FALSE;
31 function termgroup (&$config, $dn= NULL, $parent= NULL)
32 {
33 /***************
34 Some initialisations
35 ***************/
37 plugin::plugin($config, $dn, $parent);
39 $ldap= $config->get_ldap_link();
41 $this->is_account = true;
42 $this->modes["active"]= _("Activated");
43 $this->modes["locked"]= _("Locked");
44 // $this->modes["memcheck"]= _("Memory test");
45 // $this->modes["sysinfo"]= _("System analysis");
47 $this->orig_dn = $this->dn;
49 /***************
50 Get mac addresses from member objects
51 ***************/
53 /* We're only interested in the terminal members here, evaluate
54 these... */
55 if(isset($this->attrs['member'])){
56 for ($i= 0; $i<$this->attrs['member']['count']; $i++){
57 $member= $this->attrs['member'][$i];
58 $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
59 if (preg_match("/success/i", $ldap->error)){
60 $attrs = $ldap->fetch();
61 if (in_array("gotoTerminal", $attrs['objectClass']) ||
62 in_array("gotoWorkstation", $attrs['objectClass'])){
63 if (isset($attrs['macAddress'])){
64 $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
65 } else {
66 $this->members[$attrs['cn'][0]]= "";
67 }
68 }
69 }
70 }
71 }
73 /***************
74 Perpare NTP settings
75 ***************/
77 /* Create used ntp server array */
78 $this->gotoNtpServer= array();
79 if(isset($this->attrs['gotoNtpServer'])){
80 $this->inheritTimeServer = false;
81 unset($this->attrs['gotoNtpServer']['count']);
82 foreach($this->attrs['gotoNtpServer'] as $server){
83 $this->gotoNtpServer[$server] = $server;
84 }
85 }
87 /* Set inherit checkbox state */
88 if(in_array("default",$this->gotoNtpServer)){
89 $this->inheritTimeServer = true;
90 $this->gotoNtpServer=array();
91 }
93 /* Create available ntp options */
94 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
95 foreach($this->gotoNtpServers as $key => $server){
96 if($server == "default"){
97 unset($this->gotoNtpServers[$key]);
98 }
99 }
100 }
102 function check()
103 {
104 /* Call common method to give check the hook */
105 $message= plugin::check();
107 if (!$this->acl_is_createable() && $this->dn == "new"){
108 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
109 }
111 /* Check for valid ntpServer selection */
112 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
113 $message[]= _("There must be at least one NTP server selected.");
114 }
115 return($message);
116 }
118 function remove_from_parent()
119 {
120 /* Workstation startup is using gotoWorkstationTemplate too,
121 if we remove this oc all other not manged attributes will cause errors */
122 if(isset($this->attrs['gotoKernelParameters'])){
123 $this->objectclasses = array();
124 }
126 /* Remove acc */
127 plugin::remove_from_parent();
128 $ldap = $this->config->get_ldap_link();
129 $ldap->cd($this->orig_dn);
130 $ldap->modify($this->attrs);
131 $this->handle_post_events("remove");
132 new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
133 }
136 function update_term_member_FAIstate($act)
137 {
138 /* Get required informations */
139 $og = $this->parent->by_object['ogroup'];
140 $allobs = $og->objcache;
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->member 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));
169 }
170 }
171 }
173 function execute()
174 {
175 /* Call parent execute */
176 plugin::execute();
178 if(!$this->view_logged){
179 $this->view_logged = TRUE;
180 new log("view","ogroups/".get_class($this),$this->dn);
181 }
183 /***************
184 Handle requested action
185 ***************/
187 /* Watch for events */
188 if (isset($_POST['action'])){
189 $macaddresses="";
190 $names="";
191 foreach ($this->members as $cn => $macAddress){
192 $macaddresses.= "$macAddress ";
193 $names.= "$cn ";
194 }
196 if (isset($_POST['action'])){
198 /* Update members fai state */
199 $this->update_term_member_FAIstate(trim($_POST['saction']));
201 $cmd = $this->config->search("workgeneric", "ACTIONCMD",array('tabs'));
203 if ($cmd == ""){
204 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
205 } else {
206 exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
207 if ($retval != 0){
208 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
209 } else {
210 $this->didAction= TRUE;
212 /* Get dns from member objects. Create ldap object */
213 $member = $this->parent->by_object['ogroup']->member;
214 $ldap = $this->config->get_ldap_link();
215 $ldap->cd($this->config->current['BASE']);
217 /* walk trough members and add FAIstate */
218 foreach($member as $dn => $object){
220 /* Get object */
221 $ldap->cat($dn,array("objectClass"));
222 $res = $ldap->fetch();
223 $attrs = array();
225 /* Add FAI state */
226 $attrs['FAIstate'] = "";
227 if(isset($this->mapActions[$_POST['saction']])){
228 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
229 }
231 /* Fix objectClass index */
232 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
233 $attrs['objectClass'][] = $res['objectClass'][$i];
234 }
236 /* Check if we must add the objectClass */
237 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
238 $attrs['objectClass'][] = "FAIobject";
239 }
241 if($attrs['FAIstate'] == ""){
242 $attrs['FAIstate'] = array();
243 }
245 $ldap->cd($dn);
246 $ldap->modify($attrs);
247 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
248 }
249 $this->didAction= TRUE;
250 }
251 }
252 }
253 }
256 /***************
257 Add remove NTP server
258 ***************/
260 /* Add new ntp Server to our list */
261 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
262 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
263 }
265 /* Delete selected NtpServer for list of used servers */
266 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
267 foreach($_POST['gotoNtpServerSelected'] as $name){
268 unset($this->gotoNtpServer[$name]);
269 }
270 }
273 /***************
274 Prepare smarty
275 ***************/
277 /* Set government mode */
278 $smarty= get_smarty();
280 $tmp = $this->plInfo();
281 foreach($tmp['plProvidedAcls'] as $name => $translated) {
282 $smarty->assign($name."ACL",$this->getacl($name));
283 }
285 foreach($this->attributes as $attr){
286 $smarty->assign($attr, $this->$attr);
287 }
289 /* Variables */
290 foreach(array("gotoMode","gotoNtpServer") as $val){
291 $smarty->assign($val."_select", $this->$val);
292 }
294 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
295 "instant_update" => _("Instant update"),
296 "update" => _("Scheduled update"),
297 "reinstall" => _("Reinstall"),
298 "rescan" => _("Rescan hardware"),
299 "memcheck" => _("Memory test"),
300 "sysinfo" => _("System analysis")));
302 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
303 $smarty->assign("modes", $this->modes);
305 $tmp = array();
306 foreach($this->gotoNtpServers as $server){
307 if(!in_array($server,$this->gotoNtpServer)){
308 $tmp[$server] = $server;
309 }
310 }
312 $smarty->assign("gotoNtpServers",$tmp);
313 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
314 $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer);
316 /* Show main page */
317 return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
318 }
320 function save_object()
321 {
322 plugin::save_object();
323 /* Set inherit mode */
324 if(isset($_POST['workgeneric_posted'])){
325 if(isset($_POST["inheritTimeServer"])){
326 $this->inheritTimeServer = true;
327 }else{
328 $this->inheritTimeServer = false;
329 }
330 }
331 }
333 /* Save to LDAP */
334 function save()
335 {
336 plugin::save();
338 /***************
339 Prepare special vars
340 ***************/
342 /* Unset some special vars ... */
343 foreach (array("gotoSyslogServer") as $val){
344 if ($this->attrs[$val] == "default"){
345 $this->attrs[$val]= array();
346 }
347 }
349 /* Update ntp server settings */
350 if($this->inheritTimeServer){
351 $this->attrs['gotoNtpServer'] = "default";
352 }else{
353 /* Set ntpServers */
354 $this->attrs['gotoNtpServer'] = array();
355 foreach($this->gotoNtpServer as $server){
356 $this->attrs['gotoNtpServer'][] = $server;
357 }
358 }
361 /***************
362 Write to ldap
363 ***************/
365 /* Write back to ldap */
366 $ldap= $this->config->get_ldap_link();
367 $ldap->cd($this->dn);
368 $this->cleanup();
369 $ldap->modify ($this->attrs);
371 if($this->initially_was_account){
372 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
373 }else{
374 new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
375 }
377 if(!$this->didAction){
378 $this->handle_post_events("modify");
379 }
380 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
382 }
384 function plInfo()
385 {
386 return (array(
387 "plShortName" => _("System"),
388 "plDescription" => _("System group"),
389 "plSelfModify" => FALSE,
390 "plDepends" => array(),
391 "plPriority" => 5,
392 "plSection" => array("administration"),
393 "plCategory" => array("ogroups"),
394 "plProvidedAcls"=> array(
395 "gotoMode" => _("Mode"),
396 "gotoSyslogServer" => _("Syslog server"),
397 "FAIstate" => _("Action flag"),
398 "gotoNtpServer" => _("Ntp server"))
399 ));
400 }
402 function PrepareForCopyPaste($source)
403 {
404 /* Create used ntp server array */
405 $this->gotoNtpServer= array();
407 if(isset($source['gotoNtpServer'])){
408 $this->inheritTimeServer = false;
409 unset($source['gotoNtpServer']['count']);
410 foreach($source['gotoNtpServer'] as $server){
411 $this->gotoNtpServer[$server] = $server;
412 }
413 }
415 /* Set inherit checkbox state */
416 if(in_array("default",$this->gotoNtpServer)){
417 $this->inheritTimeServer = true;
418 $this->gotoNtpServer=array();
419 }
421 /* Create available ntp options */
422 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
423 foreach($this->gotoNtpServers as $key => $server){
424 if($server == "default"){
425 unset($this->gotoNtpServers[$key]);
426 }
427 }
428 }
429 }
431 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
432 ?>