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 "wake" => "",
23 "memcheck" => "memcheck",
24 "sysinfo" => "sysinfo");
26 var $attributes = array("gotoMode","gotoSyslogServer", "gotoNtpServer");
27 var $objectclasses = array("gotoWorkstationTemplate");
28 var $CopyPasteVars = array("gotoNtpServers","modes","inheritTimeServer","members");
29 var $view_logged = FALSE;
32 function termgroup (&$config, $dn= NULL, $parent= NULL)
33 {
34 /***************
35 Some initialisations
36 ***************/
38 plugin::plugin($config, $dn, $parent);
40 $ldap= $config->get_ldap_link();
42 $this->is_account = true;
43 $this->modes["active"]= _("Activated");
44 $this->modes["locked"]= _("Locked");
45 // $this->modes["memcheck"]= _("Memory test");
46 // $this->modes["sysinfo"]= _("System analysis");
48 $this->orig_dn = $this->dn;
50 /***************
51 Get mac addresses from member objects
52 ***************/
54 /* We're only interested in the terminal members here, evaluate
55 these... */
56 if(isset($this->attrs['member'])){
57 for ($i= 0; $i<$this->attrs['member']['count']; $i++){
58 $member= $this->attrs['member'][$i];
59 $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
60 if (preg_match("/success/i", $ldap->error)){
61 $attrs = $ldap->fetch();
62 if (in_array("gotoTerminal", $attrs['objectClass']) ||
63 in_array("gotoWorkstation", $attrs['objectClass'])){
64 if (isset($attrs['macAddress'])){
65 $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
66 } else {
67 $this->members[$attrs['cn'][0]]= "";
68 }
69 }
70 }
71 }
72 }
74 /***************
75 Perpare NTP settings
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)){
90 $this->inheritTimeServer = true;
91 $this->gotoNtpServer=array();
92 }
94 /* Create available ntp options */
95 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
96 foreach($this->gotoNtpServers as $key => $server){
97 if($server == "default"){
98 unset($this->gotoNtpServers[$key]);
99 }
100 }
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 new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
134 }
137 function update_term_member_FAIstate($act)
138 {
139 /* Get required informations */
140 $og = $this->parent->by_object['ogroup'];
141 $allobs = $og->objcache;
143 /* Get correct value for FAIstate */
144 $action = $this->mapActions[$act];
146 /* Get ldap connection */
147 $ldap = $this->config->get_ldap_link();
148 $ldap->cd ($this->config->current['BASE']);
150 /* Foreach member of mthis ogroup ... */
151 foreach($og->member as $key ){
153 /* check objectClasses and create attributes array */
154 $attrs = array("FAIstate"=>$action);
155 for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
156 $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
157 }
158 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
159 $attrs['objectClass'][] = "FAIobject";
160 }
161 if($attrs['FAIstate'] == ""){
162 $attrs['FAIstate'] = array();
163 }
165 /* If this objects is workstation,terminal or server upodate FAIstate */
166 if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
167 $ldap->cd ($key);
168 $ldap->modify($attrs);
169 show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
170 }
171 }
172 }
174 function execute()
175 {
176 /* Call parent execute */
177 plugin::execute();
179 if(!$this->view_logged){
180 $this->view_logged = TRUE;
181 new log("view","ogroups/".get_class($this),$this->dn);
182 }
184 /***************
185 Handle requested action
186 ***************/
188 /* Watch for events */
189 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
190 $macaddresses= array();
191 foreach ($this->members as $cn => $macAddress){
192 $macaddresses[]= $macAddress;
193 }
195 gosaSupportDaemon::send("gosa_trigger_action_".$_POST['saction'], "255.255.255.255", $macaddresses);
197 /* Update members fai state */
198 $this->update_term_member_FAIstate(trim($_POST['saction']));
200 $this->didAction= TRUE;
202 /* Get dns from member objects. Create ldap object */
203 $member = $this->parent->by_object['ogroup']->member;
204 $ldap = $this->config->get_ldap_link();
205 $ldap->cd($this->config->current['BASE']);
207 /* walk trough members and add FAIstate */
208 if ($_POST['saction'] != "wake"){
209 foreach($member as $dn => $object){
211 /* Get object */
212 $ldap->cat($dn,array("objectClass"));
213 $res = $ldap->fetch();
214 $attrs = array();
216 /* Add FAI state */
217 $attrs['FAIstate'] = "";
218 if(isset($this->mapActions[$_POST['saction']])){
219 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
220 }
222 /* Fix objectClass index */
223 for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
224 $attrs['objectClass'][] = $res['objectClass'][$i];
225 }
227 /* Check if we must add the objectClass */
228 if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
229 $attrs['objectClass'][] = "FAIobject";
230 }
232 if($attrs['FAIstate'] == ""){
233 $attrs['FAIstate'] = array();
234 }
236 $ldap->cd($dn);
237 $ldap->modify($attrs);
238 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
239 }
240 }
241 }
244 /***************
245 Add remove NTP server
246 ***************/
248 /* Add new ntp Server to our list */
249 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
250 $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
251 }
253 /* Delete selected NtpServer for list of used servers */
254 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
255 foreach($_POST['gotoNtpServerSelected'] as $name){
256 unset($this->gotoNtpServer[$name]);
257 }
258 }
261 /***************
262 Prepare smarty
263 ***************/
265 /* Set government mode */
266 $smarty= get_smarty();
268 $tmp = $this->plInfo();
269 foreach($tmp['plProvidedAcls'] as $name => $translated) {
270 $smarty->assign($name."ACL",$this->getacl($name));
271 }
273 foreach($this->attributes as $attr){
274 $smarty->assign($attr, $this->$attr);
275 }
277 /* Variables */
278 foreach(array("gotoMode","gotoNtpServer") as $val){
279 $smarty->assign($val."_select", $this->$val);
280 }
282 $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
283 "instant_update" => _("Instant update"),
284 "update" => _("Scheduled update"),
285 "wake" => _("Wake"),
286 "reinstall" => _("Reinstall"),
287 "rescan" => _("Rescan hardware"),
288 "memcheck" => _("Memory test"),
289 "sysinfo" => _("System analysis")));
291 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
292 $smarty->assign("modes", $this->modes);
294 $tmp = array();
295 foreach($this->gotoNtpServers as $server){
296 if(!in_array($server,$this->gotoNtpServer)){
297 $tmp[$server] = $server;
298 }
299 }
301 $smarty->assign("gotoNtpServers",$tmp);
302 $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
303 $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer);
305 /* Show main page */
306 return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
307 }
309 function save_object()
310 {
311 plugin::save_object();
312 /* Set inherit mode */
313 if(isset($_POST['workgeneric_posted'])){
314 if(isset($_POST["inheritTimeServer"])){
315 $this->inheritTimeServer = true;
316 }else{
317 $this->inheritTimeServer = false;
318 }
319 }
320 }
322 /* Save to LDAP */
323 function save()
324 {
325 plugin::save();
327 /***************
328 Prepare special vars
329 ***************/
331 /* Unset some special vars ... */
332 foreach (array("gotoSyslogServer") as $val){
333 if ($this->attrs[$val] == "default"){
334 $this->attrs[$val]= array();
335 }
336 }
338 /* Update ntp server settings */
339 if($this->inheritTimeServer){
340 $this->attrs['gotoNtpServer'] = "default";
341 }else{
342 /* Set ntpServers */
343 $this->attrs['gotoNtpServer'] = array();
344 foreach($this->gotoNtpServer as $server){
345 $this->attrs['gotoNtpServer'][] = $server;
346 }
347 }
350 /***************
351 Write to ldap
352 ***************/
354 /* Write back to ldap */
355 $ldap= $this->config->get_ldap_link();
356 $ldap->cd($this->dn);
357 $this->cleanup();
358 $ldap->modify ($this->attrs);
360 if($this->initially_was_account){
361 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
362 }else{
363 new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
364 }
366 if(!$this->didAction){
367 $this->handle_post_events("modify");
368 }
369 show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
371 }
373 static function plInfo()
374 {
375 return (array(
376 "plShortName" => _("System"),
377 "plDescription" => _("System group"),
378 "plSelfModify" => FALSE,
379 "plDepends" => array(),
380 "plPriority" => 5,
381 "plSection" => array("administration"),
382 "plCategory" => array("ogroups"),
383 "plProvidedAcls"=> array(
384 "gotoMode" => _("Mode"),
385 "gotoSyslogServer" => _("Syslog server"),
386 "FAIstate" => _("Action flag"),
387 "gotoNtpServer" => _("Ntp server"))
388 ));
389 }
391 function PrepareForCopyPaste($source)
392 {
393 /* Create used ntp server array */
394 $this->gotoNtpServer= array();
396 if(isset($source['gotoNtpServer'])){
397 $this->inheritTimeServer = false;
398 unset($source['gotoNtpServer']['count']);
399 foreach($source['gotoNtpServer'] as $server){
400 $this->gotoNtpServer[$server] = $server;
401 }
402 }
404 /* Set inherit checkbox state */
405 if(in_array("default",$this->gotoNtpServer)){
406 $this->inheritTimeServer = true;
407 $this->gotoNtpServer=array();
408 }
410 /* Create available ntp options */
411 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
412 foreach($this->gotoNtpServers as $key => $server){
413 if($server == "default"){
414 unset($this->gotoNtpServers[$key]);
415 }
416 }
417 }
418 }
420 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
421 ?>