476008cffc6b38b60512dd271f39f1b2becbaf32
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'])){
245 $this->deptabs->by_object['department']->tag_objects();
246 exit();
247 }
249 /* initiate recursive remove*/
250 if(isset($_GET['PerformRecMove'])){
251 $this->deptabs->by_object['department']->recursive_move("","",true);
252 exit();
253 }
255 if($this->ObjectTaggingRequested){
256 $this->ObjectTaggingRequested = false;
257 return($this->deptabs->by_object['department']->ShowTagFrame());
258 }
259 if($this->RecursiveRemoveRequested){
260 $this->RecursiveRemoveRequested = false;
261 return($this->deptabs->by_object['department']->ShowMoveFrame());
262 }
264 /* Unset current object / all operations are done */
265 if($this->ObjectInSaveMode && (!$this->RecursiveRemoveRequested) && (!$this->ObjectTaggingRequested)){
266 $this->deptabs->save();
267 $this->ObjectInSaveMode = false;
268 if ($this->dn != "new"){
269 del_lock ($this->dn);
270 }
271 gosa_log ("Department object'".$this->dn."' has been saved");
272 $this->reload ();
273 unset ($this->deptabs);
274 $this->deptabs= NULL;
275 unset ($_SESSION['objectinfo']);
276 }
278 /* User cancelt edit oder delete
279 * Cancel dialog
280 */
281 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
282 isset($_POST['delete_department_confirm'])){
284 del_lock ($this->dn);
285 unset($this->depdabs);
286 $this->deptabs= NULL;
287 unset ($_SESSION['objectinfo']);
288 }
290 /* Headpage or normal plugin screen? */
291 if ($this->deptabs == NULL){
292 /* Check sorting variable */
293 $this->reload();
295 /* Check for exeeded sizelimit */
296 if (($message= check_sizelimit()) != ""){
297 return($message);
298 }
300 /* Prepare departments */
301 $options= "";
302 foreach ($this->config->idepartments as $key => $value){
303 if ($_SESSION['CurrentMainBase'] == $key){
304 $options.= "<option selected value='$key'>$value</option>";
305 } else {
306 $options.= "<option value='$key'>$value</option>";
307 }
308 }
310 /* Generate list head */
311 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
312 " <input type='image' class='center' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
313 " <input type='image' class='center' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'> ".
314 " <input type='image' class='center' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'> ".
315 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
316 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
317 " <input class='center' type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'> ".
318 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
319 _("Base")." <select name='CurrentMainBase' onChange='mainform.submit()' class='center'>$options</select>".
320 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
321 "</div>";
323 /* Show main page */
324 $divlist = new divlist("departmenttabs");
325 $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
326 $divlist->SetEntriesPerPage(0);
327 $actions= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
328 $actions.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
330 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
332 $divlist->SetHeader(array(
333 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
334 array("string"=>_("Department name"), "attach" => "style=''"),
335 array("string" =>_("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'")));
337 foreach($this->departments as $key => $val) {
338 if(!isset($this->config->departments[trim($key)])){
339 $this->config->departments[trim($key)]="";
340 }
342 $non_empty="";
343 $keys= str_replace("/","\/",$key);
344 foreach($this->config->departments as $keyd=>$vald ){
345 if(preg_match("/".$keys."\/.*/",$keyd)){
346 $non_empty="full";
347 }
348 }
350 $title = $this->config->departments[$key];
351 $field0 = array("string" => "<img src='images/".$non_empty."folder.png' title='"._("department")."' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
352 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style='' title='".$title."'");
353 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
355 $divlist->AddEntry( array($field0,$field1,$field2));
357 }
358 $smarty->assign("departments",$divlist->DrawList() );
359 $smarty->assign("departmentshead",$listhead);
360 $smarty->assign("search_image", get_template_path('images/search.png'));
361 $smarty->assign("tree_image", get_template_path('images/tree.png'));
362 $smarty->assign("infoimage", get_template_path('images/info.png'));
363 $smarty->assign("launchimage", get_template_path('images/launch.png'));
364 $smarty->assign("deplist", $this->config->idepartments);
366 foreach( array("regex") as $type){
367 $smarty->assign("$type", $depfilter[$type]);
368 }
370 $smarty->assign("CurrentMainBase", $_SESSION['CurrentMainBase']);
372 /* Extend if we are not using javascript */
373 $smarty->assign("apply", apply_filter());
374 $smarty->assign("alphabet", generate_alphabet());
375 $smarty->assign("hint", print_sizelimit_warning());
377 $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
378 return($display);
379 }
381 /* Show main page (tabs) */
382 $display= $this->deptabs->execute();
383 if (!$this->deptabs->by_object[$this->deptabs->current]->dialog){
384 $display.= "<p style=\"text-align:right\">\n";
385 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
386 $display.= " \n";
387 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
388 $display.= "</p>";
389 }
390 return ($display);
391 }
394 function reload()
395 {
396 /* Get config */
397 $depfilter= get_global('depfilter');
398 // Added for dirlist function...
400 if(isset($_POST['deplist'])){
401 $depfilter= get_global("depfilter");
402 $_SESSION['CurrentMainBase']= $this->config->departments[trim($_POST['deplist'])];
403 register_global("depfilter", $depfilter);
404 }
406 /* Set base for all searches */
407 $base= $_SESSION['CurrentMainBase'];
409 /* Regex filter? */
410 if ($depfilter['regex'] != ""){
411 $regex= $depfilter['regex'];
412 } else {
413 $regex= "*";
414 }
416 // Configure Back Address
417 $base_back = preg_replace("/^[^,]+,/","",$base);
419 // Create Array to Test if we have a valid back button
420 $tmp = array_flip($_SESSION['config']->departments);
422 // In case of a valid back button create entry
423 if(isset($tmp[$base_back])){
424 $tmp2 ['dn'] = convert_department_dn($base_back);
426 // If empty always go to top
427 if(empty($tmp2['dn'])){
428 $tmp2['dn']="/";
429 }
430 $tmp2 ['description'][0] = _("..");
431 $result[$tmp[$base_back]]=$tmp2;
432 }
434 $res= get_list("(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
435 $this->ui->subtreeACL, $base, array("ou", "description"), GL_SIZELIMIT | GL_CONVERT);
437 $this->departments= array();
438 foreach ($res as $value){
439 if(isset($value["description"][0])){
440 $this->departments[$value['dn']]= get_sub_department($value['dn'])." - [".$value["description"][0]."]";
441 }else{
442 $this->departments[$value['dn']]= get_sub_department($value['dn']);//$value["description"][0];
443 }
444 }
445 natcasesort ($this->departments);
446 reset ($this->departments);
447 }
449 function remove_from_parent()
450 {
451 $ldap= $this->config->get_ldap_link();
452 $ldap->cd ($this->dn);
453 $ldap->recursive_remove();
455 /* Optionally execute a command after we're done */
456 $this->postremove();
458 /* Delete references to object groups */
459 $ldap->cd ($this->config->current['BASE']);
460 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
461 while ($ldap->fetch()){
462 $og= new ogroup($this->config, $ldap->getDN());
463 unset($og->member[$this->dn]);
464 $og->save ();
465 }
467 }
469 function remove_lock()
470 {
471 if (isset($this->dn)){
472 del_lock ($this->dn);
473 }
474 }
476 }
477 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
478 ?>