Code

Added Spam dialog
[gosa.git] / plugins / admin / systems / class_goSpamServer.inc
1 <?php
3 class goSpamServer extends plugin{
4         
5   /* This plugin only writes its objectClass */
6   var $objectclasses    = array("goSpamServer");
7   var $attributes       = array("saRewriteHeader","saTrustedNetworks","saRequiredScore","saFlags","saRule");
8   var $StatusFlag       = "saStatus";
9  
10   /* This class can't be assigned twice so it conflicts with itsself */
11   var $conflicts        = array("goSpamServer");
12   var $Flags            = array("B","b","C","R","D","P");
14   var $DisplayName      = "";
15   var $dn               = NULL;
16   var $cn               = "";
17   var $saStatus         = "";
19   var $saRewriteHeader  = "";
20   var $saTrustedNetworks= array();
21   var $TrustedNetworks  = array();
22   var $saRequiredScore  = 0;
23   var $saFlags          = "";
24   var $Rules            = array();
25   var $saRule           = array();
27   var $saFlagsB         = false;
28   var $saFlagsb         = false;
29   var $saFlagsC         = false;
30   var $saFlagsR         = false;
31   var $saFlagsD         = false;
32   var $saFlagsP         = false;
33  
34   var $dialog           = NULL;
35   var $ui               = NULL;
36   var $acl              = NULL;
38   function goSpamServer($config,$dn)
39   {
40     /* Init class */
41     plugin::plugin($config,$dn);
42     $this->DisplayName = _("Spamassassin");
44     /* Get userinfo & acls */
45     $this->ui = get_userinfo();
47     /* Set up the users ACL's for this 'dn' */
48     $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
50     /* Get Flags */
51     foreach($this->Flags as $flag){
52       $var = "saFlags".$flag;
53       if(preg_match("/".$flag."/",$this->saFlags)){
54         $this->$var = TRUE;
55       }
56     }
57     
58     /* Get trusted networks */
59     $this->TrustedNetworks = array();
60     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
61       $var = $this->attrs['saTrustedNetworks'];
62       for($i = 0 ; $i < $var['count'] ; $i ++ ){
63         $var2 = $this->attrs['saTrustedNetworks'][$i];
64         $this->TrustedNetworks[ $var2 ] = $var2; 
65       }
66     }
68     /* Get rules */
69     $this->Rules = array();
70     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
71       $var = $this->attrs['saRule'];
72       for($i = 0 ; $i < $var['count'] ; $i ++ ){
73         $var2 = $this->attrs['saRule'][$i];
74         $name = preg_replace("/:.*$/","",$var2);
75         $value= base64_decode(preg_replace("/^.*:/","",$var2));
76         $this->Rules[ $name ] = $value;
77       }
78     }
79   }
82   function execute()
83   {
84     /* Do we need to flip is_account state? */
85     if (isset($_POST['modify_state'])) {
86       $this->is_account = !$this->is_account;
87     }
88  
89     /* Show tab dialog headers */
90     if ($this->is_account) {
91       /* call Add Acoount to add account */
92       $display = $this->show_header(_("Remove spamassassin extension"), 
93         _("This server has spamassassin features enabled. You can disable them by clicking below."));
94     } else {
95       /* call remove Account */
96       $display = $this->show_header(_("Add spamassassin service"), 
97         _("This server has spamassassin features disabled. You can enable them by clicking below."));
98       return ($display);
99     }
101     /* Add new trusted network */
102     if(isset($_POST['AddNewTrust'])){
103       $this->AddTrust($_POST['NewTrustName']);
104     }
105   
106     /* Delete selected trusted network */
107     if(isset($_POST['DelTrust'])){
108       $this->DelTrust($_POST['TrustedNetworks']);
109     }
111     /* Add a new rule */
112     if(isset($_POST['AddRule'])){
113       $this->dialog = new goSpamServerRule($this->config,$this->dn);
114     }
115   
116     /* Cancel adding/editing specified rule */
117     if(isset($_POST['CancelRule'])){
118       $this->dialog = NULL;
119     }
121     /* Handle post to delete rules */
122     $once = true;
123     foreach($_POST as $name => $value){
124       if(preg_match("/^editRule_/",$name) && $once ){
125         $once = false;
126         $entry = preg_replace("/^editRule_/","",$name);
127         $entry = preg_replace("/_(x|y)$/","",$entry);
128         $rule = $this->Rules[$entry];
129         $name = $entry;
130         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
131       }
132       if(preg_match("/^delRule_/",$name) && $once ){
133         $once = false;
134         $entry = preg_replace("/^delRule_/","",$name);
135         $entry = preg_replace("/_(x|y)$/","",$entry);
136         unset($this->Rules[$entry]);
137       }
138     }
140     /* Save rules */
141     if(isset($_POST['SaveRule'])){
142       $this->dialog->save_object();
143       $msgs = $this->dialog->check();
144       if(count($msgs)){
145         foreach($msgs as $msg){
146           print_red($msg);
147         }
148       }else{
149         $ret = $this->dialog->save();
150         print_a($ret);
151         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
152           unset($this->Rules[$ret['orig_name']]);
153         }
154         $this->Rules[$ret['name']] = $ret['rule'];
155         $this->dialog = NULL;
156       }
157     }
158    
159     /* Display dialog if available */ 
160     if($this->dialog && $this->dialog->config){
161       $this->dialog->save_object();
162       return($this->dialog->execute());
163     }
165     /* Assign smarty vars */
166     $smarty = get_smarty(); 
167     foreach($this->attributes as $attr){
168       $smarty->assign($attr,$this->$attr);
169       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
170     }
172     /* Assign checkbox states */
173     foreach($this->Flags as $Flag){
174       $var = "saFlags".$Flag;
175       $smarty->assign("saFlags".$Flag."ACL",chkacl($this->acl,$Flag));
176       if($this->$var){
177         $smarty->assign("saFlags".$Flag."CHK"," checked " );
178       }else{
179         $smarty->assign("saFlags".$Flag."CHK","");
180       }
181     }
183     /* Create divlist */
184     $DivRules = new divSelectBox("SpamRules");
185     $DivRules->SetHeight(150);
187     if(preg_match("/disabled/",chkacl($this->acl,"saTrustedNetworks"))){
188       $actions = "";
189     }else{
190       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
191       $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
192     }
194     foreach($this->Rules as $key => $net){
195       $field1 = array("string" => $key );
196       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
197       $DivRules->AddEntry(array($field1,$field2));
198     }
199     $smarty->assign("divRules",$DivRules->DrawList()); 
200     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
202     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
203   }
206   /* Add $post to list of configured trusted */
207   function AddTrust($post)
208   {
209     if(!empty($post)){
210       $this->TrustedNetworks[$post] = $post;
211     }
212   }
215   /* Delete trusted network */
216   function DelTrust($posts)
217   {
218     foreach($posts as $post){
219       if(isset($this->TrustedNetworks[$post])){
220         unset($this->TrustedNetworks[$post]);     
221       }
222     }
223   }
226   /* remove this extension */
227   function remove_from_parent()
228   {
229     if(!$this->is_account && $this->initially_was_account){
230       plugin::remove_from_parent();
232       /* Check if this is a new entry ... add/modify */
233       $ldap = $this->config->get_ldap_link();
234       $ldap->cat($this->dn,array("objectClass"));
235       if($ldap->count()){
236         $ldap->cd($this->dn);
237         $ldap->modify($this->attrs);
238       }else{
239         $ldap->cd($this->dn);
240         $ldap->add($this->attrs);
241       }
242       show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
243       $this->handle_post_events("remove");
244     }
245   }
248   function save()
249   {
250     if(!$this->is_account) return;
251     plugin::save();
253     /* Create Flags */     
254     $this->attrs['saFlags'] = "";
255     foreach($this->Flags as $flag){
256       $var = "saFlags".$flag;
257       if($this->$var){
258         $this->attrs['saFlags'].=$flag;
259       }
260     }
262     /* Create trusted network entries */
263     $this->attrs['saTrustedNetworks'] = array();
264     foreach($this->TrustedNetworks as $net){
265       $this->attrs['saTrustedNetworks'][] = $net; 
266     }    
268     /* Rules */
269     $this->attrs['saRule'] = array();
270     foreach($this->Rules as $name => $rule){
271       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
272     }
274     /* Check if this is a new entry ... add/modify */
275     $ldap = $this->config->get_ldap_link();
276     $ldap->cat($this->dn,array("objectClass"));
277     if($ldap->count()){
278       $ldap->cd($this->dn);
279       $ldap->modify($this->attrs);
280     }else{
281       $ldap->cd($this->dn);
282       $ldap->add($this->attrs);
283     }
284     if($this->initially_was_account){
285       $this->handle_post_events("modify");
286     }else{
287       $this->handle_post_events("add");
288     }
290     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
291   }
293   function check()
294   { 
295     $message = plugin::check();
296     return($message);
297   }
298   
300   function save_object()
301   {
302     if(isset($_POST['goSpamServer'])){
303       plugin::save_object();
305       print_a($_POST);
307       foreach($this->Flags as $flag){
308         $var = "saFlags".$flag;
309         if(isset($_POST[$var])){
310           $this->$var = TRUE;
311         }else{
312           $this->$var = FALSE;
313         }
314       }
315     }    
316   }  
318   
319   /* Return plugin informations for acl handling 
320   function plInfo()
321   {
322     return (array(
323           "plShortName"   => _("Spamassassin"),
324           "plDescription" => _("Spamassassin service"),
325           "plSelfModify"  => FALSE,
326           "plDepends"     => array(),
327           "plPriority"    => 0,
328           "plSection"     => array("administration"),
329           "plCategory"    => array("server"),
330           "plProvidedAcls"=> array(
332             "saRewriteHeader"   => _("Rewrite header"),
333             "saTrustedNetworks" => _("Trusted networks"),
334             "saRequiredScore"   => _("Required score"),
335             "saRule"            => _("Rules"),
337             "saFlagB"           => _("Enable use of bayes filtering"),
338             "saFlagb"           => _("Enabled bayes auto learning"),
339             "saFlagC"           => _("Enable RBL checks"),
340             "saFlagR"           => _("Enable use of Razor"),
341             "saFlagD"           => _("Enable use of DDC"),
342             "saFlagP"           => _("Enable use of Pyzor"))
343           ));
344   }
345   */
347   /* For newer service management dialogs */
348   /*function getListEntry()
349   {
350     $this->updateStatusState();
351     $flag                   = $this->StatusFlag;
352     $fields['Status']       = $this->$flag;
353     $fields['Message']      = _("spamassassin");
354     $fields['AllowStart']   = true;
355     $fields['AllowStop']    = true;
356     $fields['AllowRestart'] = true;
357     $fields['AllowRemove']  = true;
358     $fields['AllowEdit']    = true;
359     return($fields);
360   }
362   function updateStatusState()
363   {
364     if(empty($this->StatusFlag)) return;
366     $attrs = array();
367     $flag = $this->StatusFlag;
368     $ldap = $this->config->get_ldap_link();
369     $ldap->cd($this->cn);
370     $ldap->cat($this->dn,array($flag));
371     if($ldap->count()){
372       $attrs = $ldap->fetch();
373     }
374     if(isset($attrs[$flag][0])){
375       $this->$flag = $attrs[$flag][0];
376     }
377   }
378   function action_hook($add_attrs= array())
379   {
380     /* Find postcreate entries for this class * /
381     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
382     if ($command == "" && isset($this->config->data['TABS'])){
383       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
384     }
385     if ($command != ""){
386       /* Walk through attribute list * /
387       foreach ($this->attributes as $attr){
388         if (!is_array($this->$attr)){
389           $command= preg_replace("/%$attr/", $this->$attr, $command);
390         }
391       }
392       $command= preg_replace("/%dn/", $this->dn, $command);
393       /* Additional attributes * /
394       foreach ($add_attrs as $name => $value){
395         $command= preg_replace("/%$name/", $value, $command);
396       }
398       /* If there are still some %.. in our command, try to fill these with some other class vars * /
399       if(preg_match("/%/",$command)){
400         $attrs = get_object_vars($this);
401         foreach($attrs as $name => $value){
402           if(!is_string($value)) continue;
403           $command= preg_replace("/%$name/", $value, $command);
404         }
405       }
407       if (check_command($command)){
408         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
409             $command, "Execute");
411         exec($command);
412       } else {
413         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
414         print_red ($message);
415       }
416     }
417   }
419   /* Directly save new status flag * /
420   function setStatus($value)
421   {
422     if($value == "none") return;
423     if(!$this->initially_was_account) return;
424     $ldap = $this->config->get_ldap_link();
425     $ldap->cd($this->dn);
426     $ldap->cat($this->dn,array("objectClass"));
427     if($ldap->count()){
429       $tmp = $ldap->fetch();
430       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
431         $attrs['objectClass'][] = $tmp['objectClass'][$i];
432       }
433       $flag = $this->StatusFlag;
434       $attrs[$flag] = $value;
435       $this->$flag = $value;
436       $ldap->modify($attrs);
437       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
438       $this->action_hook();
439     }
440   }
443   */
446 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
447 ?>