2fcbe8fdee774148b1fc1edbfb430f4d00d2b09c
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 var $ObjectInSaveMode = false;
43 var $RecursiveRemoveRequested = false;
44 var $ObjectTaggingRequested = false;
46 function departmentManagement ($config, $ui)
47 {
48 $this->ui= $ui;
49 $this->dn= "";
50 $this->config= $config;
52 /* Get global filter config */
53 if (!is_global("depfilter")){
54 $depfilter= array("regex" => "*");
55 register_global("depfilter", $depfilter);
56 }
57 $base= get_base_from_people($ui->dn);
58 if(!isset($_SESSION['CurrentMainBase'])){
59 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
60 }
61 $_SESSION['CurrentMainBase'] = $_SESSION['CurrentMainBase'];
62 }
64 function execute()
65 {
66 /* Call parent execute */
67 plugin::execute();
69 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
71 /* Reload departments */
72 $this->config->get_departments();
73 $this->config->make_idepartments();
74 $_SESSION['config']= $this->config;
76 $smarty = get_smarty();
77 $display = "";
78 $depfilter = get_global("depfilter");
79 $s_action = ""; // Will contain an action, like del or edit
80 $s_entry = ""; // The entry name for edit delete -...
82 if (isset($_POST['regex'])){
83 $depfilter['regex']= $_POST['regex'];
84 }
86 // Check Post action
87 foreach($_POST as $key => $val){
88 // Post for delete
89 if(preg_match("/dep_del.*/",$key)){
90 $s_action = "del";
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 edit
95 }elseif(preg_match("/dep_edit_.*/",$key)){
96 $s_action="edit";
97 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
98 $s_entry = preg_replace("/_.*$/","",$s_entry);
99 $s_entry = base64_decode($s_entry);
100 // Post for new
101 }elseif(preg_match("/dep_new.*/",$key)){
102 $s_action="new";
103 }elseif(preg_match("/dep_back.*/i",$key)){
104 $s_action="back";
105 }elseif(preg_match("/dep_home.*/i",$key)){
106 $s_action="home";
107 }elseif(preg_match("/dep_root.*/i",$key)){
108 $s_action="root";
109 }
111 }
113 if(isset($_POST['CurrentMainBase']) && $_POST['CurrentMainBase']){
114 $_SESSION['CurrentMainBase'] = $_POST['CurrentMainBase'];
115 }
117 if($s_action=="root"){
118 $_SESSION['CurrentMainBase'] = ($this->config->current['BASE']);
119 }
121 if($s_action=="home"){
122 $_SESSION['CurrentMainBase'] =(preg_replace("/^[^,]+,/","",$this->ui->dn));
123 $_SESSION['CurrentMainBase'] =(preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase'] ));
124 }
126 if($s_action=="back"){
127 $base_back = preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase']);
128 $base_back = convert_department_dn($base_back);
130 if(isset($this->config->departments[trim($base_back)])){
131 $_SESSION['CurrentMainBase'] = $this->config->departments[trim($base_back)];
132 }else{
133 $_SESSION['CurrentMainBase'] = $this->config->departments["/"];
134 }
135 }
137 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
138 $s_action="open";
139 $s_entry = base64_decode($_GET['dep_id']);
140 $_SESSION['CurrentMainBase'] = $this->config->departments[trim($s_entry)];
141 }
143 if (isset($_GET['search'])){
144 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
145 if ($s == "**"){
146 $s= "*";
147 }
148 $depfilter['regex']= $s;
149 }
150 register_global("depfilter", $depfilter);
152 /* New Entry if Posted action (s_action) == new
153 */
154 if ($s_action=="new"){
155 $this->dn= "new";
156 $this->deptabs= new deptabs($this->config,
157 $this->config->data['TABS']['DEPTABS'], $this->dn);
158 }
160 /* Edit Entry if Posted action (s_action) == edit
161 * The entry which will be edited is defined in $s_entry
162 */
163 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
164 $this->dn= $this->config->departments[trim($s_entry)];
166 if (($user= get_lock($this->dn)) != ""){
167 return(gen_locked_message ($user, $this->dn));
168 }
170 /* Lock the current entry, so everyone will get the above dialog */
171 add_lock ($this->dn, $this->ui->dn);
173 /* Set up the users ACL's for this 'dn' */
174 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
176 /* Register deptabs to trigger edit dialog */
177 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
178 $this->deptabs->set_acl($this->acl);
179 $_SESSION['objectinfo']= $this->dn;
180 }
182 /* Delete Entry if Posted action (s_action) == del
183 * The entry which will be deleted is defined in $s_entry
184 */
185 if ($s_action =="del"){
186 $this->dn= $this->config->departments[trim($s_entry)];
188 /* Check locking */
189 if (($user= get_lock($this->dn)) != ""){
190 $_SESSION['dn']= $this->dn;
191 return(gen_locked_message($user, $this->dn));
192 } else {
193 add_lock ($this->dn, $this->ui->dn);
194 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), LDAP::fix($this->dn)));
195 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
196 return ($display);
197 }
198 }
200 /* If department deletion is accepted ...
201 * Finally delete department
202 */
203 if (isset($_POST['delete_department_confirm'])){
204 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
205 $acl= get_module_permission($acl, "department", $this->dn);
206 if (chkacl($acl, "delete") == ""){
207 $this->remove_from_parent();
208 gosa_log ("Department object'".$this->dn."' has been removed");
209 $this->reload ();
210 } else {
211 print_red (_("You have no permission to remove this department."));
212 }
213 }
215 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config) ) ){
217 /* Check tabs, will feed message array */
218 $message= $this->deptabs->check();
220 /* Save, or display error message? */
221 if (count($message) == 0){
223 $this->deptabs->save(true);
224 $this->deptabs->by_object['department']->save();
226 /* Get tagging frame */
227 if($this->deptabs->by_object['department']->must_be_tagged()){
228 $this->ObjectTaggingRequested = true;
229 }
231 /* Get recursive move frame */
232 if($this->deptabs->by_object['department']->am_i_moved()){
233 $this->RecursiveRemoveRequested = true;
234 }
235 $this->ObjectInSaveMode = true;
236 } else {
237 /* Ok. There seem to be errors regarding to the tab data,
238 show message and continue as usual. */
239 show_errors($message);
240 }
241 }
243 /* This department must be tagged */
244 if((isset($_GET['TagDepartment'])) && ($this->ObjectTaggingRequested)){
245 $this->deptabs->by_object['department']->tag_objects();
246 $this->ObjectTaggingRequested = false;
247 exit();
248 }
249 /* initiate recursive remove*/
250 if((isset($_GET['PerformRecMove'])) &&($this->RecursiveRemoveRequested)){
251 $this->deptabs->by_object['department']->recursive_move("","",true);
252 $this->RecursiveRemoveRequested = false;
253 exit();
254 }
256 if($this->ObjectTaggingRequested){
257 return($this->deptabs->by_object['department']->ShowTagFrame());
258 }
259 if($this->RecursiveRemoveRequested){
260 return($this->deptabs->by_object['department']->ShowMoveFrame());
261 }
263 /* Unset current object / all operations are done */
264 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
265 $this->deptabs->save();
266 $this->ObjectInSaveMode = false;
267 if ($this->dn != "new"){
268 del_lock ($this->dn);
269 }
270 gosa_log ("Department object'".$this->dn."' has been saved");
271 $this->reload ();
272 unset ($this->deptabs);
273 $this->deptabs= NULL;
274 unset ($_SESSION['objectinfo']);
275 }
277 /* User cancelt edit oder delete
278 * Cancel dialog
279 */
280 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
281 isset($_POST['delete_department_confirm'])){
283 del_lock ($this->dn);
284 unset($this->depdabs);
285 $this->deptabs= NULL;
286 unset ($_SESSION['objectinfo']);
287 }
289 /* Headpage or normal plugin screen? */
290 if ($this->deptabs == NULL){
291 /* Check sorting variable */
292 $this->reload();
294 /* Check for exeeded sizelimit */
295 if (($message= check_sizelimit()) != ""){
296 return($message);
297 }
299 /* Prepare departments */
300 $options= "";
301 foreach ($this->config->idepartments as $key => $value){
302 if ($_SESSION['CurrentMainBase'] == $key){
303 $options.= "<option selected value='$key'>$value</option>";
304 } else {
305 $options.= "<option value='$key'>$value</option>";
306 }
307 }
309 /* Generate list head */
310 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
311 " <input type='image' class='center' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
312 " <input type='image' class='center' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'> ".
313 " <input type='image' class='center' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'> ".
314 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
315 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
316 " <input class='center' type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'> ".
317 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
318 _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
319 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
320 "</div>";
322 /* Show main page */
323 $divlist = new divlist("departmenttabs");
324 $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
325 $divlist->SetEntriesPerPage(0);
326 $actions= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
327 $actions.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
329 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
331 $divlist->SetHeader(array(
332 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
333 array("string"=>_("Department name"), "attach" => "style=''"),
334 array("string" =>_("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'")));
336 foreach($this->departments as $key => $val) {
337 if(!isset($this->config->departments[trim($key)])){
338 $this->config->departments[trim($key)]="";
339 }
341 $non_empty="";
342 $keys= str_replace("/","\/",$key);
343 foreach($this->config->departments as $keyd=>$vald ){
344 if(preg_match("/".$keys."\/.*/",$keyd)){
345 $non_empty="full";
346 }
347 }
349 $title = $this->config->departments[$key];
350 $field0 = array("string" => "<img src='images/".$non_empty."folder.png' title='"._("department")."' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
351 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style='' title='".$title."'");
352 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
354 $divlist->AddEntry( array($field0,$field1,$field2));
356 }
357 $smarty->assign("departments",$divlist->DrawList() );
358 $smarty->assign("departmentshead",$listhead);
359 $smarty->assign("search_image", get_template_path('images/search.png'));
360 $smarty->assign("tree_image", get_template_path('images/tree.png'));
361 $smarty->assign("infoimage", get_template_path('images/info.png'));
362 $smarty->assign("launchimage", get_template_path('images/launch.png'));
363 $smarty->assign("deplist", $this->config->idepartments);
365 foreach( array("regex") as $type){
366 $smarty->assign("$type", $depfilter[$type]);
367 }
369 $smarty->assign("CurrentMainBase", $_SESSION['CurrentMainBase']);
371 /* Extend if we are not using javascript */
372 $smarty->assign("apply", apply_filter());
373 $smarty->assign("alphabet", generate_alphabet());
374 $smarty->assign("hint", print_sizelimit_warning());
376 $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
377 return($display);
378 }
380 /* Show main page (tabs) */
381 $display= $this->deptabs->execute();
382 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
383 $display.= "<p style=\"text-align:right\">\n";
384 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
385 $display.= " \n";
386 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
387 $display.= "</p>";
388 }
389 return ($display);
390 }
393 function reload()
394 {
395 /* Get config */
396 $depfilter= get_global('depfilter');
397 // Added for dirlist function...
399 if(isset($_POST['deplist'])){
400 $depfilter= get_global("depfilter");
401 $_SESSION['CurrentMainBase']= $this->config->departments[trim($_POST['deplist'])];
402 register_global("depfilter", $depfilter);
403 }
405 /* Set base for all searches */
406 $base= $_SESSION['CurrentMainBase'];
408 /* Regex filter? */
409 if ($depfilter['regex'] != ""){
410 $regex= $depfilter['regex'];
411 } else {
412 $regex= "*";
413 }
415 // Configure Back Address
416 $base_back = preg_replace("/^[^,]+,/","",$base);
418 // Create Array to Test if we have a valid back button
419 $tmp = array_flip($_SESSION['config']->departments);
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 $res= get_list("(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
434 $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
436 $this->departments= array();
437 foreach ($res as $value){
438 if(isset($value["description"][0])){
439 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
440 }else{
441 $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
442 }
443 }
444 natcasesort ($this->departments);
445 reset ($this->departments);
446 }
448 function remove_from_parent()
449 {
450 $ldap= $this->config->get_ldap_link();
451 $ldap->cd ($this->dn);
452 $ldap->recursive_remove();
454 /* Optionally execute a command after we're done */
455 $this->postremove();
457 /* Delete references to object groups */
458 $ldap->cd ($this->config->current['BASE']);
459 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
460 while ($ldap->fetch()){
461 $og= new ogroup($this->config, $ldap->getDN());
462 unset($og->member[$this->dn]);
463 $og->save ();
464 }
466 }
468 function remove_lock()
469 {
470 if (isset($this->dn)){
471 del_lock ($this->dn);
472 }
473 }
475 }
476 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
477 ?>