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();
52 /* Save data */
53 $blockfilter= get_global("blockfilter");
54 foreach( array("depselect", "entry", "regex") as $type){
55 if (isset($_POST[$type])){
56 $blockfilter[$type]= $_POST[$type];
57 }
58 }
59 if (isset($_POST['depselect'])){
60 foreach( array("sendlists", "receivelists") as $type){
62 if (isset($_POST[$type])) {
63 $blockfilter[$type]= "checked";
64 } else {
65 $blockfilter[$type]= "";
66 }
67 }
68 }
69 if (isset($_GET['search'])){
70 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
71 if ($s == "**"){
72 $s= "*";
73 }
74 $blockfilter['regex']= $s;
75 }
76 register_global("blockfilter", $blockfilter);
78 /* Create new blocklist? */
79 if (isset($_POST['new_blocklist'])){
80 $this->clear_fields();
81 $this->acl= "#all#";
82 $this->dn= "new";
83 plugin::plugin ($this->config, $this->dn);
84 }
86 /* Edit selected blocklist? */
87 if ((isset($_POST['select_blocklist']) || (isset($_POST['edit_helper']) && $_POST['edit_helper'] == "1")) && isset($_POST['blocklist']) != ""){
88 $this->clear_fields();
89 $this->dn= trim($_POST['blocklist']);
90 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
91 $this->acl= get_module_permission($this->acl, "blocklists", $this->dn);
92 plugin::plugin ($this->config, $this->dn);
94 /* Load missing lists */
95 if (isset($this->attrs["goFaxSBlocklist"])){
96 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
97 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
98 }
99 $this->type= 0;
100 } elseif (isset($this->attrs["goFaxRBlocklist"])){
101 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
102 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
103 }
104 $this->type= 1;
105 }
106 $_SESSION['objectinfo']= trim($this->dn);
107 }
109 /* Delete blocklist requested */
110 if (isset($_POST['delete_blocklist']) && isset($_POST['blocklist'])){
111 $this->dn= trim($_POST['blocklist']);
113 /* Check locking */
114 if (($user= get_lock($this->dn)) != ""){
115 $_SESSION['dn']= $this->dn;
116 return(gen_locked_message($user, $this->dn));
117 } else {
118 # Lock this dn for editing
119 add_lock ($this->dn, $this->ui->dn);
121 $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
122 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
123 }
124 }
126 /* Finally delete blocklist */
127 if (isset($_POST['delete_blocklist_confirm'])){
128 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
129 $acl= get_module_permission($acl, "blocklists", $this->dn);
130 if (chkacl($acl, "all") == ""){
131 $this->remove_from_parent();
132 gosa_log ("Blocklist object'".$this->dn."' has been removed");
133 $this->reload ();
134 } else {
135 print_red (_("You have no permission to remove this blocklist."));
136 }
137 }
139 /* Cancel dialog */
140 if (isset($_POST['edit_cancel']) || isset($_POST['delete_blocklist_cancel']) ||
141 isset($_POST['delete_blocklist_confirm']) || isset($_POST['delete_lock'])){
143 del_lock ($this->dn);
144 $this->clear_fields();
145 unset($_SESSION['objectinfo']);
146 }
148 /* Handle interactions: add */
149 if (isset($_POST['add_number']) && $_POST['number'] != ""){
150 if (is_phone_nr($_POST['number'])){
151 $this->addNumber ($_POST['number']);
152 } else {
153 print_red (_("Please specify a valid phone number."));
154 }
155 }
157 /* Handle interactions: delete */
158 if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
159 $this->delNumber ($_POST['numbers']);
160 }
162 /* What about finish? */
163 if (isset($_POST['edit_finish'])){
164 $message= $this->check();
166 /* No errors, save object */
167 if (count ($message) == 0){
169 /* Perpare 'dn' in case of new entries */
170 if ($this->dn == "new"){
171 $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
172 }
174 $this->save ();
175 gosa_log ("Blocklist object'".$this->dn."' has been saved");
176 $this->acl= "#none#";
177 $this->dn= "";
178 del_lock ($this->ui->dn);
179 unset($_SESSION['objectinfo']);
180 } else {
181 /* Errors found, show message */
182 show_errors ($message);
183 }
184 }
186 /* Headpage or normal plugin screen? */
187 if ($this->dn == ""){
188 /* Check sorting variable */
189 $this->reload();
191 /* Check for exeeded sizelimit */
192 if (($message= check_sizelimit()) != ""){
193 return($message);
194 }
196 /* Show main page */
197 $smarty->assign("blocklists", $this->blocklists);
198 $smarty->assign("search_image", get_template_path('images/search.png'));
199 $smarty->assign("tree_image", get_template_path('images/tree.png'));
200 $smarty->assign("infoimage", get_template_path('images/info.png'));
201 $smarty->assign("launchimage", get_template_path('images/launch.png'));
202 $smarty->assign("apply", apply_filter());
203 foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
204 $smarty->assign("$type", $blockfilter[$type]);
205 }
206 $smarty->assign("deplist", $this->config->idepartments);
207 $smarty->assign("alphabet", generate_alphabet());
208 $smarty->assign("hint", print_sizelimit_warning());
210 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
211 }
213 /* Set base */
214 if ($this->base == ""){
215 if ($this->dn == "new"){
216 $ui= get_userinfo();
217 $this->base= dn2base($ui->dn);
218 } else {
219 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
220 }
221 }
223 /* Fill templating stuff */
224 $smarty->assign("bases", $this->config->idepartments);
225 $smarty->assign("base_select", $this->base);
226 $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
227 if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
228 $smarty->assign("selectmode", "");
229 $smarty->assign("mode", "");
230 } else {
231 $smarty->assign("selectmode", "disabled");
232 $smarty->assign("mode", "readonly");
233 }
234 foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
235 $smarty->assign("$val", $this->$val);
236 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
237 }
239 /* Lock blocklist type for non new entries */
240 if ($this->dn != "new"){
241 $smarty->assign('typeACL', "disabled");
242 }
244 /* Show main page */
245 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
246 }
248 function clear_fields()
249 {
250 $this->dn= "";
251 $this->description= "";
252 $this->cn= "";
253 $this->base= "";
254 $this->goFaxBlocklist= array();
255 $this->acl= "#none#";
256 }
258 function reload()
259 {
260 /* Get config */
261 $blockfilter= get_global('blockfilter');
263 /* Set base for all searches */
264 $base= $blockfilter['depselect'];
265 $filter= "";
267 /* Regex filter? */
268 if ($blockfilter['regex'] != ""){
269 $regex= $blockfilter['regex'];
270 } else {
271 $regex= "*";
272 }
274 /* Entry filter? */
275 /* Get list of blocklists to be shown */
276 if ($blockfilter['sendlists'] == "checked"){
277 $filter.= "(objectClass=goFaxSBlock)";
278 }
279 if ($blockfilter['receivelists'] == "checked"){
280 $filter.= "(objectClass=goFaxRBlock)";
281 }
282 if ($filter != ""){
283 $filter= "(|$filter)";
284 }
286 if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
287 $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
288 }
290 /* Generate blocklists */
291 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", TRUE, $base, array("cn", "description"));
293 $this->blocklists= array();
294 foreach ($res as $value){
296 /* Unwanted? */
297 $this->blocklists[$value["dn"]]= $value["cn"][0];
298 if (isset($value["description"][0])){
299 $this->blocklists[$value["dn"]]= $value["cn"][0]." (".
300 $value["description"][0].")";
301 }
302 }
303 natcasesort ($this->blocklists);
304 reset ($this->blocklists);
305 }
307 function remove_from_parent()
308 {
309 $ldap= $this->config->get_ldap_link();
310 $ldap->rmDir($this->dn);
311 $this->handle_post_events("remove");
312 }
315 /* Save data to object */
316 function save_object()
317 {
318 plugin::save_object();
320 /* Save type, needed to detect objectClass */
321 if (isset($_POST['type']) && chkacl($this->acl, "cn") == ""){
322 $this->type= $_POST['type'];
323 }
325 /* Save base, since this is no LDAP attribute */
326 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
327 $this->base= $_POST['base'];
328 }
329 }
332 /* Check values */
333 function check()
334 {
335 $message= array();
337 /* Permissions for that base? */
338 if ($this->base != ""){
339 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
340 } else {
341 $new_dn= $this->dn;
342 }
344 $ui= get_userinfo();
345 $acl= get_permissions ($new_dn, $ui->subtreeACL);
346 $acl= get_module_permission($acl, "blocklist", $new_dn);
347 if (chkacl($acl, "create") != ""){
348 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
349 }
352 /* check syntax: must cn */
353 if ($this->cn == ""){
354 $message[]= _("Required field 'Name' is not set.");
355 } else {
356 if (!is_uid($this->cn)){
357 $message[]= _("Required field 'Name' contains invalid characters");
358 }
359 if ($this->dn == 'new'){
360 $ldap= $this->config->get_ldap_link();
361 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
362 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))".
363 "(cn=".$this->cn."))", array("cn"));
364 if ($ldap->count() != 0){
365 $message[]= _("Specified name is already used.");
366 }
367 }
368 }
370 /* Is user allowed to create in 'base'? */
371 if (chkacl($this->acl, "create") != ""){
372 $message[]= _("No permission to create a blocklist on this base.");
373 }
375 return $message;
376 }
379 /* Save to LDAP */
380 function save()
381 {
382 plugin::save();
384 /* Type selection */
385 if ($this->type == 0){
386 $type= "goFaxSBlock";
387 } else {
388 $type= "goFaxRBlock";
389 }
391 /* Add list */
392 $this->attrs['objectClass']= $type;
393 if (count($this->goFaxBlocklist)){
394 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
395 }
397 /* Write back to ldap */
398 $ldap= $this->config->get_ldap_link();
399 $ldap->cd($ldap->getParentDir($this->dn));
400 $ldap->search("cn=$this->cn");
401 $a= $ldap->fetch();
402 if (count($a)){
403 if (!isset($this->attrs["$type"."list"])){
404 $this->attrs["$type"."list"]= array();
405 }
406 $ldap->cd($this->dn);
407 $ldap->modify($this->attrs);
408 $this->handle_post_events("modify");
409 } else {
410 $ldap->cd($this->config->current['BASE']);
411 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
412 $ldap->cd($this->dn);
413 $ldap->add($this->attrs);
414 $this->handle_post_events("add");
415 }
416 show_ldap_error($ldap->get_error());
417 }
420 function addNumber($number)
421 {
422 if (!in_array($number, $this->goFaxBlocklist)){
423 $this->goFaxBlocklist[]= $number;
424 sort($this->goFaxBlocklist);
425 }
426 }
428 function delNumber($numbers)
429 {
430 $tmp= array();
431 foreach ($this->goFaxBlocklist as $val){
432 if (!in_array($val, $numbers)){
433 $tmp[]= $val;
434 }
435 }
436 $this->goFaxBlocklist= $tmp;
437 }
439 function remove_lock()
440 {
441 if (isset($this->dn)){
442 del_lock ($this->dn);
443 }
444 }
446 }
448 ?>