Code

f5f523e8e4d4cc6bc365305703fef5d4701ac5a8
[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       /* Base select dialog */
255       $once = true;
256       foreach($_POST as $name => $value){
257         if(preg_match("/^chooseBase/",$name) && $once){
258           $once = false;
259           $this->dialog = new baseSelectDialog($this->config);
260           $this->dialog->setCurrentBase($this->base);
261         }
262       }
264       /* Dialog handling */
265       if(is_object($this->dialog)){
266         /* Must be called before save_object */
267         $this->dialog->save_object();
269         if($this->dialog->isClosed()){
270           $this->dialog = false;
271         }elseif($this->dialog->isSelected()){
272           $this->base = $this->dialog->isSelected();
273           $this->dialog= false;
274         }else{
275           return($this->dialog->execute());
276         }
277       }
279       /* Set base */
280       if ($this->base == ""){
281         if ($this->dn == "new"){
282           $ui= get_userinfo();
283           $this->base= dn2base($ui->dn);
284         } else {
285           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
286         }
287       }
289       /* Fill templating stuff */
290       $smarty->assign("bases", $this->config->idepartments);
291       $smarty->assign("base_select", $this->base);
292       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
293       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
294         $smarty->assign("selectmode", "");
295         $smarty->assign("mode", "");
296       } else {
297         $smarty->assign("selectmode", "disabled");
298         $smarty->assign("mode", "readonly");
299       }
300       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
301         $smarty->assign("$val", $this->$val);
302         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
303       }
305       /* Lock blocklist type for non new entries */
306       if ($this->dn != "new"){
307         $smarty->assign('typeACL', "disabled");
308       }
310       /* Show main page */
311       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
313     }else{
315       /***************
316         Divlist dialog 
317        ***************/
319       /* Display dialog with system list */
320       $this->DivListBlocklist->parent = $this;
321       $this->DivListBlocklist->execute();
323       /* Add departments if subsearch is disabled */
324       if(!$this->DivListBlocklist->SubSearch){
325         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
326       }
327       $this->reload();
328       $this->DivListBlocklist->setEntries($this->blocklists);
329       return($this->DivListBlocklist->Draw());
330     }
331   }
334   /* Clear garbage from edited lists */
335   function clear_fields()
336   {
337     $this->dn= "";
338     $this->description= "";
339     $this->cn= "";
340     $this->base= $_SESSION['CurrentMainBase'];
341     $this->goFaxBlocklist= array();
342   }
345   /* Reload the list of known blocklists */
346   function reload()
347   {
348     /* Init some vars */
349     $filter = $filter2      = "";
350     $base                   = $this->DivListBlocklist->selectedBase;
351     $Regex                  = $this->DivListBlocklist->Regex;
352     $SubSearch              = $this->DivListBlocklist->SubSearch;
353     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
354     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
355     $Flags                  = GL_SIZELIMIT;
356     $res = $res2            = array();
358     /* Append subsearch to Flags */
359     if($SubSearch){
360       $Flags |= GL_SUBSEARCH;
361     }else{
362       $base = "ou=gofax,ou=systems,".$base;
363     }  
365     /* Create filter */
366     if ($ShowSendBocklists){
367       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
368       $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
369     }
370     if ($ShowReceiveBlocklists){
371       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
372       $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
373     }
374    
375     $this->blocklists = array_merge($res,$res2);
377     /* appen && sort */
378     $tmp=array();
379     foreach($this->blocklists as $tkey => $val ){
380       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
381     }
382     ksort($tmp);
383     $this->blocklists=array();
384     foreach($tmp as $val){
385       $this->blocklists[]=$val;
386     }
387     reset ($this->blocklists);
388   }
391   function remove_from_parent()
392   {
393     $ldap= $this->config->get_ldap_link();
394     $ldap->rmDir($this->dn);
395     show_ldap_error($ldap->get_error(), _("Removing blocklist object failed"));
396     $this->clear_fields();
397     $this->handle_post_events("remove");
398   }
401   /* Save data to object */
402   function save_object()
403   {
404     $this->DivListBlocklist->save_object();
405     plugin::save_object();
407     if(isset($_POST['base'])){
408       $this->base = $_POST['base'];
409     }
410     foreach($this->attributes as $attr){
411       if(isset($_POST[$attr])){
412         $this->$attr = $_POST[$attr];
413       }
414     }
415   }
418   /* Check values */
419   function check()
420   {
421     /* Call common method to give check the hook */
422     $message= plugin::check();
424     /* Permissions for that base? */
425     if ($this->base != ""){
426       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
427     } else {
428       $new_dn= $this->dn;
429     }
431     if (chkacl($this->acl, "create") != ""){
432       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
433     }
436     /* check syntax: must cn */
437     if ($this->cn == ""){
438       $message[]= _("Required field 'Name' is not set.");
439     } else {
440       if (!is_uid($this->cn)){
441         $message[]= _("Required field 'Name' contains invalid characters");
442       }
443       if ($this->dn == 'new'){
444         $ldap= $this->config->get_ldap_link();
445         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
446         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
447         if ($ldap->count() != 0){
448           $message[]= _("Specified name is already used.");
449         }
450       }
451     }
453     /* Is user allowed to create in 'base'? */
454     if (chkacl($this->acl, "create") != ""){
455       $message[]= _("No permission to create a blocklist on this base.");
456     }
458     return $message;
459   }
462   /* Save to LDAP */
463   function save()
464   {
465     plugin::save();
468     /* Type selection */
469     if ($this->type == 0){
470       $type= "goFaxSBlock";
471     } else {
472       $type= "goFaxRBlock";
473     }
475     /* Add list */
476     $this->attrs['objectClass']= $type;
477     if (count($this->goFaxBlocklist)){
478       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
479     }
481     /* Write back to ldap */
482     $ldap= $this->config->get_ldap_link();
483     $ldap->cd($this->base);
484     $ldap->search("cn=$this->cn",array("cn"));
485     $ldap->cat($this->dn, array('dn'));
486     $a= $ldap->fetch();
488     if (count($a)){
489       if (!isset($this->attrs["$type"."list"])){
490         $this->attrs["$type"."list"]= array();
491       }
492       $ldap->cd($this->dn);
493       $this->cleanup();
494       $ldap->modify($this->attrs);
495       $this->handle_post_events("modify");
496     } else {
497       $ldap->cd($this->config->current['BASE']);
498       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
499       $ldap->cd($this->dn);
500       $ldap->add($this->attrs);
501       $this->handle_post_events("add");
502     }
503     show_ldap_error($ldap->get_error(), _("Saving blocklist object failed"));
504   }
507   /* Add number */
508   function addNumber($number)
509   {
510     if (!in_array($number, $this->goFaxBlocklist)){
511       $this->goFaxBlocklist[]= $number;
512       sort($this->goFaxBlocklist);
513     }
514   }
517   /* Remove number from list */
518   function delNumber($numbers)
519   {
520     $tmp= array();
521     foreach ($this->goFaxBlocklist as $val){
522       if (!in_array($val, $numbers)){
523         $tmp[]= $val;
524       }
525     }
526     $this->goFaxBlocklist= $tmp;
527   }
530   /* Delete lock */
531   function remove_lock()
532   {
533     if (isset($this->dn)){
534       del_lock ($this->dn);
535     }
536   }
539 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
540 ?>