1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2004 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 require "tabs_ogroups.inc";
22 class ogroupManagement extends plugin
23 {
24 /* Definitions */
25 var $plHeadline= "Object groups";
26 var $plDescription= "This does something";
28 /* attribute list for save action */
29 var $attributes= array();
30 var $objectclasses= array();
31 var $obtypes= array();
32 var $ogroup;
34 var $CopyPasteHandler = NULL;
35 var $DivListOGroup = NULL;
37 function ogroupManagement ($config, $dn= NULL)
38 {
39 /* Include config object */
40 $this->config= $config;
41 $this->ui= get_userinfo();
43 /* Copy & Paste enabled ?
44 */
45 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
46 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
47 }
49 /* Div lsit management */
50 $this->DivListOGroup = new divListOGroup($this->config,$this);
51 }
53 function execute()
54 {
55 /* Call parent execute */
56 plugin::execute();
58 /****************
59 Variable intialisation && Check posts for commands
60 ****************/
62 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_ogroups/");
64 $smarty = get_smarty();
65 $s_action = "";
66 $s_entry = "";
68 /* Test Posts */
69 foreach($_POST as $key => $val){
70 // Post for delete
71 if(preg_match("/^group_del.*/",$key)){
72 $s_action = "del";
73 $s_entry = preg_replace("/^group_".$s_action."_/i","",$key);
74 // Post for edit
75 }elseif(preg_match("/^group_edit_.*/",$key)){
76 $s_action="edit";
77 $s_entry = preg_replace("/^group_".$s_action."_/i","",$key);
78 // Post for new
79 }elseif(preg_match("/^group_new.*/",$key)){
80 $s_action="new";
81 }elseif(preg_match("/^editPaste.*/i",$key)){
82 $s_action="editPaste";
83 }elseif(preg_match("/^copy_.*/",$key)){
84 $s_action="copy";
85 $s_entry = preg_replace("/^copy_/i","",$key);
86 }elseif(preg_match("/^cut_.*/",$key)){
87 $s_action="cut";
88 $s_entry = preg_replace("/^cut_/i","",$key);
89 }elseif(preg_match("/^remove_multiple_ogroups/",$key)){
90 $s_action="del_multiple";
91 }elseif(preg_match("/_group_edit_/",$key)){
92 $type = preg_replace("/_group_edit_.*$/","",$key);
93 $s_action="edit";
94 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
95 $_POST['arg'] = $type;
96 }
97 }
98 $s_entry = preg_replace("/_.$/","",$s_entry);
100 // Edit if
101 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
102 $s_action ="edit";
103 $s_entry = $_GET['id'];
104 }
107 /****************
108 Copy & Paste handling
109 ****************/
111 /* Display the copy & paste dialog, if it is currently open */
112 $ret = $this->copyPasteHandling($s_action,$s_entry);
113 if($ret){
114 return($ret);
115 }
117 /****************
118 Create a new object group
119 ****************/
121 /* New group? */
122 if ($s_action=="new"){
124 /* By default we set 'dn' to 'new', all relevant plugins will
125 react on this. */
126 $this->dn= "new";
128 /* Create new usertab object */
129 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
130 $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
131 }
134 /********************
135 Delete MULTIPLE entries requested, display confirm dialog
136 ********************/
137 if ($s_action=="del_multiple"){
138 $ids = $this->list_get_selected_items();
139 if(count($ids)){
140 foreach($ids as $id){
141 $dn = $this->ogrouplist[$id]['dn'];
142 if (($user= get_lock($dn)) != ""){
143 return(gen_locked_message ($user, $dn));
144 }
145 $this->dns[$id] = $dn;
146 }
147 $dns_names = "<br><pre>";
148 foreach($this->dns as $dn){
149 add_lock ($dn, $this->ui->dn);
150 $dns_names .= $dn."\n";
151 }
152 $dns_names .="</pre>";
153 /* Lock the current entry, so nobody will edit it during deletion */
154 if (count($this->dns) == 1){
155 $smarty->assign("info", sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
156 } else {
157 $smarty->assign("info", sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
158 }
159 $smarty->assign("multiple", true);
160 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
161 }
162 }
165 /********************
166 Delete MULTIPLE entries confirmed
167 ********************/
169 /* Confirmation for deletion has been passed. Users should be deleted. */
170 if (isset($_POST['delete_multiple_ogroup_confirm'])){
172 /* Remove user by user and check acls before removeing them */
173 foreach($this->dns as $key => $dn){
175 $acl = $this->ui->get_permissions($dn, "ogroups");
176 if (preg_match('/d/', $acl)){
178 /* Delete request is permitted, perform LDAP action */
179 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
180 $this->ogroup->delete ();
181 gosa_log ("Object group'".$dn."' has been removed");
182 unset ($this->ogroup);
183 $this->ogroup= NULL;
184 } else {
186 /* Normally this shouldn't be reached, send some extra
187 logs to notify the administrator */
188 print_red (_("You are not allowed to delete this object group!"));
189 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
190 }
191 /* Remove lock file after successfull deletion */
192 del_lock ($dn);
193 unset($this->dns[$key]);
194 }
195 }
198 /********************
199 Delete MULTIPLE entries Canceled
200 ********************/
202 /* Remove lock */
203 if(isset($_POST['delete_multiple_ogroup_cancel'])){
204 foreach($this->dns as $key => $dn){
205 del_lock ($dn);
206 unset($this->dns[$key]);
207 }
208 }
211 /****************
212 Delete object group
213 ****************/
215 if ($s_action=="del"){
217 /* Get 'dn' from posted 'uid' */
218 $this->dn= $this->ogrouplist[$s_entry]['dn'];
220 /* Load permissions for selected 'dn' and check if
221 we're allowed to remove this 'dn' */
222 $acl = $this->ui->get_permissions($this->dn,"ogroups");
223 if(preg_match("/d/",$acl)){
225 /* Check locking, save current plugin in 'back_plugin', so
226 the dialog knows where to return. */
227 if (($user= get_lock($this->dn)) != ""){
228 return(gen_locked_message ($user, $this->dn));
229 }
231 /* Lock the current entry, so nobody will edit it during deletion */
232 add_lock ($this->dn, $this->ui->dn);
233 $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
234 $smarty->assign("multiple", false);
235 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
236 } else {
238 /* Obviously the user isn't allowed to delete. Show message and
239 clean session. */
240 print_red (_("You are not allowed to delete this object group!"));
241 }
242 }
245 /****************
246 Delete confirmed
247 ****************/
249 /* Confirmation for deletion has been passed. Group should be deleted. */
250 if (isset($_POST['delete_group_confirm'])){
252 /* Some nice guy may send this as POST, so we've to check
253 for the permissions again. */
254 $acl = $this->ui->get_permissions($this->dn,"ogroups");
255 if(preg_match("/d/",$acl)){
257 /* Delete request is permitted, perform LDAP action */
258 $this->ogroup= new ogrouptabs($this->config,
259 $this->config->data['TABS']['OGROUPTABS'], $this->dn);
260 $this->ogroup->delete ();
261 gosa_log ("Object group'".$this->dn."' has been removed");
262 unset ($this->ogroup);
263 $this->ogroup= NULL;
264 } else {
266 /* Normally this shouldn't be reached, send some extra
267 logs to notify the administrator */
268 print_red (_("You are not allowed to delete this object group!"));
269 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
270 }
272 /* Remove lock file after successfull deletion */
273 del_lock ($this->dn);
274 unset($_SESSION['objectinfo']);
275 }
278 /****************
279 Delete object group Canceled
280 ****************/
282 /* Delete group canceled? */
283 if (isset($_POST['delete_cancel'])){
284 del_lock ($this->dn);
285 unset($_SESSION['objectinfo']);
286 }
289 /****************
290 Edit group
291 ****************/
293 if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
295 /* Get 'dn' from posted 'uid', must be unique */
296 $this->dn= $this->ogrouplist[$s_entry]['dn'];
298 /* Check locking, save current plugin in 'back_plugin', so
299 the dialog knows where to return. */
300 if (($user= get_lock($this->dn)) != ""){
301 return(gen_locked_message ($user, $this->dn));
302 }
304 /* Lock the current entry, so everyone will get the
305 above dialog */
306 add_lock ($this->dn, $this->ui->dn);
308 /* Register grouptab to trigger edit dialog */
309 $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
310 $this->ogroup->set_acl_base($this->dn);
311 $_SESSION['objectinfo']= $this->dn;
312 }
315 /****************
316 Edit finished save
317 ****************/
319 /* Finish button has been pressed */
320 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
322 /* Check tabs, will feed message array */
323 $message= $this->ogroup->check();
325 /* Save, or display error message? */
326 if (count($message) == 0){
328 /* Save user data to ldap */
329 $this->ogroup->save();
330 gosa_log ("Object group'".$this->dn."' has been saved");
332 if (!isset($_POST['edit_apply'])){
333 /* Group has been saved successfully, remove lock from
334 LDAP. */
335 if ($this->dn != "new"){
336 del_lock ($this->dn);
337 }
339 unset ($this->ogroup);
340 $this->ogroup= NULL;
341 unset ($_SESSION['objectinfo']);
342 }
343 } else {
344 /* Ok. There seem to be errors regarding to the tab data,
345 show message and continue as usual. */
346 show_errors($message);
347 }
348 }
351 /****************
352 Cancel edit object group
353 ****************/
355 /* Cancel dialogs */
356 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
357 del_lock ($this->ogroup->dn);
358 unset ($this->ogroup);
359 $this->ogroup= NULL;
360 unset($_SESSION['objectinfo']);
361 }
364 /****************
365 Display open dialogs
366 ****************/
368 /* Show dialog if object is present */
369 if (isset($this->ogroup->config)){
370 $display= $this->ogroup->execute();
372 /* Don't show buttons if tab dialog requests this */
373 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
374 $display.= "<p style=\"text-align:right\">\n";
375 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
376 $display.= " \n";
377 if ($this->dn != "new"){
378 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
379 $display.= " \n";
380 }
381 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
382 $display.= "</p>";
383 }
384 return ($display);
385 }
388 /****************
389 Display list
390 ****************/
392 /* Check if there is a snapshot dialog open */
393 $base = $this->DivListOGroup->selectedBase;
394 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
395 return($str);
396 }
398 /* Display dialog with group list */
399 $this->DivListOGroup->parent = $this;
400 $this->DivListOGroup->execute();
402 /* Add departments if subsearch is disabled */
403 if(!$this->DivListOGroup->SubSearch){
404 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
405 }
406 $this->reload ();
407 $this->DivListOGroup->setEntries($this->ogrouplist);
408 return($this->DivListOGroup->Draw());
409 }
412 /* Return departments, that will be included within snapshot detection */
413 function get_used_snapshot_bases()
414 {
415 return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
416 }
420 function convert_list($input)
421 {
422 $temp= "";
424 $conv= array(
425 "U" => array("select_user.png" ,_("User") , "ogroup"),
426 "G" => array("select_groups.png" ,_("Group") , "ogroup"),
427 "A" => array("select_application.png" ,_("Application") , "ogroup"),
428 "D" => array("select_department.png" ,_("Department") , "ogroup"),
429 "S" => array("select_server.png" ,_("Server") , "ogroup"),
430 "F" => array("select_phone.png" ,_("Phone") , "phonequeue"),
431 "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
432 "T" => array("select_terminal.png" ,_("Terminal") , "termgroup"),
433 "P" => array("select_printer.png" ,_("Printer") , "ogroup"));
435 /* Assemble picture */
436 $type= $input['gosaGroupObjects'][0];
437 $type= preg_replace("/[^A-Z]/i","",$type);
438 if (isset($type[0])){
439 $p1['pic']= $conv[$type[0]][0];
440 $p1['tit']= $conv[$type[0]][1];
441 $p1['alt']= $type[0];
442 $p1['lnk']= $conv[$type[0]][2];
443 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
444 } else {
445 $p1['pic']= "empty.png";
446 $p1['tit']= "";
447 $p1['alt']= "";
448 $p1['lnk']= "";//$conv[$type[0]][2];
449 $html_object_1 = "<img ";
450 }
452 if (isset($type[1])){
453 $p2['pic']= $conv[$type[1]][0];
454 $p2['alt']= $type[1];
455 $p2['tit']= $conv[$type[1]][1];
456 $p2['lnk']= $conv[$type[1]][2];
457 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
458 } else {
459 $p2['pic']= "empty.png";
460 $p2['alt']= "";
461 $p2['tit']= "";
462 $p2['lnk']= ""; //$conv[$type[0]][2];
463 $html_object_2 = "<img ";
464 }
466 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
467 src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
468 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
469 src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
470 return ($temp);
471 }
474 function reload()
475 {
476 /* Set base for all searches && initialise some vars */
477 $this->ogrouplist= array();
478 $base = $this->DivListOGroup->selectedBase;
479 $filter = "(gosaGroupObjects=[])";
480 $Regex = $this->DivListOGroup->Regex;
482 $chk = array(
483 "UserGroups" => "(gosaGroupObjects=*U*)" ,
484 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
485 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
486 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
487 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
488 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
489 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
490 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
491 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
492 /* Create filter */
493 foreach($chk as $chkBox => $FilterPart){
494 if($this->DivListOGroup->$chkBox){
495 $filter .= $FilterPart;
496 }
497 }
498 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
500 if($this->DivListOGroup->SubSearch){
501 $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
502 }else{
503 $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
504 }
506 $this->ogrouplist= $res;
507 ksort ($this->ogrouplist);
508 reset ($this->ogrouplist);
509 $tmp=array();
510 foreach($this->ogrouplist as $tkey => $val ){
511 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
512 }
513 ksort($tmp);
514 $this->ogrouplist=array();
515 foreach($tmp as $val){
516 $this->ogrouplist[]=$val;
517 }
518 reset ($this->ogrouplist);
519 }
522 function list_get_selected_items()
523 {
524 $ids = array();
525 foreach($_POST as $name => $value){
526 if(preg_match("/^item_selected_[0-9]*$/",$name)){
527 $id = preg_replace("/^item_selected_/","",$name);
528 $ids[$id] = $id;
529 }
530 }
531 return($ids);
532 }
535 function copyPasteHandling($s_action,$s_entry)
536 {
537 if($this->CopyPasteHandler){
539 /* Paste copied/cutted object in here
540 */
541 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
542 $this->CopyPasteHandler->save_object();
543 $this->CopyPasteHandler->SetVar("base", $this->DivListOGroup->selectedBase);
544 return($this->CopyPasteHandler->execute());
545 }
547 /* Copy current object to CopyHandler
548 */
549 if($s_action == "copy"){
552 $this->CopyPasteHandler->Clear();
553 $dn = $this->ogrouplist[$s_entry]['dn'];
555 /* Check acls */
556 $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
557 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
558 $obj = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
559 $objNew = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], "new");
561 $types_of_tabs = $obj->by_object['ogroup']->gosaGroupObjects;
562 $objNew->by_object['ogroup']->gosaGroupObjects = $types_of_tabs;
563 $objNew->reload($types_of_tabs);
565 $this->CopyPasteHandler->Copy($obj,$objNew);
566 }
567 }
569 /* Copy current object to CopyHandler
570 */
571 if($s_action == "cut"){
572 $this->CopyPasteHandler->Clear();
573 $dn = $this->ogrouplist[$s_entry]['dn'];
575 /* Check acls */
576 $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
577 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
578 $obj = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
579 $this->CopyPasteHandler->Cut($obj);
580 }
581 }
582 }
583 }
586 function save_object()
587 {
588 $this->DivListOGroup->save_object();
589 }
591 }
593 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
594 ?>