Code

Added schema requrements
[gosa.git] / gosa-plugins / mail / admin / systems / services / spam / class_goSpamServer.inc
1 <?php
3 class gospamserver extends goService{
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 $ui               = NULL;
35   var $acl              = NULL;
36   var $view_logged  =FALSE;
38   function gospamserver(&$config,$dn, $parent= NULL)
39   {
40     /* Init class */
41     goService::goService($config,$dn, $parent);
42     $this->DisplayName = _("Spamassassin");
44     /* Get userinfo & acls */
45     $this->ui = get_userinfo();
47     /* Get Flags */
48     foreach($this->Flags as $flag){
49       $var = "saFlags".$flag;
50       if(preg_match("/".$flag."/",$this->saFlags)){
51         $this->$var = TRUE;
52       }
53     }
54     
55     /* Get trusted networks */
56     $this->TrustedNetworks = array();
57     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
58       $var = $this->attrs['saTrustedNetworks'];
59       for($i = 0 ; $i < $var['count'] ; $i ++ ){
60         $var2 = $this->attrs['saTrustedNetworks'][$i];
61         $this->TrustedNetworks[ $var2 ] = $var2; 
62       }
63     }
65     /* Get rules */
66     $this->Rules = array();
67     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
68       $var = $this->attrs['saRule'];
69       for($i = 0 ; $i < $var['count'] ; $i ++ ){
70         $var2 = $this->attrs['saRule'][$i];
71         $name = preg_replace("/:.*$/","",$var2);
72         $value= base64_decode(preg_replace("/^.*:/","",$var2));
73         $this->Rules[ $name ] = $value;
74       }
75     }
77     // Prepare lists
78     $this->ruleList = new sortableListing();
79     $this->ruleList->setDeleteable(true);
80     $this->ruleList->setInstantDelete(true);
81     $this->ruleList->setEditable(true);
82     $this->ruleList->setWidth("100%");
83     $this->ruleList->setHeight("170px");
84     $this->ruleList->setHeader(array(_("Rule")));
85     $this->ruleList->setDefaultSortColumn(0);
86     $this->ruleList->setColspecs(array('*','40px'));
87   }
90   function execute()
91   {
92     $display ="";
93     $smarty = get_smarty(); 
94     
95     if($this->is_account && !$this->view_logged){
96       $this->view_logged = TRUE;
97       new log("view","server/".get_class($this),$this->dn);
98     }
100     /* If displayed, it is ever true*/
101     $this->is_account =true;
103     /* Get acls */
104     $tmp = $this->plinfo();
105     foreach($tmp['plProvidedAcls'] as $name => $translation){
106       $smarty->assign($name."ACL",$this->getacl($name));
107     }
109     /* Add new trusted network */
110     if(isset($_POST['AddNewTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
111       $this->AddTrust($_POST['NewTrustName']);
112     }
113   
114     /* Delete selected trusted network */
115     if(isset($_POST['DelTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
116       $this->DelTrust($_POST['TrustedNetworks']);
117     }
119     /* Add a new rule */
120     if(isset($_POST['AddRule']) && $this->acl_is_writeable("saRule")){
121       $this->dialog = new goSpamServerRule($this->config,$this->dn);
122       $this->dialog->acl_base = $this->acl_base;
123       $this->dialog->acl_category = $this->acl_category;
124     }
125   
126     /* Cancel adding/editing specified rule */
127     if(isset($_POST['CancelRule'])){
128       $this->dialog = FALSE;
129     }
131     /* Handle post to delete rules */
132     $once = true;
133     $this->ruleList->save_object();
134     $action = $this->ruleList->getAction();
135     if($action['action'] == 'delete'){
136         $this->Rules = $this->ruleList->getMaintainedData();
137     }
138     if($action['action'] == 'edit'){
139         $id = $this->ruleList->getKey($action['targets'][0]);
140         $rule = $this->Rules[$id];
141         $this->dialog = new goSpamServerRule($this->config,$this->dn,$id,$rule);
142         $this->dialog->acl_base = $this->acl_base;
143         $this->dialog->acl_category = $this->acl_category;
144     }
146     /* Save rules */
147     if(isset($_POST['SaveRule']) && $this->dialog instanceOf goSpamServerRule){
148       $this->dialog->save_object();
149       $msgs = $this->dialog->check();
150       if(count($msgs)){
151         foreach($msgs as $msg){
152           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
153         }
154       }elseif($this->acl_is_writeable("saRule")){
155         $ret = $this->dialog->save();
156         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
157           unset($this->Rules[$ret['orig_name']]);
158         }
159         $this->Rules[$ret['name']] = $ret['rule'];
160         $this->dialog = FALSE;
161       }
162     }
163    
164     /* Display dialog if available */ 
165     if($this->dialog && $this->dialog->config){
166       $this->dialog->save_object();
167       return($this->dialog->execute());
168     }
170     /* Assign smarty vars */
171     foreach($this->attributes as $attr){
172       $smarty->assign($attr,$this->$attr);
173     }
175     /* Assign checkbox states */
176     foreach($this->Flags as $Flag){
177       $var = "saFlags".$Flag;
178       $smarty->assign("saFlags".$Flag."ACL", $this->getacl($var));
179       if($this->$var){
180         $smarty->assign("saFlags".$Flag."CHK"," checked " );
181       }else{
182         $smarty->assign("saFlags".$Flag."CHK","");
183       }
184     }
186     $this->ruleList->setAcl($this->getacl('saRule'));
187     $data =$lData= array();
188     foreach($this->Rules as $key => $net){
189       $lData[$key]=array('data'=> array($key));
190     }
191     $this->ruleList->setListData($this->Rules, $lData);
192     $this->ruleList->update();
194     $smarty->assign("ruleList",$this->ruleList->render());
195     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
197     /* Create Spam score select box entries */
198     $tmp = array();
199     for($i = 0 ; $i <= 20 ; $i ++ ){
200       $tmp[$i] = $i;
201     }
202     $smarty->assign("SpamScore",$tmp);
204     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
205   }
208   /* Add $post to list of configured trusted */
209   function AddTrust($post)
210   {
211     if(!empty($post)){
212       if(tests::is_ip($post) || tests::is_domain($post) || (tests::is_ip_with_subnetmask($post))){
213         $this->TrustedNetworks[$post] = $post;
214       }else{
215         msg_dialog::display(_("Error"), msgPool::invalid(_("Trusted network")), ERROR_DIALOG);
216       }
217     }
218   }
221   /* Delete trusted network */
222   function DelTrust($posts)
223   {
224     foreach($posts as $post){
225       if(isset($this->TrustedNetworks[$post])){
226         unset($this->TrustedNetworks[$post]);     
227       }
228     }
229   }
231   function save()
232   {
233     if(!$this->is_account) return;
234     plugin::save();
236     /* Create Flags */     
237     $this->attrs['saFlags'] = array();
238     foreach($this->Flags as $flag){
239       $var = "saFlags".$flag;
240       if($this->$var){
241         $this->attrs['saFlags'].=$flag;
242       }
243     }
245     /* Create trusted network entries */
246     $this->attrs['saTrustedNetworks'] = array();
247     foreach($this->TrustedNetworks as $net){
248       $this->attrs['saTrustedNetworks'][] = $net; 
249     }    
251     /* Rules */
252     $this->attrs['saRule'] = array();
253     foreach($this->Rules as $name => $rule){
254       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
255     }
257     /* Check if this is a new entry ... add/modify */
258     $ldap = $this->config->get_ldap_link();
259     $ldap->cat($this->dn,array("objectClass"));
260     if($ldap->count()){
261       $ldap->cd($this->dn);
262       $ldap->modify($this->attrs);
263     }else{
264       $ldap->cd($this->dn);
265       $ldap->add($this->attrs);
266     }
267     if($this->initially_was_account){
268       $this->handle_post_events("modify");
269       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
270     }else{
271       $this->handle_post_events("add");
272       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
273     }
275     if (!$ldap->success()){
276       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
277     }
278   }
280   function check()
281   { 
282     $message = plugin::check();
284     /* Check if required score is numeric */
285     if(!is_numeric($this->saRequiredScore)){
286       $message[] = msgPool::invalid(_("Score"),$this->saRequiredScore,"/[0-9]/");
287     }
289     return($message);
290   }
291   
293   function save_object()
294   {
295     if(isset($_POST['goSpamServer'])){
297       plugin::save_object();
299       /* Check flags */
300       foreach($this->Flags as $flag){
301         $var = "saFlags".$flag;
303         if($this->acl_is_writeable($var)){
304           if(isset($_POST[$var])){
305             $this->$var = TRUE;
306           }else{
307             $this->$var = FALSE;
308           }
309         }
310       }
311     }    
312   }  
314   
315   /* Return plugin informations for acl handling  */
316   static function plInfo()
317   {
318     return (array(
319           "plShortName"   => _("Spamassassin"),
320           "plDescription" => _("Spamassassin")." ("._("Services").")",
321           "plSelfModify"  => FALSE,
322           "plDepends"     => array(),
323           "plPriority"    => 89,
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"),
334             "start"           => _("Start"),
335             "stop"            => _("Stop"),
336             "restart"         => _("Restart"),
338             "saFlagsB"           => _("Enable use of bayes filtering"),
339             "saFlagsb"           => _("Enabled bayes auto learning"),
340             "saFlagsC"           => _("Enable RBL checks"),
341             "saFlagsR"           => _("Enable use of Razor"),
342             "saFlagsD"           => _("Enable use of DDC"),
343             "saFlagsP"           => _("Enable use of Pyzor"))
344           ));
345   }
347   /* For newer service management dialogs */
348   function getListEntry()
349   {
350     $fields                 = goService::getListEntry();
351     $fields['Message']      = _("Spamassassin");
352     #$fields['AllowEdit']    = true;
353     return($fields);
354   }
356 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
357 ?>