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 if (count($this->dns) == 1){
287 $smarty->assign("intro", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
288 } else {
289 $smarty->assign("intro", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
290 }
291 $smarty->assign("multiple", true);
292 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
293 }
294 }
297 /********************
298 Delete MULTIPLE entries confirmed
299 ********************/
301 /* Confirmation for deletion has been passed. Users should be deleted. */
302 if (isset($_POST['delete_multiple_application_confirm'])){
304 /* Remove user by user and check acls before removeing them */
305 foreach($this->dns as $key => $dn){
307 $ui = get_userinfo();
308 $acl = $ui->get_permissions($dn ,"application/application");
309 if (preg_match('/d/', $acl)){
311 /* Delete request is permitted, perform LDAP action */
312 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
313 $this->apptabs->set_acl_base($dn);
314 $this->apptabs->delete ();
315 gosa_log ("Application object'".$dn."' has been removed");
316 unset ($this->apptabs);
317 $this->apptabs= NULL;
319 } else {
320 /* Normally this shouldn't be reached, send some extra
321 logs to notify the administrator */
322 print_red (_("You are not allowed to delete this application!"));
323 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
324 }
325 /* Remove lock file after successfull deletion */
326 del_lock ($dn);
327 unset($this->dns[$key]);
328 }
329 }
332 /********************
333 Delete MULTIPLE entries Canceled
334 ********************/
336 /* Remove lock */
337 if(isset($_POST['delete_multiple_application_cancel'])){
338 foreach($this->dns as $key => $dn){
339 del_lock ($dn);
340 unset($this->dns[$key]);
341 }
342 }
344 /****************
345 Delete app
346 ****************/
348 /* Remove user was requested */
349 if ($s_action == "del"){
351 /* Get 'dn' from posted 'uid' */
352 $this->dn= $this->applications[$s_entry]['dn'];
354 /* Load permissions for selected 'dn' and check if
355 we're allowed to remove this 'dn' */
356 $ui = get_userinfo();
357 $acl = $ui->get_permissions($this->dn ,"application/application");
359 if(preg_match("/d/",$acl)){
360 /* Check locking, save current plugin in 'back_plugin', so
361 the dialog knows where to return. */
362 if (($user= get_lock($this->dn)) != ""){
363 return (gen_locked_message ($user, $this->dn));
364 }
366 /* Lock the current entry, so nobody will edit it during deletion */
367 add_lock ($this->dn, $this->ui->dn);
368 $smarty= get_smarty();
369 $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
370 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
371 } else {
373 /* Obviously the user isn't allowed to delete. Show message and
374 clean session. */
375 print_red (_("You are not allowed to delete this application!"));
376 }
377 }
380 /****************
381 Delete app confirmed
382 ****************/
384 /* Confirmation for deletion has been passed. Group should be deleted. */
385 if (isset($_POST['delete_app_confirm'])){
387 /* Some nice guy may send this as POST, so we've to check
388 for the permissions again. */
389 $ui = get_userinfo();
390 $acl = $ui->get_permissions($this->dn ,"application/application");
392 if(preg_match("/d/",$acl)){
394 /* Delete request is permitted, perform LDAP action */
395 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
396 $this->apptabs->set_acl_base($this->dn);
397 $this->apptabs->delete ();
398 gosa_log ("Application object'".$this->dn."' has been removed");
399 unset ($this->apptabs);
400 $this->apptabs= NULL;
402 } else {
404 /* Normally this shouldn't be reached, send some extra
405 logs to notify the administrator */
406 print_red (_("You are not allowed to delete this application!"));
407 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
408 }
410 /* Remove lock file after successfull deletion */
411 del_lock ($this->dn);
412 }
415 /****************
416 Delete app canceled
417 ****************/
419 /* Delete application canceled? */
420 if (isset($_POST['delete_cancel'])){
421 del_lock ($this->dn);
422 unset($_SESSION['objectinfo']);
423 }
425 /* Show tab dialog if object is present */
426 if (($this->apptabs) && (isset($this->apptabs->config))){
427 $display= $this->apptabs->execute();
429 /* Don't show buttons if tab dialog requests this */
430 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
431 $display.= "<p style=\"text-align:right\">\n";
432 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
433 $display.= " \n";
434 if ($this->dn != "new"){
435 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
436 $display.= " \n";
437 }
438 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
439 $display.= "</p>";
440 }
441 return ($display);
442 }
445 /****************
446 Dialog display
447 ****************/
449 /* Check if there is a snapshot dialog open */
450 $base = $this->DivListApplication->selectedBase;
451 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
452 return($str);
453 }
455 /* Display dialog with system list */
456 $this->DivListApplication->parent = $this;
457 $this->DivListApplication->execute();
458 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
459 $this->reload();
460 $this->DivListApplication->setEntries($this->applications);
461 return($this->DivListApplication->Draw());
462 }
465 /* Return departments, that will be included within snapshot detection */
466 function get_used_snapshot_bases(){
467 if($this->DivListApplication->selectedRelease == "main"){
468 return(array($this->DivListApplication->selectedBase));
469 }else{
470 return(array($this->DivListApplication->selectedRelease));
471 }
472 }
475 function reload()
476 {
477 $this->applications= array();
479 /* Set base for all searches */
480 $base = $this->DivListApplication->selectedBase;
481 $release = $this->DivListApplication->selectedRelease;
482 $Regex = $this->DivListApplication->Regex;
483 $SubSearch = $this->DivListApplication->SubSearch;
484 $Flags = GL_NONE | GL_SIZELIMIT;
485 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
486 $tmp = array();
487 $Releases = $this->getReleases($base);
489 if(!$this->enableReleaseManagement){
490 $use_base = "ou=apps,".$base;
491 }else{
492 if(isset($Releases[$release])){
493 $use_base = $release;
494 }else{
495 $use_base = "ou=apps,".$base;
496 }
497 }
499 if($SubSearch){
500 $Flags |= GL_SUBSEARCH;
501 }
503 $res= get_list($Filter, "application", $use_base, array("cn","description","dn","objectClass"), $Flags);
504 foreach ($res as $val){
505 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
506 }
508 ksort($tmp);
509 $this->applications=array();
510 foreach($tmp as $val){
511 $this->applications[]=$val;
512 }
513 reset ($this->applications);
514 }
516 function remove_from_parent()
517 {
518 /* Optionally execute a command after we're done */
519 $this->postremove();
520 }
523 function copyPasteHandling($s_action,$s_entry)
524 {
525 /* Paste copied/cutted object in here
526 */
527 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
528 $this->CopyPasteHandler->save_object();
529 $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
531 if($str = $this->CopyPasteHandler->execute()) {
532 return($str);
533 }
534 }
536 /* Copy current object to CopyHandler
537 */
538 if($s_action == "copy"){
539 $this->CopyPasteHandler->Clear();
540 $dn = $this->applications[$s_entry]['dn'];
542 /* Check Acls */
543 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
544 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
545 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
546 $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new","application");
547 $this->CopyPasteHandler->Copy($obj,$objNew);
548 }
549 }
551 /* Copy current object to CopyHandler
552 */
553 if($s_action == "cut"){
554 $this->CopyPasteHandler->Clear();
555 $dn = $this->applications[$s_entry]['dn'];
557 /* Check Acls */
558 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
559 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
560 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
561 $this->CopyPasteHandler->Cut($obj);
562 }
563 }
564 }
566 function list_get_selected_items()
567 {
568 $ids = array();
569 foreach($_POST as $name => $value){
570 if(preg_match("/^item_selected_[0-9]*$/",$name)){
571 $id = preg_replace("/^item_selected_/","",$name);
572 $ids[$id] = $id;
573 }
574 }
575 return($ids);
576 }
579 /* Save to LDAP */
580 function save()
581 {
582 /* Optionally execute a command after we're done */
583 $this->postcreate();
584 }
586 function remove_lock()
587 {
588 if (isset($this->apptabs->dn)){
589 del_lock ($this->apptabs->dn);
590 }
591 }
593 function save_object() {
594 $this->DivListApplication->save_object();
595 }
597 function check() {}
598 function adapt_from_template($dn) {}
599 function password_change_needed() {}
600 }
601 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
602 ?>