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;
36 var $start_pasting_copied_objects = FALSE;
38 function ogroupManagement ($config, $dn= NULL)
39 {
40 /* Include config object */
41 $this->config= $config;
42 $this->ui= get_userinfo();
44 /* Copy & Paste enabled ?
45 */
46 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
47 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48 }
50 /* Div lsit management */
51 $this->DivListOGroup = new divListOGroup($this->config,$this);
52 }
54 function execute()
55 {
56 /* Call parent execute */
57 plugin::execute();
59 /****************
60 Variable intialisation && Check posts for commands
61 ****************/
63 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_ogroups/");
65 $smarty = get_smarty();
66 $s_action = "";
67 $s_entry = "";
69 /* Test Posts */
70 foreach($_POST as $key => $val){
71 // Post for delete
72 if(preg_match("/^group_del.*/",$key)){
73 $s_action = "del";
74 $s_entry = preg_replace("/^group_".$s_action."_/i","",$key);
75 // Post for edit
76 }elseif(preg_match("/^group_edit_.*/",$key)){
77 $s_action="edit";
78 $s_entry = preg_replace("/^group_".$s_action."_/i","",$key);
79 // Post for new
80 }elseif(preg_match("/^group_new.*/",$key)){
81 $s_action="new";
82 }elseif(preg_match("/^editPaste.*/i",$key)){
83 $s_action="editPaste";
84 }elseif(preg_match("/^copy_.*/",$key)){
85 $s_action="copy";
86 $s_entry = preg_replace("/^copy_/i","",$key);
87 }elseif(preg_match("/^cut_.*/",$key)){
88 $s_action="cut";
89 $s_entry = preg_replace("/^cut_/i","",$key);
90 }elseif(preg_match("/^remove_multiple_ogroups/",$key)){
91 $s_action="del_multiple";
92 }elseif(preg_match("/^multiple_cut_ogroups/",$key)){
93 $s_action = "cut_multiple";
94 }elseif(preg_match("/^multiple_copy_ogroups/",$key)){
95 $s_action = "copy_multiple";
96 }elseif(preg_match("/_group_edit_/",$key)){
97 $type = preg_replace("/_group_edit_.*$/","",$key);
98 $s_action="edit";
99 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
100 $_POST['arg'] = $type;
101 }
102 }
103 $s_entry = preg_replace("/_.$/","",$s_entry);
105 // Edit if
106 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
107 $s_action ="edit";
108 $s_entry = $_GET['id'];
109 }
112 /****************
113 Copy & Paste handling
114 ****************/
116 /* Display the copy & paste dialog, if it is currently open */
117 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
118 if($ret){
119 return($ret);
120 }
123 /***************
124 Create a new object group
125 ****************/
127 /* New group? */
128 if ($s_action=="new"){
130 /* By default we set 'dn' to 'new', all relevant plugins will
131 react on this. */
132 $this->dn= "new";
134 /* Create new usertab object */
135 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $this->dn,"ogroups");
136 $this->ogroup->set_acl_base($this->DivListOGroup->selectedBase);
137 }
140 /********************
141 Delete MULTIPLE entries requested, display confirm dialog
142 ********************/
143 if ($s_action=="del_multiple"){
144 $ids = $this->list_get_selected_items();
145 if(count($ids)){
146 foreach($ids as $id){
147 $dn = $this->ogrouplist[$id]['dn'];
148 if (($user= get_lock($dn)) != ""){
149 return(gen_locked_message ($user, $dn));
150 }
151 $this->dns[$id] = $dn;
152 }
153 $dns_names = "<br><pre>";
154 foreach($this->dns as $dn){
155 add_lock ($dn, $this->ui->dn);
156 $dns_names .= $dn."\n";
157 }
158 $dns_names .="</pre>";
159 /* Lock the current entry, so nobody will edit it during deletion */
160 if (count($this->dns) == 1){
161 $smarty->assign("info", sprintf(_("You're about to delete the following object entry %s"), @LDAP::fix($dns_names)));
162 } else {
163 $smarty->assign("info", sprintf(_("You're about to delete the following object entries %s"), @LDAP::fix($dns_names)));
164 }
165 $smarty->assign("multiple", true);
166 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
167 }
168 }
171 /********************
172 Delete MULTIPLE entries confirmed
173 ********************/
175 /* Confirmation for deletion has been passed. Users should be deleted. */
176 if (isset($_POST['delete_multiple_ogroup_confirm'])){
178 /* Remove user by user and check acls before removeing them */
179 foreach($this->dns as $key => $dn){
181 $acl = $this->ui->get_permissions($dn, "ogroups");
182 if (preg_match('/d/', $acl)){
184 /* Delete request is permitted, perform LDAP action */
185 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
186 $this->ogroup->delete ();
187 unset ($this->ogroup);
188 $this->ogroup= NULL;
189 } else {
191 /* Normally this shouldn't be reached, send some extra
192 logs to notify the administrator */
193 print_red (_("You are not allowed to delete this object group!"));
194 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
195 }
196 /* Remove lock file after successfull deletion */
197 del_lock ($dn);
198 unset($this->dns[$key]);
199 }
200 }
203 /********************
204 Delete MULTIPLE entries Canceled
205 ********************/
207 /* Remove lock */
208 if(isset($_POST['delete_multiple_ogroup_cancel'])){
209 foreach($this->dns as $key => $dn){
210 del_lock ($dn);
211 unset($this->dns[$key]);
212 }
213 }
216 /****************
217 Delete object group
218 ****************/
220 if ($s_action=="del"){
222 /* Get 'dn' from posted 'uid' */
223 $this->dn= $this->ogrouplist[$s_entry]['dn'];
225 /* Load permissions for selected 'dn' and check if
226 we're allowed to remove this 'dn' */
227 $acl = $this->ui->get_permissions($this->dn,"ogroups");
228 if(preg_match("/d/",$acl)){
230 /* Check locking, save current plugin in 'back_plugin', so
231 the dialog knows where to return. */
232 if (($user= get_lock($this->dn)) != ""){
233 return(gen_locked_message ($user, $this->dn));
234 }
236 /* Lock the current entry, so nobody will edit it during deletion */
237 add_lock ($this->dn, $this->ui->dn);
238 $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
239 $smarty->assign("multiple", false);
240 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
241 } else {
243 /* Obviously the user isn't allowed to delete. Show message and
244 clean session. */
245 print_red (_("You are not allowed to delete this object group!"));
246 }
247 }
250 /****************
251 Delete confirmed
252 ****************/
254 /* Confirmation for deletion has been passed. Group should be deleted. */
255 if (isset($_POST['delete_group_confirm'])){
257 /* Some nice guy may send this as POST, so we've to check
258 for the permissions again. */
259 $acl = $this->ui->get_permissions($this->dn,"ogroups");
260 if(preg_match("/d/",$acl)){
262 /* Delete request is permitted, perform LDAP action */
263 $this->ogroup= new ogrouptabs($this->config,
264 $this->config->data['TABS']['OGROUPTABS'], $this->dn);
265 $this->ogroup->delete ();
266 unset ($this->ogroup);
267 $this->ogroup= NULL;
268 } else {
270 /* Normally this shouldn't be reached, send some extra
271 logs to notify the administrator */
272 print_red (_("You are not allowed to delete this object group!"));
273 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
274 }
276 /* Remove lock file after successfull deletion */
277 del_lock ($this->dn);
278 unset($_SESSION['objectinfo']);
279 }
282 /****************
283 Delete object group Canceled
284 ****************/
286 /* Delete group canceled? */
287 if (isset($_POST['delete_cancel'])){
288 del_lock ($this->dn);
289 unset($_SESSION['objectinfo']);
290 }
293 /****************
294 Edit group
295 ****************/
297 if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
299 /* Get 'dn' from posted 'uid', must be unique */
300 $this->dn= $this->ogrouplist[$s_entry]['dn'];
302 /* Check locking, save current plugin in 'back_plugin', so
303 the dialog knows where to return. */
304 if (($user= get_lock($this->dn)) != ""){
305 return(gen_locked_message ($user, $this->dn));
306 }
308 /* Lock the current entry, so everyone will get the
309 above dialog */
310 add_lock ($this->dn, $this->ui->dn);
312 /* Register grouptab to trigger edit dialog */
313 $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
314 $this->ogroup->set_acl_base($this->dn);
315 $_SESSION['objectinfo']= $this->dn;
316 }
319 /****************
320 Edit finished save
321 ****************/
323 /* Finish button has been pressed */
324 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
326 /* Check tabs, will feed message array */
327 $message= $this->ogroup->check();
329 /* Save, or display error message? */
330 if (count($message) == 0){
332 /* Save user data to ldap */
333 $this->ogroup->save();
335 if (!isset($_POST['edit_apply'])){
336 /* Group has been saved successfully, remove lock from
337 LDAP. */
338 if ($this->dn != "new"){
339 del_lock ($this->dn);
340 }
342 unset ($this->ogroup);
343 $this->ogroup= NULL;
344 unset ($_SESSION['objectinfo']);
345 }
346 } else {
347 /* Ok. There seem to be errors regarding to the tab data,
348 show message and continue as usual. */
349 show_errors($message);
350 }
351 }
354 /****************
355 Cancel edit object group
356 ****************/
358 /* Cancel dialogs */
359 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
360 del_lock ($this->ogroup->dn);
361 unset ($this->ogroup);
362 $this->ogroup= NULL;
363 unset($_SESSION['objectinfo']);
364 }
367 /****************
368 Display open dialogs
369 ****************/
371 /* Show dialog if object is present */
372 if (isset($this->ogroup->config)){
373 $display= $this->ogroup->execute();
375 /* Don't show buttons if tab dialog requests this */
376 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
377 $display.= "<p style=\"text-align:right\">\n";
378 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
379 $display.= " \n";
380 if ($this->dn != "new"){
381 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
382 $display.= " \n";
383 }
384 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
385 $display.= "</p>";
386 }
387 return ($display);
388 }
391 /****************
392 Display list
393 ****************/
395 /* Check if there is a snapshot dialog open */
396 $base = $this->DivListOGroup->selectedBase;
397 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
398 return($str);
399 }
401 /* Display dialog with group list */
402 $this->DivListOGroup->parent = $this;
403 $this->DivListOGroup->execute();
405 /* Add departments if subsearch is disabled */
406 if(!$this->DivListOGroup->SubSearch){
407 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
408 }
409 $this->reload ();
410 $this->DivListOGroup->setEntries($this->ogrouplist);
411 return($this->DivListOGroup->Draw());
412 }
415 /* Return departments, that will be included within snapshot detection */
416 function get_used_snapshot_bases()
417 {
418 return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
419 }
423 function convert_list($input)
424 {
425 $temp= "";
427 $conv= array(
428 "U" => array("select_user.png" ,_("User") , "ogroup"),
429 "G" => array("select_groups.png" ,_("Group") , "ogroup"),
430 "A" => array("select_application.png" ,_("Application") , "ogroup"),
431 "D" => array("select_department.png" ,_("Department") , "ogroup"),
432 "S" => array("select_server.png" ,_("Server") , "ogroup"),
433 "F" => array("select_phone.png" ,_("Phone") , "phonequeue"),
434 "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
435 "T" => array("select_terminal.png" ,_("Terminal") , "termgroup"),
436 "P" => array("select_printer.png" ,_("Printer") , "ogroup"));
438 /* Assemble picture */
439 $type= $input['gosaGroupObjects'][0];
440 $type= preg_replace("/[^A-Z]/i","",$type);
441 if (isset($type[0])){
442 $p1['pic']= $conv[$type[0]][0];
443 $p1['tit']= $conv[$type[0]][1];
444 $p1['alt']= $type[0];
445 $p1['lnk']= $conv[$type[0]][2];
446 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
447 } else {
448 $p1['pic']= "empty.png";
449 $p1['tit']= "";
450 $p1['alt']= "";
451 $p1['lnk']= "";//$conv[$type[0]][2];
452 $html_object_1 = "<img ";
453 }
455 if (isset($type[1])){
456 $p2['pic']= $conv[$type[1]][0];
457 $p2['alt']= $type[1];
458 $p2['tit']= $conv[$type[1]][1];
459 $p2['lnk']= $conv[$type[1]][2];
460 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
461 } else {
462 $p2['pic']= "empty.png";
463 $p2['alt']= "";
464 $p2['tit']= "";
465 $p2['lnk']= ""; //$conv[$type[0]][2];
466 $html_object_2 = "<img ";
467 }
469 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
470 src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
471 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
472 src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
473 return ($temp);
474 }
477 function reload()
478 {
479 /* Set base for all searches && initialise some vars */
480 $this->ogrouplist= array();
481 $base = $this->DivListOGroup->selectedBase;
482 $filter = "(gosaGroupObjects=[])";
483 $Regex = $this->DivListOGroup->Regex;
485 $chk = array(
486 "UserGroups" => "(gosaGroupObjects=*U*)" ,
487 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
488 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
489 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
490 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
491 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
492 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
493 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
494 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
495 /* Create filter */
496 foreach($chk as $chkBox => $FilterPart){
497 if($this->DivListOGroup->$chkBox){
498 $filter .= $FilterPart;
499 }
500 }
501 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
503 if($this->DivListOGroup->SubSearch){
504 $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
505 }else{
506 $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
507 }
509 $this->ogrouplist= $res;
510 ksort ($this->ogrouplist);
511 reset ($this->ogrouplist);
512 $tmp=array();
513 foreach($this->ogrouplist as $tkey => $val ){
514 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
515 }
516 ksort($tmp);
517 $this->ogrouplist=array();
518 foreach($tmp as $val){
519 $this->ogrouplist[]=$val;
520 }
521 reset ($this->ogrouplist);
522 }
525 function list_get_selected_items()
526 {
527 $ids = array();
528 foreach($_POST as $name => $value){
529 if(preg_match("/^item_selected_[0-9]*$/",$name)){
530 $id = preg_replace("/^item_selected_/","",$name);
531 $ids[$id] = $id;
532 }
533 }
534 return($ids);
535 }
538 function copyPasteHandling_from_queue($s_action,$s_entry)
539 {
540 /* Check if Copy & Paste is disabled */
541 if(!is_object($this->CopyPasteHandler)){
542 return("");
543 }
545 /* Add a single entry to queue */
546 if($s_action == "cut" || $s_action == "copy"){
548 /* Cleanup object queue */
549 $this->CopyPasteHandler->cleanup_queue();
550 $dn = $this->ogrouplist[$s_entry]['dn'];
551 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"ogrouptabs","OGROUPTABS","ogroups");
552 }
555 /* Add entries to queue */
556 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
558 /* Cleanup object queue */
559 $this->CopyPasteHandler->cleanup_queue();
561 /* Add new entries to CP queue */
562 foreach($this->list_get_selected_items() as $id){
563 $dn = $this->ogrouplist[$id]['dn'];
565 if($s_action == "copy_multiple"){
566 $this->CopyPasteHandler->add_to_queue($dn,"copy","ogrouptabs","OGROUPTABS","ogroups");
567 }
568 if($s_action == "cut_multiple"){
569 $this->CopyPasteHandler->add_to_queue($dn,"cut","ogrouptabs","OGROUPTABS","ogroups");
570 }
571 }
572 }
574 /* Start pasting entries */
575 if($s_action == "editPaste"){
576 $this->start_pasting_copied_objects = TRUE;
577 }
579 /* Return C&P dialog */
580 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
582 /* Load entry from queue and set base */
583 $this->CopyPasteHandler->load_entry_from_queue();
584 $this->CopyPasteHandler->SetVar("base",$this->DivListOGroup->selectedBase);
586 /* Get dialog */
587 $data = $this->CopyPasteHandler->execute();
589 /* Return dialog data */
590 if(!empty($data)){
591 return($data);
592 }
593 }
594 /* Automatically disable status for pasting */
595 if(!$this->CopyPasteHandler->entries_queued()){
596 $this->start_pasting_copied_objects = FALSE;
597 }
598 return("");
599 }
602 function save_object()
603 {
604 $this->DivListOGroup->save_object();
605 }
607 }
609 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
610 ?>