1 <?php
3 class termgroup extends plugin
4 {
5 /* attribute list for save action */
6 var $members= array();
8 /* Stores a list of the member dn's */
9 var $member_dn = array();
11 var $gotoMode = "locked";
12 var $gotoSyslogServer = "";
13 var $gotoNtpServer = array();
14 var $gotoTerminalPath = array();
15 var $gotoSwapServer = array();
16 var $modes = array();
17 var $inheritTimeServer = true;
18 var $is_account = true;
19 var $orig_dn = "";
20 var $didAction = FALSE;
21 var $mapActions = array("halt" => "halt",
22 "reboot" => "reboot",
23 "update" => "update",
24 "reinstall" => "reinstall",
25 "rescan" => "rescan",
26 "wake" => "wakeup",
27 );
29 var $attributes = array("gotoMode","gotoSyslogServer", "gotoNtpServer", "gotoTerminalPath", "gotoSwapServer");
30 var $objectclasses = array("gotoWorkstationTemplate");
31 var $CopyPasteVars = array("gotoNtpServers","modes","inheritTimeServer","members");
32 var $view_logged = FALSE;
33 var $nfsservers = array();
34 var $swapservers = array();
35 var $member_of_ogroup= false;
36 var $members_inherit_from_group = false;
38 function termgroup (&$config, $dn= NULL, $parent= NULL)
39 {
40 /***************
41 Some initialisations
42 ***************/
44 plugin::plugin($config, $dn, $parent);
45 $ldap= $config->get_ldap_link();
47 $this->is_account = true;
48 $this->modes["active"]= _("Activated");
49 $this->modes["locked"]= _("Locked");
50 // $this->modes["memcheck"]= _("Memory test");
51 // $this->modes["sysinfo"]= _("System analysis");
53 $this->orig_dn = $this->dn;
55 /***************
56 Get mac addresses from member objects
57 ***************/
59 /* We're only interested in the terminal members here, evaluate
60 these... */
61 if(isset($this->attrs['member'])){
62 for ($i= 0; $i<$this->attrs['member']['count']; $i++){
63 $member= $this->attrs['member'][$i];
64 array_push($this->member_dn, @LDAP::convert($member));
65 $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
66 if ($ldap->success()){
67 $attrs = $ldap->fetch();
68 if (in_array("gotoTerminal", $attrs['objectClass']) ||
69 in_array("gotoWorkstation", $attrs['objectClass'])){
70 if (isset($attrs['macAddress'])){
71 $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
72 } else {
73 $this->members[$attrs['cn'][0]]= "";
74 }
75 }
76 }
77 }
78 }
80 /***************
81 Perpare NTP settings
82 ***************/
84 /* Create used ntp server array */
85 $this->gotoNtpServer= array();
86 if(isset($this->attrs['gotoNtpServer'])){
87 $this->inheritTimeServer = false;
88 unset($this->attrs['gotoNtpServer']['count']);
89 foreach($this->attrs['gotoNtpServer'] as $server){
90 $this->gotoNtpServer[$server] = $server;
91 }
92 }
94 /* Get Share servers */
95 $tmp2 = array();
96 $tmp2['!']= _("Local swap");
97 foreach($this->config->data['SERVERS']['NBD'] as $server){
98 if($server != "default"){
99 $tmp2[$server]= $server;
100 }else{
101 if($this->member_of_ogroup){
102 $tmp2[$server]="["._("inherited")."]";
103 }
104 }
105 }
106 $this->swapservers= $tmp2;
108 $tmp2 = array();
109 foreach($this->config->data['SERVERS']['NFS'] as $server){
110 if($server != "default"){
111 $tmp2[$server]= $server;
112 }else{
113 if($this->member_of_ogroup){
114 $tmp2[$server]="["._("inherited")."]";
115 }
116 }
117 }
118 $this->nfsservers= $tmp2;
120 /* Set inherit checkbox state */
121 if(in_array("default",$this->gotoNtpServer)){
122 $this->inheritTimeServer = true;
123 $this->gotoNtpServer=array();
124 }
126 /* Create available ntp options */
127 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
128 foreach($this->gotoNtpServers as $key => $server){
129 if($server == "default"){
130 unset($this->gotoNtpServers[$key]);
131 }
132 }
133 }
135 function check()
136 {
137 /* Call common method to give check the hook */
138 $message= plugin::check();
140 if (!$this->acl_is_createable() && $this->dn == "new"){
141 $message[]= msgPool::permCreate();
142 }
144 /* Check for valid ntpServer selection */
145 if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
146 $message[]= msgPool::required(_("NTP server"));
147 }
148 return($message);
149 }
151 function remove_from_parent()
152 {
153 /* Workstation startup is using gotoWorkstationTemplate too,
154 if we remove this oc all other not manged attributes will cause errors */
155 if(isset($this->attrs['gotoKernelParameters'])){
156 $this->objectclasses = array();
157 }
159 /* Remove acc */
160 plugin::remove_from_parent();
161 $ldap = $this->config->get_ldap_link();
162 $ldap->cd($this->orig_dn);
163 $ldap->modify($this->attrs);
164 $this->handle_post_events("remove");
165 new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
166 }
170 function execute()
171 {
172 /* Call parent execute */
173 plugin::execute();
175 if(!$this->view_logged){
176 $this->view_logged = TRUE;
177 new log("view","ogroups/".get_class($this),$this->dn);
178 }
181 /***************
182 Handle requested action
183 ***************/
185 /* Watch for events */
186 if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
188 /* Check if we have an DaemonEvent for this action */
189 $action = $this->mapActions[get_post('saction')];
190 if(class_available("DaemonEvent")){
191 $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
192 $macaddresses= array();
193 foreach ($this->members as $cn => $macAddress){
194 $macaddresses[]= $macAddress;
195 }
197 if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
198 $evt = $events['TRIGGERED']["DaemonEvent_".$action];
199 $tmp = new $evt['CLASS_NAME']($this->config);
200 $tmp->add_targets($macaddresses);
201 $tmp->set_type(TRIGGERED_EVENT);
202 $o_queue = new gosaSupportDaemon();
203 if(!$o_queue->append($tmp)){
204 msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
205 }
206 }
207 } else {
208 msg_dialog::display(_("Event error"),
209 sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
210 }
211 }
214 /***************
215 Add remove NTP server
216 ***************/
218 /* Add new ntp Server to our list */
219 if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
220 $this->gotoNtpServer[get_post('gotoNtpServers')] = get_post('gotoNtpServers');
221 }
223 /* Delete selected NtpServer for list of used servers */
224 if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
225 foreach(get_post('gotoNtpServerSelected') as $name){
226 unset($this->gotoNtpServer[$name]);
227 }
228 }
231 /***************
232 Prepare smarty
233 ***************/
235 /* Set government mode */
236 $smarty= get_smarty();
238 if (isset($this->parent->by_name['termstartup'])){
239 $smarty->assign("is_termgroup", "1");
240 } else {
241 $smarty->assign("is_termgroup", "0");
242 }
244 $tmp = $this->plInfo();
245 foreach($tmp['plProvidedAcls'] as $name => $translated) {
246 $smarty->assign($name."ACL",$this->getacl($name));
247 }
249 foreach($this->attributes as $attr){
250 $smarty->assign($attr,set_post($this->$attr));
251 }
253 /* Variables */
254 foreach(array("gotoMode","gotoNtpServer") as $val){
255 $smarty->assign($val."_select", set_post($this->$val));
256 }
258 $smarty->assign("actions",
259 set_post(array(
260 "halt" => _("Switch off"),
261 "reboot" => _("Reboot"),
262 "instant_update" => _("Instant update"),
263 "update" => _("Scheduled update"),
264 "wake" => _("Wake"),
265 "reinstall" => _("Reinstall"),
266 "rescan" => _("Rescan hardware"),
267 "memcheck" => _("Memory test"),
268 "sysinfo" => _("System analysis"))));
270 $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
271 $smarty->assign("modes", set_post($this->modes));
273 $tmp = array();
274 foreach($this->gotoNtpServers as $server){
275 if(!in_array($server,$this->gotoNtpServer)){
276 $tmp[$server] = $server;
277 }
278 }
280 $smarty->assign("gotoNtpServers", set_post($tmp));
282 $smarty->assign("nfsservers", set_post($this->nfsservers));
283 $smarty->assign("swapservers", set_post($this->swapservers));
285 $smarty->assign("syslogservers", set_post($this->config->data['SERVERS']['SYSLOG']));
286 $smarty->assign("gotoSyslogServer_select", set_post($this->gotoSyslogServer));
288 /* Variables */
289 foreach(array("gotoTerminalPath", "gotoSwapServer") as $val){
290 $smarty->assign($val."_select", set_post($this->$val));
291 }
294 /* Show main page */
295 $smarty->assign("members_inherit_from_group", $this->members_inherit_from_group);
296 return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
297 }
300 function save_object()
301 {
302 plugin::save_object();
303 /* Set inherit mode */
304 if(isset($_POST['workgeneric_posted'])){
306 /* Handle the inherit to members button */
307 $this->members_inherit_from_group = isset($_POST['members_inherit_from_group']);
309 if(isset($_POST["inheritTimeServer"])){
310 $this->inheritTimeServer = true;
311 }else{
312 $this->inheritTimeServer = false;
313 }
314 }
315 }
317 /* Save to LDAP */
318 function save()
319 {
320 if (isset($this->parent->by_name['termstartup'])){
321 $this->objectclasses= array("gotoTerminalTemplate");
322 } else {
323 $this->objectclasses= array("gotoWorkstationTemplate");
324 }
326 plugin::save();
328 /* Tell members to inherit all attributes if the flag is set */
329 $this->inherit_attributes_to_members();
331 /***************
332 Prepare special vars
333 ***************/
335 /* Unset some special vars ... */
336 foreach (array("gotoSyslogServer") as $val){
337 if ($this->attrs[$val] == "default"){
338 $this->attrs[$val]= array();
339 }
340 }
342 /* Update ntp server settings */
343 if($this->inheritTimeServer){
344 $this->attrs['gotoNtpServer'] = "default";
345 }else{
346 /* Set ntpServers */
347 $this->attrs['gotoNtpServer'] = array();
348 foreach($this->gotoNtpServer as $server){
349 $this->attrs['gotoNtpServer'][] = $server;
350 }
351 }
354 /***************
355 Write to ldap
356 ***************/
358 /* Write back to ldap */
359 $ldap= $this->config->get_ldap_link();
360 $ldap->cd($this->dn);
361 $this->cleanup();
362 $ldap->modify ($this->attrs);
364 if($this->initially_was_account){
365 new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
366 }else{
367 new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
368 }
370 if(!$this->didAction){
371 $this->handle_post_events("modify");
372 }
373 if (!$ldap->success()){
374 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
375 }
376 }
379 static function plInfo()
380 {
381 return (array(
382 "plShortName" => _("System"),
383 "plDescription" => _("System group"),
384 "plSelfModify" => FALSE,
385 "plDepends" => array(),
386 "plPriority" => 5,
387 "plSection" => array("administration"),
388 "plCategory" => array("ogroups"),
389 "plProvidedAcls"=> array(
390 "gotoMode" => _("Mode"),
391 "gotoSyslogServer" => _("Syslog server"),
392 "FAIstate" => _("Action flag"),
393 "gotoNtpServer" => _("NTP server"),
394 "gotoTerminalPath" => _("Root server"),
395 "gotoSwapServer" => _("Swap server"))
396 ));
397 }
399 function inherit_attributes_to_members()
400 {
401 if ($this->members_inherit_from_group) {
402 foreach ($this->member_dn as $dn) {
403 $member_obj = new worktabs($this->config, $this->config->data['TABS']['WORKTABS'], $dn, "workstation");
404 $member_obj->by_object['workgeneric']->set_everything_to_inherited();
405 $member_obj->save();
406 }
407 }
408 }
410 function PrepareForCopyPaste($source)
411 {
412 /* Create used ntp server array */
413 $this->gotoNtpServer= array();
415 $source_o = new termgroup ($this->config, $source['dn']);
416 foreach(array("gotoTerminalPath","gotoSwapServer","gotoSyslogServer","gotoMode") as $attr){
417 $this->$attr = $source_o->$attr;
418 }
420 if(isset($source['gotoNtpServer'])){
421 $this->inheritTimeServer = false;
422 unset($source['gotoNtpServer']['count']);
423 foreach($source['gotoNtpServer'] as $server){
424 $this->gotoNtpServer[$server] = $server;
425 }
426 }
428 /* Set inherit checkbox state */
429 if(in_array("default",$this->gotoNtpServer)){
430 $this->inheritTimeServer = true;
431 $this->gotoNtpServer=array();
432 }
434 /* Create available ntp options */
435 $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
436 foreach($this->gotoNtpServers as $key => $server){
437 if($server == "default"){
438 unset($this->gotoNtpServers[$key]);
439 }
440 }
441 }
442 }
444 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
445 ?>