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();
67 unset($se['all']);
68 $se_str = "";
69 foreach($se as $server) {
70 $se_str .= $server." ";
71 }
72 }else{
73 /* We have only one server selected */
74 $se_str = $this->Server;
75 }
77 /* Check all post that will effect all entries */
78 $only_once = true;
79 foreach(array("unhold_all","hold_all","del_all","requeue_all") as $attr){
80 foreach($_POST as $name => $value){
81 if((preg_match("/".$attr."/",$name))&&($only_once)){
82 if(chkacl($this->acl,$attr)!=""){
83 $only_once = false;
84 #FIXME Please choose a better error msg, if release is released
85 print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$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'];
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(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$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{
124 /* Special handling for option='header' */
125 if($opt == "header"){
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");
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 /* Only display this if the query cmd is executeable */
171 if($str = @$this->pass_cmd ($q_cmd)){
172 /* Parse returned data */
173 $mailQueueParser = new parseMailQueue($str,$this->Server);
174 }else{
175 /* On error/ no return value / false return value */
176 print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
177 $error = true;
178 }
180 }else{
181 $mailQueueParser = NULL;
182 foreach($this->getServer() as $ServerID=>$ServerName){
184 /* Don't query the server named all :) */
185 if($ServerID == "all") continue;
187 /* Prepare query cmd */
188 $q_cmd = preg_replace("/%action/" ,"query" ,$this->mailQueueScript);
189 $q_cmd = preg_replace("/%server/" ,$ServerName ,$q_cmd);
190 $q_cmd = preg_replace("/%id/" ,"ALL" ,$q_cmd);
192 /* Shell exec this cmd */
193 if($str = @$this->pass_cmd ($q_cmd)){
195 /* If there is no parser available, create one */
196 if($mailQueueParser == NULL){
197 $mailQueueParser = new parseMailQueue($str,$ServerID);
198 }else{
199 $mailQueueParser->parseAdditionalQueue($str,$ServerID);
200 }
201 /* On error/ no return value / false return value */
202 }else{
203 print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
204 $error = true;
205 }
206 }
208 }
210 if(!$error){
212 /* Filter data with the given */
213 $mailQueueParser->OrderBy($this->OrderBy,$this->SortType);
214 $mailQueueParser->OnlyDaysAgo($this->Time);
215 $mailQueueParser->CreateDate();
217 if($this->Stat == "hold"){
218 $mailQueueParser->Search(true,array("Hold"),true);
219 }
220 if($this->Stat == "unhold"){
221 $mailQueueParser->Search(false,array("Hold"),true);
222 }
223 if($this->Stat == "active"){
224 $mailQueueParser->Search(true,array("Active"),true);
225 }
226 if($this->Stat == "nonactive"){
227 $mailQueueParser->Search(false,array("Active"),true);
228 }
230 $mailQueueParser->Search($this->Search,array("MailID","Size","Sender","Recipient","Error","Arrival"));
232 /* */
233 $entries = $mailQueueParser->GetAll();
235 if(count($entries) ==0 ){
236 $smarty->assign("all_ok",false);
237 }
239 $smarty->assign("entries" , array_slice($entries,$this->Page,$this->range));
240 $smarty->assign("plug" , "?plug=".$_GET['plug']);
241 $smarty->assign("r_stats" , $this->getStats());
242 $smarty->assign("stats" , array_flip($this->getStats()));
243 $smarty->assign("stat" , $this->Stat);
244 $smarty->assign("p_server" , $this->Server);
245 $smarty->assign("p_servers" , $this->getServer());
246 $smarty->assign("p_serverKeys" , array_flip($this->getServer()));
247 $smarty->assign("p_time" , $this->Time);
248 $smarty->assign("p_times" , $this->getTimes());
249 $smarty->assign("p_timeKeys" , array_flip($this->getTimes()));
250 $smarty->assign("search_for" , $this->Search);
251 $smarty->assign("range_selector", range_selector(count($entries), $this->Page, $this->range,"EntriesPerPage"));
252 $smarty->assign("OrderBy" , $this->OrderBy);
254 /* Display sort arrow */
255 if($this->SortType == "up"){
256 $smarty->assign("SortType","<img src='images/sort_up.png' alt='"._("up")."' border='0'>");
257 }else{
258 $smarty->assign("SortType","<img src='images/sort_down.png' alt='"._("down")."' border='0'>");
259 }
260 }
261 }
263 /* In case of an error */
264 if($error){
265 $smarty->assign("all_ok" , "false");
266 $smarty->assign("r_stats" , $this->getStats());
267 $smarty->assign("stats" , array_flip($this->getStats()));
268 $smarty->assign("stat" , $this->Stat);
269 $smarty->assign("plug" , "?plug=".$_GET['plug']);
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("OrderBy" , $this->OrderBy);
278 }
279 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
280 }
283 /* return selectable server */
284 function getServer()
285 {
286 $ret= array("all"=>_("All"));
287 $ldap = $this->config->get_ldap_link();
288 $ldap->cd($this->config->current['BASE']);
289 $ldap->search("(objectClass=goMailServer)",array("cn"));
290 while($tmp = $ldap->fetch()){
291 $ret[$tmp['cn'][0]]= $tmp['cn'][0];
292 }
293 return($ret);
294 }
297 /* Return selectable times*/
298 function getTimes()
299 {
300 $ret = array();
301 $ret['nolimit']=_("no limit");
302 foreach(array(1,2,4,8,12,24,36,48) as $i){
303 if($i == 1){
304 $ret[$i] = $i." "._("hour");
305 }else{
306 $ret[$i] = $i." "._("hours");
307 }
308 }
309 return($ret);
310 }
313 /* Save post values*/
314 function save_object($save_current= FALSE)
315 {
316 if(isset($_POST['p_server'])){
317 $this->Server = $_POST['p_server'];
318 }
319 if(isset($_POST['p_time'])){
320 $this->Time = $_POST['p_time'];
321 }
322 if(isset($_POST['search_for'])){
323 $this->Search = $_POST['search_for'];
324 }
325 if(isset($_POST['Stat'])){
326 $this->Stat = $_POST['Stat'];
327 }
328 if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
329 $this->Page = $_GET['start'];
330 }
332 if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
333 $old = $this->OrderBy;
334 $this->OrderBy = $_GET['sort'];
335 if($this->OrderBy == $old)
336 {
337 if($this->SortType== "up"){
338 $this->SortType = "down";
339 }else{
340 $this->SortType = "up";
341 }
342 }
343 }
345 }
347 /* Return stats */
348 function getStats()
349 {
350 return(array(
351 "all" =>_("All"),
352 "hold" =>_("Hold"),
353 "unhold" =>_("Un hold"),
354 "active" =>_("Active"),
355 "nonactive" =>_("Not active")
356 ));
357 }
359 }
360 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
361 ?>