ece1d495c816a8d60e109106b9ecb940645fb0ea
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");
29 function termgroup ($config, $dn= NULL, $parent= NULL)
30 {
31 /***************
32 Some initialisations
33 ***************/
35 plugin::plugin($config, $dn, $parent);
37 $ldap= $config->get_ldap_link();
39 $this->is_account = true;
40 $this->modes["active"]= _("Activated");
41 $this->modes["locked"]= _("Locked");
42 // $this->modes["memcheck"]= _("Memory test");
43 // $this->modes["sysinfo"]= _("System analysis");
45 $this->orig_dn = $this->dn;
47 /***************
48 Get mac addresses from member objects
49 ***************/
51 /* We're only interested in the terminal members here, evaluate
52 these... */
53 if(isset($this->attrs['member'])){
54 for ($i= 0; $i<$this->attrs['member']['count']; $i++){
55 $member= $this->attrs['member'][$i];
56 $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
57 if (preg_match("/success/i", $ldap->error)){
58 $attrs = $ldap->fetch();
59 if (in_array("gotoTerminal", $attrs['objectClass']) ||
60 in_array("gotoWorkstation", $attrs['objectClass'])){
61 if (isset($attrs['macAddress'])){
62 $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
63 } else {
64 $this->members[$attrs['cn'][0]]= "";
65 }
66 }
67 }
68 }
69 }
71 /***************
72 Perpare NTP settings
73 ***************/
75 /* Create used ntp server array */
76 $this->gotoNtpServer= array();
77 if(isset($this->attrs['gotoNtpServer'])){
78 $this->inheritTimeServer = false;
79 unset($this->attrs['gotoNtpServer']['count']);
80 foreach($this->attrs['gotoNtpServer'] as $server){
81 $this->gotoNtpServer[$server] = $server;
82 }
83 }
85 /* Set inherit checkbox state */
86 if(in_array("default",$this->gotoNtpServer)){
87 $this->inheritTimeServer = true;
88 $this->gotoNtpServer=array();
89 }
91 /* Create available ntp options */
92 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
93 foreach($this->gotoNtpServers as $key => $server){
94 if($server == "default"){
95 unset($this->gotoNtpServers[$key]);
96 }
97 }
99 if($this->is_account){
100 @log::log("view","ogroups/".get_class($this),$this->dn);
101 }
102 }
104 function check()
105 {
106 /* Call common method to give check the hook */
107 $message= plugin::check();
109 if (!$this->acl_is_createable() && $this->dn == "new"){
110 $message[]= _("You have no permissions to create a workstation on this 'Base'.");
111 }
113 /* Check for valid ntpServer selection */
114 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
115 $message[]= _("There must be at least one NTP server selected.");
116 }
117 return($message);
118 }
120 function remove_from_parent()
121 {
122 /* Workstation startup is using gotoWorkstationTemplate too,
123 if we remove this oc all other not manged attributes will cause errors */
124 if(isset($this->attrs['gotoKernelParameters'])){
125 $this->objectclasses = array();
126 }
128 /* Remove acc */
129 plugin::remove_from_parent();
130 $ldap = $this->config->get_ldap_link();
131 $ldap->cd($this->orig_dn);
132 $ldap->modify($this->attrs);
133 $this->handle_post_events("remove");
134 @log::log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
135 }
138 function update_term_member_FAIstate($act)
139 {
140 /* Get required informations */
141 $og = $this->parent->by_object['ogroup'];
142 $allobs = $og->objcache;
144 /* Get correct value for FAIstate */
145 $action = $this->mapActions[$act];
147 /* Get ldap connection */
148 $ldap = $this->config->get_ldap_link();
149 $ldap->cd ($this->config->current['BASE']);
151 /* Foreach member of mthis ogroup ... */
152 foreach($og->member as $key ){
154 /* check objectClasses and create attributes array */
155 $attrs = array("FAIstate"=>$action);
156 for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
157 $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
158 }
159 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
160 $attrs['objectClass'][] = "FAIobject";
161 }
162 if($attrs['FAIstate'] == ""){
163 $attrs['FAIstate'] = array();
164 }
166 /* If this objects is workstation,terminal or server upodate FAIstate */
167 if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
168 $ldap->cd ($key);
169 $ldap->modify($attrs);
170 show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
172 if(!preg_match("/success/i",$ldap->get_error())) {
173 gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
174 }else{
175 gosa_log("OK. Updating FAIstate to '".$action."' : ".$key);
176 }
177 }
178 }
179 }
181 function execute()
182 {
183 /* Call parent execute */
184 plugin::execute();
187 /***************
188 Handle requested action
189 ***************/
191 /* Watch for events */
192 if (isset($_POST['action'])){
193 $macaddresses="";
194 $names="";
195 foreach ($this->members as $cn => $macAddress){
196 $macaddresses.= "$macAddress ";
197 $names.= "$cn ";
198 }
200 if (isset($_POST['action'])){
202 /* Update members fai state */
203 $this->update_term_member_FAIstate(trim($_POST['saction']));
205 $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
207 if ($cmd == ""){
208 print_red(_("No ACTIONCMD definition found in your gosa.conf"));
209 } else {
210 exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
211 if ($retval != 0){
212 print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
213 } else {
214 $this->didAction= TRUE;
216 /* Get dns from member objects. Create ldap object */
217 $member = $this->parent->by_object['ogroup']->member;
218 $ldap = $this->config->get_ldap_link();
219 $ldap->cd($this->config->current['BASE']);
221 /* walk trough members and add FAIstate */
222 foreach($member as $dn => $object){
224 /* Get object */
225 $ldap->cat($dn,array("objectClass"));
226 $res = $ldap->fetch();
227 $attrs = array();
229 /* Add FAI state */
230 $attrs['FAIstate'] = "";
231 if(isset($this->mapActions[$_POST['saction']])){
232 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
233 }
235 /* Fix objectClass index */
236 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
237 $attrs['objectClass'][] = $res['objectClass'][$i];
238 }
240 /* Check if we must add the objectClass */
241 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
242 $attrs['objectClass'][] = "FAIobject";
243 }
245 if($attrs['FAIstate'] == ""){
246 $attrs['FAIstate'] = array();
247 }
249 $ldap->cd($dn);
250 $ldap->modify($attrs);
251 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
252 }
253 $this->didAction= TRUE;
254 }
255 }
256 }
257 }
260 /***************
261 Add remove NTP server
262 ***************/
264 /* Add new ntp Server to our list */
265 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
266 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
267 }
269 /* Delete selected NtpServer for list of used servers */
270 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
271 foreach($_POST['gotoNtpServerSelected'] as $name){
272 unset($this->gotoNtpServer[$name]);
273 }
274 }
277 /***************
278 Prepare smarty
279 ***************/
281 /* Set government mode */
282 $smarty= get_smarty();
284 $tmp = $this->plInfo();
285 foreach($tmp['plProvidedAcls'] as $name => $translated) {
286 $smarty->assign($name."ACL",$this->getacl($name));
287 }
289 foreach($this->attributes as $attr){
290 $smarty->assign($attr, $this->$attr);
291 }
293 /* Variables */
294 foreach(array("gotoMode","gotoNtpServer") as $val){
295 $smarty->assign($val."_select", $this->$val);
296 }
298 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
299 "instant_update" => _("Instant update"),
300 "update" => _("Scheduled update"),
301 "reinstall" => _("Reinstall"),
302 "rescan" => _("Rescan hardware"),
303 "memcheck" => _("Memory test"),
304 "sysinfo" => _("System analysis")));
306 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
307 $smarty->assign("modes", $this->modes);
309 $tmp = array();
310 foreach($this->gotoNtpServers as $server){
311 if(!in_array($server,$this->gotoNtpServer)){
312 $tmp[$server] = $server;
313 }
314 }
316 $smarty->assign("gotoNtpServers",$tmp);
317 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
318 $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer);
320 /* Show main page */
321 return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
322 }
324 function save_object()
325 {
326 plugin::save_object();
327 /* Set inherit mode */
328 if(isset($_POST['workgeneric_posted'])){
329 if(isset($_POST["inheritTimeServer"])){
330 $this->inheritTimeServer = true;
331 }else{
332 $this->inheritTimeServer = false;
333 }
334 }
335 }
337 /* Save to LDAP */
338 function save()
339 {
340 plugin::save();
342 /***************
343 Prepare special vars
344 ***************/
346 /* Unset some special vars ... */
347 foreach (array("gotoSyslogServer") as $val){
348 if ($this->attrs[$val] == "default"){
349 $this->attrs[$val]= array();
350 }
351 }
353 /* Update ntp server settings */
354 if($this->inheritTimeServer){
355 $this->attrs['gotoNtpServer'] = "default";
356 }else{
357 /* Set ntpServers */
358 $this->attrs['gotoNtpServer'] = array();
359 foreach($this->gotoNtpServer as $server){
360 $this->attrs['gotoNtpServer'][] = $server;
361 }
362 }
365 /***************
366 Write to ldap
367 ***************/
369 /* Write back to ldap */
370 $ldap= $this->config->get_ldap_link();
371 $ldap->cd($this->dn);
372 $this->cleanup();
373 $ldap->modify ($this->attrs);
375 if($this->initially_was_account){
376 @log::log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
377 }else{
378 @log::log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
379 }
381 if(!$this->didAction){
382 $this->handle_post_events("modify");
383 }
384 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
386 }
388 function plInfo()
389 {
390 return (array(
391 "plShortName" => _("System"),
392 "plDescription" => _("System group"),
393 "plSelfModify" => FALSE,
394 "plDepends" => array(),
395 "plPriority" => 5,
396 "plSection" => array("administration"),
397 "plCategory" => array("ogroups"),
398 "plProvidedAcls"=> array(
399 "gotoMode" => _("Mode"),
400 "gotoSyslogServer" => _("Syslog server"),
401 "FAIstate" => _("Action flag"),
402 "gotoNtpServer" => _("Ntp server"))
403 ));
404 }
408 }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>