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 */
21 class applicationManagement extends plugin
22 {
23 /* Definitions */
24 var $plHeadline = "Applications";
25 var $plDescription = "This does something";
27 /* Dialog attributes */
28 var $apptabs = NULL;
29 var $ui = NULL;
30 var $CopyPasteHandler = NULL;
31 var $DivListApplication = NULL;
32 var $applications = array();
33 var $enableReleaseManagement = false;
34 var $start_pasting_copied_objects = FALSE;
36 var $app_base ="";
37 var $app_release ="";
39 function IsReleaseManagementActivated()
40 {
41 /* Check if we should enable the release selection */
42 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
43 if(!empty($tmp)){
44 return(true);
45 }
46 return(false);
47 }
50 function applicationManagement (&$config, &$ui)
51 {
52 /* Save configuration for internal use */
53 $this->config = &$config;
54 $this->ui = &$ui;
56 /* Check if copy & paste is activated */
57 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
58 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
59 }
61 /* Creat dialog object */
62 $this->DivListApplication = new divListApplication($this->config,$this);
64 if($this->IsReleaseManagementActivated()){
65 /* Check if we should enable the release selection */
66 $this->enableReleaseManagement = true;
68 /* Hide SubSearch checkbox */
69 $this->DivListApplication->DisableCheckBox("SubSearch");
70 }
72 /* Set default release */
73 if(!$this->IsReleaseManagementActivated()){
74 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
75 if(!session::is_set("app_filter")){
76 session::set("app_filter",array("app_base" => $this->app_base));
77 }
78 $app_filter = session::get("app_filter");
79 $this->app_base = $app_filter['app_base'];
80 }else{
81 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
82 if(!session::is_set("app_filter")){
83 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
84 }
85 $app_filter = session::get("app_filter");
86 $this->app_base = $app_filter['app_base'];
87 $this->app_release = $app_filter['app_release'];
88 }
89 }
92 function getReleases()
93 {
94 $ldap = $this->config->get_ldap_link();
95 $ret = array();
96 $base = $this->app_base;
98 $ret[$this->app_base] = "/";
99 $ldap->cd($base);
100 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
101 while($attrs = $ldap->fetch()){
102 $str = str_replace($base,"",$attrs['dn']);
103 $tmp = array_reverse( split("ou=",$str));
104 $str = "";
105 foreach($tmp as $val){
106 $val = trim(preg_replace("/,/","",$val));
107 if(empty($val)) break;
108 $str .= "/".$val;
109 }
110 if(!empty($str)){
111 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
112 }
113 }
114 asort($ret);
115 return($ret);
116 }
118 function execute()
119 {
120 /* Call parent execute */
121 plugin::execute();
124 /****************
125 Variable init
126 ****************/
128 /* These vars will be stored if you try to open a locked app,
129 to be able to perform your last requests after showing a warning message */
130 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/"));
132 $smarty = get_smarty(); // Smarty instance
133 $s_action = ""; // Contains the action to proceed
134 $s_entry = ""; // The value for s_action
135 $base_back = ""; // The Link for Backbutton
137 /* Test Posts */
138 foreach($_POST as $key => $val){
139 // Post for delete
140 if(preg_match("/appl_del.*/",$key)){
141 $s_action = "del";
142 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
143 // Post for edit
144 }elseif(preg_match("/appl_edit_.*/",$key)){
145 $s_action="edit";
146 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
147 // Post for new
148 }elseif(preg_match("/^copy_.*/",$key)){
149 $s_action="copy";
150 $s_entry = preg_replace("/^copy_/i","",$key);
151 }elseif(preg_match("/^cut_.*/",$key)){
152 $s_action="cut";
153 $s_entry = preg_replace("/^cut_/i","",$key);
154 // Post for new
155 }elseif(preg_match("/^appl_new.*/",$key)){
156 $s_action="new";
157 }elseif(preg_match("/^remove_multiple_applications/",$key)){
158 $s_action="del_multiple";
159 }elseif(preg_match("/^editPaste.*/i",$key)){
160 $s_action="editPaste";
161 }elseif(preg_match("/^multiple_copy_groups/",$key)){
162 $s_action = "copy_multiple";
163 }elseif(preg_match("/^multiple_cut_groups/",$key)){
164 $s_action = "cut_multiple";
165 }
166 }
168 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
169 $s_action ="edit";
170 $s_entry = $_GET['id'];
171 }
173 $s_entry = preg_replace("/_.$/","",$s_entry);
176 /* handle C&P from layers menu */
177 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
178 $s_action = "copy_multiple";
179 }
180 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
181 $s_action = "cut_multiple";
182 }
183 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
184 $s_action = "editPaste";
185 }
187 /* Create options */
188 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
189 $s_action = "new";
190 }
192 /* handle remove from layers menu */
193 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
194 $s_action = "del_multiple";
195 }
197 /****************
198 Copy & Paste handling
199 ****************/
201 /* Display the copy & paste dialog, if it is currently open */
202 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
203 if($ret){
204 return($ret);
205 }
207 /****************
208 Create a new app
209 ****************/
211 /* New application? */
212 if ($s_action=="new"){
214 /* By default we set 'dn' to 'new', all relevant plugins will
215 react on this. */
216 $this->dn= "new";
218 /* Create new usertab object */
219 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
220 $this->apptabs->parent = &$this;
221 $this->apptabs->set_acl_base($this->app_base);
222 }
225 /****************
226 Edit entry canceled
227 ****************/
229 /* Cancel dialogs */
230 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
231 del_lock ($this->apptabs->dn);
232 unset ($this->apptabs);
233 $this->apptabs= NULL;
234 session::un_set('objectinfo');
235 }
238 /****************
239 Edit entry finished
240 ****************/
242 /* Finish apps edit is triggered by the tabulator dialog, so
243 the user wants to save edited data. Check and save at this
244 point. */
245 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
247 /* Check tabs, will feed message array */
248 $this->apptabs->last= $this->apptabs->current;
249 $this->apptabs->save_object();
250 $message= $this->apptabs->check();
252 /* Save, or display error message? */
253 if (count($message) == 0){
255 /* Save data data to ldap */
256 $this->apptabs->save();
258 if (!isset($_POST['edit_apply'])){
259 /* Application has been saved successfully, remove lock from
260 LDAP. */
261 if ($this->dn != "new"){
262 del_lock ($this->dn);
263 }
264 unset ($this->apptabs);
265 $this->apptabs= NULL;
266 session::un_set('objectinfo');
267 }else{
269 /* Reinitialize tab */
270 if($this->apptabs instanceof tabs){
271 $this->apptabs->re_init();
272 }
273 }
274 } else {
275 /* Ok. There seem to be errors regarding to the tab data,
276 show message and continue as usual. */
277 msg_dialog::displayChecks($message);
278 }
279 }
282 /****************
283 Edit entry
284 ****************/
286 /* User wants to edit data? */
287 if (($s_action=="edit") && (!isset($this->apptabs->config))){
289 /* Get 'dn' from posted 'applist', must be unique */
290 $this->dn= $this->applications[$s_entry]['dn'];
292 /* Check locking, save current plugin in 'back_plugin', so
293 the dialog knows where to return. */
294 if (($user= get_lock($this->dn)) != ""){
295 return(gen_locked_message ($user, $this->dn));
296 }
298 /* Lock the current entry, so everyone will get the
299 above dialog */
300 add_lock ($this->dn, $this->ui->dn);
302 /* Register apptabs to trigger edit dialog */
303 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
304 $this->apptabs->parent = &$this;
305 $this->apptabs->set_acl_base($this->dn);
306 session::set('objectinfo',$this->dn);
307 }
311 /********************
312 Delete MULTIPLE entries requested, display confirm dialog
313 ********************/
315 if ($s_action=="del_multiple"){
316 $ids = $this->list_get_selected_items();
318 if(count($ids)){
320 foreach($ids as $id){
321 $dn = $this->applications[$id]['dn'];
322 $this->dns[$id] = $dn;
323 }
324 if ($user= get_multiple_locks($this->dns)){
325 return(gen_locked_message($user,$this->dns));
326 }
328 $dns_names = array();
329 foreach($this->dns as $dn){
330 add_lock ($dn, $this->ui->dn);
331 $dns_names[] =@LDAP::fix($dn);
332 }
334 /* Lock the current entry, so nobody will edit it during deletion */
335 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
336 $smarty->assign("multiple", true);
337 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
338 }
339 }
342 /********************
343 Delete MULTIPLE entries confirmed
344 ********************/
346 /* Confirmation for deletion has been passed. Users should be deleted. */
347 if (isset($_POST['delete_multiple_application_confirm'])){
349 /* Remove user by user and check acls before removeing them */
350 foreach($this->dns as $key => $dn){
352 $ui = get_userinfo();
353 $acl = $ui->get_permissions($dn ,"application/application");
354 if (preg_match('/d/', $acl)){
356 /* Delete request is permitted, perform LDAP action */
357 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
358 $this->apptabs->parent = &$this;
359 $this->apptabs->set_acl_base($dn);
360 $this->apptabs->delete ();
361 unset ($this->apptabs);
362 $this->apptabs= NULL;
364 } else {
365 /* Normally this shouldn't be reached, send some extra
366 logs to notify the administrator */
367 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
368 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
369 }
370 /* Remove lock file after successfull deletion */
371 del_lock ($dn);
372 unset($this->dns[$key]);
373 }
374 }
377 /********************
378 Delete MULTIPLE entries Canceled
379 ********************/
381 /* Remove lock */
382 if(isset($_POST['delete_multiple_application_cancel'])){
383 foreach($this->dns as $key => $dn){
384 del_lock ($dn);
385 unset($this->dns[$key]);
386 }
387 }
389 /****************
390 Delete app
391 ****************/
393 /* Remove user was requested */
394 if ($s_action == "del"){
396 /* Get 'dn' from posted 'uid' */
397 $this->dn= $this->applications[$s_entry]['dn'];
399 /* Load permissions for selected 'dn' and check if
400 we're allowed to remove this 'dn' */
401 $ui = get_userinfo();
402 $acl = $ui->get_permissions($this->dn ,"application/application");
404 if(preg_match("/d/",$acl)){
405 /* Check locking, save current plugin in 'back_plugin', so
406 the dialog knows where to return. */
407 if (($user= get_lock($this->dn)) != ""){
408 return (gen_locked_message ($user, $this->dn));
409 }
411 /* Lock the current entry, so nobody will edit it during deletion */
412 add_lock ($this->dn, $this->ui->dn);
413 $smarty= get_smarty();
414 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
415 $smarty->assign("multiple", false);
416 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
417 } else {
419 /* Obviously the user isn't allowed to delete. Show message and
420 clean session. */
421 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
422 }
423 }
426 /****************
427 Delete app confirmed
428 ****************/
430 /* Confirmation for deletion has been passed. Group should be deleted. */
431 if (isset($_POST['delete_app_confirm'])){
433 /* Some nice guy may send this as POST, so we've to check
434 for the permissions again. */
435 $ui = get_userinfo();
436 $acl = $ui->get_permissions($this->dn ,"application/application");
438 if(preg_match("/d/",$acl)){
440 /* Delete request is permitted, perform LDAP action */
441 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
442 $this->apptabs->parent = &$this;
443 $this->apptabs->set_acl_base($this->dn);
444 $this->apptabs->delete ();
445 unset ($this->apptabs);
446 $this->apptabs= NULL;
448 } else {
450 /* Normally this shouldn't be reached, send some extra
451 logs to notify the administrator */
452 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
453 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
454 }
456 /* Remove lock file after successfull deletion */
457 del_lock ($this->dn);
458 }
461 /****************
462 Delete app canceled
463 ****************/
465 /* Delete application canceled? */
466 if (isset($_POST['delete_cancel'])){
467 del_lock ($this->dn);
468 session::un_set('objectinfo');
469 }
471 /* Show tab dialog if object is present */
472 if (($this->apptabs) && (isset($this->apptabs->config))){
473 $display= $this->apptabs->execute();
475 /* Don't show buttons if tab dialog requests this */
476 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
477 $display.= "<p style=\"text-align:right\">\n";
478 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
479 $display.= " \n";
480 if ($this->dn != "new"){
481 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
482 $display.= " \n";
483 }
484 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
485 $display.= "</p>";
486 }
487 return ($display);
488 }
491 /****************
492 Dialog display
493 ****************/
495 /* Check if there is a snapshot dialog open */
496 if($this->IsReleaseManagementActivated()){
497 $base = $this->app_release;
498 }else{
499 $base = $this->app_base;
500 }
501 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
502 return($str);
503 }
505 /* Display dialog with system list */
506 $this->DivListApplication->parent = $this;
507 $this->DivListApplication->execute();
508 if(!$this->IsReleaseManagementActivated()){
509 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
510 }
511 $this->reload();
512 $this->DivListApplication->setEntries($this->applications);
513 return($this->DivListApplication->Draw());
514 }
517 /* Return departments, that will be included within snapshot detection */
518 function get_used_snapshot_bases()
519 {
520 if($this->IsReleaseManagementActivated()){
521 return(array($this->app_release));
522 }else{
523 return(array($this->app_base));
524 }
525 }
528 function reload()
529 {
530 $this->applications= array();
532 /* Set base for all searches */
533 $Regex = $this->DivListApplication->Regex;
534 $SubSearch = $this->DivListApplication->SubSearch;
535 $Flags = GL_NONE | GL_SIZELIMIT;
536 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
537 $tmp = array();
539 if(!$this->IsReleaseManagementActivated()){
540 $use_base = $this->app_base;
541 if($SubSearch){
542 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
543 }
544 }else{
545 $use_base = $this->app_release;
546 $SubSearch= FALSE;
547 }
549 if($SubSearch){
550 $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
551 }else{
552 $res= get_list($Filter, "application",$use_base, array("cn","description","dn","objectClass"), $Flags);
553 }
554 $tmp2 = array();
555 foreach ($res as $val){
556 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
557 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
558 }
560 natcasesort($tmp2);
561 $this->applications=array();
562 foreach($tmp2 as $val){
563 $this->applications[]=$tmp[$val];
564 }
565 reset ($this->applications);
566 }
568 function remove_from_parent()
569 {
570 /* Optionally execute a command after we're done */
571 $this->postremove();
572 }
575 function copyPasteHandling_from_queue($s_action,$s_entry)
576 {
577 /* Check if Copy & Paste is disabled */
578 if(!is_object($this->CopyPasteHandler)){
579 return("");
580 }
582 /* Add a single entry to queue */
583 if($s_action == "cut" || $s_action == "copy"){
585 /* Cleanup object queue */
586 $this->CopyPasteHandler->cleanup_queue();
587 $dn = $this->applications[$s_entry]['dn'];
588 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
589 }
592 /* Add entries to queue */
593 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
595 /* Cleanup object queue */
596 $this->CopyPasteHandler->cleanup_queue();
598 /* Add new entries to CP queue */
599 foreach($this->list_get_selected_items() as $id){
600 $dn = $this->applications[$id]['dn'];
602 if($s_action == "copy_multiple"){
603 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
604 }
605 if($s_action == "cut_multiple"){
606 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
607 }
608 }
609 }
611 /* Start pasting entries */
612 if($s_action == "editPaste"){
613 $this->start_pasting_copied_objects = TRUE;
614 }
617 /* Return C&P dialog */
618 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
621 /* Get dialog */
622 $this->CopyPasteHandler->SetVar("base",$this->app_base);
623 $this->CopyPasteHandler->SetVar("parent",$this);
624 $data = $this->CopyPasteHandler->execute();
626 /* Return dialog data */
627 if(!empty($data)){
628 return($data);
629 }
630 }
632 /* Automatically disable status for pasting */
633 if(!$this->CopyPasteHandler->entries_queued()){
634 $this->start_pasting_copied_objects = FALSE;
635 }
636 return("");
637 }
640 function list_get_selected_items()
641 {
642 $ids = array();
643 foreach($_POST as $name => $value){
644 if(preg_match("/^item_selected_[0-9]*$/",$name)){
645 $id = preg_replace("/^item_selected_/","",$name);
646 $ids[$id] = $id;
647 }
648 }
649 return($ids);
650 }
653 /* Save to LDAP */
654 function save()
655 {
656 /* Optionally execute a command after we're done */
657 $this->postcreate();
658 }
660 function remove_lock()
661 {
662 if (isset($this->apptabs->dn)){
663 del_lock ($this->apptabs->dn);
664 }
665 }
667 function save_object()
668 {
669 $this->DivListApplication->save_object();
670 if(is_object($this->CopyPasteHandler)){
671 $this->CopyPasteHandler->save_object();
672 }
674 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
675 $sel_rel = get_post('app_release');
676 $releases = array_flip($this->getReleases());
677 if(isset($releases[$sel_rel])){
678 $this->app_release = $releases[$sel_rel];
679 }
680 $app_filter = session::get("app_filter");
681 $app_filter['app_release'] = $this->app_release;
682 session::set("app_filter",$app_filter);
683 }elseif(!$this->IsReleaseManagementActivated()){
684 $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
685 $app_filter = session::get("app_filter");
686 $app_filter['app_base'] = $this->app_base;
687 session::set("app_filter",$app_filter);
688 }
689 }
691 function check() {}
692 function adapt_from_template($dn, $skip= array()) {}
693 function password_change_needed() {}
694 }
695 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
696 ?>