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();
214 if (!isset($_POST['edit_apply'])){
215 /* Application has been saved successfully, remove lock from
216 LDAP. */
217 if ($this->dn != "new"){
218 del_lock ($this->dn);
219 }
220 unset ($this->apptabs);
221 $this->apptabs= NULL;
222 unset ($_SESSION['objectinfo']);
223 }
224 } else {
225 /* Ok. There seem to be errors regarding to the tab data,
226 show message and continue as usual. */
227 show_errors($message);
228 }
229 }
232 /****************
233 Edit entry
234 ****************/
236 /* User wants to edit data? */
237 if (($s_action=="edit") && (!isset($this->apptabs->config))){
239 /* Get 'dn' from posted 'applist', must be unique */
240 $this->dn= $this->applications[$s_entry]['dn'];
242 /* Check locking, save current plugin in 'back_plugin', so
243 the dialog knows where to return. */
244 if (($user= get_lock($this->dn)) != ""){
245 return(gen_locked_message ($user, $this->dn));
246 }
248 /* Lock the current entry, so everyone will get the
249 above dialog */
250 add_lock ($this->dn, $this->ui->dn);
252 /* Register apptabs to trigger edit dialog */
253 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
254 $this->apptabs->set_acl_base($this->dn);
255 $_SESSION['objectinfo']= $this->dn;
256 }
260 /********************
261 Delete MULTIPLE entries requested, display confirm dialog
262 ********************/
264 if ($s_action=="del_multiple"){
265 $ids = $this->list_get_selected_items();
267 if(count($ids)){
269 foreach($ids as $id){
270 $dn = $this->applications[$id]['dn'];
271 if (($user= get_lock($dn)) != ""){
272 return(gen_locked_message ($user, $dn));
273 }
274 $this->dns[$id] = $dn;
275 }
277 $dns_names = "<br><pre>";
278 foreach($this->dns as $dn){
279 add_lock ($dn, $this->ui->dn);
280 $dns_names .= $dn."\n";
281 }
282 $dns_names .="</pre>";
284 /* Lock the current entry, so nobody will edit it during deletion */
285 if (count($this->dns) == 1){
286 $smarty->assign("intro", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
287 } else {
288 $smarty->assign("intro", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
289 }
290 $smarty->assign("multiple", true);
291 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
292 }
293 }
296 /********************
297 Delete MULTIPLE entries confirmed
298 ********************/
300 /* Confirmation for deletion has been passed. Users should be deleted. */
301 if (isset($_POST['delete_multiple_application_confirm'])){
303 /* Remove user by user and check acls before removeing them */
304 foreach($this->dns as $key => $dn){
306 $ui = get_userinfo();
307 $acl = $ui->get_permissions($dn ,"application/application");
308 if (preg_match('/d/', $acl)){
310 /* Delete request is permitted, perform LDAP action */
311 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
312 $this->apptabs->set_acl_base($dn);
313 $this->apptabs->delete ();
314 unset ($this->apptabs);
315 $this->apptabs= NULL;
317 } else {
318 /* Normally this shouldn't be reached, send some extra
319 logs to notify the administrator */
320 print_red (_("You are not allowed to delete this application!"));
321 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
322 }
323 /* Remove lock file after successfull deletion */
324 del_lock ($dn);
325 unset($this->dns[$key]);
326 }
327 }
330 /********************
331 Delete MULTIPLE entries Canceled
332 ********************/
334 /* Remove lock */
335 if(isset($_POST['delete_multiple_application_cancel'])){
336 foreach($this->dns as $key => $dn){
337 del_lock ($dn);
338 unset($this->dns[$key]);
339 }
340 }
342 /****************
343 Delete app
344 ****************/
346 /* Remove user was requested */
347 if ($s_action == "del"){
349 /* Get 'dn' from posted 'uid' */
350 $this->dn= $this->applications[$s_entry]['dn'];
352 /* Load permissions for selected 'dn' and check if
353 we're allowed to remove this 'dn' */
354 $ui = get_userinfo();
355 $acl = $ui->get_permissions($this->dn ,"application/application");
357 if(preg_match("/d/",$acl)){
358 /* Check locking, save current plugin in 'back_plugin', so
359 the dialog knows where to return. */
360 if (($user= get_lock($this->dn)) != ""){
361 return (gen_locked_message ($user, $this->dn));
362 }
364 /* Lock the current entry, so nobody will edit it during deletion */
365 add_lock ($this->dn, $this->ui->dn);
366 $smarty= get_smarty();
367 $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
368 $smarty->assign("multiple", false);
369 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
370 } else {
372 /* Obviously the user isn't allowed to delete. Show message and
373 clean session. */
374 print_red (_("You are not allowed to delete this application!"));
375 }
376 }
379 /****************
380 Delete app confirmed
381 ****************/
383 /* Confirmation for deletion has been passed. Group should be deleted. */
384 if (isset($_POST['delete_app_confirm'])){
386 /* Some nice guy may send this as POST, so we've to check
387 for the permissions again. */
388 $ui = get_userinfo();
389 $acl = $ui->get_permissions($this->dn ,"application/application");
391 if(preg_match("/d/",$acl)){
393 /* Delete request is permitted, perform LDAP action */
394 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
395 $this->apptabs->set_acl_base($this->dn);
396 $this->apptabs->delete ();
397 unset ($this->apptabs);
398 $this->apptabs= NULL;
400 } else {
402 /* Normally this shouldn't be reached, send some extra
403 logs to notify the administrator */
404 print_red (_("You are not allowed to delete this application!"));
405 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
406 }
408 /* Remove lock file after successfull deletion */
409 del_lock ($this->dn);
410 }
413 /****************
414 Delete app canceled
415 ****************/
417 /* Delete application canceled? */
418 if (isset($_POST['delete_cancel'])){
419 del_lock ($this->dn);
420 unset($_SESSION['objectinfo']);
421 }
423 /* Show tab dialog if object is present */
424 if (($this->apptabs) && (isset($this->apptabs->config))){
425 $display= $this->apptabs->execute();
427 /* Don't show buttons if tab dialog requests this */
428 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
429 $display.= "<p style=\"text-align:right\">\n";
430 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
431 $display.= " \n";
432 if ($this->dn != "new"){
433 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
434 $display.= " \n";
435 }
436 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
437 $display.= "</p>";
438 }
439 return ($display);
440 }
443 /****************
444 Dialog display
445 ****************/
447 /* Check if there is a snapshot dialog open */
448 $base = $this->DivListApplication->selectedBase;
449 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
450 return($str);
451 }
453 /* Display dialog with system list */
454 $this->DivListApplication->parent = $this;
455 $this->DivListApplication->execute();
456 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
457 $this->reload();
458 $this->DivListApplication->setEntries($this->applications);
459 return($this->DivListApplication->Draw());
460 }
463 /* Return departments, that will be included within snapshot detection */
464 function get_used_snapshot_bases(){
465 if($this->DivListApplication->selectedRelease == "main"){
466 return(array($this->DivListApplication->selectedBase));
467 }else{
468 return(array($this->DivListApplication->selectedRelease));
469 }
470 }
473 function reload()
474 {
475 $this->applications= array();
477 /* Set base for all searches */
478 $base = $this->DivListApplication->selectedBase;
479 $release = $this->DivListApplication->selectedRelease;
480 $Regex = $this->DivListApplication->Regex;
481 $SubSearch = $this->DivListApplication->SubSearch;
482 $Flags = GL_NONE | GL_SIZELIMIT;
483 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
484 $tmp = array();
485 $Releases = $this->getReleases($base);
487 if(!$this->enableReleaseManagement){
488 $use_base = "ou=apps,".$base;
489 }else{
490 if(isset($Releases[$release])){
491 $use_base = $release;
492 }else{
493 $use_base = "ou=apps,".$base;
494 }
495 }
497 if($SubSearch){
498 $Flags |= GL_SUBSEARCH;
499 }
501 $res= get_list($Filter, "application", $use_base, array("cn","description","dn","objectClass"), $Flags);
502 foreach ($res as $val){
503 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
504 }
506 ksort($tmp);
507 $this->applications=array();
508 foreach($tmp as $val){
509 $this->applications[]=$val;
510 }
511 reset ($this->applications);
512 }
514 function remove_from_parent()
515 {
516 /* Optionally execute a command after we're done */
517 $this->postremove();
518 }
521 function copyPasteHandling($s_action,$s_entry)
522 {
523 /* Paste copied/cutted object in here
524 */
525 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
526 $this->CopyPasteHandler->save_object();
527 $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
529 if($str = $this->CopyPasteHandler->execute()) {
530 return($str);
531 }
532 }
534 /* Copy current object to CopyHandler
535 */
536 if($s_action == "copy"){
537 $this->CopyPasteHandler->Clear();
538 $dn = $this->applications[$s_entry]['dn'];
540 /* Check Acls */
541 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
542 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
543 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
544 $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new","application");
545 $this->CopyPasteHandler->Copy($obj,$objNew);
546 }
547 }
549 /* Copy current object to CopyHandler
550 */
551 if($s_action == "cut"){
552 $this->CopyPasteHandler->Clear();
553 $dn = $this->applications[$s_entry]['dn'];
555 /* Check Acls */
556 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
557 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
558 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
559 $this->CopyPasteHandler->Cut($obj);
560 }
561 }
562 }
564 function list_get_selected_items()
565 {
566 $ids = array();
567 foreach($_POST as $name => $value){
568 if(preg_match("/^item_selected_[0-9]*$/",$name)){
569 $id = preg_replace("/^item_selected_/","",$name);
570 $ids[$id] = $id;
571 }
572 }
573 return($ids);
574 }
577 /* Save to LDAP */
578 function save()
579 {
580 /* Optionally execute a command after we're done */
581 $this->postcreate();
582 }
584 function remove_lock()
585 {
586 if (isset($this->apptabs->dn)){
587 del_lock ($this->apptabs->dn);
588 }
589 }
591 function save_object() {
592 $this->DivListApplication->save_object();
593 }
595 function check() {}
596 function adapt_from_template($dn) {}
597 function password_change_needed() {}
598 }
599 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
600 ?>