Code

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