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();
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,$bool = false)
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(($bool)&&($data[$attr]==$filter)){
56 $found = true;
57 }elseif(preg_match("/".str_replace("*",".*",$filter)."/i",$data[$attr])){
58 $found= true;
59 }
60 }
62 /* if nothing found, delete this entry */
63 if($found == false){
64 unset($this->a_parsedData[$key]);
65 }
66 }
67 }
69 /* Convert date from timestamp to human readable */
70 function CreateDate()
71 {
72 foreach($this->a_parsedData as $key => $data){
73 $this->a_parsedData[$key]['Arrival'] = date("d.m.Y H:i:s",$data['Arrival']);
74 }
75 }
77 /* Order by specified field */
78 function OrderBy($str = "Arrival",$type = "up" )
79 {
80 $tmp = array();
81 /* If the given field is not valid */
82 if(!in_array($str,array("MailID","Size","Sender","Recipient","Arrival","Error","Server"))){
83 return(false);
84 }
86 /* Size need special handling, cause it contains numbers
87 */
88 if($str == "Size"){
89 foreach($this->a_parsedData as $data){
90 $struse = "";
91 for($i = strlen($data['Size']); $i < 10 ; $i++ ){
92 $struse .="0";
93 }
94 $struse .= $data[$str].$data['MailID'].$data['Server'];
95 $tmp[$struse]= $data;
96 }
97 }else{
98 foreach($this->a_parsedData as $data){
99 $tmp[strtolower($data[$str]).$data['MailID']."-".$data['Server']]= $data;
100 }
101 }
102 ksort($tmp);
103 if($type != "up"){
104 $tmp = array_reverse($tmp);
105 }
106 $this->a_parsedData = array();
107 foreach($tmp as $data){
108 $this->a_parsedData[$data['MailID']."-".$data['Server']] = $data;
109 }
110 return(true);
111 }
113 function GetAll()
114 {
115 return($this->a_parsedData);
116 }
118 /* Checks if the given MailID exists */
119 function IDExists($id)
120 {
121 foreach($this->a_parsedData as $entry){
122 if($entry['MailID'] == $id) return(true);
123 }
124 return(false);
125 }
127 function parseAdditionalQueue($str, $server)
128 {
129 $this->_parse($str, $server);
130 }
132 /* This function parses the given data
133 * it creates an array with all given queue entries
134 */
135 function _parse($str, $server)
136 {
137 $i = 0; // Temp var
138 $entries = array(); // Contains an array with the raw data for every single entry
139 $s_tmp = ""; // Buffer
141 $s_mailID = ""; // Queue ID
142 $s_Size = ""; // Mail size
143 $s_Arrival = ""; // Arrival time
144 $s_Sender = ""; // Sender
145 $s_Recipient = ""; // Recipient
146 $s_Error = ""; // Occured error
148 /* Remove header
149 */
150 $this->s_dataToParse = preg_replace("/^.*------\n/","",$str);
152 /* Create array with single entries
153 */
154 $entries = split("\n\n",$this->s_dataToParse);
156 /* The last entry in this array is not realy an valid entry, its some kind of status.
157 * It would be something like this : -- 795 Kbytes in 124 Requests.
158 */
159 $this->i_count = (count($entries))-1;
161 for($i = 0 ; $i < $this->i_count; $i ++ ){
163 while(strstr($entries[$i]," ")){
164 $entries[$i] = str_replace(" "," ",$entries[$i]);
165 }
167 $s_buffer = split("\n",preg_replace("/[\\n\\r\\t]/s","\n",$entries[$i]));
169 /* Get mailID */
170 $tmp = split(" ",$s_buffer[0]);
172 /* Get values */
173 $s_mailID = $tmp[0];
174 $s_Size = $tmp[1];
175 $s_Sender = $tmp[6];
177 /* Parse time */
178 $tmp3 = split(":",$tmp[5]);
179 $tmp2 = strtotime($tmp[4]." ".$tmp[3]." ".date("Y"));
180 $s_Arrival= mktime($tmp3[0],$tmp3[1],$tmp3[2],date("d",$tmp2),date("m",$tmp2),date("Y",$tmp2));
182 $s_Error = $s_buffer[1];
183 $s_Recipient = $s_buffer[2];
185 /*
186 * The message is in the active queue, i.e. the message is
187 selected for delivery.
189 ! The message is in the hold queue, i.e. no further deliv-delivery
190 ery attempt will be made until the mail is taken off
191 hold.
192 */
194 $s_Hold = false;
195 if(preg_match("/\!/",$s_mailID)){
196 $s_mailID = preg_replace("/\!/","",$s_mailID);
197 $s_Hold = "true";
198 }
200 $s_Active = false;
201 if(preg_match("/\*/",$s_mailID)){
202 $s_mailID = preg_replace("/\*/","",$s_mailID);
203 $s_Active = true;
204 }
206 /* Append data */
207 $this->a_parsedData[$s_mailID."-".$server]['Server'] = $server;
208 $this->a_parsedData[$s_mailID."-".$server]['MailID'] = $s_mailID;
209 $this->a_parsedData[$s_mailID."-".$server]['Size'] = $s_Size;
210 $this->a_parsedData[$s_mailID."-".$server]['Arrival'] = $s_Arrival;
211 $this->a_parsedData[$s_mailID."-".$server]['Sender'] = $s_Sender;
212 $this->a_parsedData[$s_mailID."-".$server]['Recipient'] = $s_Recipient;
213 $this->a_parsedData[$s_mailID."-".$server]['Hold'] = $s_Hold;
214 $this->a_parsedData[$s_mailID."-".$server]['Active'] = $s_Active;
215 $this->a_parsedData[$s_mailID."-".$server]['Error'] = $this->_parseError($s_Error);
216 }
217 return($this->a_parsedData);
218 }
220 /* Parse Error part of the entry */
221 function _parseError($str)
222 {
223 $str = trim(preg_replace("/[()]/","",$str));
224 $tmp2 = split(":",$str);
225 $tmp = array_reverse($tmp2);
226 $err = preg_replace("/#.*$/","",$tmp[0]);
227 $text = preg_replace("/said$/i","",trim($tmp2[0]));
228 return($err);
229 }
231 }
236 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
237 ?>