Code

Fixed Blocklists
[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 $goFaxBlocklist= array();
18   var $base= "";
20   /* Headpage attributes */
21   var $blocklists= array();
23   /* attribute list for save action */
24   var $attributes= array("cn", "description");
25   var $objectclasses= array();
27   function blocklist ($config, $ui)
28   {
29     $this->ui= $ui;
30     $this->dn= "";
31     $this->config= $config;
33     /* Get global filter config */
34     if (!is_global("blockfilter")){
35       $uii= get_userinfo();
36       $base= get_base_from_people($ui->dn);
37       $blockfilter= array("sendlists" => "checked",
38           "receivelists" => "checked",
39           "entry" => "*",
40           "depselect" => $base,
41           "regex" => "*");
42       register_global("blockfilter", $blockfilter);
43     }else{
44       $blockfilter = $_SESSION["blockfilter"];
45       $this->base = $blockfilter['depselect'];
46     }
47   }
49   function execute()
50   {
51     /* Get global smarty instance */
52     $smarty= get_smarty();
53     $blockfilter   = get_global("blockfilter"); // contains Filter Settings
54     $s_action     = "";                       // Contains the action to proceed
55     $s_entry      = "";                       // The value for s_action
56     $base_back    = "";                       // The Link for Backbutton
58     /* Start for New List Managment */
59     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
60       $s_action="open";
61       $s_entry = base64_decode($_GET['dep_id']);
62       $blockfilter['depselect']= "".$this->config->departments[trim($s_entry)];
63       $this->reload();
64     }
66     /* Test Posts */
67     foreach($_POST as $key => $val){
68       // Post for delete
69       if(preg_match("/user_del.*/",$key)){
70         $s_action = "del";
71         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
72         // Post for edit
73       }elseif(preg_match("/user_edit_.*/",$key)){
74         $s_action="edit";
75         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
76         // Post for new
77       }elseif(preg_match("/dep_back.*/i",$key)){
78         $s_action="back";
79       }elseif(preg_match("/user_new.*/",$key)){
80         $s_action="new";
81       }elseif(preg_match("/dep_home.*/i",$key)){
82         $s_action="home";
83       }elseif(preg_match("/user_tplnew.*/i",$key)){
84         $s_action="new_tpl";
85       }elseif(preg_match("/user_chgpw.*/i",$key)){
86         $s_action="change_pw";
87         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
88       }elseif(preg_match("/dep_root.*/i",$key)){
89         $s_action="root";
90       }
91     }
95     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
96       $s_action ="edit";
97       $s_entry  = $_GET['id'];
98     }
100     $s_entry  = preg_replace("/_.$/","",$s_entry);
102     /* Department changed? */
103     if(isset($_POST['depselect']) && $_POST['depselect']){
104       $blockfilter['depselect']= $_POST['depselect'];
105       $this->reload();
106     }
108     /* Homebutton is posted */
109     if($s_action=="home"){
110       $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
111       $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$blockfilter['depselect']));
112       $this->reload();
113     }
115     if($s_action=="root"){
116       $blockfilter['depselect']=($this->config->current['BASE']);
117       $this->reload();
118     }
119     
120     /* If Backbutton is Posted */
121     if($s_action=="back"){
122       $base_back          = preg_replace("/^[^,]+,/","",$blockfilter['depselect']);
123       $base_back          = convert_department_dn($base_back);
125       if(isset($this->config->departments[trim($base_back)])){
126         $blockfilter['depselect']= $this->config->departments[trim($base_back)];
127       }else{
128         $blockfilter['depselect']= $this->config->departments["/"];
129       }
130       $this->reload();
131     }
133     register_global("blockfilter", $blockfilter);
135     /* Save data */
136     $blockfilter= get_global("blockfilter");
137     foreach( array( "entry", "regex") as $type){
138       if (isset($_POST[$type])){
139         $blockfilter[$type]= $_POST[$type];
140       }
141     }
142     if (isset($_POST['depselect'])){
143       foreach( array("sendlists", "receivelists") as $type){
145         if (isset($_POST[$type])) {
146           $blockfilter[$type]= "checked";
147         } else {
148           $blockfilter[$type]= "";
149         }
150       }
151     }
152     if (isset($_GET['search'])){
153       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
154       if ($s == "**"){
155         $s= "*";
156       }
157       $blockfilter['regex']= $s;
158     }
159     register_global("blockfilter", $blockfilter);
161     /* Create new blocklist? */
162     if ($s_action=="new"){
163       $this->clear_fields();
164       $this->acl= "#all#";
165       $this->dn= "new";
166       plugin::plugin ($this->config, $this->dn);
167     }
169     /* Edit selected blocklist? */
170     if ($s_action=="edit"){
171       $this->clear_fields();
172       $this->dn=$this->blocklists[$s_entry]['dn']; 
173       $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
174       $this->acl= get_module_permission($this->acl, "blocklists", $this->dn);
175       plugin::plugin ($this->config, $this->dn);
177       /* Load missing lists */
178       if (isset($this->attrs["goFaxSBlocklist"])){
179         for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
180           $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
181         }
182         $this->type= 0;
183       } elseif (isset($this->attrs["goFaxRBlocklist"])){
184         for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
185           $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
186         }
187         $this->type= 1;
188       }
189       $_SESSION['objectinfo']= trim($this->dn);
190     }
192     /* Delete blocklist requested */
193     if ($s_action=="del"){
194       $this->dn=$this->blocklists[$s_entry]['dn'];
195       /* Check locking */
196       if (($user= get_lock($this->dn)) != ""){
197         $_SESSION['dn']= $this->dn;
198         return(gen_locked_message($user, $this->dn));
199       } else {
200 # Lock this dn for editing
201         add_lock ($this->dn, $this->ui->dn);
203         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
204         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
205       }
206     }
208     /* Finally delete blocklist */
209     if (isset($_POST['delete_blocklist_confirm'])){
210       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
211       $acl= get_module_permission($acl, "blocklists", $this->dn);
212       if (chkacl($acl, "all") == ""){
213         $this->remove_from_parent();
214         gosa_log ("Blocklist object'".$this->dn."' has been removed");
215         $this->reload ();
216       } else {
217         print_red (_("You have no permission to remove this blocklist."));
218       }
219     }
221     /* Cancel dialog */
222     if (isset($_POST['edit_cancel']) || isset($_POST['delete_blocklist_cancel']) ||
223         isset($_POST['delete_blocklist_confirm']) || isset($_POST['delete_lock'])){
225       del_lock ($this->dn);
226       $this->clear_fields();
227       unset($_SESSION['objectinfo']);
228     }
230     /* Handle interactions: add */
231     if (isset($_POST['add_number']) && $_POST['number'] != ""){
232       if (is_phone_nr($_POST['number'])){
233         $this->addNumber ($_POST['number']);
234       } else {
235         print_red (_("Please specify a valid phone number."));
236       }
237     }
239     /* Handle interactions: delete */
240     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
241       $this->delNumber ($_POST['numbers']);
242     }
244     /* What about finish? */
245     if (isset($_POST['edit_finish'])){
246       $message= $this->check();
248       /* No errors, save object */
249       if (count ($message) == 0){
251         /* Perpare 'dn' in case of new entries */
252         if ($this->dn == "new"){
253           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
254         }
256         $this->save ();
257         gosa_log ("Blocklist object'".$this->dn."' has been saved");
258         $this->acl= "#none#";
259         $this->dn= "";
260         del_lock ($this->ui->dn);
261         unset($_SESSION['objectinfo']);
262       } else {
263         /* Errors found, show message */
264         show_errors ($message);
265       }
266     }
268     /* Headpage or normal plugin screen? */
269     if ($this->dn == ""){
270       /* Check sorting variable */
271       $this->reload();
273       /* Check for exeeded sizelimit */
274       if (($message= check_sizelimit()) != ""){
275         return($message);
276       }
278    $options= "";
279     foreach ($this->config->idepartments as $key => $value){
280             if ($blockfilter['depselect'] == $key){
281                     $options.= "<option selected value='$key'>$value</option>";
282             } else {
283                     $options.= "<option value='$key'>$value</option>";
284             }
285     }
289       /* NEW LIST MANAGMENT */
290       $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
291         " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
292         " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
293         " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
294         " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
295         " <input type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'>&nbsp;".
296         " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;"._("Current base")."&nbsp;<select name='depselect' onChange='mainform.submit()'>$options</select>".
297         " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
298         "</div>";
300       $action= "<input type='image' src='images/edit.png' alt='"._("edit")."'     name='user_edit_%KEY%' title='"._("Edit user")."'>";
301       $action.= "<input type='image' src='images/editdelete.png' alt='"._("delete")."'   name='user_del_%KEY%' title='"._("Delete user")."'>";
304       $divlist = new divlist("faxtabs");
305       $divlist->SetHeader(array(
306             array("string" => "&nbsp;"),
307             array("string" => _("Blocklist name")." / "._("Department")),
308             array("string" => _("Actions"), "attach" => "style='border:none'" )));
311       $divlist->SetSummary(_("This table displays faxblocklists for the selected tree."));
312       $divlist->SetEntriesPerPage(20);
314       // Defining Links
315       $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
316       foreach($this->departments as $key=> $val){
317         if(!isset($this->config->departments[trim($key)])){
318           $this->config->departments[trim($key)]="";
319         }
321         $non_empty="";
322         $keys= str_replace("/","\/",$key);
323         foreach($this->config->departments as $keyd=>$vald ){
324           if(preg_match("/".$keys."\/.*/",$keyd)){
325             $non_empty="full";
326           }
327         }
329         $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>");
330         $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
331         $field3 = array("string" => "&nbsp;", "attach" => "style='border:none'");
332         $divlist->AddEntry(array($field1,$field2,$field3));
333       }
335       // User and Template  Images
336       $blockimg = "<img src='images/list_blocklist.png' alt='User' title='%s'>";
337       $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
339       foreach($this->blocklists as $key => $val){
340         // Generate Array to Add
341         $display= "[".$val["cn"][0]."]";
342         $field1 = array("string" => sprintf($blockimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
343         $field2 = array("string" => sprintf($editlink,$key,$display),"attach"=>"title='".$val['dn']."'");
344         $field3 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='border:none; text-align:right;width:40px'");
345         $divlist->AddEntry( array($field1,$field2,$field3));
346       }
348       /* Show main page */
349       $smarty->assign("blocklistshead", $listhead);
350       $smarty->assign("blocklists", $divlist->DrawList());
351       $smarty->assign("search_image", get_template_path('images/search.png'));
352       $smarty->assign("tree_image", get_template_path('images/tree.png'));
353       $smarty->assign("infoimage", get_template_path('images/info.png'));
354       $smarty->assign("launchimage", get_template_path('images/launch.png'));
355       $smarty->assign("apply", apply_filter());
356       foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
357         $smarty->assign("$type", $blockfilter[$type]);
358       }
359       $smarty->assign("deplist", $this->config->idepartments);
360       $smarty->assign("alphabet", generate_alphabet());
361       $smarty->assign("hint", print_sizelimit_warning());
363       return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
364     }
366     /* Set base */
367     if ($this->base == ""){
368       if ($this->dn == "new"){
369         $ui= get_userinfo();
370         $this->base= dn2base($ui->dn);
371       } else {
372         $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
373       }
374     }
376     /* Fill templating stuff */
377     $smarty->assign("bases", $this->config->idepartments);
378     $smarty->assign("base_select", $_SESSION['blockfilter']['depselect']);//$this->base);
379     $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
380     if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
381       $smarty->assign("selectmode", "");
382       $smarty->assign("mode", "");
383     } else {
384       $smarty->assign("selectmode", "disabled");
385       $smarty->assign("mode", "readonly");
386     }
387     foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
388       $smarty->assign("$val", $this->$val);
389       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
390     }
392     /* Lock blocklist type for non new entries */
393     if ($this->dn != "new"){
394       $smarty->assign('typeACL', "disabled");
395     }
397     /* Show main page */
398     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
399   }
401   function clear_fields()
402   {
403     $this->dn= "";
404     $this->description= "";
405     $this->cn= "";
406     $this->base= "";
407     $this->goFaxBlocklist= array();
408     $this->acl= "#none#";
409   }
411   function reload()
412   {
413     /* Get config */
414     $blockfilter= get_global('blockfilter');
416     /* Set base for all searches */
417     $base= $blockfilter['depselect'];
418     $filter= "";
420     /* Regex filter? */
421     if ($blockfilter['regex'] != ""){
422       $regex= $blockfilter['regex'];
423     } else {
424       $regex= "*";
425     }
427     /* Entry filter? */
428     /* Get list of blocklists to be shown */
429     if ($blockfilter['sendlists'] == "checked"){
430       $filter.= "(objectClass=goFaxSBlock)";
431     }
432     if ($blockfilter['receivelists'] == "checked"){
433       $filter.= "(objectClass=goFaxRBlock)";
434     }
435     if ($filter != ""){
436       $filter= "(|$filter)";
437     }
439     if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
440       $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
441     }
443     /* Generate blocklists */
444     $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", FALSE, "ou=gofax,ou=systems,".$base, array("*"));
446     $this->blocklists=$res;
448     /* NEW LIST MANAGMENT
449      * We also need to search for the departments
450      * So we are able to navigate like in konquerer
451      */
452     $base2 = preg_replace("/ou=people,/i","",$base);
453     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
454         TRUE, $base2, array("ou", "description"), TRUE);
456     $this->departments= array();
457     $tmp = array();
458     foreach ($res3 as $value){
459       $tmp[strtolower($value['dn']).$value['dn']]=$value;
460     }
461     ksort($tmp);
462     foreach($tmp as $value){
463       if($value["description"][0]!=".."){
464         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
465       }else{
466         $this->departments[$value['dn']]=$value["description"][0];
467       }
468     }
470     /* END NEW LIST MANAGMENT
471      */
472     $tmp=array();
473     foreach($this->blocklists as $tkey => $val ){
474       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
475     }
476     ksort($tmp);
477     $this->blocklists=array();
478     foreach($tmp as $val){
479       $this->blocklists[]=$val;
480     }
481     reset ($this->blocklists);
482   }
484   function remove_from_parent()
485   {
486     $ldap= $this->config->get_ldap_link();
487     $ldap->rmDir($this->dn);
488     $this->handle_post_events("remove");
489   }
492   /* Save data to object */
493   function save_object()
494   {
495     plugin::save_object();
497     /* Save type, needed to detect objectClass */
498     if (isset($_POST['type']) && chkacl($this->acl, "cn") == ""){
499       $this->type= $_POST['type'];
500     }
502     /* Save base, since this is no LDAP attribute */
503     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
504       $this->base= $_POST['base'];
505     }
506   }
509   /* Check values */
510   function check()
511   {
512     $message= array();
514     /* Permissions for that base? */
515     if ($this->base != ""){
516       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
517     } else {
518       $new_dn= $this->dn;
519     }
521     $ui= get_userinfo();
522     $acl= get_permissions ($new_dn, $ui->subtreeACL);
523     $acl= get_module_permission($acl, "blocklist", $new_dn);
524     if (chkacl($acl, "create") != ""){
525       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
526     }
529     /* check syntax: must cn */
530     if ($this->cn == ""){
531       $message[]= _("Required field 'Name' is not set.");
532     } else {
533       if (!is_uid($this->cn)){
534         $message[]= _("Required field 'Name' contains invalid characters");
535       }
536       if ($this->dn == 'new'){
537         $ldap= $this->config->get_ldap_link();
538         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
539         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))".
540             "(cn=".$this->cn."))", array("cn"));
541         if ($ldap->count() != 0){
542           $message[]= _("Specified name is already used.");
543         }
544       }
545     }
547     /* Is user allowed to create in 'base'? */
548     if (chkacl($this->acl, "create") != ""){
549       $message[]= _("No permission to create a blocklist on this base.");
550     }
552     return $message;
553   }
556   /* Save to LDAP */
557   function save()
558   {
559     plugin::save();
561     /* Type selection */
562     if ($this->type == 0){
563       $type= "goFaxSBlock";
564     } else {
565       $type= "goFaxRBlock";
566     }
568     /* Add list */
569     $this->attrs['objectClass']= $type;
570     if (count($this->goFaxBlocklist)){
571       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
572     }
574     /* Write back to ldap */
575     $ldap= $this->config->get_ldap_link();
576     $ldap->cd($ldap->getParentDir($this->dn));
577     $ldap->search("cn=$this->cn");
578     $a= $ldap->fetch();
579     if (count($a)){
580       if (!isset($this->attrs["$type"."list"])){
581         $this->attrs["$type"."list"]= array();
582       }
583       $ldap->cd($this->dn);
584       $ldap->modify($this->attrs);
585       $this->handle_post_events("modify");
586     } else {
587       $ldap->cd($this->config->current['BASE']);
588       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
589       $ldap->cd($this->dn);
590       $ldap->add($this->attrs);
591       $this->handle_post_events("add");
592     }
593     show_ldap_error($ldap->get_error());
594   }
597   function addNumber($number)
598   {
599     if (!in_array($number, $this->goFaxBlocklist)){
600       $this->goFaxBlocklist[]= $number;
601       sort($this->goFaxBlocklist);
602     }
603   }
605   function delNumber($numbers)
606   {
607     $tmp= array();
608     foreach ($this->goFaxBlocklist as $val){
609       if (!in_array($val, $numbers)){
610         $tmp[]= $val;
611       }
612     }
613     $this->goFaxBlocklist= $tmp;
614   }
616   function remove_lock()
617   {
618     if (isset($this->dn)){
619       del_lock ($this->dn);
620     }
621   }
624 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
625 ?>