6ad90f7bc42deae9eb6032fba5b0d26127fb2e11
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 function departmentManagement ($config, $ui)
43 {
44 $this->ui= $ui;
45 $this->dn= "";
46 $this->config= $config;
48 /* Get global filter config */
49 if (!is_global("depfilter")){
50 $depfilter= array("regex" => "*");
51 register_global("depfilter", $depfilter);
52 }
53 $base= get_base_from_people($ui->dn);
54 $_SESSION['CurrentMainBase'] = $_SESSION['CurrentMainBase'];
55 }
57 function execute()
58 {
59 /* Call parent execute */
60 plugin::execute();
62 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^dep_edit_.*/","/^dep_del_.*/");
64 /* Reload departments */
65 $this->config->departments = get_departments();
66 $smarty = get_smarty();
67 $display = "";
68 $depfilter = get_global("depfilter");
69 $s_action = ""; // Will contain an action, like del or edit
70 $s_entry = ""; // The entry name for edit delete -...
72 $this->config->make_idepartments();
74 if (isset($_POST['regex'])){
75 $depfilter['regex']= $_POST['regex'];
76 }
78 // Check Post action
79 foreach($_POST as $key => $val){
80 // Post for delete
81 if(preg_match("/dep_del.*/",$key)){
82 $s_action = "del";
83 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
84 $s_entry = preg_replace("/_.*$/","",$s_entry);
85 $s_entry = base64_decode($s_entry);
86 // Post for edit
87 }elseif(preg_match("/dep_edit_.*/",$key)){
88 $s_action="edit";
89 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
90 $s_entry = preg_replace("/_.*$/","",$s_entry);
91 $s_entry = base64_decode($s_entry);
92 // Post for new
93 }elseif(preg_match("/dep_new.*/",$key)){
94 $s_action="new";
95 }elseif(preg_match("/dep_back.*/i",$key)){
96 $s_action="back";
97 }elseif(preg_match("/dep_home.*/i",$key)){
98 $s_action="home";
99 }elseif(preg_match("/dep_root.*/i",$key)){
100 $s_action="root";
101 }
103 }
105 if(isset($_POST['CurrentMainBase']) && $_POST['CurrentMainBase']){
106 $_SESSION['CurrentMainBase'] = $_POST['CurrentMainBase'];
107 }
109 if($s_action=="root"){
110 $_SESSION['CurrentMainBase'] = ($this->config->current['BASE']);
111 }
113 if($s_action=="home"){
114 $_SESSION['CurrentMainBase'] =(preg_replace("/^[^,]+,/","",$this->ui->dn));
115 $_SESSION['CurrentMainBase'] =(preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase'] ));
116 }
118 if($s_action=="back"){
119 $base_back = preg_replace("/^[^,]+,/","", $_SESSION['CurrentMainBase']);
120 $base_back = convert_department_dn($base_back);
122 if(isset($this->config->departments[trim($base_back)])){
123 $_SESSION['CurrentMainBase'] = $this->config->departments[trim($base_back)];
124 }else{
125 $_SESSION['CurrentMainBase'] = $this->config->departments["/"];
126 }
127 }
129 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
130 $s_action="open";
131 $s_entry = base64_decode($_GET['dep_id']);
132 $_SESSION['CurrentMainBase'] = $this->config->departments[trim($s_entry)];
133 }
135 if (isset($_GET['search'])){
136 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
137 if ($s == "**"){
138 $s= "*";
139 }
140 $depfilter['regex']= $s;
141 }
142 register_global("depfilter", $depfilter);
144 /* New Entry if Posted action (s_action) == new
145 */
146 if ($s_action=="new"){
147 $this->dn= "new";
148 $this->deptabs= new deptabs($this->config,
149 $this->config->data['TABS']['DEPTABS'], $this->dn);
150 }
152 /* Edit Entry if Posted action (s_action) == edit
153 * The entry which will be edited is defined in $s_entry
154 */
155 if (( $s_action=="edit") && (!isset($this->deptabs->config))){
156 $this->dn= $this->config->departments[trim($s_entry)];
158 if (($user= get_lock($this->dn)) != ""){
159 return(gen_locked_message ($user, $this->dn));
160 }
162 /* Lock the current entry, so everyone will get the above dialog */
163 add_lock ($this->dn, $this->ui->dn);
165 /* Set up the users ACL's for this 'dn' */
166 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
168 /* Register deptabs to trigger edit dialog */
169 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
170 $this->deptabs->set_acl($this->acl);
171 $_SESSION['objectinfo']= $this->dn;
172 }
174 /* Delete Entry if Posted action (s_action) == del
175 * The entry which will be deleted is defined in $s_entry
176 */
177 if ($s_action =="del"){
178 $this->dn= $this->config->departments[trim($s_entry)];
180 /* Check locking */
181 if (($user= get_lock($this->dn)) != ""){
182 $_SESSION['dn']= $this->dn;
183 return(gen_locked_message($user, $this->dn));
184 } else {
185 add_lock ($this->dn, $this->ui->dn);
186 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), LDAP::fix($this->dn)));
187 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
188 return ($display);
189 }
190 }
192 /* If department deletion is accepted ...
193 * Finally delete department
194 */
195 if (isset($_POST['delete_department_confirm'])){
196 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
197 $acl= get_module_permission($acl, "department", $this->dn);
198 if (chkacl($acl, "delete") == ""){
199 $this->remove_from_parent();
200 gosa_log ("Department object'".$this->dn."' has been removed");
201 $this->reload ();
202 } else {
203 print_red (_("You have no permission to remove this department."));
204 }
205 }
207 /* Edit Complete ...
208 * Finish request
209 */
210 if((isset($_GET['PerformRecMove'])) &&( $this->deptabs->by_object['department']->am_i_moved())){
211 $this->deptabs->save(true);
212 $this->deptabs->by_object['department']->recursive_move("","",true);
213 $this->reload ();
214 /* Group has been saved successfully, remove lock from
215 LDAP. */
216 if ($this->dn != "new"){
217 del_lock ($this->dn);
218 }
220 unset ($this->deptabs);
221 $this->deptabs= NULL;
222 unset ($_SESSION['objectinfo']);
223 exit();
224 }
226 if ((isset($_POST['edit_finish'])) && (isset($this->deptabs->config) ) ){
228 /* Check tabs, will feed message array */
229 $message= $this->deptabs->check();
231 /* Save, or display error message? */
232 if (count($message) == 0){
234 /* Save user data to ldap */
235 $disp = $this->deptabs->save();
236 if(!$this->deptabs->by_object['department']->am_i_moved()){
237 gosa_log ("Department object'".$this->dn."' has been saved");
239 /* Group has been saved successfully, remove lock from
240 LDAP. */
241 if ($this->dn != "new"){
242 del_lock ($this->dn);
243 }
245 /* There's no page reload so we have to read new users at
246 this point. */
247 $this->reload ();
248 unset ($this->deptabs);
249 $this->deptabs= NULL;
250 unset ($_SESSION['objectinfo']);
251 }else{
252 return($disp);
253 }
254 } else {
255 /* Ok. There seem to be errors regarding to the tab data,
256 show message and continue as usual. */
257 show_errors($message);
258 }
259 }
262 /* User cancelt edit oder delete
263 * Cancel dialog
264 */
265 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
266 isset($_POST['delete_department_confirm'])){
268 del_lock ($this->dn);
269 unset($this->depdabs);
270 $this->deptabs= NULL;
271 unset ($_SESSION['objectinfo']);
272 }
274 /* Headpage or normal plugin screen? */
275 if ($this->deptabs == NULL){
276 /* Check sorting variable */
277 $this->reload();
279 /* Check for exeeded sizelimit */
280 if (($message= check_sizelimit()) != ""){
281 return($message);
282 }
284 /* Prepare departments */
285 $options= "";
286 foreach ($this->config->idepartments as $key => $value){
287 if ($_SESSION['CurrentMainBase'] == $key){
288 $options.= "<option selected value='$key'>$value</option>";
289 } else {
290 $options.= "<option value='$key'>$value</option>";
291 }
292 }
294 /* Generate list head */
295 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
296 " <input type='image' class='center' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
297 " <input type='image' class='center' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'> ".
298 " <input type='image' class='center' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'> ".
299 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
300 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
301 " <input class='center' type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'> ".
302 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
303 _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
304 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
305 "</div>";
307 /* Show main page */
308 $divlist = new divlist("departmenttabs");
309 $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
310 $divlist->SetEntriesPerPage(0);
311 $actions= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
312 $actions.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
314 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
316 $divlist->SetHeader(array(
317 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
318 array("string"=>_("Department name"), "attach" => "style=''"),
319 array("string" =>_("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'")));
321 foreach($this->departments as $key => $val) {
322 if(!isset($this->config->departments[trim($key)])){
323 $this->config->departments[trim($key)]="";
324 }
326 $non_empty="";
327 $keys= str_replace("/","\/",$key);
328 foreach($this->config->departments as $keyd=>$vald ){
329 if(preg_match("/".$keys."\/.*/",$keyd)){
330 $non_empty="full";
331 }
332 }
334 $title = $this->config->departments[$key];
335 $field0 = array("string" => "<img src='images/".$non_empty."folder.png' title='"._("department")."' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
336 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style='' title='".$title."'");
337 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
339 $divlist->AddEntry( array($field0,$field1,$field2));
341 }
342 $smarty->assign("departments",$divlist->DrawList() );
343 $smarty->assign("departmentshead",$listhead);
344 $smarty->assign("search_image", get_template_path('images/search.png'));
345 $smarty->assign("tree_image", get_template_path('images/tree.png'));
346 $smarty->assign("infoimage", get_template_path('images/info.png'));
347 $smarty->assign("launchimage", get_template_path('images/launch.png'));
348 $smarty->assign("deplist", $this->config->idepartments);
350 foreach( array("regex") as $type){
351 $smarty->assign("$type", $depfilter[$type]);
352 }
354 $smarty->assign("CurrentMainBase", $_SESSION['CurrentMainBase']);
356 /* Extend if we are not using javascript */
357 $smarty->assign("apply", apply_filter());
358 $smarty->assign("alphabet", generate_alphabet());
359 $smarty->assign("hint", print_sizelimit_warning());
361 $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
362 return($display);
363 }
365 /* Show main page (tabs) */
366 $display= $this->deptabs->execute();
368 $display.= "<p style=\"text-align:right\">\n";
369 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
370 $display.= " \n";
371 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
372 $display.= "</p>";
374 return ($display);
375 }
378 function reload()
379 {
380 /* Get config */
381 $depfilter= get_global('depfilter');
382 // Added for dirlist function...
384 if(isset($_POST['deplist'])){
385 $depfilter= get_global("depfilter");
386 $_SESSION['CurrentMainBase']= $this->config->departments[trim($_POST['deplist'])];
387 register_global("depfilter", $depfilter);
388 }
390 /* Set base for all searches */
391 $base= $_SESSION['CurrentMainBase'];
393 /* Regex filter? */
394 if ($depfilter['regex'] != ""){
395 $regex= $depfilter['regex'];
396 } else {
397 $regex= "*";
398 }
400 // Configure Back Address
401 $base_back = preg_replace("/^[^,]+,/","",$base);
403 // Create Array to Test if we have a valid back button
404 $tmp = array_flip($_SESSION['config']->departments);
406 // In case of a valid back button create entry
407 if(isset($tmp[$base_back])){
408 $tmp2 ['dn'] = convert_department_dn($base_back);
410 // If empty always go to top
411 if(empty($tmp2['dn'])){
412 $tmp2['dn']="/";
413 }
414 $tmp2 ['description'][0] = _("..");
415 $result[$tmp[$base_back]]=$tmp2;
416 }
418 $res= get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
419 TRUE, $base, array("ou", "description"), TRUE);
421 //hickert Replaced Block
422 // $this->departments= array();
423 // foreach ($res as $value){
424 // $title= convert_department_dn($value["dn"]);
425 // $this->departments[$title]= "$title [".$value["description"][0]."]";
426 // $this->departments[$value['dn']]= "[".$value["description"][0]."]";
427 // }
429 $this->departments= array();
430 foreach ($res as $value){
431 if(isset($value["description"][0])){
432 $this->departments[$value['dn']]= $value['dn']." - [".$value["description"][0]."]";
433 }else{
434 $this->departments[$value['dn']]= $value['dn'];//$value["description"][0];
435 }
436 }
437 natcasesort ($this->departments);
438 reset ($this->departments);
440 /* Unset global department list */
441 unset($_SESSION['cached_departments']);
442 }
444 function remove_from_parent()
445 {
446 $ldap= $this->config->get_ldap_link();
447 $ldap->cd ($this->dn);
448 $ldap->recursive_remove();
450 /* Optionally execute a command after we're done */
451 $this->postremove();
453 /* Delete references to object groups */
454 $ldap->cd ($this->config->current['BASE']);
455 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
456 while ($ldap->fetch()){
457 $og= new ogroup($this->config, $ldap->getDN());
458 unset($og->member[$this->dn]);
459 $og->save ();
460 }
462 }
464 function remove_lock()
465 {
466 if (isset($this->dn)){
467 del_lock ($this->dn);
468 }
469 }
471 }
473 ?>