Code

Fixed fax report / blocklist acls
[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     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
33   }
35   function execute()
36   {
37     /* Call parent execute */
38     plugin::execute();
40     /***************
41       Init vars 
42      ***************/
44     /* Get global smarty instance */
45     $smarty       = get_smarty();
46     $s_action     = "";                       // Contains the action to proceed
47     $s_entry      = "";                       // The value for s_action
48     $base_back    = "";                       // The Link for Backbutton
51     /***************
52       Fetch posts 
53      ***************/
55     /* Test Posts */
56     foreach($_POST as $key => $val){
57       // Post for delete
58       if(preg_match("/user_del.*/",$key)){
59         $s_action = "del";
60         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
61         // Post for edit
62       }elseif(preg_match("/user_edit_.*/",$key)){
63         $s_action="edit";
64         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
65         // Post for new
66       }elseif(preg_match("/user_new.*/",$key)){
67         $s_action="new";
68       }elseif(preg_match("/user_tplnew.*/i",$key)){
69         $s_action="new_tpl";
70       }elseif(preg_match("/user_chgpw.*/i",$key)){
71         $s_action="change_pw";
72         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
73       }
74     }
76     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
77       $s_action ="edit";
78       $s_entry  = $_GET['id'];
79     }
81     $s_entry  = preg_replace("/_.$/","",$s_entry);
84     /***************
85       Cancel some dialogs  
86      ***************/
88     /* Cancel dialog */
89     if (isset($_POST['edit_cancel']) || 
90         isset($_POST['delete_blocklist_cancel']) ||
91         isset($_POST['delete_blocklist_confirm']) || 
92         isset($_POST['delete_lock'])){
94       if(!isset($_POST['delete_blocklist_confirm'])){
95         $this->clear_fields();
96       }
97       del_lock ($this->dn);
98       unset($_SESSION['objectinfo']);
99     }
102     /***************
103       Create a new blocklist (dialog)
104      ***************/
106     /* Create new blocklist? */
107     if ($s_action=="new"){
108       $this->clear_fields();
109       $this->dn= "new";
110       plugin::plugin ($this->config, $this->dn);
111     }
114     /***************
115       Edit blocklist 
116      ***************/
118     /* Edit selected blocklist? */
119     if (($s_action=="edit") && (empty($this->dn))){
120       $this->clear_fields();
121       $this->dn=$this->blocklists[$s_entry]['dn']; 
122       if (($user= get_lock($this->dn)) != ""){
123         $_SESSION['dn']= $this->dn;
124         //$this->dn ="";
125         return(gen_locked_message($user, $this->dn));
126       } else {
127 # Lock this dn for editing
128         add_lock ($this->dn, $this->ui->dn);
130         plugin::plugin ($this->config, $this->dn);
132         /* Load missing lists */
133         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
134           if(isset($this->attrs["goFaxSBlocklist"])){
135             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
136               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
137             }
138           }
139           $this->type= 0;
140         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
141           if(isset($this->attrs["goFaxRBlocklist"])){
142             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
143               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
144             }
145           }
146           $this->type= 1;
147         }
148         $_SESSION['objectinfo']= trim($this->dn);
149       }
150     }
153     /***************
154       Remove blocklist
155      ***************/
157     /* Delete blocklist requested */
158     if ($s_action=="del"){
159       $this->dn=$this->blocklists[$s_entry]['dn'];
160       /* Check locking */
161       if (($user= get_lock($this->dn)) != ""){
162         $_SESSION['dn']= $this->dn;
163         return(gen_locked_message($user, $this->dn));
164       } else {
165 # Lock this dn for editing
166         add_lock ($this->dn, $this->ui->dn);
168         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
169         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
170       }
171     }
174     /***************
175       Remove blocklist confirmed
176      ***************/
178     /* Finally delete blocklist */
179     if (isset($_POST['delete_blocklist_confirm'])){
180       if ($this->acl_is_removeable()){
181         $this->remove_from_parent();
182         gosa_log ("Blocklist object'".$this->dn."' has been removed");
183         $this->reload ();
184       } else {
185         print_red (_("You have no permission to remove this blocklist."));
186       }
187     }
190     /***************
191       Add numer to blocklist
192      ***************/
195     /* Handle interactions: add */
196     if (isset($_POST['add_number']) && $_POST['number'] != ""){
197       if (is_phone_nr($_POST['number'])){
198         $this->addNumber ($_POST['number']);
199       } else {
200         print_red (_("Please specify a valid phone number."));
201       }
202     }
205     /***************
206       Delete number from list
207      ***************/
209     /* Handle interactions: delete */
210     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
211       $this->delNumber ($_POST['numbers']);
212     }
215     /***************
216       Edit finished
217      ***************/
219     /* What about finish? */
220     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
221       $message= $this->check();
222       $this->remove_lock();
224       /* No errors, save object */
225       if (count ($message) == 0){
227         /* Perpare 'dn' in case of new entries */
228         if ($this->dn == "new"){
229           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
230         }
232         $this->save ();
233         gosa_log ("Blocklist object'".$this->dn."' has been saved");
234       
235         if (!isset($_POST['edit_apply'])){
236           $this->dn= "";
237           del_lock ($this->ui->dn);
238           unset($_SESSION['objectinfo']);
239         }
240       } else {
241         /* Errors found, show message */
242         show_errors ($message);
243       }
244     }
247     /***************
248       Object currently in edit  
249      ***************/
251     if($this->dn){
253       $dn = $this->dn;
254       if($dn == "new"){
255         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
256       }
258       $this->set_acl_category("gofaxlist");
259       $this->set_acl_base($dn);
262       /* Base select dialog */
263       $once = true;
264       foreach($_POST as $name => $value){
265         if(preg_match("/^chooseBase/",$name) && $once){
266           $once = false;
267           $this->dialog = new baseSelectDialog($this->config,$this);
268           $this->dialog->setCurrentBase($this->base);
269         }
270       }
272       /* Dialog handling */
273       if(is_object($this->dialog)){
274         /* Must be called before save_object */
275         $this->dialog->save_object();
277         if($this->dialog->isClosed()){
278           $this->dialog = false;
279         }elseif($this->dialog->isSelected()){
280           $this->base = $this->dialog->isSelected();
281           $this->dialog= false;
282         }else{
283           return($this->dialog->execute());
284         }
285       }
287       /* Set base */
288       if ($this->base == ""){
289         if ($this->dn == "new"){
290           $ui= get_userinfo();
291           $this->base= dn2base($ui->dn);
292         } else {
293           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
294         }
295       }
297       /* Fill templating stuff */
298       $smarty->assign("bases", $this->get_allowed_bases());
299       $smarty->assign("base_select", $this->base);
300       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
301       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
302         $smarty->assign("selectmode", "");
303         $smarty->assign("mode", "");
304         $smarty->assign("apply", "0");
305       } else {
306         $smarty->assign("selectmode", "disabled");
307         $smarty->assign("mode", "readonly");
308         $smarty->assign("apply", "1");
309       }
311       $tmp = $this->plInfo();
312       foreach($tmp['plProvidedAcls'] as $name => $translation){
313         $smarty->assign($name."ACL",$this->getacl($name));
314       }
316       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
317         $smarty->assign("$val", $this->$val);
318       }
320       $baseACL = $this->getacl("base");
321       if(!$this->acl_is_moveable()) {
322         $baseACL = preg_replace("/w/","",$baseACL);
323       }
324       $smarty->assign("baseACL",          $baseACL);
326       /* Lock blocklist type for non new entries */
327       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
329       /* Show main page */
330       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
332     }else{
334       /***************
335         Divlist dialog 
336        ***************/
338       /* Check if there is a snapshot dialog open */
339       $base = $this->DivListBlocklist->selectedBase;
340       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
341         return($str);
342       }
344       /* Display dialog with system list */
345       $this->DivListBlocklist->parent = $this;
346       $this->DivListBlocklist->execute();
348       /* Add departments if subsearch is disabled */
349       if(!$this->DivListBlocklist->SubSearch){
350         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
351       }
352       $this->reload();
353       $this->DivListBlocklist->setEntries($this->blocklists);
354       return($this->DivListBlocklist->Draw());
355     }
356   }
359   /* Return departments, that will be included within snapshot detection */
360   function get_used_snapshot_bases()
361   {
362     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
363   }
366   /* Clear garbage from edited lists */
367   function clear_fields()
368   {
369     $this->dn= "";
370     $this->description= "";
371     $this->cn= "";
372     $this->base= $_SESSION['CurrentMainBase'];
373     $this->goFaxBlocklist= array();
374   }
377   /* Reload the list of known blocklists */
378   function reload()
379   {
380     /* Init some vars */
381     $filter = $filter2      = "";
382     $base                   = $this->DivListBlocklist->selectedBase;
383     $Regex                  = $this->DivListBlocklist->Regex;
384     $SubSearch              = $this->DivListBlocklist->SubSearch;
385     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
386     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
387     $Flags                  = GL_SIZELIMIT;
388     $res = $res2            = array();
390     /* Append subsearch to Flags */
391     if($SubSearch){
392       $Flags |= GL_SUBSEARCH;
393     }else{
394       $base = "ou=gofax,ou=systems,".$base;
395     }  
397     /* Create filter */
398     if ($ShowSendBocklists){
399       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
400       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
401     }
402     if ($ShowReceiveBlocklists){
403       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
404       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
405     }
406    
407     $this->blocklists = array_merge($res,$res2);
409     /* appen && sort */
410     $tmp=array();
411     foreach($this->blocklists as $tkey => $val ){
413       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
414       if(preg_match("/r/",$acl)){
415         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
416       }
417     }
418     ksort($tmp);
419     $this->blocklists=array();
420     foreach($tmp as $val){
421       $this->blocklists[]=$val;
422     }
423     reset ($this->blocklists);
424   }
427   function remove_from_parent()
428   {
429     $ldap= $this->config->get_ldap_link();
430     $ldap->rmDir($this->dn);
431     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
432     $this->clear_fields();
433     $this->handle_post_events("remove");
434   }
437   /* Save data to object */
438   function save_object()
439   {
440     $this->DivListBlocklist->save_object();
441     plugin::save_object();
443     $baseACL = $this->getacl("base");
444     if(!$this->acl_is_moveable()) {
445       $baseACL = preg_replace("/w/","",$baseACL);
446     }
448     if(isset($_POST['base']) && preg_match("/w/",$baseACL)){
449       $this->base = $_POST['base'];
450     }
451     
452     foreach($this->attributes as $attr){
453       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
454         $this->$attr = $_POST[$attr];
455       }
456     }
457     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
458       $this->type = $_POST['type'];
459     }
460   }
463   /* Check values */
464   function check()
465   {
466     /* Call common method to give check the hook */
467     $message= plugin::check();
469     /* Permissions for that base? */
470     if ($this->base != ""){
471       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
472     } else {
473       $new_dn= $this->dn;
474     }
476     if (!$this->acl_is_createable() && $this->dn == "new"){
477       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
478     }
480     /* check syntax: must cn */
481     if ($this->cn == ""){
482       $message[]= _("Required field 'Name' is not set.");
483     } else {
484       if (!is_uid($this->cn)){
485         $message[]= _("Required field 'Name' contains invalid characters");
486       }
487       if ($this->dn == 'new'){
488         $ldap= $this->config->get_ldap_link();
489         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
490         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
491         if ($ldap->count() != 0){
492           $message[]= _("Specified name is already used.");
493         }
494       }
495     }
497     return $message;
498   }
501   /* Save to LDAP */
502   function save()
503   {
504     plugin::save();
507     /* Type selection */
508     if ($this->type == 0){
509       $type= "goFaxSBlock";
510     } else {
511       $type= "goFaxRBlock";
512     }
514     /* Add list */
515     $this->attrs['objectClass']= $type;
516     if (count($this->goFaxBlocklist)){
517       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
518     }
520     /* Write back to ldap */
521     $ldap= $this->config->get_ldap_link();
522     $ldap->cd($this->base);
523     $ldap->search("cn=$this->cn",array("cn"));
524     $ldap->cat($this->dn, array('dn'));
525     $a= $ldap->fetch();
527     if (count($a)){
528       if (!isset($this->attrs["$type"."list"])){
529         $this->attrs["$type"."list"]= array();
530       }
531       $ldap->cd($this->dn);
532       $this->cleanup();
533       $ldap->modify($this->attrs);
534       $this->handle_post_events("modify");
535     } else {
536       $ldap->cd($this->config->current['BASE']);
537       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
538       $ldap->cd($this->dn);
539       $ldap->add($this->attrs);
540       $this->handle_post_events("add");
541     }
542     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
543   }
546   /* Add number */
547   function addNumber($number)
548   {
549     if (!in_array($number, $this->goFaxBlocklist)){
550       $this->goFaxBlocklist[]= $number;
551       sort($this->goFaxBlocklist);
552     }
553   }
556   /* Remove number from list */
557   function delNumber($numbers)
558   {
559     $tmp= array();
560     foreach ($this->goFaxBlocklist as $val){
561       if (!in_array($val, $numbers)){
562         $tmp[]= $val;
563       }
564     }
565     $this->goFaxBlocklist= $tmp;
566   }
569   /* Delete lock */
570   function remove_lock()
571   {
572     if (isset($this->dn)){
573       del_lock ($this->dn);
574     }
575   }
577   /* Return plugin informations for acl handling */
578   function plInfo()
579   {
580     return (array(  
581           "plShortName"       => _("Fax"),
582           "plDescription"     => _("Fax blocklists"),
583           "plSelfModify"      => FALSE,
584           "plDepends"         => array(),
586           "plPriority"    => 0,
587           "plSection"     => array("administration" => _("FAX Blocklists")),
588           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
589                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
590           "plProvidedAcls" => array(
591             "cn"              => _("Name"),
592             "description"     => _("Description"),
593             "base"            => _("Base"),
594             "goFaxBlocklist"  => _("Blocklist"),
595             "type"            => _("Blocklist type"))
596           ));
597   }
600 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
601 ?>