Code

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