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_application.inc";
22 class applicationManagement extends plugin
23 {
24 /* Definitions */
25 var $plHeadline = "Applications";
26 var $plDescription = "This does something";
28 /* Dialog attributes */
29 var $apptabs = NULL;
30 var $ui = NULL;
31 var $CopyPasteHandler = NULL;
32 var $DivListApplication = NULL;
33 var $applications = array();
34 var $enableReleaseManagement = false;
36 function IsReleaseManagementActivated()
37 {
38 /* Check if we should enable the release selection */
39 $tmp = search_config($this->config->data,"faiManagement","CLASS");
40 if(!empty($tmp)){
41 return(true);
42 }
43 return(false);
44 }
46 function applicationManagement ($config, $ui)
47 {
48 /* Save configuration for internal use */
49 $this->config = $config;
50 $this->ui = $ui;
52 /* Check if copy & paste is activated */
53 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
54 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
55 }
57 /* Creat dialog object */
58 $this->DivListApplication = new divListApplication($this->config,$this);
60 if($this->IsReleaseManagementActivated()){
61 /* Check if we should enable the release selection */
62 $this->enableReleaseManagement = true;
64 /* Hide SubSearch checkbox */
65 $this->DivListApplication->DisableCheckBox("SubSearch");
66 }
67 }
69 function getReleases($base)
70 {
71 $ldap = $this->config->get_ldap_link();
72 $dn = "ou=apps,".$base;
73 $ret = array();
74 $ret ["ou=apps,".$base] = "/";
76 $ldap->cd($dn);
77 $ldap->search("objectClass=organizationalUnit",array("ou"));
79 while($attrs = $ldap->fetch()){
80 $str = str_replace($dn,"",$attrs['dn']);
81 $tmp = array_reverse( split("ou=",$str));
82 $str = "";
83 foreach($tmp as $val){
84 $val = trim(preg_replace("/,/","",$val));
85 if(empty($val)) break;
86 $str .= "/".$val;
87 }
88 if(!empty($str)){
89 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
90 }
91 }
92 asort($ret);
93 return($ret);
94 }
96 function execute()
97 {
98 /* Call parent execute */
99 plugin::execute();
102 /****************
103 Variable init
104 ****************/
106 /* These vars will be stored if you try to open a locked app,
107 to be able to perform your last requests after showing a warning message */
108 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/");
110 $smarty = get_smarty(); // Smarty instance
111 $s_action = ""; // Contains the action to proceed
112 $s_entry = ""; // The value for s_action
113 $base_back = ""; // The Link for Backbutton
115 /* Test Posts */
116 foreach($_POST as $key => $val){
117 // Post for delete
118 if(preg_match("/appl_del.*/",$key)){
119 $s_action = "del";
120 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
121 // Post for edit
122 }elseif(preg_match("/appl_edit_.*/",$key)){
123 $s_action="edit";
124 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
125 // Post for new
126 }elseif(preg_match("/^copy_.*/",$key)){
127 $s_action="copy";
128 $s_entry = preg_replace("/^copy_/i","",$key);
129 }elseif(preg_match("/^cut_.*/",$key)){
130 $s_action="cut";
131 $s_entry = preg_replace("/^cut_/i","",$key);
132 // Post for new
133 }elseif(preg_match("/^appl_new.*/",$key)){
134 $s_action="new";
135 }elseif(preg_match("/^remove_multiple_applications/",$key)){
136 $s_action="del_multiple";
137 }elseif(preg_match("/^editPaste.*/i",$key)){
138 $s_action="editPaste";
139 }
140 }
142 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
143 $s_action ="edit";
144 $s_entry = $_GET['id'];
145 }
147 $s_entry = preg_replace("/_.$/","",$s_entry);
150 /****************
151 Copy & Paste handling
152 ****************/
154 /* Only perform copy / paste if it is enabled
155 */
156 if($this->CopyPasteHandler){
157 if($str = $this->copyPasteHandling($s_action,$s_entry)){
158 return($str);
159 };
160 }
163 /****************
164 Create a new app
165 ****************/
167 /* New application? */
168 if ($s_action=="new"){
170 /* By default we set 'dn' to 'new', all relevant plugins will
171 react on this. */
172 $this->dn= "new";
174 /* Create new usertab object */
175 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
176 $this->apptabs->set_acl_base($this->DivListApplication->selectedBase);
177 }
180 /****************
181 Edit entry canceled
182 ****************/
184 /* Cancel dialogs */
185 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
186 del_lock ($this->apptabs->dn);
187 unset ($this->apptabs);
188 $this->apptabs= NULL;
189 unset ($_SESSION['objectinfo']);
190 }
193 /****************
194 Edit entry finished
195 ****************/
197 /* Finish apps edit is triggered by the tabulator dialog, so
198 the user wants to save edited data. Check and save at this
199 point. */
200 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
202 /* Check tabs, will feed message array */
203 $this->apptabs->last= $this->apptabs->current;
204 $this->apptabs->save_object();
205 $message= $this->apptabs->check();
207 /* Save, or display error message? */
208 if (count($message) == 0){
210 /* Save data data to ldap */
211 $this->apptabs->set_release($this->DivListApplication->selectedRelease);
212 $this->apptabs->save();
213 gosa_log ("Application object'".$this->dn."' has been saved");
215 if (!isset($_POST['edit_apply'])){
216 /* Application has been saved successfully, remove lock from
217 LDAP. */
218 if ($this->dn != "new"){
219 del_lock ($this->dn);
220 }
221 unset ($this->apptabs);
222 $this->apptabs= NULL;
223 unset ($_SESSION['objectinfo']);
224 }
225 } else {
226 /* Ok. There seem to be errors regarding to the tab data,
227 show message and continue as usual. */
228 show_errors($message);
229 }
230 }
233 /****************
234 Edit entry
235 ****************/
237 /* User wants to edit data? */
238 if (($s_action=="edit") && (!isset($this->apptabs->config))){
240 /* Get 'dn' from posted 'applist', must be unique */
241 $this->dn= $this->applications[$s_entry]['dn'];
243 /* Check locking, save current plugin in 'back_plugin', so
244 the dialog knows where to return. */
245 if (($user= get_lock($this->dn)) != ""){
246 return(gen_locked_message ($user, $this->dn));
247 }
249 /* Lock the current entry, so everyone will get the
250 above dialog */
251 add_lock ($this->dn, $this->ui->dn);
253 /* Register apptabs to trigger edit dialog */
254 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
255 $this->apptabs->set_acl_base($this->dn);
256 $_SESSION['objectinfo']= $this->dn;
257 }
261 /********************
262 Delete MULTIPLE entries requested, display confirm dialog
263 ********************/
265 if ($s_action=="del_multiple"){
266 $ids = $this->list_get_selected_items();
268 if(count($ids)){
270 foreach($ids as $id){
271 $dn = $this->applications[$id]['dn'];
272 if (($user= get_lock($dn)) != ""){
273 return(gen_locked_message ($user, $dn));
274 }
275 $this->dns[$id] = $dn;
276 }
278 $dns_names = "<br><pre>";
279 foreach($this->dns as $dn){
280 add_lock ($dn, $this->ui->dn);
281 $dns_names .= $dn."\n";
282 }
283 $dns_names .="</pre>";
285 /* Lock the current entry, so nobody will edit it during deletion */
286 $smarty->assign("intro", sprintf(_("You're about to delete the following application(s) %s"), @LDAP::fix($dns_names)));
287 $smarty->assign("multiple", true);
288 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
289 }
290 }
293 /********************
294 Delete MULTIPLE entries confirmed
295 ********************/
297 /* Confirmation for deletion has been passed. Users should be deleted. */
298 if (isset($_POST['delete_multiple_application_confirm'])){
300 /* Remove user by user and check acls before removeing them */
301 foreach($this->dns as $key => $dn){
303 $ui = get_userinfo();
304 $acl = $ui->get_permissions($dn ,"application/application");
305 if (preg_match('/d/', $acl)){
307 /* Delete request is permitted, perform LDAP action */
308 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
309 $this->apptabs->set_acl_base($dn);
310 $this->apptabs->delete ();
311 gosa_log ("Application object'".$dn."' has been removed");
312 unset ($this->apptabs);
313 $this->apptabs= NULL;
315 } else {
316 /* Normally this shouldn't be reached, send some extra
317 logs to notify the administrator */
318 print_red (_("You are not allowed to delete this application!"));
319 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
320 }
321 /* Remove lock file after successfull deletion */
322 del_lock ($dn);
323 unset($this->dns[$key]);
324 }
325 }
328 /********************
329 Delete MULTIPLE entries Canceled
330 ********************/
332 /* Remove lock */
333 if(isset($_POST['delete_multiple_application_cancel'])){
334 foreach($this->dns as $key => $dn){
335 del_lock ($dn);
336 unset($this->dns[$key]);
337 }
338 }
340 /****************
341 Delete app
342 ****************/
344 /* Remove user was requested */
345 if ($s_action == "del"){
347 /* Get 'dn' from posted 'uid' */
348 $this->dn= $this->applications[$s_entry]['dn'];
350 /* Load permissions for selected 'dn' and check if
351 we're allowed to remove this 'dn' */
352 $ui = get_userinfo();
353 $acl = $ui->get_permissions($this->dn ,"application/application");
355 if(preg_match("/d/",$acl)){
356 /* Check locking, save current plugin in 'back_plugin', so
357 the dialog knows where to return. */
358 if (($user= get_lock($this->dn)) != ""){
359 return (gen_locked_message ($user, $this->dn));
360 }
362 /* Lock the current entry, so nobody will edit it during deletion */
363 add_lock ($this->dn, $this->ui->dn);
364 $smarty= get_smarty();
365 $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
366 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
367 } else {
369 /* Obviously the user isn't allowed to delete. Show message and
370 clean session. */
371 print_red (_("You are not allowed to delete this application!"));
372 }
373 }
376 /****************
377 Delete app confirmed
378 ****************/
380 /* Confirmation for deletion has been passed. Group should be deleted. */
381 if (isset($_POST['delete_app_confirm'])){
383 /* Some nice guy may send this as POST, so we've to check
384 for the permissions again. */
385 $ui = get_userinfo();
386 $acl = $ui->get_permissions($this->dn ,"application/application");
388 if(preg_match("/d/",$acl)){
390 /* Delete request is permitted, perform LDAP action */
391 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
392 $this->apptabs->set_acl_base($this->dn);
393 $this->apptabs->delete ();
394 gosa_log ("Application object'".$this->dn."' has been removed");
395 unset ($this->apptabs);
396 $this->apptabs= NULL;
398 } else {
400 /* Normally this shouldn't be reached, send some extra
401 logs to notify the administrator */
402 print_red (_("You are not allowed to delete this application!"));
403 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
404 }
406 /* Remove lock file after successfull deletion */
407 del_lock ($this->dn);
408 }
411 /****************
412 Delete app canceled
413 ****************/
415 /* Delete application canceled? */
416 if (isset($_POST['delete_cancel'])){
417 del_lock ($this->dn);
418 unset($_SESSION['objectinfo']);
419 }
421 /* Show tab dialog if object is present */
422 if (($this->apptabs) && (isset($this->apptabs->config))){
423 $display= $this->apptabs->execute();
425 /* Don't show buttons if tab dialog requests this */
426 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
427 $display.= "<p style=\"text-align:right\">\n";
428 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
429 $display.= " \n";
430 if ($this->dn != "new"){
431 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
432 $display.= " \n";
433 }
434 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
435 $display.= "</p>";
436 }
437 return ($display);
438 }
441 /****************
442 Dialog display
443 ****************/
445 /* Check if there is a snapshot dialog open */
446 $base = $this->DivListApplication->selectedBase;
447 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
448 return($str);
449 }
451 /* Display dialog with system list */
452 $this->DivListApplication->parent = $this;
453 $this->DivListApplication->execute();
454 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
455 $this->reload();
456 $this->DivListApplication->setEntries($this->applications);
457 return($this->DivListApplication->Draw());
458 }
461 /* Return departments, that will be included within snapshot detection */
462 function get_used_snapshot_bases(){
463 if($this->DivListApplication->selectedRelease == "main"){
464 return(array($this->DivListApplication->selectedBase));
465 }else{
466 return(array($this->DivListApplication->selectedRelease));
467 }
468 }
471 function reload()
472 {
473 $this->applications= array();
475 /* Set base for all searches */
476 $base = $this->DivListApplication->selectedBase;
477 $release = $this->DivListApplication->selectedRelease;
478 $Regex = $this->DivListApplication->Regex;
479 $SubSearch = $this->DivListApplication->SubSearch;
480 $Flags = GL_NONE | GL_SIZELIMIT;
481 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
482 $tmp = array();
483 $Releases = $this->getReleases($base);
485 if(!$this->enableReleaseManagement){
486 $use_base = "ou=apps,".$base;
487 }else{
488 if(isset($Releases[$release])){
489 $use_base = $release;
490 }else{
491 $use_base = "ou=apps,".$base;
492 }
493 }
495 if($SubSearch){
496 $Flags |= GL_SUBSEARCH;
497 }
499 $res= get_list($Filter, "application", $use_base, array("cn","description","dn","objectClass"), $Flags);
500 foreach ($res as $val){
501 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
502 }
504 ksort($tmp);
505 $this->applications=array();
506 foreach($tmp as $val){
507 $this->applications[]=$val;
508 }
509 reset ($this->applications);
510 }
512 function remove_from_parent()
513 {
514 /* Optionally execute a command after we're done */
515 $this->postremove();
516 }
519 function copyPasteHandling($s_action,$s_entry)
520 {
521 /* Paste copied/cutted object in here
522 */
523 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
524 $this->CopyPasteHandler->save_object();
525 $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
527 if($str = $this->CopyPasteHandler->execute()) {
528 return($str);
529 }
530 }
532 /* Copy current object to CopyHandler
533 */
534 if($s_action == "copy"){
535 $this->CopyPasteHandler->Clear();
536 $dn = $this->applications[$s_entry]['dn'];
538 /* Check Acls */
539 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
540 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
541 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
542 $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new","application");
543 $this->CopyPasteHandler->Copy($obj,$objNew);
544 }
545 }
547 /* Copy current object to CopyHandler
548 */
549 if($s_action == "cut"){
550 $this->CopyPasteHandler->Clear();
551 $dn = $this->applications[$s_entry]['dn'];
553 /* Check Acls */
554 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
555 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
556 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
557 $this->CopyPasteHandler->Cut($obj);
558 }
559 }
560 }
562 function list_get_selected_items()
563 {
564 $ids = array();
565 foreach($_POST as $name => $value){
566 if(preg_match("/^item_selected_[0-9]*$/",$name)){
567 $id = preg_replace("/^item_selected_/","",$name);
568 $ids[$id] = $id;
569 }
570 }
571 return($ids);
572 }
575 /* Save to LDAP */
576 function save()
577 {
578 /* Optionally execute a command after we're done */
579 $this->postcreate();
580 }
582 function remove_lock()
583 {
584 if (isset($this->apptabs->dn)){
585 del_lock ($this->apptabs->dn);
586 }
587 }
589 function save_object() {
590 $this->DivListApplication->save_object();
591 }
593 function check() {}
594 function adapt_from_template($dn) {}
595 function password_change_needed() {}
596 }
597 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
598 ?>