Code

Applied planned layout modifications
[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       $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     }
45   }
47   function execute()
48   {
49     /* Get global smarty instance */
50     $smarty= get_smarty();
51     $blockfilter   = get_global("blockfilter"); // contains Filter Settings
52     $s_action     = "";                       // Contains the action to proceed
53     $s_entry      = "";                       // The value for s_action
54     $base_back    = "";                       // The Link for Backbutton
56     /* Start for New List Managment */
57     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
58       $s_action="open";
59       $s_entry = base64_decode($_GET['dep_id']);
60       $blockfilter['depselect']= "".$this->config->departments[trim($s_entry)];
61       $this->reload();
62     }
64     /* Test Posts */
65     foreach($_POST as $key => $val){
66       // Post for delete
67       if(preg_match("/user_del.*/",$key)){
68         $s_action = "del";
69         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
70         // Post for edit
71       }elseif(preg_match("/user_edit_.*/",$key)){
72         $s_action="edit";
73         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
74         // Post for new
75       }elseif(preg_match("/dep_back.*/i",$key)){
76         $s_action="back";
77       }elseif(preg_match("/user_new.*/",$key)){
78         $s_action="new";
79       }elseif(preg_match("/dep_home.*/i",$key)){
80         $s_action="home";
81       }elseif(preg_match("/user_tplnew.*/i",$key)){
82         $s_action="new_tpl";
83       }elseif(preg_match("/user_chgpw.*/i",$key)){
84         $s_action="change_pw";
85         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
86       }elseif(preg_match("/dep_root.*/i",$key)){
87         $s_action="root";
88       }
89     }
93     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
94       $s_action ="edit";
95       $s_entry  = $_GET['id'];
96     }
98     $s_entry  = preg_replace("/_.$/","",$s_entry);
100     /* Department changed? */
101     if(isset($_POST['depselect']) && $_POST['depselect']){
102       $blockfilter['depselect']= $_POST['depselect'];
103       $this->reload();
104     }
106     /* Homebutton is posted */
107     if($s_action=="home"){
108       $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
109       $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$blockfilter['depselect']));
110       $this->reload();
111     }
113     if($s_action=="root"){
114       $blockfilter['depselect']=($this->config->current['BASE']);
115       $this->reload();
116     }
117     
118     /* If Backbutton is Posted */
119     if($s_action=="back"){
120       $base_back          = preg_replace("/^[^,]+,/","",$blockfilter['depselect']);
121       $base_back          = convert_department_dn($base_back);
123       if(isset($this->config->departments[trim($base_back)])){
124         $blockfilter['depselect']= $this->config->departments[trim($base_back)];
125       }else{
126         $blockfilter['depselect']= $this->config->departments["/"];
127       }
128       $this->reload();
129     }
131     register_global("blockfilter", $blockfilter);
133     /* Save data */
134     $blockfilter= get_global("blockfilter");
135     foreach( array( "entry", "regex") as $type){
136       if (isset($_POST[$type])){
137         $blockfilter[$type]= $_POST[$type];
138       }
139     }
140     if (isset($_POST['depselect'])){
141       foreach( array("sendlists", "receivelists") as $type){
143         if (isset($_POST[$type])) {
144           $blockfilter[$type]= "checked";
145         } else {
146           $blockfilter[$type]= "";
147         }
148       }
149     }
150     if (isset($_GET['search'])){
151       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
152       if ($s == "**"){
153         $s= "*";
154       }
155       $blockfilter['regex']= $s;
156     }
157     register_global("blockfilter", $blockfilter);
159     /* Create new blocklist? */
160     if ($s_action=="new"){
161       $this->clear_fields();
162       $this->acl= "#all#";
163       $this->dn= "new";
164       plugin::plugin ($this->config, $this->dn);
165     }
167     /* Edit selected blocklist? */
168     if ($s_action=="edit"){
169       $this->clear_fields();
170       $this->dn=$this->blocklists[$s_entry]['dn']; 
171       $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
172       $this->acl= get_module_permission($this->acl, "blocklists", $this->dn);
173       plugin::plugin ($this->config, $this->dn);
175       /* Load missing lists */
176       if (isset($this->attrs["goFaxSBlocklist"])){
177         for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
178           $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
179         }
180         $this->type= 0;
181       } elseif (isset($this->attrs["goFaxRBlocklist"])){
182         for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
183           $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
184         }
185         $this->type= 1;
186       }
187       $_SESSION['objectinfo']= trim($this->dn);
188     }
190     /* Delete blocklist requested */
191     if ($s_action=="del"){
192       $this->dn=$this->blocklists[$s_entry]['dn'];
193       /* Check locking */
194       if (($user= get_lock($this->dn)) != ""){
195         $_SESSION['dn']= $this->dn;
196         return(gen_locked_message($user, $this->dn));
197       } else {
198 # Lock this dn for editing
199         add_lock ($this->dn, $this->ui->dn);
201         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
202         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
203       }
204     }
206     /* Finally delete blocklist */
207     if (isset($_POST['delete_blocklist_confirm'])){
208       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
209       $acl= get_module_permission($acl, "blocklists", $this->dn);
210       if (chkacl($acl, "all") == ""){
211         $this->remove_from_parent();
212         gosa_log ("Blocklist object'".$this->dn."' has been removed");
213         $this->reload ();
214       } else {
215         print_red (_("You have no permission to remove this blocklist."));
216       }
217     }
219     /* Cancel dialog */
220     if (isset($_POST['edit_cancel']) || isset($_POST['delete_blocklist_cancel']) ||
221         isset($_POST['delete_blocklist_confirm']) || isset($_POST['delete_lock'])){
223       del_lock ($this->dn);
224       $this->clear_fields();
225       unset($_SESSION['objectinfo']);
226     }
228     /* Handle interactions: add */
229     if (isset($_POST['add_number']) && $_POST['number'] != ""){
230       if (is_phone_nr($_POST['number'])){
231         $this->addNumber ($_POST['number']);
232       } else {
233         print_red (_("Please specify a valid phone number."));
234       }
235     }
237     /* Handle interactions: delete */
238     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
239       $this->delNumber ($_POST['numbers']);
240     }
242     /* What about finish? */
243     if (isset($_POST['edit_finish'])){
244       $message= $this->check();
246       /* No errors, save object */
247       if (count ($message) == 0){
249         /* Perpare 'dn' in case of new entries */
250         if ($this->dn == "new"){
251           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
252         }
254         $this->save ();
255         gosa_log ("Blocklist object'".$this->dn."' has been saved");
256         $this->acl= "#none#";
257         $this->dn= "";
258         del_lock ($this->ui->dn);
259         unset($_SESSION['objectinfo']);
260       } else {
261         /* Errors found, show message */
262         show_errors ($message);
263       }
264     }
266     /* Headpage or normal plugin screen? */
267     if ($this->dn == ""){
268       /* Check sorting variable */
269       $this->reload();
271       /* Check for exeeded sizelimit */
272       if (($message= check_sizelimit()) != ""){
273         return($message);
274       }
276    $options= "";
277     foreach ($this->config->idepartments as $key => $value){
278             if ($blockfilter['depselect'] == $key){
279                     $options.= "<option selected value='$key'>$value</option>";
280             } else {
281                     $options.= "<option value='$key'>$value</option>";
282             }
283     }
287       /* NEW LIST MANAGMENT */
288       $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
289         " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
290         " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
291         " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
292         " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
293         " <input type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'>&nbsp;".
294         " <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>".
295         " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
296         "</div>";
298       $action= "<input type='image' src='images/edit.png' alt='"._("edit")."'     name='user_edit_%KEY%' title='"._("Edit user")."'>";
299       $action.= "<input type='image' src='images/editdelete.png' alt='"._("delete")."'   name='user_del_%KEY%' title='"._("Delete user")."'>";
302       $divlist = new divlist();
303       $divlist->SetHeader(array(
304             array("string" => "&nbsp;"),
305             array("string" => _("Blocklist name")." / "._("Department")),
306             array("string" => _("Actions"), "attach" => "style='border:none'" )));
309       $divlist->SetSummary(_("This table displays faxblocklists for the selected tree."));
310       $divlist->SetEntriesPerPage(20);
312       // Defining Links
313       $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
314       foreach($this->departments as $key=> $val){
315         $field1 = array("string" => "<img src='images/folder.png' alt='department'>");
316         $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
317         $field3 = array("string" => "&nbsp;", "attach" => "style='border:none'");
318         $divlist->AddEntry(array($field1,$field2,$field3));
319       }
321       // User and Template  Images
322       $blockimg = "<img src='images/list_blocklist.png' alt='User' title='%s'>";
323       $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
325       foreach($this->blocklists as $key => $val){
326         // Generate Array to Add
327         $display= "[".$val["cn"][0]."]";
328         $field1 = array("string" => sprintf($blockimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
329         $field2 = array("string" => sprintf($editlink,$key,$display),"attach"=>"title='".$val['dn']."'");
330         $field3 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='border:none; text-align:right;width:40px'");
331         $divlist->AddEntry( array($field1,$field2,$field3));
332       }
334       /* Show main page */
335       $smarty->assign("blocklistshead", $listhead);
336       $smarty->assign("blocklists", $divlist->DrawList());
337       $smarty->assign("search_image", get_template_path('images/search.png'));
338       $smarty->assign("tree_image", get_template_path('images/tree.png'));
339       $smarty->assign("infoimage", get_template_path('images/info.png'));
340       $smarty->assign("launchimage", get_template_path('images/launch.png'));
341       $smarty->assign("apply", apply_filter());
342       foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
343         $smarty->assign("$type", $blockfilter[$type]);
344       }
345       $smarty->assign("deplist", $this->config->idepartments);
346       $smarty->assign("alphabet", generate_alphabet());
347       $smarty->assign("hint", print_sizelimit_warning());
349       return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
350     }
352     /* Set base */
353     if ($this->base == ""){
354       if ($this->dn == "new"){
355         $ui= get_userinfo();
356         $this->base= dn2base($ui->dn);
357       } else {
358         $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
359       }
360     }
362     /* Fill templating stuff */
363     $smarty->assign("bases", $this->config->idepartments);
364     $smarty->assign("base_select", $this->base);
365     $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
366     if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
367       $smarty->assign("selectmode", "");
368       $smarty->assign("mode", "");
369     } else {
370       $smarty->assign("selectmode", "disabled");
371       $smarty->assign("mode", "readonly");
372     }
373     foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
374       $smarty->assign("$val", $this->$val);
375       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
376     }
378     /* Lock blocklist type for non new entries */
379     if ($this->dn != "new"){
380       $smarty->assign('typeACL', "disabled");
381     }
383     /* Show main page */
384     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
385   }
387   function clear_fields()
388   {
389     $this->dn= "";
390     $this->description= "";
391     $this->cn= "";
392     $this->base= "";
393     $this->goFaxBlocklist= array();
394     $this->acl= "#none#";
395   }
397   function reload()
398   {
399     /* Get config */
400     $blockfilter= get_global('blockfilter');
402     /* Set base for all searches */
403     $base= $blockfilter['depselect'];
404     $filter= "";
406     /* Regex filter? */
407     if ($blockfilter['regex'] != ""){
408       $regex= $blockfilter['regex'];
409     } else {
410       $regex= "*";
411     }
413     /* Entry filter? */
414     /* Get list of blocklists to be shown */
415     if ($blockfilter['sendlists'] == "checked"){
416       $filter.= "(objectClass=goFaxSBlock)";
417     }
418     if ($blockfilter['receivelists'] == "checked"){
419       $filter.= "(objectClass=goFaxRBlock)";
420     }
421     if ($filter != ""){
422       $filter= "(|$filter)";
423     }
425     if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
426       $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
427     }
429     /* Generate blocklists */
430     $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", TRUE, $base, array("*"));
432     $this->blocklists=$res;
434     /* NEW LIST MANAGMENT
435      * We also need to search for the departments
436      * So we are able to navigate like in konquerer
437      */
438     $base2 = preg_replace("/ou=people,/i","",$base);
440     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
441         TRUE, $base2, array("ou", "description"), TRUE);
443     $this->departments= array();
444     foreach ($res3 as $value){
445       if($value["description"][0]!=".."){
446         $this->departments[$value['dn']]= convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
447       }else{
448         $this->departments[$value['dn']]=$value["description"][0];
449       }
450     }
452     /* END NEW LIST MANAGMENT
453      */
455     reset ($this->blocklists);
456   }
458   function remove_from_parent()
459   {
460     $ldap= $this->config->get_ldap_link();
461     $ldap->rmDir($this->dn);
462     $this->handle_post_events("remove");
463   }
466   /* Save data to object */
467   function save_object()
468   {
469     plugin::save_object();
471     /* Save type, needed to detect objectClass */
472     if (isset($_POST['type']) && chkacl($this->acl, "cn") == ""){
473       $this->type= $_POST['type'];
474     }
476     /* Save base, since this is no LDAP attribute */
477     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
478       $this->base= $_POST['base'];
479     }
480   }
483   /* Check values */
484   function check()
485   {
486     $message= array();
488     /* Permissions for that base? */
489     if ($this->base != ""){
490       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
491     } else {
492       $new_dn= $this->dn;
493     }
495     $ui= get_userinfo();
496     $acl= get_permissions ($new_dn, $ui->subtreeACL);
497     $acl= get_module_permission($acl, "blocklist", $new_dn);
498     if (chkacl($acl, "create") != ""){
499       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
500     }
503     /* check syntax: must cn */
504     if ($this->cn == ""){
505       $message[]= _("Required field 'Name' is not set.");
506     } else {
507       if (!is_uid($this->cn)){
508         $message[]= _("Required field 'Name' contains invalid characters");
509       }
510       if ($this->dn == 'new'){
511         $ldap= $this->config->get_ldap_link();
512         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
513         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))".
514             "(cn=".$this->cn."))", array("cn"));
515         if ($ldap->count() != 0){
516           $message[]= _("Specified name is already used.");
517         }
518       }
519     }
521     /* Is user allowed to create in 'base'? */
522     if (chkacl($this->acl, "create") != ""){
523       $message[]= _("No permission to create a blocklist on this base.");
524     }
526     return $message;
527   }
530   /* Save to LDAP */
531   function save()
532   {
533     plugin::save();
535     /* Type selection */
536     if ($this->type == 0){
537       $type= "goFaxSBlock";
538     } else {
539       $type= "goFaxRBlock";
540     }
542     /* Add list */
543     $this->attrs['objectClass']= $type;
544     if (count($this->goFaxBlocklist)){
545       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
546     }
548     /* Write back to ldap */
549     $ldap= $this->config->get_ldap_link();
550     $ldap->cd($ldap->getParentDir($this->dn));
551     $ldap->search("cn=$this->cn");
552     $a= $ldap->fetch();
553     if (count($a)){
554       if (!isset($this->attrs["$type"."list"])){
555         $this->attrs["$type"."list"]= array();
556       }
557       $ldap->cd($this->dn);
558       $ldap->modify($this->attrs);
559       $this->handle_post_events("modify");
560     } else {
561       $ldap->cd($this->config->current['BASE']);
562       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
563       $ldap->cd($this->dn);
564       $ldap->add($this->attrs);
565       $this->handle_post_events("add");
566     }
567     show_ldap_error($ldap->get_error());
568   }
571   function addNumber($number)
572   {
573     if (!in_array($number, $this->goFaxBlocklist)){
574       $this->goFaxBlocklist[]= $number;
575       sort($this->goFaxBlocklist);
576     }
577   }
579   function delNumber($numbers)
580   {
581     $tmp= array();
582     foreach ($this->goFaxBlocklist as $val){
583       if (!in_array($val, $numbers)){
584         $tmp[]= $val;
585       }
586     }
587     $this->goFaxBlocklist= $tmp;
588   }
590   function remove_lock()
591   {
592     if (isset($this->dn)){
593       del_lock ($this->dn);
594     }
595   }
598 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
599 ?>