Code

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