Code

Added apply button
[gosa.git] / plugins / gofax / blocklists / class_blocklistManagement.inc
1 <?php
2 class blocklist extends plugin
3 {
4   /* Definitions */
5   var $plHeadline= "FAX Blocklists";
6   var $plDescription= "This does something";
8   /* CLI vars */
9   var $cli_summary= "Handling of GOfax block lists";
10   var $cli_description= "Some longer text\nfor help";
11   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
13   /* blocklist attributes */
14   var $cn               = "";
15   var $description      = "";
16   var $type             = "";
17   var $base             = "";
19   var $goFaxBlocklist   = array();
20   var $blocklists       = array();
21   var $attributes       = array("cn", "description");
22   var $objectclasses    = array();
23   var $DivListBlocklist = NULL;
25   function blocklist ($config, $ui)
26   {
27     /* Init class */
28     $this->ui     = $ui;
29     $this->dn     = "";
30     $this->config = $config;
32     $ui           = get_userinfo();
33     $acl          = get_permissions ($ui->dn, $ui->subtreeACL);
34     $this->acl    = get_module_permission($acl, "blocklists", $ui->dn);
36     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
37   }
39   function execute()
40   {
41     /* Call parent execute */
42     plugin::execute();
44     /***************
45       Init vars 
46      ***************/
48     /* Get global smarty instance */
49     $smarty       = get_smarty();
50     $s_action     = "";                       // Contains the action to proceed
51     $s_entry      = "";                       // The value for s_action
52     $base_back    = "";                       // The Link for Backbutton
55     /***************
56       Fetch posts 
57      ***************/
59     /* Test Posts */
60     foreach($_POST as $key => $val){
61       // Post for delete
62       if(preg_match("/user_del.*/",$key)){
63         $s_action = "del";
64         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
65         // Post for edit
66       }elseif(preg_match("/user_edit_.*/",$key)){
67         $s_action="edit";
68         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
69         // Post for new
70       }elseif(preg_match("/user_new.*/",$key)){
71         $s_action="new";
72       }elseif(preg_match("/user_tplnew.*/i",$key)){
73         $s_action="new_tpl";
74       }elseif(preg_match("/user_chgpw.*/i",$key)){
75         $s_action="change_pw";
76         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
77       }
78     }
80     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
81       $s_action ="edit";
82       $s_entry  = $_GET['id'];
83     }
85     $s_entry  = preg_replace("/_.$/","",$s_entry);
88     /***************
89       Cancel some dialogs  
90      ***************/
92     /* Cancel dialog */
93     if (isset($_POST['edit_cancel']) || 
94         isset($_POST['delete_blocklist_cancel']) ||
95         isset($_POST['delete_blocklist_confirm']) || 
96         isset($_POST['delete_lock'])){
98       if(!isset($_POST['delete_blocklist_confirm'])){
99         $this->clear_fields();
100       }
101       del_lock ($this->dn);
102       unset($_SESSION['objectinfo']);
103     }
106     /***************
107       Cancel some dialogs  
108      ***************/
110     /* Create new blocklist? */
111     if ($s_action=="new"){
112       $this->clear_fields();
113       $this->dn= "new";
114       plugin::plugin ($this->config, $this->dn);
115     }
118     /***************
119       Edit blocklist 
120      ***************/
122     /* Edit selected blocklist? */
123     if (($s_action=="edit") && (empty($this->dn))){
124       $this->clear_fields();
125       $this->dn=$this->blocklists[$s_entry]['dn']; 
126       if (($user= get_lock($this->dn)) != ""){
127         $_SESSION['dn']= $this->dn;
128         //$this->dn ="";
129         return(gen_locked_message($user, $this->dn));
130       } else {
131 # Lock this dn for editing
132         add_lock ($this->dn, $this->ui->dn);
134         plugin::plugin ($this->config, $this->dn);
136         /* Load missing lists */
137         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
138           if(isset($this->attrs["goFaxSBlocklist"])){
139             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
140               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
141             }
142           }
143           $this->type= 0;
144         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
145           if(isset($this->attrs["goFaxRBlocklist"])){
146             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
147               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
148             }
149           }
150           $this->type= 1;
151         }
152         $_SESSION['objectinfo']= trim($this->dn);
153       }
154     }
157     /***************
158       Remove blocklist
159      ***************/
161     /* Delete blocklist requested */
162     if ($s_action=="del"){
163       $this->dn=$this->blocklists[$s_entry]['dn'];
164       /* Check locking */
165       if (($user= get_lock($this->dn)) != ""){
166         $_SESSION['dn']= $this->dn;
167         return(gen_locked_message($user, $this->dn));
168       } else {
169 # Lock this dn for editing
170         add_lock ($this->dn, $this->ui->dn);
172         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
173         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
174       }
175     }
178     /***************
179       Remove blocklist confirmed
180      ***************/
182     /* Finally delete blocklist */
183     if (isset($_POST['delete_blocklist_confirm'])){
184       if (chkacl($this->acl, "delete") == ""){
185         $this->remove_from_parent();
186         gosa_log ("Blocklist object'".$this->dn."' has been removed");
187         $this->reload ();
188       } else {
189         print_red (_("You have no permission to remove this blocklist."));
190       }
191     }
194     /***************
195       Add numer to blocklist
196      ***************/
199     /* Handle interactions: add */
200     if (isset($_POST['add_number']) && $_POST['number'] != ""){
201       if (is_phone_nr($_POST['number'])){
202         $this->addNumber ($_POST['number']);
203       } else {
204         print_red (_("Please specify a valid phone number."));
205       }
206     }
209     /***************
210       Delete number from list
211      ***************/
213     /* Handle interactions: delete */
214     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
215       $this->delNumber ($_POST['numbers']);
216     }
219     /***************
220       Edit finished
221      ***************/
223     /* What about finish? */
224     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
225       $message= $this->check();
226       $this->remove_lock();
228       /* No errors, save object */
229       if (count ($message) == 0){
231         /* Perpare 'dn' in case of new entries */
232         if ($this->dn == "new"){
233           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
234         }
236         $this->save ();
237         gosa_log ("Blocklist object'".$this->dn."' has been saved");
238       
239         if (!isset($_POST['edit_apply'])){
240           $this->dn= "";
241           del_lock ($this->ui->dn);
242           unset($_SESSION['objectinfo']);
243         }
244       } else {
245         /* Errors found, show message */
246         show_errors ($message);
247       }
248     }
251     /***************
252       Object currently in edit  
253      ***************/
255     if($this->dn){
257       /* Base select dialog */
258       $once = true;
259       foreach($_POST as $name => $value){
260         if(preg_match("/^chooseBase/",$name) && $once){
261           $once = false;
262           $this->dialog = new baseSelectDialog($this->config);
263           $this->dialog->setCurrentBase($this->base);
264         }
265       }
267       /* Dialog handling */
268       if(is_object($this->dialog)){
269         /* Must be called before save_object */
270         $this->dialog->save_object();
272         if($this->dialog->isClosed()){
273           $this->dialog = false;
274         }elseif($this->dialog->isSelected()){
275           $this->base = $this->dialog->isSelected();
276           $this->dialog= false;
277         }else{
278           return($this->dialog->execute());
279         }
280       }
282       /* Set base */
283       if ($this->base == ""){
284         if ($this->dn == "new"){
285           $ui= get_userinfo();
286           $this->base= dn2base($ui->dn);
287         } else {
288           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
289         }
290       }
292       /* Fill templating stuff */
293       $smarty->assign("bases", $this->config->idepartments);
294       $smarty->assign("base_select", $this->base);
295       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
296       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
297         $smarty->assign("selectmode", "");
298         $smarty->assign("mode", "");
299         $smarty->assign("apply", "0");
300       } else {
301         $smarty->assign("selectmode", "disabled");
302         $smarty->assign("mode", "readonly");
303         $smarty->assign("apply", "1");
304       }
305       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
306         $smarty->assign("$val", $this->$val);
307         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
308       }
310       /* Lock blocklist type for non new entries */
311       if ($this->dn != "new"){
312         $smarty->assign('typeACL', "disabled");
313       }
315       /* Show main page */
316       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
318     }else{
320       /***************
321         Divlist dialog 
322        ***************/
324       /* Display dialog with system list */
325       $this->DivListBlocklist->parent = $this;
326       $this->DivListBlocklist->execute();
328       /* Add departments if subsearch is disabled */
329       if(!$this->DivListBlocklist->SubSearch){
330         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
331       }
332       $this->reload();
333       $this->DivListBlocklist->setEntries($this->blocklists);
334       return($this->DivListBlocklist->Draw());
335     }
336   }
339   /* Clear garbage from edited lists */
340   function clear_fields()
341   {
342     $this->dn= "";
343     $this->description= "";
344     $this->cn= "";
345     $this->base= $_SESSION['CurrentMainBase'];
346     $this->goFaxBlocklist= array();
347   }
350   /* Reload the list of known blocklists */
351   function reload()
352   {
353     /* Init some vars */
354     $filter = $filter2      = "";
355     $base                   = $this->DivListBlocklist->selectedBase;
356     $Regex                  = $this->DivListBlocklist->Regex;
357     $SubSearch              = $this->DivListBlocklist->SubSearch;
358     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
359     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
360     $Flags                  = GL_SIZELIMIT;
361     $res = $res2            = array();
363     /* Append subsearch to Flags */
364     if($SubSearch){
365       $Flags |= GL_SUBSEARCH;
366     }else{
367       $base = "ou=gofax,ou=systems,".$base;
368     }  
370     /* Create filter */
371     if ($ShowSendBocklists){
372       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
373       $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
374     }
375     if ($ShowReceiveBlocklists){
376       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
377       $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
378     }
379    
380     $this->blocklists = array_merge($res,$res2);
382     /* appen && sort */
383     $tmp=array();
384     foreach($this->blocklists as $tkey => $val ){
385       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
386     }
387     ksort($tmp);
388     $this->blocklists=array();
389     foreach($tmp as $val){
390       $this->blocklists[]=$val;
391     }
392     reset ($this->blocklists);
393   }
396   function remove_from_parent()
397   {
398     $ldap= $this->config->get_ldap_link();
399     $ldap->rmDir($this->dn);
400     show_ldap_error($ldap->get_error(), _("Removing blocklist object failed"));
401     $this->clear_fields();
402     $this->handle_post_events("remove");
403   }
406   /* Save data to object */
407   function save_object()
408   {
409     $this->DivListBlocklist->save_object();
410     plugin::save_object();
412     if(isset($_POST['base'])){
413       $this->base = $_POST['base'];
414     }
415     foreach($this->attributes as $attr){
416       if(isset($_POST[$attr])){
417         $this->$attr = $_POST[$attr];
418       }
419     }
420   }
423   /* Check values */
424   function check()
425   {
426     /* Call common method to give check the hook */
427     $message= plugin::check();
429     /* Permissions for that base? */
430     if ($this->base != ""){
431       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
432     } else {
433       $new_dn= $this->dn;
434     }
436     if (chkacl($this->acl, "create") != ""){
437       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
438     }
441     /* check syntax: must cn */
442     if ($this->cn == ""){
443       $message[]= _("Required field 'Name' is not set.");
444     } else {
445       if (!is_uid($this->cn)){
446         $message[]= _("Required field 'Name' contains invalid characters");
447       }
448       if ($this->dn == 'new'){
449         $ldap= $this->config->get_ldap_link();
450         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
451         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
452         if ($ldap->count() != 0){
453           $message[]= _("Specified name is already used.");
454         }
455       }
456     }
458     /* Is user allowed to create in 'base'? */
459     if (chkacl($this->acl, "create") != ""){
460       $message[]= _("No permission to create a blocklist on this base.");
461     }
463     return $message;
464   }
467   /* Save to LDAP */
468   function save()
469   {
470     plugin::save();
473     /* Type selection */
474     if ($this->type == 0){
475       $type= "goFaxSBlock";
476     } else {
477       $type= "goFaxRBlock";
478     }
480     /* Add list */
481     $this->attrs['objectClass']= $type;
482     if (count($this->goFaxBlocklist)){
483       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
484     }
486     /* Write back to ldap */
487     $ldap= $this->config->get_ldap_link();
488     $ldap->cd($this->base);
489     $ldap->search("cn=$this->cn",array("cn"));
490     $ldap->cat($this->dn, array('dn'));
491     $a= $ldap->fetch();
493     if (count($a)){
494       if (!isset($this->attrs["$type"."list"])){
495         $this->attrs["$type"."list"]= array();
496       }
497       $ldap->cd($this->dn);
498       $this->cleanup();
499       $ldap->modify($this->attrs);
500       $this->handle_post_events("modify");
501     } else {
502       $ldap->cd($this->config->current['BASE']);
503       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
504       $ldap->cd($this->dn);
505       $ldap->add($this->attrs);
506       $this->handle_post_events("add");
507     }
508     show_ldap_error($ldap->get_error(), _("Saving blocklist object failed"));
509   }
512   /* Add number */
513   function addNumber($number)
514   {
515     if (!in_array($number, $this->goFaxBlocklist)){
516       $this->goFaxBlocklist[]= $number;
517       sort($this->goFaxBlocklist);
518     }
519   }
522   /* Remove number from list */
523   function delNumber($numbers)
524   {
525     $tmp= array();
526     foreach ($this->goFaxBlocklist as $val){
527       if (!in_array($val, $numbers)){
528         $tmp[]= $val;
529       }
530     }
531     $this->goFaxBlocklist= $tmp;
532   }
535   /* Delete lock */
536   function remove_lock()
537   {
538     if (isset($this->dn)){
539       del_lock ($this->dn);
540     }
541   }
544 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
545 ?>