Code

Generic FAX section for acl's
[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']) || isset($_POST['edit_apply'])) && (!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       
239         if (!isset($_POST['edit_apply'])){
240           $this->dn= "";
241           del_lock ($this->ui->dn);
242           unset($_SESSION['objectinfo']);
243         }
244       } else {
245         /* Errors found, show message */
246         show_errors ($message);
247       }
248     }
251     /***************
252       Object currently in edit  
253      ***************/
255     if($this->dn){
257       /* Base select dialog */
258       $once = true;
259       foreach($_POST as $name => $value){
260         if(preg_match("/^chooseBase/",$name) && $once){
261           $once = false;
262           $this->dialog = new baseSelectDialog($this->config,$this);
263           $this->dialog->setCurrentBase($this->base);
264         }
265       }
267       /* Dialog handling */
268       if(is_object($this->dialog)){
269         /* Must be called before save_object */
270         $this->dialog->save_object();
272         if($this->dialog->isClosed()){
273           $this->dialog = false;
274         }elseif($this->dialog->isSelected()){
275           $this->base = $this->dialog->isSelected();
276           $this->dialog= false;
277         }else{
278           return($this->dialog->execute());
279         }
280       }
282       /* Set base */
283       if ($this->base == ""){
284         if ($this->dn == "new"){
285           $ui= get_userinfo();
286           $this->base= dn2base($ui->dn);
287         } else {
288           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
289         }
290       }
292       /* Fill templating stuff */
293       $smarty->assign("bases", $this->config->idepartments);
294       $smarty->assign("base_select", $this->base);
295       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
296       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
297         $smarty->assign("selectmode", "");
298         $smarty->assign("mode", "");
299         $smarty->assign("apply", "0");
300       } else {
301         $smarty->assign("selectmode", "disabled");
302         $smarty->assign("mode", "readonly");
303         $smarty->assign("apply", "1");
304       }
305       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
306         $smarty->assign("$val", $this->$val);
307         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
308       }
310       /* Lock blocklist type for non new entries */
311       if ($this->dn != "new"){
312         $smarty->assign('typeACL', "disabled");
313       }
315       /* Show main page */
316       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
318     }else{
320       /***************
321         Divlist dialog 
322        ***************/
324       /* Check if there is a snapshot dialog open */
325       $base = $this->DivListBlocklist->selectedBase;
326       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
327         return($str);
328       }
330       /* Display dialog with system list */
331       $this->DivListBlocklist->parent = $this;
332       $this->DivListBlocklist->execute();
334       /* Add departments if subsearch is disabled */
335       if(!$this->DivListBlocklist->SubSearch){
336         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
337       }
338       $this->reload();
339       $this->DivListBlocklist->setEntries($this->blocklists);
340       return($this->DivListBlocklist->Draw());
341     }
342   }
345   /* Return departments, that will be included within snapshot detection */
346   function get_used_snapshot_bases()
347   {
348     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
349   }
352   /* Clear garbage from edited lists */
353   function clear_fields()
354   {
355     $this->dn= "";
356     $this->description= "";
357     $this->cn= "";
358     $this->base= $_SESSION['CurrentMainBase'];
359     $this->goFaxBlocklist= array();
360   }
363   /* Reload the list of known blocklists */
364   function reload()
365   {
366     /* Init some vars */
367     $filter = $filter2      = "";
368     $base                   = $this->DivListBlocklist->selectedBase;
369     $Regex                  = $this->DivListBlocklist->Regex;
370     $SubSearch              = $this->DivListBlocklist->SubSearch;
371     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
372     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
373     $Flags                  = GL_SIZELIMIT;
374     $res = $res2            = array();
376     /* Append subsearch to Flags */
377     if($SubSearch){
378       $Flags |= GL_SUBSEARCH;
379     }else{
380       $base = "ou=gofax,ou=systems,".$base;
381     }  
383     /* Create filter */
384     if ($ShowSendBocklists){
385       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
386       $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
387     }
388     if ($ShowReceiveBlocklists){
389       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
390       $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
391     }
392    
393     $this->blocklists = array_merge($res,$res2);
395     /* appen && sort */
396     $tmp=array();
397     foreach($this->blocklists as $tkey => $val ){
398       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
399     }
400     ksort($tmp);
401     $this->blocklists=array();
402     foreach($tmp as $val){
403       $this->blocklists[]=$val;
404     }
405     reset ($this->blocklists);
406   }
409   function remove_from_parent()
410   {
411     $ldap= $this->config->get_ldap_link();
412     $ldap->rmDir($this->dn);
413     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
414     $this->clear_fields();
415     $this->handle_post_events("remove");
416   }
419   /* Save data to object */
420   function save_object()
421   {
422     $this->DivListBlocklist->save_object();
423     plugin::save_object();
425     if(isset($_POST['base'])){
426       $this->base = $_POST['base'];
427     }
428     foreach($this->attributes as $attr){
429       if(isset($_POST[$attr])){
430         $this->$attr = $_POST[$attr];
431       }
432     }
433   }
436   /* Check values */
437   function check()
438   {
439     /* Call common method to give check the hook */
440     $message= plugin::check();
442     /* Permissions for that base? */
443     if ($this->base != ""){
444       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
445     } else {
446       $new_dn= $this->dn;
447     }
449     if (chkacl($this->acl, "create") != ""){
450       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
451     }
454     /* check syntax: must cn */
455     if ($this->cn == ""){
456       $message[]= _("Required field 'Name' is not set.");
457     } else {
458       if (!is_uid($this->cn)){
459         $message[]= _("Required field 'Name' contains invalid characters");
460       }
461       if ($this->dn == 'new'){
462         $ldap= $this->config->get_ldap_link();
463         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
464         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
465         if ($ldap->count() != 0){
466           $message[]= _("Specified name is already used.");
467         }
468       }
469     }
471     /* Is user allowed to create in 'base'? */
472     if (chkacl($this->acl, "create") != ""){
473       $message[]= _("No permission to create a blocklist on this base.");
474     }
476     return $message;
477   }
480   /* Save to LDAP */
481   function save()
482   {
483     plugin::save();
486     /* Type selection */
487     if ($this->type == 0){
488       $type= "goFaxSBlock";
489     } else {
490       $type= "goFaxRBlock";
491     }
493     /* Add list */
494     $this->attrs['objectClass']= $type;
495     if (count($this->goFaxBlocklist)){
496       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
497     }
499     /* Write back to ldap */
500     $ldap= $this->config->get_ldap_link();
501     $ldap->cd($this->base);
502     $ldap->search("cn=$this->cn",array("cn"));
503     $ldap->cat($this->dn, array('dn'));
504     $a= $ldap->fetch();
506     if (count($a)){
507       if (!isset($this->attrs["$type"."list"])){
508         $this->attrs["$type"."list"]= array();
509       }
510       $ldap->cd($this->dn);
511       $this->cleanup();
512       $ldap->modify($this->attrs);
513       $this->handle_post_events("modify");
514     } else {
515       $ldap->cd($this->config->current['BASE']);
516       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
517       $ldap->cd($this->dn);
518       $ldap->add($this->attrs);
519       $this->handle_post_events("add");
520     }
521     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
522   }
525   /* Add number */
526   function addNumber($number)
527   {
528     if (!in_array($number, $this->goFaxBlocklist)){
529       $this->goFaxBlocklist[]= $number;
530       sort($this->goFaxBlocklist);
531     }
532   }
535   /* Remove number from list */
536   function delNumber($numbers)
537   {
538     $tmp= array();
539     foreach ($this->goFaxBlocklist as $val){
540       if (!in_array($val, $numbers)){
541         $tmp[]= $val;
542       }
543     }
544     $this->goFaxBlocklist= $tmp;
545   }
548   /* Delete lock */
549   function remove_lock()
550   {
551     if (isset($this->dn)){
552       del_lock ($this->dn);
553     }
554   }
556   /* Return plugin informations for acl handling */
557   function plInfo()
558   {
559     return (array(  
560           "plDescription"     => _("Fax Blocklists"),
561           "plSelfModify"      => TRUE,
562           "plDepends"         => array("objectClass" => "goFaxSBlock", "description"=> _("FAX")),
563           "cn"              => _("Name"),
564           "description"     => _("Description"),
565           "base"            => _("Base"),
566           "type"            => _("Blocklist type")));
567   }
570 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
571 ?>