1 <?php
3 class gospamserver extends plugin{
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 $dialog = NULL;
35 var $ui = NULL;
36 var $acl = NULL;
38 function gospamserver($config,$dn, $parent= NULL)
39 {
40 /* Init class */
41 plugin::plugin($config,$dn, $parent);
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 }
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 $smarty = get_smarty();
86 if(get_class($this->parent) == "servtabs"){
88 $smarty->assign("servtabs",true);
89 /* Do we need to flip is_account state? */
90 if (isset($_POST['modify_state'])) {
91 $this->is_account = !$this->is_account;
92 }
94 /* Show tab dialog headers */
95 if ($this->is_account) {
96 /* call Add Acoount to add account */
97 $display = $this->show_header(_("Remove spamassassin extension"),
98 _("This server has spamassassin features enabled. You can disable them by clicking below."));
99 } else {
100 /* call remove Account */
101 $display = $this->show_header(_("Add spamassassin service"),
102 _("This server has spamassassin features disabled. You can enable them by clicking below."));
103 return ($display);
104 }
105 }else{
106 $this->is_account =true;
107 $display ="";
108 $smarty->assign("servtabs",false);
109 }
111 /* Add new trusted network */
112 if(isset($_POST['AddNewTrust'])){
113 $this->AddTrust($_POST['NewTrustName']);
114 }
116 /* Delete selected trusted network */
117 if(isset($_POST['DelTrust'])){
118 $this->DelTrust($_POST['TrustedNetworks']);
119 }
121 /* Add a new rule */
122 if(isset($_POST['AddRule'])){
123 $this->dialog = new goSpamServerRule($this->config,$this->dn);
124 }
126 /* Cancel adding/editing specified rule */
127 if(isset($_POST['CancelRule'])){
128 $this->dialog = NULL;
129 }
131 /* Handle post to delete rules */
132 $once = true;
133 foreach($_POST as $name => $value){
134 if(preg_match("/^editRule_/",$name) && $once ){
135 $once = false;
136 $entry = preg_replace("/^editRule_/","",$name);
137 $entry = preg_replace("/_(x|y)$/","",$entry);
138 $rule = $this->Rules[$entry];
139 $name = $entry;
140 $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
141 }
142 if(preg_match("/^delRule_/",$name) && $once ){
143 $once = false;
144 $entry = preg_replace("/^delRule_/","",$name);
145 $entry = preg_replace("/_(x|y)$/","",$entry);
146 unset($this->Rules[$entry]);
147 }
148 }
150 /* Save rules */
151 if(isset($_POST['SaveRule'])){
152 $this->dialog->save_object();
153 $msgs = $this->dialog->check();
154 if(count($msgs)){
155 foreach($msgs as $msg){
156 print_red($msg);
157 }
158 }else{
159 $ret = $this->dialog->save();
160 if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
161 unset($this->Rules[$ret['orig_name']]);
162 }
163 $this->Rules[$ret['name']] = $ret['rule'];
164 $this->dialog = NULL;
165 }
166 }
168 /* Display dialog if available */
169 if($this->dialog && $this->dialog->config){
170 $this->dialog->save_object();
171 return($this->dialog->execute());
172 }
174 /* Assign smarty vars */
175 foreach($this->attributes as $attr){
176 $smarty->assign($attr,$this->$attr);
177 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
178 }
180 /* Assign checkbox states */
181 foreach($this->Flags as $Flag){
182 $var = "saFlags".$Flag;
183 $smarty->assign("saFlags".$Flag."ACL",chkacl($this->acl,$Flag));
184 if($this->$var){
185 $smarty->assign("saFlags".$Flag."CHK"," checked " );
186 }else{
187 $smarty->assign("saFlags".$Flag."CHK","");
188 }
189 }
191 /* Create divlist */
192 $DivRules = new divSelectBox("SpamRules");
193 $DivRules->SetHeight(130);
195 if(preg_match("/disabled/",chkacl($this->acl,"saTrustedNetworks"))){
196 $actions = "";
197 }else{
198 $actions = "<input type='image' src='images/edit.png' name='editRule_%s'>";
199 $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
200 }
202 foreach($this->Rules as $key => $net){
203 $field1 = array("string" => $key );
204 $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
205 $DivRules->AddEntry(array($field1,$field2));
206 }
207 $smarty->assign("divRules",$DivRules->DrawList());
208 $smarty->assign("TrustedNetworks",$this->TrustedNetworks);
210 /* Create Spam score select box entries */
211 $tmp = array();
212 for($i = 0 ; $i <= 20 ; $i ++ ){
213 $tmp[$i] = $i;
214 }
215 $smarty->assign("SpamScore",$tmp);
217 return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
218 }
221 /* Add $post to list of configured trusted */
222 function AddTrust($post)
223 {
224 if(!empty($post)){
225 if(is_ip($post) || is_domain($post) || (is_ip_with_subnetmask($post))){
226 $this->TrustedNetworks[$post] = $post;
227 }else{
228 print_red(_("Specified value is not a valid 'trusted network' value."));
229 }
230 }
231 }
234 /* Delete trusted network */
235 function DelTrust($posts)
236 {
237 foreach($posts as $post){
238 if(isset($this->TrustedNetworks[$post])){
239 unset($this->TrustedNetworks[$post]);
240 }
241 }
242 }
245 /* remove this extension */
246 function remove_from_parent()
247 {
249 if(!$this->is_account && $this->initially_was_account){
251 plugin::remove_from_parent();
253 /* Remove status flag, it is not a memeber of
254 this->attributes, so ensure that it is deleted too */
255 if(!empty($this->StatusFlag)){
256 $this->attrs[$this->StatusFlag] = array();
257 }
259 /* Check if this is a new entry ... add/modify */
260 $ldap = $this->config->get_ldap_link();
261 $ldap->cat($this->dn,array("objectClass"));
262 if($ldap->count()){
263 $ldap->cd($this->dn);
264 $ldap->modify($this->attrs);
265 }else{
266 $ldap->cd($this->dn);
267 $ldap->add($this->attrs);
268 }
269 show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
270 $this->handle_post_events("remove");
271 }
272 }
275 function save()
276 {
277 if(!$this->is_account) return;
278 plugin::save();
280 /* Create Flags */
281 $this->attrs['saFlags'] = array();
282 foreach($this->Flags as $flag){
283 $var = "saFlags".$flag;
284 if($this->$var){
285 $this->attrs['saFlags'].=$flag;
286 }
287 }
289 /* Create trusted network entries */
290 $this->attrs['saTrustedNetworks'] = array();
291 foreach($this->TrustedNetworks as $net){
292 $this->attrs['saTrustedNetworks'][] = $net;
293 }
295 /* Rules */
296 $this->attrs['saRule'] = array();
297 foreach($this->Rules as $name => $rule){
298 $this->attrs['saRule'][] = $name.":".base64_encode($rule);
299 }
301 /* Check if this is a new entry ... add/modify */
302 $ldap = $this->config->get_ldap_link();
303 $ldap->cat($this->dn,array("objectClass"));
304 if($ldap->count()){
305 $ldap->cd($this->dn);
306 $ldap->modify($this->attrs);
307 }else{
308 $ldap->cd($this->dn);
309 $ldap->add($this->attrs);
310 }
311 if($this->initially_was_account){
312 $this->handle_post_events("modify");
313 }else{
314 $this->handle_post_events("add");
315 }
317 show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
318 }
320 function check()
321 {
322 $message = plugin::check();
324 /* Check if required score is numeric */
325 if(!is_numeric($this->saRequiredScore)){
326 $message[] = _("Required score must be a numeric value.");
327 }
329 return($message);
330 }
333 function save_object()
334 {
335 if(isset($_POST['goSpamServer'])){
337 plugin::save_object();
339 /* Check flags */
340 foreach($this->Flags as $flag){
341 $var = "saFlags".$flag;
342 if(isset($_POST[$var])){
343 $this->$var = TRUE;
344 }else{
345 $this->$var = FALSE;
346 }
347 }
348 }
349 }
352 /* Return plugin informations for acl handling
353 function plInfo()
354 {
355 return (array(
356 "plShortName" => _("Spamassassin"),
357 "plDescription" => _("Spamassassin service"),
358 "plSelfModify" => FALSE,
359 "plDepends" => array(),
360 "plPriority" => 0,
361 "plSection" => array("administration"),
362 "plCategory" => array("server"),
363 "plProvidedAcls"=> array(
365 "saRewriteHeader" => _("Rewrite header"),
366 "saTrustedNetworks" => _("Trusted networks"),
367 "saRequiredScore" => _("Required score"),
368 "saRule" => _("Rules"),
370 "saFlagB" => _("Enable use of bayes filtering"),
371 "saFlagb" => _("Enabled bayes auto learning"),
372 "saFlagC" => _("Enable RBL checks"),
373 "saFlagR" => _("Enable use of Razor"),
374 "saFlagD" => _("Enable use of DDC"),
375 "saFlagP" => _("Enable use of Pyzor"))
376 ));
377 }
378 */
380 /* For newer service management dialogs */
381 function getListEntry()
382 {
383 $this->updateStatusState();
384 $flag = $this->StatusFlag;
385 $fields['Status'] = $this->$flag;
386 $fields['Message'] = _("Spamassassin");
387 $fields['AllowStart'] = true;
388 $fields['AllowStop'] = true;
389 $fields['AllowRestart'] = true;
390 $fields['AllowRemove'] = true;
391 $fields['AllowEdit'] = true;
392 return($fields);
393 }
395 function updateStatusState()
396 {
397 if(empty($this->StatusFlag)) return;
399 $attrs = array();
400 $flag = $this->StatusFlag;
401 $ldap = $this->config->get_ldap_link();
402 $ldap->cd($this->cn);
403 $ldap->cat($this->dn,array($flag));
404 if($ldap->count()){
405 $attrs = $ldap->fetch();
406 }
407 if(isset($attrs[$flag][0])){
408 $this->$flag = $attrs[$flag][0];
409 }
410 }
411 function action_hook($add_attrs= array())
412 {
413 /* Find postcreate entries for this class */
414 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
415 if ($command == "" && isset($this->config->data['TABS'])){
416 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
417 }
418 if ($command != ""){
419 /* Walk through attribute list */
420 foreach ($this->attributes as $attr){
421 if (!is_array($this->$attr)){
422 $command= preg_replace("/%$attr/", $this->$attr, $command);
423 }
424 }
425 $command= preg_replace("/%dn/", $this->dn, $command);
426 /* Additional attributes */
427 foreach ($add_attrs as $name => $value){
428 $command= preg_replace("/%$name/", $value, $command);
429 }
431 /* If there are still some %.. in our command, try to fill these with some other class vars */
432 if(preg_match("/%/",$command)){
433 $attrs = get_object_vars($this);
434 foreach($attrs as $name => $value){
435 if(!is_string($value)) continue;
436 $command= preg_replace("/%$name/", $value, $command);
437 }
438 }
440 if (check_command($command)){
441 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
442 $command, "Execute");
444 exec($command);
445 } else {
446 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
447 print_red ($message);
448 }
449 }
450 }
452 /* Directly save new status flag */
453 function setStatus($value)
454 {
455 if($value == "none") return;
456 if(!$this->initially_was_account) return;
457 $ldap = $this->config->get_ldap_link();
458 $ldap->cd($this->dn);
459 $ldap->cat($this->dn,array("objectClass"));
460 if($ldap->count()){
462 $tmp = $ldap->fetch();
463 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
464 $attrs['objectClass'][] = $tmp['objectClass'][$i];
465 }
466 $flag = $this->StatusFlag;
467 $attrs[$flag] = $value;
468 $this->$flag = $value;
469 $ldap->modify($attrs);
470 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
471 $this->action_hook();
472 }
473 }
476 }
477 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
478 ?>