Code

Added must asterisk to home Server attribute
[gosa.git] / 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;   
23   function mailqueue($config, $dn= NULL)
24   {
25     $this->config           = $config;
26     if (isset($this->config->current['MAILQUEUESCRIPTPATH'])){
27       $this->mailQueueScript  = $this->config->current['MAILQUEUESCRIPTPATH'];    
28     }
30     if(isset($this->config->data['MAIN']['MAILQUEUESCRIPTPATH'])){
31        $this->mailQueueScript  = $this->config->data['MAIN']['MAILQUEUESCRIPTPATH'];
32     }
34     $this->Server           = "all";
35   }
38   function pass_cmd($str)
39   {
40 //    print_red($str);
41     return(shell_exec($str));
42   }
45   function execute()
46   {
47     /* Call parent execute */
48     plugin::execute();
50     if(isset($_POST['EntriesPerPage'])){
51       $this->range = $_POST['EntriesPerPage'];
52     }
54     $smarty= get_smarty();
55     $error =false;
57     if(empty($this->mailQueueScript)){
58       print_red(_("Please check your 'gosa.conf', there is no 'MAILQUEUESCRIPTPATH' specified."));
59       $error = true;
60     }else{
62       /* If we have more than one server selected (all), 
63          create a string with all servers separated by ', '
64        */ 
65       if($this->Server=="all"){
66         $se = $this->getServer();
68         unset($se['all']);
69         $se_str = "";
70         foreach($se as $server) {
71           $se_str .= $server." ";
72         }
73       }else{
74         /* We have only one server selected */
75         $se_str = $this->Server;
76       }
78       /* Check all post that will effect all entries */
79       $only_once = true;
80       foreach(array("unhold_all","hold_all","del_all","requeue_all") as $attr){
81         foreach($_POST as $name => $value){
82           if((preg_match("/".$attr."/",$name))&&($only_once)){
83             if(chkacl($this->acl,$attr)!=""){
84               $only_once = false;
85               print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),$attr));
86             }else{
87               $only_once = false;
88               $act = preg_replace("/_.*$/i","",$attr);
89               $r_cmd = preg_replace("/%action/" , $act           ,$this->mailQueueScript);
90               $r_cmd = preg_replace("/%server/" , $se_str         ,$r_cmd);
91               $r_cmd = preg_replace("/%id/"     , "ALL"           ,$r_cmd);
92               if($this->pass_cmd($r_cmd)==false){
93                 print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$r_cmd));
94               }
95             }
96           }
97         }
98       }        
100       /* Check single entry manipulation  posts */
101       $only_once = true;
103       /* act specifies the command to execute */
104       if(isset($_GET['act'])){
105         $opt = $_GET['act'];  
106   
107         /* The option to exec should be one of these */
108         if(in_array($opt,array("unhold","hold","del","requeue","query","header"))){
109           $only_once = false;
111           if(chkacl($this->acl,$opt)!=""){
112               print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),$opt));
113           }else{
114             /* Create cmd */
115             $r_cmd = preg_replace("/%action/" , $opt            ,$this->mailQueueScript);
116             $r_cmd = preg_replace("/%server/" , $this->Server   ,$r_cmd);
117             $r_cmd = preg_replace("/%id/"     , $_GET['id']     ,$r_cmd);
119             /* Execute cmd */
120             if(!$str = $this->pass_cmd($r_cmd)){
121               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$r_cmd));
122             }else{
123             
124               /* Special handling for option='header' */
125               if($opt == "header"){
126           
127                 /* Create table which displays the header informations */
128                 $this->disp_header ="\n<table width='80%'>";
129                 foreach(split("\n",$str) as $line){
130                   $line = trim($line);
131                   if(empty($line)) {
132                     continue;
133                   }
134                   $this->disp_header .= "\n<tr>";
135                   $tmp0 = preg_replace("/:.*$/","",$line);
136                   $tmp1 = preg_replace("/^.*:/","",$line);
137                   $this->disp_header .= "\n<td style='background-color:#EEEEEE;'>".$tmp0."</td><td>".$tmp1."</td>"; 
138                   $this->disp_header .= "\n</tr>";
139                 }              
140                 $this->disp_header .= "\n</table>";
141               }
142             }
143           }
144         }
145       }        
148       /* Back is posted from the header display page */
149       if(isset($_POST['back'])){
150         $this->disp_header = false;
151       }
153       /* If there is a header in disp_header, then display it */
154       if($this->disp_header){
155         $smarty->assign("header",$this->disp_header);
156         return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
157       }
159       /* tell smarty to display the search results*/
160       $smarty->assign("all_ok"      , "true");
161       
162       /* A single server is selected */
163       if($this->Server != "all"){    
165         /* Create Query cmd */ 
166         $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
167         $q_cmd = preg_replace("/%server/" ,$this->Server,$q_cmd);
168         $q_cmd = preg_replace("/%id/"     ,"all"        ,$q_cmd);
170         if(chkacl($this->acl,"query")!=""){
171           print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),"query"));
172           $mailQueueParser = new parseMailQueue("",$this->Server);
173         }else{
175           /* Only display this if the query cmd is executeable */
176           if($str = @$this->pass_cmd ($q_cmd)){
177             /* Parse returned data */
178             $mailQueueParser = new parseMailQueue($str,$this->Server);
179           }else{
180             /* On error/ no return value / false return value */
181             print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
182             $error = true;
183           }
184         }
185       }else{
186         $mailQueueParser = NULL;
187         foreach($this->getServer() as $ServerID=>$ServerName){
189           /* Don't query the server named all :) */
190           if($ServerID == "all") continue;
192           /* Prepare query cmd */
193           $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
194           $q_cmd = preg_replace("/%server/" ,$ServerName  ,$q_cmd);
195           $q_cmd = preg_replace("/%id/"     ,"ALL"           ,$q_cmd);
197           if(chkacl($this->acl,"query")!=""){
198             print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),"query"));
199             $mailQueueParser = new parseMailQueue("",$this->Server);
200           }else{
202             /* Shell exec this cmd */
203             if($str = @$this->pass_cmd ($q_cmd)){
205               /* If there is no parser available, create one  */
206               if($mailQueueParser == NULL){
207                 $mailQueueParser = new parseMailQueue($str,$ServerID);
208               }else{
209                 $mailQueueParser->parseAdditionalQueue($str,$ServerID);
210               }
211               /* On error/ no return value / false return value */
212             }else{
213               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
214               $error = true;
215             }
216           }
217         }
218       }     
220       /* Check for existing servers 
221           objectClass=goMailServer is required at least for one server. 
222          Else display error   */
223       $server = $this->getServer();
224       if((count($server) == 1 ) && (isset($server['all']))){
225         print_red(_("There are no mail server specified."));
226         $error = true;
227       }
229       if(!$error){
231         /* Filter data with the given */
232         $mailQueueParser->OrderBy($this->OrderBy,$this->SortType);
233         $mailQueueParser->OnlyDaysAgo($this->Time);
234         $mailQueueParser->CreateDate();
235      
236         if($this->Stat == "hold"){
237           $mailQueueParser->Search(true,array("Hold"),true);
238         }
239         if($this->Stat == "unhold"){
240           $mailQueueParser->Search(false,array("Hold"),true);
241         }
242         if($this->Stat == "active"){
243           $mailQueueParser->Search(true,array("Active"),true);
244         }
245         if($this->Stat == "nonactive"){
246           $mailQueueParser->Search(false,array("Active"),true);
247         }
249         $mailQueueParser->Search($this->Search,array("MailID","Size","Sender","Recipient","Error","Arrival"));
251         /* */
252         $entries = $mailQueueParser->GetAll();
253   
254         if(count($entries) ==0 ){
255           $smarty->assign("all_ok",false);
256         }
257  
258         $smarty->assign("entries"       , array_slice($entries,$this->Page,$this->range));
259         $smarty->assign("plug"          , "?plug=".$_GET['plug']);
260         $smarty->assign("r_stats"       , $this->getStats());
261         $smarty->assign("stats"         , array_flip($this->getStats()));
262         $smarty->assign("stat"          , $this->Stat);
263         $smarty->assign("p_server"      , $this->Server);
264         $smarty->assign("p_servers"     , $this->getServer());
265         $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
266         $smarty->assign("p_time"        , $this->Time);
267         $smarty->assign("p_times"       , $this->getTimes());
268         $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
269         $smarty->assign("search_for"    , $this->Search);
270         $smarty->assign("range_selector", range_selector(count($entries), $this->Page, $this->range,"EntriesPerPage")); 
271         $smarty->assign("OrderBy"       , $this->OrderBy);
273         /* Display sort arrow */
274         if($this->SortType == "up"){
275           $smarty->assign("SortType","<img src='images/sort_up.png' alt='"._("up")."' border='0'>");
276         }else{
277           $smarty->assign("SortType","<img src='images/sort_down.png' alt='"._("down")."' border='0'>");
278         }
279       }
280     }
282     /* In case of an error */
283     if($error){  
284       $smarty->assign("all_ok"        , "false");
285       $smarty->assign("r_stats"       , $this->getStats());
286       $smarty->assign("stats"         , array_flip($this->getStats()));
287       $smarty->assign("stat"          , $this->Stat);
288       $smarty->assign("plug"          , "?plug=".$_GET['plug']);
289       $smarty->assign("p_server"      , $this->Server);
290       $smarty->assign("p_servers"     , $this->getServer());
291       $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
292       $smarty->assign("p_time"        , $this->Time);
293       $smarty->assign("p_times"       , $this->getTimes());
294       $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
295       $smarty->assign("search_for"    , $this->Search);
296       $smarty->assign("OrderBy"       , $this->OrderBy);
297     }
298     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
299   }
302   /* return selectable server */
303   function getServer()
304   {
305     $ret= array("all"=>_("All"));
306     $ldap = $this->config->get_ldap_link();
307     $ldap->cd($this->config->current['BASE']);    
308     $ldap->search("(objectClass=goMailServer)",array("cn"));
309     while($tmp = $ldap->fetch()){
310       $ret[$tmp['cn'][0]]= $tmp['cn'][0];
311     }
312     return($ret);
313   }
316   /* Return selectable times*/
317   function getTimes()
318   {
319     $ret = array();
320     $ret['nolimit']=_("no limit"); 
321     foreach(array(1,2,4,8,12,24,36,48) as $i){
322       if($i == 1){
323         $ret[$i] = $i."&nbsp;"._("hour");
324       }else{
325         $ret[$i] = $i."&nbsp;"._("hours");
326       }
327     }
328     return($ret);
329   }
332   /* Save post values*/
333   function save_object($save_current= FALSE)
334   {
335     if(isset($_POST['p_server'])){
336       $this->Server = $_POST['p_server'];
337     }
338     if(isset($_POST['p_time'])){
339       $this->Time = $_POST['p_time'];
340     }
341     if(isset($_POST['search_for'])){
342       $this->Search = $_POST['search_for'];
343     }
344     if(isset($_POST['Stat'])){
345       $this->Stat = $_POST['Stat'];
346     }
347     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
348       $this->Page = $_GET['start'];
349     }
351     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
352       $old = $this->OrderBy;
353       $this->OrderBy = $_GET['sort'];
354       if($this->OrderBy == $old)
355       {
356         if($this->SortType== "up"){
357           $this->SortType = "down";
358         }else{
359           $this->SortType = "up";
360         }
361       }
362     }
364   }
366   /* Return stats */
367   function getStats()
368   {
369     return(array(
370           "all"     =>_("All"),
371           "hold"    =>_("Hold"),
372           "unhold"  =>_("Un hold"),
373           "active"  =>_("Active"),
374           "nonactive"  =>_("Not active")
375           ));
376   }
379 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
380 ?>