1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 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_department.inc";
22 class departmentManagement extends plugin
23 {
24 /* Definitions */
25 var $plHeadline= "Departments";
26 var $plDescription= "This does something";
28 /* CLI vars */
29 var $cli_summary= "Handling of LDAP subtrees";
30 var $cli_description= "Some longer text\nfor help";
31 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
33 /* Headpage attributes */
34 var $last_dep_sorting= "invalid";
35 var $departments= array();
36 var $deptabs= NULL;
38 /* attribute list for save action */
39 var $attributes= array();
40 var $objectclasses= array();
42 /* Vars to handle operations after saving the department
43 Recursive move && tagging */
44 var $ObjectInSaveMode = false; // Is true, if current object wasn't saved right now
45 var $ObjectTaggingRequested = false; // Object must be tagged, an iframe will be shown.
46 var $RecursiveRemoveRequested = false; // Is true, if this object must be moved, an iframe will be displayed in this case
48 function departmentManagement ($config, $ui)
49 {
50 $this->ui= $ui;
51 $this->dn= "";
52 $this->config= $config;
53 $this->DivListDepartment = new divListDepartment($this->config,$this);
54 }
56 function execute()
57 {
58 global $config;
60 /* Call parent execute */
61 plugin::execute();
63 /***************
64 Var init
65 ***************/
67 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/","/^item_selected/","/^remove_multiple_departments/");
69 /* Reload departments */
70 $smarty = get_smarty();
71 $display = "";
72 $s_action = ""; // Will contain an action, like del or edit
73 $s_entry = ""; // The entry name for edit delete -...
76 /***************
77 Check posts
78 ***************/
80 // Check Post action
81 foreach($_POST as $key => $val){
82 // Post for delete
83 if(preg_match("/dep_del.*/",$key)){
84 $s_action = "del";
85 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
86 $s_entry = preg_replace("/_.*$/","",$s_entry);
87 $s_entry = base64_decode($s_entry);
88 // Post for edit
89 }elseif(preg_match("/dep_edit_.*/",$key)){
90 $s_action="edit";
91 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
92 $s_entry = preg_replace("/_.*$/","",$s_entry);
93 $s_entry = base64_decode($s_entry);
94 // Post for new
95 }elseif(preg_match("/^remove_multiple_departments/",$key)){
96 $s_action="del_multiple";
97 }elseif(preg_match("/dep_new.*/",$key)){
98 $s_action="new";
99 }
100 }
103 /***************
104 Create a new department
105 ***************/
107 /* New Entry if Posted action (s_action) == new
108 */
109 if ($s_action=="new"){
110 $this->dn= "new";
111 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
112 $this->deptabs->set_acl_base($this->DivListDepartment->selectedBase);
113 }
116 /***************
117 Edit entry
118 ***************/
120 /* Edit Entry if Posted action (s_action) == edit
121 * The entry which will be edited is defined in $s_entry
122 */
123 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
124 $this->dn= $this->config->departments[trim($s_entry)];
126 if (($user= get_lock($this->dn)) != ""){
127 return(gen_locked_message ($user, $this->dn));
128 }
130 /* Lock the current entry, so everyone will get the above dialog */
131 add_lock ($this->dn, $this->ui->dn);
133 /* Register deptabs to trigger edit dialog */
134 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn,"department");
135 $this->deptabs->set_acl_base($this->dn);
137 $_SESSION['objectinfo']= $this->dn;
138 }
141 /********************
142 Delete MULTIPLE entries requested, display confirm dialog
143 ********************/
145 if ($s_action=="del_multiple"){
146 $ids = $this->list_get_selected_items();
149 if(count($ids)){
150 foreach($ids as $id){
151 $id = base64_decode($id);
152 $dn = $this->config->departments[$id];
154 if (($user= get_lock($dn)) != ""){
155 return(gen_locked_message ($user, $dn));
156 }
157 $this->dns[$id] = $dn;
158 }
160 $dns_names = "<br><pre>";
161 foreach($this->dns as $dn){
162 add_lock ($dn, $this->ui->dn);
163 $dns_names .= $dn."\n";
164 }
165 $dns_names .="</pre>";
167 /* Lock the current entry, so nobody will edit it during deletion */
168 $smarty->assign("info", sprintf(_("You're about to delete the following department(s) %s"), @LDAP::fix($dns_names)));
169 $smarty->assign("multiple", true);
170 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
171 }
172 }
175 /********************
176 Delete MULTIPLE entries confirmed
177 ********************/
179 /* Confirmation for deletion has been passed. Users should be deleted. */
180 if (isset($_POST['delete_multiple_department_confirm'])){
182 /* Remove user by user and check acls before removeing them */
183 foreach($this->dns as $key => $dn){
184 $acl = $this->ui->get_permissions($dn,"department/department");
185 if (preg_match('/d/', $acl)){
187 /* Delete request is permitted, perform LDAP action */
188 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $dn,"department");
189 $this->deptabs->set_acl_base();
190 $this->deptabs->delete ();
191 $this->deptabs = NULL;
192 gosa_log ("Department object'".$this->dn."' has been removed");
193 } else {
194 print_red (_("You have no permission to remove this department."));
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_department_cancel'])){
209 foreach($this->dns as $key => $dn){
210 del_lock ($dn);
211 unset($this->dns[$key]);
212 }
213 }
216 /***************
217 Delete entry
218 ***************/
220 /* Delete Entry if Posted action (s_action) == del
221 * The entry which will be deleted is defined in $s_entry
222 */
223 if ($s_action =="del"){
224 $this->dn= $this->config->departments[trim($s_entry)];
226 /* check acls */
227 $acl = $this->ui->get_permissions($this->dn,"department/department");
228 if(preg_match("/d/",$acl)){
230 /* Check locking */
231 if (($user= get_lock($this->dn)) != ""){
232 $_SESSION['dn']= $this->dn;
233 return(gen_locked_message($user, $this->dn));
234 } else {
235 add_lock ($this->dn, $this->ui->dn);
236 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), @LDAP::fix($this->dn)));
237 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
238 return ($display);
239 }
240 }else{
241 print_red (_("You have no permission to remove this department."));
242 }
243 }
246 /***************
247 Delete department confirmed
248 ***************/
250 /* If department deletion is accepted ...
251 * Finally delete department
252 */
253 if (isset($_POST['delete_department_confirm'])){
255 /* check acls */
256 $acl = $this->ui->get_permissions($this->dn,"department/department");
257 if(preg_match("/d/",$acl)){
258 $this->remove_from_parent();
259 gosa_log ("Department object'".$this->dn."' has been removed");
260 } else {
261 print_red (_("You have no permission to remove this department."));
262 }
263 }
266 /***************
267 Edit department finished
268 ***************/
270 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config)) && !isset($_POST['dep_move_confirm'])){
271 $this->deptabs->save_object();
272 $obj = $this->deptabs->by_object['department'];
273 if($obj->orig_dn != "new"){
274 if($obj->orig_ou != $obj->ou || $obj->base != $obj->orig_base){
275 return($smarty->fetch(get_template_path("dep_move_confirm.tpl",TRUE)));
276 }
277 }
278 }
280 /* Save changes */
281 if ((isset($_POST['edit_finish'])|| isset($_POST['dep_move_confirm'])) && (isset($this->deptabs->config))){
283 /* Check tabs, will feed message array */
284 $message= $this->deptabs->check();
286 /* Save, or display error message? */
287 if (count($message) == 0){
289 $this->deptabs->save(true);
290 $this->config->get_departments();
292 /* This object must be tagged, so set ObjectTaggingRequested to true */
293 if($this->deptabs->by_object['department']->must_be_tagged()){
294 $this->ObjectTaggingRequested = true;
295 }
297 /* Get recursive move is required, set RecursiveRemoveRequested to true */
298 if($this->deptabs->by_object['department']->am_i_moved()){
299 $this->RecursiveRemoveRequested = true;
300 }
302 /* This var indicated that there is an object which isn't saved right now. */
303 $this->ObjectInSaveMode = true;
305 } else {
306 /* Ok. There seem to be errors regarding to the tab data,
307 show message and continue as usual. */
308 show_errors($message);
309 }
310 }
313 /***************
314 Handle Tagging (Return output for an iframe)
315 ***************/
317 /* This department must be tagged (Is called from iframe, generates output)*/
318 if(isset($_GET['TagDepartment'])){
319 $this->deptabs->by_object['department']->tag_objects();
320 exit();
321 }
324 /***************
325 Handle recursive move (Return output for an iframe)
326 ***************/
328 /* initiate recursive remove (Is called from iframe, generates output)*/
329 if(isset($_GET['PerformRecMove'])){
330 $this->deptabs->by_object['department']->recursive_move("","",true);
331 $this->DivListDepartment->selectedBase = $this->deptabs->by_object['department']->dn;
332 exit();
333 }
336 /***************
337 Return iframes, which call tagging / recusrsive move
338 ***************/
340 /* While one of these vars below isset, we must return an iframe,
341 to perform requested operation */
342 if($this->ObjectTaggingRequested){
343 $this->ObjectTaggingRequested = false;
344 return($this->deptabs->by_object['department']->ShowTagFrame());
345 }
346 if($this->RecursiveRemoveRequested){
347 $this->RecursiveRemoveRequested = false;
348 return($this->deptabs->by_object['department']->ShowMoveFrame());
349 }
352 /***************
353 In case of tagging/moving the object wasn't deleted, do it know
354 ***************/
356 /* If there is an unsaved object and all operations are done
357 remove locks & save object tab & unset current object */
358 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
359 $this->deptabs->save();
360 $this->config->get_departments();
361 $this->ObjectInSaveMode = false;
362 if ($this->dn != "new"){
363 del_lock ($this->dn);
364 }
365 gosa_log ("Department object'".$this->dn."' has been saved");
366 unset ($this->deptabs);
367 $this->deptabs= NULL;
368 unset ($_SESSION['objectinfo']);
369 }
372 /***************
373 Dialog canceled
374 ***************/
376 /* User canceled edit oder delete
377 * Cancel dialog
378 */
379 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) || isset($_POST['delete_department_confirm'])){
380 del_lock ($this->dn);
381 unset($this->depdabs);
382 $this->deptabs= NULL;
383 unset ($_SESSION['objectinfo']);
384 }
386 /* Headpage or normal plugin screen? */
387 if ($this->deptabs != NULL){
389 /* Show main page (tabs) */
390 $display= $this->deptabs->execute();
391 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
392 $display.= "<p style=\"text-align:right\">\n";
393 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
394 $display.= " \n";
395 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
396 $display.= "</p>";
397 }
398 return ($display);
399 }else{
400 /* Display dialog with group list */
401 $this->DivListDepartment->parent = $this;
402 $this->DivListDepartment->execute();
403 $this->reload();
404 $this->DivListDepartment->DepartmentsAdded = true;
405 $this->DivListDepartment->setEntries($this->departments);
406 return($this->DivListDepartment->Draw());
407 }
408 }
411 function reload()
412 {
413 /* Vairaible init */
414 $base = $this->DivListDepartment->selectedBase;
415 $base_back = preg_replace("/^[^,]+,/","",$base);
416 $Regex = $this->DivListDepartment->Regex;
418 // Create Array to Test if we have a valid back button
419 $tmp = $_SESSION['config']->idepartments;
421 // In case of a valid back button create entry
422 if(isset($tmp[$base_back])){
423 $tmp2 ['dn'] = convert_department_dn($base_back);
425 // If empty always go to top
426 if(empty($tmp2['dn'])){
427 $tmp2['dn']="/";
428 }
429 $tmp2 ['description'][0] = _("..");
430 $result[$tmp[$base_back]]=$tmp2;
431 }
433 if($this->DivListDepartment->SubSearch){
434 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
435 "department", $base, array("ou", "description"), GL_SIZELIMIT | GL_SUBSEARCH);
436 }else{
437 $res= get_list("(&(|(ou=$Regex)(description=$Regex))(objectClass=gosaDepartment))",
438 "department", $base, array("ou", "description"), GL_SIZELIMIT );
439 }
441 $this->departments= array();
443 /* Add current base to the list of available departments, but only if its naming attribute is 'ou' */
444 if(preg_match("/^ou=/",$base)){
445 $this->departments [ convert_department_dn($base) ] = ".";
446 }
448 foreach ($res as $key => $value){
450 /* Don't display base as entry on subsearch */
451 if(($value['dn'] == $base) && ($this->DivListDepartment->SubSearch)){
452 continue;
453 }
455 $cdn= convert_department_dn($value['dn']);
457 /* Append to dep list */
458 if(isset($value["description"][0])){
459 $this->departments[$cdn]= get_sub_department($cdn)." - [".$value["description"][0]."]";
460 }else{
461 $this->departments[$cdn]= get_sub_department($cdn);//$value["description"][0];
462 }
463 }
464 natcasesort ($this->departments);
465 reset ($this->departments);
466 }
468 function remove_from_parent()
469 {
470 $ldap= $this->config->get_ldap_link();
471 $ldap->cd ($this->dn);
472 $ldap->recursive_remove();
474 /* Optionally execute a command after we're done */
475 $this->postremove();
477 /* Delete references to object groups */
478 $ldap->cd ($this->config->current['BASE']);
479 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
480 while ($ldap->fetch()){
481 $og= new ogroup($this->config, $ldap->getDN());
482 unset($og->member[$this->dn]);
483 $og->save ();
484 }
486 }
489 function list_get_selected_items()
490 {
491 $ids = array();
492 foreach($_POST as $name => $value){
493 if(preg_match("/^item_selected_[a-z0-9\=]*$/i",$name)){
494 $id = preg_replace("/^item_selected_/","",$name);
495 $ids[$id] = $id;
496 }
497 }
498 return($ids);
499 }
502 function remove_lock()
503 {
504 if (isset($this->dn)){
505 del_lock ($this->dn);
506 }
507 }
509 function save_object()
510 {
511 /* reload department */
512 $this->config->get_departments();
514 $this->config->make_idepartments();
515 $this->DivListDepartment->config= $this->config;
516 $this->DivListDepartment->save_object();
517 }
519 }
520 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
521 ?>