Code

Updated divlists.
[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;
31     $this->base = $_SESSION['CurrentMainBase']; 
32     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
33     $this->set_acl_category("gofaxlist");
34     $this->set_acl_base($this->base);
35   }
37   function execute()
38   {
39     /* Call parent execute */
40     plugin::execute();
42     $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFaxBlocklist_/","/^act$/","/^id$/","/^remove_multiple_blocklists/","/^item_selected/");
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("/^goFaxBlocklist_del.*/",$key)){
63         $s_action = "del";
64         $s_entry  = preg_replace("/^goFaxBlocklist_del_/i","",$key);
65         // Post for edit
66       }elseif(preg_match("/^goFaxBlocklist_edit_.*/",$key)){
67         $s_action="edit";
68         $s_entry  = preg_replace("/^goFaxBlocklist_edit_/i","",$key);
69         // Post for new
70       }elseif(preg_match("/^goFaxBlocklist_new.*/",$key)){
71         $s_action="new";
72       }elseif(preg_match("/^remove_multiple_blocklists/",$key)){
73         $s_action="del_multiple";
74       }
75     }
77     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
78       $s_action ="edit";
79       $s_entry  = $_GET['id'];
80     }
82     $s_entry  = preg_replace("/_.$/","",$s_entry);
85     /***************
86       Cancel some dialogs  
87      ***************/
89     /* Cancel dialog */
90     if (isset($_POST['edit_cancel']) || 
91         isset($_POST['delete_blocklist_cancel']) ||
92         isset($_POST['delete_blocklist_confirm']) || 
93         isset($_POST['delete_lock'])){
95       del_lock ($this->dn);
96       if(!isset($_POST['delete_blocklist_confirm'])){
97         $this->clear_fields();
98       }
99       unset($_SESSION['objectinfo']);
100     }
103     /***************
104       Create a new blocklist (dialog)
105      ***************/
107     /* Create new blocklist? */
108     if ($s_action=="new"){
109       $this->clear_fields();
110       $this->dn= "new";
111       plugin::plugin ($this->config, $this->dn);
112     }
115     /***************
116       Edit blocklist 
117      ***************/
119     /* Edit selected blocklist? */
120     if (($s_action=="edit") && (empty($this->dn))){
121       $this->clear_fields();
122       $this->dn=$this->blocklists[$s_entry]['dn']; 
123       if (($user= get_lock($this->dn)) != ""){
124         $_SESSION['dn']= $this->dn;
125         //$this->dn ="";
126         return(gen_locked_message($user, $this->dn));
127       } else {
128 # Lock this dn for editing
129         add_lock ($this->dn, $this->ui->dn);
131         plugin::plugin ($this->config, $this->dn);
133         /* Load missing lists */
134         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
135           if(isset($this->attrs["goFaxSBlocklist"])){
136             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
137               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
138             }
139           }
140           $this->type= 0;
141         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
142           if(isset($this->attrs["goFaxRBlocklist"])){
143             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
144               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
145             }
146           }
147           $this->type= 1;
148         }
149         $_SESSION['objectinfo']= trim($this->dn);
150       }
151     }
154     /********************
155       Delete MULTIPLE entries requested, display confirm dialog
156      ********************/
158     if ($s_action=="del_multiple"){
160       $this->dns = array();
161       $ids = $this->list_get_selected_items();
163       if(count($ids)){
164         foreach($ids as $id){
165           $dn = $this->blocklists[$id]['dn'];
166           if (($user= get_lock($dn)) != ""){
167             return(gen_locked_message ($user, $dn));
168           }
169           $this->dns[$id] = $dn;
170         }
172         $dns_names = "<br><pre>";
173         foreach($this->dns as $dn){
174           add_lock ($dn, $this->ui->dn);
175           $dns_names .= $dn."\n";
176         }
177         $dns_names .="</pre>";
179         /* Lock the current entry, so nobody will edit it during deletion */
180         $smarty->assign("info",     sprintf(_("You're about to delete the following blocklists(s) %s"), @LDAP::fix($dns_names)));
181         $smarty->assign("multiple", true);
182         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
183       }
184     }
187     /********************
188       Delete MULTIPLE entries confirmed
189      ********************/
191     /* Confirmation for deletion has been passed. Users should be deleted. */
192     if (isset($_POST['delete_multiple_blocklist_confirm'])){
194       /* Remove user by user and check acls before removeing them */
195       foreach($this->dns as $key => $dn){
196         $acl = $this->ui->get_permissions($dn, "blocklist/blocklist");
197         if (preg_match('/d/', $acl)){
198           $this->dn = $dn;
199           $this->remove_from_parent();
200         } else {
201           print_red (sprintf(_("You are not allowed to delete the blocklist '%s'!"),$dn));
202         }
203         /* Remove lock file after successfull deletion */
204         del_lock ($dn);
205         unset($this->dns[$key]);
206       }
207       $this->reload();
208     }
211     /********************
212       Delete MULTIPLE entries Canceled
213      ********************/
215     /* Remove lock */
216     if(isset($_POST['delete_multiple_blocklist_cancel'])){
217       foreach($this->dns as $key => $dn){
218         del_lock ($dn);
219         unset($this->dns[$key]);
220       }
221     }
224     /***************
225       Remove blocklist
226      ***************/
228     /* Delete blocklist requested */
229     if ($s_action=="del"){
230       $this->dn=$this->blocklists[$s_entry]['dn'];
231       /* Check locking */
232       if (($user= get_lock($this->dn)) != ""){
233         $_SESSION['dn']= $this->dn;
234         return(gen_locked_message($user, $this->dn));
235       } else {
236 # Lock this dn for editing
237         add_lock ($this->dn, $this->ui->dn);
239         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
240         $smarty->assign("multiple", false);
241         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
242       }
243     }
246     /***************
247       Remove blocklist confirmed
248      ***************/
250     /* Finally delete blocklist */
251     if (isset($_POST['delete_blocklist_confirm'])){
252       if ($this->acl_is_removeable()){
253         $this->remove_from_parent();
254         gosa_log ("Blocklist object'".$this->dn."' has been removed");
255         $this->reload ();
256       } else {
257         print_red (_("You have no permission to remove this blocklist."));
258       }
259     }
262     /***************
263       Add numer to blocklist
264      ***************/
267     /* Handle interactions: add */
268     if (isset($_POST['add_number']) && $_POST['number'] != ""){
269       if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
270         $this->addNumber ($_POST['number']);
271       } else {
272         print_red (_("Please specify a valid phone number."));
273       }
274     }
277     /***************
278       Delete number from list
279      ***************/
281     /* Handle interactions: delete */
282     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
283       $this->delNumber ($_POST['numbers']);
284     }
287     /***************
288       Edit finished
289      ***************/
291     /* What about finish? */
292     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
293       $message= $this->check();
294       $this->remove_lock();
296       /* No errors, save object */
297       if (count ($message) == 0){
299         /* Perpare 'dn' in case of new entries */
300         if ($this->dn == "new"){
301           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
302         }
304         $this->save ();
305         gosa_log ("Blocklist object'".$this->dn."' has been saved");
306       
307         if (!isset($_POST['edit_apply'])){
308           $this->dn= "";
309           del_lock ($this->ui->dn);
310           unset($_SESSION['objectinfo']);
311         }
312       } else {
313         /* Errors found, show message */
314         show_errors ($message);
315       }
316     }
319     /***************
320       Object currently in edit  
321      ***************/
323     if($this->dn){
325       $dn = $this->dn;
326   
327       /* set Account states, this is nescessary for get_allowed_bases */
328       $this->initially_was_account= true;
329       $this->is_account = true;
330       if($dn == "new"){
331         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
332         $this->initially_was_account= false;
333       }
335       $this->set_acl_category("gofaxlist");
336       $this->set_acl_base($dn);
338       /* Base select dialog */
339       $once = true;
340       foreach($_POST as $name => $value){
341         if(preg_match("/^chooseBase/",$name) && $once){
342           $once = false;
343           $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
344           $this->dialog->setCurrentBase($this->base);
345         }
346       }
348       /* Dialog handling */
349       if(is_object($this->dialog)){
350         /* Must be called before save_object */
351         $this->dialog->save_object();
353         if($this->dialog->isClosed()){
354           $this->dialog = false;
355         }elseif($this->dialog->isSelected()){
357           /* A new base was selected, check if it is a valid one */
358           $tmp = $this->get_allowed_bases();
359           if(isset($tmp[$this->dialog->isSelected()])){
360             $this->base = $this->dialog->isSelected();
361           }
362           $this->dialog= false;
364         }else{
365           return($this->dialog->execute());
366         }
367       }
369       /* Set base */
370       if ($this->base == ""){
371         if ($this->dn == "new"){
372           $ui= get_userinfo();
373           $this->base= dn2base($ui->dn);
374         } else {
375           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
376         }
377       }
379       /* Fill templating stuff */
380       $smarty->assign("bases", $this->get_allowed_bases());
382       $smarty->assign("base_select", $this->base);
383       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
384       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
385         $smarty->assign("selectmode", "");
386         $smarty->assign("mode", "");
387         $smarty->assign("apply", "0");
388       } else {
389         $smarty->assign("selectmode", "disabled");
390         $smarty->assign("mode", "readonly");
391         $smarty->assign("apply", "1");
392       }
394       $tmp = $this->plInfo();
395       foreach($tmp['plProvidedAcls'] as $name => $translation){
396         $smarty->assign($name."ACL",$this->getacl($name));
397       }
399       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
400         $smarty->assign("$val", $this->$val);
401       }
403       /* Lock blocklist type for non new entries */
404       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
406       /* Show main page */
407       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
409     }else{
411       /***************
412         Divlist dialog 
413        ***************/
415       /* Check if there is a snapshot dialog open */
416       $base = $this->DivListBlocklist->selectedBase;
417       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
418         return($str);
419       }
421       /* Display dialog with system list */
422       $this->DivListBlocklist->parent = $this;
423       $this->DivListBlocklist->execute();
425       /* Add departments if subsearch is disabled */
426       if(!$this->DivListBlocklist->SubSearch){
427         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase,3,1);
428       }
429       $this->reload();
430       $this->DivListBlocklist->setEntries($this->blocklists);
431       return($this->DivListBlocklist->Draw());
432     }
433   }
436   function list_get_selected_items()
437   {
438     $ids = array();
439     foreach($_POST as $name => $value){
440       if(preg_match("/^item_selected_[0-9]*$/",$name)){
441         $id   = preg_replace("/^item_selected_/","",$name);
442         $ids[$id] = $id;
443       }
444     }
445     return($ids);
446   }
449   /* Return departments, that will be included within snapshot detection */
450   function get_used_snapshot_bases()
451   {
452     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
453   }
456   /* Clear garbage from edited lists */
457   function clear_fields()
458   {
459     $this->dn= "";
460     $this->description= "";
461     $this->cn= "";
462     $this->base= $_SESSION['CurrentMainBase'];
463     $this->goFaxBlocklist= array();
464   }
467   /* Reload the list of known blocklists */
468   function reload()
469   {
470     /* Init some vars */
471     $filter = $filter2      = "";
472     $base                   = $this->DivListBlocklist->selectedBase;
473     $Regex                  = $this->DivListBlocklist->Regex;
474     $SubSearch              = $this->DivListBlocklist->SubSearch;
475     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
476     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
477     $Flags                  = GL_SIZELIMIT;
478     $res = $res2            = array();
480     /* Append subsearch to Flags */
481     if($SubSearch){
482       $Flags |= GL_SUBSEARCH;
483     }else{
484       $base = "ou=gofax,ou=systems,".$base;
485     }  
487     /* Create filter */
488     if ($ShowSendBocklists){
489       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
490       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
491     }
492     if ($ShowReceiveBlocklists){
493       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
494       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
495     }
496    
497     $this->blocklists = array_merge($res,$res2);
499     /* appen && sort */
500     $tmp=array();
501     foreach($this->blocklists as $tkey => $val ){
503       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
504       if(preg_match("/r/",$acl)){
505         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
506       }
507     }
508     ksort($tmp);
509     $this->blocklists=array();
510     foreach($tmp as $val){
511       $this->blocklists[]=$val;
512     }
513     reset ($this->blocklists);
514   }
517   function remove_from_parent()
518   {
519     $ldap= $this->config->get_ldap_link();
520     $ldap->rmDir($this->dn);
521     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
522     $this->clear_fields();
523     $this->handle_post_events("remove");
524   }
527   /* Save data to object */
528   function save_object()
529   {
530     $this->DivListBlocklist->save_object();
532     /* Create a base backup and reset the
533        base directly after calling plugin::save_object();
534        Base will be set seperatly a few lines below */
535     $base_tmp = $this->base;
536     plugin::save_object();
537     $this->base = $base_tmp;
539     /* Save base, since this is no LDAP attribute */
540     $tmp = $this->get_allowed_bases();
541     if(isset($_POST['base'])){
542       if(isset($tmp[$_POST['base']])){
543         $this->base= $_POST['base'];
544       }
545     }
547     foreach($this->attributes as $attr){
548       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
549         $this->$attr = $_POST[$attr];
550       }
551     }
552     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
553       $this->type = $_POST['type'];
554     }
555   }
558   /* Check values */
559   function check()
560   {
561     /* Call common method to give check the hook */
562     $message= plugin::check();
564     /* Permissions for that base? */
565     if ($this->base != ""){
566       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
567     } else {
568       $new_dn= $this->dn;
569     }
571     /* check syntax: must cn */
572     if ($this->cn == ""){
573       $message[]= _("Required field 'Name' is not set.");
574     } else {
575       if (!is_uid($this->cn)){
576         $message[]= _("Required field 'Name' contains invalid characters");
577       }
578       if ($this->dn == 'new'){
579         $ldap= $this->config->get_ldap_link();
580         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
581         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
582         if ($ldap->count() != 0){
583           $message[]= _("Specified name is already used.");
584         }
585       }
586     }
588     return $message;
589   }
592   /* Save to LDAP */
593   function save()
594   {
595     plugin::save();
598     /* Type selection */
599     if ($this->type == 0){
600       $type= "goFaxSBlock";
601     } else {
602       $type= "goFaxRBlock";
603     }
605     /* Add list */
606     $this->attrs['objectClass']= $type;
607     if (count($this->goFaxBlocklist)){
608       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
609     }
611     /* Write back to ldap */
612     $ldap= $this->config->get_ldap_link();
613     $ldap->cd($this->base);
614     $ldap->search("cn=$this->cn",array("cn"));
615     $ldap->cat($this->dn, array('dn'));
616     $a= $ldap->fetch();
618     if (count($a)){
619       if (!isset($this->attrs["$type"."list"])){
620         $this->attrs["$type"."list"]= array();
621       }
622       $ldap->cd($this->dn);
623       $this->cleanup();
624       $ldap->modify($this->attrs);
625       $this->handle_post_events("modify");
626     } else {
627       $ldap->cd($this->config->current['BASE']);
628       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
629       $ldap->cd($this->dn);
630       $ldap->add($this->attrs);
631       $this->handle_post_events("add");
632     }
633     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
634   }
637   /* Add number */
638   function addNumber($number)
639   {
640     if (!in_array($number, $this->goFaxBlocklist)){
641       $this->goFaxBlocklist[]= $number;
642       sort($this->goFaxBlocklist);
643     }
644   }
647   /* Remove number from list */
648   function delNumber($numbers)
649   {
650     $tmp= array();
651     foreach ($this->goFaxBlocklist as $val){
652       if (!in_array($val, $numbers)){
653         $tmp[]= $val;
654       }
655     }
656     $this->goFaxBlocklist= $tmp;
657   }
660   /* Delete lock */
661   function remove_lock()
662   {
663     if (isset($this->dn)){
664       del_lock ($this->dn);
665     }
666   }
668   /* Return plugin informations for acl handling */
669   function plInfo()
670   {
671     return (array(  
672           "plShortName"       => _("Fax"),
673           "plDescription"     => _("Fax blocklists"),
674           "plSelfModify"      => FALSE,
675           "plDepends"         => array(),
677           "plPriority"    => 0,
678           "plSection"     => array("administration" => _("FAX Blocklists")),
679           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
680                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
681           "plProvidedAcls" => array(
682             "cn"              => _("Name"),
683             "description"     => _("Description"),
684             "base"            => _("Base"),
685             "goFaxBlocklist"  => _("Blocklist"),
686             "type"            => _("Blocklist type"))
687           ));
688   }
691 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
692 ?>