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 unset ($this->ogroup);
182 $this->ogroup= NULL;
183 } else {
185 /* Normally this shouldn't be reached, send some extra
186 logs to notify the administrator */
187 print_red (_("You are not allowed to delete this object group!"));
188 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
189 }
190 /* Remove lock file after successfull deletion */
191 del_lock ($dn);
192 unset($this->dns[$key]);
193 }
194 }
197 /********************
198 Delete MULTIPLE entries Canceled
199 ********************/
201 /* Remove lock */
202 if(isset($_POST['delete_multiple_ogroup_cancel'])){
203 foreach($this->dns as $key => $dn){
204 del_lock ($dn);
205 unset($this->dns[$key]);
206 }
207 }
210 /****************
211 Delete object group
212 ****************/
214 if ($s_action=="del"){
216 /* Get 'dn' from posted 'uid' */
217 $this->dn= $this->ogrouplist[$s_entry]['dn'];
219 /* Load permissions for selected 'dn' and check if
220 we're allowed to remove this 'dn' */
221 $acl = $this->ui->get_permissions($this->dn,"ogroups");
222 if(preg_match("/d/",$acl)){
224 /* Check locking, save current plugin in 'back_plugin', so
225 the dialog knows where to return. */
226 if (($user= get_lock($this->dn)) != ""){
227 return(gen_locked_message ($user, $this->dn));
228 }
230 /* Lock the current entry, so nobody will edit it during deletion */
231 add_lock ($this->dn, $this->ui->dn);
232 $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
233 $smarty->assign("multiple", false);
234 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
235 } else {
237 /* Obviously the user isn't allowed to delete. Show message and
238 clean session. */
239 print_red (_("You are not allowed to delete this object group!"));
240 }
241 }
244 /****************
245 Delete confirmed
246 ****************/
248 /* Confirmation for deletion has been passed. Group should be deleted. */
249 if (isset($_POST['delete_group_confirm'])){
251 /* Some nice guy may send this as POST, so we've to check
252 for the permissions again. */
253 $acl = $this->ui->get_permissions($this->dn,"ogroups");
254 if(preg_match("/d/",$acl)){
256 /* Delete request is permitted, perform LDAP action */
257 $this->ogroup= new ogrouptabs($this->config,
258 $this->config->data['TABS']['OGROUPTABS'], $this->dn);
259 $this->ogroup->delete ();
260 unset ($this->ogroup);
261 $this->ogroup= NULL;
262 } else {
264 /* Normally this shouldn't be reached, send some extra
265 logs to notify the administrator */
266 print_red (_("You are not allowed to delete this object group!"));
267 new log("security","ogroups/".get_class($this),$dn,array(),"Tried to trick deletion.");
268 }
270 /* Remove lock file after successfull deletion */
271 del_lock ($this->dn);
272 unset($_SESSION['objectinfo']);
273 }
276 /****************
277 Delete object group Canceled
278 ****************/
280 /* Delete group canceled? */
281 if (isset($_POST['delete_cancel'])){
282 del_lock ($this->dn);
283 unset($_SESSION['objectinfo']);
284 }
287 /****************
288 Edit group
289 ****************/
291 if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
293 /* Get 'dn' from posted 'uid', must be unique */
294 $this->dn= $this->ogrouplist[$s_entry]['dn'];
296 /* Check locking, save current plugin in 'back_plugin', so
297 the dialog knows where to return. */
298 if (($user= get_lock($this->dn)) != ""){
299 return(gen_locked_message ($user, $this->dn));
300 }
302 /* Lock the current entry, so everyone will get the
303 above dialog */
304 add_lock ($this->dn, $this->ui->dn);
306 /* Register grouptab to trigger edit dialog */
307 $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
308 $this->ogroup->set_acl_base($this->dn);
309 $_SESSION['objectinfo']= $this->dn;
310 }
313 /****************
314 Edit finished save
315 ****************/
317 /* Finish button has been pressed */
318 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
320 /* Check tabs, will feed message array */
321 $message= $this->ogroup->check();
323 /* Save, or display error message? */
324 if (count($message) == 0){
326 /* Save user data to ldap */
327 $this->ogroup->save();
329 if (!isset($_POST['edit_apply'])){
330 /* Group has been saved successfully, remove lock from
331 LDAP. */
332 if ($this->dn != "new"){
333 del_lock ($this->dn);
334 }
336 unset ($this->ogroup);
337 $this->ogroup= NULL;
338 unset ($_SESSION['objectinfo']);
339 }
340 } else {
341 /* Ok. There seem to be errors regarding to the tab data,
342 show message and continue as usual. */
343 show_errors($message);
344 }
345 }
348 /****************
349 Cancel edit object group
350 ****************/
352 /* Cancel dialogs */
353 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
354 del_lock ($this->ogroup->dn);
355 unset ($this->ogroup);
356 $this->ogroup= NULL;
357 unset($_SESSION['objectinfo']);
358 }
361 /****************
362 Display open dialogs
363 ****************/
365 /* Show dialog if object is present */
366 if (isset($this->ogroup->config)){
367 $display= $this->ogroup->execute();
369 /* Don't show buttons if tab dialog requests this */
370 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
371 $display.= "<p style=\"text-align:right\">\n";
372 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
373 $display.= " \n";
374 if ($this->dn != "new"){
375 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
376 $display.= " \n";
377 }
378 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
379 $display.= "</p>";
380 }
381 return ($display);
382 }
385 /****************
386 Display list
387 ****************/
389 /* Check if there is a snapshot dialog open */
390 $base = $this->DivListOGroup->selectedBase;
391 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
392 return($str);
393 }
395 /* Display dialog with group list */
396 $this->DivListOGroup->parent = $this;
397 $this->DivListOGroup->execute();
399 /* Add departments if subsearch is disabled */
400 if(!$this->DivListOGroup->SubSearch){
401 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
402 }
403 $this->reload ();
404 $this->DivListOGroup->setEntries($this->ogrouplist);
405 return($this->DivListOGroup->Draw());
406 }
409 /* Return departments, that will be included within snapshot detection */
410 function get_used_snapshot_bases()
411 {
412 return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
413 }
417 function convert_list($input)
418 {
419 $temp= "";
421 $conv= array(
422 "U" => array("select_user.png" ,_("User") , "ogroup"),
423 "G" => array("select_groups.png" ,_("Group") , "ogroup"),
424 "A" => array("select_application.png" ,_("Application") , "ogroup"),
425 "D" => array("select_department.png" ,_("Department") , "ogroup"),
426 "S" => array("select_server.png" ,_("Server") , "ogroup"),
427 "F" => array("select_phone.png" ,_("Phone") , "phonequeue"),
428 "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
429 "T" => array("select_terminal.png" ,_("Terminal") , "termgroup"),
430 "P" => array("select_printer.png" ,_("Printer") , "ogroup"));
432 /* Assemble picture */
433 $type= $input['gosaGroupObjects'][0];
434 $type= preg_replace("/[^A-Z]/i","",$type);
435 if (isset($type[0])){
436 $p1['pic']= $conv[$type[0]][0];
437 $p1['tit']= $conv[$type[0]][1];
438 $p1['alt']= $type[0];
439 $p1['lnk']= $conv[$type[0]][2];
440 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
441 } else {
442 $p1['pic']= "empty.png";
443 $p1['tit']= "";
444 $p1['alt']= "";
445 $p1['lnk']= "";//$conv[$type[0]][2];
446 $html_object_1 = "<img ";
447 }
449 if (isset($type[1])){
450 $p2['pic']= $conv[$type[1]][0];
451 $p2['alt']= $type[1];
452 $p2['tit']= $conv[$type[1]][1];
453 $p2['lnk']= $conv[$type[1]][2];
454 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
455 } else {
456 $p2['pic']= "empty.png";
457 $p2['alt']= "";
458 $p2['tit']= "";
459 $p2['lnk']= ""; //$conv[$type[0]][2];
460 $html_object_2 = "<img ";
461 }
463 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
464 src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
465 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
466 src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
467 return ($temp);
468 }
471 function reload()
472 {
473 /* Set base for all searches && initialise some vars */
474 $this->ogrouplist= array();
475 $base = $this->DivListOGroup->selectedBase;
476 $filter = "(gosaGroupObjects=[])";
477 $Regex = $this->DivListOGroup->Regex;
479 $chk = array(
480 "UserGroups" => "(gosaGroupObjects=*U*)" ,
481 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
482 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
483 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
484 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
485 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
486 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
487 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
488 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
489 /* Create filter */
490 foreach($chk as $chkBox => $FilterPart){
491 if($this->DivListOGroup->$chkBox){
492 $filter .= $FilterPart;
493 }
494 }
495 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
497 if($this->DivListOGroup->SubSearch){
498 $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
499 }else{
500 $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
501 }
503 $this->ogrouplist= $res;
504 ksort ($this->ogrouplist);
505 reset ($this->ogrouplist);
506 $tmp=array();
507 foreach($this->ogrouplist as $tkey => $val ){
508 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
509 }
510 ksort($tmp);
511 $this->ogrouplist=array();
512 foreach($tmp as $val){
513 $this->ogrouplist[]=$val;
514 }
515 reset ($this->ogrouplist);
516 }
519 function list_get_selected_items()
520 {
521 $ids = array();
522 foreach($_POST as $name => $value){
523 if(preg_match("/^item_selected_[0-9]*$/",$name)){
524 $id = preg_replace("/^item_selected_/","",$name);
525 $ids[$id] = $id;
526 }
527 }
528 return($ids);
529 }
532 function copyPasteHandling($s_action,$s_entry)
533 {
534 if($this->CopyPasteHandler){
536 /* Paste copied/cutted object in here
537 */
538 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
539 $this->CopyPasteHandler->save_object();
540 $this->CopyPasteHandler->SetVar("base", $this->DivListOGroup->selectedBase);
541 return($this->CopyPasteHandler->execute());
542 }
544 /* Copy current object to CopyHandler
545 */
546 if($s_action == "copy"){
549 $this->CopyPasteHandler->Clear();
550 $dn = $this->ogrouplist[$s_entry]['dn'];
552 /* Check acls */
553 $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
554 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
555 $obj = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
556 $objNew = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], "new");
558 $types_of_tabs = $obj->by_object['ogroup']->gosaGroupObjects;
559 $objNew->by_object['ogroup']->gosaGroupObjects = $types_of_tabs;
560 $objNew->reload($types_of_tabs);
562 $this->CopyPasteHandler->Copy($obj,$objNew);
563 }
564 }
566 /* Copy current object to CopyHandler
567 */
568 if($s_action == "cut"){
569 $this->CopyPasteHandler->Clear();
570 $dn = $this->ogrouplist[$s_entry]['dn'];
572 /* Check acls */
573 $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
574 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
575 $obj = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
576 $this->CopyPasteHandler->Cut($obj);
577 }
578 }
579 }
580 }
583 function save_object()
584 {
585 $this->DivListOGroup->save_object();
586 }
588 }
590 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
591 ?>