bc42936f2e5eb81f2c1a6ed9c811c2741e05760c
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 }
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'> ".
290 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
291 " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
292 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
293 " <input type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'> ".
294 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> "._("Current base")." <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")."'> ".
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("faxtabs");
303 $divlist->SetHeader(array(
304 array("string" => " "),
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']."&act=dep_open&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" => " ", "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']."&id=%s&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 $tmp = array();
445 foreach ($res3 as $value){
446 $tmp[strtolower($value['dn']).$value['dn']]=$value;
447 }
448 ksort($tmp);
449 foreach($tmp as $value){
450 if($value["description"][0]!=".."){
451 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
452 }else{
453 $this->departments[$value['dn']]=$value["description"][0];
454 }
455 }
457 /* END NEW LIST MANAGMENT
458 */
459 $tmp=array();
460 foreach($this->blocklists as $tkey => $val ){
461 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
462 }
463 ksort($tmp);
464 $this->blocklists=array();
465 foreach($tmp as $val){
466 $this->blocklists[]=$val;
467 }
468 reset ($this->blocklists);
469 }
471 function remove_from_parent()
472 {
473 $ldap= $this->config->get_ldap_link();
474 $ldap->rmDir($this->dn);
475 $this->handle_post_events("remove");
476 }
479 /* Save data to object */
480 function save_object()
481 {
482 plugin::save_object();
484 /* Save type, needed to detect objectClass */
485 if (isset($_POST['type']) && chkacl($this->acl, "cn") == ""){
486 $this->type= $_POST['type'];
487 }
489 /* Save base, since this is no LDAP attribute */
490 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
491 $this->base= $_POST['base'];
492 }
493 }
496 /* Check values */
497 function check()
498 {
499 $message= array();
501 /* Permissions for that base? */
502 if ($this->base != ""){
503 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
504 } else {
505 $new_dn= $this->dn;
506 }
508 $ui= get_userinfo();
509 $acl= get_permissions ($new_dn, $ui->subtreeACL);
510 $acl= get_module_permission($acl, "blocklist", $new_dn);
511 if (chkacl($acl, "create") != ""){
512 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
513 }
516 /* check syntax: must cn */
517 if ($this->cn == ""){
518 $message[]= _("Required field 'Name' is not set.");
519 } else {
520 if (!is_uid($this->cn)){
521 $message[]= _("Required field 'Name' contains invalid characters");
522 }
523 if ($this->dn == 'new'){
524 $ldap= $this->config->get_ldap_link();
525 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
526 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))".
527 "(cn=".$this->cn."))", array("cn"));
528 if ($ldap->count() != 0){
529 $message[]= _("Specified name is already used.");
530 }
531 }
532 }
534 /* Is user allowed to create in 'base'? */
535 if (chkacl($this->acl, "create") != ""){
536 $message[]= _("No permission to create a blocklist on this base.");
537 }
539 return $message;
540 }
543 /* Save to LDAP */
544 function save()
545 {
546 plugin::save();
548 /* Type selection */
549 if ($this->type == 0){
550 $type= "goFaxSBlock";
551 } else {
552 $type= "goFaxRBlock";
553 }
555 /* Add list */
556 $this->attrs['objectClass']= $type;
557 if (count($this->goFaxBlocklist)){
558 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
559 }
561 /* Write back to ldap */
562 $ldap= $this->config->get_ldap_link();
563 $ldap->cd($ldap->getParentDir($this->dn));
564 $ldap->search("cn=$this->cn");
565 $a= $ldap->fetch();
566 if (count($a)){
567 if (!isset($this->attrs["$type"."list"])){
568 $this->attrs["$type"."list"]= array();
569 }
570 $ldap->cd($this->dn);
571 $ldap->modify($this->attrs);
572 $this->handle_post_events("modify");
573 } else {
574 $ldap->cd($this->config->current['BASE']);
575 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
576 $ldap->cd($this->dn);
577 $ldap->add($this->attrs);
578 $this->handle_post_events("add");
579 }
580 show_ldap_error($ldap->get_error());
581 }
584 function addNumber($number)
585 {
586 if (!in_array($number, $this->goFaxBlocklist)){
587 $this->goFaxBlocklist[]= $number;
588 sort($this->goFaxBlocklist);
589 }
590 }
592 function delNumber($numbers)
593 {
594 $tmp= array();
595 foreach ($this->goFaxBlocklist as $val){
596 if (!in_array($val, $numbers)){
597 $tmp[]= $val;
598 }
599 }
600 $this->goFaxBlocklist= $tmp;
601 }
603 function remove_lock()
604 {
605 if (isset($this->dn)){
606 del_lock ($this->dn);
607 }
608 }
610 }
611 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
612 ?>