Code

e52198d13c9625aa7713418879d56109d6e61300
[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         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
151           unset($this->Rules[$ret['orig_name']]);
152         }
153         $this->Rules[$ret['name']] = $ret['rule'];
154         $this->dialog = NULL;
155       }
156     }
157    
158     /* Display dialog if available */ 
159     if($this->dialog && $this->dialog->config){
160       $this->dialog->save_object();
161       return($this->dialog->execute());
162     }
164     /* Assign smarty vars */
165     $smarty = get_smarty(); 
166     foreach($this->attributes as $attr){
167       $smarty->assign($attr,$this->$attr);
168       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
169     }
171     /* Assign checkbox states */
172     foreach($this->Flags as $Flag){
173       $var = "saFlags".$Flag;
174       $smarty->assign("saFlags".$Flag."ACL",chkacl($this->acl,$Flag));
175       if($this->$var){
176         $smarty->assign("saFlags".$Flag."CHK"," checked " );
177       }else{
178         $smarty->assign("saFlags".$Flag."CHK","");
179       }
180     }
182     /* Create divlist */
183     $DivRules = new divSelectBox("SpamRules");
184     $DivRules->SetHeight(150);
186     if(preg_match("/disabled/",chkacl($this->acl,"saTrustedNetworks"))){
187       $actions = "";
188     }else{
189       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
190       $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
191     }
193     foreach($this->Rules as $key => $net){
194       $field1 = array("string" => $key );
195       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
196       $DivRules->AddEntry(array($field1,$field2));
197     }
198     $smarty->assign("divRules",$DivRules->DrawList()); 
199     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
201     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
202   }
205   /* Add $post to list of configured trusted */
206   function AddTrust($post)
207   {
208     if(!empty($post)){
209       $this->TrustedNetworks[$post] = $post;
210     }
211   }
214   /* Delete trusted network */
215   function DelTrust($posts)
216   {
217     foreach($posts as $post){
218       if(isset($this->TrustedNetworks[$post])){
219         unset($this->TrustedNetworks[$post]);     
220       }
221     }
222   }
225   /* remove this extension */
226   function remove_from_parent()
227   {
228     if(!$this->is_account && $this->initially_was_account){
229       plugin::remove_from_parent();
231       /* Check if this is a new entry ... add/modify */
232       $ldap = $this->config->get_ldap_link();
233       $ldap->cat($this->dn,array("objectClass"));
234       if($ldap->count()){
235         $ldap->cd($this->dn);
236         $ldap->modify($this->attrs);
237       }else{
238         $ldap->cd($this->dn);
239         $ldap->add($this->attrs);
240       }
241       show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
242       $this->handle_post_events("remove");
243     }
244   }
247   function save()
248   {
249     if(!$this->is_account) return;
250     plugin::save();
252     /* Create Flags */     
253     $this->attrs['saFlags'] = "";
254     foreach($this->Flags as $flag){
255       $var = "saFlags".$flag;
256       if($this->$var){
257         $this->attrs['saFlags'].=$flag;
258       }
259     }
261     /* Create trusted network entries */
262     $this->attrs['saTrustedNetworks'] = array();
263     foreach($this->TrustedNetworks as $net){
264       $this->attrs['saTrustedNetworks'][] = $net; 
265     }    
267     /* Rules */
268     $this->attrs['saRule'] = array();
269     foreach($this->Rules as $name => $rule){
270       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
271     }
273     /* Check if this is a new entry ... add/modify */
274     $ldap = $this->config->get_ldap_link();
275     $ldap->cat($this->dn,array("objectClass"));
276     if($ldap->count()){
277       $ldap->cd($this->dn);
278       $ldap->modify($this->attrs);
279     }else{
280       $ldap->cd($this->dn);
281       $ldap->add($this->attrs);
282     }
283     if($this->initially_was_account){
284       $this->handle_post_events("modify");
285     }else{
286       $this->handle_post_events("add");
287     }
289     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
290   }
292   function check()
293   { 
294     $message = plugin::check();
295     return($message);
296   }
297   
299   function save_object()
300   {
301     if(isset($_POST['goSpamServer'])){
302       plugin::save_object();
303       foreach($this->Flags as $flag){
304         $var = "saFlags".$flag;
305         if(isset($_POST[$var])){
306           $this->$var = TRUE;
307         }else{
308           $this->$var = FALSE;
309         }
310       }
311     }    
312   }  
314   
315   /* Return plugin informations for acl handling 
316   function plInfo()
317   {
318     return (array(
319           "plShortName"   => _("Spamassassin"),
320           "plDescription" => _("Spamassassin service"),
321           "plSelfModify"  => FALSE,
322           "plDepends"     => array(),
323           "plPriority"    => 0,
324           "plSection"     => array("administration"),
325           "plCategory"    => array("server"),
326           "plProvidedAcls"=> array(
328             "saRewriteHeader"   => _("Rewrite header"),
329             "saTrustedNetworks" => _("Trusted networks"),
330             "saRequiredScore"   => _("Required score"),
331             "saRule"            => _("Rules"),
333             "saFlagB"           => _("Enable use of bayes filtering"),
334             "saFlagb"           => _("Enabled bayes auto learning"),
335             "saFlagC"           => _("Enable RBL checks"),
336             "saFlagR"           => _("Enable use of Razor"),
337             "saFlagD"           => _("Enable use of DDC"),
338             "saFlagP"           => _("Enable use of Pyzor"))
339           ));
340   }
341   */
343   /* For newer service management dialogs */
344   /*function getListEntry()
345   {
346     $this->updateStatusState();
347     $flag                   = $this->StatusFlag;
348     $fields['Status']       = $this->$flag;
349     $fields['Message']      = _("spamassassin");
350     $fields['AllowStart']   = true;
351     $fields['AllowStop']    = true;
352     $fields['AllowRestart'] = true;
353     $fields['AllowRemove']  = true;
354     $fields['AllowEdit']    = true;
355     return($fields);
356   }
358   function updateStatusState()
359   {
360     if(empty($this->StatusFlag)) return;
362     $attrs = array();
363     $flag = $this->StatusFlag;
364     $ldap = $this->config->get_ldap_link();
365     $ldap->cd($this->cn);
366     $ldap->cat($this->dn,array($flag));
367     if($ldap->count()){
368       $attrs = $ldap->fetch();
369     }
370     if(isset($attrs[$flag][0])){
371       $this->$flag = $attrs[$flag][0];
372     }
373   }
374   function action_hook($add_attrs= array())
375   {
376     /* Find postcreate entries for this class * /
377     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
378     if ($command == "" && isset($this->config->data['TABS'])){
379       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
380     }
381     if ($command != ""){
382       /* Walk through attribute list * /
383       foreach ($this->attributes as $attr){
384         if (!is_array($this->$attr)){
385           $command= preg_replace("/%$attr/", $this->$attr, $command);
386         }
387       }
388       $command= preg_replace("/%dn/", $this->dn, $command);
389       /* Additional attributes * /
390       foreach ($add_attrs as $name => $value){
391         $command= preg_replace("/%$name/", $value, $command);
392       }
394       /* If there are still some %.. in our command, try to fill these with some other class vars * /
395       if(preg_match("/%/",$command)){
396         $attrs = get_object_vars($this);
397         foreach($attrs as $name => $value){
398           if(!is_string($value)) continue;
399           $command= preg_replace("/%$name/", $value, $command);
400         }
401       }
403       if (check_command($command)){
404         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
405             $command, "Execute");
407         exec($command);
408       } else {
409         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
410         print_red ($message);
411       }
412     }
413   }
415   /* Directly save new status flag * /
416   function setStatus($value)
417   {
418     if($value == "none") return;
419     if(!$this->initially_was_account) return;
420     $ldap = $this->config->get_ldap_link();
421     $ldap->cd($this->dn);
422     $ldap->cat($this->dn,array("objectClass"));
423     if($ldap->count()){
425       $tmp = $ldap->fetch();
426       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
427         $attrs['objectClass'][] = $tmp['objectClass'][$i];
428       }
429       $flag = $this->StatusFlag;
430       $attrs[$flag] = $value;
431       $this->$flag = $value;
432       $ldap->modify($attrs);
433       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
434       $this->action_hook();
435     }
436   }
439   */
442 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
443 ?>