0ea12925a243bf00a5ddec9d87b302fd3916d74a
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"), _("Phone number is not valid!"), 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[]= _("Name is empty!");
208 } else {
209 if (!tests::is_uid($this->cn)){
210 $message[]= _("Name contains invalid characters!");
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[]= _("Name is already in use!");
218 }
219 }
220 }
221 return $message;
222 }
225 /* Save to LDAP */
226 function save()
227 {
228 plugin::save();
230 /* Type selection */
231 if ($this->type == BLOCK_LIST_SEND){
232 $type= "goFaxSBlocklist";
233 $this->attrs['objectClass']= "goFaxSBlock";
234 } else {
235 $type= "goFaxRBlocklist";
236 $this->attrs['objectClass']= "goFaxRBlock";
237 }
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 }
265 if (!$ldap->success()){
266 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
267 }
268 }
271 /* Add number */
272 function addNumber($number)
273 {
274 if (!in_array($number, $this->goFaxBlocklist)){
275 $this->goFaxBlocklist[]= $number;
276 sort($this->goFaxBlocklist);
277 }
278 }
281 /* Remove number from list */
282 function delNumber($numbers)
283 {
284 $tmp= array();
285 foreach ($this->goFaxBlocklist as $val){
286 if (!in_array($val, $numbers)){
287 $tmp[]= $val;
288 }
289 }
290 $this->goFaxBlocklist= $tmp;
291 }
294 function getCopyDialog()
295 {
296 $smarty = get_smarty();
297 $smarty->assign("cn",$this->cn);
298 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
299 $ret = array();
300 $ret['string'] = $str;
301 $ret['status'] = "";
302 return($ret);
303 }
305 function saveCopyDialog()
306 {
307 if(isset($_POST['cn'])){
308 $this->cn = get_post('cn');
309 }
310 }
313 function PrepareForCopyPaste($source)
314 {
315 plugin::PrepareForCopyPaste($source);
317 /* We will not be able to rename edited blocklists */
318 $this->readonly = TRUE;
320 /* Get blocklist type and defined numbers */
321 if (in_array("goFaxSBlock",$source['objectClass'])){
322 if(isset($source["goFaxSBlocklist"])){
323 for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
324 $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
325 }
326 }
327 $this->type= BLOCK_LIST_SEND;
328 } elseif (in_array("goFaxRBlock",$source['objectClass'])){
329 if(isset($source["goFaxRBlocklist"])){
330 for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
331 $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
332 }
333 }
334 $this->type= BLOCK_LIST_RECEIVE;
335 }
337 /* Set base */
338 if ($this->dn == "new"){
339 if(session::is_set('CurrentMainBase')){
340 $this->base = session::get('CurrentMainBase');
341 }else{
342 $ui= get_userinfo();
343 $this->base= dn2base($ui->dn);
344 }
345 } else {
346 $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
347 }
348 }
351 /* Return plugin informations for acl handling */
352 static function plInfo()
353 {
354 return (array(
355 "plShortName" => _("Fax"),
356 "plDescription" => _("Fax blocklists"),
357 "plSelfModify" => FALSE,
358 "plDepends" => array(),
360 "plPriority" => 0,
361 "plSection" => array("administration" => _("FAX Blocklists")),
362 "plCategory" => array("gofaxlist" => array("description" => _("Fax blocklists"),
363 "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
364 "plProvidedAcls" => array(
365 "cn" => _("Name"),
366 "description" => _("Description"),
367 "base" => _("Base"),
368 "goFaxBlocklist" => _("Blocklist"),
369 "type" => _("Blocklist type"))
370 ));
371 }
373 }
374 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
375 ?>