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)
13 {
14 $this->s_dataToParse = $s_data;
15 $this->_parse();
16 }
19 /* Remove all entries which are older than the last x hours
20 */
21 function OnlyDaysAgo($str)
22 {
23 /* Get current time */
24 $cur = time();
26 /* Only perform this filter, if the given parameter is valid */
27 if((is_numeric($str))&&($str != 0)){
29 /* hours are given as parameter */
30 $cur = $cur - ($str*(60*60));
32 /* Remove old entries */
33 foreach($this->a_parsedData as $key => $data){
34 if($data['Arrival'] < $cur){
35 unset($this->a_parsedData[$key]);
36 }
37 }
38 }
39 }
42 /* Only keep entries that contains the $filter
43 * in any of the given $fields
44 */
45 function Search($filter,$fields)
46 {
47 /* Go through all entries */
48 foreach($this->a_parsedData as $key => $data){
50 /* not found yet */
51 $found = false;
53 foreach($fields as $attr){
54 if(preg_match("/".str_replace("*",".*",$filter)."/i",$data[$attr])){
55 $found= true;
56 }
57 }
59 /* if nothing found, delete this entry */
60 if($found == false){
61 unset($this->a_parsedData[$key]);
62 }
63 }
64 }
66 /* Convert date from timestamp to human readable */
67 function CreateDate()
68 {
69 foreach($this->a_parsedData as $key => $data){
70 $this->a_parsedData[$key]['Arrival'] = date("d.m.Y H:i:s",$data['Arrival']);
71 }
72 }
74 /* Order by specified field */
75 function OrderBy($str = "Arrival",$type = "up" )
76 {
77 $tmp = array();
79 /* If the given field is not valid */
80 if(!in_array($str,array("MailID","Size","Sender","Recipient","Arrival","Error"))){
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'];
93 $tmp[$struse]= $data;
94 }
95 }else{
96 foreach($this->a_parsedData as $data){
97 $tmp[strtolower($data[$str]).$data['MailID']]= $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;
107 }
108 }
110 function GetAll()
111 {
112 return($this->a_parsedData);
113 }
115 /* Checks if the given MailID exists */
116 function IDExists($id)
117 {
118 return(((isset($this->a_parsedData[$id]))&&(is_array($this->a_parsedData[$id]))));
119 }
121 /* This function parses the given data
122 * it creates an array with all given queue entries
123 */
124 function _parse()
125 {
126 $i = 0; // Temp var
127 $entries = array(); // Contains an array with the raw data for every single entry
128 $s_tmp = ""; // Buffer
130 $s_mailID = ""; // Queue ID
131 $s_Size = ""; // Mail size
132 $s_Arrival = ""; // Arrival time
133 $s_Sender = ""; // Sender
134 $s_Recipient = ""; // Recipient
135 $s_Error = ""; // Occured error
137 /* Remove header
138 */
139 $this->s_dataToParse = preg_replace("/^.*------\n/","",$this->s_dataToParse);
141 /* Create array with single entries
142 */
143 $entries = split("\n\n",$this->s_dataToParse);
145 /* The last entry in this array is not realy an valid entry, its some kind of status.
146 * It would be something like this : -- 795 Kbytes in 124 Requests.
147 */
148 $this->i_count = (count($entries))-1;
150 for($i = 0 ; $i < $this->i_count; $i ++ ){
152 while(strstr($entries[$i]," ")){
153 $entries[$i] = str_replace(" "," ",$entries[$i]);
154 }
156 $s_buffer = split("\n",preg_replace("/[\\n\\r\\t]/s","\n",$entries[$i]));
158 /* Get mailID */
159 $tmp = split(" ",$s_buffer[0]);
161 /* Get values */
162 $s_mailID = $tmp[0];
163 $s_Size = $tmp[1];
164 $s_Sender = $tmp[6];
166 /* Parse time */
167 $tmp3 = split(":",$tmp[5]);
168 $tmp2 = strtotime($tmp[4]." ".$tmp[3]." ".date("Y"));
169 $s_Arrival= mktime($tmp3[0],$tmp3[1],$tmp3[2],date("d",$tmp2),date("m",$tmp2),date("Y",$tmp2));
171 $s_Error = $s_buffer[1];
172 $s_Recipient = $s_buffer[2];
174 /* Append data */
175 $this->a_parsedData[$s_mailID]['MailID'] = $s_mailID;
176 $this->a_parsedData[$s_mailID]['Size'] = $s_Size;
177 $this->a_parsedData[$s_mailID]['Arrival'] = $s_Arrival;
178 $this->a_parsedData[$s_mailID]['Sender'] = $s_Sender;
179 $this->a_parsedData[$s_mailID]['Recipient'] = $s_Recipient;
180 $this->a_parsedData[$s_mailID]['Error'] = $this->_parseError($s_Error);
181 }
182 return($this->a_parsedData);
183 }
185 /* Parse Error part of the entry */
186 function _parseError($str)
187 {
188 $str = trim(preg_replace("/[()]/","",$str));
189 $tmp2 = split(":",$str);
190 $tmp = array_reverse($tmp2);
191 $err = preg_replace("/#.*$/","",$tmp[0]);
192 $text = preg_replace("/said$/i","",trim($tmp2[0]));
193 return($err);
194 }
196 }
201 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
202 ?>