Code

Updated icon menu
[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   /* blocklist attributes */
9   var $cn               = "";
10   var $description      = "";
11   var $type             = "";
12   var $base             = "";
14   var $goFaxBlocklist   = array();
15   var $blocklists       = array();
16   var $attributes       = array("cn", "description");
17   var $objectclasses    = array();
18   var $DivListBlocklist = NULL;
20   function blocklist ($config, $ui)
21   {
22     /* Init class */
23     $this->ui     = $ui;
24     $this->dn     = "";
25     $this->config = $config;
27     $ui           = get_userinfo();
28     $acl          = get_permissions ($ui->dn, $ui->subtreeACL);
29     $this->acl    = get_module_permission($acl, "blocklists", $ui->dn);
31     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
32   }
34   function execute()
35   {
36     /* Call parent execute */
37     plugin::execute();
39     /***************
40       Init vars 
41      ***************/
43     /* Get global smarty instance */
44     $smarty       = get_smarty();
45     $s_action     = "";                       // Contains the action to proceed
46     $s_entry      = "";                       // The value for s_action
47     $base_back    = "";                       // The Link for Backbutton
50     /***************
51       Fetch posts 
52      ***************/
54     /* Test Posts */
55     foreach($_POST as $key => $val){
56       // Post for delete
57       if(preg_match("/user_del.*/",$key)){
58         $s_action = "del";
59         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
60         // Post for edit
61       }elseif(preg_match("/user_edit_.*/",$key)){
62         $s_action="edit";
63         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
64         // Post for new
65       }elseif(preg_match("/user_new.*/",$key)){
66         $s_action="new";
67       }elseif(preg_match("/user_tplnew.*/i",$key)){
68         $s_action="new_tpl";
69       }elseif(preg_match("/user_chgpw.*/i",$key)){
70         $s_action="change_pw";
71         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
72       }
73     }
75     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
76       $s_action ="edit";
77       $s_entry  = $_GET['id'];
78     }
80     $s_entry  = preg_replace("/_.$/","",$s_entry);
83     /***************
84       Cancel some dialogs  
85      ***************/
87     /* Cancel dialog */
88     if (isset($_POST['edit_cancel']) || 
89         isset($_POST['delete_blocklist_cancel']) ||
90         isset($_POST['delete_blocklist_confirm']) || 
91         isset($_POST['delete_lock'])){
93       if(!isset($_POST['delete_blocklist_confirm'])){
94         $this->clear_fields();
95       }
96       del_lock ($this->dn);
97       unset($_SESSION['objectinfo']);
98     }
101     /***************
102       Cancel some dialogs  
103      ***************/
105     /* Create new blocklist? */
106     if ($s_action=="new"){
107       $this->clear_fields();
108       $this->dn= "new";
109       plugin::plugin ($this->config, $this->dn);
110     }
113     /***************
114       Edit blocklist 
115      ***************/
117     /* Edit selected blocklist? */
118     if (($s_action=="edit") && (empty($this->dn))){
119       $this->clear_fields();
120       $this->dn=$this->blocklists[$s_entry]['dn']; 
121       if (($user= get_lock($this->dn)) != ""){
122         $_SESSION['dn']= $this->dn;
123         //$this->dn ="";
124         return(gen_locked_message($user, $this->dn));
125       } else {
126 # Lock this dn for editing
127         add_lock ($this->dn, $this->ui->dn);
129         plugin::plugin ($this->config, $this->dn);
131         /* Load missing lists */
132         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
133           if(isset($this->attrs["goFaxSBlocklist"])){
134             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
135               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
136             }
137           }
138           $this->type= 0;
139         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
140           if(isset($this->attrs["goFaxRBlocklist"])){
141             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
142               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
143             }
144           }
145           $this->type= 1;
146         }
147         $_SESSION['objectinfo']= trim($this->dn);
148       }
149     }
152     /***************
153       Remove blocklist
154      ***************/
156     /* Delete blocklist requested */
157     if ($s_action=="del"){
158       $this->dn=$this->blocklists[$s_entry]['dn'];
159       /* Check locking */
160       if (($user= get_lock($this->dn)) != ""){
161         $_SESSION['dn']= $this->dn;
162         return(gen_locked_message($user, $this->dn));
163       } else {
164 # Lock this dn for editing
165         add_lock ($this->dn, $this->ui->dn);
167         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
168         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
169       }
170     }
173     /***************
174       Remove blocklist confirmed
175      ***************/
177     /* Finally delete blocklist */
178     if (isset($_POST['delete_blocklist_confirm'])){
179       if (chkacl($this->acl, "delete") == ""){
180         $this->remove_from_parent();
181         gosa_log ("Blocklist object'".$this->dn."' has been removed");
182         $this->reload ();
183       } else {
184         print_red (_("You have no permission to remove this blocklist."));
185       }
186     }
189     /***************
190       Add numer to blocklist
191      ***************/
194     /* Handle interactions: add */
195     if (isset($_POST['add_number']) && $_POST['number'] != ""){
196       if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
197         $this->addNumber ($_POST['number']);
198       } else {
199         print_red (_("Please specify a valid phone number."));
200       }
201     }
204     /***************
205       Delete number from list
206      ***************/
208     /* Handle interactions: delete */
209     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
210       $this->delNumber ($_POST['numbers']);
211     }
214     /***************
215       Edit finished
216      ***************/
218     /* What about finish? */
219     if ((isset($_POST['edit_finish'])) && (!empty($this->dn))){
220       $message= $this->check();
221       $this->remove_lock();
223       /* No errors, save object */
224       if (count ($message) == 0){
226         /* Perpare 'dn' in case of new entries */
227         if ($this->dn == "new"){
228           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
229         }
231         $this->save ();
232         gosa_log ("Blocklist object'".$this->dn."' has been saved");
233         $this->dn= "";
234         del_lock ($this->ui->dn);
235         unset($_SESSION['objectinfo']);
236       } else {
237         /* Errors found, show message */
238         show_errors ($message);
239       }
240     }
243     /***************
244       Object currently in edit  
245      ***************/
247     if($this->dn){
249       /* Base select dialog */
250       $once = true;
251       foreach($_POST as $name => $value){
252         if(preg_match("/^chooseBase/",$name) && $once){
253           $once = false;
254           $this->dialog = new baseSelectDialog($this->config);
255           $this->dialog->setCurrentBase($this->base);
256         }
257       }
259       /* Dialog handling */
260       if(is_object($this->dialog)){
261         /* Must be called before save_object */
262         $this->dialog->save_object();
264         if($this->dialog->isClosed()){
265           $this->dialog = false;
266         }elseif($this->dialog->isSelected()){
267           $this->base = $this->dialog->isSelected();
268           $this->dialog= false;
269         }else{
270           return($this->dialog->execute());
271         }
272       }
274       /* Set base */
275       if ($this->base == ""){
276         if ($this->dn == "new"){
277           $ui= get_userinfo();
278           $this->base= dn2base($ui->dn);
279         } else {
280           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
281         }
282       }
284       /* Fill templating stuff */
285       $smarty->assign("bases", $this->config->idepartments);
286       $smarty->assign("base_select", $this->base);
287       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
288       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
289         $smarty->assign("selectmode", "");
290         $smarty->assign("mode", "");
291       } else {
292         $smarty->assign("selectmode", "disabled");
293         $smarty->assign("mode", "readonly");
294       }
295       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
296         $smarty->assign("$val", $this->$val);
297         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
298       }
300       /* Lock blocklist type for non new entries */
301       if ($this->dn != "new"){
302         $smarty->assign('typeACL', "disabled");
303       }
305       /* Show main page */
306       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
308     }else{
310       /***************
311         Divlist dialog 
312        ***************/
314       /* Display dialog with system list */
315       $this->DivListBlocklist->parent = $this;
316       $this->DivListBlocklist->execute();
318       /* Add departments if subsearch is disabled */
319       if(!$this->DivListBlocklist->SubSearch){
320         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
321       }
322       $this->reload();
323       $this->DivListBlocklist->setEntries($this->blocklists);
324       return($this->DivListBlocklist->Draw());
325     }
326   }
329   /* Clear garbage from edited lists */
330   function clear_fields()
331   {
332     $this->dn= "";
333     $this->description= "";
334     $this->cn= "";
335     $this->base= $_SESSION['CurrentMainBase'];
336     $this->goFaxBlocklist= array();
337   }
340   /* Reload the list of known blocklists */
341   function reload()
342   {
343     /* Init some vars */
344     $filter = $filter2      = "";
345     $base                   = $this->DivListBlocklist->selectedBase;
346     $Regex                  = $this->DivListBlocklist->Regex;
347     $SubSearch              = $this->DivListBlocklist->SubSearch;
348     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
349     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
350     $Flags                  = GL_SIZELIMIT;
351     $res = $res2            = array();
353     /* Append subsearch to Flags */
354     if($SubSearch){
355       $Flags |= GL_SUBSEARCH;
356     }else{
357       $base = "ou=gofax,ou=systems,".$base;
358     }  
360     /* Create filter */
361     if ($ShowSendBocklists){
362       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
363       $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
364     }
365     if ($ShowReceiveBlocklists){
366       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
367       $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
368     }
369    
370     $this->blocklists = array_merge($res,$res2);
372     /* appen && sort */
373     $tmp=array();
374     foreach($this->blocklists as $tkey => $val ){
375       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
376     }
377     ksort($tmp);
378     $this->blocklists=array();
379     foreach($tmp as $val){
380       $this->blocklists[]=$val;
381     }
382     reset ($this->blocklists);
383   }
386   function remove_from_parent()
387   {
388     $ldap= $this->config->get_ldap_link();
389     $ldap->rmDir($this->dn);
390     show_ldap_error($ldap->get_error(), _("Removing blocklist object failed"));
391     $this->clear_fields();
392     $this->handle_post_events("remove");
393   }
396   /* Save data to object */
397   function save_object()
398   {
399     $this->DivListBlocklist->save_object();
400     plugin::save_object();
402     if(isset($_POST['base'])){
403       $this->base = $_POST['base'];
404     }
405     foreach($this->attributes as $attr){
406       if(isset($_POST[$attr])){
407         $this->$attr = $_POST[$attr];
408       }
409     }
410     if(isset($_POST['type'])){
411       $this->type = $_POST['type'];
412     }
414   }
417   /* Check values */
418   function check()
419   {
420     /* Call common method to give check the hook */
421     $message= plugin::check();
423     /* Permissions for that base? */
424     if ($this->base != ""){
425       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
426     } else {
427       $new_dn= $this->dn;
428     }
430     if (chkacl($this->acl, "create") != ""){
431       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
432     }
435     /* check syntax: must cn */
436     if ($this->cn == ""){
437       $message[]= _("Required field 'Name' is not set.");
438     } else {
439       if (!is_uid($this->cn)){
440         $message[]= _("Required field 'Name' contains invalid characters");
441       }
442       if ($this->dn == 'new'){
443         $ldap= $this->config->get_ldap_link();
444         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
445         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
446         if ($ldap->count() != 0){
447           $message[]= _("Specified name is already used.");
448         }
449       }
450     }
452     /* Is user allowed to create in 'base'? */
453     if (chkacl($this->acl, "create") != ""){
454       $message[]= _("No permission to create a blocklist on this base.");
455     }
457     return $message;
458   }
461   /* Save to LDAP */
462   function save()
463   {
464     plugin::save();
467     /* Type selection */
468     if ($this->type == 0){
469       $type= "goFaxSBlock";
470     } else {
471       $type= "goFaxRBlock";
472     }
474     /* Add list */
475     $this->attrs['objectClass']= $type;
476     if (count($this->goFaxBlocklist)){
477       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
478     }
480     /* Write back to ldap */
481     $ldap= $this->config->get_ldap_link();
482     $ldap->cd($this->base);
483     $ldap->search("cn=$this->cn",array("cn"));
484     $ldap->cat($this->dn, array('dn'));
485     $a= $ldap->fetch();
487     if (count($a)){
488       if (!isset($this->attrs["$type"."list"])){
489         $this->attrs["$type"."list"]= array();
490       }
491       $ldap->cd($this->dn);
492       $this->cleanup();
493       $ldap->modify($this->attrs);
494       $this->handle_post_events("modify");
495     } else {
496       $ldap->cd($this->config->current['BASE']);
497       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
498       $ldap->cd($this->dn);
499       $ldap->add($this->attrs);
500       $this->handle_post_events("add");
501     }
502     show_ldap_error($ldap->get_error(), _("Saving blocklist object failed"));
503   }
506   /* Add number */
507   function addNumber($number)
508   {
509     if (!in_array($number, $this->goFaxBlocklist)){
510       $this->goFaxBlocklist[]= $number;
511       sort($this->goFaxBlocklist);
512     }
513   }
516   /* Remove number from list */
517   function delNumber($numbers)
518   {
519     $tmp= array();
520     foreach ($this->goFaxBlocklist as $val){
521       if (!in_array($val, $numbers)){
522         $tmp[]= $val;
523       }
524     }
525     $this->goFaxBlocklist= $tmp;
526   }
529   /* Delete lock */
530   function remove_lock()
531   {
532     if (isset($this->dn)){
533       del_lock ($this->dn);
534     }
535   }
538 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
539 ?>