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 $smarty->assign("info", sprintf(_("You're about to delete the following object group(s) %s"), @LDAP::fix($dns_names)));
155 $smarty->assign("multiple", true);
156 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
157 }
158 }
161 /********************
162 Delete MULTIPLE entries confirmed
163 ********************/
165 /* Confirmation for deletion has been passed. Users should be deleted. */
166 if (isset($_POST['delete_multiple_ogroup_confirm'])){
168 /* Remove user by user and check acls before removeing them */
169 foreach($this->dns as $key => $dn){
171 $acl = $this->ui->get_permissions($dn, "ogroups");
172 if (preg_match('/d/', $acl)){
174 /* Delete request is permitted, perform LDAP action */
175 $this->ogroup= new ogrouptabs($this->config,$this->config->data['TABS']['OGROUPTABS'], $dn);
176 $this->ogroup->delete ();
177 gosa_log ("Object group'".$dn."' has been removed");
178 unset ($this->ogroup);
179 $this->ogroup= NULL;
180 } else {
182 /* Normally this shouldn't be reached, send some extra
183 logs to notify the administrator */
184 print_red (_("You are not allowed to delete this object group!"));
185 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
186 }
187 /* Remove lock file after successfull deletion */
188 del_lock ($dn);
189 unset($this->dns[$key]);
190 }
191 }
194 /********************
195 Delete MULTIPLE entries Canceled
196 ********************/
198 /* Remove lock */
199 if(isset($_POST['delete_multiple_ogroup_cancel'])){
200 foreach($this->dns as $key => $dn){
201 del_lock ($dn);
202 unset($this->dns[$key]);
203 }
204 }
207 /****************
208 Delete object group
209 ****************/
211 if ($s_action=="del"){
213 /* Get 'dn' from posted 'uid' */
214 $this->dn= $this->ogrouplist[$s_entry]['dn'];
216 /* Load permissions for selected 'dn' and check if
217 we're allowed to remove this 'dn' */
218 $acl = $this->ui->get_permissions($this->dn,"ogroups");
219 if(preg_match("/d/",$acl)){
221 /* Check locking, save current plugin in 'back_plugin', so
222 the dialog knows where to return. */
223 if (($user= get_lock($this->dn)) != ""){
224 return(gen_locked_message ($user, $this->dn));
225 }
227 /* Lock the current entry, so nobody will edit it during deletion */
228 add_lock ($this->dn, $this->ui->dn);
229 $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), @LDAP::fix($this->dn)));
230 $smarty->assign("multiple", false);
231 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
232 } else {
234 /* Obviously the user isn't allowed to delete. Show message and
235 clean session. */
236 print_red (_("You are not allowed to delete this object group!"));
237 }
238 }
241 /****************
242 Delete confirmed
243 ****************/
245 /* Confirmation for deletion has been passed. Group should be deleted. */
246 if (isset($_POST['delete_group_confirm'])){
248 /* Some nice guy may send this as POST, so we've to check
249 for the permissions again. */
250 $acl = $this->ui->get_permissions($this->dn,"ogroups");
251 if(preg_match("/d/",$acl)){
253 /* Delete request is permitted, perform LDAP action */
254 $this->ogroup= new ogrouptabs($this->config,
255 $this->config->data['TABS']['OGROUPTABS'], $this->dn);
256 $this->ogroup->delete ();
257 gosa_log ("Object group'".$this->dn."' has been removed");
258 unset ($this->ogroup);
259 $this->ogroup= NULL;
260 } else {
262 /* Normally this shouldn't be reached, send some extra
263 logs to notify the administrator */
264 print_red (_("You are not allowed to delete this object group!"));
265 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
266 }
268 /* Remove lock file after successfull deletion */
269 del_lock ($this->dn);
270 unset($_SESSION['objectinfo']);
271 }
274 /****************
275 Delete object group Canceled
276 ****************/
278 /* Delete group canceled? */
279 if (isset($_POST['delete_cancel'])){
280 del_lock ($this->dn);
281 unset($_SESSION['objectinfo']);
282 }
285 /****************
286 Edit group
287 ****************/
289 if (($s_action=="edit") && (! isset($this->ogroup->config)) ){
291 /* Get 'dn' from posted 'uid', must be unique */
292 $this->dn= $this->ogrouplist[$s_entry]['dn'];
294 /* Check locking, save current plugin in 'back_plugin', so
295 the dialog knows where to return. */
296 if (($user= get_lock($this->dn)) != ""){
297 return(gen_locked_message ($user, $this->dn));
298 }
300 /* Lock the current entry, so everyone will get the
301 above dialog */
302 add_lock ($this->dn, $this->ui->dn);
304 /* Register grouptab to trigger edit dialog */
305 $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $this->dn, "ogroups");
306 $this->ogroup->set_acl_base($this->dn);
307 $_SESSION['objectinfo']= $this->dn;
308 }
311 /****************
312 Edit finished save
313 ****************/
315 /* Finish button has been pressed */
316 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->ogroup->config)) ){
318 /* Check tabs, will feed message array */
319 $message= $this->ogroup->check();
321 /* Save, or display error message? */
322 if (count($message) == 0){
324 /* Save user data to ldap */
325 $this->ogroup->save();
326 gosa_log ("Object group'".$this->dn."' has been saved");
328 if (!isset($_POST['edit_apply'])){
329 /* Group has been saved successfully, remove lock from
330 LDAP. */
331 if ($this->dn != "new"){
332 del_lock ($this->dn);
333 }
335 unset ($this->ogroup);
336 $this->ogroup= NULL;
337 unset ($_SESSION['objectinfo']);
338 }
339 } else {
340 /* Ok. There seem to be errors regarding to the tab data,
341 show message and continue as usual. */
342 show_errors($message);
343 }
344 }
347 /****************
348 Cancel edit object group
349 ****************/
351 /* Cancel dialogs */
352 if ((isset($_POST['edit_cancel'])) && (isset($this->ogroup->dn))){
353 del_lock ($this->ogroup->dn);
354 unset ($this->ogroup);
355 $this->ogroup= NULL;
356 unset($_SESSION['objectinfo']);
357 }
360 /****************
361 Display open dialogs
362 ****************/
364 /* Show dialog if object is present */
365 if (isset($this->ogroup->config)){
366 $display= $this->ogroup->execute();
368 /* Don't show buttons if tab dialog requests this */
369 if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
370 $display.= "<p style=\"text-align:right\">\n";
371 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
372 $display.= " \n";
373 if ($this->dn != "new"){
374 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
375 $display.= " \n";
376 }
377 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
378 $display.= "</p>";
379 }
380 return ($display);
381 }
384 /****************
385 Display list
386 ****************/
388 /* Check if there is a snapshot dialog open */
389 $base = $this->DivListOGroup->selectedBase;
390 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
391 return($str);
392 }
394 /* Display dialog with group list */
395 $this->DivListOGroup->parent = $this;
396 $this->DivListOGroup->execute();
398 /* Add departments if subsearch is disabled */
399 if(!$this->DivListOGroup->SubSearch){
400 $this->DivListOGroup->AddDepartments($this->DivListOGroup->selectedBase,4,1);
401 }
402 $this->reload ();
403 $this->DivListOGroup->setEntries($this->ogrouplist);
404 return($this->DivListOGroup->Draw());
405 }
408 /* Return departments, that will be included within snapshot detection */
409 function get_used_snapshot_bases()
410 {
411 return(array(get_groups_ou().$this->DivListOGroup->selectedBase));
412 }
416 function convert_list($input)
417 {
418 $temp= "";
420 $conv= array(
421 "U" => array("select_user.png" ,_("User") , "ogroup"),
422 "G" => array("select_groups.png" ,_("Group") , "ogroup"),
423 "A" => array("select_application.png" ,_("Application") , "ogroup"),
424 "D" => array("select_department.png" ,_("Department") , "ogroup"),
425 "S" => array("select_server.png" ,_("Server") , "ogroup"),
426 "F" => array("select_phone.png" ,_("Phone") , "phonequeue"),
427 "W" => array("select_workstation.png" ,_("Workstation") , "workstartup"),
428 "T" => array("select_terminal.png" ,_("Terminal") , "termgroup"),
429 "P" => array("select_printer.png" ,_("Printer") , "ogroup"));
431 /* Assemble picture */
432 $type= $input['gosaGroupObjects'][0];
433 $type= preg_replace("/[^A-Z]/i","",$type);
434 if (isset($type[0])){
435 $p1['pic']= $conv[$type[0]][0];
436 $p1['tit']= $conv[$type[0]][1];
437 $p1['alt']= $type[0];
438 $p1['lnk']= $conv[$type[0]][2];
439 $html_object_1 = "<input type='image' name='".$p1['lnk']."_group_edit_%KEY' ";
440 } else {
441 $p1['pic']= "empty.png";
442 $p1['tit']= "";
443 $p1['alt']= "";
444 $p1['lnk']= "";//$conv[$type[0]][2];
445 $html_object_1 = "<img ";
446 }
448 if (isset($type[1])){
449 $p2['pic']= $conv[$type[1]][0];
450 $p2['alt']= $type[1];
451 $p2['tit']= $conv[$type[1]][1];
452 $p2['lnk']= $conv[$type[1]][2];
453 $html_object_2 = "<input type='image' name='".$p2['lnk']."_group_edit_%KEY' ";
454 } else {
455 $p2['pic']= "empty.png";
456 $p2['alt']= "";
457 $p2['tit']= "";
458 $p2['lnk']= ""; //$conv[$type[0]][2];
459 $html_object_2 = "<img ";
460 }
462 $temp = $html_object_1." class='center' id='iconA".preg_replace("/_/","",$p1['lnk']."_group_edit_%KEY")."'
463 src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
464 $temp.= $html_object_2." class='center' id='iconB".preg_replace("/_/","",$p2['lnk']."_group_edit_%KEY")."'
465 src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
466 return ($temp);
467 }
470 function reload()
471 {
472 /* Set base for all searches && initialise some vars */
473 $this->ogrouplist= array();
474 $base = $this->DivListOGroup->selectedBase;
475 $filter = "(gosaGroupObjects=[])";
476 $Regex = $this->DivListOGroup->Regex;
478 $chk = array(
479 "UserGroups" => "(gosaGroupObjects=*U*)" ,
480 "GroupGroups" => "(gosaGroupObjects=*G*)" ,
481 "ApplicationGroups" => "(gosaGroupObjects=*A*)" ,
482 "DepartmentGroups" => "(gosaGroupObjects=*D*)" ,
483 "ServerGroups" => "(gosaGroupObjects=*S*)" ,
484 "WorkstationGroups" => "(gosaGroupObjects=*W*)" ,
485 "TerminalGroups" => "(gosaGroupObjects=*T*)" ,
486 "PrinterGroups" => "(gosaGroupObjects=*P*)" ,
487 "PhoneGroups" => "(gosaGroupObjects=*F*)" );
488 /* Create filter */
489 foreach($chk as $chkBox => $FilterPart){
490 if($this->DivListOGroup->$chkBox){
491 $filter .= $FilterPart;
492 }
493 }
494 $filter= "(&(cn=$Regex)(objectClass=gosaGroupOfNames)(|$filter))";
496 if($this->DivListOGroup->SubSearch){
497 $res= get_list($filter, "ogroups", $base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT | GL_SUBSEARCH);
498 }else{
499 $res= get_list($filter, "ogroups", get_groups_ou().$base, array("cn","objectClass","gosaGroupObjects"), GL_SIZELIMIT );
500 }
502 $this->ogrouplist= $res;
503 ksort ($this->ogrouplist);
504 reset ($this->ogrouplist);
505 $tmp=array();
506 foreach($this->ogrouplist as $tkey => $val ){
507 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
508 }
509 ksort($tmp);
510 $this->ogrouplist=array();
511 foreach($tmp as $val){
512 $this->ogrouplist[]=$val;
513 }
514 reset ($this->ogrouplist);
515 }
518 function list_get_selected_items()
519 {
520 $ids = array();
521 foreach($_POST as $name => $value){
522 if(preg_match("/^item_selected_[0-9]*$/",$name)){
523 $id = preg_replace("/^item_selected_/","",$name);
524 $ids[$id] = $id;
525 }
526 }
527 return($ids);
528 }
531 function copyPasteHandling($s_action,$s_entry)
532 {
533 if($this->CopyPasteHandler){
535 /* Paste copied/cutted object in here
536 */
537 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
538 $this->CopyPasteHandler->save_object();
539 $this->CopyPasteHandler->SetVar("base", $this->DivListOGroup->selectedBase);
540 return($this->CopyPasteHandler->execute());
541 }
543 /* Copy current object to CopyHandler
544 */
545 if($s_action == "copy"){
548 $this->CopyPasteHandler->Clear();
549 $dn = $this->ogrouplist[$s_entry]['dn'];
551 /* Check acls */
552 $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
553 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
554 $obj = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
555 $objNew = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], "new");
557 $types_of_tabs = $obj->by_object['ogroup']->gosaGroupObjects;
558 $objNew->by_object['ogroup']->gosaGroupObjects = $types_of_tabs;
559 $objNew->reload($types_of_tabs);
561 $this->CopyPasteHandler->Copy($obj,$objNew);
562 }
563 }
565 /* Copy current object to CopyHandler
566 */
567 if($s_action == "cut"){
568 $this->CopyPasteHandler->Clear();
569 $dn = $this->ogrouplist[$s_entry]['dn'];
571 /* Check acls */
572 $acl_all= $this->ui->has_complete_category_acls($dn,"ogroups");
573 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
574 $obj = new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'], $dn);
575 $this->CopyPasteHandler->Cut($obj);
576 }
577 }
578 }
579 }
582 function save_object()
583 {
584 $this->DivListOGroup->save_object();
585 }
587 }
589 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
590 ?>