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 $s_entry = preg_replace("/_.*$/","",$s_entry);
80 $s_entry = base64_decode($s_entry);
81 // Post for edit
82 }elseif(preg_match("/dep_edit_.*/",$key)){
83 $s_action="edit";
84 $s_entry = preg_replace("/dep_".$s_action."_/i","",$key);
85 $s_entry = preg_replace("/_.*$/","",$s_entry);
86 $s_entry = base64_decode($s_entry);
87 // Post for new
88 }elseif(preg_match("/dep_new.*/",$key)){
89 $s_action="new";
90 }elseif(preg_match("/dep_back.*/i",$key)){
91 $s_action="back";
92 }elseif(preg_match("/dep_home.*/i",$key)){
93 $s_action="home";
94 }elseif(preg_match("/dep_root.*/i",$key)){
95 $s_action="root";
96 }
98 }
100 if(isset($_POST['depselect']) && $_POST['depselect']){
101 $depfilter['depselect']= $_POST['depselect'];
102 }
104 if($s_action=="root"){
105 $depfilter['depselect']=($this->config->current['BASE']);
106 }
108 if($s_action=="home"){
109 $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
110 $depfilter['depselect']=(preg_replace("/^[^,]+,/","",$depfilter['depselect']));
111 }
113 if($s_action=="back"){
114 $base_back = preg_replace("/^[^,]+,/","",$depfilter['depselect']);
115 $base_back = convert_department_dn($base_back);
117 if(isset($this->config->departments[trim($base_back)])){
118 $depfilter['depselect']= $this->config->departments[trim($base_back)];
119 }else{
120 $depfilter['depselect']= $this->config->departments["/"];
121 }
122 }
124 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
125 $s_action="open";
126 $s_entry = base64_decode($_GET['dep_id']);
127 $depfilter['depselect']= $this->config->departments[trim($s_entry)];
128 }
130 if (isset($_GET['search'])){
131 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
132 if ($s == "**"){
133 $s= "*";
134 }
135 $depfilter['regex']= $s;
136 }
137 register_global("depfilter", $depfilter);
139 /* New Entry if Posted action (s_action) == new
140 */
141 if ($s_action=="new"){
142 $this->acl= array(":all");
143 $this->dn= "new";
145 $this->deptabs= new deptabs($this->config,
146 $this->config->data['TABS']['DEPTABS'], $this->dn);
147 $this->deptabs->set_acl($this->acl);
148 }
150 /* Edit Entry if Posted action (s_action) == edit
151 * The entry which will be edited is defined in $s_entry
152 */
153 if ($s_action=="edit"){
155 $this->dn= $this->config->departments[trim($s_entry)];
157 if (($user= get_lock($this->dn)) != ""){
158 return(gen_locked_message ($user, $this->dn));
159 }
161 /* Lock the current entry, so everyone will get the above dialog */
162 add_lock ($this->dn, $this->ui->dn);
164 /* Set up the users ACL's for this 'dn' */
165 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
167 /* Register deptabs to trigger edit dialog */
168 $this->deptabs= new deptabs($this->config,$this->config->data['TABS']['DEPTABS'], $this->dn);
169 $this->deptabs->set_acl($this->acl);
170 $_SESSION['objectinfo']= $this->dn;
171 }
173 /* Delete Entry if Posted action (s_action) == del
174 * The entry which will be deleted is defined in $s_entry
175 */
176 if ($s_action =="del"){
177 $this->dn= $this->config->departments[trim($s_entry)];
179 /* Check locking */
180 if (($user= get_lock($this->dn)) != ""){
181 $_SESSION['dn']= $this->dn;
182 return(gen_locked_message($user, $this->dn));
183 } else {
184 add_lock ($this->dn, $this->ui->dn);
185 $smarty->assign("info", sprintf(_("You're about to delete the whole LDAP subtree placed under '%s'."), $this->dn));
186 $display.= $smarty->fetch (get_template_path('remove.tpl', TRUE));
187 return ($display);
188 }
189 }
191 /* If department deletion is accepted ...
192 * Finally delete department
193 */
194 if (isset($_POST['delete_department_confirm'])){
195 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
196 $acl= get_module_permission($acl, "department", $this->dn);
197 if (chkacl($acl, "all") == ""){
198 $this->remove_from_parent();
199 gosa_log ("Department object'".$this->dn."' has been removed");
200 $this->reload ();
201 } else {
202 print_red (_("You have no permission to remove this department."));
203 }
204 }
206 /* Edit Complete ...
207 * Finish request
208 */
209 if (isset($_POST['edit_finish'])){
211 /* Check tabs, will feed message array */
212 $message= $this->deptabs->check();
214 /* Save, or display error message? */
215 if (count($message) == 0){
217 /* Save user data to ldap */
218 $this->deptabs->save();
219 gosa_log ("Department object'".$this->dn."' has been saved");
221 /* Group has been saved successfully, remove lock from
222 LDAP. */
223 if ($this->dn != "new"){
224 del_lock ($this->dn);
225 }
227 /* There's no page reload so we have to read new users at
228 this point. */
229 $this->reload ();
230 unset ($this->deptabs);
231 $this->deptabs= NULL;
232 unset ($_SESSION['objectinfo']);
234 } else {
235 /* Ok. There seem to be errors regarding to the tab data,
236 show message and continue as usual. */
237 show_errors($message);
238 }
239 }
242 /* User cancelt edit oder delete
243 * Cancel dialog
244 */
245 if (isset($_POST['edit_cancel']) || isset($_POST['delete_cancel']) ||
246 isset($_POST['delete_department_confirm']) || isset($_POST['delete_lock'])){
248 del_lock ($this->dn);
249 unset($this->depdabs);
250 $this->deptabs= NULL;
251 unset ($_SESSION['objectinfo']);
252 }
255 /* Headpage or normal plugin screen? */
256 if ($this->deptabs == NULL){
257 /* Check sorting variable */
258 $this->reload();
260 /* Check for exeeded sizelimit */
261 if (($message= check_sizelimit()) != ""){
262 return($message);
263 }
265 /* Prepare departments */
266 $options= "";
267 foreach ($this->config->idepartments as $key => $value){
268 if ($depfilter['depselect'] == $key){
269 $options.= "<option selected value='$key'>$value</option>";
270 } else {
271 $options.= "<option value='$key'>$value</option>";
272 }
273 }
275 /* Generate list head */
276 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
277 " <input type='image' src='images/list_up.png' align='middle' title='"._("Go up one department")."' name='dep_back' alt='"._("Up")."'> ".
278 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
279 " <input type='image' src='images/list_home.png' align='middle' title='"._("Go to users home department")."' name='dep_home' alt='"._("Home")."'> ".
280 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
281 " <input type='image' src='images/list_new_department.png' align='middle' alt='"._("Create new department")."' name='dep_new'> ".
282 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
283 _("Current base")." <select name='depselect' onChange='mainform.submit()'>$options</select>".
284 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
285 "</div>";
287 /* Show main page */
288 $divlist = new divlist("departmenttabs");
289 $divlist->SetSummary(_("This table displays all departments, in the selected tree."));
290 $divlist->SetEntriesPerPage(20);
291 $actions= "<input type='image' src='images/edit.png' alt='"._("edit")."' name='dep_edit_%KEY%' title='"._("Edit this entry")."'>";
292 $actions.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='dep_del_%KEY%' title='"._("Delete this entry")."'>";
294 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
296 $divlist->SetHeader(array(array("string" => " "),
297 array("string"=>_("Department name")),
298 array("string" =>_("Actions"), "attach" => "style='text-align: right;border:none'")));
301 foreach($this->departments as $key => $val) {
302 if(!isset($this->config->departments[trim($key)])){
303 $this->config->departments[trim($key)]="";
304 }
306 $non_empty="";
307 $keys= str_replace("/","\/",$key);
308 foreach($this->config->departments as $keyd=>$vald ){
309 if(preg_match("/".$keys."\/.*/",$keyd)){
310 $non_empty="full";
311 }
312 }
315 $title = $this->config->departments[$key];
316 $field0 = array("string" => "<img src='images/".$non_empty."folder.png' title='"._("department")."' alt='department'>","attach"=>"style='width:20px;'");
317 $field1 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style='width:100%' title='".$title."'");
318 $field2 = array("string" => preg_replace("/%KEY%/", base64_encode($key), $actions),"attach"=>"style='text-align:right;border:none'");
320 $divlist->AddEntry( array($field0,$field1,$field2));
322 }
323 $smarty->assign("departments",$divlist->DrawList() );
324 $smarty->assign("departmentshead",$listhead);
325 $smarty->assign("search_image", get_template_path('images/search.png'));
326 $smarty->assign("tree_image", get_template_path('images/tree.png'));
327 $smarty->assign("infoimage", get_template_path('images/info.png'));
328 $smarty->assign("launchimage", get_template_path('images/launch.png'));
329 $smarty->assign("deplist", $this->config->idepartments);
331 foreach( array("depselect", "regex") as $type){
332 $smarty->assign("$type", $depfilter[$type]);
333 }
335 /* Extend if we are not using javascript */
336 $smarty->assign("apply", apply_filter());
337 $smarty->assign("alphabet", generate_alphabet());
338 $smarty->assign("hint", print_sizelimit_warning());
340 $display= $smarty->fetch(get_template_path('headpage.tpl', TRUE));
341 return($display);
342 }
344 /* Show main page (tabs) */
345 $display= $this->deptabs->execute();
347 $display.= "<p style=\"text-align:right\">\n";
348 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
349 $display.= " \n";
350 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
351 $display.= "</p>";
353 return ($display);
354 }
357 function reload()
358 {
359 /* Get config */
360 $depfilter= get_global('depfilter');
361 // Added for dirlist function...
363 if(isset($_POST['deplist'])){
364 $depfilter= get_global("depfilter");
365 $depfilter['depselect']= $this->config->departments[trim($_POST['deplist'])];
366 register_global("depfilter", $depfilter);
367 }
369 /* Set base for all searches */
370 $base= $depfilter['depselect'];
372 /* Regex filter? */
373 if ($depfilter['regex'] != ""){
374 $regex= $depfilter['regex'];
375 } else {
376 $regex= "*";
377 }
379 // Configure Back Address
380 $base_back = preg_replace("/^[^,]+,/","",$base);
382 // Create Array to Test if we have a valid back button
383 $tmp = array_flip($_SESSION['config']->departments);
385 // In case of a valid back button create entry
386 if(isset($tmp[$base_back])){
387 $tmp2 ['dn'] = convert_department_dn($base_back);
389 // If empty always go to top
390 if(empty($tmp2['dn'])){
391 $tmp2['dn']="/";
392 }
393 $tmp2 ['description'][0] = _("..");
394 $result[$tmp[$base_back]]=$tmp2;
395 }
397 $res= get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
398 TRUE, $base, array("ou", "description"), TRUE);
400 //hickert Replaced Block
401 // $this->departments= array();
402 // foreach ($res as $value){
403 // $title= convert_department_dn($value["dn"]);
404 // $this->departments[$title]= "$title [".$value["description"][0]."]";
405 // $this->departments[$value['dn']]= "[".$value["description"][0]."]";
406 // }
408 $this->departments= array();
409 foreach ($res as $value){
410 if($value["description"][0]!=".."){
411 $this->departments[$value['dn']]= convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
412 }else{
413 $this->departments[$value['dn']]=$value["description"][0];
414 }
415 }
416 natcasesort ($this->departments);
417 reset ($this->departments);
418 }
420 function remove_from_parent()
421 {
422 $ldap= $this->config->get_ldap_link();
423 $ldap->cd ($this->dn);
424 $ldap->recursive_remove();
426 /* Optionally execute a command after we're done */
427 $this->postremove();
429 /* Delete references to object groups */
430 $ldap->cd ($this->config->current['BASE']);
431 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
432 while ($ldap->fetch()){
433 $og= new ogroup($this->config, $ldap->getDN());
434 unset($og->member[$this->dn]);
435 $og->save ();
436 }
438 }
440 function remove_lock()
441 {
442 if (isset($this->dn)){
443 del_lock ($this->dn);
444 }
445 }
447 }
449 ?>