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