Code

Rise description size to 80
[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' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
308         " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&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         " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
311         " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
312         " <input class='center' type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'>&nbsp;".
313         " <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>".
314         " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
315         "</div>";
317       $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."'     name='user_edit_%KEY%' title='"._("Edit user")."'>";
318       $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."'   name='user_del_%KEY%' title='"._("Delete user")."'>";
321       $divlist = new divlist("faxtabs");
322       $divlist->SetHeader(array(
323             array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
324             array("string" => _("Blocklist name")." / "._("Department"), "attach" => "style=''"),
325             array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
328       $divlist->SetSummary(_("This table displays faxblocklists for the selected tree."));
329       $divlist->SetEntriesPerPage(0);
331       // Defining Links
332       $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
333       foreach($this->departments as $key=> $val){
334         if(!isset($this->config->departments[trim($key)])){
335           $this->config->departments[trim($key)]="";
336         }
338         $non_empty="";
339         $keys= str_replace("/","\/",$key);
340         foreach($this->config->departments as $keyd=>$vald ){
341           if(preg_match("/".$keys."\/.*/",$keyd)){
342             $non_empty="full";
343           }
344         }
346         $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
347         $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
348         $field3 = array("string" => "&nbsp;", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
349         $divlist->AddEntry(array($field1,$field2,$field3));
350       }
352       // User and Template  Images
353       $blockimg = "<img class='center' src='images/list_blocklist.png' alt='User' title='%s'>";
354       $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
356       foreach($this->blocklists as $key => $val){
357         // Generate Array to Add
358         $display= "[".$val["cn"][0]."]";
359         $field1 = array("string" => sprintf($blockimg,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
360         $field2 = array("string" => sprintf($editlink,$key,$display), "attach" => "style='' title='".$val['dn']."'");
361         $field3 = array("string" => preg_replace("/%KEY%/", "$key", $action), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
362         $divlist->AddEntry( array($field1,$field2,$field3));
363       }
365       /* Show main page */
366       $smarty->assign("blocklistshead", $listhead);
367       $smarty->assign("blocklists", $divlist->DrawList());
368       $smarty->assign("search_image", get_template_path('images/search.png'));
369       $smarty->assign("tree_image", get_template_path('images/tree.png'));
370       $smarty->assign("infoimage", get_template_path('images/info.png'));
371       $smarty->assign("launchimage", get_template_path('images/launch.png'));
372       $smarty->assign("apply", apply_filter());
373       foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
374         $smarty->assign("$type", $blockfilter[$type]);
375       }
376       $smarty->assign("deplist", $this->config->idepartments);
377       $smarty->assign("alphabet", generate_alphabet());
378       $smarty->assign("hint", print_sizelimit_warning());
380       return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
381     }
383     /* Set base */
384     if ($this->base == ""){
385       if ($this->dn == "new"){
386         $ui= get_userinfo();
387         $this->base= dn2base($ui->dn);
388       } else {
389         $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
390       }
391     }
393     /* Fill templating stuff */
394     $smarty->assign("bases", $this->config->idepartments);
395     $smarty->assign("base_select", $this->base);
396     $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
397     if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
398       $smarty->assign("selectmode", "");
399       $smarty->assign("mode", "");
400     } else {
401       $smarty->assign("selectmode", "disabled");
402       $smarty->assign("mode", "readonly");
403     }
404     foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
405       $smarty->assign("$val", $this->$val);
406       $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
407     }
409     /* Lock blocklist type for non new entries */
410     if ($this->dn != "new"){
411       $smarty->assign('typeACL', "disabled");
412     }
414     /* Show main page */
415     return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
416   }
418   function clear_fields()
419   {
420     $this->dn= "";
421     $this->description= "";
422     $this->cn= "";
423     $this->base= $_SESSION['blockfilter']['depselect'];
424     $this->goFaxBlocklist= array();
425   }
427   function reload()
428   {
429     /* Get config */
430     $blockfilter= get_global('blockfilter');
432     /* Set base for all searches */
433     $base= $blockfilter['depselect'];
434     $filter= "";
436     /* Regex filter? */
437     if ($blockfilter['regex'] != ""){
438       $regex= $blockfilter['regex'];
439     } else {
440       $regex= "*";
441     }
443     /* Entry filter? */
444     /* Get list of blocklists to be shown */
445     if ($blockfilter['sendlists'] == "checked"){
446       $filter.= "(objectClass=goFaxSBlock)";
447     }
448     if ($blockfilter['receivelists'] == "checked"){
449       $filter.= "(objectClass=goFaxRBlock)";
450     }
451     if ($filter != ""){
452       $filter= "(|$filter)";
453     }
455     if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
456       $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
457     }
459     /* Generate blocklists */
460     $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", FALSE, "ou=gofax,ou=systems,".$base, array("*"));
462     $this->blocklists=$res;
464     /* NEW LIST MANAGMENT
465      * We also need to search for the departments
466      * So we are able to navigate like in konquerer
467      */
468     $peopleBase = get_people_ou();
469     if(empty($peopleBase)){
470       $base2 = $base;
471     }else{
472       $base2 = preg_replace("/".$peopleBase.",/i","",$base);
473     }
474    
475     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
476         TRUE, $base2, array("ou", "description"), TRUE);
478     $this->departments= array();
479     $tmp = array();
480     foreach ($res3 as $value){
481       $tmp[strtolower($value['dn']).$value['dn']]=$value;
482     }
483     ksort($tmp);
484     foreach($tmp as $value){
485       if(isset($value["description"][0])){
486         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
487       }else{
488         $this->departments[$value['dn']]=convert_department_dn2($value['dn']);//$value["description"][0];
489       }
490     }
492     /* END NEW LIST MANAGMENT
493      */
494     $tmp=array();
495     foreach($this->blocklists as $tkey => $val ){
496       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
497     }
498     ksort($tmp);
499     $this->blocklists=array();
500     foreach($tmp as $val){
501       $this->blocklists[]=$val;
502     }
503     reset ($this->blocklists);
504   }
506   function remove_from_parent()
507   {
508     $ldap= $this->config->get_ldap_link();
509     $ldap->rmDir($this->dn);
510     $this->handle_post_events("remove");
511   }
514   /* Save data to object */
515   function save_object()
516   {
517     plugin::save_object();
519     if(isset($_POST['base'])){
520       $this->base = $_POST['base'];
521     }
522  
523     foreach($this->attributes as $attr){
524       if(isset($_POST[$attr])){
525         $this->$attr = $_POST[$attr];
526       }
527     }
528   }
530   /* Check values */
531   function check()
532   {
533     $message= array();
535     /* Permissions for that base? */
536     if ($this->base != ""){
537       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
538     } else {
539       $new_dn= $this->dn;
540     }
542     if (chkacl($this->acl, "create") != ""){
543       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
544     }
547     /* check syntax: must cn */
548     if ($this->cn == ""){
549       $message[]= _("Required field 'Name' is not set.");
550     } else {
551       if (!is_uid($this->cn)){
552         $message[]= _("Required field 'Name' contains invalid characters");
553       }
554       if ($this->dn == 'new'){
555         $ldap= $this->config->get_ldap_link();
556         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
557         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
558         if ($ldap->count() != 0){
559           $message[]= _("Specified name is already used.");
560         }
561       }
562     }
564     /* Is user allowed to create in 'base'? */
565     if (chkacl($this->acl, "create") != ""){
566       $message[]= _("No permission to create a blocklist on this base.");
567     }
569     return $message;
570   }
573   /* Save to LDAP */
574   function save()
575   {
576     plugin::save();
577     
579     /* Type selection */
580     if ($this->type == 0){
581       $type= "goFaxSBlock";
582     } else {
583       $type= "goFaxRBlock";
584     }
586     /* Add list */
587     $this->attrs['objectClass']= $type;
588     if (count($this->goFaxBlocklist)){
589       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
590     }
592     /* Write back to ldap */
593     $ldap= $this->config->get_ldap_link();
594     $ldap->cd($this->base);
595     $ldap->search("cn=$this->cn",array("cn"));
596     $ldap->cat($this->dn);
597     $a= $ldap->fetch();
598     
599     if (count($a)){
600       if (!isset($this->attrs["$type"."list"])){
601         $this->attrs["$type"."list"]= array();
602       }
603       $ldap->cd($this->dn);
604       $this->cleanup();
605       $ldap->modify($this->attrs);
606       $this->handle_post_events("modify");
607     } else {
608       $ldap->cd($this->config->current['BASE']);
609       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
610       $ldap->cd($this->dn);
611       $ldap->add($this->attrs);
612       $this->handle_post_events("add");
613     }
614     show_ldap_error($ldap->get_error());
615   }
618   function addNumber($number)
619   {
620     if (!in_array($number, $this->goFaxBlocklist)){
621       $this->goFaxBlocklist[]= $number;
622       sort($this->goFaxBlocklist);
623     }
624   }
626   function delNumber($numbers)
627   {
628     $tmp= array();
629     foreach ($this->goFaxBlocklist as $val){
630       if (!in_array($val, $numbers)){
631         $tmp[]= $val;
632       }
633     }
634     $this->goFaxBlocklist= $tmp;
635   }
637   function remove_lock()
638   {
639     if (isset($this->dn)){
640       del_lock ($this->dn);
641     }
642   }
645 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
646 ?>