Subversion Repositories Applications.papyrus

Rev

Rev 1087 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1087 Rev 1253
1
<?php
1
<?php
2
// $Id: ezmlm-parser.php,v 1.2 2005-09-27 16:43:08 alexandre_tb Exp $
2
// $Id: ezmlm-parser.php,v 1.2.4.1 2007-03-12 11:22:12 alexandre_tb Exp $
3
//
3
//
4
// ezmlm-parser.php - ezmlm-php v2.0
4
// ezmlm-parser.php - ezmlm-php v2.0
5
// --------------------------------------------------------------
5
// --------------------------------------------------------------
6
// Contains all the code for parsing messages.
6
// Contains all the code for parsing messages.
7
// It handles all the nessesary decoding, attachments, etc...
7
// It handles all the nessesary decoding, attachments, etc...
8
// Note this does all the parsing itself now removing the dependancy
8
// Note this does all the parsing itself now removing the dependancy
9
// on the mailparse library (as it looks like it will never make
9
// on the mailparse library (as it looks like it will never make
10
// it into the official inclusion with PHP)...
10
// it into the official inclusion with PHP)...
11
// --------------------------------------------------------------
11
// --------------------------------------------------------------
12
 
12
 
13
require_once("ezmlm.php");
13
require_once("ezmlm.php");
14
require_once("Mail/mimeDecode.php") ;
14
require_once("Mail/mimeDecode.php") ;
15
// CLASS: ezmlm-parser
15
// CLASS: ezmlm-parser
16
class ezmlm_parser extends ezmlm_php {
16
class ezmlm_parser extends ezmlm_php {
17
        var $headers;           // the full untouched headers of the message
17
    var $headers;           // the full untouched headers of the message
18
        var $body;              // the full untouched (but decoded) body (this is not $this->parts[0]->body)
18
    var $body;              // the full untouched (but decoded) body (this is not $this->parts[0]->body)
19
	var $parts;		// all the parts, if it is a multipart message. each part is an ezmlm_parser object...
19
	var $parts;		// all the parts, if it is a multipart message. each part is an ezmlm_parser object...
20
 
20
 
21
        // Here's the most accessed headers, everything else can be
21
    // Here's the most accessed headers, everything else can be
22
        // accessed from the $this->headers array.
22
    // accessed from the $this->headers array.
23
        var $to;                // To:
23
    var $to;                // To:
24
        var $from;              // From:
24
    var $from;              // From:
25
        var $date;              // Date:
25
    var $date;              // Date:
26
        var $subject;           // Subject:
26
    var $subject;           // Subject:
27
        var $replyto;           // Reply-To:
27
    var $replyto;           // Reply-To:
28
        var $contenttype;       // Content-Type:
28
    var $contenttype;       // Content-Type:
29
 
29
 
30
	var $multipart;		// TRUE if the message is a multipart message
30
	var $multipart;		// TRUE if the message is a multipart message
31
 
31
 
32
	var $msgfile;		// if parsed from a file, this is the filename...
32
	var $msgfile;		// if parsed from a file, this is the filename...
33
 
33
 
34
	// functions
34
	// functions
35
 
35
 
36
	// recent_msgs - parses and returns an arbitrary number of the most recent messages
36
	// recent_msgs - parses and returns an arbitrary number of the most recent messages
37
	function recent_msgs($show = 20, $month = "") {
37
	function recent_msgs($show = 20, $month = "") {
38
		if ($month == "") { $month = date("Ym"); }
-
 
39
		$threadyear = substr($month,0,4);
-
 
40
		$threadmonth = substr($month,4,2);
-
 
41
 
-
 
42
		if (!is_file($this->listdir . "/archive/threads/" . $month)) {
-
 
43
			if ($threadmonth == '01') { $prevthread = ($threadyear - 1) . "12"; }
-
 
44
			else if ($threadmonth >= 11) { $prevthread = $threadyear . ($threadmonth - 1); }
-
 
45
			else { $prevthread = $threadyear . "0" . ($threadmonth - 1); }
-
 
46
			return $this->recent_msgs($show,$prevthread);
-
 
47
		}
-
 
48
        // on ouvre les fichiers de threads du dernier mois
-
 
49
		$fd = fopen($this->listdir . "/archive/threads/" . $month, "r");
-
 
50
		fseek($fd,-256,SEEK_END);
-
 
51
        
-
 
52
        // on récupère la dernière ligne
-
 
53
        
-
 
54
		while (!feof($fd)) {
-
 
55
			$temp = fgets($fd,4096);
-
 
56
			if ($temp != "") { $curthread = $temp; }
-
 
57
		}
-
 
58
        $nombre_message = 0 ;
-
 
59
        fseek ($fd, 0) ;
-
 
60
        while (!feof($fd)) {
-
 
61
            $nombre_message++;
-
 
62
			fgets($fd);
-
 
63
		}
-
 
64
		fclose($fd);
-
 
65
        ///echo "<br />".$curthread."<br />" ;
-
 
66
		$subjectfile = preg_replace("/^[0-9]*\:([a-z]*) \[.*/", "\\1", $curthread);
-
 
67
		$subjectfile = substr($subjectfile,0,2) . "/" . substr($subjectfile,2,18); // on ne garde que les 2 1ère lettre du hash, slash et le reste du hash
-
 
68
        
-
 
69
        // on ouvre le fichier des sujets
-
 
70
        // présenté comme suit :
-
 
71
        // hash sujet originel   (sur la première ligne)
-
 
72
        // num_message:annéemois:hash_auteur Nom Auteur
-
 
73
		$fd = fopen($this->listdir . "/archive/subjects/" . $subjectfile, "r");
-
 
74
		fseek($fd,-512,SEEK_END);
-
 
75
        
-
 
76
        // on prend la dernière ligne
-
 
77
		while (!feof($fd)) {
-
 
78
			$temp = fgets($fd,4096);
-
 
79
			if ($temp != "") { $cursubject = $temp; }
-
 
80
		}
-
 
81
		fclose($fd);
-
 
82
	
-
 
83
		list($msgnum,$fromthread,$authorid) = split(":",$cursubject);
-
 
84
		$msgdir = (int)($msgnum / 100); // on reconstruit le répertoire du message en divisant son numéro par 100
-
 
85
		
38
		
86
		$numshown = 0;
39
		$repertoire_archive = opendir($this->listdir . "/archive/");
87
        
40
 
88
		$msgfiles = array();
41
		$repertoire_message = array() ;
89
        // on boucle 100 fois
42
		$dernier_repertoire = 0 ;
90
		for ($i = 0; $i <= 99; $i++) {
43
		while (false !== ($item = readdir($repertoire_archive))) {
91
			if (($msgdir == 0) and ($i == 0)) { $i++; };
44
			// $item contient les noms des repertoires
-
 
45
			// on ne garde que ceux qui sont des chiffres
92
			if ($i < 10) { $msgfile = "0" . $i; }
46
 
-
 
47
			if (preg_match('/[0-9]+/', $item)) {
93
			else { $msgfile = $i; }
48
				// on ouvre le fichier d index de chaque repertoire
94
			if (!is_file($this->listdir . "/archive/" . $msgdir . "/" . $msgfile)) { break; }
49
				if ((int) $item > $dernier_repertoire) $dernier_repertoire = (int) $item;
-
 
50
			
95
            
51
			}
-
 
52
		}
-
 
53
		$tableau_message = array() ;
96
		}
54
		$compteur_message = 0 ;
97
        if ($show == '') $show = $nombre_message ;  // Si aucun paramètre n'est passé on renvoie tous les fichiers du mois
55
		$fichier_index = fopen ($this->listdir.'/archive/'.$dernier_repertoire.'/index', 'r') ;
-
 
56
		while (!feof($fichier_index)) {
98
		while ($numshown < $show) {
57
				// Recuperation du numero de message, du hash du sujet et du sujet
-
 
58
				$temp = fgets($fichier_index, 4096);
-
 
59
				preg_match('/([0-9]+): ([a-z]+) (.*)/', $temp, $match) ;
-
 
60
				
99
			$i--;
61
				// dans la seconde on recupere la date, hash auteur et auteur
-
 
62
				$temp = fgets($fichier_index, 4096);
100
			if ($i < 0) {
63
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9]) ([^;]+);(.*) (.*)/', $temp, $match_deuxieme_ligne) ;
-
 
64
				if ($match[1] != '') {
-
 
65
				$tableau_message[$match[1]] = array ($match[2], $match[3], 
101
				$i = 99;
66
									$match_deuxieme_ligne[1].' '.$match_deuxieme_ligne[2].' '.$match_deuxieme_ligne[3], 
102
				$msgdir--;
67
									$match_deuxieme_ligne[5], 
-
 
68
									$match_deuxieme_ligne[6]);
103
				if ($msgdir < 0) { break; }
69
				}
104
			}
70
			}
-
 
71
			fclose ($fichier_index);
-
 
72
		// on renverse le tableau pour afficher les derniers messages en premier
-
 
73
		$tableau_message = array_reverse($tableau_message, true);
-
 
74
		
-
 
75
		// On compte le nombre de message, s il est inferieur $show et que l on est
-
 
76
		// pas dans le premier index, on ouvre le fichier precedent et recupere
-
 
77
		// le n dernier message
-
 
78
		
105
			if ($i < 10) {
79
		if (count ($tableau_message) < $show && $dernier_repertoire != '0') {
-
 
80
			$avant_dernier_repertoire = $dernier_repertoire - 1 ;
-
 
81
			// On utilise file_get_contents pour renverser le fichier
106
				$msgfile = $this->listdir . "/archive/" . $msgdir . "/0" . $i;
82
			$fichier_index = array_reverse(
-
 
83
									explode ("\n", 
107
			} else {
84
										preg_replace ('/\n$/', '', 
-
 
85
											file_get_contents ($this->listdir.'/archive/'.$avant_dernier_repertoire.'/index')) ), true) ;
-
 
86
			reset ($fichier_index);
-
 
87
			//var_dump ($fichier_index);
-
 
88
			
-
 
89
			for ($i = count ($tableau_message); $i <= $show; $i++) {
-
 
90
				// Recuperation du numero de message, du hash du sujet et du sujet
-
 
91
				// dans la seconde on recupere la date, hash auteur et auteur
-
 
92
 
-
 
93
				preg_match('/\t([0-9]+) ([a-zA-Z][a-zA-Z][a-zA-Z]) ([0-9][0-9][0-9][0-9]) ([^;]+);(.*) (.*)/', 
-
 
94
									current ($fichier_index), $match_deuxieme_ligne) ;
-
 
95
				preg_match('/([0-9]+): ([a-z]+) (.*)/', next($fichier_index), $match) ;
-
 
96
				next ($fichier_index);
-
 
97
				
-
 
98
				if ($match[1] != '') {
-
 
99
				$tableau_message[$match[1]] = array ($match[2], $match[3], 
-
 
100
									$match_deuxieme_ligne[1].' '.$match_deuxieme_ligne[2].' '.$match_deuxieme_ligne[3], 
-
 
101
									$match_deuxieme_ligne[5], 
-
 
102
									$match_deuxieme_ligne[6]);
108
				$msgfile = $this->listdir . "/archive/" . $msgdir . "/" . $i;
103
				}
109
			}
-
 
110
            /*
-
 
111
			$msg = new ezmlm_parser();
-
 
112
			$msg->parse_file($msgfile);
-
 
113
            */
-
 
114
            if (!is_file($msgfile)) {
-
 
115
                if (is_file($this->listdir . "/" . $msgfile)) {
-
 
116
                    $msgfile = $this->listdir . "/" . $msgfile; 
-
 
117
                } else if (is_file($this->listdir . "/archive/" . $msgfile)) { 
-
 
118
                    $msgfile = $this->listdir . "/archive/" . $msgfile; 
-
 
119
                } else { 
-
 
120
                    return $msgfiles; 
-
 
121
                }
-
 
122
            }
-
 
123
            $message = file_get_contents($msgfile) ;
-
 
124
            $mimeDecode = new Mail_mimeDecode($message) ;
-
 
125
            $mailDecode = $mimeDecode->decode() ;
-
 
126
            $mailDecode->msgfile = $msgfile ;
-
 
127
            $mailDecode->nummessage = $msgdir.$i ;
-
 
128
			$msgfiles[] = $mailDecode ;
-
 
129
            
-
 
130
			unset($mailDecode);
-
 
131
			$numshown++;
104
			}
-
 
105
		}
132
		}
106
			
133
        
107
		
134
		return $msgfiles;
108
		return ($tableau_message) ;
135
	}
109
	}
136
 
110
 
137
 
111
 
138
	// parse_file - opens a file and feeds the data to parse, file can be relative to the listdir
112
	// parse_file - opens a file and feeds the data to parse, file can be relative to the listdir
139
	function parse_file($file,$simple = FALSE) {
113
	function parse_file($file,$simple = FALSE) {
140
		if (!is_file($file)) {
114
		if (!is_file($file)) {
141
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
115
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
142
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
116
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
143
			else { return FALSE; }
117
			else { return FALSE; }
144
		}
118
		}
145
 
119
 
146
		$this->msgfile = $file;
120
		$this->msgfile = $file;
147
        $data = '' ;
121
        $data = '' ;
148
		$fd = fopen($file, "r");
122
		$fd = fopen($file, "r");
149
		while (!feof($fd)) { $data .= fgets($fd,4096); }
123
		while (!feof($fd)) { $data .= fgets($fd,4096); }
150
		fclose($fd);
124
		fclose($fd);
151
		return $this->parse($data,$simple);
125
		return $this->parse($data,$simple);
152
	}
126
	}
153
 
127
 
154
    // parse_file_headers - ouvre un fichier et analyse les entêtes
128
    // parse_file_headers - ouvre un fichier et analyse les entêtes
155
	function parse_file_headers($file,$simple = FALSE) {
129
	function parse_file_headers($file,$simple = FALSE) {
156
		if (!is_file($file)) {
130
		if (!is_file($file)) {
157
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
131
			if (is_file($this->listdir . "/" . $file)) { $file = $this->listdir . "/" . $file; }
158
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
132
			else if (is_file($this->listdir . "/archive/" . $file)) { $file = $this->listdir . "/archive/" . $file; }
159
			else { return FALSE; }
133
			else { return FALSE; }
160
		}
134
		}
161
 
135
 
162
		$this->msgfile = $file;
136
		$this->msgfile = $file;
163
        $data = file_get_contents ($file) ;
137
        $data = file_get_contents ($file) ;
164
        $message = file_get_contents($file) ;
138
        $message = file_get_contents($file) ;
165
        $mimeDecode = new Mail_mimeDecode($message) ;
139
        $mimeDecode = new Mail_mimeDecode($message) ;
166
        $mailDecode = $mimeDecode->decode() ;
140
        $mailDecode = $mimeDecode->decode() ;
167
        return $mailDecode ;
141
        return $mailDecode ;
168
		/*$fd = fopen($file, "r");
142
		/*$fd = fopen($file, "r");
169
		while (!feof($fd)) { $data .= fgets($fd,4096); }
143
		while (!feof($fd)) { $data .= fgets($fd,4096); }
170
		fclose($fd);*/
144
		fclose($fd);*/
171
        if ($this->_get_headers($data, $simple)) return true ;
145
        if ($this->_get_headers($data, $simple)) return true ;
172
		return false ;
146
		return false ;
173
	}
147
	}
174
 
148
 
175
	// this does all of the work (well it calls two functions that do all the work :)
149
	// this does all of the work (well it calls two functions that do all the work :)
176
	// all the decoding a part breaking follows RFC2045 (http://www.faqs.org/rfcs/rfc2045.html)
150
	// all the decoding a part breaking follows RFC2045 (http://www.faqs.org/rfcs/rfc2045.html)
177
	function parse($data,$simple = FALSE) {
151
	function parse($data,$simple = FALSE) {
178
        
152
        
179
		if (($this->_get_headers($data,$simple)) && $this->_get_body($data,$simple)) { return TRUE; }
153
		if (($this->_get_headers($data,$simple)) && $this->_get_body($data,$simple)) { return TRUE; }
180
		return FALSE;
154
		return FALSE;
181
	}
155
	}
182
 
156
 
183
	// all of these are internal functions, you shouldn't call them directly...
157
	// all of these are internal functions, you shouldn't call them directly...
184
 
158
 
185
	// _ct_parse: parse Content-Type headers -> $ct[0] = Full header, $ct[1] = Content-Type, $ct[2] ... $ct[n] = AP's
159
	// _ct_parse: parse Content-Type headers -> $ct[0] = Full header, $ct[1] = Content-Type, $ct[2] ... $ct[n] = AP's
186
	function _ct_parse() {
160
	function _ct_parse() {
187
		$instr = $this->headers['content-type'];
161
		$instr = $this->headers['content-type'];
188
		preg_replace('/\(.*\)/','',$instr); // strip rfc822 comments
162
		preg_replace('/\(.*\)/','',$instr); // strip rfc822 comments
189
		if (preg_match('/: /', $instr)) {
163
		if (preg_match('/: /', $instr)) {
190
			$ct = preg_split('/:/',trim($instr),2);
164
			$ct = preg_split('/:/',trim($instr),2);
191
			$ct = preg_split('/;/',trim($ct[1]));
165
			$ct = preg_split('/;/',trim($ct[1]));
192
		} else {
166
		} else {
193
			$ct = preg_split('/;/',trim($instr));
167
			$ct = preg_split('/;/',trim($instr));
194
		}
168
		}
195
		if (isset($ct[1])) $attrs = preg_split('/[\s\n]/',$ct[1]);
169
		if (isset($ct[1])) $attrs = preg_split('/[\s\n]/',$ct[1]);
196
		$i = 2;
170
		$i = 2;
197
		$ct[1] = $ct[0];
171
		$ct[1] = $ct[0];
198
		$ct[0] = $this->headers['content-type'];
172
		$ct[0] = $this->headers['content-type'];
199
        if (isset($attrs) && is_array($attrs)) {
173
        if (isset($attrs) && is_array($attrs)) {
200
            while (list($key, $val) = each($attrs)) {
174
            while (list($key, $val) = each($attrs)) {
201
                if ($val == '') continue;
175
                if ($val == '') continue;
202
                $ap = preg_split('/=/',$val,2);
176
                $ap = preg_split('/=/',$val,2);
203
                if (preg_match('/^"/',$ap[1])) { $ap[1] = substr($ap[1],1,strlen($ap[1])-2); }
177
                if (preg_match('/^"/',$ap[1])) { $ap[1] = substr($ap[1],1,strlen($ap[1])-2); }
204
                $ct[$i] = $ap;
178
                $ct[$i] = $ap;
205
                $i++;
179
                $i++;
206
            }
180
            }
207
        }
181
        }
208
		// are we a multipart message?
182
		// are we a multipart message?
209
		if (preg_match('/^multipart/i', $ct[1])) { $this->multipart = TRUE; }
183
		if (preg_match('/^multipart/i', $ct[1])) { $this->multipart = TRUE; }
210
 
184
 
211
		return $ct;
185
		return $ct;
212
	}
186
	}
213
 
187
 
214
	// _get_headers: pulls the headers out of the data and builds the $this->headers array
188
	// _get_headers: pulls the headers out of the data and builds the $this->headers array
215
	function _get_headers($data,$simple = FALSE) {
189
	function _get_headers($data,$simple = FALSE) {
216
		$lines = preg_split('/\n/', $data);
190
		$lines = preg_split('/\n/', $data);
217
		while (list($key, $val) = each($lines)) {
191
		while (list($key, $val) = each($lines)) {
218
			$val = trim($val);
192
			$val = trim($val);
219
			if ($val == "") break;
193
			if ($val == "") break;
220
			if (preg_match('/^From[^:].*$/', $val)) continue;	/* strips out any From lines added by the MTA */
194
			if (preg_match('/^From[^:].*$/', $val)) continue;	/* strips out any From lines added by the MTA */
221
 
195
 
222
			$hdr = preg_split('/: /', $val, 2);
196
			$hdr = preg_split('/: /', $val, 2);
223
			if (count($hdr) == 1) {
197
			if (count($hdr) == 1) {
224
				// this is a continuation of the last header (like a recieved from line)
198
				// this is a continuation of the last header (like a recieved from line)
225
				$this->headers[$last] .= $val;
199
				$this->headers[$last] .= $val;
226
			} else {
200
			} else {
227
				$this->headers[strtolower($hdr[0])] = $hdr[1];
201
				$this->headers[strtolower($hdr[0])] = $hdr[1];
228
                //echo htmlspecialchars($this->headers['from'])."<br />" ;
202
                //echo htmlspecialchars($this->headers['from'])."<br />" ;
229
				$last = strtolower($hdr[0]);
203
				$last = strtolower($hdr[0]);
230
			}
204
			}
231
		}
205
		}
232
        // ajout alex
206
        // ajout alex
233
        // pour supprimer le problème des ISO...
207
        // pour supprimer le problème des ISO...
234
        // a déplacer ailleur, et appelé avant affichage
208
        // a déplacer ailleur, et appelé avant affichage
235
        
209
        
236
        if (preg_match ('/windows-[0-9][0-9][0-9][0-9]/', $this->headers['subject'], $nombre)) {
210
        if (preg_match ('/windows-[0-9][0-9][0-9][0-9]/', $this->headers['subject'], $nombre)) {
237
            $reg_exp = $nombre[0] ;
211
            $reg_exp = $nombre[0] ;
238
        } else {
212
        } else {
239
            $reg_exp = 'ISO-8859-15?' ;
213
            $reg_exp = 'ISO-8859-15?' ;
240
        }
214
        }
241
        if (preg_match ('/UTF/i', $this->headers['subject'])) $reg_exp = 'UTF-8' ;
215
        if (preg_match ('/UTF/i', $this->headers['subject'])) $reg_exp = 'UTF-8' ;
242
        preg_match_all ("/=\?$reg_exp\?(Q|B)\?(.*?)\?=/i", $this->headers['subject'], $match, PREG_PATTERN_ORDER)  ;
216
        preg_match_all ("/=\?$reg_exp\?(Q|B)\?(.*?)\?=/i", $this->headers['subject'], $match, PREG_PATTERN_ORDER)  ;
243
        for ($i = 0; $i < count ($match[0]); $i++ ) {
217
        for ($i = 0; $i < count ($match[0]); $i++ ) {
244
            
218
            
245
                if ($match[1][$i] == 'Q') {
219
                if ($match[1][$i] == 'Q') {
246
                    $decode = quoted_printable_decode ($match[2][$i]) ;
220
                    $decode = quoted_printable_decode ($match[2][$i]) ;
247
                } elseif ($match[1][$i] == 'B') {
221
                } elseif ($match[1][$i] == 'B') {
248
                    $decode = base64_decode ($match[2][$i]) ;
222
                    $decode = base64_decode ($match[2][$i]) ;
249
                }
223
                }
250
                $decode = preg_replace ("/_/", " ", $decode) ;
224
                $decode = preg_replace ("/_/", " ", $decode) ;
251
            if ($reg_exp == 'UTF-8') {
225
            if ($reg_exp == 'UTF-8') {
252
                $decode = utf8_decode ($decode) ;
226
                $decode = utf8_decode ($decode) ;
253
            } 
227
            } 
254
            $this->headers['subject'] = str_replace ($match[0][$i], $decode, $this->headers['subject']) ;
228
            $this->headers['subject'] = str_replace ($match[0][$i], $decode, $this->headers['subject']) ;
255
        }
229
        }
256
		// sanity anyone?
230
		// sanity anyone?
257
		if (!$this->headers['content-type']) { $this->headers['content-type'] = "text/plain; charset=us-ascii"; }
231
		if (!$this->headers['content-type']) { $this->headers['content-type'] = "text/plain; charset=us-ascii"; }
258
		if (!$simple) { $this->headers['content-type'] = $this->_ct_parse(); }
232
		if (!$simple) { $this->headers['content-type'] = $this->_ct_parse(); }
259
        
233
        
260
 
234
 
261
		return TRUE;
235
		return TRUE;
262
	}
236
	}
263
 
237
 
264
	// _get_body: pulls the body out of the data and fills $this->body, decoding the data if nessesary.
238
	// _get_body: pulls the body out of the data and fills $this->body, decoding the data if nessesary.
265
	function _get_body($data,$simple = FALSE) {
239
	function _get_body($data,$simple = FALSE) {
266
		$lines = preg_split('/\n/', $data);
240
		$lines = preg_split('/\n/', $data);
267
		$doneheaders = FALSE;
241
		$doneheaders = FALSE;
268
        
242
        
269
		$data = "";
243
		$data = "";
270
		while (list($key,$val) = each($lines)) {
244
		while (list($key,$val) = each($lines)) {
271
            //echo htmlspecialchars($val)."<br>";
245
            //echo htmlspecialchars($val)."<br>";
272
			if (($val == '') and (!$doneheaders)) {
246
			if (($val == '') and (!$doneheaders)) {
273
				$doneheaders = TRUE;
247
				$doneheaders = TRUE;
274
				continue;
248
				continue;
275
			} else if ($doneheaders) {
249
			} else if ($doneheaders) {
276
				$data .= $val . "\n";
250
				$data .= $val . "\n";
277
			}
251
			}
278
		}
252
		}
279
 
253
 
280
		// now here comes the fun part... decoding.
254
		// now here comes the fun part... decoding.
281
		switch($this->headers['content-transfer-encoding']) {
255
		switch($this->headers['content-transfer-encoding']) {
282
			case 'binary':
256
			case 'binary':
283
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_binary($data)),$simple);
257
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_binary($data)),$simple);
284
				break;
258
				break;
285
 
259
 
286
			case 'base64':
260
			case 'base64':
287
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_base64($data)),$simple);
261
				$this->body = $this->_cte_8bit($this->_cte_qp($this->_cte_base64($data)),$simple);
288
				break;
262
				break;
289
 
263
 
290
			case 'quoted-printable':
264
			case 'quoted-printable':
291
				$this->body = $this->_cte_8bit($this->_cte_qp($data),$simple);
265
				$this->body = $this->_cte_8bit($this->_cte_qp($data),$simple);
292
				break;
266
				break;
293
 
267
 
294
			case '8bit':
268
			case '8bit':
295
				$this->body = $this->_cte_8bit($data,$simple);
269
				$this->body = $this->_cte_8bit($data,$simple);
296
				break;
270
				break;
297
 
271
 
298
			case '7bit':		// 7bit doesn't need to be decoded
272
			case '7bit':		// 7bit doesn't need to be decoded
299
			default:		// And the fall through as well...
273
			default:		// And the fall through as well...
300
				$this->body = $data;
274
				$this->body = $data;
301
				break;
275
				break;
302
		}
276
		}
303
        //echo  $this->headers['content-type'][2][1];
277
        //echo  $this->headers['content-type'][2][1];
304
        if (isset($this->headers['content-type'][2][1]) && $this->headers['content-type'][2][1] == 'UTF-8') {
278
        if (isset($this->headers['content-type'][2][1]) && $this->headers['content-type'][2][1] == 'UTF-8') {
305
                //$this->body = utf8_decode ($this->body) ;
279
                //$this->body = utf8_decode ($this->body) ;
306
                //echo quoted_printable_decode(utf8_decode ($this->body)) ;
280
                //echo quoted_printable_decode(utf8_decode ($this->body)) ;
307
        }
281
        }
308
		if ($simple) { return TRUE; }
282
		if ($simple) { return TRUE; }
309
 
283
 
310
		// if we are a multipart message then break up the parts and decode, set the appropriate variables.
284
		// if we are a multipart message then break up the parts and decode, set the appropriate variables.
311
		// here comes the best part about making ezmlm-php OOP. since each part is just really a little message
285
		// here comes the best part about making ezmlm-php OOP. since each part is just really a little message
312
		// in itself each part becomes a new parser object and all the wheels turn again... :)
286
		// in itself each part becomes a new parser object and all the wheels turn again... :)
313
		if ($this->multipart) {
287
		if ($this->multipart) {
314
            
288
            
315
			$boundary = '';
289
			$boundary = '';
316
			for ($i = 2; $i <= count($this->headers['content-type']); $i++) {
290
			for ($i = 2; $i <= count($this->headers['content-type']); $i++) {
317
				if (preg_match('/boundary/i', $this->headers['content-type'][$i][0])) {
291
				if (preg_match('/boundary/i', $this->headers['content-type'][$i][0])) {
318
					$boundary = $this->headers['content-type'][$i][1];
292
					$boundary = $this->headers['content-type'][$i][1];
319
                    
293
                    
320
				}
294
				}
321
			}
295
			}
322
			if ($boundary != '') {
296
			if ($boundary != '') {
323
				$this->_get_parts($this->body,$boundary);
297
				$this->_get_parts($this->body,$boundary);
324
			} else {
298
			} else {
325
				// whoopps... something's not right here. we were told that the message is supposed
299
				// whoopps... something's not right here. we were told that the message is supposed
326
				// to be a multipart message, yet the boundary wasn't set in the content type.
300
				// to be a multipart message, yet the boundary wasn't set in the content type.
327
				// mark the message as non multipart and add a message to the top of the body.
301
				// mark the message as non multipart and add a message to the top of the body.
328
				$this->multipart = FALSE;
302
				$this->multipart = FALSE;
329
				$this->body = "PARSER ERROR:\nWHILE PARSING THIS MESSAGE AS A MULTIPART MESSAGE AS DEFINED IN RFC2045 THE BOUNDARY IDENTIFIER WAS NOT FOUND!\nTHIS MESSAGE WILL NOT DISPLAY CORRECTLY!\n\n" . $this->body;
303
				$this->body = "PARSER ERROR:\nWHILE PARSING THIS MESSAGE AS A MULTIPART MESSAGE AS DEFINED IN RFC2045 THE BOUNDARY IDENTIFIER WAS NOT FOUND!\nTHIS MESSAGE WILL NOT DISPLAY CORRECTLY!\n\n" . $this->body;
330
			}
304
			}
331
		}
305
		}
332
 
306
 
333
		return TRUE;
307
		return TRUE;
334
	}
308
	}
335
 
309
 
336
	// _get_parts: breaks up $data into parts based on $boundary following the rfc specs
310
	// _get_parts: breaks up $data into parts based on $boundary following the rfc specs
337
	// detailed in section 5 of RFC2046 (http://www.faqs.org/rfcs/rfc2046.html)
311
	// detailed in section 5 of RFC2046 (http://www.faqs.org/rfcs/rfc2046.html)
338
	// After the parts are broken up they are then turned into parser objects and the
312
	// After the parts are broken up they are then turned into parser objects and the
339
	// resulting array of parts is set to $this->parts;
313
	// resulting array of parts is set to $this->parts;
340
	function _get_parts($data,$boundary) {
314
	function _get_parts($data,$boundary) {
341
		$inpart = -1;
315
		$inpart = -1;
342
		$lines = preg_split('/\n/', $data);
316
		$lines = preg_split('/\n/', $data);
343
        // La première partie contient l'avertissement pour les client mail ne supportant pas
317
        // La première partie contient l'avertissement pour les client mail ne supportant pas
344
        // multipart, elle est stocké dans parts[-1]
318
        // multipart, elle est stocké dans parts[-1]
345
		while(list($key,$val) = each($lines)) {
319
		while(list($key,$val) = each($lines)) {
346
			if ($val == "--" . $boundary) { $inpart++; continue; } // start of a part
320
			if ($val == "--" . $boundary) { $inpart++; continue; } // start of a part
347
			else if ($val == "--" . $boundary . "--") { break; } // the end of the last part
321
			else if ($val == "--" . $boundary . "--") { break; } // the end of the last part
348
			else { $parts[$inpart] .= $val . "\n"; }
322
			else { $parts[$inpart] .= $val . "\n"; }
349
		}
323
		}
350
        
324
        
351
		for ($i = 0; $i < count($parts) - 1; $i++) {    // On saute la première partie
325
		for ($i = 0; $i < count($parts) - 1; $i++) {    // On saute la première partie
352
			$part[$i] = new ezmlm_parser();
326
			$part[$i] = new ezmlm_parser();
353
			$part[$i]->parse($parts[$i]);
327
			$part[$i]->parse($parts[$i]);
354
			$this->parts[$i] = $part[$i];
328
			$this->parts[$i] = $part[$i];
355
            //echo $this->parts[$i]."<br>" ;
329
            //echo $this->parts[$i]."<br>" ;
356
		}
330
		}
357
        
331
        
358
	}	
332
	}	
359
 
333
 
360
	// _cte_8bit: decode a content transfer encoding of 8bit
334
	// _cte_8bit: decode a content transfer encoding of 8bit
361
	// NOTE: this function is a little bit special. Since the end result will be displayed in
335
	// NOTE: this function is a little bit special. Since the end result will be displayed in
362
	// a web browser _cte_8bit decodes ASCII characters > 127 (the US-ASCII table) into the
336
	// a web browser _cte_8bit decodes ASCII characters > 127 (the US-ASCII table) into the
363
	// html ordinal equivilant, it also ensures that the messages content-type is changed
337
	// html ordinal equivilant, it also ensures that the messages content-type is changed
364
	// to include text/html if it changes anything...
338
	// to include text/html if it changes anything...
365
	function _cte_8bit($data,$simple = FALSE) {
339
	function _cte_8bit($data,$simple = FALSE) {
366
		if ($simple) { return $data; }
340
		if ($simple) { return $data; }
367
		$changed = FALSE;
341
		$changed = FALSE;
368
		$chars = preg_split('//',$data);
342
		$chars = preg_split('//',$data);
369
		while (list($key,$val) = each($chars)) {
343
		while (list($key,$val) = each($chars)) {
370
			if (ord($val) > 127) { $out .= '&#' . ord($val) . ';'; $changed = TRUE; }
344
			if (ord($val) > 127) { $out .= '&#' . ord($val) . ';'; $changed = TRUE; }
371
			else { $out .= $val; }
345
			else { $out .= $val; }
372
		}
346
		}
373
		if ($changed) { $this->headers['content-type'][1] = 'text/html'; }
347
		if ($changed) { $this->headers['content-type'][1] = 'text/html'; }
374
		return $out;
348
		return $out;
375
	}
349
	}
376
 
350
 
377
	// _cte_binary: decode a content transfer encoding of binary
351
	// _cte_binary: decode a content transfer encoding of binary
378
	function _cte_binary($data) { return $data; }
352
	function _cte_binary($data) { return $data; }
379
 
353
 
380
	// _cte_base64: decode a content transfer encoding of base64
354
	// _cte_base64: decode a content transfer encoding of base64
381
	function _cte_base64($data) { return base64_decode($data); }
355
	function _cte_base64($data) { return base64_decode($data); }
382
 
356
 
383
	// _cte_qp: decode a content transfer encoding of quoted_printable
357
	// _cte_qp: decode a content transfer encoding of quoted_printable
384
	function _cte_qp($data) {
358
	function _cte_qp($data) {
385
		// For the time being we'll use PHP's function, it seems to work well enough.
359
		// For the time being we'll use PHP's function, it seems to work well enough.
386
		return quoted_printable_decode($data);
360
		return quoted_printable_decode($data);
387
	}
361
	}
388
    
362
    
389
}
363
}