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 goFonMacro extends plugin
22 {
23 /* Definitions */
24 var $plHeadline = "Phone macros";
25 var $plDescription = "This does something";
27 /* Dialog attributes */
28 var $macrotabs = NULL;
29 var $macros = array();
30 var $ui = NULL;
31 var $DivListMacro = NULL;
33 var $CopyPasteHandler = NULL;
34 var $start_pasting_copied_objects = FALSE;
36 function gofonMacro(&$config, $ui)
37 {
38 /* Save configuration for internal use */
39 $this->config= $config;
40 $this->ui= $ui;
42 $this->DivListMacro = new divListMacro($this->config,$this);
44 /* Copy & Paste enabled ?*/
45 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
46 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
47 }
48 }
50 function execute()
51 {
52 /* Call parent execute */
53 plugin::execute();
55 session::set('LOCK_VARS_TO_USE',array("/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/"));
57 /*****************
58 Variable initialisation
59 *****************/
61 $s_action = ""; // Contains the action to proceed
62 $s_entry = ""; // The value for s_action
63 $base_back = ""; // The Link for Backbutton
64 $smarty= get_smarty();
67 /*****************
68 Check Posts
69 *****************/
71 /* Test Posts */
72 foreach($_POST as $key => $val){
73 // Post for delete
74 if(preg_match("/^goFonMacro_del/",$key)){
75 $s_action = "del";
76 $s_entry = preg_replace("/^goFonMacro_del_/i","",$key);
77 // Post for edit
78 }elseif(preg_match("/^goFonMacro_edit_/",$key)){
79 $s_action="edit";
80 $s_entry = preg_replace("/^goFonMacro_edit_/i","",$key);
81 // Post for new
82 }elseif(preg_match("/^goFonMacro_new/",$key)){
83 $s_action="new";
84 }elseif(preg_match("/^remove_multiple_macros/",$key)){
85 $s_action="del_multiple";
86 }elseif(preg_match("/^editPaste.*/i",$key)){
87 $s_action="editPaste";
88 }elseif(preg_match("/^copy_.*/",$key)){
89 $s_action="copy";
90 $s_entry = preg_replace("/^copy_/i","",$key);
91 }elseif(preg_match("/^cut_.*/",$key)){
92 $s_action="cut";
93 $s_entry = preg_replace("/^cut_/i","",$key);
94 }elseif(preg_match("/^multiple_copy_objects/",$key)){
95 $s_action = "copy_multiple";
96 }elseif(preg_match("/^multiple_cut_objects/",$key)){
97 $s_action = "cut_multiple";
98 }
99 }
100 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
101 $s_action ="edit";
102 $s_entry = $_GET['id'];
103 }
104 $s_entry = preg_replace("/_.$/","",$s_entry);
107 /********************
108 Copy & Paste Handling ...
109 ********************/
112 /* handle C&P from layers menu */
113 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
114 $s_action = "copy_multiple";
115 }
116 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
117 $s_action = "cut_multiple";
118 }
119 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
120 $s_action = "editPaste";
121 }
123 /* Create options */
124 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "goFonMacro_new"){
125 $s_action = "new";
126 }
128 /* handle remove from layers menu */
129 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
130 $s_action = "del_multiple";
131 }
133 /* Display the copy & paste dialog, if it is currently open */
134 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
135 if($ret){
136 return($ret);
137 }
140 /*****************
141 Create a new Macro
142 *****************/
144 /* New macro? */
145 if ($s_action=="new"){
147 /* By default we set 'dn' to 'new', all relevant plugins will
148 react on this. */
149 $this->dn= "new";
151 /* Create new usertab object */
152 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
153 $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
154 }
156 /*****************
157 Edit || Password canceled
158 *****************/
160 /* Cancel dialogs */
161 if (isset($_POST['edit_cancel'])){
162 del_lock ($this->macrotabs->dn);
163 unset ($this->macrotabs);
164 $this->macrotabs= NULL;
165 session::un_set('objectinfo');
166 }
169 /*****************
170 Edit finised
171 *****************/
173 /* Finish mac edit is triggered by the tabulator dialog, so
174 the user wants to save edited data. Check and save at this
175 point. */
176 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
178 /* Check tabs, will feed message array */
179 $this->macrotabs->last= $this->macrotabs->current;
180 $this->macrotabs->save_object();
181 $message= $this->macrotabs->check();
183 /* Save, or display error message? */
184 if (count($message) == 0){
186 /* Save data data to ldap */
187 $this->macrotabs->save();
189 if (!isset($_POST['edit_apply'])){
190 /* macro has been saved successfully, remove lock from
191 LDAP. */
192 if ($this->dn != "new"){
193 del_lock ($this->dn);
194 }
196 unset ($this->macrotabs);
197 $this->macrotabs= NULL;
198 session::un_set('objectinfo');
199 }
200 } else {
201 /* Ok. There seem to be errors regarding to the tab data,
202 show message and continue as usual. */
203 show_errors($message);
204 }
205 }
208 /*****************
209 Edit macro
210 *****************/
212 /* User wants to edit data */
213 if (($s_action=="edit") && (!isset($this->macrotabs->config))){
215 $dn = $this->macros[$s_entry]['dn'];
216 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
217 if(preg_match("/r/",$acl)){
218 $this->dn = $dn;
220 /* Check locking, save current plugin in 'back_plugin', so
221 the dialog knows where to return. */
222 if (($user= get_lock($this->dn)) != ""){
223 return(gen_locked_message ($user, $this->dn));
224 }
226 /* Lock the current entry, so everyone will get the
227 above dialog */
228 add_lock ($this->dn, $this->ui->dn);
229 /* Register macrotabs to trigger edit dialog */
230 $this->macrotabs= new macrotabs($this->config,
231 $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
232 session::set('objectinfo',$this->dn);
233 }
234 }
238 /********************
239 Delete MULTIPLE entries requested, display confirm dialog
240 ********************/
242 if ($s_action=="del_multiple"){
243 $ids = $this->list_get_selected_items();
245 if(count($ids)){
247 foreach($ids as $id){
248 $dn = $this->macros[$id]['dn'];
249 if (($user= get_lock($dn)) != ""){
250 return(gen_locked_message ($user, $dn));
251 }
252 $this->dns[$id] = $dn;
253 }
255 $dns_names = "<br><pre>";
256 foreach($this->dns as $dn){
257 add_lock ($dn, $this->ui->dn);
258 $dns_names .= $dn."\n";
259 }
260 $dns_names .="</pre>";
262 /* Lock the current entry, so nobody will edit it during deletion */
263 $smarty->assign("intro", sprintf(_("You're about to delete the following macro(s) %s"), @LDAP::fix($dns_names)));
264 $smarty->assign("multiple", true);
265 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
266 }
267 }
270 /********************
271 Delete MULTIPLE entries confirmed
272 ********************/
274 /* Confirmation for deletion has been passed. Users should be deleted. */
275 if (isset($_POST['delete_multiple_macro_confirm'])){
277 /* Remove user by user and check acls before removeing them */
278 foreach($this->dns as $key => $dn){
280 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
281 if(preg_match("/d/",$acl)){
283 /* Delete request is permitted, perform LDAP action */
284 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
285 $this->macrotabs->delete ();
286 unset ($this->macrotabs);
287 $this->macrotabs= NULL;
289 } else {
290 msg_dialog::display(_("Permission error"), _("You have not permission to delete this entry!"), ERROR_DIALOG);
291 }
293 /* Remove lock file after successfull deletion */
294 del_lock ($dn);
295 unset($this->dns[$key]);
296 }
297 }
300 /********************
301 Delete MULTIPLE entries Canceled
302 ********************/
304 /* Remove lock */
305 if(isset($_POST['delete_multiple_macro_cancel'])){
306 foreach($this->dns as $key => $dn){
307 del_lock ($dn);
308 unset($this->dns[$key]);
309 }
310 }
313 /*****************
314 Remove macro
315 *****************/
317 /* Remove user was requested */
318 if ($s_action=="del"){
320 /* Get 'dn' from posted 'uid' */
321 $dn = $this->macros[$s_entry]['dn'];
322 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
324 if(preg_match("/d/",$acl)){
326 $this->dn = $dn;
328 /* Check locking, save current plugin in 'back_plugin', so
329 the dialog knows where to return. */
330 if (($user= get_lock($this->dn)) != ""){
331 return (gen_locked_message ($user, $this->dn));
332 }
334 /* Lock the current entry, so nobody will edit it during deletion */
335 add_lock ($this->dn, $this->ui->dn);
336 $smarty= get_smarty();
337 $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
338 $smarty->assign("multiple", false);
339 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
340 } else {
342 /* Obviously the user isn't allowed to delete. Show message and
343 clean session. */
344 msg_dialog::display(_("Permission error"), _("You have not permission to delete this entry!"), ERROR_DIALOG);
345 }
346 }
349 /*****************
350 Remove macro
351 *****************/
353 /* Confirmation for deletion has been passed. Macro should be deleted. */
354 if (isset($_POST['delete_macro_confirm'])){
356 $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
358 if(preg_match("/r/",$acl)){
360 /* Delete request is permitted, perform LDAP action */
361 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
362 $this->macrotabs->delete ();
363 unset ($this->macrotabs);
364 $this->macrotabs= NULL;
365 } else {
366 /* Normally this shouldn't be reached, send some extra
367 logs to notify the administrator */
368 msg_dialog::display(_("Permission error"), _("You have not permission to delete this entry!"), ERROR_DIALOG);
369 new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
370 }
371 /* Remove lock file after successfull deletion */
372 del_lock ($this->dn);
373 }
376 /*****************
377 Display open dialogs
378 *****************/
380 /* Show tab dialog if object is present */
381 if (($this->macrotabs) && (isset($this->macrotabs->config))){
382 $display= $this->macrotabs->execute();
384 /* Don't show buttons if tab dialog requests this */
385 if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
386 $display.= "<p style=\"text-align:right\">\n";
387 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
388 $display.= " \n";
389 if ($this->dn != "new"){
390 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
391 $display.= " \n";
392 }
393 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
394 $display.= "</p>";
395 }
396 return ($display);
397 }
400 /*****************
401 Display entry list
402 *****************/
404 /* Check if there is a snapshot dialog open */
405 $base = $this->DivListMacro->selectedBase;
406 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
407 return($str);
408 }
410 /* Return rendered main page */
411 /* Display dialog with system list */
412 $this->DivListMacro->parent = $this;
413 $this->DivListMacro->execute();
415 /* Add departments if subsearch is disabled */
416 if(!$this->DivListMacro->SubSearch){
417 $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
418 }
419 $this->reload();
420 $this->DivListMacro->setEntries($this->macros);
421 return($this->DivListMacro->Draw());
422 }
425 function copyPasteHandling_from_queue($s_action,$s_entry)
426 {
427 /* Check if Copy & Paste is disabled */
428 if(!is_object($this->CopyPasteHandler)){
429 return("");
430 }
432 /* Add a single entry to queue */
433 if($s_action == "cut" || $s_action == "copy"){
435 /* Cleanup object queue */
436 $this->CopyPasteHandler->cleanup_queue();
437 $dn = $this->macros[$s_entry]['dn'];
438 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
439 }
441 /* Add entries to queue */
442 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
444 /* Cleanup object queue */
445 $this->CopyPasteHandler->cleanup_queue();
447 /* Add new entries to CP queue */
448 foreach($this->list_get_selected_items() as $id){
449 $dn = $this->macros[$id]['dn'];
451 if($s_action == "copy_multiple"){
452 $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
453 }
454 if($s_action == "cut_multiple"){
455 $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
456 }
457 }
458 }
460 /* Start pasting entries */
461 if($s_action == "editPaste"){
462 $this->start_pasting_copied_objects = TRUE;
463 }
465 /* Return C&P dialog */
466 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
468 /* Load entry from queue and set base */
469 $this->CopyPasteHandler->load_entry_from_queue();
470 $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
472 /* Get dialog */
473 $data = $this->CopyPasteHandler->execute();
475 /* Return dialog data */
476 if(!empty($data)){
477 return($data);
478 }
479 }
481 /* Automatically disable status for pasting */
482 if(!$this->CopyPasteHandler->entries_queued()){
483 $this->start_pasting_copied_objects = FALSE;
484 }
485 return("");
486 }
490 /* Return departments, that will be included within snapshot detection */
491 function get_used_snapshot_bases()
492 {
493 return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
494 }
497 function reload()
498 {
499 /* Set base for all searches */
500 $base = $this->DivListMacro->selectedBase;
501 $SubSearch = $this->DivListMacro->SubSearch;
502 $Regex = $this->DivListMacro->Regex;
503 $Filter = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
504 $Flags = GL_SIZELIMIT;
505 $Attrs = array("cn","description","displayName","goFonMacroVisible");
507 /* Prepare for ls or search*/
508 if($SubSearch){
509 $Flags |= GL_SUBSEARCH;
510 }else{
511 $base = get_ou('macroou').$base;
512 }
514 /* Generate macro list */
515 $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
517 $tmp=array();
518 foreach($res as $tkey => $val ){
519 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
520 }
521 ksort($tmp);
522 $this->macros=array();
523 foreach($tmp as $val){
524 $this->macros[]=$val;
525 }
527 reset ($this->macros);
528 }
531 /* Save data to object */
532 function save_object()
533 {
534 $this->DivListMacro->save_object();
535 }
538 function list_get_selected_items()
539 {
540 $ids = array();
541 foreach($_POST as $name => $value){
542 if(preg_match("/^item_selected_[0-9]*$/",$name)){
543 $id = preg_replace("/^item_selected_/","",$name);
544 $ids[$id] = $id;
545 }
546 }
547 return($ids);
548 }
551 function adapt_from_template($dn) { }
552 function check() { }
553 }
554 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
555 ?>