Code

Updated Blocklist (divlist)
[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'])) && (!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         $this->dn= "";
239         del_lock ($this->ui->dn);
240         unset($_SESSION['objectinfo']);
241       } else {
242         /* Errors found, show message */
243         show_errors ($message);
244       }
245     }
248     /***************
249       Object currently in edit  
250      ***************/
252     if($this->dn){
254       /* Set base */
255       if ($this->base == ""){
256         if ($this->dn == "new"){
257           $ui= get_userinfo();
258           $this->base= dn2base($ui->dn);
259         } else {
260           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
261         }
262       }
264       /* Fill templating stuff */
265       $smarty->assign("bases", $this->config->idepartments);
266       $smarty->assign("base_select", $this->base);
267       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
268       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
269         $smarty->assign("selectmode", "");
270         $smarty->assign("mode", "");
271       } else {
272         $smarty->assign("selectmode", "disabled");
273         $smarty->assign("mode", "readonly");
274       }
275       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
276         $smarty->assign("$val", $this->$val);
277         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
278       }
280       /* Lock blocklist type for non new entries */
281       if ($this->dn != "new"){
282         $smarty->assign('typeACL', "disabled");
283       }
285       /* Show main page */
286       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
288     }else{
290       /***************
291         Divlist dialog 
292        ***************/
294       /* Display dialog with system list */
295       $this->DivListBlocklist->execute();
297       /* Add departments if subsearch is disabled */
298       if(!$this->DivListBlocklist->SubSearch){
299         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
300       }
301       $this->reload();
302       $this->DivListBlocklist->setEntries($this->blocklists);
303       return($this->DivListBlocklist->Draw());
304     }
305   }
308   /* Clear garbage from edited lists */
309   function clear_fields()
310   {
311     $this->dn= "";
312     $this->description= "";
313     $this->cn= "";
314     $this->base= $_SESSION['CurrentMainBase'];
315     $this->goFaxBlocklist= array();
316   }
319   /* Reload the list of known blocklists */
320   function reload()
321   {
322     /* Init some vars */
323     $filter = $filter2      = "";
324     $base                   = $this->DivListBlocklist->selectedBase;
325     $Regex                  = $this->DivListBlocklist->Regex;
326     $SubSearch              = $this->DivListBlocklist->SubSearch;
327     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
328     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
329     $Flags                  = GL_SIZELIMIT;
330     $res = $res2            = array();
332     /* Append subsearch to Flags */
333     if($SubSearch){
334       $Flags |= GL_SUBSEARCH;
335     }else{
336       $base = "ou=gofax,ou=systems,".$base;
337     }  
339     /* Create filter */
340     if ($ShowSendBocklists){
341       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
342       $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
343     }
344     if ($ShowReceiveBlocklists){
345       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
346       $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
347     }
348    
349     $this->blocklists = array_merge($res,$res2);
351     /* appen && sort */
352     $tmp=array();
353     foreach($this->blocklists as $tkey => $val ){
354       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
355     }
356     ksort($tmp);
357     $this->blocklists=array();
358     foreach($tmp as $val){
359       $this->blocklists[]=$val;
360     }
361     reset ($this->blocklists);
362   }
365   function remove_from_parent()
366   {
367     $ldap= $this->config->get_ldap_link();
368     $ldap->rmDir($this->dn);
369     show_ldap_error($ldap->get_error(), _("Removing blocklist object failed"));
370     $this->clear_fields();
371     $this->handle_post_events("remove");
372   }
375   /* Save data to object */
376   function save_object()
377   {
378     $this->DivListBlocklist->save_object();
379     plugin::save_object();
381     if(isset($_POST['base'])){
382       $this->base = $_POST['base'];
383     }
384     foreach($this->attributes as $attr){
385       if(isset($_POST[$attr])){
386         $this->$attr = $_POST[$attr];
387       }
388     }
389   }
392   /* Check values */
393   function check()
394   {
395     /* Call common method to give check the hook */
396     $message= plugin::check();
398     /* Permissions for that base? */
399     if ($this->base != ""){
400       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
401     } else {
402       $new_dn= $this->dn;
403     }
405     if (chkacl($this->acl, "create") != ""){
406       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
407     }
410     /* check syntax: must cn */
411     if ($this->cn == ""){
412       $message[]= _("Required field 'Name' is not set.");
413     } else {
414       if (!is_uid($this->cn)){
415         $message[]= _("Required field 'Name' contains invalid characters");
416       }
417       if ($this->dn == 'new'){
418         $ldap= $this->config->get_ldap_link();
419         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
420         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
421         if ($ldap->count() != 0){
422           $message[]= _("Specified name is already used.");
423         }
424       }
425     }
427     /* Is user allowed to create in 'base'? */
428     if (chkacl($this->acl, "create") != ""){
429       $message[]= _("No permission to create a blocklist on this base.");
430     }
432     return $message;
433   }
436   /* Save to LDAP */
437   function save()
438   {
439     plugin::save();
442     /* Type selection */
443     if ($this->type == 0){
444       $type= "goFaxSBlock";
445     } else {
446       $type= "goFaxRBlock";
447     }
449     /* Add list */
450     $this->attrs['objectClass']= $type;
451     if (count($this->goFaxBlocklist)){
452       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
453     }
455     /* Write back to ldap */
456     $ldap= $this->config->get_ldap_link();
457     $ldap->cd($this->base);
458     $ldap->search("cn=$this->cn",array("cn"));
459     $ldap->cat($this->dn, array('dn'));
460     $a= $ldap->fetch();
462     if (count($a)){
463       if (!isset($this->attrs["$type"."list"])){
464         $this->attrs["$type"."list"]= array();
465       }
466       $ldap->cd($this->dn);
467       $this->cleanup();
468       $ldap->modify($this->attrs);
469       $this->handle_post_events("modify");
470     } else {
471       $ldap->cd($this->config->current['BASE']);
472       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
473       $ldap->cd($this->dn);
474       $ldap->add($this->attrs);
475       $this->handle_post_events("add");
476     }
477     show_ldap_error($ldap->get_error(), _("Saving blocklist object failed"));
478   }
481   /* Add number */
482   function addNumber($number)
483   {
484     if (!in_array($number, $this->goFaxBlocklist)){
485       $this->goFaxBlocklist[]= $number;
486       sort($this->goFaxBlocklist);
487     }
488   }
491   /* Remove number from list */
492   function delNumber($numbers)
493   {
494     $tmp= array();
495     foreach ($this->goFaxBlocklist as $val){
496       if (!in_array($val, $numbers)){
497         $tmp[]= $val;
498       }
499     }
500     $this->goFaxBlocklist= $tmp;
501   }
504   /* Delete lock */
505   function remove_lock()
506   {
507     if (isset($this->dn)){
508       del_lock ($this->dn);
509     }
510   }
513 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
514 ?>