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