Code

Added goVirusServer service
[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     $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
49     $this->acl= get_module_permission($acl, "goSpamServer", $this->ui->dn);
51     /* Get Flags */
52     foreach($this->Flags as $flag){
53       $var = "saFlags".$flag;
54       if(preg_match("/".$flag."/",$this->saFlags)){
55         $this->$var = TRUE;
56       }
57     }
58     
59     /* Get trusted networks */
60     $this->TrustedNetworks = array();
61     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
62       $var = $this->attrs['saTrustedNetworks'];
63       for($i = 0 ; $i < $var['count'] ; $i ++ ){
64         $var2 = $this->attrs['saTrustedNetworks'][$i];
65         $this->TrustedNetworks[ $var2 ] = $var2; 
66       }
67     }
69     /* Get rules */
70     $this->Rules = array();
71     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
72       $var = $this->attrs['saRule'];
73       for($i = 0 ; $i < $var['count'] ; $i ++ ){
74         $var2 = $this->attrs['saRule'][$i];
75         $name = preg_replace("/:.*$/","",$var2);
76         $value= base64_decode(preg_replace("/^.*:/","",$var2));
77         $this->Rules[ $name ] = $value;
78       }
79     }
80   }
83   function execute()
84   {
85     /* Do we need to flip is_account state? */
86     if (isset($_POST['modify_state'])) {
87       $this->is_account = !$this->is_account;
88     }
89  
90     /* Show tab dialog headers */
91     if ($this->is_account) {
92       /* call Add Acoount to add account */
93       $display = $this->show_header(_("Remove spamassassin extension"), 
94         _("This server has spamassassin features enabled. You can disable them by clicking below."));
95     } else {
96       /* call remove Account */
97       $display = $this->show_header(_("Add spamassassin service"), 
98         _("This server has spamassassin features disabled. You can enable them by clicking below."));
99       return ($display);
100     }
102     /* Add new trusted network */
103     if(isset($_POST['AddNewTrust'])){
104       $this->AddTrust($_POST['NewTrustName']);
105     }
106   
107     /* Delete selected trusted network */
108     if(isset($_POST['DelTrust'])){
109       $this->DelTrust($_POST['TrustedNetworks']);
110     }
112     /* Add a new rule */
113     if(isset($_POST['AddRule'])){
114       $this->dialog = new goSpamServerRule($this->config,$this->dn);
115     }
116   
117     /* Cancel adding/editing specified rule */
118     if(isset($_POST['CancelRule'])){
119       $this->dialog = NULL;
120     }
122     /* Handle post to delete rules */
123     $once = true;
124     foreach($_POST as $name => $value){
125       if(preg_match("/^editRule_/",$name) && $once ){
126         $once = false;
127         $entry = preg_replace("/^editRule_/","",$name);
128         $entry = preg_replace("/_(x|y)$/","",$entry);
129         $rule = $this->Rules[$entry];
130         $name = $entry;
131         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
132       }
133       if(preg_match("/^delRule_/",$name) && $once ){
134         $once = false;
135         $entry = preg_replace("/^delRule_/","",$name);
136         $entry = preg_replace("/_(x|y)$/","",$entry);
137         unset($this->Rules[$entry]);
138       }
139     }
141     /* Save rules */
142     if(isset($_POST['SaveRule'])){
143       $this->dialog->save_object();
144       $msgs = $this->dialog->check();
145       if(count($msgs)){
146         foreach($msgs as $msg){
147           print_red($msg);
148         }
149       }else{
150         $ret = $this->dialog->save();
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(130);
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();
304       foreach($this->Flags as $flag){
305         $var = "saFlags".$flag;
306         if(isset($_POST[$var])){
307           $this->$var = TRUE;
308         }else{
309           $this->$var = FALSE;
310         }
311       }
312     }    
313   }  
315   
316   /* Return plugin informations for acl handling 
317   function plInfo()
318   {
319     return (array(
320           "plShortName"   => _("Spamassassin"),
321           "plDescription" => _("Spamassassin service"),
322           "plSelfModify"  => FALSE,
323           "plDepends"     => array(),
324           "plPriority"    => 0,
325           "plSection"     => array("administration"),
326           "plCategory"    => array("server"),
327           "plProvidedAcls"=> array(
329             "saRewriteHeader"   => _("Rewrite header"),
330             "saTrustedNetworks" => _("Trusted networks"),
331             "saRequiredScore"   => _("Required score"),
332             "saRule"            => _("Rules"),
334             "saFlagB"           => _("Enable use of bayes filtering"),
335             "saFlagb"           => _("Enabled bayes auto learning"),
336             "saFlagC"           => _("Enable RBL checks"),
337             "saFlagR"           => _("Enable use of Razor"),
338             "saFlagD"           => _("Enable use of DDC"),
339             "saFlagP"           => _("Enable use of Pyzor"))
340           ));
341   }
342   */
344   /* For newer service management dialogs */
345   /*function getListEntry()
346   {
347     $this->updateStatusState();
348     $flag                   = $this->StatusFlag;
349     $fields['Status']       = $this->$flag;
350     $fields['Message']      = _("spamassassin");
351     $fields['AllowStart']   = true;
352     $fields['AllowStop']    = true;
353     $fields['AllowRestart'] = true;
354     $fields['AllowRemove']  = true;
355     $fields['AllowEdit']    = true;
356     return($fields);
357   }
359   function updateStatusState()
360   {
361     if(empty($this->StatusFlag)) return;
363     $attrs = array();
364     $flag = $this->StatusFlag;
365     $ldap = $this->config->get_ldap_link();
366     $ldap->cd($this->cn);
367     $ldap->cat($this->dn,array($flag));
368     if($ldap->count()){
369       $attrs = $ldap->fetch();
370     }
371     if(isset($attrs[$flag][0])){
372       $this->$flag = $attrs[$flag][0];
373     }
374   }
375   function action_hook($add_attrs= array())
376   {
377     /* Find postcreate entries for this class * /
378     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
379     if ($command == "" && isset($this->config->data['TABS'])){
380       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
381     }
382     if ($command != ""){
383       /* Walk through attribute list * /
384       foreach ($this->attributes as $attr){
385         if (!is_array($this->$attr)){
386           $command= preg_replace("/%$attr/", $this->$attr, $command);
387         }
388       }
389       $command= preg_replace("/%dn/", $this->dn, $command);
390       /* Additional attributes * /
391       foreach ($add_attrs as $name => $value){
392         $command= preg_replace("/%$name/", $value, $command);
393       }
395       /* If there are still some %.. in our command, try to fill these with some other class vars * /
396       if(preg_match("/%/",$command)){
397         $attrs = get_object_vars($this);
398         foreach($attrs as $name => $value){
399           if(!is_string($value)) continue;
400           $command= preg_replace("/%$name/", $value, $command);
401         }
402       }
404       if (check_command($command)){
405         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
406             $command, "Execute");
408         exec($command);
409       } else {
410         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
411         print_red ($message);
412       }
413     }
414   }
416   /* Directly save new status flag * /
417   function setStatus($value)
418   {
419     if($value == "none") return;
420     if(!$this->initially_was_account) return;
421     $ldap = $this->config->get_ldap_link();
422     $ldap->cd($this->dn);
423     $ldap->cat($this->dn,array("objectClass"));
424     if($ldap->count()){
426       $tmp = $ldap->fetch();
427       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
428         $attrs['objectClass'][] = $tmp['objectClass'][$i];
429       }
430       $flag = $this->StatusFlag;
431       $attrs[$flag] = $value;
432       $this->$flag = $value;
433       $ldap->modify($attrs);
434       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
435       $this->action_hook();
436     }
437   }
440   */
443 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
444 ?>