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 var $dns = array();
38 var $acl_module = array("gofonmacro");
40 function gofonMacro(&$config, $ui)
41 {
42 /* Save configuration for internal use */
43 $this->config= $config;
44 $this->ui= $ui;
46 $this->DivListMacro = new divListMacro($this->config,$this);
48 /* Copy & Paste enabled ?*/
49 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
50 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
51 }
52 }
54 function execute()
55 {
56 /* Call parent execute */
57 plugin::execute();
59 session::set('LOCK_VARS_TO_USE',array("/^menu_action/","/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/"));
61 /*****************
62 Variable initialisation
63 *****************/
65 $s_action = ""; // Contains the action to proceed
66 $s_entry = ""; // The value for s_action
67 $base_back = ""; // The Link for Backbutton
68 $smarty= get_smarty();
71 /*****************
72 Check Posts
73 *****************/
75 /* Test Posts */
76 foreach($_POST as $key => $val){
77 // Post for delete
78 if(preg_match("/^goFonMacro_del/",$key)){
79 $s_action = "del";
80 $s_entry = preg_replace("/^goFonMacro_del_/i","",$key);
81 // Post for edit
82 }elseif(preg_match("/^goFonMacro_edit_/",$key)){
83 $s_action="edit";
84 $s_entry = preg_replace("/^goFonMacro_edit_/i","",$key);
85 // Post for new
86 }elseif(preg_match("/^goFonMacro_new/",$key)){
87 $s_action="new";
88 }elseif(preg_match("/^remove_multiple_macros/",$key)){
89 $s_action="del_multiple";
90 }elseif(preg_match("/^editPaste.*/i",$key)){
91 $s_action="editPaste";
92 }elseif(preg_match("/^copy_.*/",$key)){
93 $s_action="copy";
94 $s_entry = preg_replace("/^copy_/i","",$key);
95 }elseif(preg_match("/^cut_.*/",$key)){
96 $s_action="cut";
97 $s_entry = preg_replace("/^cut_/i","",$key);
98 }elseif(preg_match("/^multiple_copy_objects/",$key)){
99 $s_action = "copy_multiple";
100 }elseif(preg_match("/^multiple_cut_objects/",$key)){
101 $s_action = "cut_multiple";
102 }
103 }
104 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
105 $s_action ="edit";
106 $s_entry = $_GET['id'];
107 }
108 $s_entry = preg_replace("/_.$/","",$s_entry);
111 /********************
112 Copy & Paste Handling ...
113 ********************/
116 /* handle C&P from layers menu */
117 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
118 $s_action = "copy_multiple";
119 }
120 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
121 $s_action = "cut_multiple";
122 }
123 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
124 $s_action = "editPaste";
125 }
127 /* Create options */
128 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "goFonMacro_new"){
129 $s_action = "new";
130 }
132 /* handle remove from layers menu */
133 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
134 $s_action = "del_multiple";
135 }
137 /* Display the copy & paste dialog, if it is currently open */
138 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
139 if($ret){
140 return($ret);
141 }
144 /*****************
145 Create a new Macro
146 *****************/
148 /* New macro? */
149 if ($s_action=="new"){
151 /* By default we set 'dn' to 'new', all relevant plugins will
152 react on this. */
153 $this->dn= "new";
155 /* Create new usertab object */
156 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
157 $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
158 }
160 /*****************
161 Edit || Password canceled
162 *****************/
164 /* Cancel dialogs */
165 if (isset($_POST['edit_cancel'])){
166 $this->remove_lock();
167 unset ($this->macrotabs);
168 $this->macrotabs= NULL;
169 session::un_set('objectinfo');
170 }
173 /*****************
174 Edit finised
175 *****************/
177 /* Finish mac edit is triggered by the tabulator dialog, so
178 the user wants to save edited data. Check and save at this
179 point. */
180 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
182 /* Check tabs, will feed message array */
183 $this->macrotabs->last= $this->macrotabs->current;
184 $this->macrotabs->save_object();
185 $message= $this->macrotabs->check();
187 /* Save, or display error message? */
188 if (count($message) == 0){
190 /* Save data data to ldap */
191 $this->macrotabs->save();
193 if (!isset($_POST['edit_apply'])){
194 /* macro has been saved successfully, remove lock from
195 LDAP. */
196 if ($this->dn != "new"){
197 $this->remove_lock();
198 }
200 unset ($this->macrotabs);
201 $this->macrotabs= NULL;
202 session::un_set('objectinfo');
203 }else{
205 /* Reinitialize tab */
206 if($this->macrotabs instanceof tabs){
207 $this->macrotabs->re_init();
208 }
209 }
210 } else {
211 /* Ok. There seem to be errors regarding to the tab data,
212 show message and continue as usual. */
213 msg_dialog::displayChecks($message);
214 }
215 }
218 /*****************
219 Edit macro
220 *****************/
222 /* User wants to edit data */
223 if (($s_action=="edit") && (!isset($this->macrotabs->config))){
225 $dn = $this->macros[$s_entry]['dn'];
226 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
227 if(preg_match("/r/",$acl)){
228 $this->dn = $dn;
230 /* Check locking, save current plugin in 'back_plugin', so
231 the dialog knows where to return. */
232 if (($user= get_lock($this->dn)) != ""){
233 return(gen_locked_message ($user, $this->dn));
234 }
236 /* Lock the current entry, so everyone will get the
237 above dialog */
238 add_lock ($this->dn, $this->ui->dn);
240 /* Register macrotabs to trigger edit dialog */
241 $this->macrotabs= new macrotabs($this->config,
242 $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
243 session::set('objectinfo',$this->dn);
244 }
245 }
249 /********************
250 Delete MULTIPLE entries requested, display confirm dialog
251 ********************/
253 if ($s_action=="del_multiple"){
254 $ids = $this->list_get_selected_items();
257 $this->dns = array();
258 if(count($ids)){
259 $disallowed = array();
260 foreach($ids as $id){
261 $dn = $this->macros[$id]['dn'];
262 $acl = $this->ui->get_permissions($dn, "gofonmacro/macro");
263 if(preg_match("/d/",$acl)){
264 $this->dns[$id] = $dn;
265 }else{
266 $disallowed[] = $dn;
267 }
268 }
270 if(count($disallowed)){
271 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
272 }
274 if(count($this->dns)){
276 /* Check locks */
277 if ($user= get_multiple_locks($this->dns)){
278 return(gen_locked_message($user,$this->dns));
279 }
281 $dns_names = array();
282 foreach($this->dns as $dn){
283 $dns_names[] = @LDAP::fix($dn);
284 }
286 /* Add entry locks */
287 add_lock ($this->dns, $this->ui->dn);
289 /* Lock the current entry, so nobody will edit it during deletion */
290 $smarty->assign("intro", msgPool::deleteInfo($dns_names,("macro")));
291 $smarty->assign("multiple", true);
292 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
293 }
294 }
295 }
298 /********************
299 Delete MULTIPLE entries confirmed
300 ********************/
302 /* Confirmation for deletion has been passed. Users should be deleted. */
303 if (isset($_POST['delete_multiple_macro_confirm'])){
305 /* Remove user by user and check acls before removeing them */
306 foreach($this->dns as $key => $dn){
308 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
309 if(preg_match("/d/",$acl)){
311 /* Delete request is permitted, perform LDAP action */
312 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
313 $this->macrotabs->delete ();
314 unset ($this->macrotabs);
315 $this->macrotabs= NULL;
317 } else {
318 msg_dialog::display(_("Permission error"),msgPool::permDelete(), ERROR_DIALOG);
319 }
321 }
323 /* Remove lock file after successfull deletion */
324 $this->remove_lock();
325 $this->dns = array();
326 }
329 /********************
330 Delete MULTIPLE entries Canceled
331 ********************/
333 /* Remove lock */
334 if(isset($_POST['delete_multiple_macro_cancel'])){
335 $this->remove_lock();
336 $this->dns = array();
337 }
340 /*****************
341 Remove macro
342 *****************/
344 /* Remove user was requested */
345 if ($s_action=="del"){
347 /* Get 'dn' from posted 'uid' */
348 $dn = $this->macros[$s_entry]['dn'];
349 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
351 if(preg_match("/d/",$acl)){
353 $this->dn = $dn;
355 /* Check locking, save current plugin in 'back_plugin', so
356 the dialog knows where to return. */
357 if (($user= get_lock($this->dn)) != ""){
358 return (gen_locked_message ($user, $this->dn));
359 }
361 /* Lock the current entry, so nobody will edit it during deletion */
362 add_lock ($this->dn, $this->ui->dn);
363 $smarty= get_smarty();
364 $smarty->assign("intro", msgPool::deleteInfo(@LDAP::fix($this->dn),_("macro")));
365 $smarty->assign("multiple", false);
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 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
372 }
373 }
376 /*****************
377 Remove macro
378 *****************/
380 /* Confirmation for deletion has been passed. Macro should be deleted. */
381 if (isset($_POST['delete_macro_confirm'])){
383 $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
385 if(preg_match("/r/",$acl)){
387 /* Delete request is permitted, perform LDAP action */
388 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
389 $this->macrotabs->delete ();
390 unset ($this->macrotabs);
391 $this->macrotabs= NULL;
392 } else {
393 /* Normally this shouldn't be reached, send some extra
394 logs to notify the administrator */
395 msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
396 new log("security","gofonmacro/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
397 }
399 /* Remove lock file after successfull deletion */
400 $this->remove_lock();
401 }
404 /*****************
405 Display open dialogs
406 *****************/
408 /* Show tab dialog if object is present */
409 if (($this->macrotabs) && (isset($this->macrotabs->config))){
410 $display= $this->macrotabs->execute();
412 /* Don't show buttons if tab dialog requests this */
413 if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
414 $display.= "<p style=\"text-align:right\">\n";
415 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
416 $display.= " \n";
417 if ($this->dn != "new"){
418 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
419 $display.= " \n";
420 }
421 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
422 $display.= "</p>";
423 }
424 return ($display);
425 }
428 /*****************
429 Display entry list
430 *****************/
432 /* Check if there is a snapshot dialog open */
433 $base = $this->DivListMacro->selectedBase;
434 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
435 return($str);
436 }
438 /* Return rendered main page */
439 /* Display dialog with system list */
440 $this->DivListMacro->parent = $this;
441 $this->DivListMacro->execute();
443 /* Add departments if subsearch is disabled */
444 if(!$this->DivListMacro->SubSearch){
445 $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
446 }
447 $this->reload();
448 $this->DivListMacro->setEntries($this->macros);
449 return($this->DivListMacro->Draw());
450 }
453 function copyPasteHandling_from_queue($s_action,$s_entry)
454 {
455 /* Check if Copy & Paste is disabled */
456 if(!is_object($this->CopyPasteHandler)){
457 return("");
458 }
460 $ui = get_userinfo();
462 /* Add a single entry to queue */
463 if($s_action == "cut" || $s_action == "copy"){
465 /* Cleanup object queue */
466 $this->CopyPasteHandler->cleanup_queue();
467 $dn = $this->macros[$s_entry]['dn'];
469 if($s_action == "copy" && $ui->is_copyable($dn,"gofonmacro","macro")){
470 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
471 }
472 if($s_action == "cut" && $ui->is_cutable($dn,"gofonmacro","macro")){
473 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"macrotabs","MACROTABS","gofonmacro");
474 }
475 }
477 /* Add entries to queue */
478 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
480 /* Cleanup object queue */
481 $this->CopyPasteHandler->cleanup_queue();
483 /* Add new entries to CP queue */
484 foreach($this->list_get_selected_items() as $id){
485 $dn = $this->macros[$id]['dn'];
487 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"gofonmacro","macro")){
488 $this->CopyPasteHandler->add_to_queue($dn,"copy","macrotabs","MACROTABS","gofonmacro");
489 }
490 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"gofonmacro","macro")){
491 $this->CopyPasteHandler->add_to_queue($dn,"cut","macrotabs","MACROTABS","gofonmacro");
492 }
493 }
494 }
496 /* Start pasting entries */
497 if($s_action == "editPaste"){
498 $this->start_pasting_copied_objects = TRUE;
499 }
501 /* Return C&P dialog */
502 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
504 /* Get dialog */
505 $this->CopyPasteHandler->SetVar("base",$this->DivListMacro->selectedBase);
506 $data = $this->CopyPasteHandler->execute();
508 /* Return dialog data */
509 if(!empty($data)){
510 return($data);
511 }
512 }
514 /* Automatically disable status for pasting */
515 if(!$this->CopyPasteHandler->entries_queued()){
516 $this->start_pasting_copied_objects = FALSE;
517 }
518 return("");
519 }
523 /* Return departments, that will be included within snapshot detection */
524 function get_used_snapshot_bases()
525 {
526 return(array(get_ou('macroou').$this->DivListMacro->selectedBase));
527 }
530 function reload()
531 {
532 /* Set base for all searches */
533 $base = $this->DivListMacro->selectedBase;
534 $SubSearch = $this->DivListMacro->SubSearch;
535 $Regex = $this->DivListMacro->Regex;
536 $Filter = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
537 $Flags = GL_SIZELIMIT;
538 $Attrs = array("cn","description","displayName","goFonMacroVisible");
540 /* Prepare for ls or search*/
541 if($SubSearch){
542 $Flags |= GL_SUBSEARCH;
543 }else{
544 $base = get_ou('macroou').$base;
545 }
547 /* Generate macro list */
548 $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
550 $tmp=array();
551 foreach($res as $tkey => $val ){
552 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
553 }
554 ksort($tmp);
555 $this->macros=array();
556 foreach($tmp as $val){
557 $this->macros[]=$val;
558 }
560 reset ($this->macros);
561 }
564 /* Save data to object */
565 function save_object()
566 {
567 $this->DivListMacro->save_object();
568 if(is_object($this->CopyPasteHandler)){
569 $this->CopyPasteHandler->save_object();
570 }
571 }
574 function list_get_selected_items()
575 {
576 $ids = array();
577 foreach($_POST as $name => $value){
578 if(preg_match("/^item_selected_[0-9]*$/",$name)){
579 $id = preg_replace("/^item_selected_/","",$name);
580 $ids[$id] = $id;
581 }
582 }
583 return($ids);
584 }
586 /*! \brief Remove entry locks if the plugin was aborted.
587 */
588 function remove_lock()
589 {
590 if($this->dn) del_lock($this->dn);
591 if(is_array($this->dns) && count($this->dns)) del_lock($this->dns);
592 }
594 function adapt_from_template($dn, $skip= array()) { }
595 function check() { }
596 }
597 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
598 ?>