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(session::is_set('CurrentMainBase')){
58 $this->base = session::get('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 (tests::is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
120 $this->addNumber ($_POST['number']);
121 } else {
122 msg_dialog::display(_("Error"), msgPool::invalid(_("Phone number")), ERROR_DIALOG);
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 if (!$ldap->success()){
193 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
194 }
195 $this->handle_post_events("remove");
196 }
199 /* Check values */
200 function check()
201 {
202 /* Call common method to give check the hook */
203 $message= plugin::check();
205 /* check syntax: must cn */
206 if ($this->cn == ""){
207 $message[]= msgPool::required(_("Name"));
208 } else {
209 if (!tests::is_uid($this->cn)){
210 $message[]= msgPool::invalid(_("Name"));
211 }
212 if ($this->dn == 'new'){
213 $ldap= $this->config->get_ldap_link();
214 $ldap->cd (get_ou('blocklistou').$this->config->current["BASE"]);
215 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
216 if ($ldap->count() != 0){
217 $message[]= msgPool::duplicated(_("Name"));
218 }
219 }
220 }
221 return $message;
222 }
225 /* Save to LDAP */
226 function save()
227 {
228 /* Type selection */
229 if ($this->type == BLOCK_LIST_SEND){
230 $type= "goFaxSBlocklist";
231 $this->objectclasses = array("goFaxSBlock");
232 } else {
233 $type= "goFaxRBlocklist";
234 $this->objectclasses = array("goFaxRBlock");
235 }
237 plugin::save();
239 /* Add list */
240 if (count($this->goFaxBlocklist)){
241 $this->attrs[$type]= $this->goFaxBlocklist;
242 }
244 /* Write back to ldap */
245 $ldap= $this->config->get_ldap_link();
246 $ldap->cd($this->base);
247 $ldap->cat($this->dn, array('dn'));
248 if ($ldap->count()){
249 if (!isset($this->attrs[$type])){
250 $this->attrs[$type]= array();
251 }
252 $ldap->cd($this->dn);
253 $this->cleanup();
254 $ldap->modify($this->attrs);
255 new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
256 $this->handle_post_events("modify");
257 } else {
258 $ldap->cd($this->config->current['BASE']);
259 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
260 $ldap->cd($this->dn);
261 $ldap->add($this->attrs);
262 new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
263 $this->handle_post_events("add");
264 }
266 if (!$ldap->success()){
267 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
268 }
269 }
272 /* Add number */
273 function addNumber($number)
274 {
275 if (!in_array($number, $this->goFaxBlocklist)){
276 $this->goFaxBlocklist[]= $number;
277 sort($this->goFaxBlocklist);
278 }
279 }
282 /* Remove number from list */
283 function delNumber($numbers)
284 {
285 $tmp= array();
286 foreach ($this->goFaxBlocklist as $val){
287 if (!in_array($val, $numbers)){
288 $tmp[]= $val;
289 }
290 }
291 $this->goFaxBlocklist= $tmp;
292 }
295 function getCopyDialog()
296 {
297 $smarty = get_smarty();
298 $smarty->assign("cn",$this->cn);
299 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
300 $ret = array();
301 $ret['string'] = $str;
302 $ret['status'] = "";
303 return($ret);
304 }
306 function saveCopyDialog()
307 {
308 if(isset($_POST['cn'])){
309 $this->cn = get_post('cn');
310 }
311 }
314 function PrepareForCopyPaste($source)
315 {
316 plugin::PrepareForCopyPaste($source);
318 /* We will not be able to rename edited blocklists */
319 $this->readonly = TRUE;
321 /* Get blocklist type and defined numbers */
322 if (in_array("goFaxSBlock",$source['objectClass'])){
323 if(isset($source["goFaxSBlocklist"])){
324 for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
325 $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
326 }
327 }
328 $this->type= BLOCK_LIST_SEND;
329 } elseif (in_array("goFaxRBlock",$source['objectClass'])){
330 if(isset($source["goFaxRBlocklist"])){
331 for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
332 $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
333 }
334 }
335 $this->type= BLOCK_LIST_RECEIVE;
336 }
338 /* Set base */
339 if ($this->dn == "new"){
340 if(session::is_set('CurrentMainBase')){
341 $this->base = session::get('CurrentMainBase');
342 }else{
343 $ui= get_userinfo();
344 $this->base= dn2base($ui->dn);
345 }
346 } else {
347 $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
348 }
349 }
352 /* Return plugin informations for acl handling */
353 static function plInfo()
354 {
355 return (array(
356 "plShortName" => _("Fax"),
357 "plDescription" => _("Fax blocklists"),
358 "plSelfModify" => FALSE,
359 "plDepends" => array(),
361 "plPriority" => 0,
362 "plSection" => array("administration" => _("Fax blocklists")),
363 "plCategory" => array("gofaxlist" => array("description" => _("Fax blocklists"),
364 "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
365 "plProvidedAcls" => array(
366 "cn" => _("Name"),
367 "description" => _("Description"),
368 "base" => _("Base"),
369 "goFaxBlocklist" => _("Blocklist"),
370 "type" => _("Blocklist type"))
371 ));
372 }
374 }
375 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
376 ?>