Code

mailQueue is now able to handle more than one server
[gosa.git] / plugins / addons / mailqueue / class_parseMailQueue.inc
1 <?php
3 class parseMailQueue
4 {
5         var $s_dataToParse;
6         var $a_parsedData;
7   var $i_count; 
9   /* Contructor
10    * $s_data specifies the data that shuold be parse
11    */
12         function parseMailQueue($s_data,$server)
13         {
14     $this->s_dataToParse = $s_data;             
15           $this->a_parsedData = array();
16     $this->_parse($s_data,$server);
17   }
20   /* Remove all entries which are older than the last x hours
21    */
22   function OnlyDaysAgo($str)
23   {
24     /* Get current time */
25     $cur = time();
26   
27     /* Only perform this filter, if the given parameter is valid */
28     if((is_numeric($str))&&($str != 0)){
30       /* hours are given as parameter */
31       $cur = $cur - ($str*(60*60));
33       /* Remove old entries */
34       foreach($this->a_parsedData as $key => $data){
35         if($data['Arrival'] < $cur){
36           unset($this->a_parsedData[$key]);
37         }
38       }
39     }
40   }
43   /* Only keep entries that contains the $filter
44    * in any of the given $fields
45    */
46   function Search($filter,$fields)
47   {
48     /* Go through all entries */
49     foreach($this->a_parsedData as $key => $data){
51       /* not found yet */
52       $found = false;
54       foreach($fields as $attr){
55         if(preg_match("/".str_replace("*",".*",$filter)."/i",$data[$attr])){
56           $found= true;
57         }
58       }
59   
60       /* if nothing found, delete this entry */
61       if($found == false){
62         unset($this->a_parsedData[$key]);
63       }
64     }
65   }
67   /* Convert date from timestamp to human readable */
68   function CreateDate()
69   {
70     foreach($this->a_parsedData as $key => $data){
71       $this->a_parsedData[$key]['Arrival'] = date("d.m.Y H:i:s",$data['Arrival']);
72     }
73   }
75   /* Order by specified field */
76   function OrderBy($str = "Arrival",$type = "up" )
77   {
78     $tmp = array();
79     /* If the given field is not valid */
80     if(!in_array($str,array("MailID","Size","Sender","Recipient","Arrival","Error","Server"))){
81       return(false);
82     }
84     /* Size need special handling, cause it contains numbers 
85      */
86     if($str == "Size"){
87       foreach($this->a_parsedData as $data){
88         $struse = "";
89         for($i = strlen($data['Size']); $i < 10 ; $i++  ){
90           $struse .="0";
91         }
92         $struse .= $data[$str].$data['MailID'].$data['Server'];
93         $tmp[$struse]= $data;
94       }
95     }else{
96       foreach($this->a_parsedData as $data){
97         $tmp[strtolower($data[$str]).$data['MailID']."-".$data['Server']]= $data;
98       }
99     } 
100     ksort($tmp);
101     if($type != "up"){
102       $tmp = array_reverse($tmp);
103     }
104     $this->a_parsedData = array();
105     foreach($tmp as $data){
106       $this->a_parsedData[$data['MailID']."-".$data['Server']] = $data;
107     }
108     return(true);
109   }
110   
111   function GetAll()
112   {
113     return($this->a_parsedData);
114   }
116   /* Checks if the given MailID exists */
117   function IDExists($id)
118   {
119     return(((isset($this->a_parsedData[$id]))&&(is_array($this->a_parsedData[$id]))));
120   }
122   function parseAdditionalQueue($str, $server)
123   {
124     $this->_parse($str, $server);
125   }
127   /* This function parses the given data 
128    * it creates an array with all given queue entries
129    */
130   function _parse($str, $server)
131   {
132     $i              =  0;       // Temp var 
133     $entries        = array();  // Contains an array with the raw data for every single entry
134     $s_tmp          = "";       // Buffer
136     $s_mailID       = "";       // Queue ID 
137     $s_Size         = "";       // Mail size 
138     $s_Arrival      = "";       // Arrival time
139     $s_Sender       = "";       // Sender
140     $s_Recipient    = "";       // Recipient 
141     $s_Error        = "";       // Occured error
143     /* Remove header
144      */
145     $this->s_dataToParse = preg_replace("/^.*------\n/","",$str);
147     /* Create array with single entries
148      */
149     $entries = split("\n\n",$this->s_dataToParse);
150   
151     /* The last entry in this array is not realy an valid entry, its some kind of status.
152      * It would be something like this : -- 795 Kbytes in 124 Requests.
153      */
154     $this->i_count = (count($entries))-1;
155   
156     for($i = 0 ; $i < $this->i_count; $i ++ ){
157     
158       while(strstr($entries[$i],"  ")){
159         $entries[$i] = str_replace("  "," ",$entries[$i]);  
160       } 
161     
162       $s_buffer = split("\n",preg_replace("/[\\n\\r\\t]/s","\n",$entries[$i]));
163         
164       /* Get mailID */
165       $tmp = split(" ",$s_buffer[0]);
167       /* Get values */
168       $s_mailID   = $tmp[0];
169       $s_Size     = $tmp[1];
170       $s_Sender   = $tmp[6];
172       /* Parse time */
173       $tmp3 = split(":",$tmp[5]);
174       $tmp2 = strtotime($tmp[4]." ".$tmp[3]." ".date("Y"));
175       $s_Arrival= mktime($tmp3[0],$tmp3[1],$tmp3[2],date("d",$tmp2),date("m",$tmp2),date("Y",$tmp2));
177       $s_Error      = $s_buffer[1];
178       $s_Recipient  = $s_buffer[2];
180       /* Append data */
181       $this->a_parsedData[$s_mailID."-".$server]['Server']     = $server; 
182       $this->a_parsedData[$s_mailID."-".$server]['MailID']     = $s_mailID; 
183       $this->a_parsedData[$s_mailID."-".$server]['Size']       = $s_Size; 
184       $this->a_parsedData[$s_mailID."-".$server]['Arrival']    = $s_Arrival; 
185       $this->a_parsedData[$s_mailID."-".$server]['Sender']     = $s_Sender; 
186       $this->a_parsedData[$s_mailID."-".$server]['Recipient']  = $s_Recipient; 
187       $this->a_parsedData[$s_mailID."-".$server]['Error']      = $this->_parseError($s_Error); 
188     }
189     return($this->a_parsedData);
190   }
192   /* Parse Error part of the entry */
193   function _parseError($str)
194   {
195     $str   = trim(preg_replace("/[()]/","",$str));
196     $tmp2 = split(":",$str);
197     $tmp = array_reverse($tmp2);
198     $err  = preg_replace("/#.*$/","",$tmp[0]);
199     $text = preg_replace("/said$/i","",trim($tmp2[0])); 
200     return($err);
201   }
208 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
209 ?>