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