Code

b0146012fe49f11719c3f5aea44272b7e1bb62fd
[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;
24   var $view_logged = FALSE;
25   var $CopyPasteHandler = NULL;
27   function blocklist ($config, $ui)
28   {
29     /* Init class */
30     $this->ui     = $ui;
31     $this->dn     = "";
32     $this->config = $config;
33     $this->base = $_SESSION['CurrentMainBase']; 
34     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
35     $this->set_acl_category("gofaxlist");
36     $this->set_acl_base($this->base);
37   }
39   function execute()
40   {
41     /* Call parent execute */
42     plugin::execute();
44     $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFaxBlocklist_/","/^act$/","/^id$/","/^remove_multiple_blocklists/","/^item_selected/");
46     /***************
47       Init vars 
48      ***************/
50     /* Get global smarty instance */
51     $smarty       = get_smarty();
52     $s_action     = "";                       // Contains the action to proceed
53     $s_entry      = "";                       // The value for s_action
54     $base_back    = "";                       // The Link for Backbutton
57     /***************
58       Fetch posts 
59      ***************/
61     /* Test Posts */
62     foreach($_POST as $key => $val){
63       // Post for delete
64       if(preg_match("/^goFaxBlocklist_del.*/",$key)){
65         $s_action = "del";
66         $s_entry  = preg_replace("/^goFaxBlocklist_del_/i","",$key);
67         // Post for edit
68       }elseif(preg_match("/^goFaxBlocklist_edit_.*/",$key)){
69         $s_action="edit";
70         $s_entry  = preg_replace("/^goFaxBlocklist_edit_/i","",$key);
71         // Post for new
72       }elseif(preg_match("/^goFaxBlocklist_new.*/",$key)){
73         $s_action="new";
74       }elseif(preg_match("/^remove_multiple_blocklists/",$key)){
75         $s_action="del_multiple";
76       }
77     }
79     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
80       $s_action ="edit";
81       $s_entry  = $_GET['id'];
82     }
84     $s_entry  = preg_replace("/_.$/","",$s_entry);
87     /***************
88       Cancel some dialogs  
89      ***************/
91     /* Cancel dialog */
92     if (isset($_POST['edit_cancel']) || 
93         isset($_POST['delete_blocklist_cancel']) ||
94         isset($_POST['delete_blocklist_confirm']) || 
95         isset($_POST['delete_lock'])){
97       del_lock ($this->dn);
98       if(!isset($_POST['delete_blocklist_confirm'])){
99         $this->clear_fields();
100       }
101       unset($_SESSION['objectinfo']);
102     }
105     /***************
106       Create a new blocklist (dialog)
107      ***************/
109     /* Create new blocklist? */
110     if ($s_action=="new"){
111       $this->clear_fields();
112       $this->dn= "new";
113       plugin::plugin ($this->config, $this->dn);
114     }
117     /***************
118       Edit blocklist 
119      ***************/
121     /* Edit selected blocklist? */
122     if (($s_action=="edit") && (empty($this->dn))){
123       $this->clear_fields();
124       $this->dn=$this->blocklists[$s_entry]['dn']; 
125       if (($user= get_lock($this->dn)) != ""){
126         $_SESSION['dn']= $this->dn;
127         //$this->dn ="";
128         return(gen_locked_message($user, $this->dn));
129       } else {
130 # Lock this dn for editing
131         add_lock ($this->dn, $this->ui->dn);
133         plugin::plugin ($this->config, $this->dn);
135         /* Load missing lists */
136         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
137           if(isset($this->attrs["goFaxSBlocklist"])){
138             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
139               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
140             }
141           }
142           $this->type= 0;
143         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
144           if(isset($this->attrs["goFaxRBlocklist"])){
145             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
146               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
147             }
148           }
149           $this->type= 1;
150         }
151         $_SESSION['objectinfo']= trim($this->dn);
152       }
153     }
156     /********************
157       Delete MULTIPLE entries requested, display confirm dialog
158      ********************/
160     if ($s_action=="del_multiple"){
162       $this->dns = array();
163       $ids = $this->list_get_selected_items();
165       if(count($ids)){
166         foreach($ids as $id){
167           $dn = $this->blocklists[$id]['dn'];
168           if (($user= get_lock($dn)) != ""){
169             return(gen_locked_message ($user, $dn));
170           }
171           $this->dns[$id] = $dn;
172         }
174         $dns_names = "<br><pre>";
175         foreach($this->dns as $dn){
176           add_lock ($dn, $this->ui->dn);
177           $dns_names .= $dn."\n";
178         }
179         $dns_names .="</pre>";
181         /* Lock the current entry, so nobody will edit it during deletion */
182         $smarty->assign("info",     sprintf(_("You're about to delete the following blocklists(s) %s"), @LDAP::fix($dns_names)));
183         $smarty->assign("multiple", true);
184         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
185       }
186     }
189     /********************
190       Delete MULTIPLE entries confirmed
191      ********************/
193     /* Confirmation for deletion has been passed. Users should be deleted. */
194     if (isset($_POST['delete_multiple_blocklist_confirm'])){
196       /* Remove user by user and check acls before removeing them */
197       foreach($this->dns as $key => $dn){
198         $acl = $this->ui->get_permissions($dn, "blocklist/blocklist");
199         if (preg_match('/d/', $acl)){
200           $this->dn = $dn;
201           $this->remove_from_parent();
202         } else {
203           print_red (sprintf(_("You are not allowed to delete the blocklist '%s'!"),$dn));
204         }
205         /* Remove lock file after successfull deletion */
206         del_lock ($dn);
207         unset($this->dns[$key]);
208       }
209       $this->reload();
210     }
213     /********************
214       Delete MULTIPLE entries Canceled
215      ********************/
217     /* Remove lock */
218     if(isset($_POST['delete_multiple_blocklist_cancel'])){
219       foreach($this->dns as $key => $dn){
220         del_lock ($dn);
221         unset($this->dns[$key]);
222       }
223     }
226     /***************
227       Remove blocklist
228      ***************/
230     /* Delete blocklist requested */
231     if ($s_action=="del"){
232       $this->dn=$this->blocklists[$s_entry]['dn'];
233       /* Check locking */
234       if (($user= get_lock($this->dn)) != ""){
235         $_SESSION['dn']= $this->dn;
236         return(gen_locked_message($user, $this->dn));
237       } else {
238 # Lock this dn for editing
239         add_lock ($this->dn, $this->ui->dn);
241         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
242         $smarty->assign("multiple", false);
243         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
244       }
245     }
248     /***************
249       Remove blocklist confirmed
250      ***************/
252     /* Finally delete blocklist */
253     if (isset($_POST['delete_blocklist_confirm'])){
254       if ($this->acl_is_removeable()){
255         $this->remove_from_parent();
256         $this->reload ();
257       } else {
258         print_red (_("You have no permission to remove this blocklist."));
259       }
260     }
263     /***************
264       Add numer to blocklist
265      ***************/
268     /* Handle interactions: add */
269     if (isset($_POST['add_number']) && $_POST['number'] != ""){
270       if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
271         $this->addNumber ($_POST['number']);
272       } else {
273         print_red (_("Please specify a valid phone number."));
274       }
275     }
278     /***************
279       Delete number from list
280      ***************/
282     /* Handle interactions: delete */
283     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
284       $this->delNumber ($_POST['numbers']);
285     }
288     /***************
289       Edit finished
290      ***************/
292     /* What about finish? */
293     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
294       $message= $this->check();
295       $this->remove_lock();
297       /* No errors, save object */
298       if (count ($message) == 0){
300         /* Perpare 'dn' in case of new entries */
301         if ($this->dn == "new"){
302           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
303         }
305         $this->save ();
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;
327       /* Log view */
328       if(!$this->view_logged){
329         $this->view_logged = TRUE;
330         new log("view","users/".get_class($this),$this->dn);
331       }
332  
333       /* set Account states, this is nescessary for get_allowed_bases */
334       $this->initially_was_account= true;
335       $this->is_account = true;
336       if($dn == "new"){
337         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
338         $this->initially_was_account= false;
339       }
341       $this->set_acl_category("gofaxlist");
342       $this->set_acl_base($dn);
344       /* Base select dialog */
345       $once = true;
346       foreach($_POST as $name => $value){
347         if(preg_match("/^chooseBase/",$name) && $once){
348           $once = false;
349           $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
350           $this->dialog->setCurrentBase($this->base);
351         }
352       }
354       /* Dialog handling */
355       if(is_object($this->dialog)){
356         /* Must be called before save_object */
357         $this->dialog->save_object();
359         if($this->dialog->isClosed()){
360           $this->dialog = false;
361         }elseif($this->dialog->isSelected()){
363           /* A new base was selected, check if it is a valid one */
364           $tmp = $this->get_allowed_bases();
365           if(isset($tmp[$this->dialog->isSelected()])){
366             $this->base = $this->dialog->isSelected();
367           }
368           $this->dialog= false;
370         }else{
371           return($this->dialog->execute());
372         }
373       }
375       /* Set base */
376       if ($this->base == ""){
377         if ($this->dn == "new"){
378           $ui= get_userinfo();
379           $this->base= dn2base($ui->dn);
380         } else {
381           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
382         }
383       }
385       /* Fill templating stuff */
386       $smarty->assign("bases", $this->get_allowed_bases());
388       $smarty->assign("base_select", $this->base);
389       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
390       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
391         $smarty->assign("selectmode", "");
392         $smarty->assign("mode", "");
393         $smarty->assign("apply", "0");
394       } else {
395         $smarty->assign("selectmode", "disabled");
396         $smarty->assign("mode", "readonly");
397         $smarty->assign("apply", "1");
398       }
400       $tmp = $this->plInfo();
401       foreach($tmp['plProvidedAcls'] as $name => $translation){
402         $smarty->assign($name."ACL",$this->getacl($name));
403       }
405       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
406         $smarty->assign("$val", $this->$val);
407       }
409       /* Lock blocklist type for non new entries */
410       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
412       /* Show main page */
413       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
415     }else{
417       /***************
418         Divlist dialog 
419        ***************/
421       /* Check if there is a snapshot dialog open */
422       $base = $this->DivListBlocklist->selectedBase;
423       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
424         return($str);
425       }
427       /* Display dialog with system list */
428       $this->DivListBlocklist->parent = $this;
429       $this->DivListBlocklist->execute();
431       /* Add departments if subsearch is disabled */
432       if(!$this->DivListBlocklist->SubSearch){
433         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase,3,1);
434       }
435       $this->reload();
436       $this->DivListBlocklist->setEntries($this->blocklists);
437       return($this->DivListBlocklist->Draw());
438     }
439   }
442   function list_get_selected_items()
443   {
444     $ids = array();
445     foreach($_POST as $name => $value){
446       if(preg_match("/^item_selected_[0-9]*$/",$name)){
447         $id   = preg_replace("/^item_selected_/","",$name);
448         $ids[$id] = $id;
449       }
450     }
451     return($ids);
452   }
455   /* Return departments, that will be included within snapshot detection */
456   function get_used_snapshot_bases()
457   {
458     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
459   }
462   /* Clear garbage from edited lists */
463   function clear_fields()
464   {
465     $this->dn= "";
466     $this->description= "";
467     $this->cn= "";
468     $this->base= $_SESSION['CurrentMainBase'];
469     $this->view_logged = FALSE;
470     $this->goFaxBlocklist= array();
471   }
474   /* Reload the list of known blocklists */
475   function reload()
476   {
477     /* Init some vars */
478     $filter = $filter2      = "";
479     $base                   = $this->DivListBlocklist->selectedBase;
480     $Regex                  = $this->DivListBlocklist->Regex;
481     $SubSearch              = $this->DivListBlocklist->SubSearch;
482     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
483     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
484     $Flags                  = GL_SIZELIMIT;
485     $res = $res2            = array();
487     /* Append subsearch to Flags */
488     if($SubSearch){
489       $Flags |= GL_SUBSEARCH;
490     }else{
491       $base = "ou=gofax,ou=systems,".$base;
492     }  
494     /* Create filter */
495     if ($ShowSendBocklists){
496       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
497       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
498     }
499     if ($ShowReceiveBlocklists){
500       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
501       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
502     }
503    
504     $this->blocklists = array_merge($res,$res2);
506     /* appen && sort */
507     $tmp=array();
508     foreach($this->blocklists as $tkey => $val ){
510       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
511       if(preg_match("/r/",$acl)){
512         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
513       }
514     }
515     ksort($tmp);
516     $this->blocklists=array();
517     foreach($tmp as $val){
518       $this->blocklists[]=$val;
519     }
520     reset ($this->blocklists);
521   }
524   function remove_from_parent()
525   {
526     $ldap= $this->config->get_ldap_link();
527     $ldap->rmDir($this->dn);
529     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
530   
531     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
532     $this->clear_fields();
533     $this->handle_post_events("remove");
534   }
537   /* Save data to object */
538   function save_object()
539   {
540     $this->DivListBlocklist->save_object();
542     /* Create a base backup and reset the
543        base directly after calling plugin::save_object();
544        Base will be set seperatly a few lines below */
545     $base_tmp = $this->base;
546     plugin::save_object();
547     $this->base = $base_tmp;
549     /* Save base, since this is no LDAP attribute */
550     $tmp = $this->get_allowed_bases();
551     if(isset($_POST['base'])){
552       if(isset($tmp[$_POST['base']])){
553         $this->base= $_POST['base'];
554       }
555     }
557     foreach($this->attributes as $attr){
558       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
559         $this->$attr = $_POST[$attr];
560       }
561     }
562     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
563       $this->type = $_POST['type'];
564     }
565   }
568   /* Check values */
569   function check()
570   {
571     /* Call common method to give check the hook */
572     $message= plugin::check();
574     /* Permissions for that base? */
575     if ($this->base != ""){
576       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
577     } else {
578       $new_dn= $this->dn;
579     }
581     /* check syntax: must cn */
582     if ($this->cn == ""){
583       $message[]= _("Required field 'Name' is not set.");
584     } else {
585       if (!is_uid($this->cn)){
586         $message[]= _("Required field 'Name' contains invalid characters");
587       }
588       if ($this->dn == 'new'){
589         $ldap= $this->config->get_ldap_link();
590         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
591         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
592         if ($ldap->count() != 0){
593           $message[]= _("Specified name is already used.");
594         }
595       }
596     }
598     return $message;
599   }
602   /* Save to LDAP */
603   function save()
604   {
605     plugin::save();
608     /* Type selection */
609     if ($this->type == 0){
610       $type= "goFaxSBlock";
611     } else {
612       $type= "goFaxRBlock";
613     }
615     /* Add list */
616     $this->attrs['objectClass']= $type;
617     if (count($this->goFaxBlocklist)){
618       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
619     }
621     /* Write back to ldap */
622     $ldap= $this->config->get_ldap_link();
623     $ldap->cd($this->base);
624     $ldap->search("cn=$this->cn",array("cn"));
625     $ldap->cat($this->dn, array('dn'));
626     $a= $ldap->fetch();
628     if (count($a)){
629       if (!isset($this->attrs["$type"."list"])){
630         $this->attrs["$type"."list"]= array();
631       }
632       $ldap->cd($this->dn);
633       $this->cleanup();
634       $ldap->modify($this->attrs);
635       new log("modify","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
636       $this->handle_post_events("modify");
637     } else {
638       $ldap->cd($this->config->current['BASE']);
639       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
640       $ldap->cd($this->dn);
641       $ldap->add($this->attrs);
642       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
643       $this->handle_post_events("add");
644     }
645     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
646   }
649   /* Add number */
650   function addNumber($number)
651   {
652     if (!in_array($number, $this->goFaxBlocklist)){
653       $this->goFaxBlocklist[]= $number;
654       sort($this->goFaxBlocklist);
655     }
656   }
659   /* Remove number from list */
660   function delNumber($numbers)
661   {
662     $tmp= array();
663     foreach ($this->goFaxBlocklist as $val){
664       if (!in_array($val, $numbers)){
665         $tmp[]= $val;
666       }
667     }
668     $this->goFaxBlocklist= $tmp;
669   }
672   /* Delete lock */
673   function remove_lock()
674   {
675     if (isset($this->dn)){
676       del_lock ($this->dn);
677     }
678   }
680   /* Return plugin informations for acl handling */
681   function plInfo()
682   {
683     return (array(  
684           "plShortName"       => _("Fax"),
685           "plDescription"     => _("Fax blocklists"),
686           "plSelfModify"      => FALSE,
687           "plDepends"         => array(),
689           "plPriority"    => 0,
690           "plSection"     => array("administration" => _("FAX Blocklists")),
691           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
692                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
693           "plProvidedAcls" => array(
694             "cn"              => _("Name"),
695             "description"     => _("Description"),
696             "base"            => _("Base"),
697             "goFaxBlocklist"  => _("Blocklist"),
698             "type"            => _("Blocklist type"))
699           ));
700   }
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>