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