[gosa.git] / trunk / 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";
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;
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 }
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 }
76 }
79 function execute()
80 {
81 $display ="";
82 $smarty = get_smarty();
84 if($this->is_account && !$this->view_logged){
85 $this->view_logged = TRUE;
86 new log("view","server/".get_class($this),$this->dn);
87 }
89 /* If displayed, it is ever true*/
90 $this->is_account =true;
92 /* Get acls */
93 $tmp = $this->plinfo();
94 foreach($tmp['plProvidedAcls'] as $name => $translation){
95 $smarty->assign($name."ACL",$this->getacl($name));
96 }
98 /* Add new trusted network */
99 if(isset($_POST['AddNewTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
100 $this->AddTrust($_POST['NewTrustName']);
101 }
103 /* Delete selected trusted network */
104 if(isset($_POST['DelTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
105 $this->DelTrust($_POST['TrustedNetworks']);
106 }
108 /* Add a new rule */
109 if(isset($_POST['AddRule']) && $this->acl_is_writeable("saRule")){
110 $this->dialog = new goSpamServerRule($this->config,$this->dn);
111 }
113 /* Cancel adding/editing specified rule */
114 if(isset($_POST['CancelRule'])){
115 $this->dialog = FALSE;
116 }
118 /* Handle post to delete rules */
119 $once = true;
120 foreach($_POST as $name => $value){
121 if(preg_match("/^editRule_/",$name) && $once && $this->acl_is_readable("saRule")){
122 $once = false;
123 $entry = preg_replace("/^editRule_/","",$name);
124 $entry = preg_replace("/_(x|y)$/","",$entry);
125 $rule = $this->Rules[$entry];
126 $name = $entry;
127 $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
128 }
129 if(preg_match("/^delRule_/",$name) && $once && $this->acl_is_writeable("saRule")){
130 $once = false;
131 $entry = preg_replace("/^delRule_/","",$name);
132 $entry = preg_replace("/_(x|y)$/","",$entry);
133 unset($this->Rules[$entry]);
134 }
135 }
137 /* Save rules */
138 if(isset($_POST['SaveRule'])){
139 $this->dialog->save_object();
140 $msgs = $this->dialog->check();
141 if(count($msgs)){
142 foreach($msgs as $msg){
143 msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
144 }
145 }elseif($this->acl_is_writeable("saRule")){
146 $ret = $this->dialog->save();
147 if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
148 unset($this->Rules[$ret['orig_name']]);
149 }
150 $this->Rules[$ret['name']] = $ret['rule'];
151 $this->dialog = FALSE;
152 }
153 }
155 /* Display dialog if available */
156 if($this->dialog && $this->dialog->config){
157 $this->dialog->save_object();
158 return($this->dialog->execute());
159 }
161 /* Assign smarty vars */
162 foreach($this->attributes as $attr){
163 $smarty->assign($attr,$this->$attr);
164 }
166 /* Assign checkbox states */
167 foreach($this->Flags as $Flag){
168 $var = "saFlags".$Flag;
169 $smarty->assign("saFlags".$Flag."ACL", $this->getacl($Flag));
170 if($this->$var){
171 $smarty->assign("saFlags".$Flag."CHK"," checked " );
172 }else{
173 $smarty->assign("saFlags".$Flag."CHK","");
174 }
175 }
177 /* Create divlist */
178 $DivRules = new divSelectBox("SpamRules");
179 $DivRules->SetHeight(130);
181 if(!$this->acl_is_writeable("saTrustedNetworks")){
182 $actions = "";
183 }else{
184 $actions = "<input type='image' src='images/lists/edit.png' name='editRule_%s'>";
185 if($this->acl_is_writeable("saRule")){
186 $actions.= "<input type='image' src='images/lists/trash.png' name='delRule_%s'>";
187 }
188 }
190 foreach($this->Rules as $key => $net){
191 $field1 = array("string" => $key );
192 $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
193 $DivRules->AddEntry(array($field1,$field2));
194 }
195 $smarty->assign("divRules",$DivRules->DrawList());
196 $smarty->assign("TrustedNetworks",$this->TrustedNetworks);
198 /* Create Spam score select box entries */
199 $tmp = array();
200 for($i = 0 ; $i <= 20 ; $i ++ ){
201 $tmp[$i] = $i;
202 }
203 $smarty->assign("SpamScore",$tmp);
205 return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
206 }
209 /* Add $post to list of configured trusted */
210 function AddTrust($post)
211 {
212 if(!empty($post)){
213 if(tests::is_ip($post) || tests::is_domain($post) || (tests::is_ip_with_subnetmask($post))){
214 $this->TrustedNetworks[$post] = $post;
215 }else{
216 msg_dialog::display(_("Error"), msgPool::invalid(_("Trusted network")), ERROR_DIALOG);
217 }
218 }
219 }
222 /* Delete trusted network */
223 function DelTrust($posts)
224 {
225 foreach($posts as $post){
226 if(isset($this->TrustedNetworks[$post])){
227 unset($this->TrustedNetworks[$post]);
228 }
229 }
230 }
232 function save()
233 {
234 if(!$this->is_account) return;
235 plugin::save();
237 /* Create Flags */
238 $this->attrs['saFlags'] = array();
239 foreach($this->Flags as $flag){
240 $var = "saFlags".$flag;
241 if($this->$var){
242 $this->attrs['saFlags'].=$flag;
243 }
244 }
246 /* Create trusted network entries */
247 $this->attrs['saTrustedNetworks'] = array();
248 foreach($this->TrustedNetworks as $net){
249 $this->attrs['saTrustedNetworks'][] = $net;
250 }
252 /* Rules */
253 $this->attrs['saRule'] = array();
254 foreach($this->Rules as $name => $rule){
255 $this->attrs['saRule'][] = $name.":".base64_encode($rule);
256 }
258 /* Check if this is a new entry ... add/modify */
259 $ldap = $this->config->get_ldap_link();
260 $ldap->cat($this->dn,array("objectClass"));
261 if($ldap->count()){
262 $ldap->cd($this->dn);
263 $ldap->modify($this->attrs);
264 }else{
265 $ldap->cd($this->dn);
266 $ldap->add($this->attrs);
267 }
268 if($this->initially_was_account){
269 $this->handle_post_events("modify");
270 new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
271 }else{
272 $this->handle_post_events("add");
273 new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
274 }
276 if (!$ldap->success()){
277 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
278 }
279 }
281 function check()
282 {
283 $message = plugin::check();
285 /* Check if required score is numeric */
286 if(!is_numeric($this->saRequiredScore)){
287 $message[] = msgPool::invalid(_("Score"),$this->saRequiredScore,"/[0-9]/");
288 }
290 return($message);
291 }
294 function save_object()
295 {
296 if(isset($_POST['goSpamServer'])){
298 plugin::save_object();
300 /* Check flags */
301 foreach($this->Flags as $flag){
302 $var = "saFlags".$flag;
304 if($this->acl_is_writeable($var)){
305 if(isset($_POST[$var])){
306 $this->$var = TRUE;
307 }else{
308 $this->$var = FALSE;
309 }
310 }
311 }
312 }
313 }
316 /* Return plugin informations for acl handling */
317 static function plInfo()
318 {
319 return (array(
320 "plShortName" => _("Spamassassin"),
321 "plDescription" => _("Spamassassin")." ("._("Services").")",
322 "plSelfModify" => FALSE,
323 "plDepends" => array(),
324 "plPriority" => 89,
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 }
343 /* For newer service management dialogs */
344 function getListEntry()
345 {
346 $fields = goService::getListEntry();
347 $fields['Message'] = _("Spamassassin");
348 #$fields['AllowEdit'] = true;
349 return($fields);
350 }
351 }
352 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
353 ?>