6a3bbbb74344e8f4555efd3d9f2f8bfb3a97fd3a
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;
54 /* Get global filter config */
55 if (!is_global("depfilter")){
56 $depfilter= array("regex" => "*");
57 register_global("depfilter", $depfilter);
58 }
59 $base= get_base_from_people($ui->dn);
60 if(!isset($_SESSION['CurrentMainBase'])){
61 $_SESSION['CurrentMainBase'] = $this->config->current['BASE'];
62 }
63 $_SESSION['CurrentMainBase'] = $_SESSION['CurrentMainBase'];
64 }
66 function execute()
67 {
68 /* Call parent execute */
69 plugin::execute();
71 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
73 /* Reload departments */
74 $this->config->get_departments();
75 $this->config->make_idepartments();
76 $_SESSION['config']= $this->config;
78 $smarty = get_smarty();
79 $display = "";
80 $depfilter = get_global("depfilter");
81 $s_action = ""; // Will contain an action, like del or edit
82 $s_entry = ""; // The entry name for edit delete -...
84 if (isset($_POST['regex'])){
85 $depfilter['regex']= $_POST['regex'];
86 }
88 // Check Post action
89 foreach($_POST as $key => $val){
90 // Post for delete
91 if(preg_match("/dep_del.*/",$key)){
92 $s_action = "del";
93 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
94 $s_entry = preg_replace("/_.*$/","",$s_entry);
95 $s_entry = base64_decode($s_entry);
96 // Post for edit
97 }elseif(preg_match("/dep_edit_.*/",$key)){
98 $s_action="edit";
99 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
100 $s_entry = preg_replace("/_.*$/","",$s_entry);
101 $s_entry = base64_decode($s_entry);
102 // Post for new
103 }elseif(preg_match("/dep_new.*/",$key)){
104 $s_action="new";
105 }elseif(preg_match("/dep_back.*/i",$key)){
106 $s_action="back";
107 }elseif(preg_match("/dep_home.*/i",$key)){
108 $s_action="home";
109 }elseif(preg_match("/dep_root.*/i",$key)){
110 $s_action="root";
111 }
113 }
115 if(isset($_POST['CurrentMainBase']) && $_POST['CurrentMainBase']){
116 $_SESSION['CurrentMainBase'] = $_POST['CurrentMainBase'];
117 }
119 if($s_action=="root"){
120 $_SESSION['CurrentMainBase'] = ($this->config->current['BASE']);
121 }
123 if($s_action=="home"){
124 $_SESSION['CurrentMainBase'] =(preg_replace("/^[^,]+,/","",$this->ui->dn));
125 $_SESSION['CurrentMainBase'] =(preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase'] ));
126 }
128 if($s_action=="back"){
129 $base_back = preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase']);
130 $base_back = convert_department_dn($base_back);
132 if(isset($this->config->departments[trim($base_back)])){
133 $_SESSION['CurrentMainBase'] = $this->config->departments[trim($base_back)];
134 }else{
135 $_SESSION['CurrentMainBase'] = $this->config->departments["/"];
136 }
137 }
139 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
140 $s_action="open";
141 $s_entry = base64_decode($_GET['dep_id']);
142 $_SESSION['CurrentMainBase'] = $this->config->departments[trim($s_entry)];
143 }
145 if (isset($_GET['search'])){
146 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
147 if ($s == "**"){
148 $s= "*";
149 }
150 $depfilter['regex']= $s;
151 }
152 register_global("depfilter", $depfilter);
154 /* New Entry if Posted action (s_action) == new
155 */
156 if ($s_action=="new"){
157 $this->dn= "new";
158 $this->deptabs= new deptabs($this->config,
159 $this->config->data['TABS']['DEPTABS'], $this->dn);
160 }
162 /* Edit Entry if Posted action (s_action) == edit
163 * The entry which will be edited is defined in $s_entry
164 */
165 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
166 $this->dn= $this->config->departments[trim($s_entry)];
168 if (($user= get_lock($this->dn)) != ""){
169 return(gen_locked_message ($user, $this->dn));
170 }
172 /* Lock the current entry, so everyone will get the above dialog */
173 add_lock ($this->dn, $this->ui->dn);
175 /* Set up the users ACL's for this 'dn' */
176 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
178 /* Register deptabs to trigger edit dialog */
179 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
180 $this->deptabs->set_acl($this->acl);
181 $_SESSION['objectinfo']= $this->dn;
182 }
184 /* Delete Entry if Posted action (s_action) == del
185 * The entry which will be deleted is defined in $s_entry
186 */
187 if ($s_action =="del"){
188 $this->dn= $this->config->departments[trim($s_entry)];
190 /* Check locking */
191 if (($user= get_lock($this->dn)) != ""){
192 $_SESSION['dn']= $this->dn;
193 return(gen_locked_message($user, $this->dn));
194 } else {
195 add_lock ($this->dn, $this->ui->dn);
196 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), LDAP::fix($this->dn)));
197 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
198 return ($display);
199 }
200 }
202 /* If department deletion is accepted ...
203 * Finally delete department
204 */
205 if (isset($_POST['delete_department_confirm'])){
206 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
207 $acl= get_module_permission($acl, "department", $this->dn);
208 if (chkacl($acl, "delete") == ""){
209 $this->remove_from_parent();
210 gosa_log ("Department object'".$this->dn."' has been removed");
211 $this->reload ();
212 } else {
213 print_red (_("You have no permission to remove this department."));
214 }
215 }
217 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config))){
219 /* Check tabs, will feed message array */
220 $message= $this->deptabs->check();
222 /* Save, or display error message? */
223 if (count($message) == 0){
225 $this->deptabs->save(true);
226 $this->deptabs->by_object['department']->save();
228 /* This object must be tagged, so set ObjectTaggingRequested to true */
229 if($this->deptabs->by_object['department']->must_be_tagged()){
230 $this->ObjectTaggingRequested = true;
231 }
233 /* Get recursive move is required, set RecursiveRemoveRequested to true */
234 if($this->deptabs->by_object['department']->am_i_moved()){
235 $this->RecursiveRemoveRequested = true;
236 }
238 /* This var indicated that there is an object which isn't saved right now. */
239 $this->ObjectInSaveMode = true;
241 } else {
242 /* Ok. There seem to be errors regarding to the tab data,
243 show message and continue as usual. */
244 show_errors($message);
245 }
246 }
248 /* This department must be tagged (Is called from iframe, generates output)*/
249 if(isset($_GET['TagDepartment'])){
250 $this->deptabs->by_object['department']->tag_objects();
251 exit();
252 }
254 /* initiate recursive remove (Is called from iframe, generates output)*/
255 if(isset($_GET['PerformRecMove'])){
256 $this->deptabs->by_object['department']->recursive_move("","",true);
257 exit();
258 }
260 /* While one of these vars below isset, we must return an iframe,
261 to perform requested operation */
262 if($this->ObjectTaggingRequested){
263 $this->ObjectTaggingRequested = false;
264 return($this->deptabs->by_object['department']->ShowTagFrame());
265 }
266 if($this->RecursiveRemoveRequested){
267 $this->RecursiveRemoveRequested = false;
268 return($this->deptabs->by_object['department']->ShowMoveFrame());
269 }
271 /* If there is an unsaved object and all operations are done
272 remove locks & save object tab & unset current object */
273 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
274 $this->deptabs->save();
275 $this->ObjectInSaveMode = false;
276 if ($this->dn != "new"){
277 del_lock ($this->dn);
278 }
279 gosa_log ("Department object'".$this->dn."' has been saved");
280 $this->reload ();
281 unset ($this->deptabs);
282 $this->deptabs= NULL;
283 unset ($_SESSION['objectinfo']);
284 }
286 /* User cancelt edit oder delete
287 * Cancel dialog
288 */
289 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
290 isset($_POST['delete_department_confirm'])){
292 del_lock ($this->dn);
293 unset($this->depdabs);
294 $this->deptabs= NULL;
295 unset ($_SESSION['objectinfo']);
296 }
298 /* Headpage or normal plugin screen? */
299 if ($this->deptabs == NULL){
300 /* Check sorting variable */
301 $this->reload();
303 /* Check for exeeded sizelimit */
304 if (($message= check_sizelimit()) != ""){
305 return($message);
306 }
308 /* Prepare departments */
309 $options= "";
310 foreach ($this->config->idepartments as $key => $value){
311 if ($_SESSION['CurrentMainBase'] == $key){
312 $options.= "<option selected value='$key'>$value</option>";
313 } else {
314 $options.= "<option value='$key'>$value</option>";
315 }
316 }
318 /* Generate list head */
319 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
320 " <input type='image' class='center' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
321 " <input type='image' class='center' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'> ".
322 " <input type='image' class='center' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'> ".
323 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
324 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
325 " <input class='center' type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'> ".
326 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
327 _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
328 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
329 "</div>";
331 /* Show main page */
332 $divlist = new divlist("departmenttabs");
333 $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
334 $divlist->SetEntriesPerPage(0);
335 $actions= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
336 $actions.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
338 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
340 $divlist->SetHeader(array(
341 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
342 array("string"=>_("Department name"), "attach" => "style=''"),
343 array("string" =>_("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'")));
345 foreach($this->departments as $key => $val) {
346 if(!isset($this->config->departments[trim($key)])){
347 $this->config->departments[trim($key)]="";
348 }
350 $non_empty="";
351 $keys= str_replace("/","\/",$key);
352 foreach($this->config->departments as $keyd=>$vald ){
353 if(preg_match("/".$keys."\/.*/",$keyd)){
354 $non_empty="full";
355 }
356 }
358 $title = $this->config->departments[$key];
359 $field0 = array("string" => "<img src='images/".$non_empty."folder.png' title='"._("department")."' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
360 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style='' title='".$title."'");
361 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
363 $divlist->AddEntry( array($field0,$field1,$field2));
365 }
366 $smarty->assign("departments",$divlist->DrawList() );
367 $smarty->assign("departmentshead",$listhead);
368 $smarty->assign("search_image", get_template_path('images/search.png'));
369 $smarty->assign("tree_image", get_template_path('images/tree.png'));
370 $smarty->assign("infoimage", get_template_path('images/info.png'));
371 $smarty->assign("launchimage", get_template_path('images/launch.png'));
372 $smarty->assign("deplist", $this->config->idepartments);
374 foreach( array("regex") as $type){
375 $smarty->assign("$type", $depfilter[$type]);
376 }
378 $smarty->assign("CurrentMainBase", $_SESSION['CurrentMainBase']);
380 /* Extend if we are not using javascript */
381 $smarty->assign("apply", apply_filter());
382 $smarty->assign("alphabet", generate_alphabet());
383 $smarty->assign("hint", print_sizelimit_warning());
385 $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
386 return($display);
387 }
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 }
402 function reload()
403 {
404 /* Get config */
405 $depfilter= get_global('depfilter');
406 // Added for dirlist function...
408 if(isset($_POST['deplist'])){
409 $depfilter= get_global("depfilter");
410 $_SESSION['CurrentMainBase']= $this->config->departments[trim($_POST['deplist'])];
411 register_global("depfilter", $depfilter);
412 }
414 /* Set base for all searches */
415 $base= $_SESSION['CurrentMainBase'];
417 /* Regex filter? */
418 if ($depfilter['regex'] != ""){
419 $regex= $depfilter['regex'];
420 } else {
421 $regex= "*";
422 }
424 // Configure Back Address
425 $base_back = preg_replace("/^[^,]+,/","",$base);
427 // Create Array to Test if we have a valid back button
428 $tmp = array_flip($_SESSION['config']->departments);
430 // In case of a valid back button create entry
431 if(isset($tmp[$base_back])){
432 $tmp2 ['dn'] = convert_department_dn($base_back);
434 // If empty always go to top
435 if(empty($tmp2['dn'])){
436 $tmp2['dn']="/";
437 }
438 $tmp2 ['description'][0] = _("..");
439 $result[$tmp[$base_back]]=$tmp2;
440 }
442 $res= get_list("(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
443 $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
445 $this->departments= array();
446 foreach ($res as $value){
447 if(isset($value["description"][0])){
448 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
449 }else{
450 $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
451 }
452 }
453 natcasesort ($this->departments);
454 reset ($this->departments);
455 }
457 function remove_from_parent()
458 {
459 $ldap= $this->config->get_ldap_link();
460 $ldap->cd ($this->dn);
461 $ldap->recursive_remove();
463 /* Optionally execute a command after we're done */
464 $this->postremove();
466 /* Delete references to object groups */
467 $ldap->cd ($this->config->current['BASE']);
468 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
469 while ($ldap->fetch()){
470 $og= new ogroup($this->config, $ldap->getDN());
471 unset($og->member[$this->dn]);
472 $og->save ();
473 }
475 }
477 function remove_lock()
478 {
479 if (isset($this->dn)){
480 del_lock ($this->dn);
481 }
482 }
484 }
485 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
486 ?>