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_macros.inc";
22 class gofonMacro extends plugin
23 {
24 /* Definitions */
25 var $plHeadline = "Phone macros";
26 var $plDescription = "This does something";
28 /* Dialog attributes */
29 var $macrotabs = NULL;
30 var $macros = array();
31 var $ui = NULL;
32 var $DivListMacro = NULL;
34 function gofonMacro($config, $ui)
35 {
36 /* Save configuration for internal use */
37 $this->config= $config;
38 $this->ui= $ui;
40 $this->DivListMacro = new divListMacro($this->config,$this);
41 }
43 function execute()
44 {
45 /* Call parent execute */
46 plugin::execute();
48 $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFonMacro_/","/^act$/","/^id$/","/^item_selected/","/^remove_multiple_macros/");
50 /*****************
51 Variable initialisation
52 *****************/
54 $s_action = ""; // Contains the action to proceed
55 $s_entry = ""; // The value for s_action
56 $base_back = ""; // The Link for Backbutton
57 $smarty= get_smarty();
60 /*****************
61 Check Posts
62 *****************/
64 /* Test Posts */
65 foreach($_POST as $key => $val){
66 // Post for delete
67 if(preg_match("/^goFonMacro_del/",$key)){
68 $s_action = "del";
69 $s_entry = preg_replace("/^goFonMacro_del_/i","",$key);
70 // Post for edit
71 }elseif(preg_match("/^goFonMacro_edit_/",$key)){
72 $s_action="edit";
73 $s_entry = preg_replace("/^goFonMacro_edit_/i","",$key);
74 // Post for new
75 }elseif(preg_match("/^goFonMacro_new/",$key)){
76 $s_action="new";
77 }elseif(preg_match("/^remove_multiple_macros/",$key)){
78 $s_action="del_multiple";
79 }
80 }
81 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
82 $s_action ="edit";
83 $s_entry = $_GET['id'];
84 }
85 $s_entry = preg_replace("/_.$/","",$s_entry);
88 /*****************
89 Create a new Macro
90 *****************/
92 /* New macro? */
93 if ($s_action=="new"){
95 /* By default we set 'dn' to 'new', all relevant plugins will
96 react on this. */
97 $this->dn= "new";
99 /* Create new usertab object */
100 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
101 $this->macrotabs->set_acl_base($this->DivListMacro->selectedBase);
102 }
104 /*****************
105 Edit || Password canceled
106 *****************/
108 /* Cancel dialogs */
109 if (isset($_POST['edit_cancel'])){
110 del_lock ($this->macrotabs->dn);
111 unset ($this->macrotabs);
112 $this->macrotabs= NULL;
113 unset ($_SESSION['objectinfo']);
114 }
117 /*****************
118 Edit finised
119 *****************/
121 /* Finish mac edit is triggered by the tabulator dialog, so
122 the user wants to save edited data. Check and save at this
123 point. */
124 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->macrotabs->config))){
126 /* Check tabs, will feed message array */
127 $this->macrotabs->last= $this->macrotabs->current;
128 $this->macrotabs->save_object();
129 $message= $this->macrotabs->check();
131 /* Save, or display error message? */
132 if (count($message) == 0){
134 /* Save data data to ldap */
135 $this->macrotabs->save();
136 gosa_log ("Macro object'".$this->dn."' has been saved");
138 if (!isset($_POST['edit_apply'])){
139 /* macro has been saved successfully, remove lock from
140 LDAP. */
141 if ($this->dn != "new"){
142 del_lock ($this->dn);
143 }
145 unset ($this->macrotabs);
146 $this->macrotabs= NULL;
147 unset ($_SESSION['objectinfo']);
148 }
149 } else {
150 /* Ok. There seem to be errors regarding to the tab data,
151 show message and continue as usual. */
152 show_errors($message);
153 }
154 }
157 /*****************
158 Edit macro
159 *****************/
161 /* User wants to edit data */
162 if (($s_action=="edit") && (!isset($this->macrotabs->config))){
164 $dn = $this->macros[$s_entry]['dn'];
165 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
166 if(preg_match("/r/",$acl)){
167 $this->dn = $dn;
169 /* Check locking, save current plugin in 'back_plugin', so
170 the dialog knows where to return. */
171 if (($user= get_lock($this->dn)) != ""){
172 return(gen_locked_message ($user, $this->dn));
173 }
175 /* Lock the current entry, so everyone will get the
176 above dialog */
177 add_lock ($this->dn, $this->ui->dn);
178 /* Register macrotabs to trigger edit dialog */
179 $this->macrotabs= new macrotabs($this->config,
180 $this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
181 $_SESSION['objectinfo']= $this->dn;
182 }
183 }
187 /********************
188 Delete MULTIPLE entries requested, display confirm dialog
189 ********************/
191 if ($s_action=="del_multiple"){
192 $ids = $this->list_get_selected_items();
194 if(count($ids)){
196 foreach($ids as $id){
197 $dn = $this->macros[$id]['dn'];
198 if (($user= get_lock($dn)) != ""){
199 return(gen_locked_message ($user, $dn));
200 }
201 $this->dns[$id] = $dn;
202 }
204 $dns_names = "<br><pre>";
205 foreach($this->dns as $dn){
206 add_lock ($dn, $this->ui->dn);
207 $dns_names .= $dn."\n";
208 }
209 $dns_names .="</pre>";
211 /* Lock the current entry, so nobody will edit it during deletion */
212 $smarty->assign("intro", sprintf(_("You're about to delete the following user(s) %s"), @LDAP::fix($dns_names)));
213 $smarty->assign("multiple", true);
214 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
215 }
216 }
219 /********************
220 Delete MULTIPLE entries confirmed
221 ********************/
223 /* Confirmation for deletion has been passed. Users should be deleted. */
224 if (isset($_POST['delete_multiple_macro_confirm'])){
226 /* Remove user by user and check acls before removeing them */
227 foreach($this->dns as $key => $dn){
229 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
230 if(preg_match("/d/",$acl)){
232 /* Delete request is permitted, perform LDAP action */
233 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $dn,"gofonmacro");
234 $this->macrotabs->delete ();
235 gosa_log ("macro object'".$dn."' has been removed");
236 unset ($this->macrotabs);
237 $this->macrotabs= NULL;
239 } else {
240 print_red (_("You are not allowed to delete this macro!"));
241 }
243 /* Remove lock file after successfull deletion */
244 del_lock ($dn);
245 unset($this->dns[$key]);
246 }
247 }
250 /********************
251 Delete MULTIPLE entries Canceled
252 ********************/
254 /* Remove lock */
255 if(isset($_POST['delete_multiple_macro_cancel'])){
256 foreach($this->dns as $key => $dn){
257 del_lock ($dn);
258 unset($this->dns[$key]);
259 }
260 }
263 /*****************
264 Remove macro
265 *****************/
267 /* Remove user was requested */
268 if ($s_action=="del"){
270 /* Get 'dn' from posted 'uid' */
271 $dn = $this->macros[$s_entry]['dn'];
272 $acl = $this->ui->get_permissions($dn,"gofonmacro/macro");
274 if(preg_match("/d/",$acl)){
276 $this->dn = $dn;
278 /* Check locking, save current plugin in 'back_plugin', so
279 the dialog knows where to return. */
280 if (($user= get_lock($this->dn)) != ""){
281 return (gen_locked_message ($user, $this->dn));
282 }
284 /* Lock the current entry, so nobody will edit it during deletion */
285 add_lock ($this->dn, $this->ui->dn);
286 $smarty= get_smarty();
287 $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
288 $smarty->assign("multiple", false);
289 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
290 } else {
292 /* Obviously the user isn't allowed to delete. Show message and
293 clean session. */
294 print_red (_("You are not allowed to delete this macro!"));
295 }
296 }
299 /*****************
300 Remove macro
301 *****************/
303 /* Confirmation for deletion has been passed. Macro should be deleted. */
304 if (isset($_POST['delete_macro_confirm'])){
306 $acl = $this->ui->get_permissions($this->dn,"gofonmacro/macro");
308 if(preg_match("/r/",$acl)){
310 /* Delete request is permitted, perform LDAP action */
311 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn,"gofonmacro");
312 $this->macrotabs->delete ();
313 gosa_log ("macro object'".$this->dn."' has been removed");
314 unset ($this->macrotabs);
315 $this->macrotabs= NULL;
316 } else {
317 /* Normally this shouldn't be reached, send some extra
318 logs to notify the administrator */
319 print_red (_("You are not allowed to delete this macro!"));
320 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
321 }
322 /* Remove lock file after successfull deletion */
323 del_lock ($this->dn);
324 }
327 /*****************
328 Display open dialogs
329 *****************/
331 /* Show tab dialog if object is present */
332 if (($this->macrotabs) && (isset($this->macrotabs->config))){
333 $display= $this->macrotabs->execute();
335 /* Don't show buttons if tab dialog requests this */
336 if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
337 $display.= "<p style=\"text-align:right\">\n";
338 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
339 $display.= " \n";
340 if ($this->dn != "new"){
341 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
342 $display.= " \n";
343 }
344 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
345 $display.= "</p>";
346 }
347 return ($display);
348 }
351 /*****************
352 Display entry list
353 *****************/
355 /* Check if there is a snapshot dialog open */
356 $base = $this->DivListMacro->selectedBase;
357 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
358 return($str);
359 }
361 /* Return rendered main page */
362 /* Display dialog with system list */
363 $this->DivListMacro->parent = $this;
364 $this->DivListMacro->execute();
366 /* Add departments if subsearch is disabled */
367 if(!$this->DivListMacro->SubSearch){
368 $this->DivListMacro->AddDepartments($this->DivListMacro->selectedBase,4,1);
369 }
370 $this->reload();
371 $this->DivListMacro->setEntries($this->macros);
372 return($this->DivListMacro->Draw());
373 }
376 /* Return departments, that will be included within snapshot detection */
377 function get_used_snapshot_bases()
378 {
379 return(array("ou=macros,ou=asterisk,ou=configs,ou=systems,".$this->DivListMacro->selectedBase));
380 }
383 function reload()
384 {
385 /* Set base for all searches */
386 $base = $this->DivListMacro->selectedBase;
387 $SubSearch = $this->DivListMacro->SubSearch;
388 $Regex = $this->DivListMacro->Regex;
389 $Filter = "(&(cn=".$Regex.")(objectClass=goFonMacro))";
390 $Flags = GL_SIZELIMIT;
391 $Attrs = array("cn","description","displayName","goFonMacroVisible");
393 /* Prepare for ls or search*/
394 if($SubSearch){
395 $Flags |= GL_SUBSEARCH;
396 }else{
397 $base = "ou=macros,ou=asterisk,ou=configs,ou=systems,".$base;
398 }
400 /* Generate macro list */
401 $res= get_list($Filter, "gofonmacro", $base, $Attrs, $Flags);
403 $tmp=array();
404 foreach($res as $tkey => $val ){
405 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
406 }
407 ksort($tmp);
408 $this->macros=array();
409 foreach($tmp as $val){
410 $this->macros[]=$val;
411 }
413 reset ($this->macros);
414 }
417 /* Save data to object */
418 function save_object()
419 {
420 $this->DivListMacro->save_object();
421 }
424 function list_get_selected_items()
425 {
426 $ids = array();
427 foreach($_POST as $name => $value){
428 if(preg_match("/^item_selected_[0-9]*$/",$name)){
429 $id = preg_replace("/^item_selected_/","",$name);
430 $ids[$id] = $id;
431 }
432 }
433 return($ids);
434 }
437 function adapt_from_template($dn) { }
438 function check() { }
439 }
440 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
441 ?>