1 <?php
3 define("BLOCK_LIST_RECEIVE" , 0);
4 define("BLOCK_LIST_SEND" , 1);
6 class blocklistGeneric extends plugin
7 {
8 /* Definitions */
9 var $plHeadline= "FAX Blocklists";
10 var $plDescription= "This does something";
12 var $cn ="";
13 var $description = "";
14 var $base = "";
16 var $type;
17 var $goFaxBlocklist = array();
18 var $readonly = FALSE;
19 var $view_logged = FALSE;
20 var $attributes = array("cn","description");
22 var $ignore_account = TRUE;
24 function __construct($config,$dn = "new")
25 {
26 plugin::plugin($config,$dn);
28 /* Set default list type */
29 $this->type = BLOCK_LIST_SEND;
31 /* Load defined numbers */
32 if($dn != "new"){
34 /* We will not be able to rename edited blocklists */
35 $this->readonly = TRUE;
37 /* Get blocklist type and defined numbers */
38 if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
39 if(isset($this->attrs["goFaxSBlocklist"])){
40 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
41 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
42 }
43 }
44 $this->type= BLOCK_LIST_SEND;
45 } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
46 if(isset($this->attrs["goFaxRBlocklist"])){
47 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
48 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
49 }
50 }
51 $this->type= BLOCK_LIST_RECEIVE;
52 }
53 }
55 /* Set base */
56 if ($this->dn == "new"){
57 if(isset($_SESSION['CurrentMainBase'])){
58 $this->base= $_SESSION['CurrentMainBase'];
59 }else{
60 $ui= get_userinfo();
61 $this->base= dn2base($ui->dn);
62 }
63 } else {
64 $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
65 }
66 }
68 public function execute()
69 {
70 /* Log view */
71 if(!$this->view_logged){
72 $this->view_logged = TRUE;
73 new log("view","blocklist/".get_class($this),$this->dn);
74 }
77 /**************
78 * Base select dialog
79 **************/
81 $once = true;
82 foreach($_POST as $name => $value){
83 if(preg_match("/^chooseBase/",$name) && $once){
84 $once = false;
85 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
86 $this->dialog->setCurrentBase($this->base);
87 }
88 }
90 /* Dialog handling */
91 if(is_object($this->dialog)){
92 /* Must be called before save_object */
93 $this->dialog->save_object();
95 if($this->dialog->isClosed()){
96 $this->dialog = false;
97 }elseif($this->dialog->isSelected()){
99 /* A new base was selected, check if it is a valid one */
100 $tmp = $this->get_allowed_bases();
101 if(isset($tmp[$this->dialog->isSelected()])){
102 $this->base = $this->dialog->isSelected();
103 }
104 $this->dialog= false;
106 }else{
107 return($this->dialog->execute());
108 }
109 }
112 /***************
113 Add numer to blocklist
114 ***************/
117 /* Handle interactions: add */
118 if (isset($_POST['add_number']) && $_POST['number'] != ""){
119 if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
120 $this->addNumber ($_POST['number']);
121 } else {
122 print_red (_("Please specify a valid phone number."));
123 }
124 }
127 /***************
128 Delete number from list
129 ***************/
131 /* Handle interactions: delete */
132 if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
133 $this->delNumber ($_POST['numbers']);
134 }
137 /***************
138 Template output
139 ***************/
141 $smarty = get_smarty();
142 foreach($this->attributes as $name){
143 $smarty->assign($name,$this->$name);
144 }
145 $tmp = $this->plInfo();
146 foreach($tmp['plProvidedAcls'] as $name => $translation){
147 $smarty->assign($name."ACL",$this->getacl($name));
148 }
149 $smarty->assign("goFaxBlocklist",$this->goFaxBlocklist);
150 $smarty->assign("cnACL",$this->getacl("cn",$this->readonly));
151 $smarty->assign("typeACL",$this->getacl("type",$this->readonly));
152 $smarty->assign("base",$this->base);
153 $smarty->assign("bases", $this->get_allowed_bases());
154 $smarty->assign("types", array(BLOCK_LIST_SEND => _("send"), BLOCK_LIST_RECEIVE => _("receive")));
155 $smarty->assign("type", $this->type);
156 return($smarty->fetch(get_template_path('generic.tpl', TRUE)));
157 }
160 public function save_object()
161 {
162 if(isset($_POST['blocklist_posted'])){
163 $tmp_cn = $this->cn;
164 plugin::save_object();
165 $this->cn = $tmp_cn;
167 /* Save base, since this is no LDAP attribute */
168 $tmp = $this->get_allowed_bases();
169 if(isset($_POST['base'])){
170 if(isset($tmp[$_POST['base']])){
171 $this->base= $_POST['base'];
172 }
173 }
174 $tmp = $this->attributes;
175 $tmp[] = "type";
176 foreach($tmp as $attr){
177 if(in_array($attr,array("cn","type")) && $this->readonly){
178 continue;
179 }elseif(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
180 $this->$attr = $_POST[$attr];
181 }
182 }
183 }
184 }
187 function remove_from_parent()
188 {
189 $ldap= $this->config->get_ldap_link();
190 $ldap->rmDir($this->dn);
191 new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
192 show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
193 $this->handle_post_events("remove");
194 }
197 /* Check values */
198 function check()
199 {
200 /* Call common method to give check the hook */
201 $message= plugin::check();
203 /* check syntax: must cn */
204 if ($this->cn == ""){
205 $message[]= _("Required field 'Name' is not set.");
206 } else {
207 if (!is_uid($this->cn)){
208 $message[]= _("Required field 'Name' contains invalid characters");
209 }
210 if ($this->dn == 'new'){
211 $ldap= $this->config->get_ldap_link();
212 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
213 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
214 if ($ldap->count() != 0){
215 $message[]= _("Specified name is already used.");
216 }
217 }
218 }
219 return $message;
220 }
223 /* Save to LDAP */
224 function save()
225 {
226 plugin::save();
228 /* Type selection */
229 if ($this->type == BLOCK_LIST_SEND){
230 $type= "goFaxSBlocklist";
231 $this->attrs['objectClass']= "goFaxSBlock";
232 } else {
233 $type= "goFaxRBlocklist";
234 $this->attrs['objectClass']= "goFaxRBlock";
235 }
237 /* Add list */
238 if (count($this->goFaxBlocklist)){
239 $this->attrs[$type]= $this->goFaxBlocklist;
240 }
242 /* Write back to ldap */
243 $ldap= $this->config->get_ldap_link();
244 $ldap->cd($this->base);
245 $ldap->cat($this->dn, array('dn'));
246 if ($ldap->count()){
247 if (!isset($this->attrs[$type])){
248 $this->attrs[$type]= array();
249 }
250 $ldap->cd($this->dn);
251 $this->cleanup();
252 $ldap->modify($this->attrs);
253 new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
254 $this->handle_post_events("modify");
255 } else {
256 $ldap->cd($this->config->current['BASE']);
257 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
258 $ldap->cd($this->dn);
259 $ldap->add($this->attrs);
260 new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
261 $this->handle_post_events("add");
262 }
263 show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
264 }
267 /* Add number */
268 function addNumber($number)
269 {
270 if (!in_array($number, $this->goFaxBlocklist)){
271 $this->goFaxBlocklist[]= $number;
272 sort($this->goFaxBlocklist);
273 }
274 }
277 /* Remove number from list */
278 function delNumber($numbers)
279 {
280 $tmp= array();
281 foreach ($this->goFaxBlocklist as $val){
282 if (!in_array($val, $numbers)){
283 $tmp[]= $val;
284 }
285 }
286 $this->goFaxBlocklist= $tmp;
287 }
290 function getCopyDialog()
291 {
292 $smarty = get_smarty();
293 $smarty->assign("cn",$this->cn);
294 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
295 $ret = array();
296 $ret['string'] = $str;
297 $ret['status'] = "";
298 return($ret);
299 }
301 function saveCopyDialog()
302 {
303 if(isset($_POST['cn'])){
304 $this->cn = get_post('cn');
305 }
306 }
309 function PrepareForCopyPaste($source)
310 {
311 plugin::PrepareForCopyPaste($source);
313 /* We will not be able to rename edited blocklists */
314 $this->readonly = TRUE;
316 /* Get blocklist type and defined numbers */
317 if (in_array("goFaxSBlock",$source['objectClass'])){
318 if(isset($source["goFaxSBlocklist"])){
319 for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
320 $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
321 }
322 }
323 $this->type= BLOCK_LIST_SEND;
324 } elseif (in_array("goFaxRBlock",$source['objectClass'])){
325 if(isset($source["goFaxRBlocklist"])){
326 for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
327 $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
328 }
329 }
330 $this->type= BLOCK_LIST_RECEIVE;
331 }
333 /* Set base */
334 if ($this->dn == "new"){
335 if(isset($_SESSION['CurrentMainBase'])){
336 $this->base= $_SESSION['CurrentMainBase'];
337 }else{
338 $ui= get_userinfo();
339 $this->base= dn2base($ui->dn);
340 }
341 } else {
342 $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
343 }
344 }
347 /* Return plugin informations for acl handling */
348 static function plInfo()
349 {
350 return (array(
351 "plShortName" => _("Fax"),
352 "plDescription" => _("Fax blocklists"),
353 "plSelfModify" => FALSE,
354 "plDepends" => array(),
356 "plPriority" => 0,
357 "plSection" => array("administration" => _("FAX Blocklists")),
358 "plCategory" => array("gofaxlist" => array("description" => _("Fax blocklists"),
359 "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
360 "plProvidedAcls" => array(
361 "cn" => _("Name"),
362 "description" => _("Description"),
363 "base" => _("Base"),
364 "goFaxBlocklist" => _("Blocklist"),
365 "type" => _("Blocklist type"))
366 ));
367 }
369 }
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>