38753f4b171a9a38fd8b77c4e86453203709576f
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 /* Reload departments */
60 $this->config->departments = get_departments();
61 $smarty = get_smarty();
62 $display = "";
63 $depfilter = get_global("depfilter");
64 $s_action = ""; // Will contain an action, like del or edit
65 $s_entry = ""; // The entry name for edit delete -...
67 $this->config->make_idepartments();
69 if (isset($_POST['regex'])){
70 $depfilter['regex']= $_POST['regex'];
71 }
73 // Check Post action
74 foreach($_POST as $key => $val){
75 // Post for delete
76 if(preg_match("/dep_del.*/",$key)){
77 $s_action = "del";
78 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
79 // Post for edit
80 }elseif(preg_match("/dep_edit_.*/",$key)){
81 $s_action="edit";
82 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
83 // Post for new
84 }elseif(preg_match("/dep_new.*/",$key)){
85 $s_action="new";
86 }elseif(preg_match("/dep_back.*/i",$key)){
87 $s_action="back";
88 }elseif(preg_match("/dep_home.*/i",$key)){
89 $s_action="home";
90 }elseif(preg_match("/dep_root.*/i",$key)){
91 $s_action="root";
92 }
93 }
95 if(isset($_POST['depselect']) && $_POST['depselect']){
96 $depfilter['depselect']= $_POST['depselect'];
97 $this->reload();
98 }
100 if($s_action=="root"){
101 $depfilter['depselect']=($this->config->current['BASE']);
102 $this->reload();
103 }
105 if($s_action=="home"){
106 $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
107 $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$depfilter['depselect']));
108 $this->reload();
109 }
111 if($s_action=="back"){
112 $base_back = preg_replace("/^[^,]+,/","",$depfilter['depselect']);
113 $base_back = convert_department_dn($base_back);
115 if(isset($this->config->departments[trim($base_back)])){
116 $depfilter['depselect']= $this->config->departments[trim($base_back)];
117 }else{
118 $depfilter['depselect']= $this->config->departments["/"];
119 }
120 $this->reload();
121 }
123 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
124 $s_action="open";
125 $s_entry = base64_decode($_GET['dep_id']);
126 $depfilter['depselect']= $this->config->departments[trim($s_entry)];
127 $this->reload();
128 }
131 $s_entry = preg_replace("/_.*/","",$s_entry);
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();
292 $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
293 $divlist->SetEntriesPerPage(20);
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(array("string" => " "),
300 array("string"=>_("Department name")),
301 array("string" =>_("Actions"), "attach" => "style='text-align: right;border:none'")));
303 foreach($this->departments as $key => $val) {
304 if(!isset($this->config->departments[trim($key)])){
305 $this->config->departments[trim($key)]="";
306 }
307 $field0 = array("string" => "<img src='images/folder.png' alt='department'>","attach"=>"style='width:20px;'");
308 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "width='100%'");
309 $field2 = array("string" => preg_replace("/%KEY%/", $key, $actions),"attach"=>"style='text-align:right;border:none'");
311 $divlist->AddEntry( array($field0,$field1,$field2));
313 }
314 $smarty->assign("departments",$divlist->DrawList() );
315 $smarty->assign("departmentshead",$listhead);
316 $smarty->assign("search_image", get_template_path('images/search.png'));
317 $smarty->assign("tree_image", get_template_path('images/tree.png'));
318 $smarty->assign("infoimage", get_template_path('images/info.png'));
319 $smarty->assign("launchimage", get_template_path('images/launch.png'));
320 $smarty->assign("deplist", $this->config->idepartments);
322 foreach( array("depselect", "regex") as $type){
323 $smarty->assign("$type", $depfilter[$type]);
324 }
326 /* Extend if we are not using javascript */
327 $smarty->assign("apply", apply_filter());
328 $smarty->assign("alphabet", generate_alphabet());
329 $smarty->assign("hint", print_sizelimit_warning());
331 $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
332 return($display);
333 }
335 /* Show main page (tabs) */
336 $display= $this->deptabs->execute();
338 $display.= "<p style=\"text-align:right\">\n";
339 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
340 $display.= " \n";
341 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
342 $display.= "</p>";
344 return ($display);
345 }
348 function reload()
349 {
350 /* Get config */
351 $depfilter= get_global('depfilter');
352 // Added for dirlist function...
354 if(isset($_POST['deplist'])){
355 $depfilter= get_global("depfilter");
356 $depfilter['depselect']= $this->config->departments[trim($_POST['deplist'])];
357 register_global("depfilter", $depfilter);
358 }
360 /* Set base for all searches */
361 $base= $depfilter['depselect'];
363 /* Regex filter? */
364 if ($depfilter['regex'] != ""){
365 $regex= $depfilter['regex'];
366 } else {
367 $regex= "*";
368 }
370 // Configure Back Address
371 $base_back = preg_replace("/^[^,]+,/","",$base);
373 // Create Array to Test if we have a valid back button
374 $tmp = array_flip($_SESSION['config']->departments);
376 // In case of a valid back button create entry
377 if(isset($tmp[$base_back])){
378 $tmp2 ['dn'] = convert_department_dn($base_back);
380 // If empty always go to top
381 if(empty($tmp2['dn'])){
382 $tmp2['dn']="/";
383 }
384 $tmp2 ['description'][0] = _("..");
385 $result[$tmp[$base_back]]=$tmp2;
386 }
388 $res= get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
389 TRUE, $base, array("ou", "description"), TRUE);
391 //hickert Replaced Block
392 // $this->departments= array();
393 // foreach ($res as $value){
394 // $title= convert_department_dn($value["dn"]);
395 // $this->departments[$title]= "$title [".$value["description"][0]."]";
396 // $this->departments[$value['dn']]= "[".$value["description"][0]."]";
397 // }
399 $this->departments= array();
400 foreach ($res as $value){
401 if($value["description"][0]!=".."){
402 $this->departments[$value['dn']]= convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
403 }else{
404 $this->departments[$value['dn']]=$value["description"][0];
405 }
406 }
407 natcasesort ($this->departments);
408 reset ($this->departments);
409 }
411 function remove_from_parent()
412 {
413 $ldap= $this->config->get_ldap_link();
414 $ldap->cd ($this->dn);
415 $ldap->recursive_remove();
417 /* Optionally execute a command after we're done */
418 $this->postremove();
420 /* Delete references to object groups */
421 $ldap->cd ($this->config->current['BASE']);
422 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
423 while ($ldap->fetch()){
424 $og= new ogroup($this->config, $ldap->getDN());
425 unset($og->member[$this->dn]);
426 $og->save ();
427 }
429 }
431 function remove_lock()
432 {
433 if (isset($this->dn)){
434 del_lock ($this->dn);
435 }
436 }
438 }
440 ?>