Code

Moved dfs
[gosa.git] / gosa-core / plugins / addons / mailqueue / class_mailqueue.inc
1 <?php
3 class mailqueue extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Mail queue";
7   var $plDescription= "This does something";
9   /* attribute list for save action */
10   var $attributes     = array();
11   var $objectclasses  = array();
12   var $mailQueueScript= "";    
13   var $Server         = "all";
14   var $Time           = 0;
15   var $Search         = "*";
16   var $Page           = 0;
17   var $Stat           = "all";
18   var $OrderBy        = "Arrival";
19   var $SortType       = "up";
20   var $disp_header    = false;
21   var $range          = 20;   
22   var $view_logged = FALSE;
24   function mailqueue(&$config, $dn= NULL)
25   {
26     $this->config           = &$config;
27     if (isset($this->config->current['MAILQUEUESCRIPTPATH'])){
28       $this->mailQueueScript  = $this->config->current['MAILQUEUESCRIPTPATH'];    
29     }
31     if(isset($this->config->data['MAIN']['MAILQUEUESCRIPTPATH'])){
32        $this->mailQueueScript  = $this->config->data['MAIN']['MAILQUEUESCRIPTPATH'];
33     }
35     $this->Server           = "all";
36   }
39   function pass_cmd($str)
40   {
41     return(shell_exec($str));
42   }
45   function execute()
46   {
47     /* Call parent execute */
48     plugin::execute();
50     /* Log view */
51     if(!$this->view_logged){
52       $this->view_logged = TRUE;
53       new log("view","mailqueue/".get_class($this),$this->dn);
54     }
56     if(isset($_POST['EntriesPerPage'])){
57       $this->range = $_POST['EntriesPerPage'];
58     }
60     $smarty= get_smarty();
61     $error =false;
63     if(empty($this->mailQueueScript)){
64       msg_dialog::display(_("Configuration error"), sprintf(_("Missing '%s' directive in configuration!"), "mailqueuescriptpath"), ERROR_DIALOG);
65       $error = true;
66     }else{
68       /* If we have more than one server selected (all), 
69          create a string with all servers separated by ', '
70        */ 
71       if($this->Server=="all"){
72         $se = $this->getServer();
74         unset($se['all']);
75         $se_str = "";
76         foreach($se as $server) {
77           $se_str .= $server." ";
78         }
79       }else{
80         /* We have only one server selected */
81         $se_str = $this->Server;
82       }
84       /* Check all post that will effect all entries */
85       $only_once = true;
86       foreach(array("unhold_all","hold_all","del_all","requeue_all") as $attr){
87         foreach($_POST as $name => $value){
88           if((preg_match("/".$attr."/",$name))&&($only_once)){
89             if(!$this->acl_is_readable($attr)){
90               $only_once = false;
91               msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to submit a '%s' command!"), $attr), ERROR_DIALOG);
92             }else{
93               $only_once = false;
94               $act = preg_replace("/_.*$/i","",$attr);
95               $r_cmd = preg_replace("/%action/" , $act           ,$this->mailQueueScript);
96               $r_cmd = preg_replace("/%server/" , $se_str         ,$r_cmd);
97               $r_cmd = preg_replace("/%id/"     , "ALL"           ,$r_cmd);
98               if($this->pass_cmd($r_cmd)==false){
99                 msg_dialog::display(_("Configuration error"), sprintf(_("'%s' defined for the '%s' directive cannot be executed!"), $r_cmd, "mailqueuescriptpath"), ERROR_DIALOG);
100               }
101             }
102           }
103         }
104       }        
106       /* Check single entry manipulation  posts */
107       $only_once = true;
109       /* act specifies the command to execute */
110       if(isset($_GET['act'])){
111         $opt = $_GET['act'];  
112   
113         /* The option to exec should be one of these */
114         if(in_array($opt,array("unhold","hold","del","requeue","query","header"))){
115           $only_once = false;
117           if(!$this->acl_is_readable($opt)){
118               msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to submit a '%s' command!"), $opt), ERROR_DIALOG);
119           }else{
120             /* Create cmd */
121             $r_cmd = preg_replace("/%action/" , $opt            ,$this->mailQueueScript);
122             $r_cmd = preg_replace("/%server/" , $this->Server   ,$r_cmd);
123             $r_cmd = preg_replace("/%id/"     , $_GET['id']     ,$r_cmd);
125             /* Execute cmd */
126             if(!$str = $this->pass_cmd($r_cmd)){
127               msg_dialog::display(_("Configuration error"), sprintf(_("'%s' defined for the '%s' directive cannot be executed!"), $r_cmd, "mailqueuescriptpath"), ERROR_DIALOG);
128             }else{
129             
130               /* Special handling for option='header' */
131               if($opt == "header"){
132           
133                 /* Create table which displays the header informations */
134                 $this->disp_header ="\n<table width='80%'>";
135                 foreach(split("\n",$str) as $line){
136                   $line = trim($line);
137                   if(empty($line)) {
138                     continue;
139                   }
140                   $this->disp_header .= "\n<tr>";
141                   $tmp0 = preg_replace("/:.*$/","",$line);
142                   $tmp1 = preg_replace("/^.*:/","",$line);
143                   $this->disp_header .= "\n<td style='background-color:#EEEEEE;'>".$tmp0."</td><td>".$tmp1."</td>"; 
144                   $this->disp_header .= "\n</tr>";
145                 }              
146                 $this->disp_header .= "\n</table>";
147               }
148             }
149           }
150         }
151       }        
154       /* Back is posted from the header display page */
155       if(isset($_POST['back'])){
156         $this->disp_header = false;
157       }
159       /* If there is a header in disp_header, then display it */
160       if($this->disp_header){
161         $smarty->assign("header",$this->disp_header);
162         return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
163       }
165       /* tell smarty to display the search results*/
166       $smarty->assign("all_ok"      , "true");
167       
168       /* A single server is selected */
169       if($this->Server != "all"){    
171         /* Create Query cmd */ 
172         $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
173         $q_cmd = preg_replace("/%server/" ,$this->Server,$q_cmd);
174         $q_cmd = preg_replace("/%id/"     ,"all"        ,$q_cmd);
176         if(!$this->acl_is_readable("query")){
177           msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to submit a '%s' command!"), "query"), ERROR_DIALOG);
178           $mailQueueParser = new parseMailQueue("",$this->Server);
179         }else{
181           /* Only display this if the query cmd is executeable */
182           if($str = @$this->pass_cmd ($q_cmd)){
183             /* Parse returned data */
184             $mailQueueParser = new parseMailQueue($str,$this->Server);
185           }else{
186             /* On error/ no return value / false return value */
187             msg_dialog::display(_("Configuration error"), sprintf(_("'%s' defined for the '%s' directive cannot be executed!"), $q_cmd, "mailqueuescriptpath"), ERROR_DIALOG);
188             $error = true;
189           }
190         }
191       }else{
192         $mailQueueParser = NULL;
193         foreach($this->getServer() as $ServerID=>$ServerName){
195           /* Don't query the server named all :) */
196           if($ServerID == "all") continue;
198           /* Prepare query cmd */
199           $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
200           $q_cmd = preg_replace("/%server/" ,$ServerName  ,$q_cmd);
201           $q_cmd = preg_replace("/%id/"     ,"ALL"           ,$q_cmd);
203           
204           if(!$this->acl_is_readable("query")){
205             msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to submit a '%s' command!"), "query"), ERROR_DIALOG);
206             $mailQueueParser = new parseMailQueue("",$this->Server);
207           }else{
209             /* Shell exec this cmd */
210             if($str = @$this->pass_cmd ($q_cmd)){
212               /* If there is no parser available, create one  */
213               if($mailQueueParser === NULL){
214                 $mailQueueParser = new parseMailQueue($str,$ServerID);
215               }else{
216                 $mailQueueParser->parseAdditionalQueue($str,$ServerID);
217               }
218               /* On error/ no return value / false return value */
219             }else{
220               msg_dialog::display(_("Configuration error"), sprintf(_("'%s' defined for the '%s' directive cannot be executed!"), $q_cmd, "mailqueuescriptpath"), ERROR_DIALOG);
221               $error = true;
222             }
223           }
224         }
225       }     
227       /* Check for existing servers 
228           objectClass=goMailServer is required at least for one server. 
229          Else display error   */
230       $server = $this->getServer();
231       if((count($server) == 1 ) && (isset($server['all']))){
232         msg_dialog::display(_("Configuration error"), _("No mail servers sepcified!"), ERROR_DIALOG);
233         $error = true;
234       }
236       if(!$error){
238         /* Filter data with the given */
239         $mailQueueParser->OrderBy($this->OrderBy,$this->SortType);
240         $mailQueueParser->OnlyDaysAgo($this->Time);
241         $mailQueueParser->CreateDate();
242      
243         if($this->Stat == "hold"){
244           $mailQueueParser->Search(true,array("Hold"),true);
245         }
246         if($this->Stat == "unhold"){
247           $mailQueueParser->Search(false,array("Hold"),true);
248         }
249         if($this->Stat == "active"){
250           $mailQueueParser->Search(true,array("Active"),true);
251         }
252         if($this->Stat == "nonactive"){
253           $mailQueueParser->Search(false,array("Active"),true);
254         }
256         $mailQueueParser->Search($this->Search,array("MailID","Size","Sender","Recipient","Error","Arrival"));
258         /* */
259         $entries = $mailQueueParser->GetAll();
260   
261         if(count($entries) ==0 ){
262           $smarty->assign("all_ok",false);
263         }
264  
265         $smarty->assign("entries"       , array_slice($entries,$this->Page,$this->range));
266         $smarty->assign("plug"          , "?plug=".$_GET['plug']);
267         $smarty->assign("r_stats"       , $this->getStats());
268         $smarty->assign("stats"         , array_flip($this->getStats()));
269         $smarty->assign("stat"          , $this->Stat);
270         $smarty->assign("p_server"      , $this->Server);
271         $smarty->assign("p_servers"     , $this->getServer());
272         $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
273         $smarty->assign("p_time"        , $this->Time);
274         $smarty->assign("p_times"       , $this->getTimes());
275         $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
276         $smarty->assign("search_for"    , $this->Search);
277         $smarty->assign("range_selector", range_selector(count($entries), $this->Page, $this->range,"EntriesPerPage")); 
278         $smarty->assign("OrderBy"       , $this->OrderBy);
280         /* Display sort arrow */
281         if($this->SortType == "up"){
282           $smarty->assign("SortType","<img src='images/sort_up.png' alt='"._("up")."' border='0'>");
283         }else{
284           $smarty->assign("SortType","<img src='images/sort_down.png' alt='"._("down")."' border='0'>");
285         }
286       }
287     }
289     /* In case of an error */
290     if($error){  
291       $smarty->assign("all_ok"        , "false");
292       $smarty->assign("r_stats"       , $this->getStats());
293       $smarty->assign("stats"         , array_flip($this->getStats()));
294       $smarty->assign("stat"          , $this->Stat);
295       $smarty->assign("plug"          , "?plug=".$_GET['plug']);
296       $smarty->assign("p_server"      , $this->Server);
297       $smarty->assign("p_servers"     , $this->getServer());
298       $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
299       $smarty->assign("p_time"        , $this->Time);
300       $smarty->assign("p_times"       , $this->getTimes());
301       $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
302       $smarty->assign("search_for"    , $this->Search);
303       $smarty->assign("OrderBy"       , $this->OrderBy);
304     }
305     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
306   }
309   /* return selectable server */
310   function getServer()
311   {
312     $ret= array("all"=>_("All"));
313     $ldap = $this->config->get_ldap_link();
314     $ldap->cd($this->config->current['BASE']);    
315     $ldap->search("(objectClass=goMailServer)",array("cn"));
316     while($tmp = $ldap->fetch()){
317       $ret[$tmp['cn'][0]]= $tmp['cn'][0];
318     }
319     return($ret);
320   }
323   /* Return selectable times*/
324   function getTimes()
325   {
326     $ret = array();
327     $ret['nolimit']=_("no limit"); 
328     foreach(array(1,2,4,8,12,24,36,48) as $i){
329       if($i == 1){
330         $ret[$i] = $i."&nbsp;"._("hour");
331       }else{
332         $ret[$i] = $i."&nbsp;"._("hours");
333       }
334     }
335     return($ret);
336   }
339   /* Save post values*/
340   function save_object($save_current= FALSE)
341   {
342     if(isset($_POST['p_server'])){
343       $this->Server = $_POST['p_server'];
344     }
345     if(isset($_POST['p_time'])){
346       $this->Time = $_POST['p_time'];
347     }
348     if(isset($_POST['search_for'])){
349       $this->Search = $_POST['search_for'];
350     }
351     if(isset($_POST['Stat'])){
352       $this->Stat = $_POST['Stat'];
353     }
354     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
355       $this->Page = $_GET['start'];
356     }
358     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
359       $old = $this->OrderBy;
360       $this->OrderBy = $_GET['sort'];
361       if($this->OrderBy == $old)
362       {
363         if($this->SortType== "up"){
364           $this->SortType = "down";
365         }else{
366           $this->SortType = "up";
367         }
368       }
369     }
371   }
373   /* Return stats */
374   function getStats()
375   {
376     return(array(
377           "all"     =>_("All"),
378           "hold"    =>_("Hold"),
379           "unhold"  =>_("Un hold"),
380           "active"  =>_("Active"),
381           "nonactive"  =>_("Not active")
382           ));
383   }
385   /* Return plugin informations for acl handling
386      #FIXME You can only read attributes within this report plugin */
387   static function plInfo()
388   {
389     return (array(
390         "plShortName"   => _("Mailqueue"),
391         "plDescription" => _("Mailqueue addon"),
392         "plSelfModify"  => FALSE,
393         "plDepends"     => array(),
394         "plPriority"    => 1,
395         "plSection"     => array("addon"),
396         "plCategory"    => array("mailqueue" => array("objectClass" => "none", "description" => _("Mail queue addon"))),
398         "plProvidedAcls" => array(
399             "unhold_all"      => _("Unhold all messages"),
400             "hold_all"        => _("Hold all messages"),
401             "del_all"         => _("Delete all messages"),
402             "requeue_all"     => _("Requeue all messages"),
403             "unhold"          => _("Unhold message"),
404             "hold"            => _("Hold message"),
405             "del"             => _("Delete message"),
406             "requeue"         => _("Requeue message"),
407             "query"           => _("Gathering queue data"),
408             "header"          => _("Get header information")
409           )
410         ));
411   }
416 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
417 ?>