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